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