casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
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);
363  Double getDouble (const TableExprId& id);
364  DComplex getDComplex (const TableExprId& id);
365  String getString (const TableExprId& id);
366  TaqlRegex getRegex (const TableExprId& id);
367  MVTime getDate (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.
374  static NodeDataType checkOperands (Block<Int>& dtypeOper,
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
Class representing a function in table select expression.
Definition: ExprFuncNode.h:77
static String stringDateTime(const MVTime &dt, Int prec)
std::vector< TENShPtr > & rwOperands()
Definition: ExprFuncNode.h:398
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:265
special function returning Int row id (meant for GIVING)
Definition: ExprFuncNode.h:273
Int64 getInt(const TableExprId &id)
for Int, Double or Complex returning Double or Complex
Definition: ExprFuncNode.h:119
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
for any array returning Bool scalar
Definition: ExprFuncNode.h:221
Main interface class to a read/write table.
Definition: Table.h:157
cone search functions, implemented in derived class
Definition: ExprFuncNode.h:280
void fillUnits()
Fill the result unit in the node.
DComplex getDComplex(const TableExprId &id)
for Bool array returning Int scalar
Definition: ExprFuncNode.h:202
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:310
MVTime getDate(const TableExprId &id)
for Int, Double, Complex or String returning Bool
Definition: ExprFuncNode.h:289
for Int, Double or DComplex returning Double
Definition: ExprFuncNode.h:111
Double getScale() const
Get possible unit scale factor (needed for sqrt).
Definition: ExprFuncNode.h:391
angular distance returning radians
Definition: ExprFuncNode.h:277
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.
static String stringDMS(double val, Int prec)
LatticeExprNode cos(const LatticeExprNode &expr)
const std::vector< TENShPtr > & operands() const
Some functions to be used by TableExprNodeFuncArray.
Definition: ExprFuncNode.h:396
static String stringTime(const MVTime &dt, Int prec)
for any array returning Int scalar
Definition: ExprFuncNode.h:226
for any array returning Int array
Definition: ExprFuncNode.h:229
for Int, Double, Bool or String returning Int (using floor)
Definition: ExprFuncNode.h:117
defines physical units
Definition: Unit.h:189
static DComplex string2Complex(const String &)
for any type returning array of that type
Definition: ExprFuncNode.h:211
for Int, Double or DComplex returning Int, Double or Complex
Definition: ExprFuncNode.h:103
special function resembling if statement
Definition: ExprFuncNode.h:275
for Double or DComplex returning Double
Definition: ExprFuncNode.h:115
special function returning Int row number
Definition: ExprFuncNode.h:271
for Int, Double or DComplex array returning Bool
Definition: ExprFuncNode.h:217
NodeDataType argDataType() const
Definition: ExprFuncNode.h:402
for Int, Double or DComplex returning Double or Complex
Definition: ExprFuncNode.h:92
formatTypes
Format types.
Definition: MVTime.h:276
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:87
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:267
Double getDouble(const TableExprId &id)
for Int, Double or Complex array returning the same
Definition: ExprFuncNode.h:134
static Double string2Real(const String &)
for Int or Double returning Int or Double
Definition: ExprFuncNode.h:126
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:132
special function returning a random Double number
Definition: ExprFuncNode.h:269
return angles as hms strings
Definition: ExprFuncNode.h:263
Table table_p
(needed for sqrt)
Definition: ExprFuncNode.h:475
~TableExprFuncNode()
Destructor.
normalize angle between -pi and pi
Definition: ExprFuncNode.h:287
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:698
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.
const Unit & unit() const
Get the unit.
Definition: ExprNodeRep.h:731
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:147
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:108
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:193
String: the storage and methods of handling collections of characters.
Definition: String.h:225
static String stringDate(const MVTime &dt)
FunctionType funcType() const
Definition: ExprFuncNode.h:400
for Int or Double array returning Double
Definition: ExprFuncNode.h:156
Class to handle date/time type conversions and I/O.
Definition: MVTime.h:270
LatticeExprNode sin(const LatticeExprNode &expr)
Numerical 1-argument functions.
for Int, Double, DComplex, Bool or String returning Double
Definition: ExprFuncNode.h:113
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.