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