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  argFUNC, //# 23
110  // for Int, Double, DComplex, Bool or String returning Double
111  realFUNC, //# 24
112  imagFUNC, //# 25
113  // for Int, Double, Bool or String returning Int (using floor)
114  intFUNC, //# 26
115  // for Int or Double returning Double
116  asinFUNC, //# 27
117  acosFUNC, //# 28
118  atanFUNC, //# 29
119  atan2FUNC, //# 30
120  tanFUNC, //# 31
121  tanhFUNC, //# 32
122  // for Int or Double returning Int or Double
123  signFUNC, //# 33
124  roundFUNC, //# 34
125  floorFUNC, //# 35
126  ceilFUNC, //# 36
127  fmodFUNC, //# 37
128  // for Int, Double or DComplex returning DComplex
129  complexFUNC, //# 38
130  // for Int, Double or Complex array returning the same
131  arrsumFUNC, //# 39
132  arrsumsFUNC, //# 40
137  // for Int or Double array returning Int or Double
138  arrminFUNC, //# 45
139  arrminsFUNC, //# 46
140  runminFUNC, //# 47
141  boxminFUNC, //# 48
142  arrmaxFUNC, //# 49
143  arrmaxsFUNC, //# 50
144  runmaxFUNC, //# 51
145  boxmaxFUNC, //# 52
146  // for Int or Double array returning Double
147  arrmeanFUNC, //# 53
148  arrmeansFUNC, //# 54
149  runmeanFUNC, //# 55
150  boxmeanFUNC, //# 56
159  arravdevFUNC, //# 65
161  runavdevFUNC, //# 67
162  boxavdevFUNC, //# 68
163  arrrmsFUNC, //# 69
164  arrrmssFUNC, //# 70
165  runrmsFUNC, //# 71
166  boxrmsFUNC, //# 72
173  // for Bool array returning Bool
174  anyFUNC, //# 79
175  anysFUNC, //# 80
176  runanyFUNC, //# 81
177  boxanyFUNC, //# 82
178  allFUNC, //# 83
179  allsFUNC, //# 84
180  runallFUNC, //# 85
181  boxallFUNC, //# 86
182  // for Bool array returning Int scalar
183  ntrueFUNC, //# 87
184  ntruesFUNC, //# 88
185  nfalseFUNC, //# 89
186  nfalsesFUNC, //# 90
187  // for any type returning array of that type
188  arrayFUNC, //# 91
190  resizeFUNC, //# 93
191  diagonalFUNC, //# 94
192  // for Int, Double or DComplex array returning Bool
193  isnanFUNC, //# 95
194  isinfFUNC, //# 96
195  isfiniteFUNC, //# 97
196  // for any array returning Bool scalar
197  isdefFUNC, //# 98
198  isnullFUNC, //# 99
199  iscolFUNC, //# 100
200  iskeyFUNC, //# 101
201  // for any array returning Int scalar
202  ndimFUNC, //# 102
203  nelemFUNC, //# 103
204  // for any array returning Int array
205  shapeFUNC, //# 104
206  // for String
207  strlengthFUNC, //# 105 returning Int
208  upcaseFUNC, //# 106 returning String
209  downcaseFUNC, //# 107 returning String
210  capitalizeFUNC, //# 108 returning String
211  trimFUNC, //# 109 returning String
212  ltrimFUNC, //# 110 returning String
213  rtrimFUNC, //# 111 returning String
214  substrFUNC, //# 112 returning String
215  replaceFUNC, //# 113 returning String
216  regexFUNC, //# 114 returning TaqlRegex
217  patternFUNC, //# 115 returning TaqlRegex
218  sqlpatternFUNC, //# 116 returning TaqlRegex
219  // for Date
220  datetimeFUNC, //# 117 returning Date
221  mjdtodateFUNC, //# 118 returning Date
222  mjdFUNC, //# 119 returning Double
223  dateFUNC, //# 120 returning Date
224  timeFUNC, //# 121 returning Double (in radians)
225  yearFUNC, //# 122 returning Int
226  monthFUNC, //# 123 returning Int
227  dayFUNC, //# 124 returning Int
228  cmonthFUNC, //# 125 returning String
229  weekdayFUNC, //# 126 returning Int
230  cdowFUNC, //# 127 returning String
231  weekFUNC, //# 128 returning Int
232  ctodFUNC, //# 129 returning String
233  cdateFUNC, //# 130 returning String
234  ctimeFUNC, //# 131 returning String
235  // return values as strings
236  stringFUNC, //# 132
237  // return angles as hms strings
238  hmsFUNC, //# 133
239  // return angles as dms strings
240  dmsFUNC, //# 134
241  // return angles as hms/dms strings
242  hdmsFUNC, //# 135
243  // special function returning a random Double number
244  randFUNC, //# 136
245  // special function returning Int row number
246  rownrFUNC, //# 137
247  // special function returning Int row id (meant for GIVING)
248  rowidFUNC, //# 138
249  // special function resembling if statement
250  iifFUNC, //# 139
251  // angular distance returning radians
252  angdistFUNC, //# 140
253  angdistxFUNC, //# 141
254  // cone search functions, implemented in derived class
255  conesFUNC, //# 142
256  cones3FUNC, //# 143
257  anyconeFUNC, //# 144
258  anycone3FUNC, //# 145
259  findconeFUNC, //# 146
260  findcone3FUNC, //# 147
261  // for Int, Double, Complex or String returning Bool
262  boolFUNC, //# 148
263  // masked array functions
264  nullarrayFUNC, //# 149
265  marrayFUNC, //# 150
266  arrdataFUNC, //# 151
267  arrmaskFUNC, //# 152
268  negatemaskFUNC, //# 153
269  replmaskedFUNC, //# 154
271  arrflatFUNC, //# 156
272  //# AGGREGATE functions must be the last ones.
273  FirstAggrFunc, //# 157
278  //# Grouping doing aggregation on the fly; reducing to a scalar per group
279  gminFUNC, //# 161
292  //# Grouping doing aggregation on the fly; reducing to an array per group
309  //# Grouping requiring aggregation of rows when getting result
310  gaggrFUNC, //# 188
314  gexpridFUNC, //# special function (can be inserted by TableParse)
315  NRFUNC //# 193 should be last
316  };
317 
318  // Constructor
320  const TableExprNodeSet& source,
321  const Table& = Table());
322 
323  // Destructor
325 
326  // 'get' Functions to get the desired result of a function
327  // <group>
328  Bool getBool (const TableExprId& id);
329  Int64 getInt (const TableExprId& id);
330  Double getDouble (const TableExprId& id);
331  DComplex getDComplex (const TableExprId& id);
332  String getString (const TableExprId& id);
333  TaqlRegex getRegex (const TableExprId& id);
334  MVTime getDate (const TableExprId& id);
335  // </group>
336 
337  // Check the data and value types of the operands.
338  // It sets the exptected data and value types of the operands.
339  // Set the value type of the function result and returns
340  // the data type of the function result.
341  static NodeDataType checkOperands (Block<Int>& dtypeOper,
342  ValueType& resVT,
343  Block<Int>& vtypeOper,
344  FunctionType,
346 
347  // Fill the result unit in the node.
348  // Adapt the children nodes if their units need to be converted.
349  // It returns a possible scale factor in case result unit is SI (for sqrt).
350  static Double fillUnits (TableExprNodeRep* node,
352  FunctionType func);
353 
354  // Link the children to the node and convert the children
355  // to constants if possible. Also convert the node to
356  // constant if possible.
357  static TableExprNodeRep* fillNode (TableExprFuncNode* thisNode,
359  const Block<Int>& dtypeOper);
360 
361  // Link the children to the node and convert the children
362  // to constants if possible.
363  static void fillChildNodes (TableExprFuncNode* thisNode,
365  const Block<Int>& dtypeOper);
366 
367  // Set unit scale factor (needed for sqrt).
368  void setScale (Double scale)
369  { scale_p = scale; }
370 
371  // Get possible unit scale factor (needed for sqrt).
372  Double getScale() const
373  { return scale_p; }
374 
375  // Some functions to be used by TableExprNodeFuncArray.
376  // <group>
378  { return operands_p; }
380  { return operands_p; }
382  { return funcType_p; }
384  { return argDataType_p; }
385  // </group>
386 
387  // Get the possible print format, width, and/or precision.
388  static void getPrintFormat (String& fmt, Int& width, Int& prec,
390  const TableExprId& id);
391 
392  // Convert the date and/or time to a string.
393  // <group>
394  static String stringDT (const MVTime& dt, Int prec, MVTime::formatTypes);
395  static String stringDateTime (const MVTime& dt, Int prec);
396  static String stringDate (const MVTime& dt);
397  static String stringTime (const MVTime& dt, Int prec);
398  // </group>
399 
400  // Convert a value to a string.
401  // If <src>fmt</src> is empty, ostringstream is used.
402  // Otherwise the printf-like format is used.
403  // If possible, a double value is converted to radians if formatted as angle.
404  // <group>
405  static String stringValue (Bool val, const String& fmt, Int width);
406  static String stringValue (Int64 val, const String& fmt, Int width);
407  static String stringValue (Double val, const String& fmt,
408  Int width, Int prec,
409  const std::pair<int,int>& mvFormat,
410  const Unit& unit);
411  static String stringValue (const DComplex& val, const String& fmt,
412  Int width, Int prec);
413  static String stringValue (const String& val, const String& fmt,
414  Int width);
415  static String stringValue (const MVTime& val, const String& fmt,
416  Int width,
417  const std::pair<int,int>& mvFormat);
418 
419  // Convert angle to a string (hms or dms).
420  // <group>
421  static String stringAngle (double val, Int prec,
422  MVAngle::formatTypes type);
423  static String stringHMS (double val, Int prec);
424  static String stringDMS (double val, Int prec);
425  // </group>
426 
427  // Get the MVTime/Angle format and optional precision.
428  // 0,0 is returned if empty or unknown format.
429  static std::pair<int,int> getMVFormat (const String& fmt);
430 
431  // Get the angular distance between two positions on a sphere.
432  static double angdist (double ra1, double dec1, double ra2, double dec2)
433  { return acos (sin(dec1)*sin(dec2) + cos(dec1)*cos(dec2)*cos(ra1-ra2)); }
434 
435  // Read a string as an integer, double, complex or bool.
436  static Int64 string2Int (const String&);
437  static Double string2Real (const String&);
438  static DComplex string2Complex (const String&);
439  static Bool string2Bool (const String&);
440 
441 private:
442  // Try if the function gives a constant result.
443  // If so, set the expression type to Constant.
444  void tryToConst();
445 
446  // Make the units of nodes from <src>starg</src> till <src>endarg</src>
447  // equal. Return the unit found.
448  static const Unit& makeEqualUnits (PtrBlock<TableExprNodeRep*>& nodes,
449  uInt starg, uInt endarg);
450 
451  //# Data members.
452  FunctionType funcType_p; // which function
453  NodeDataType argDataType_p; // common argument data type
454  Double scale_p; // possible scaling for unit conversion
455  // (needed for sqrt)
456  Table table_p; // table (for iscolumn and iskeyword)
457 };
458 
459 
460 } //# NAMESPACE CASACORE - END
461 
462 #endif
Class representing a function in table select expression.
Definition: ExprFuncNode.h:76
static String stringDateTime(const MVTime &dt, Int prec)
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:240
special function returning Int row id (meant for GIVING)
Definition: ExprFuncNode.h:248
Int64 getInt(const TableExprId &id)
for Int or Double returning Double
Definition: ExprFuncNode.h:116
static double angdist(double ra1, double dec1, double ra2, double dec2)
Get the angular distance between two positions on a sphere.
Definition: ExprFuncNode.h:432
for any array returning Bool scalar
Definition: ExprFuncNode.h:197
for Bool array returning Bool
Definition: ExprFuncNode.h:174
Main interface class to a read/write table.
Definition: Table.h:149
cone search functions, implemented in derived class
Definition: ExprFuncNode.h:255
DComplex getDComplex(const TableExprId &id)
Double getScale() const
Get possible unit scale factor (needed for sqrt).
Definition: ExprFuncNode.h:372
static TableExprNodeRep * fillNode(TableExprFuncNode *thisNode, PtrBlock< TableExprNodeRep *> &nodes, const Block< Int > &dtypeOper)
Link the children to the node and convert the children to constants if possible.
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:306
MVTime getDate(const TableExprId &id)
void setScale(Double scale)
Set unit scale factor (needed for sqrt).
Definition: ExprFuncNode.h:368
for Int, Double, Complex or String returning Bool
Definition: ExprFuncNode.h:262
PtrBlock< TableExprNodeRep * > operands_p
Definition: ExprNodeRep.h:696
PtrBlock< TableExprNodeRep * > & rwOperands()
Definition: ExprFuncNode.h:379
angular distance returning radians
Definition: ExprFuncNode.h:252
String getString(const TableExprId &id)
NodeDataType
Define the data types of a node.
Definition: ExprNodeRep.h:155
formatTypes
Format types.
Definition: MVAngle.h:251
Abstract base class for a node in a table column expression tree.
Definition: ExprNodeRep.h:151
void tryToConst()
Try if the function gives a constant result.
const PtrBlock< TableExprNodeRep * > & operands() const
Some functions to be used by TableExprNodeFuncArray.
Definition: ExprFuncNode.h:377
static String stringDMS(double val, Int prec)
FunctionType funcType() const
Definition: ExprFuncNode.h:381
LatticeExprNode cos(const LatticeExprNode &expr)
static String stringTime(const MVTime &dt, Int prec)
for any array returning Int scalar
Definition: ExprFuncNode.h:202
for any array returning Int array
Definition: ExprFuncNode.h:205
for Int, Double, Bool or String returning Int (using floor)
Definition: ExprFuncNode.h:114
static void getPrintFormat(String &fmt, Int &width, Int &prec, const PtrBlock< TableExprNodeRep *> &operands, const TableExprId &id)
Get the possible print format, width, and/or precision.
defines physical units
Definition: Unit.h:189
static DComplex string2Complex(const String &)
for any type returning array of that type
Definition: ExprFuncNode.h:188
for Int, Double or DComplex returning Int, Double or Complex
Definition: ExprFuncNode.h:102
special function resembling if statement
Definition: ExprFuncNode.h:250
special function returning Int row number
Definition: ExprFuncNode.h:246
for Int, Double or DComplex array returning Bool
Definition: ExprFuncNode.h:193
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:660
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:242
for Bool array returning Int scalar
Definition: ExprFuncNode.h:183
static void fillChildNodes(TableExprFuncNode *thisNode, PtrBlock< TableExprNodeRep *> &nodes, const Block< Int > &dtypeOper)
Link the children to the node and convert the children to constants if possible.
Double getDouble(const TableExprId &id)
NodeDataType argDataType() const
Definition: ExprFuncNode.h:383
for Int, Double or Complex array returning the same
Definition: ExprFuncNode.h:131
static Double string2Real(const String &)
for Int or Double returning Int or Double
Definition: ExprFuncNode.h:123
static Int64 string2Int(const String &)
Read a string as an integer, double, complex or bool.
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:170
static Double fillUnits(TableExprNodeRep *node, PtrBlock< TableExprNodeRep *> &nodes, FunctionType func)
Fill the result unit in the node.
static String stringHMS(double val, Int prec)
for Int, Double or DComplex returning DComplex
Definition: ExprFuncNode.h:129
special function returning a random Double number
Definition: ExprFuncNode.h:244
TableExprFuncNode(FunctionType, NodeDataType, ValueType, const TableExprNodeSet &source, const Table &=Table())
Constructor.
return angles as hms strings
Definition: ExprFuncNode.h:238
const Unit & unit() const
Get the unit.
Definition: ExprNodeRep.h:729
Table table_p
(needed for sqrt)
Definition: ExprFuncNode.h:456
A drop-in replacement for Block<T*>.
Definition: Block.h:861
~TableExprFuncNode()
Destructor.
static String stringValue(Bool val, const String &fmt, Int width)
Convert a value to a string.
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:138
static const Unit & makeEqualUnits(PtrBlock< TableExprNodeRep *> &nodes, uInt starg, uInt endarg)
Make the units of nodes from starg till endarg equal.
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:98
for Int, Double or DComplex returning Int or Double
Definition: ExprFuncNode.h:107
Class to handle a Regex or StringDistance.
Definition: ExprNodeRep.h:75
LatticeExprNode acos(const LatticeExprNode &expr)
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:147
Class to handle date/time type conversions and I/O.
Definition: MVTime.h:266
static NodeDataType checkOperands(Block< Int > &dtypeOper, ValueType &resVT, Block< Int > &vtypeOper, FunctionType, PtrBlock< TableExprNodeRep *> &)
Check the data and value types of the operands.
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:111
unsigned int uInt
Definition: aipstype.h:51