MeteoIODoc 20241221.207bde49
NetCDFIO.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2014 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 NetCDFIO_H
20#define NetCDFIO_H
21
22#include <meteoio/IOInterface.h>
24
25#include <string>
26
27namespace mio {
28
29class ncFiles {
30 public:
31 enum Mode {READ, WRITE};
32
33 ncFiles(const std::string& filename, const Mode& mode, const Config& cfg, const std::string& schema_name, const bool& i_debug=false);
34 ncFiles(const ncFiles& c);
35 ncFiles& operator = (const ncFiles& c);
36 ~ncFiles();
37
38 std::pair<Date, Date> getDateRange() const;
39 std::set<size_t> getParams() const;
40 std::vector<Date> getTimestamps() const;
41 Grid2DObject read2DGrid(const size_t& param, const Date& date);
42 Grid2DObject read2DGrid(const std::string& varname);
43 std::vector< double > readPointsIn2DGrid(const size_t& param, const Date& date, const std::vector< std::pair<size_t, size_t> >& Pts);
44
45 void write2DGrid(const Grid2DObject& grid_in, ncpp::nc_variable& var, const Date& date);
46 void write2DGrid(const Grid2DObject& grid_in, size_t param, std::string param_name, const Date& date);
47
48 void writeMeteo(const std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& station_idx=IOUtils::npos);
49
50 std::vector<StationData> readStationData();
51 std::vector< std::vector<MeteoData> > readMeteoData(const Date& dateStart, const Date& dateEnd);
52
53 bool hasDimension(const size_t& dim) const;
54
55 private:
56 void initFromFile(const std::string& filename);
57 void initVariablesFromFile();
58 void initDimensionsFromFile();
59
60 Grid2DObject read2DGrid(const ncpp::nc_variable& var, const size_t& time_pos, const bool& m2mm=false, const bool& reZero=false);
61 std::vector< double > readPointsIn2DGrid(const ncpp::nc_variable& var, const size_t& time_pos, const std::vector< std::pair<size_t, size_t> >& Pts, const bool& m2mm=false);
62 double read_0Dvariable(const size_t& param) const;
63 std::vector<Date> read_1Dvariable() const;
64 std::vector<double> read_1Dvariable(const size_t& param) const;
65 std::vector<std::string> read_1Dstringvariable(const size_t& param) const;
66 std::vector<std::string> read_stationIDs() const;
67 std::vector< std::pair<size_t, std::string> > getTSParameters() const;
68 size_t read_1DvariableLength(const ncpp::nc_variable& var) const;
69 size_t readDimension(const int& dimid) const;
70 bool hasVariable(const size_t& var) const;
71
72 void writeGridMetadataHeader(const Grid2DObject& grid_in);
73 void writeMeteoMetadataHeader(const std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& station_idx);
74 static Date getRefDate(const std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& station_idx);
75 static std::vector< std::pair<Date,size_t> > createCommonTimeBase(const std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& station_idx);
76 static void pushVar(std::vector<size_t> &nc_variables, const size_t& param);
77 size_t addToVars(const size_t& param);
78 size_t addToVars(const std::string& name);
79 void appendVariablesList(std::vector<size_t> &nc_variables, const std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& station_idx);
80 bool setAssociatedVariable(const size_t& param, const Date& ref_date);
81 size_t addTimestamp(const Date& date);
82 const std::vector<double> fillBufferForAssociatedVar(const std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& station_idx, const ncpp::nc_variable& var) const;
83 const std::vector<double> fillBufferForVar(const std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& station_idx, const ncpp::nc_variable& var) const;
84 static const std::vector<double> fillBufferForVar(const Grid2DObject& grid, ncpp::nc_variable& var);
85 void applyUnits(Grid2DObject& grid, const std::string& units, const size_t& time_pos, const bool& m2mm) const;
86 void applyUnits(double& val, const std::string& units, const size_t& time_pos, const bool& m2mm) const;
87 static void applyUnits(std::vector< std::vector<MeteoData> >& vecMeteo, const size_t& nrStations, const size_t& nrSteps, const std::string& units, const std::string& parname);
88 size_t getParameterIndex(const std::string& param_name);
89
90 ACDD acdd;
91 NC_SCHEMA schema;
92 std::map<size_t, ncpp::nc_variable> vars;
93 std::map<std::string, ncpp::nc_variable> unknown_vars;
94 std::vector< std::pair<Date,size_t> > vecTime; //date and index in the NetCDF time vector to accomodate unsorted time base
95 std::vector<double> vecX, vecY;
96 std::map<size_t, ncpp::nc_dimension> dimensions_map;
97 std::string file_and_path, coord_sys, coord_param;
98 double TZ;
99 double dflt_zref, dflt_uref;
100 double dflt_slope, dflt_azi;
101 size_t max_unknown_param_idx;
102 bool strict_schema, lax_schema, debug, isLatLon;
103 std::string nc_filename;
104 int ncid;
105 bool keep_input_files_open, keep_output_files_open, allow_missing_coords;
106};
107
114class NetCDFIO : public IOInterface {
115 public:
116 NetCDFIO(const std::string& configfile);
118 NetCDFIO(const Config& cfgreader);
119
120 virtual bool list2DGrids(const Date& start, const Date& end, std::map<Date, std::set<size_t> >& list);
121 virtual void read2DGrid(Grid2DObject& grid_out, const std::string& parameter="");
122 virtual void read2DGrid(Grid2DObject& grid_out, const MeteoGrids::Parameters& parameter, const Date& date);
123 virtual void readPointsIn2DGrid(std::vector<double>& data, const MeteoGrids::Parameters& parameter, const Date& date, const std::vector< std::pair<size_t, size_t> >& Pts);
124 virtual void readDEM(DEMObject& dem_out);
125
126 virtual void write2DGrid(const Grid2DObject& grid_in, const std::string& filename);
127 virtual void write2DGrid(const Grid2DObject& grid_in, const MeteoGrids::Parameters& parameter, const Date& date);
128
129 virtual void writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMeteo, const std::string& name="");
130
131 virtual void readStationData(const Date& date, std::vector<StationData>& vecStation);
132 virtual void readMeteoData(const Date& dateStart, const Date& dateEnd,
133 std::vector< std::vector<MeteoData> >& vecMeteo);
134
135 private:
136 void parseInputOutputSection();
137 void scanPath(const std::string& in_path, const std::string& nc_ext, std::vector< std::pair<std::pair<Date,Date>, ncFiles> > &nc_files);
138 void cleanMeteoCache(std::vector< std::pair<std::pair<Date,Date>, ncFiles> > &meteo_files);
139
140 const Config cfg;
141 std::vector< std::pair<std::pair<Date,Date>, ncFiles> > cache_grid_files; //cache of grid files in GRID2DPATH
142 std::map<std::string, ncFiles> cache_grids_out; //cache of output GRID2D files
143 std::vector< ncFiles > cache_inmeteo_files; //cache of meteo files in input METEOPATH
144 std::set<std::string> in_stations;
145 std::vector<MeteoGrids::Parameters> available_params;
146 std::string in_schema_grid, out_schema_grid, in_schema_meteo, out_schema_meteo, in_grid2d_path, in_nc_ext, out_grid2d_path, grid2d_out_file;
147 std::string out_meteo_path, out_meteo_file;
148 bool debug, out_single_file;
149 bool split_by_year, split_by_var;
150};
151
152} //namespace
153#endif
This class contains and handles NetCDF schemas.
Definition: libncpp.h:144
This class contains and handles NetCDF Attribute Conventions Dataset Discovery attributes (see ACDD).
Definition: libacdd.h:115
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 represent DEMs and automatically compute some properties. This class stores elevation grid...
Definition: DEMObject.h:40
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
A class to represent 2D Grids. Typical application as DEM or Landuse Model.
Definition: Grid2DObject.h:42
A class representing the IO Layer of the software Alpine3D. For each type of IO (File,...
Definition: IOInterface.h:98
Parameters
this enum provides names for possible meteogrids (from an ARPS file, etc)
Definition: MeteoData.h:46
This plug-in allows reading and writing of NetCDF files for gridded data.
Definition: NetCDFIO.h:114
virtual void readMeteoData(const Date &dateStart, const Date &dateEnd, std::vector< std::vector< MeteoData > > &vecMeteo)
Fill vecMeteo with a time series of objects corresponding to the interval indicated by dateStart and ...
Definition: NetCDFIO.cc:677
virtual bool list2DGrids(const Date &start, const Date &end, std::map< Date, std::set< size_t > > &list)
Return the list of grids within a given time period that could be read by the plugin,...
Definition: NetCDFIO.cc:457
virtual void read2DGrid(Grid2DObject &grid_out, const std::string &parameter="")
A generic function for parsing 2D grids into a Grid2DObject. The string parameter shall be used for a...
Definition: NetCDFIO.cc:483
virtual void readPointsIn2DGrid(std::vector< double > &data, const MeteoGrids::Parameters &parameter, const Date &date, const std::vector< std::pair< size_t, size_t > > &Pts)
Read the given meteo parameter into a vector for a list of points. Each plugin has its own logic for ...
Definition: NetCDFIO.cc:531
virtual void readStationData(const Date &date, std::vector< StationData > &vecStation)
Fill vecStation with StationData objects for a certain date of interest.
Definition: NetCDFIO.cc:659
virtual void readDEM(DEMObject &dem_out)
Parse the DEM (Digital Elevation Model) into the Grid2DObject.
Definition: NetCDFIO.cc:563
NetCDFIO(const NetCDFIO &)
virtual void write2DGrid(const Grid2DObject &grid_in, const std::string &filename)
Write a Grid2DObject The filename is specified relative to GRID2DPATH for most plugins.
Definition: NetCDFIO.cc:575
virtual void writeMeteoData(const std::vector< std::vector< MeteoData > > &vecMeteo, const std::string &name="")
Write vecMeteo time series to a certain destination.
Definition: NetCDFIO.cc:635
NetCDFIO(const std::string &configfile)
Definition: NetCDFIO.cc:342
Definition: NetCDFIO.h:29
Grid2DObject read2DGrid(const size_t &param, const Date &date)
Definition: NetCDFIO.cc:930
~ncFiles()
Definition: NetCDFIO.cc:806
Mode
Definition: NetCDFIO.h:31
@ WRITE
Definition: NetCDFIO.h:31
@ READ
Definition: NetCDFIO.h:31
std::vector< StationData > readStationData()
Definition: NetCDFIO.cc:1293
std::vector< std::vector< MeteoData > > readMeteoData(const Date &dateStart, const Date &dateEnd)
Definition: NetCDFIO.cc:1440
std::vector< Date > getTimestamps() const
Definition: NetCDFIO.cc:870
void writeMeteo(const std::vector< std::vector< MeteoData > > &vecMeteo, const size_t &station_idx=IOUtils::npos)
Definition: NetCDFIO.cc:1160
std::set< size_t > getParams() const
Definition: NetCDFIO.cc:860
ncFiles(const std::string &filename, const Mode &mode, const Config &cfg, const std::string &schema_name, const bool &i_debug=false)
Definition: NetCDFIO.cc:701
void write2DGrid(const Grid2DObject &grid_in, ncpp::nc_variable &var, const Date &date)
Definition: NetCDFIO.cc:1088
bool hasDimension(const size_t &dim) const
Definition: NetCDFIO.cc:2218
ncFiles & operator=(const ncFiles &c)
Definition: NetCDFIO.cc:773
std::pair< Date, Date > getDateRange() const
Definition: NetCDFIO.cc:854
std::vector< double > readPointsIn2DGrid(const size_t &param, const Date &date, const std::vector< std::pair< size_t, size_t > > &Pts)
Definition: NetCDFIO.cc:1005
const size_t npos
npos is the out-of-range value
Definition: IOUtils.h:80
Definition: Config.cc:31