MeteoIODoc 20250905.f67af007
 
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 {
121class ACDD {
122 public:
124 struct ACDD_ATTR; //forward declaration
125 struct VARS_ATTR; //forward declaration
126 typedef struct ACDD_ATTR acdd_attrs;
127 typedef struct VARS_ATTR vars_attr;
128
133 ACDD(const bool& set_enable);
134
135 //defining some iterators so the callers can loop over all available attributes
136 using const_iterator = std::map<std::string, acdd_attrs>::const_iterator;
137 const_iterator cbegin() const noexcept { return attributes.cbegin(); }
138 const_iterator cend() const noexcept { return attributes.cend(); }
139
145 void setEnabled(const bool& i_enable);
146
147 void setUserConfig(const mio::Config& cfg, const std::string& section, const bool& allow_multi_line=true);
148
149 void addAttribute(const std::string& att_name, const std::string& att_value, const Mode& mode=MERGE);
150 void addAttribute(const std::string& att_name, const double& att_value, const Mode& mode=MERGE);
151
156 void deleteAttribute(const std::string& att_name) {attributes.erase( att_name );}
157
162 bool isEnabled() const {return enabled;}
163
164 std::string getAttribute(std::string &att_name) const;
165
166 void setGeometry(const mio::Grid2DObject& grid, const bool& isLatLon);
167 void setGeometry(const std::vector< std::vector<mio::MeteoData> >& vecMeteo, const bool& isLatLon);
168 void setGeometry(const mio::Coords& location, const bool& isLatLon);
169 void setGeometry(const std::set< mio::Coords >& vecLocation, const bool& isLatLon);
170
171 void setTimeCoverage(const std::vector< std::vector<mio::MeteoData> >& vecMeteo);
172 void setTimeCoverage(const std::vector<mio::MeteoData>& vecMeteo);
173 void setTimeCoverage(const std::vector<std::string>& vec_timestamp, const double& TZ);
174
175
176 //support for NcML
184 void setEnableNcML(const bool& i_enable);
185 bool enableNcML() const {return enable_ncml;}
186 void writeNcML(const std::string& data_filename) const;
187 void addDimension( const std::string& var_name, const std::string& var_long_name, const size_t& length);
188 void addVariable( const std::string& var_name, const std::string& var_long_name, const std::string& var_units);
189
190 std::string toString() const;
191
192 private:
193 static std::map<std::string, acdd_attrs> initAttributes();
194 static std::set< std::pair< std::string, std::set<std::string> > > initLinks();
195 static size_t countCommas(const std::string& str);
196 void checkLinkedAttributes();
197
198 std::map<std::string, acdd_attrs> attributes; //all the ACDD attributes with their properties
199 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)
200 std::set< vars_attr > dimensions, variables;
201 bool enabled; //helper boolean for callers to know if this object should be used or not
202 bool enable_ncml; //helper boolean for callers to trigger the writing of NcML files together with the data file
203};
204
205struct ACDD::VARS_ATTR { //TODO at some point, this should be stored in MeteoData as timeseries metadata
206 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) {}
207 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) {}
208
209 //"units" is ignored here, since with the same name and standard_name, they would have the same units
210 bool operator<(const ACDD::VARS_ATTR& other) const {
211 return (name < other.name) ||
212 (name == other.name && standard_name < other.standard_name) ||
213 (name == other.name && standard_name == other.standard_name && length < other.length);
214 }
215
216 std::string name, standard_name, units;
217 size_t length;
218};
219
220
230 ACDD_ATTR() : name(), value(), cfg_key(), default_value(), Default(true) {}
231
232 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()) {}
233
234 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) {}
235
236 std::string getValue() const {return value;}
237 std::string getName() const {return name;}
238 void setUserConfig(const mio::Config& cfg, const std::string& section, const bool& allow_multi_line);
239 void setValue(const std::string& i_value, const Mode& mode=MERGE);
240 bool isDefault() const {return Default;}
241
242private:
243 static void readFromFile(std::string& value, const mio::Config& cfg, const std::string& cfg_key, const std::string& section, const bool& allow_multi_line);
244
245 std::string name, value, cfg_key, default_value;
246 bool Default;
247};
248
249} //namespace
250#endif
This class contains and handles NetCDF Attribute Conventions Dataset Discovery attributes (see ACDD).
Definition libacdd.h:121
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:156
void addDimension(const std::string &var_name, const std::string &var_long_name, const size_t &length)
Definition libacdd.cc:287
void setEnableNcML(const bool &i_enable)
Should an NcML file be written alongside the data file?
Definition libacdd.cc:276
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:211
std::string toString() const
Definition libacdd.cc:347
void writeNcML(const std::string &data_filename) const
Definition libacdd.cc:297
void setTimeCoverage(const std::vector< std::vector< mio::MeteoData > > &vecMeteo)
Definition libacdd.cc:609
void addVariable(const std::string &var_name, const std::string &var_long_name, const std::string &var_units)
Definition libacdd.cc:292
std::map< std::string, acdd_attrs >::const_iterator const_iterator
Definition libacdd.h:136
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:224
Mode
Definition libacdd.h:123
@ APPEND
Definition libacdd.h:123
@ REPLACE
Definition libacdd.h:123
@ MERGE
Definition libacdd.h:123
const_iterator cend() const noexcept
Definition libacdd.h:138
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:368
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:396
const_iterator cbegin() const noexcept
Definition libacdd.h:137
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:162
void setGeometry(const mio::Grid2DObject &grid, const bool &isLatLon)
Definition libacdd.cc:404
bool enableNcML() const
Definition libacdd.h:185
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:229
bool isDefault() const
Definition libacdd.h:240
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:158
ACDD_ATTR(const std::string &i_name, const std::string &i_cfg_key, const std::string &i_default_value="")
Definition libacdd.h:232
void setValue(const std::string &i_value, const Mode &mode=MERGE)
Set an ACDD value.
Definition libacdd.cc:190
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:234
std::string getName() const
Definition libacdd.h:237
std::string getValue() const
Definition libacdd.h:236
ACDD_ATTR()
Definition libacdd.h:230
Definition libacdd.h:205
std::string name
Definition libacdd.h:216
VARS_ATTR(const std::string &i_name, const std::string &i_standard_name, const std::string &i_units)
Definition libacdd.h:206
size_t length
Definition libacdd.h:217
std::string units
Definition libacdd.h:216
bool operator<(const ACDD::VARS_ATTR &other) const
Definition libacdd.h:210
VARS_ATTR(const std::string &i_name, const std::string &i_standard_name, const size_t &i_length)
Definition libacdd.h:207
std::string standard_name
Definition libacdd.h:216