casa  5.7.0-16
 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>
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
175  arravdevFUNC, //# 79
177  runavdevFUNC, //# 81
178  boxavdevFUNC, //# 82
179  arrrmsFUNC, //# 83
180  arrrmssFUNC, //# 84
181  runrmsFUNC, //# 85
182  boxrmsFUNC, //# 86
191  // for Bool array returning Bool
192  arranyFUNC, //# 95
193  arranysFUNC, //# 96
194  runanyFUNC, //# 97
195  boxanyFUNC, //# 98
196  arrallFUNC, //# 99
197  arrallsFUNC, //# 100
198  runallFUNC, //# 101
199  boxallFUNC, //# 102
200  // for Bool array returning Int scalar
201  arrntrueFUNC, //# 103
202  arrntruesFUNC, //# 104
203  runntrueFUNC, //# 105
204  boxntrueFUNC, //# 106
205  arrnfalseFUNC, //# 107
206  arrnfalsesFUNC, //# 108
207  runnfalseFUNC, //# 109
208  boxnfalseFUNC, //# 110
209  // for any type returning array of that type
210  arrayFUNC, //# 111
211  transposeFUNC, //# 112
212  areverseFUNC, //# 113
213  resizeFUNC, //# 114
214  diagonalFUNC, //# 115
215  // for Int, Double or DComplex array returning Bool
216  isnanFUNC, //# 116
217  isinfFUNC, //# 117
218  isfiniteFUNC, //# 118
219  // for any array returning Bool scalar
220  isdefFUNC, //# 119
221  isnullFUNC, //# 120
222  iscolFUNC, //# 121
223  iskeyFUNC, //# 122
224  // for any array returning Int scalar
225  ndimFUNC, //# 123
226  nelemFUNC, //# 124
227  // for any array returning Int array
228  shapeFUNC, //# 125
229  // for String
230  strlengthFUNC, //# 126 returning Int
231  upcaseFUNC, //# 127 returning String
232  downcaseFUNC, //# 128 returning String
233  capitalizeFUNC, //# 129 returning String
234  sreverseFUNC, //# 130
235  trimFUNC, //# 131 returning String
236  ltrimFUNC, //# 132 returning String
237  rtrimFUNC, //# 133 returning String
238  substrFUNC, //# 134 returning String
239  replaceFUNC, //# 135 returning String
240  regexFUNC, //# 136 returning TaqlRegex
241  patternFUNC, //# 137 returning TaqlRegex
242  sqlpatternFUNC, //# 138 returning TaqlRegex
243  // for Date
244  datetimeFUNC, //# 139 returning Date
245  mjdtodateFUNC, //# 140 returning Date
246  mjdFUNC, //# 141 returning Double
247  dateFUNC, //# 142 returning Date
248  timeFUNC, //# 143 returning Double (in radians)
249  yearFUNC, //# 144 returning Int
250  monthFUNC, //# 145 returning Int
251  dayFUNC, //# 146 returning Int
252  cmonthFUNC, //# 147 returning String
253  weekdayFUNC, //# 148 returning Int
254  cdowFUNC, //# 149 returning String
255  weekFUNC, //# 150 returning Int
256  ctodFUNC, //# 151 returning String
257  cdateFUNC, //# 152 returning String
258  ctimeFUNC, //# 153 returning String
259  // return values as strings
260  stringFUNC, //# 154
261  // return angles as hms strings
262  hmsFUNC, //# 155
263  // return angles as dms strings
264  dmsFUNC, //# 156
265  // return angles as hms/dms strings
266  hdmsFUNC, //# 157
267  // special function returning a random Double number
268  randFUNC, //# 158
269  // special function returning Int row number
270  rownrFUNC, //# 159
271  // special function returning Int row id (meant for GIVING)
272  rowidFUNC, //# 160
273  // special function resembling if statement
274  iifFUNC, //# 161
275  // angular distance returning radians
276  angdistFUNC, //# 162
277  angdistxFUNC, //# 163
278  // cone search functions, implemented in derived class
279  conesFUNC, //# 164
280  cones3FUNC, //# 165
281  anyconeFUNC, //# 166
282  anycone3FUNC, //# 167
283  findconeFUNC, //# 168
284  findcone3FUNC, //# 169
285  // normalize angle between -pi and pi
286  normangleFUNC, //# 170
287  // for Int, Double, Complex or String returning Bool
288  boolFUNC, //# 171
289  // masked array functions
290  nullarrayFUNC, //# 172
291  marrayFUNC, //# 173
292  arrdataFUNC, //# 174
293  arrmaskFUNC, //# 175
294  negatemaskFUNC, //# 176
295  replmaskedFUNC, //# 177
297  arrflatFUNC, //# 179
298  //# AGGREGATE functions must be the last ones.
299  FirstAggrFunc, //# 180
304  //# Grouping doing aggregation on the fly; reducing to a scalar per group
305  gminFUNC, //# 184
320  //# Grouping doing aggregation on the fly; reducing to an array per group
339  //# Grouping requiring aggregation of rows when getting result
340  gaggrFUNC, //# 215
344  gexpridFUNC, //# special function (can be inserted by TableParse)
345  NRFUNC //# 220 should be last
346  };
347 
348  // Constructor
350  const TableExprNodeSet& source,
351  const vector<TENShPtr>& nodes,
352  const Block<Int>& dtypeOper,
353  const Table& = Table());
354 
355  // Destructor
357 
358  // 'get' Functions to get the desired result of a function
359  // <group>
360  Bool getBool (const TableExprId& id);
361  Int64 getInt (const TableExprId& id);
362  Double getDouble (const TableExprId& id);
363  DComplex getDComplex (const TableExprId& id);
364  String getString (const TableExprId& id);
365  TaqlRegex getRegex (const TableExprId& id);
366  MVTime getDate (const TableExprId& id);
367  // </group>
368 
369  // Check the data and value types of the operands.
370  // It sets the exptected data and value types of the operands.
371  // Set the value type of the function result and returns
372  // the data type of the function result.
373  static NodeDataType checkOperands (Block<Int>& dtypeOper,
374  ValueType& resVT,
375  Block<Int>& vtypeOper,
376  FunctionType,
377  std::vector<TENShPtr>&);
378 
379  // Fill the result unit in the node.
380  // Adapt the children nodes if their units need to be converted.
381  // It returns a possible scale factor in case result unit is SI (for sqrt).
382  void fillUnits();
383 
384  // Link the children to the node and convert the children
385  // to constants if possible.
386  void fillChildNodes (const vector<TENShPtr>& nodes,
387  const Block<Int>& dtypeOper);
388 
389  // Get possible unit scale factor (needed for sqrt).
390  Double getScale() const
391  { return scale_p; }
392 
393  // Some functions to be used by TableExprNodeFuncArray.
394  // <group>
395  const std::vector<TENShPtr>& operands() const
396  { return operands_p; }
397  std::vector<TENShPtr>& rwOperands()
398  { return operands_p; }
400  { return funcType_p; }
402  { return argDataType_p; }
403  // </group>
404 
405  // Get the possible print format, width, and/or precision.
406  static void getPrintFormat (String& fmt, Int& width, Int& prec,
407  const std::vector<TENShPtr>& operands,
408  const TableExprId& id);
409 
410  // Convert the date and/or time to a string.
411  // <group>
412  static String stringDT (const MVTime& dt, Int prec, MVTime::formatTypes);
413  static String stringDateTime (const MVTime& dt, Int prec);
414  static String stringDate (const MVTime& dt);
415  static String stringTime (const MVTime& dt, Int prec);
416  // </group>
417 
418  // Convert a value to a string.
419  // If <src>fmt</src> is empty, ostringstream is used.
420  // Otherwise the printf-like format is used.
421  // If possible, a double value is converted to radians if formatted as angle.
422  // <group>
423  static String stringValue (Bool val, const String& fmt, Int width);
424  static String stringValue (Int64 val, const String& fmt, Int width);
425  static String stringValue (Double val, const String& fmt,
426  Int width, Int prec,
427  const std::pair<int,int>& mvFormat,
428  const Unit& unit);
429  static String stringValue (const DComplex& val, const String& fmt,
430  Int width, Int prec);
431  static String stringValue (const String& val, const String& fmt,
432  Int width);
433  static String stringValue (const MVTime& val, const String& fmt,
434  Int width,
435  const std::pair<int,int>& mvFormat);
436 
437  // Convert angle to a string (hms or dms).
438  // <group>
439  static String stringAngle (double val, Int prec,
441  static String stringHMS (double val, Int prec);
442  static String stringDMS (double val, Int prec);
443  // </group>
444 
445  // Get the MVTime/Angle format and optional precision.
446  // 0,0 is returned if empty or unknown format.
447  static std::pair<int,int> getMVFormat (const String& fmt);
448 
449  // Get the angular distance between two positions on a sphere.
450  static double angdist (double ra1, double dec1, double ra2, double dec2)
451  { return acos (sin(dec1)*sin(dec2) + cos(dec1)*cos(dec2)*cos(ra1-ra2)); }
452 
453  // Read a string as an integer, double, complex or bool.
454  static Int64 string2Int (const String&);
455  static Double string2Real (const String&);
456  static DComplex string2Complex (const String&);
457  static Bool string2Bool (const String&);
458 
459 private:
460  // Try if the function gives a constant result.
461  // If so, set the expression type to Constant.
462  void tryToConst();
463 
464  // Make the units of nodes from <src>starg</src> till <src>endarg</src>
465  // equal. Return the unit found.
466  static const Unit& makeEqualUnits (std::vector<TENShPtr>& nodes,
467  uInt starg, uInt endarg);
468 
469  //# Data members.
470  FunctionType funcType_p; // which function
471  NodeDataType argDataType_p; // common argument data type
472  Double scale_p; // possible scaling for unit conversion
473  // (needed for sqrt)
474  Table table_p; // table (for iscolumn and iskeyword)
475 };
476 
477 
478 } //# NAMESPACE CASACORE - END
479 
480 #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:397
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:264
special function returning Int row id (meant for GIVING)
Definition: ExprFuncNode.h:272
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:450
for any array returning Bool scalar
Definition: ExprFuncNode.h:220
Main interface class to a read/write table.
Definition: Table.h:153
cone search functions, implemented in derived class
Definition: ExprFuncNode.h:279
void fillUnits()
Fill the result unit in the node.
DComplex getDComplex(const TableExprId &id)
for Bool array returning Int scalar
Definition: ExprFuncNode.h:201
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:288
for Int, Double or DComplex returning Double
Definition: ExprFuncNode.h:110
Double getScale() const
Get possible unit scale factor (needed for sqrt).
Definition: ExprFuncNode.h:390
virtual casacore::String type() const
Implements RegionShape::type.
Definition: RegionShapes.h:548
angular distance returning radians
Definition: ExprFuncNode.h:276
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:395
static String stringTime(const MVTime &dt, Int prec)
for any array returning Int scalar
Definition: ExprFuncNode.h:225
for any array returning Int array
Definition: ExprFuncNode.h:228
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:210
for Int, Double or DComplex returning Int, Double or Complex
Definition: ExprFuncNode.h:102
special function resembling if statement
Definition: ExprFuncNode.h:274
for Double or DComplex returning Double
Definition: ExprFuncNode.h:114
special function returning Int row number
Definition: ExprFuncNode.h:270
for Int, Double or DComplex array returning Bool
Definition: ExprFuncNode.h:216
NodeDataType argDataType() const
Definition: ExprFuncNode.h:401
for Int, Double or DComplex returning Double or Complex
Definition: ExprFuncNode.h:91
formatTypes
Format types.
Definition: MVTime.h:275
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:266
Double getDouble(const TableExprId &id)
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:268
return angles as hms strings
Definition: ExprFuncNode.h:262
Table table_p
(needed for sqrt)
Definition: ExprFuncNode.h:474
~TableExprFuncNode()
Destructor.
normalize angle between -pi and pi
Definition: ExprFuncNode.h:286
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: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:192
String: the storage and methods of handling collections of characters.
Definition: String.h:223
static String stringDate(const MVTime &dt)
FunctionType funcType() const
Definition: ExprFuncNode.h:399
for Int or Double array returning Double
Definition: ExprFuncNode.h:155
Class to handle date/time type conversions and I/O.
Definition: MVTime.h:269
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.
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42