MeteoIODoc 20241221.207bde49
libacdd.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2020 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef LIBACDD_H
20#define LIBACDD_H
21
22#include <meteoio/IOUtils.h>
23#include <meteoio/Config.h>
26
27#include <string>
28#include <vector>
29
30namespace mio {
115class ACDD {
116 public:
118 struct ACDD_ATTR; //forward declaration
119 typedef struct ACDD_ATTR acdd_attrs;
120
125 ACDD(const bool& set_enable);
126
127 //defining some iterators so the callers can loop over all available attributes
128 using const_iterator = std::map<std::string, acdd_attrs>::const_iterator;
129 const_iterator cbegin() const noexcept { return attributes.cbegin(); }
130 const_iterator cend() const noexcept { return attributes.cend(); }
131
137 void setEnabled(const bool& i_enable);
138 void setUserConfig(const mio::Config& cfg, const std::string& section, const bool& allow_multi_line=true);
139
140 void addAttribute(const std::string& att_name, const std::string& att_value, const Mode& mode=MERGE);
141 void addAttribute(const std::string& att_name, const double& att_value, const Mode& mode=MERGE);
142
147 bool isEnabled() const {return enabled;}
148 std::string getAttribute(std::string &att_name) const;
149
150 void setGeometry(const mio::Grid2DObject& grid, const bool& isLatLon);
151 void setGeometry(const std::vector< std::vector<mio::MeteoData> >& vecMeteo, const bool& isLatLon);
152 void setGeometry(const mio::Coords& location, const bool& isLatLon);
153 void setGeometry(const std::vector< mio::Coords >& vecLocation, const bool& isLatLon);
154
155 void setTimeCoverage(const std::vector< std::vector<mio::MeteoData> >& vecMeteo);
156 void setTimeCoverage(const std::vector<mio::MeteoData>& vecMeteo);
157 void setTimeCoverage(const std::vector<std::string>& vec_timestamp, const double& TZ);
158
159 std::string toString() const;
160
161 private:
162 static std::map<std::string, acdd_attrs> initAttributes();
163 static std::set< std::pair< std::string, std::set<std::string> > > initLinks();
164 static size_t countCommas(const std::string& str);
165 void checkLinkedAttributes();
166
167 std::map<std::string, acdd_attrs> attributes; //all the ACDD attributes with their properties
168 std::set< std::pair< std::string, std::set<std::string> > > linked_attributes; //attribute names that are linked together, ie must have the same number of sub-elements (comma delimited)
169 bool enabled; //helper boolean for callers to know if this object should be used or not
170};
171
172
182 ACDD_ATTR() : name(), value(), cfg_key(), default_value(), Default(true) {}
183
184 ACDD_ATTR(const std::string& i_name, const std::string& i_cfg_key, const std::string& i_default_value="") : name(i_name), value(), cfg_key(i_cfg_key), default_value(i_default_value), Default(i_name.empty()) {}
185
186 ACDD_ATTR(const std::string& i_name, const std::string& i_value, const std::string& i_cfg_key, const std::string& i_default_value) : name(i_name), value(i_value), cfg_key(i_cfg_key), default_value(i_default_value), Default(false) {}
187
188 std::string getValue() const {return value;}
189 std::string getName() const {return name;}
190 void setUserConfig(const mio::Config& cfg, const std::string& section, const bool& allow_multi_line);
191 void setValue(const std::string& i_value, const Mode& mode=MERGE);
192 bool isDefault() const {return Default;}
193
194private:
195 static void readFromFile(std::string& value, const mio::Config& cfg, const std::string& cfg_key, const std::string& section, const bool& allow_multi_line);
196
197 std::string name, value, cfg_key, default_value;
198 bool Default;
199};
200
201} //namespace
202#endif
This class contains and handles NetCDF Attribute Conventions Dataset Discovery attributes (see ACDD).
Definition: libacdd.h:115
void setUserConfig(const mio::Config &cfg, const std::string &section, const bool &allow_multi_line=true)
Read all config keys from the selected section and apply some special processing for some keys.
Definition: libacdd.cc:209
ACDD(const bool &set_enable)
Constructor, the argument allows the object to know if the acdd metadata should be written out or not...
Definition: libacdd.cc:38
std::string toString() const
Definition: libacdd.cc:274
void setTimeCoverage(const std::vector< std::vector< mio::MeteoData > > &vecMeteo)
Definition: libacdd.cc:523
std::map< std::string, acdd_attrs >::const_iterator const_iterator
Definition: libacdd.h:128
void setEnabled(const bool &i_enable)
Set an internal boolean as a helper for the caller to know if ACDD support should be enabled or not....
Definition: libacdd.cc:222
Mode
Definition: libacdd.h:117
@ APPEND
Definition: libacdd.h:117
@ REPLACE
Definition: libacdd.h:117
@ MERGE
Definition: libacdd.h:117
const_iterator cend() const noexcept
Definition: libacdd.h:130
void addAttribute(const std::string &att_name, const std::string &att_value, const Mode &mode=MERGE)
Add an attribute and its content to the internal list.
Definition: libacdd.cc:294
std::string getAttribute(std::string &att_name) const
Given an attribute name, return its associated value (or an empty string if it does not exists)
Definition: libacdd.cc:322
const_iterator cbegin() const noexcept
Definition: libacdd.h:129
bool isEnabled() const
Get an internal boolean as a helper for the caller to know if ACDD support should be enabled or not.
Definition: libacdd.h:147
void setGeometry(const mio::Grid2DObject &grid, const bool &isLatLon)
Definition: libacdd.cc:330
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s...
Definition: Config.h:79
A class to handle geographic coordinate systems. This class offers an easy way to transparently conve...
Definition: Coords.h:83
A class to represent 2D Grids. Typical application as DEM or Landuse Model.
Definition: Grid2DObject.h:42
Definition: Config.cc:31
This structure provides low level functions to handle and store individual ACDD fields.
Definition: libacdd.h:181
bool isDefault() const
Definition: libacdd.h:192
void setUserConfig(const mio::Config &cfg, const std::string &section, const bool &allow_multi_line)
Set the value of the attribute, either from a config file key, or from an environment variable of the...
Definition: libacdd.cc:156
ACDD_ATTR(const std::string &i_name, const std::string &i_cfg_key, const std::string &i_default_value="")
Definition: libacdd.h:184
void setValue(const std::string &i_value, const Mode &mode=MERGE)
Set an ACDD value.
Definition: libacdd.cc:188
ACDD_ATTR(const std::string &i_name, const std::string &i_value, const std::string &i_cfg_key, const std::string &i_default_value)
Definition: libacdd.h:186
std::string getName() const
Definition: libacdd.h:189
std::string getValue() const
Definition: libacdd.h:188
ACDD_ATTR()
Definition: libacdd.h:182