TECA
The Toolkit for Extreme Climate Analysis
teca_evaluate_expression.h
1 #ifndef teca_evaluate_expression_h
2 #define teca_evaluate_expression_h
3 
4 #include "teca_shared_object.h"
5 #include "teca_algorithm.h"
6 #include "teca_metadata.h"
7 #include "teca_array_attributes.h"
8 
9 #include <string>
10 #include <vector>
11 
12 TECA_SHARED_OBJECT_FORWARD_DECL(teca_evaluate_expression)
13 
14 /** @brief
15  * An algorithm that evaluates an expression stores the
16  * result in a new variable.
17  *
18  * @details
19  * The expression parser supports the following operations:
20  * +,-,*,/,%,<.<=,>,>=,==,!=,&&,||.!,?
21  *
22  * Grouping in the expression is denoted in the usual
23  * way: ()
24  *
25  * Constants in the expression are expanded to full length
26  * arrays and can be typed. The supported types are:
27  * d,f,L,l,i,s,c
28  * Corresponding to double,float, long long, long, int,
29  * short and char respectively. Integer types can be
30  * unsigned by including u after the code.
31  */
33 {
34 public:
35  TECA_ALGORITHM_STATIC_NEW(teca_evaluate_expression)
36  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_evaluate_expression)
37  TECA_ALGORITHM_CLASS_NAME(teca_evaluate_expression)
39 
40  // report/initialize to/from Boost program options
41  // objects.
42  TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION()
43  TECA_SET_ALGORITHM_PROPERTIES()
44 
45  /** @name expression
46  * Set the expression to evaluate.
47  */
48  ///@{
49  /// Set the expression.
50  void set_expression(const std::string &expr);
51 
52  /// Get the expression.
53  std::string get_expression()
54  { return this->expression; }
55  ///@}
56 
57  /** @name result_variable
58  * set the name of the variable to store the result in
59  */
60  ///@{
61  TECA_ALGORITHM_PROPERTY(std::string, result_variable)
62  ///@}
63 
64  /** @name result_attributes
65  * set the NetCDF attributes of the result. This is needed for
66  * teca_cf_writer and is otherwise optional.
67  */
68  ///@{
69  TECA_ALGORITHM_PROPERTY(teca_array_attributes, result_attributes)
70  ///@}
71 
72  /** @name remove_dependent_variables
73  * when set columns used in the calculation are removed from the output.
74  * default off.
75  */
76  ///@{
77  TECA_ALGORITHM_PROPERTY(int, remove_dependent_variables)
78  ///@}
79 
80 protected:
82 
83 private:
84  using teca_algorithm::get_output_metadata;
85 
86  teca_metadata get_output_metadata(unsigned int port,
87  const std::vector<teca_metadata> &input_md) override;
88 
89  std::vector<teca_metadata> get_upstream_request(
90  unsigned int port, const std::vector<teca_metadata> &input_md,
91  const teca_metadata &request) override;
92 
93  const_p_teca_dataset execute(unsigned int port,
94  const std::vector<const_p_teca_dataset> &input_data,
95  const teca_metadata &request) override;
96 
97 private:
98  std::string expression;
99  std::string result_variable;
100  teca_array_attributes result_attributes;
101 
102  std::string postfix_expression;
103  std::set<std::string> dependent_variables;
104  int remove_dependent_variables;
105 };
106 
107 #endif
The interface to TECA pipeline architecture.
Definition: teca_algorithm.h:244
An algorithm that evaluates an expression stores the result in a new variable.
Definition: teca_evaluate_expression.h:33
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:22
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
TECA_EXPORT bool set(teca_variant_array *varr, unsigned long i, PyObject *obj)
Set i'th element of the variant array to the value of the object.
Definition: teca_py_array.h:305
A convenience container for conventional array attributes necessary and/or useful when producing NetC...
Definition: teca_array_attributes.h:32