TECA
The Toolkit for Extreme Climate Analysis
teca_metadata_util.h
Go to the documentation of this file.
1 #ifndef teca_metadata_util_h
2 #define teca_metadata_util_h
3 
4 /// @file
5 
6 #include "teca_config.h"
7 #include "teca_common.h"
8 #include "teca_metadata.h"
9 
10 #include <string>
11 
12 
13 /// Codes for dealing with teca_metadata
15 {
16 /** Given a collection of array attributes (following the conventions used by
17  * the teca_cf_reader) and a mesh extent, compute and return the valid extent
18  * of the array. This takes into account 1d and 2d arrays on a 3d mesh. Return
19  * zero if successful. The mesh_dims_active key is required, if not found 1
20  * is returned and the array_extent is set to the mesh_extent.
21  */
23 int get_array_extent(const teca_metadata &array_attributes,
24  const unsigned long mesh_extent[8], unsigned long array_extent[8]);
25 
26 /** Get the requested index extent from the request.
27  *
28  * @param[in] request the request
29  * @param[out] request_key the name of the key holding the index
30  * @apram[out] indices the requested index extent
31  * @returns zero if the index was successfully obtained.
32  */
33 template <typename index_t>
35  std::string &request_key, index_t indices[2])
36 {
37  // get the requested index
38  if (request.get("index_request_key", request_key))
39  {
40  TECA_ERROR("Failed to locate the index_request_key")
41  return -1;
42  }
43 
44  if (request.get(request_key, indices))
45  {
46  TECA_ERROR("Failed to get the requested indices using the"
47  " index_request_key \"" << request_key << "\"")
48  return -1;
49  }
50 
51  return 0;
52 }
53 
54 /** Get the requested index from the request. Use this function when the
55  * algorithm can not handle requests for multiple indices. A check is performed
56  * to ensure that only one index was requested.
57  *
58  * @param[in] request the request
59  * @param[out] request_key the name of the key holding the index
60  * @apram[out] index the requested index
61  * @returns zero if the index was successfully obtained.
62  */
63 template <typename index_t>
65  std::string &request_key, index_t &index)
66 {
67  // get the requested index
68  if (request.get("index_request_key", request_key))
69  {
70  TECA_ERROR("Failed to locate the index_request_key")
71  return -1;
72  }
73 
74  index_t indices[2];
75  if (request.get(request_key, indices))
76  {
77  TECA_ERROR("Failed to get the requested index using the"
78  " index_request_key \"" << request_key << "\"")
79  return -1;
80  }
81 
82  index_t n_indices = indices[1] - indices[0] + 1;
83  if (n_indices != 1)
84  {
85  TECA_ERROR(<< n_indices << " requested when one was required")
86  return -1;
87  }
88 
89  index = indices[0];
90 
91  return 0;
92 }
93 
94 };
95 
96 #endif
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.
Codes for dealing with teca_metadata.
Definition: teca_metadata_util.h:15
int get_requested_indices(const teca_metadata &request, std::string &request_key, index_t indices[2])
Definition: teca_metadata_util.h:34
TECA_EXPORT int get_array_extent(const teca_metadata &array_attributes, const unsigned long mesh_extent[8], unsigned long array_extent[8])
int get_requested_index(const teca_metadata &request, std::string &request_key, index_t &index)
Definition: teca_metadata_util.h:64
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:161