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