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