URI:
       tVariableMetadata.hh - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
  HTML git clone git://src.adamsgaard.dk/pism
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
       tVariableMetadata.hh (5789B)
       ---
            1 // Copyright (C) 2009--2018, 2020 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 public:
           72   VariableMetadata(const std::string &name, units::System::Ptr system, unsigned int ndims = 0);
           73   virtual ~VariableMetadata();
           74 
           75   // setters
           76   void set_number(const std::string &name, double value);
           77   void set_numbers(const std::string &name, const std::vector<double> &values);
           78   void set_name(const std::string &name);
           79   void set_string(const std::string &name, const std::string &value);
           80 
           81   void set_time_independent(bool flag);
           82   void set_output_type(IO_Type type);
           83 
           84   void clear_all_doubles();
           85   void clear_all_strings();
           86 
           87   // getters
           88   units::System::Ptr unit_system() const;
           89 
           90   double get_number(const std::string &name) const;
           91   std::vector<double> get_numbers(const std::string &name) const;
           92   std::string get_name() const;
           93   std::string get_string(const std::string &name) const;
           94 
           95   unsigned int get_n_spatial_dimensions() const;
           96 
           97   bool has_attribute(const std::string &name) const;
           98   bool has_attributes() const;
           99   bool get_time_independent() const;
          100   IO_Type get_output_type() const;
          101 
          102   typedef std::map<std::string,std::string> StringAttrs;
          103   const StringAttrs& get_all_strings() const;
          104 
          105   typedef std::map<std::string,std::vector<double> > DoubleAttrs;
          106   const DoubleAttrs& get_all_doubles() const;
          107 
          108   void report_to_stdout(const Logger &log, int verbosity_threshold) const;
          109   void check_range(const std::string &filename, double min, double max);
          110   void report_range(const Logger &log, double min, double max, bool found_by_standard_name);
          111 
          112 protected:
          113   unsigned int m_n_spatial_dims;
          114 
          115 private:
          116   //! @brief The unit system to use.
          117   units::System::Ptr m_unit_system;
          118 
          119   //! string and boolean attributes
          120   std::map<std::string, std::string> m_strings;
          121 
          122   //! scalar and array attributes
          123   std::map<std::string, std::vector<double> > m_doubles;
          124   std::string m_short_name;
          125   bool m_time_independent;
          126 
          127   IO_Type m_output_type;
          128 };
          129 
          130 bool set_contains(const std::set<std::string> &S, const VariableMetadata &field);
          131 
          132 //! Spatial NetCDF variable (corresponding to a 2D or 3D scalar field).
          133 class SpatialVariableMetadata : public VariableMetadata {
          134 public:
          135   SpatialVariableMetadata(units::System::Ptr system, const std::string &name);
          136   SpatialVariableMetadata(units::System::Ptr system, const std::string &name,
          137                           const std::vector<double> &zlevels);
          138   virtual ~SpatialVariableMetadata();
          139 
          140   void set_levels(const std::vector<double> &levels);
          141   const std::vector<double>& get_levels() const;
          142 
          143   VariableMetadata& get_x();
          144   VariableMetadata& get_y();
          145   VariableMetadata& get_z();
          146 
          147   const VariableMetadata& get_x() const;
          148   const VariableMetadata& get_y() const;
          149   const VariableMetadata& get_z() const;
          150 
          151 private:
          152   VariableMetadata m_x, m_y, m_z;
          153   std::vector<double> m_zlevels;
          154   void init_internal(const std::string &name,
          155                      const std::vector<double> &z_levels);
          156 };
          157 
          158 //! An internal class for reading, writing and converting time-series.
          159 class TimeseriesMetadata : public VariableMetadata {
          160 public:
          161   TimeseriesMetadata(const std::string &name, const std::string &dimension_name,
          162                      units::System::Ptr system);
          163   virtual ~TimeseriesMetadata();
          164 
          165   std::string get_dimension_name() const;
          166 private:
          167   //! the name of the NetCDF dimension this timeseries depends on
          168   std::string m_dimension_name;
          169 };
          170 
          171 class TimeBoundsMetadata : public TimeseriesMetadata
          172 {
          173 public:
          174   TimeBoundsMetadata(const std::string &name, const std::string &dimension_name,
          175                      units::System::Ptr system);
          176   virtual ~TimeBoundsMetadata();
          177   std::string get_bounds_name() const;
          178 private:
          179   std::string m_bounds_name;
          180 };
          181 
          182 } // end of namespace pism
          183 
          184 #endif  // __VariableMetadata_hh