TECA
The Toolkit for Extreme Climate Analysis
teca_cartesian_mesh_source.h
1 #ifndef teca_cartesian_mesh_source_h
2 #define teca_cartesian_mesh_source_h
3 
4 #include "teca_algorithm.h"
5 #include "teca_metadata.h"
6 
7 #include <functional>
8 #include <map>
9 #include <utility>
10 
11 TECA_SHARED_OBJECT_FORWARD_DECL(teca_cartesian_mesh_source)
12 
13 /** The signature of the callback used to specify user defined fields.
14  * f(x, y, z, t) -> w
15  * Given spatial coordinate axes x,y,z and the time t, return the
16  * 3D field w.
17  */
18 using field_generator_callback = std::function<p_teca_variant_array(
20  const const_p_teca_variant_array &, double)>;
21 
22 /** An object that bundles field name, the metadata attributes needed for I/O,
23  * and a field generator callback. Use this with append_field_generator
24  */
26 {
27  std::string name;
28  teca_metadata attributes;
29  field_generator_callback generator;
30 };
31 
33 
34 inline
35 bool operator==(const field_generator &l, const field_generator &r)
36 {
37  return l.name == r.name;
38 }
39 
40 inline
41 bool operator!=(const field_generator &l, const field_generator &r)
42 {
43  return l.name != r.name;
44 }
45 
46 /** @brief
47  * An algorithm that generates a teca_cartesian_mesh of the requested
48  * spatial and temporal dimensions with optional user defined fields.
49  *
50  * @details
51  * User defined fields are specified by passing callbacks and metadata
52  * via field_generator and append_field_generator
53  *
54  * The spatial and temporal dimensions are set by the combination of
55  * whole_extent and bounds.
56  *
57  * The names of coordinate axes are set by the combination
58  * of x_axis_variable, y_axis_variable, z_axis_variable,
59  * and t_axis_variable
60  *
61  * The units of the coordinate axes are set by the combination of
62  * x_axis_units, y_axis_units, z_axis_units, calendar,
63  * and time_units.
64  */
66 {
67 public:
68  TECA_ALGORITHM_STATIC_NEW(teca_cartesian_mesh_source)
69  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_cartesian_mesh_source)
70  TECA_ALGORITHM_CLASS_NAME(teca_cartesian_mesh_source)
72 
73  // report/initialize to/from Boost program options
74  // objects.
75  TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION()
76  TECA_SET_ALGORITHM_PROPERTIES()
77 
78  /** @name coordinate_type_code
79  * set/get the type code for generated coordinates. The default is a 64 bit
80  * floating point type. Use teca_variant_array_code<NT>::get() to get
81  * specific type codes for C++ POD types NT.
82  */
83  ///@{
84  TECA_ALGORITHM_PROPERTY(unsigned int, coordinate_type_code)
85  ///@}
86 
87  /** @name field_type_code
88  * set/get the type code for generated fields. The default is a 64 bit
89  * floating point type. Use teca_variant_array_code<NT>::get() to get
90  * specific type codes for C++ POD types NT.
91  */
92  ///@{
93  TECA_ALGORITHM_PROPERTY(unsigned int, field_type_code)
94  ///@}
95 
96  /** @name whole_extent
97  * set/get the global index space extent of the data. the extents are
98  * given by 8 values, 6 spatial plus 2 temporal, in the following order
99  * [i0 i1 j0 j1 k0 k1 q0 q1] This should be the same on all ranks
100  */
101  ///@{
102  TECA_ALGORITHM_VECTOR_PROPERTY(unsigned long, whole_extent)
103 
104  /** Set the spatial extents from a metadata object following the
105  * conventions defined by the teca_cf_reader. If three_d is true the
106  * extents in the z-direction are copied, otherwise they are set to 0.
107  * Returns zero if successful and non-zero if the supplied metadata is
108  * missing any of the requisite information.
109  **/
110  int set_spatial_extents(const teca_metadata &md, bool three_d = true);
111  ///@}
112 
113  /** @name bounds
114  * set/get the global bounds of the data. the bounds are 8 values 6 spatial
115  * plus 2 temporal in the following order. [x0 x1 y0 y1 z0 z1 t0 t1]
116  * this should be the same on all ranks.
117  */
118  ///@{
119  TECA_ALGORITHM_VECTOR_PROPERTY(double, bound)
120 
121  /** Set the spatial bounds from a metadata object following the conventions
122  * defined by the teca_cf_reader. Returns zero if successful and
123  * non-zero if the supplied metadata is missing any of the requisite
124  * information.
125  */
126  int set_spatial_bounds(const teca_metadata &md, bool three_d = true);
127 
128  ///@}
129 
130  /** @name x_axis_variable
131  * set the name of the variable to use for the coordinate axes and
132  * optionally associated attributes.
133  */
134  ///@{
135  /** set the name of the t_axis_variable */
136  void set_x_axis_variable(const std::string &name);
137 
138  /** Set the name of the variable and its attributes. See
139  * teca_array_attributes for more information.
140  */
141  void set_x_axis_variable(const std::string &name, const teca_metadata &atts);
142 
143  /** Set the name of the variable and its attributes using conventions
144  * defined by the teca_cf_reader. Returns zero if successful and
145  * non-zero if the supplied metadata is missing any of the requisite
146  * information.
147  */
148  int set_x_axis_variable(const teca_metadata &md);
149  ///@}
150 
151  /** @name y_axis_variable
152  * set the name of the variable to use for the coordinate axes and
153  * optionally associated attributes.
154  */
155  ///@{
156  /** set the name of the y_axis_variable */
157  void set_y_axis_variable(const std::string &name);
158 
159  /** Set the name of the variable and its attributes. See
160  * teca_array_attributes for more information.
161  */
162  void set_y_axis_variable(const std::string &name, const teca_metadata &atts);
163 
164  /** Set the name of the variable and its attributes using conventions
165  * defined by the teca_cf_reader. Returns zero if successful and
166  * non-zero if the supplied metadata is missing any of the requisite
167  * information.
168  */
169  int set_y_axis_variable(const teca_metadata &md);
170  ///@}
171 
172  /** @name z_axis_variable
173  * set the name of the variable to use for the coordinate axes and
174  * optionally associated attributes.
175  */
176  ///@{
177  /** set the name of the z_axis_variable */
178  void set_z_axis_variable(const std::string &name);
179 
180  /** Set the name of the variable and its attributes. See
181  * teca_array_attributes for more information.
182  */
183  void set_z_axis_variable(const std::string &name, const teca_metadata &atts);
184 
185  /** Set the name of the variable and its attributes using conventions
186  * defined by the teca_cf_reader. Returns zero if successful and
187  * non-zero if the supplied metadata is missing any of the requisite
188  * information.
189  */
190  int set_z_axis_variable(const teca_metadata &md);
191  ///@}
192 
193  /** @name t_axis_variable
194  * set the name of the variable to use for the coordinate axes and
195  * optionally associated attributes.
196  */
197  ///@{
198  /** set the name of the t_axis_variable */
199  void set_t_axis_variable(const std::string &name);
200 
201  /** Set the calendar, and time units of the t_axis_variable */
202  void set_calendar(const std::string &calendar, const std::string &units);
203 
204  /** Set the name of the variable and its attributes. See
205  * teca_array_attributes for more information.
206  */
207  void set_t_axis_variable(const std::string &name,
208  const teca_metadata &atts);
209 
210  /** Set the name of the variable and its attributes using conventions
211  * defined by the teca_cf_reader. Returns zero if successful and
212  * non-zero if the supplied metadata is missing any of the requisite
213  * information.
214  */
215  int set_t_axis_variable(const teca_metadata &md);
216 
217  /** Set the time axis using coordinate conventions defined by the
218  * teca_cf_reader. When a time axis is provided values are served up from
219  * the array rather than being generated. Execution control keys are also
220  * made use of if present. Returns zero if successful and non-zero if the
221  * supplied metadata is missing any of the requisite information.
222  */
223  int set_t_axis(const teca_metadata &md);
224 
225  /** Set the time axis directly. When a time axis is provided values are
226  * served up from the array rather than being generated. Execution control
227  * keys are also made use of if present.
228  */
229  void set_t_axis(const p_teca_variant_array &t);
230  ///@}
231 
232  /** @name output_metadata
233  * Set the output metadata directly. The provided metadata must contain
234  * "coordinates" as defined by the teca_cf_reader because these are
235  * required for mesh generation. Pipeline execution control keys as defined
236  * by teca_index_executive are also required. Calendaring metadata is
237  * recommended. A copy of the passed object is made but "variables" are
238  * replaced with those generated by this class, if any. As a result be sure
239  * to specifiy field generators before calling this method. Returns 0 if
240  * successful, and non-zero if the supplied metadata doesn't contain the
241  * expected information. No error messages are sent to the terminal.
242  */
243  ///@{
244  int set_output_metadata(const teca_metadata &md);
245  ///@}
246 
247  /** @name append_field_generator
248  * set a callback function f(x,y,z,t) that generates a field named name
249  * x,y,z are coordinate axes in variant arrays, t is the double precision
250  * time value.
251  */
252  ///@{
253  void append_field_generator(const std::string &name,
254  const teca_metadata &atts, field_generator_callback &callback);
255  ///@}
256 
257  /** @name field_generator
258  * Set/get the named callbacks that generate fields on the mesh. These
259  * should be packaged in the field_generator struct so that field name
260  * and attributes for I/O are provided together with the callback.
261  */
262  ///@{
263  TECA_ALGORITHM_VECTOR_PROPERTY(field_generator_t, field_generator)
264  ///@}
265 
266 protected:
268 
269 private:
270  /// implements the report phase of pipeline execution
271  teca_metadata get_output_metadata(unsigned int port,
272  const std::vector<teca_metadata> &input_md) override;
273 
274  /// implements the execute phase of pipeline execution
275  const_p_teca_dataset execute(unsigned int port,
276  const std::vector<const_p_teca_dataset> &input_data,
277  const teca_metadata &request) override;
278 
279  /// updates the modification state
280  void set_modified() override;
281 
282  /// clears cached metadata in response to modification of algorithm properties
283  void clear_cached_metadata();
284 
285 private:
286  unsigned int coordinate_type_code;
287  unsigned int field_type_code;
288  std::string x_axis_variable;
289  std::string y_axis_variable;
290  std::string z_axis_variable;
291  std::string t_axis_variable;
292  teca_metadata x_axis_attributes;
293  teca_metadata y_axis_attributes;
294  teca_metadata z_axis_attributes;
295  teca_metadata t_axis_attributes;
296  std::vector<unsigned long> whole_extents;
297  std::vector<double> bounds;
298 
299  std::vector<field_generator_t> field_generators;
300 
301  struct internals_t;
302  internals_t *internals;
303 };
304 
305 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:18
teca_cartesian_mesh_source::set_t_axis_variable
void set_t_axis_variable(const std::string &name)
field_generator
Definition: teca_cartesian_mesh_source.h:25
teca_cartesian_mesh_source::set_spatial_extents
int set_spatial_extents(const teca_metadata &md, bool three_d=true)
teca_cartesian_mesh_source::set_spatial_bounds
int set_spatial_bounds(const teca_metadata &md, bool three_d=true)
teca_cartesian_mesh_source::set_y_axis_variable
void set_y_axis_variable(const std::string &name)
teca_cartesian_mesh_source::set_t_axis
int set_t_axis(const teca_metadata &md)
teca_cartesian_mesh_source::set_z_axis_variable
void set_z_axis_variable(const std::string &name)
const_p_teca_variant_array
std::shared_ptr< const teca_variant_array > const_p_teca_variant_array
Definition: teca_variant_array.h:22
teca_cartesian_mesh_source::set_x_axis_variable
void set_x_axis_variable(const std::string &name)
teca_cartesian_mesh_source
An algorithm that generates a teca_cartesian_mesh of the requested spatial and temporal dimensions wi...
Definition: teca_cartesian_mesh_source.h:65
p_teca_variant_array
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:22
teca_cartesian_mesh_source::set_calendar
void set_calendar(const std::string &calendar, const std::string &units)
teca_algorithm
The interface to TECA pipeline architecture.
Definition: teca_algorithm.h:237