MeteoIODoc 20260811.aeb9aef5
Environmental timeseries pre-processing
Loading...
Searching...
No Matches
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 {
132class ACDD {
133 public:
135 struct ACDD_ATTR; //forward declaration
136 struct VARS_ATTR; //forward declaration
137 typedef struct ACDD_ATTR acdd_attrs;
138 typedef struct VARS_ATTR vars_attr;
139
144 ACDD(const bool& set_enable);
145
146 //defining some iterators so the callers can loop over all available attributes
147 using const_iterator = std::map<std::string, acdd_attrs>::const_iterator;
148 const_iterator cbegin() const noexcept { return attributes.cbegin(); }
149 const_iterator cend() const noexcept { return attributes.cend(); }
150
156 void setEnabled(const bool& i_enable);
157
158 void setUserConfig(const mio::Config& cfg, const std::string& section, const bool& allow_multi_line=true);
159
160 void addAttribute(const std::string& att_name, const std::string& att_value, const Mode& mode=MERGE);
161 void addAttribute(const std::string& att_name, const double& att_value, const Mode& mode=MERGE);
162
167 void deleteAttribute(const std::string& att_name) {attributes.erase( att_name );}
168
173 bool isEnabled() const {return enabled;}
174
175 std::string getAttribute(std::string &att_name) const;
176
177 void setGeometry(const mio::Grid2DObject& grid, const bool& isLatLon);
178 void setGeometry(const std::vector< std::vector<mio::MeteoData> >& vecMeteo, const bool& isLatLon);
179 void setGeometry(const mio::Coords& location, const bool& isLatLon);
180 void setGeometry(const std::set< mio::Coords >& vecLocation, const bool& isLatLon);
181
182 void setTimeCoverage(const std::vector< std::vector<mio::MeteoData> >& vecMeteo);
183 void setTimeCoverage(const std::vector<mio::MeteoData>& vecMeteo);
184 void setTimeCoverage(const std::vector<std::string>& vec_timestamp, const double& TZ);
185
186 //support for JSON metadata
193 void setEnableJson(const bool& i_enable);
194 bool enableJson() const {return enable_json;}
195 void writeJson(const std::string& data_filename) const;
196
197 void addDimension( const std::string& var_name, const std::string& var_long_name, const size_t& length);
198 void addVariable( const std::string& var_name, const std::string& var_long_name, const std::string& var_units);
199
200 std::string toString() const;
201
202 private:
203 static std::map<std::string, acdd_attrs> initAttributes();
204 static std::set< std::pair< std::string, std::set<std::string> > > initLinks();
205 static size_t countCommas(const std::string& str);
206 void checkLinkedAttributes();
207
208 static bool isWigosID(const std::string& str);
209 std::map<std::string, acdd_attrs> attributes; //all the ACDD attributes with their properties
210 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)
211 std::set< vars_attr > dimensions, variables;
212 bool enabled; //helper boolean for callers to know if this object should be used or not
213 bool enable_json; //helper boolean for callers to trigger the writing of json metadata files together with the data file
214};
215
216struct ACDD::VARS_ATTR { //TODO at some point, this should be stored in MeteoData as timeseries metadata
217 VARS_ATTR(const std::string& i_name, const std::string& i_standard_name, const std::string& i_units) : name(i_name), standard_name(i_standard_name), units(i_units), length(IOUtils::npos) {}
218 VARS_ATTR(const std::string& i_name, const std::string& i_standard_name, const size_t& i_length) : name(i_name), standard_name(i_standard_name), units(), length(i_length) {}
219
220 //"units" is ignored here, since with the same name and standard_name, they would have the same units
221 bool operator<(const ACDD::VARS_ATTR& other) const {
222 return (name < other.name) ||
223 (name == other.name && standard_name < other.standard_name) ||
224 (name == other.name && standard_name == other.standard_name && length < other.length);
225 }
226
227 std::string name, standard_name, units;
228 size_t length;
229};
230
231
241 ACDD_ATTR() : name(), value(), cfg_key(), default_value(), Default(true) {}
242
243 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()) {}
244
245 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) {}
246
247 std::string getValue() const {return value;}
248 std::string getName() const {return name;}
249 void setUserConfig(const mio::Config& cfg, const std::string& section, const bool& allow_multi_line);
250 void setValue(const std::string& i_value, const Mode& mode=MERGE);
251 bool isDefault() const {return Default;}
252
253private:
254 static void readFromFile(std::string& value, const mio::Config& cfg, const std::string& cfg_key, const std::string& section, const bool& allow_multi_line);
255
256 std::string name, value, cfg_key, default_value;
257 bool Default;
258};
259
260} //namespace
261#endif
This class contains and handles NetCDF Attribute Conventions Dataset Discovery attributes (see ACDD).
Definition libacdd.h:132
void deleteAttribute(const std::string &att_name)
Delete an attribute (this is required in some rare cases if an attribute is handled separately,...
Definition libacdd.h:167
void addDimension(const std::string &var_name, const std::string &var_long_name, const size_t &length)
Definition libacdd.cc:343
bool enableJson() const
Definition libacdd.h:194
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:260
void setEnableJson(const bool &i_enable)
Should a json file containing the metadata be written alongside the data file?
Definition libacdd.cc:332
std::string toString() const
Definition libacdd.cc:416
void setTimeCoverage(const std::vector< std::vector< mio::MeteoData > > &vecMeteo)
Definition libacdd.cc:683
void addVariable(const std::string &var_name, const std::string &var_long_name, const std::string &var_units)
Definition libacdd.cc:348
std::map< std::string, acdd_attrs >::const_iterator const_iterator
Definition libacdd.h:147
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:280
Mode
Definition libacdd.h:134
@ APPEND
Definition libacdd.h:134
@ REPLACE
Definition libacdd.h:134
@ MERGE
Definition libacdd.h:134
const_iterator cend() const noexcept
Definition libacdd.h:149
void writeJson(const std::string &data_filename) const
Definition libacdd.cc:353
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:437
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:465
const_iterator cbegin() const noexcept
Definition libacdd.h:148
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:173
void setGeometry(const mio::Grid2DObject &grid, const bool &isLatLon)
Definition libacdd.cc:475
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:34
This structure provides low level functions to handle and store individual ACDD fields.
Definition libacdd.h:240
bool isDefault() const
Definition libacdd.h:251
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:207
ACDD_ATTR(const std::string &i_name, const std::string &i_cfg_key, const std::string &i_default_value="")
Definition libacdd.h:243
void setValue(const std::string &i_value, const Mode &mode=MERGE)
Set an ACDD value.
Definition libacdd.cc:239
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:245
std::string getName() const
Definition libacdd.h:248
std::string getValue() const
Definition libacdd.h:247
ACDD_ATTR()
Definition libacdd.h:241
Definition libacdd.h:216
std::string name
Definition libacdd.h:227
VARS_ATTR(const std::string &i_name, const std::string &i_standard_name, const std::string &i_units)
Definition libacdd.h:217
size_t length
Definition libacdd.h:228
std::string units
Definition libacdd.h:227
bool operator<(const ACDD::VARS_ATTR &other) const
Definition libacdd.h:221
VARS_ATTR(const std::string &i_name, const std::string &i_standard_name, const size_t &i_length)
Definition libacdd.h:218
std::string standard_name
Definition libacdd.h:227