casacore
Loading...
Searching...
No Matches
ImageExprParse.h
Go to the documentation of this file.
1// # ImageExprParse.h: Classes to hold results from image expression parser
2// # Copyright (C) 1998,1999,2000,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: casa-feedback@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#ifndef IMAGES_IMAGEEXPRPARSE_H
27#define IMAGES_IMAGEEXPRPARSE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/lattices/LEL/LatticeExpr.h>
32#include <casacore/casa/BasicSL/Complex.h>
33#include <casacore/casa/BasicSL/String.h>
34#include <casacore/casa/Utilities/DataType.h>
35#include <casacore/casa/stdvector.h>
36#include <casacore/casa/HDF5/HDF5File.h>
37
38namespace casacore { // # NAMESPACE CASACORE - BEGIN
39
40// # Forward Declarations
41template <class T>
42class Block;
43class ImageRegion;
44class Table;
45class Slice;
46
47// <summary>
48// Class to hold values from image expression parser
49// </summary>
50
51// <use visibility=export>
52
53// <reviewed reviewer="" date="" tests="">
54// </reviewed>
55
56// <prerequisite>
57// # Classes you should understand before using this one.
58// <li> <linkto class=LatticeExpr>LatticeExpr</linkto>
59// </prerequisite>
60
61// <etymology>
62// ImageExprParse is the class used to parse an image expression command.
63// </etymology>
64
65// <synopsis>
66// ImageExprParse is used by the parser of image expression statements.
67// The parser is written in Bison and Flex in files ImageExprGram.y and .l.
68// The statements in there use the routines in this file to act
69// upon a reduced rule.
70// <p>
71// The main function (and the only function to be used by a user) is the
72// static function ImageExprParse::command which parses an expression command.
73// It returns a <linkto class=LatticeExprNode>LatticeExprNode</linkto>
74// object containing the expression represented as a tree.
75// The object can be used as a <src>Lattice(Expr)<T></src> in other operations.
76// <p>
77// The syntax of the command is similar to that of expressions in C++.
78// E.g.
79// <srcblock>
80// min(img1, img2) + sin(img3)
81// </srcblock>
82// The following items can be used in an expression:
83// <ul>
84// <li> Binary operators +, -, *, /, % (modulo), and ^ (power).
85// <li> Unary operators + and -.
86// <li> Comparison operators ==, >, >=, <, <=, and !=.
87// <li> Logical operators &&, ||, and !.
88// <li> Constant single and double precision values.
89// <br>No exponent or exponent "e" results in single precision (Float),
90// while "d" results in double precision (Double).
91// <li> The imaginary part of a complex value can be given by the suffix "i".
92// A full complex number can be given by addition. E.g. "3+4i".
93// The complex is single (Complex) or double (DComplex) precision
94// depending on the constituting parts.
95// <li> The special constants pi and e can be given as a double precision
96// value by means of the functions pi() and e().
97// <li> Boolean constants T and F can be given.
98// <li> A lot of functions are available.
99// They are the same as the ones supported by class
100// <linkto class=LatticeExprNode>LatticeExprNode</linkto>.
101// <li> Explicit conversion functions float, double, complex and dcomplex
102// are available. Conversions are automatically done where needed,
103// but for performance reasons it may sometimes be better to do
104// explicit conversions. See also below in the first example.
105// <li> An image can to be given using its file name. The file name
106// can contain environment variables and user home directories
107// using the standard UNIX syntax $ENVVAR and ~username.
108// There are 3 ways to specify a file name:
109// <ol>
110// <li> When the name contains no other special characters than
111// $, ~, and . it can be given as such.
112// <li> Backslashes can be used to escape individual special characters.
113// <li> The full name can be enclosed in quotes (single or double)
114// to escape the entire name. Adjacent quoted parts
115// are combined to one name, which can be used to use quotes
116// in the file name.
117// </ol>
118// Note that escaping has to be used too for the file name
119// T or F (otherwise it is the boolean constant).
120// E.g.
121// <srcblock>
122// image.data
123// "~noordam/data/image.data"
124// "~/image.data"
125// "$HOME/image.data"
126// $HOME\/image.data
127// "ab'c"'d"e' results in ab'cd"e
128// </srcblock>
129// Only input images with data type Float and Complex are supported,
130// because those data types are the only ones used so far.
131// Support of Bool, Double, and DComplex is very simple to build in.
132// The resulting lattice can be of type Bool, Float, Double,
133// Complex, and DComplex.
134// <li> An image can also be given by means of the <src>$n</src> notation,
135// where <src>n</src> is the sequence number in the
136// <src>tempLattices</src> argument given to the <src>command</src>
137// function. Note that the sequence numbers start counting at 1
138// (to be compliant with glish indexing).
139// <br>It can, for instance, be used to use non-persistent lattices
140// in an expression.
141// </ul>
142// When the expression is parsed, it is checked if the images and lattices
143// involved have conforming shapes and coordinates. Note, however, that
144// some functions (e.g. mean) reduce an image to a scalar. Such an image
145// can have a different shape and coordinates.
146// <p>
147// The data types of the images and constants involved can be different.
148// The data type of a subexpression is the common data type (e.g.
149// Float and Double result in Double; Complex and Double result in DComplex).
150// Automatic implicit conversions are done where needed. However, for
151// performance reasons it may sometimes be better to convert explicitly.
152// See below in the first example.
153// <p>
154// The expression evaluator (which is not part of the parser) evaluates
155// the expression in chunks to avoid having to keep large temporary
156// results. A scalar subexpression is evaluated only once to avoid
157// unnecessary (possibly expensive) calculations.
158// <p>
159// Some examples:
160// <dl>
161// <dt> <src> img1 + min(float(pi()), mean(img2)) </src>
162// <dd> Suppose img1 and img2 are images with single precision data.
163// They do not need to have conforming shapes and coordinates,
164// because only the mean of img2 is used.
165// <br>Note that pi is explicitly converted to single precision,
166// because pi() results in a Double. If that was not done,
167// the expression result would be a Double with the effect that
168// all data of img1 had to be converted to Double.
169// <dt> <src> min(img1, (min(img1)+max(img1))/2) </src>
170// <dd> This example shows that there are 2 min functions. One with a
171// single argument returning the minimum value of that image.
172// The other with 2 arguments returning a lattice containing
173// img1 data clipped at the value of the 2nd argument.
174// </dl>
175// </synopsis>
176
177// <example>
178// <srcblock>
179// LatticeExpr<Double> expr ("a + sin(b)");
180// ArrayLattice<Double> arr(expr.shape());
181// arr.copyData (expr);
182// </srcblock>
183// Line 1 creates a LatticeExpr object for the given expression. Note that
184// <src>a</src> and <src>b</src> are names of lattice files (e.g. PagedImage).
185// <br> Line 2 creates an ArrayLattice with the same shape as the expression
186// (which is the shape of lattice a (and b)).
187// <br> Line 3 copies the result of the expression to the ArrayLattice.
188// </example>
189
190// <motivation>
191// It is necessary to be able to give an image expression command in ASCII.
192// This can be used in glish to operate on lattices/images.
193// </motivation>
194
195// # <todo asof="$DATE:$">
196// # A List of bugs, limitations, extensions or planned refinements.
197// # </todo>
198
200 public:
201 // Parse the given command.
202 // It will open all lattices needed.
203 // It returns the resulting image expression.
204 // <br>The <src>tempLattices/tempRegions</src> arguments make it possible
205 // to use temporary lattices/images and regions in the expression by means
206 // of the <src>$n</src> notation.
207 // <br> If a directory name is given, it is used instead of the working
208 // directory for relative file names.
209 // <group>
210 static LatticeExprNode command(const String& str, const String& dirName = String());
211 static LatticeExprNode command(const String& str, const Block<LatticeExprNode>& tempLattices,
212 const Block<const ImageRegion*>& tempRegions,
213 const String& dirName = String());
214 // </group>
215
216 // Construct a literal object for the given type.
217 // <group>
222 ImageExprParse(const Complex& value);
223 ImageExprParse(const DComplex& value);
226 // </group>
227
228 // Make a LatticeExprNode for a function.
229 // <group>
234 const LatticeExprNode& arg3) const;
235 // </group>
236
237 // Make a LatticeExprNode object for the lattice or region name.
239
240 // Make a LatticeExprNode object for the name of constant, lattice,
241 // or region.
243
244 // Make a LatticeExprNode object for the temporary region number.
246
247 // Make a LatticeExprNode object for the literal value.
249
250 // Make a Slice object from 1-3 literals.
251 // <group>
252 static Slice* makeSlice(const ImageExprParse& start);
253 static Slice* makeSlice(const ImageExprParse& start, const ImageExprParse& end);
254 static Slice* makeSlice(const ImageExprParse& start, const ImageExprParse& end,
255 const ImageExprParse& incr);
256 // </group>
257
258 // Make a node for the INDEXIN function.
259 static LatticeExprNode makeIndexinNode(const LatticeExprNode& axis, const vector<Slice>& slices);
260
261 // Make an array from a value list.
263
264 // Make an IPosition containing the binning values.
265 static IPosition makeBinning(const LatticeExprNode& values);
266
267 // Get the names of the images used in the expression.
268 static const vector<String>& getImageNames() { return theirNames; }
269
270 // Set the static node object (used by the .y file).
271 static void setNode(const LatticeExprNode& node) { theirNode = node; }
272
273 // Keep track of the nodes allocated while parsing the expression.
274 // <group>
275 static void addNode(LatticeExprNode* node);
276 static void addNode(ImageExprParse* node);
277 static void deleteNodes();
278 // </group>
279
280 // A function to test addDir. It first sets the directory.
281 static String setAddDir(const String& dirName, const String& fileName);
282
283 private:
284 // If a directory was given, prepend it to the file name if relative.
285 static String addDir(const String& fileName);
286
287 // Try if the name represent a lattice or image.
288 // Return False if not.
290
291 // Make the node from the image name and a mask name.
292 // The mask name can be NOMASK (case insensitive) meaning that no mask
293 // is applied to the image.
295
296 // Callback function for RegionHandlerTable to get the table to be used.
297 static Table& getRegionTable(void*, Bool);
298
299 // Callback function for RegionHandlerHDF5 to get the file to be used.
300 static const std::shared_ptr<HDF5File>& getRegionHDF5(void*);
301
302 // # A 'global' node object to hold the resulting expression.
304
305 // # The names of the images used in the expression.
306 // # and the level of nesting.
307 static vector<String> theirNames;
309
310 DataType itsType;
311 Bool itsBval; // # boolean literal
312 Int itsIval; // # integer literal
313 Float itsFval; // # Float literal
314 Double itsDval; // # Double literal
315 Complex itsCval; // # Complex literal
316 DComplex itsDCval; // # DComplex literal
317 String itsSval; // # lattice name; function name
318};
319
320} // namespace casacore
321
322#endif
static String setAddDir(const String &dirName, const String &fileName)
A function to test addDir.
LatticeExprNode makeLRNode() const
Make a LatticeExprNode object for the lattice or region name.
ImageExprParse(Double value)
LatticeExprNode makeLiteralNode() const
Make a LatticeExprNode object for the literal value.
LatticeExprNode makeFuncNode(const LatticeExprNode &arg1, const LatticeExprNode &arg2) const
static Slice * makeSlice(const ImageExprParse &start)
Make a Slice object from 1-3 literals.
static LatticeExprNode theirNode
static LatticeExprNode makeIndexinNode(const LatticeExprNode &axis, const vector< Slice > &slices)
Make a node for the INDEXIN function.
static const std::shared_ptr< HDF5File > & getRegionHDF5(void *)
Callback function for RegionHandlerHDF5 to get the file to be used.
static Table & getRegionTable(void *, Bool)
Callback function for RegionHandlerTable to get the table to be used.
static IPosition makeBinning(const LatticeExprNode &values)
Make an IPosition containing the binning values.
LatticeExprNode makeFuncNode(const LatticeExprNode &arg1, const LatticeExprNode &arg2, const LatticeExprNode &arg3) const
LatticeExprNode makeImageNode(const String &name, const String &mask) const
Make the node from the image name and a mask name.
ImageExprParse(const Complex &value)
static Slice * makeSlice(const ImageExprParse &start, const ImageExprParse &end, const ImageExprParse &incr)
ImageExprParse(const String &value)
LatticeExprNode makeRegionNode() const
Make a LatticeExprNode object for the temporary region number.
ImageExprParse(const Char *value)
static Slice * makeSlice(const ImageExprParse &start, const ImageExprParse &end)
static void deleteNodes()
static LatticeExprNode command(const String &str, const Block< LatticeExprNode > &tempLattices, const Block< const ImageRegion * > &tempRegions, const String &dirName=String())
Bool tryLatticeNode(LatticeExprNode &node, const String &name) const
Try if the name represent a lattice or image.
ImageExprParse(Bool value)
Construct a literal object for the given type.
static const vector< String > & getImageNames()
Get the names of the images used in the expression.
static LatticeExprNode makeValueList(const Block< LatticeExprNode > &values)
Make an array from a value list.
static vector< String > theirNames
ImageExprParse(Float value)
ImageExprParse(const DComplex &value)
LatticeExprNode makeFuncNode() const
Make a LatticeExprNode for a function.
static String addDir(const String &fileName)
If a directory was given, prepend it to the file name if relative.
static void addNode(LatticeExprNode *node)
Keep track of the nodes allocated while parsing the expression.
static void setNode(const LatticeExprNode &node)
Set the static node object (used by the.y file).
LatticeExprNode makeLitLRNode() const
Make a LatticeExprNode object for the name of constant, lattice, or region.
static LatticeExprNode command(const String &str, const String &dirName=String())
Parse the given command.
static void addNode(ImageExprParse *node)
LatticeExprNode makeFuncNode(const LatticeExprNode &arg1) const
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
float Float
Definition aipstype.h:52
String name() const
Return the name of the field.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
double Double
Definition aipstype.h:53
iterator end()
Definition Block.h:601
char Char
Definition aipstype.h:44