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