casacore
Loading...
Searching...
No Matches
ExprFuncNode.h
Go to the documentation of this file.
1// # ExprFuncNode.h: Class representing a function in table select expression
2// # Copyright (C) 1994,1995,1996,1997,1998,2000,2001,2003
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef TABLES_EXPRFUNCNODE_H
27#define TABLES_EXPRFUNCNODE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/TaQL/ExprNodeRep.h>
32#include <casacore/casa/Quanta/MVAngle.h>
33#include <casacore/casa/BasicMath/Math.h>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// # Forward Declarations
39
40// <summary>
41// Class representing a function in table select expression
42// </summary>
43
44// <use visibility=local>
45
46// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
47// </reviewed>
48// <prerequisite>
49// # Classes you should understand before using this one.
50// <li> <linkto class=TableExprNodeMulti>TableExprNodeMulti</linkto>
51// </prerequisite>
52
53// <synopsis>
54// This class represents a function in a table select tree.
55// The <src>rownumber</src> function is represented by class
56// <linkto class=TableExprNodeRownr>TableExprNodeRownr</linkto>.
57// The <src>rowid</src> function is represented by class
58// <linkto class=TableExprNodeRowid>TableExprNodeRowid</linkto>.
59// The <src>rand</src> function is represented by class
60// <linkto class=TableExprNodeRandom>TableExprNodeRandom</linkto>.
61// <p>
62// When one wants to add a function to the table selection grammar,
63// the following has to be done:
64// <ul>
65// <li> Add the function to the enum below.
66// <li> Implement the function in the get functions in ExprFuncNode(Array).cc.
67// <li> Implement the function in the checkOperands in ExprFuncNode.cc.
68// <li> Declare and define the function in ExprNode.h (for C++ binding).
69// <li> Add the function to findFunc in TableParse.cc (for TaQL).
70// </ul>
71// </synopsis>
72
74 public:
75 // # Define the function types.
77 piFUNC, // # 0
78 eFUNC, // # 1
79 cFUNC, // # 2
80 // for Int, or Double or Complex returning Bool
81 // (2 is with default tolerance)
82 near2FUNC, // # 3
83 near3FUNC, // # 4
86 // for Int, Double or DComplex returning Double or Complex
87 sinFUNC, // # 7
88 sinhFUNC, // # 8
89 cosFUNC, // # 9
90 coshFUNC, // # 10
91 expFUNC, // # 11
92 logFUNC, // # 12
93 log10FUNC, // # 13
94 sqrtFUNC, // # 14
95 powFUNC, // # 15
96 conjFUNC, // # 16
97 // for Int, Double or DComplex returning Int, Double or Complex
98 squareFUNC, // # 17
99 cubeFUNC, // # 18
100 minFUNC, // # 19
101 maxFUNC, // # 20
102 // for Int, Double or DComplex returning Int or Double
103 normFUNC, // # 21
104 absFUNC, // # 22
105 // for Int, Double or DComplex returning Double
106 argFUNC, // # 23
107 // for Int, Double, DComplex, Bool or String returning Double
108 realFUNC, // # 24
109 // for Double or DComplex returning Double
110 imagFUNC, // # 25
111 // for Int, Double, Bool or String returning Int (using floor)
112 intFUNC, // # 26
113 // for Int, Double or Complex returning Double or Complex
114 asinFUNC, // # 27
115 acosFUNC, // # 28
116 atanFUNC, // # 29
117 atan2FUNC, // # 30
118 tanFUNC, // # 31
119 tanhFUNC, // # 32
120 // for Int or Double returning Int or Double
121 signFUNC, // # 33
122 roundFUNC, // # 34
123 floorFUNC, // # 35
124 ceilFUNC, // # 36
125 fmodFUNC, // # 37
126 // for DComplex or String returning DComplex
127 complexFUNC, // # 38
128 // for Int, Double or Complex array returning the same
129 arrsumFUNC, // # 39
130 arrsumsFUNC, // # 40
131 runsumFUNC, // # 41
132 boxsumFUNC, // # 42
141 // for Int or Double array returning Int or Double
142 arrminFUNC, // # 51
143 arrminsFUNC, // # 52
144 runminFUNC, // # 53
145 boxminFUNC, // # 54
146 arrmaxFUNC, // # 55
147 arrmaxsFUNC, // # 56
148 runmaxFUNC, // # 57
149 boxmaxFUNC, // # 58
150 // for Int or Double array returning Double
151 arrmeanFUNC, // # 59
153 runmeanFUNC, // # 61
154 boxmeanFUNC, // # 62
175 arrrmsFUNC, // # 83
176 arrrmssFUNC, // # 84
177 runrmsFUNC, // # 85
178 boxrmsFUNC, // # 86
187 // for Bool array returning Bool
188 arranyFUNC, // # 95
189 arranysFUNC, // # 96
190 runanyFUNC, // # 97
191 boxanyFUNC, // # 98
192 arrallFUNC, // # 99
193 arrallsFUNC, // # 100
194 runallFUNC, // # 101
195 boxallFUNC, // # 102
196 // for Bool array returning Int scalar
197 arrntrueFUNC, // # 103
199 runntrueFUNC, // # 105
200 boxntrueFUNC, // # 106
205 // for any type returning array of that type
206 arrayFUNC, // # 111
208 areverseFUNC, // # 113
209 resizeFUNC, // # 114
210 diagonalFUNC, // # 115
211 // for Int, Double or DComplex array returning Bool
212 isnanFUNC, // # 116
213 isinfFUNC, // # 117
214 isfiniteFUNC, // # 118
215 // for any array returning Bool scalar
216 isdefFUNC, // # 119
217 isnullFUNC, // # 120
218 iscolFUNC, // # 121
219 iskeyFUNC, // # 122
220 // for any array returning Int scalar
221 ndimFUNC, // # 123
222 nelemFUNC, // # 124
223 // for any array returning Int array
224 shapeFUNC, // # 125
225 // for String
226 strlengthFUNC, // # 126 returning Int
227 upcaseFUNC, // # 127 returning String
228 downcaseFUNC, // # 128 returning String
229 capitalizeFUNC, // # 129 returning String
230 sreverseFUNC, // # 130
231 trimFUNC, // # 131 returning String
232 ltrimFUNC, // # 132 returning String
233 rtrimFUNC, // # 133 returning String
234 substrFUNC, // # 134 returning String
235 replaceFUNC, // # 135 returning String
236 regexFUNC, // # 136 returning TaqlRegex
237 patternFUNC, // # 137 returning TaqlRegex
238 sqlpatternFUNC, // # 138 returning TaqlRegex
239 // for Date
240 datetimeFUNC, // # 139 returning Date
241 mjdtodateFUNC, // # 140 returning Date
242 mjdFUNC, // # 141 returning Double
243 dateFUNC, // # 142 returning Date
244 timeFUNC, // # 143 returning Double (in radians)
245 yearFUNC, // # 144 returning Int
246 monthFUNC, // # 145 returning Int
247 dayFUNC, // # 146 returning Int
248 cmonthFUNC, // # 147 returning String
249 weekdayFUNC, // # 148 returning Int
250 cdowFUNC, // # 149 returning String
251 weekFUNC, // # 150 returning Int
252 ctodFUNC, // # 151 returning String
253 cdateFUNC, // # 152 returning String
254 ctimeFUNC, // # 153 returning String
255 // return values as strings
256 stringFUNC, // # 154
257 // return angles as hms strings
258 hmsFUNC, // # 155
259 // return angles as dms strings
260 dmsFUNC, // # 156
261 // return angles as hms/dms strings
262 hdmsFUNC, // # 157
263 // special function returning a random Double number
264 randFUNC, // # 158
265 // special function returning Int row number
266 rownrFUNC, // # 159
267 // special function returning Int row id (meant for GIVING)
268 rowidFUNC, // # 160
269 // special function resembling if statement
270 iifFUNC, // # 161
271 // angular distance returning radians
272 angdistFUNC, // # 162
273 angdistxFUNC, // # 163
274 // cone search functions, implemented in derived class
275 conesFUNC, // # 164
276 cones3FUNC, // # 165
277 anyconeFUNC, // # 166
278 anycone3FUNC, // # 167
279 findconeFUNC, // # 168
281 // normalize angle between -pi and pi
283 // for Int, Double, Complex or String returning Bool
284 boolFUNC, // # 171
285 // masked array functions
287 marrayFUNC, // # 173
288 arrdataFUNC, // # 174
289 arrmaskFUNC, // # 175
293 arrflatFUNC, // # 179
294 // # AGGREGATE functions must be the last ones.
300 // # Grouping doing aggregation on the fly; reducing to a scalar per group
301 gminFUNC, // # 184
316 // # Grouping doing aggregation on the fly; reducing to an array per group
335 // # Grouping requiring aggregation of rows when getting result
336 gaggrFUNC, // # 215
340 gexpridFUNC, // # special function (can be inserted by TableParse)
341 NRFUNC // # 220 should be last
342 };
343
344 // Constructor
346 const vector<TENShPtr>& nodes, const Block<Int>& dtypeOper,
347 const TableExprInfo& = TableExprInfo());
348
349 // Destructor
351
352 // 'get' Functions to get the desired result of a function
353 // <group>
357 DComplex getDComplex(const TableExprId& id);
361 // </group>
362
363 // Check the data and value types of the operands.
364 // It sets the exptected data and value types of the operands.
365 // Set the value type of the function result and returns
366 // the data type of the function result.
367 static NodeDataType checkOperands(Block<Int>& dtypeOper, ValueType& resVT, Block<Int>& vtypeOper,
368 FunctionType, std::vector<TENShPtr>&);
369
370 // Fill the result unit in the node.
371 // Adapt the children nodes if their units need to be converted.
372 // It returns a possible scale factor in case result unit is SI (for sqrt).
373 void fillUnits();
374
375 // Link the children to the node and convert the children
376 // to constants if possible.
377 void fillChildNodes(const vector<TENShPtr>& nodes, const Block<Int>& dtypeOper);
378
379 // Get possible unit scale factor (needed for sqrt).
380 Double getScale() const { return scale_p; }
381
382 // Some functions to be used by TableExprNodeFuncArray.
383 // <group>
384 const std::vector<TENShPtr>& operands() const { return operands_p; }
385 std::vector<TENShPtr>& rwOperands() { return operands_p; }
386 FunctionType funcType() const { return funcType_p; }
388 // </group>
389
390 // Get the possible print format, width, and/or precision.
391 static void getPrintFormat(String& fmt, Int& width, Int& prec,
392 const std::vector<TENShPtr>& operands, const TableExprId& id);
393
394 // Convert the date and/or time to a string.
395 // <group>
396 static String stringDT(const MVTime& dt, Int prec, MVTime::formatTypes);
397 static String stringDateTime(const MVTime& dt, Int prec);
398 static String stringDate(const MVTime& dt);
399 static String stringTime(const MVTime& dt, Int prec);
400 // </group>
401
402 // Convert a value to a string.
403 // If <src>fmt</src> is empty, ostringstream is used.
404 // Otherwise the printf-like format is used.
405 // If possible, a double value is converted to radians if formatted as angle.
406 // <group>
407 static String stringValue(Bool val, const String& fmt, Int width);
408 static String stringValue(Int64 val, const String& fmt, Int width);
409 static String stringValue(Double val, const String& fmt, Int width, Int prec,
410 const std::pair<int, int>& mvFormat, const Unit& unit);
411 static String stringValue(const DComplex& val, const String& fmt, Int width, Int prec);
412 static String stringValue(const String& val, const String& fmt, Int width);
413 static String stringValue(const MVTime& val, const String& fmt, Int width,
414 const std::pair<int, int>& mvFormat);
415
416 // Convert angle to a string (hms or dms).
417 // <group>
418 static String stringAngle(double val, Int prec, MVAngle::formatTypes type);
419 static String stringHMS(double val, Int prec);
420 static String stringDMS(double val, Int prec);
421 // </group>
422
423 // Get the MVTime/Angle format and optional precision.
424 // 0,0 is returned if empty or unknown format.
425 static std::pair<int, int> getMVFormat(const String& fmt);
426
427 // Get the angular distance between two positions on a sphere.
428 static double angdist(double ra1, double dec1, double ra2, double dec2) {
429 return acos(sin(dec1) * sin(dec2) + cos(dec1) * cos(dec2) * cos(ra1 - ra2));
430 }
431
432 // Read a string as an integer, double, complex or bool.
433 static Int64 string2Int(const String&);
434 static Double string2Real(const String&);
435 static DComplex string2Complex(const String&);
436 static Bool string2Bool(const String&);
437
438 private:
439 // Try if the function gives a constant result.
440 // If so, set the expression type to Constant.
442
443 // Make the units of nodes from <src>starg</src> till <src>endarg</src>
444 // equal. Return the unit found.
445 static const Unit& makeEqualUnits(std::vector<TENShPtr>& nodes, uInt starg, uInt endarg);
446
447 // # Data members.
448 FunctionType funcType_p; // which function
449 NodeDataType argDataType_p; // common argument data type
450 Double scale_p; // possible scaling for unit conversion
451 // (needed for sqrt)
452 Table table_p; // table (for iscolumn and iskeyword)
453};
454
455} // namespace casacore
456
457#endif
formatTypes
Format types.
Definition MVAngle.h:246
formatTypes
Format types.
Definition MVTime.h:271
String: the storage and methods of handling collections of characters.
Definition String.h:355
static Double string2Real(const String &)
Double getDouble(const TableExprId &id)
Double getScale() const
Get possible unit scale factor (needed for sqrt).
static double angdist(double ra1, double dec1, double ra2, double dec2)
Get the angular distance between two positions on a sphere.
String getString(const TableExprId &id)
void fillUnits()
Fill the result unit in the node.
static String stringValue(const String &val, const String &fmt, Int width)
TaqlRegex getRegex(const TableExprId &id)
static String stringDateTime(const MVTime &dt, Int prec)
NodeDataType argDataType() const
static String stringValue(const MVTime &val, const String &fmt, Int width, const std::pair< int, int > &mvFormat)
@ sinFUNC
for Int, Double or DComplex returning Double or Complex
@ complexFUNC
for DComplex or String returning DComplex
@ isdefFUNC
for any array returning Bool scalar
@ stringFUNC
return values as strings
@ boolFUNC
for Int, Double, Complex or String returning Bool
@ angdistFUNC
angular distance returning radians
@ iifFUNC
special function resembling if statement
@ realFUNC
for Int, Double, DComplex, Bool or String returning Double
@ hmsFUNC
return angles as hms strings
@ arrsumFUNC
for Int, Double or Complex array returning the same
@ asinFUNC
for Int, Double or Complex returning Double or Complex
@ arranyFUNC
for Bool array returning Bool
@ arrmeanFUNC
for Int or Double array returning Double
@ rowidFUNC
special function returning Int row id (meant for GIVING)
@ arrayFUNC
for any type returning array of that type
@ shapeFUNC
for any array returning Int array
@ near2FUNC
for Int, or Double or Complex returning Bool (2 is with default tolerance)
@ conesFUNC
cone search functions, implemented in derived class
@ isnanFUNC
for Int, Double or DComplex array returning Bool
@ intFUNC
for Int, Double, Bool or String returning Int (using floor)
@ normangleFUNC
normalize angle between -pi and pi
@ rownrFUNC
special function returning Int row number
@ argFUNC
for Int, Double or DComplex returning Double
@ arrminFUNC
for Int or Double array returning Int or Double
@ nullarrayFUNC
masked array functions
@ hdmsFUNC
return angles as hms/dms strings
@ arrntrueFUNC
for Bool array returning Int scalar
@ imagFUNC
for Double or DComplex returning Double
@ ndimFUNC
for any array returning Int scalar
@ randFUNC
special function returning a random Double number
@ normFUNC
for Int, Double or DComplex returning Int or Double
@ dmsFUNC
return angles as dms strings
@ signFUNC
for Int or Double returning Int or Double
@ squareFUNC
for Int, Double or DComplex returning Int, Double or Complex
static String stringValue(Bool val, const String &fmt, Int width)
Convert a value to a string.
static String stringValue(Int64 val, const String &fmt, Int width)
static String stringValue(Double val, const String &fmt, Int width, Int prec, const std::pair< int, int > &mvFormat, const Unit &unit)
DComplex getDComplex(const TableExprId &id)
std::vector< TENShPtr > & rwOperands()
static String stringAngle(double val, Int prec, MVAngle::formatTypes type)
Convert angle to a string (hms or dms).
static Int64 string2Int(const String &)
Read a string as an integer, double, complex or bool.
static String stringDT(const MVTime &dt, Int prec, MVTime::formatTypes)
Convert the date and/or time to a string.
Table table_p
(needed for sqrt)
TableExprFuncNode(FunctionType, NodeDataType, ValueType, const TableExprNodeSet &source, const vector< TENShPtr > &nodes, const Block< Int > &dtypeOper, const TableExprInfo &=TableExprInfo())
Constructor.
static String stringValue(const DComplex &val, const String &fmt, Int width, Int prec)
static String stringDate(const MVTime &dt)
const std::vector< TENShPtr > & operands() const
Some functions to be used by TableExprNodeFuncArray.
static NodeDataType checkOperands(Block< Int > &dtypeOper, ValueType &resVT, Block< Int > &vtypeOper, FunctionType, std::vector< TENShPtr > &)
Check the data and value types of the operands.
static Bool string2Bool(const String &)
static String stringDMS(double val, Int prec)
void fillChildNodes(const vector< TENShPtr > &nodes, const Block< Int > &dtypeOper)
Link the children to the node and convert the children to constants if possible.
static String stringHMS(double val, Int prec)
FunctionType funcType() const
static void getPrintFormat(String &fmt, Int &width, Int &prec, const std::vector< TENShPtr > &operands, const TableExprId &id)
Get the possible print format, width, and/or precision.
static DComplex string2Complex(const String &)
void tryToConst()
Try if the function gives a constant result.
static String stringTime(const MVTime &dt, Int prec)
static const Unit & makeEqualUnits(std::vector< TENShPtr > &nodes, uInt starg, uInt endarg)
Make the units of nodes from starg till endarg equal.
MVTime getDate(const TableExprId &id)
Bool getBool(const TableExprId &id)
'get' Functions to get the desired result of a function
static std::pair< int, int > getMVFormat(const String &fmt)
Get the MVTime/Angle format and optional precision.
Int64 getInt(const TableExprId &id)
Class to connect a Table and its alias name.
TableExprNodeMulti(NodeDataType, ValueType, OperType, const TableExprNodeRep &source)
Constructor.
std::vector< TENShPtr > operands_p
const Unit & unit() const
Get the unit.
NodeDataType
Define the data types of a node.
ValueType
Define the value types.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
LatticeExprNode acos(const LatticeExprNode &expr)
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode sin(const LatticeExprNode &expr)
Numerical 1-argument functions.
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode cos(const LatticeExprNode &expr)
double Double
Definition aipstype.h:53