TECA
The Toolkit for Extreme Climate Analysis
teca_array_attributes.h
1 #ifndef teca_array_attributes_h
2 #define teca_array_attributes_h
3 
4 #include "teca_config.h"
5 #include "teca_metadata.h"
6 
7 #include <ostream>
8 #include <variant>
9 
10 /** @brief
11  * A convenience container for conventional array attributes necessary and/or
12  * useful when producing NetCDF CF format files using the teca_cf_writer.
13  *
14  * @details
15  *
16  * | Member | Description |
17  * | ------ | ----------- |
18  * | type_code | storage type as defined by teca_variant_array::type_code() |
19  * | centering | one of: no_centering, point_centering, cell_centering, |
20  * | | edge_centering, or face_centering |
21  * | size | number of elements in the array |
22  * | units | string describing the units that the variable is in. |
23  * | long name | a more descriptive name |
24  * | description | text describing the data |
25  * | have_fill_value | set non-zero to indicate that a fill_value has been |
26  * | | provided. |
27  * | fill_value | value used to identify missing or invalid data |
28  */
30 {
31  teca_array_attributes() : type_code(0),
32  centering(0), size(0), units(), long_name(), description(),
33  have_fill_value(0), fill_value(1e20f)
34  {}
35 
36  template <typename fv_t = float>
37  teca_array_attributes(unsigned int tc, unsigned int cen,
38  unsigned long n, const std::string &un, const std::string &ln,
39  const std::string &descr, const int &have_fv=0, const fv_t &fv=fv_t(1e20f)) :
40  type_code(tc), centering(cen), size(n), units(un), long_name(ln),
41  description(descr), have_fill_value(have_fv), fill_value(fv)
42  {}
43 
45  teca_array_attributes &operator=(const teca_array_attributes &) = default;
46 
47  /// Convert a from metadata object.
49  type_code(0), centering(0), size(0), units(), long_name(),
50  description(), have_fill_value(0), fill_value(1.e20f)
51  {
52  from(md);
53  }
54 
55  /// Convert from metadata object.
56  teca_array_attributes &operator=(const teca_metadata &md);
57 
58  /// Converts to a metadata object.
59  operator teca_metadata() const;
60 
61  /// Adds current values to the metadata object
62  int to(teca_metadata &md) const;
63 
64  /// Adds the current values to the metadata object, only if they don't exist.
65  int merge_to(teca_metadata &md) const;
66 
67  /// Intializes values from the metadata object.
68  int from(const teca_metadata &md);
69 
70  /// Send to the stream in human readable form.
71  void to_stream(std::ostream &os) const;
72 
73  /** The possible mesh centrings.
74  *
75  * For coordinate system with orthogonal axes x,y,z relative to cell
76  * centering:
77  *
78  * > If A is one of x,y or z then A_face_centering data is located on the
79  * > low A face i.e. shifted in the -A direction and arrays will be longer
80  * > by 1 value in the A direction.
81  * >
82  * > If A is one of x,y or z then A_edge_centering data is located on the
83  * > low side edge parallel to A corrdinate axis. i.e. shifted in the -B
84  * > and -C directions and arrays will be longer by 1 value in the B and C
85  * > directions.
86  * >
87  * > point_centering data is located on the low corner. i.e. shifted
88  * > in the -A,-B, and -C directions and arrays will be longer
89  * > by 1 value in the A, B and C directions.
90  *
91  * Arrays that are not associated with geometric locations should
92  * be identified as no_centering.
93  *
94  * The default centering is cell centering.
95  */
96  enum
97  {
98  invalid_value = 0,
99  cell_centering = 0x0100,
100  x_face_centering = 0x0201,
101  y_face_centering = 0x0202,
102  z_face_centering = 0x0203,
103  x_edge_centering = 0x0401,
104  y_edge_centering = 0x0402,
105  z_edge_centering = 0x0403,
106  point_centering = 0x0800,
107  no_centering = 0x1000,
108  };
109 
110  /// convert the centering code to a string
111  static const char *centering_to_string(int cen);
112 
113  using fill_value_t =
114  std::variant<char, unsigned char, short, unsigned short,
115  int, unsigned int, long, unsigned long, long long,
116  unsigned long long, float, double>;
117 
118  unsigned int type_code;
119  unsigned int centering;
120  unsigned long size;
121  std::string units;
122  std::string long_name;
123  std::string description;
124  int have_fill_value;
125  fill_value_t fill_value;
126 };
127 
128 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_array_attributes::teca_array_attributes
teca_array_attributes(const teca_metadata &md)
Convert a from metadata object.
Definition: teca_array_attributes.h:48
teca_array_attributes
A convenience container for conventional array attributes necessary and/or useful when producing NetC...
Definition: teca_array_attributes.h:29
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.