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