PISM, A Parallel Ice Sheet Model  stable v2.0.4 committed by Constantine Khrulev on 2022-05-25 12:02:27 -0800
VariableMetadata.hh
Go to the documentation of this file.
1 // Copyright (C) 2009--2018, 2020, 2021, 2022 Constantine Khroulev
2 //
3 // This file is part of PISM.
4 //
5 // PISM is free software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the Free Software
7 // Foundation; either version 3 of the License, or (at your option) any later
8 // version.
9 //
10 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with PISM; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef __VariableMetadata_hh
20 #define __VariableMetadata_hh
21 
22 #include <set>
23 #include <map>
24 #include <vector>
25 #include <string>
26 
27 #include "Units.hh"
28 
29 #include "pism/util/io/IO_Flags.hh" // IO_Type
30 
31 namespace pism {
32 
33 class Logger;
34 
35 //! @brief A class for handling variable metadata, reading, writing and converting
36 //! from input units and to output units.
37 /*! A NetCDF variable can have any number of attributes, but some of them get
38  special treatment:
39 
40  - units: specifies internal units. When read, a variable is
41  converted to these units. When written, it is converted from these
42  to glaciological_units.
43 
44  - glaciological_units: is never written to a file; replaces 'units'
45  in the output file.
46 
47  - valid_min, valid_max: specify the valid range of a variable. Are
48  read from an input file *only* if not specified previously. If
49  both are set, then valid_range is used in the output instead.
50 
51  Also:
52 
53  - empty string attributes are ignored (they are not written to the
54  output file and has_attribute("foo") returns false if "foo" is
55  absent or equal to an empty string).
56 
57  Typical attributes stored here:
58 
59  - long_name
60  - standard_name
61  - pism_intent
62  - units
63  - glaciological_units (saved to files as "units")
64 
65  Use the `name` of "PISM_GLOBAL" to read and write global attributes.
66  (See also File.)
67 
68 */
69 
70 class VariableMetadata;
71 
73 public:
74  friend class VariableMetadata;
75  ConstAttribute(const ConstAttribute&) = delete;
77 
78  operator std::string() const;
79  operator double() const;
80  operator std::vector<double> () const;
81 protected:
82  ConstAttribute(const VariableMetadata *var, const std::string &name);
83  ConstAttribute(ConstAttribute&& a) noexcept;
84 
85  std::string m_name;
87 };
88 
89 class Attribute : public ConstAttribute {
90 public:
91  friend class VariableMetadata;
92  void operator=(const std::string &value);
93  void operator=(const std::initializer_list<double> &value);
94  void operator=(const std::vector<double> &value);
95 private:
97 };
98 
100 public:
101  VariableMetadata(const std::string &name, units::System::Ptr system, unsigned int ndims = 0);
102  virtual ~VariableMetadata() = default;
103 
104  Attribute operator[](const std::string &name) {
105  return Attribute(this, name);
106  }
107 
108  const ConstAttribute operator[](const std::string &name) const {
109  return ConstAttribute(this, name);
110  }
111 
112  // getters and setters
113  double get_number(const std::string &name) const;
114  void set_number(const std::string &name, double value);
115 
116  std::vector<double> get_numbers(const std::string &name) const;
117  void set_numbers(const std::string &name, const std::vector<double> &values);
118 
119  std::string get_name() const;
120  void set_name(const std::string &name);
121 
122  std::string get_string(const std::string &name) const;
123  void set_string(const std::string &name, const std::string &value);
124  void set_units_without_validation(const std::string &value);
125 
126  bool get_time_independent() const;
127  void set_time_independent(bool flag);
128 
129  IO_Type get_output_type() const;
130  void set_output_type(IO_Type type);
131 
132  void clear_all_doubles();
133  void clear_all_strings();
134 
135  // more getters
137 
138  unsigned int n_spatial_dimensions() const;
139 
140  bool has_attribute(const std::string &name) const;
141  bool has_attributes() const;
142 
143  typedef std::map<std::string,std::string> StringAttrs;
144  const StringAttrs& all_strings() const;
145 
146  typedef std::map<std::string,std::vector<double> > DoubleAttrs;
147  const DoubleAttrs& all_doubles() const;
148 
149  void report_to_stdout(const Logger &log, int verbosity_threshold) const;
150  void check_range(const std::string &filename, double min, double max) const;
151  void report_range(const Logger &log, double min, double max, bool found_by_standard_name);
152 
153 protected:
154  unsigned int m_n_spatial_dims;
155 
156 private:
157  //! @brief The unit system to use.
159 
160  //! string and boolean attributes
161  std::map<std::string, std::string> m_strings;
162 
163  //! scalar and array attributes
164  std::map<std::string, std::vector<double> > m_doubles;
165  std::string m_short_name;
167 
169 };
170 
171 //! Spatial NetCDF variable (corresponding to a 2D or 3D scalar field).
173 public:
174  SpatialVariableMetadata(units::System::Ptr system, const std::string &name,
175  const std::vector<double> &zlevels = {0.0});
176  virtual ~SpatialVariableMetadata() = default;
177 
178  const std::vector<double>& levels() const;
179 
180  VariableMetadata& x();
181  VariableMetadata& y();
182  VariableMetadata& z();
183 
184  const VariableMetadata& x() const;
185  const VariableMetadata& y() const;
186  const VariableMetadata& z() const;
187 
188 private:
190  std::vector<double> m_zlevels;
191 };
192 
193 } // end of namespace pism
194 
195 #endif // __VariableMetadata_hh
void operator=(const std::string &value)
ConstAttribute & operator=(const ConstAttribute &)=delete
ConstAttribute(const ConstAttribute &)=delete
VariableMetadata * m_var
A basic logging class.
Definition: Logger.hh:40
std::vector< double > m_zlevels
const std::vector< double > & levels() const
SpatialVariableMetadata(units::System::Ptr system, const std::string &name, const std::vector< double > &zlevels={0.0})
virtual ~SpatialVariableMetadata()=default
Spatial NetCDF variable (corresponding to a 2D or 3D scalar field).
const ConstAttribute operator[](const std::string &name) const
Attribute operator[](const std::string &name)
void set_numbers(const std::string &name, const std::vector< double > &values)
Set a scalar attribute to a single (scalar) value.
void report_to_stdout(const Logger &log, int verbosity_threshold) const
std::map< std::string, std::string > StringAttrs
unsigned int n_spatial_dimensions() const
void set_time_independent(bool flag)
VariableMetadata(const std::string &name, units::System::Ptr system, unsigned int ndims=0)
void set_units_without_validation(const std::string &value)
units::System::Ptr m_unit_system
The unit system to use.
void set_name(const std::string &name)
double get_number(const std::string &name) const
Get a single-valued scalar attribute.
const DoubleAttrs & all_doubles() const
void set_number(const std::string &name, double value)
Set a scalar attribute to a single (scalar) value.
const StringAttrs & all_strings() const
std::string get_string(const std::string &name) const
Get a string attribute.
void set_output_type(IO_Type type)
virtual ~VariableMetadata()=default
std::map< std::string, std::string > m_strings
string and boolean attributes
void report_range(const Logger &log, double min, double max, bool found_by_standard_name)
Report the range of a global Vec v.
std::map< std::string, std::vector< double > > DoubleAttrs
units::System::Ptr unit_system() const
std::map< std::string, std::vector< double > > m_doubles
scalar and array attributes
bool get_time_independent() const
std::vector< double > get_numbers(const std::string &name) const
Get an array-of-doubles attribute.
IO_Type get_output_type() const
bool has_attribute(const std::string &name) const
std::string get_name() const
void check_range(const std::string &filename, double min, double max) const
Check if the range [min, max] is a subset of [valid_min, valid_max].
void set_string(const std::string &name, const std::string &value)
Set a string attribute.
std::shared_ptr< System > Ptr
Definition: Units.hh:47
double max(const IceModelVec2S &input)
Finds maximum over all the values in an IceModelVec2S object. Ignores ghosts.
IO_Type
Definition: IO_Flags.hh:31
double min(const IceModelVec2S &input)
Finds minimum over all the values in an IceModelVec2S object. Ignores ghosts.