casacore
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: aips2-request@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 //# $Id: ExprFuncNode.h 21277 2012-10-31 16:07:31Z gervandiepen $
27 
28 #ifndef TABLES_EXPRFUNCNODE_H
29 #define TABLES_EXPRFUNCNODE_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/tables/TaQL/ExprNodeRep.h>
34 #include <casacore/casa/Quanta/MVAngle.h>
35 #include <casacore/casa/BasicMath/Math.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# Forward Declarations
40 class TableExprNodeSet;
41 
42 
43 // <summary>
44 // Class representing a function in table select expression
45 // </summary>
46 
47 // <use visibility=local>
48 
49 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
50 // </reviewed>
51 // <prerequisite>
52 //# Classes you should understand before using this one.
53 // <li> <linkto class=TableExprNodeMulti>TableExprNodeMulti</linkto>
54 // </prerequisite>
55 
56 // <synopsis>
57 // This class represents a function in a table select tree.
58 // The <src>rownumber</src> function is represented by class
59 // <linkto class=TableExprNodeRownr>TableExprNodeRownr</linkto>.
60 // The <src>rowid</src> function is represented by class
61 // <linkto class=TableExprNodeRowid>TableExprNodeRowid</linkto>.
62 // The <src>rand</src> function is represented by class
63 // <linkto class=TableExprNodeRandom>TableExprNodeRandom</linkto>.
64 // <p>
65 // When one wants to add a function to the table selection grammar,
66 // the following has to be done:
67 // <ul>
68 // <li> Add the function to the enum below.
69 // <li> Implement the function in the get functions in ExprFuncNode(Array).cc.
70 // <li> Implement the function in the checkOperands in ExprFuncNode.cc.
71 // <li> Declare and define the function in ExprNode.h (for C++ binding).
72 // <li> Add the function to findFunc in TableParse.cc (for TaQL).
73 // </ul>
74 // </synopsis>
75 
76 
78 {
79 public:
80  //# Define the function types.
81  enum FunctionType {
82  piFUNC, //# 0
83  eFUNC, //# 1
84  cFUNC, //# 2
85  // for Int, or Double or Complex returning Bool
86  // (2 is with default tolerance)
87  near2FUNC, //# 3
88  near3FUNC, //# 4
89  nearabs2FUNC, //# 5
90  nearabs3FUNC, //# 6
91  // for Int, Double or DComplex returning Double or Complex
92  sinFUNC, //# 7
93  sinhFUNC, //# 8
94  cosFUNC, //# 9
95  coshFUNC, //# 10
96  expFUNC, //# 11
97  logFUNC, //# 12
98  log10FUNC, //# 13
99  sqrtFUNC, //# 14
100  powFUNC, //# 15
101  conjFUNC, //# 16
102  // for Int, Double or DComplex returning Int, Double or Complex
103  squareFUNC, //# 17
104  cubeFUNC, //# 18
105  minFUNC, //# 19
106  maxFUNC, //# 20
107  // for Int, Double or DComplex returning Int or Double
108  normFUNC, //# 21
109  absFUNC, //# 22
110  // for Int, Double or DComplex returning Double
111  argFUNC, //# 23
112  // for Int, Double, DComplex, Bool or String returning Double
113  realFUNC, //# 24
114  // for Double or DComplex returning Double
115  imagFUNC, //# 25
116  // for Int, Double, Bool or String returning Int (using floor)
117  intFUNC, //# 26
118  // for Int, Double or Complex returning Double or Complex
119  asinFUNC, //# 27
120  acosFUNC, //# 28
121  atanFUNC, //# 29
122  atan2FUNC, //# 30
123  tanFUNC, //# 31
124  tanhFUNC, //# 32
125  // for Int or Double returning Int or Double
126  signFUNC, //# 33
127  roundFUNC, //# 34
128  floorFUNC, //# 35
129  ceilFUNC, //# 36
130  fmodFUNC, //# 37
131  // for DComplex or String returning DComplex
132  complexFUNC, //# 38
133  // for Int, Double or Complex array returning the same
134  arrsumFUNC, //# 39
135  arrsumsFUNC, //# 40
136  runsumFUNC, //# 41
137  boxsumFUNC, //# 42
146  // for Int or Double array returning Int or Double
147  arrminFUNC, //# 51
148  arrminsFUNC, //# 52
149  runminFUNC, //# 53
150  boxminFUNC, //# 54
151  arrmaxFUNC, //# 55
152  arrmaxsFUNC, //# 56
153  runmaxFUNC, //# 57
154  boxmaxFUNC, //# 58
155  // for Int or Double array returning Double
156  arrmeanFUNC, //# 59
157  arrmeansFUNC, //# 60
158  runmeanFUNC, //# 61
159  boxmeanFUNC, //# 62
176  arravdevFUNC, //# 79
178  runavdevFUNC, //# 81
179  boxavdevFUNC, //# 82
180  arrrmsFUNC, //# 83
181  arrrmssFUNC, //# 84
182  runrmsFUNC, //# 85
183  boxrmsFUNC, //# 86
192  // for Bool array returning Bool
193  arranyFUNC, //# 95
194  arranysFUNC, //# 96
195  runanyFUNC, //# 97
196  boxanyFUNC, //# 98
197  arrallFUNC, //# 99
198  arrallsFUNC, //# 100
199  runallFUNC, //# 101
200  boxallFUNC, //# 102
201  // for Bool array returning Int scalar
202  arrntrueFUNC, //# 103
203  arrntruesFUNC, //# 104
204  runntrueFUNC, //# 105
205  boxntrueFUNC, //# 106
206  arrnfalseFUNC, //# 107
207  arrnfalsesFUNC, //# 108
208  runnfalseFUNC, //# 109
209  boxnfalseFUNC, //# 110
210  // for any type returning array of that type
211  arrayFUNC, //# 111
212  transposeFUNC, //# 112
213  areverseFUNC, //# 113
214  resizeFUNC, //# 114
215  diagonalFUNC, //# 115
216  // for Int, Double or DComplex array returning Bool
217  isnanFUNC, //# 116
218  isinfFUNC, //# 117
219  isfiniteFUNC, //# 118
220  // for any array returning Bool scalar
221  isdefFUNC, //# 119
222  isnullFUNC, //# 120
223  iscolFUNC, //# 121
224  iskeyFUNC, //# 122
225  // for any array returning Int scalar
226  ndimFUNC, //# 123
227  nelemFUNC, //# 124
228  // for any array returning Int array
229  shapeFUNC, //# 125
230  // for String
231  strlengthFUNC, //# 126 returning Int
232  upcaseFUNC, //# 127 returning String
233  downcaseFUNC, //# 128 returning String
234  capitalizeFUNC, //# 129 returning String
235  sreverseFUNC, //# 130
236  trimFUNC, //# 131 returning String
237  ltrimFUNC, //# 132 returning String
238  rtrimFUNC, //# 133 returning String
239  substrFUNC, //# 134 returning String
240  replaceFUNC, //# 135 returning String
241  regexFUNC, //# 136 returning TaqlRegex
242  patternFUNC, //# 137 returning TaqlRegex
243  sqlpatternFUNC, //# 138 returning TaqlRegex
244  // for Date
245  datetimeFUNC, //# 139 returning Date
246  mjdtodateFUNC, //# 140 returning Date
247  mjdFUNC, //# 141 returning Double
248  dateFUNC, //# 142 returning Date
249  timeFUNC, //# 143 returning Double (in radians)
250  yearFUNC, //# 144 returning Int
251  monthFUNC, //# 145 returning Int
252  dayFUNC, //# 146 returning Int
253  cmonthFUNC, //# 147 returning String
254  weekdayFUNC, //# 148 returning Int
255  cdowFUNC, //# 149 returning String
256  weekFUNC, //# 150 returning Int
257  ctodFUNC, //# 151 returning String
258  cdateFUNC, //# 152 returning String
259  ctimeFUNC, //# 153 returning String
260  // return values as strings
261  stringFUNC, //# 154
262  // return angles as hms strings
263  hmsFUNC, //# 155
264  // return angles as dms strings
265  dmsFUNC, //# 156
266  // return angles as hms/dms strings
267  hdmsFUNC, //# 157
268  // special function returning a random Double number
269  randFUNC, //# 158
270  // special function returning Int row number
271  rownrFUNC, //# 159
272  // special function returning Int row id (meant for GIVING)
273  rowidFUNC, //# 160
274  // special function resembling if statement
275  iifFUNC, //# 161
276  // angular distance returning radians
277  angdistFUNC, //# 162
278  angdistxFUNC, //# 163
279  // cone search functions, implemented in derived class
280  conesFUNC, //# 164
281  cones3FUNC, //# 165
282  anyconeFUNC, //# 166
283  anycone3FUNC, //# 167
284  findconeFUNC, //# 168
285  findcone3FUNC, //# 169
286  // normalize angle between -pi and pi
287  normangleFUNC, //# 170
288  // for Int, Double, Complex or String returning Bool
289  boolFUNC, //# 171
290  // masked array functions
291  nullarrayFUNC, //# 172
292  marrayFUNC, //# 173
293  arrdataFUNC, //# 174
294  arrmaskFUNC, //# 175
295  negatemaskFUNC, //# 176
296  replmaskedFUNC, //# 177
298  arrflatFUNC, //# 179
299  //# AGGREGATE functions must be the last ones.
300  FirstAggrFunc, //# 180
305  //# Grouping doing aggregation on the fly; reducing to a scalar per group
306  gminFUNC, //# 184
321  //# Grouping doing aggregation on the fly; reducing to an array per group
340  //# Grouping requiring aggregation of rows when getting result
341  gaggrFUNC, //# 215
345  gexpridFUNC, //# special function (can be inserted by TableParse)
346  NRFUNC //# 220 should be last
347  };
348 
349  // Constructor
351  const TableExprNodeSet& source,
352  const vector<TENShPtr>& nodes,
353  const Block<Int>& dtypeOper,
354  const Table& = Table());
355 
356  // Destructor
358 
359  // 'get' Functions to get the desired result of a function
360  // <group>
361  Bool getBool (const TableExprId& id);
362  Int64 getInt (const TableExprId& id);
368  // </group>
369 
370  // Check the data and value types of the operands.
371  // It sets the exptected data and value types of the operands.
372  // Set the value type of the function result and returns
373  // the data type of the function result.
375  ValueType& resVT,
376  Block<Int>& vtypeOper,
377  FunctionType,
378  std::vector<TENShPtr>&);
379 
380  // Fill the result unit in the node.
381  // Adapt the children nodes if their units need to be converted.
382  // It returns a possible scale factor in case result unit is SI (for sqrt).
383  void fillUnits();
384 
385  // Link the children to the node and convert the children
386  // to constants if possible.
387  void fillChildNodes (const vector<TENShPtr>& nodes,
388  const Block<Int>& dtypeOper);
389 
390  // Get possible unit scale factor (needed for sqrt).
391  Double getScale() const
392  { return scale_p; }
393 
394  // Some functions to be used by TableExprNodeFuncArray.
395  // <group>
396  const std::vector<TENShPtr>& operands() const
397  { return operands_p; }
398  std::vector<TENShPtr>& rwOperands()
399  { return operands_p; }
401  { return funcType_p; }
403  { return argDataType_p; }
404  // </group>
405 
406  // Get the possible print format, width, and/or precision.
407  static void getPrintFormat (String& fmt, Int& width, Int& prec,
408  const std::vector<TENShPtr>& operands,
409  const TableExprId& id);
410 
411  // Convert the date and/or time to a string.
412  // <group>
413  static String stringDT (const MVTime& dt, Int prec, MVTime::formatTypes);
414  static String stringDateTime (const MVTime& dt, Int prec);
415  static String stringDate (const MVTime& dt);
416  static String stringTime (const MVTime& dt, Int prec);
417  // </group>
418 
419  // Convert a value to a string.
420  // If <src>fmt</src> is empty, ostringstream is used.
421  // Otherwise the printf-like format is used.
422  // If possible, a double value is converted to radians if formatted as angle.
423  // <group>
424  static String stringValue (Bool val, const String& fmt, Int width);
425  static String stringValue (Int64 val, const String& fmt, Int width);
426  static String stringValue (Double val, const String& fmt,
427  Int width, Int prec,
428  const std::pair<int,int>& mvFormat,
429  const Unit& unit);
430  static String stringValue (const DComplex& val, const String& fmt,
431  Int width, Int prec);
432  static String stringValue (const String& val, const String& fmt,
433  Int width);
434  static String stringValue (const MVTime& val, const String& fmt,
435  Int width,
436  const std::pair<int,int>& mvFormat);
437 
438  // Convert angle to a string (hms or dms).
439  // <group>
440  static String stringAngle (double val, Int prec,
441  MVAngle::formatTypes type);
442  static String stringHMS (double val, Int prec);
443  static String stringDMS (double val, Int prec);
444  // </group>
445 
446  // Get the MVTime/Angle format and optional precision.
447  // 0,0 is returned if empty or unknown format.
448  static std::pair<int,int> getMVFormat (const String& fmt);
449 
450  // Get the angular distance between two positions on a sphere.
451  static double angdist (double ra1, double dec1, double ra2, double dec2)
452  { return acos (sin(dec1)*sin(dec2) + cos(dec1)*cos(dec2)*cos(ra1-ra2)); }
453 
454  // Read a string as an integer, double, complex or bool.
455  static Int64 string2Int (const String&);
456  static Double string2Real (const String&);
457  static DComplex string2Complex (const String&);
458  static Bool string2Bool (const String&);
459 
460 private:
461  // Try if the function gives a constant result.
462  // If so, set the expression type to Constant.
463  void tryToConst();
464 
465  // Make the units of nodes from <src>starg</src> till <src>endarg</src>
466  // equal. Return the unit found.
467  static const Unit& makeEqualUnits (std::vector<TENShPtr>& nodes,
468  uInt starg, uInt endarg);
469 
470  //# Data members.
471  FunctionType funcType_p; // which function
472  NodeDataType argDataType_p; // common argument data type
473  Double scale_p; // possible scaling for unit conversion
474  // (needed for sqrt)
475  Table table_p; // table (for iscolumn and iskeyword)
476 };
477 
478 
479 } //# NAMESPACE CASACORE - END
480 
481 #endif
formatTypes
Format types.
Definition: MVAngle.h:251
formatTypes
Format types.
Definition: MVTime.h:276
String: the storage and methods of handling collections of characters.
Definition: String.h:225
static Double string2Real(const String &)
Double getDouble(const TableExprId &id)
Double getScale() const
Get possible unit scale factor (needed for sqrt).
Definition: ExprFuncNode.h:391
static double angdist(double ra1, double dec1, double ra2, double dec2)
Get the angular distance between two positions on a sphere.
Definition: ExprFuncNode.h:451
String getString(const TableExprId &id)
void fillUnits()
Fill the result unit in the node.
static const Unit & makeEqualUnits(std::vector< TENShPtr > &nodes, uInt starg, uInt endarg)
Make the units of nodes from starg till endarg equal.
static String stringValue(const String &val, const String &fmt, Int width)
TaqlRegex getRegex(const TableExprId &id)
static std::pair< int, int > getMVFormat(const String &fmt)
Get the MVTime/Angle format and optional precision.
static String stringDateTime(const MVTime &dt, Int prec)
NodeDataType argDataType() const
Definition: ExprFuncNode.h:402
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
Definition: ExprFuncNode.h:92
@ complexFUNC
for DComplex or String returning DComplex
Definition: ExprFuncNode.h:132
@ isdefFUNC
for any array returning Bool scalar
Definition: ExprFuncNode.h:221
@ stringFUNC
return values as strings
Definition: ExprFuncNode.h:261
@ boolFUNC
for Int, Double, Complex or String returning Bool
Definition: ExprFuncNode.h:289
@ angdistFUNC
angular distance returning radians
Definition: ExprFuncNode.h:277
@ iifFUNC
special function resembling if statement
Definition: ExprFuncNode.h:275
@ realFUNC
for Int, Double, DComplex, Bool or String returning Double
Definition: ExprFuncNode.h:113
@ hmsFUNC
return angles as hms strings
Definition: ExprFuncNode.h:263
@ arrsumFUNC
for Int, Double or Complex array returning the same
Definition: ExprFuncNode.h:134
@ asinFUNC
for Int, Double or Complex returning Double or Complex
Definition: ExprFuncNode.h:119
@ arranyFUNC
for Bool array returning Bool
Definition: ExprFuncNode.h:193
@ arrmeanFUNC
for Int or Double array returning Double
Definition: ExprFuncNode.h:156
@ rowidFUNC
special function returning Int row id (meant for GIVING)
Definition: ExprFuncNode.h:273
@ arrayFUNC
for any type returning array of that type
Definition: ExprFuncNode.h:211
@ shapeFUNC
for any array returning Int array
Definition: ExprFuncNode.h:229
@ near2FUNC
for Int, or Double or Complex returning Bool (2 is with default tolerance)
Definition: ExprFuncNode.h:87
@ conesFUNC
cone search functions, implemented in derived class
Definition: ExprFuncNode.h:280
@ isnanFUNC
for Int, Double or DComplex array returning Bool
Definition: ExprFuncNode.h:217
@ intFUNC
for Int, Double, Bool or String returning Int (using floor)
Definition: ExprFuncNode.h:117
@ normangleFUNC
normalize angle between -pi and pi
Definition: ExprFuncNode.h:287
@ rownrFUNC
special function returning Int row number
Definition: ExprFuncNode.h:271
@ argFUNC
for Int, Double or DComplex returning Double
Definition: ExprFuncNode.h:111
@ arrminFUNC
for Int or Double array returning Int or Double
Definition: ExprFuncNode.h:147
@ nullarrayFUNC
masked array functions
Definition: ExprFuncNode.h:291
@ hdmsFUNC
return angles as hms/dms strings
Definition: ExprFuncNode.h:267
@ arrntrueFUNC
for Bool array returning Int scalar
Definition: ExprFuncNode.h:202
@ imagFUNC
for Double or DComplex returning Double
Definition: ExprFuncNode.h:115
@ ndimFUNC
for any array returning Int scalar
Definition: ExprFuncNode.h:226
@ randFUNC
special function returning a random Double number
Definition: ExprFuncNode.h:269
@ normFUNC
for Int, Double or DComplex returning Int or Double
Definition: ExprFuncNode.h:108
@ dmsFUNC
return angles as dms strings
Definition: ExprFuncNode.h:265
@ signFUNC
for Int or Double returning Int or Double
Definition: ExprFuncNode.h:126
@ squareFUNC
for Int, Double or DComplex returning Int, Double or Complex
Definition: ExprFuncNode.h:103
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)
const std::vector< TENShPtr > & operands() const
Some functions to be used by TableExprNodeFuncArray.
Definition: ExprFuncNode.h:396
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)
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.
TableExprFuncNode(FunctionType, NodeDataType, ValueType, const TableExprNodeSet &source, const vector< TENShPtr > &nodes, const Block< Int > &dtypeOper, const Table &=Table())
Constructor.
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)
Definition: ExprFuncNode.h:475
static String stringValue(const DComplex &val, const String &fmt, Int width, Int prec)
static String stringDate(const MVTime &dt)
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
Definition: ExprFuncNode.h:400
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)
std::vector< TENShPtr > & rwOperands()
Definition: ExprFuncNode.h:398
MVTime getDate(const TableExprId &id)
Bool getBool(const TableExprId &id)
'get' Functions to get the desired result of a function
Int64 getInt(const TableExprId &id)
Abstract base class for a node having multiple child nodes.
Definition: ExprNodeRep.h:661
std::vector< TENShPtr > operands_p
Definition: ExprNodeRep.h:698
const Unit & unit() const
Get the unit.
Definition: ExprNodeRep.h:731
NodeDataType
Define the data types of a node.
Definition: ExprNodeRep.h:161
ValueType
Define the value types.
Definition: ExprNodeRep.h:176
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:311
this file contains all the compiler specific defines
Definition: mainpage.dox:28
LatticeExprNode acos(const LatticeExprNode &expr)
unsigned int uInt
Definition: aipstype.h:51
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:38
int Int
Definition: aipstype.h:50
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
LatticeExprNode cos(const LatticeExprNode &expr)
double Double
Definition: aipstype.h:55