WSL/SLF GitLab Repository

NetCDFIO.h 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***********************************************************************************/
/*  Copyright 2009 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
/***********************************************************************************/
/* This file is part of MeteoIO.
    MeteoIO is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MeteoIO is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with MeteoIO.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __NetCDFIO_H__
#define __NetCDFIO_H__

#include <meteoio/IOInterface.h>
#include <meteoio/Config.h>
23
#include <meteoio/ResamplingAlgorithms2D.h>
24
#include <meteoio/meteostats/libinterpol1D.h>
25

26
#include <netcdf.h>
27
#include <string>
28
29
#include <cmath>
#include <cstdio>
30
31
32
33
34

namespace mio {

/**
 * @class NetCDFIO
35
 * @brief This plug-in allows reading and writing of NetCDF files formatted according to CNRM standard.
36
37
38
39
40
41
42
 *
 * @ingroup plugins
 * @author Thomas Egger
 * @date   2014-03-13
 */
class NetCDFIO : public IOInterface {
	public:
43
44
		enum TimeUnit { seconds, hours, days };

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
		NetCDFIO(const std::string& configfile);
		NetCDFIO(const NetCDFIO&);
		NetCDFIO(const Config& cfgreader);
		~NetCDFIO() throw();

		virtual void read2DGrid(Grid2DObject& grid_out, const std::string& parameter="");
		virtual void read2DGrid(Grid2DObject& grid_out, const MeteoGrids::Parameters& parameter, const Date& date);
		virtual void readDEM(DEMObject& dem_out);
		virtual void readLanduse(Grid2DObject& landuse_out);

		virtual void readStationData(const Date& date, std::vector<StationData>& vecStation);
		virtual void readMeteoData(const Date& dateStart, const Date& dateEnd,
		                           std::vector< std::vector<MeteoData> >& vecMeteo,
		                           const size_t& stationindex=IOUtils::npos);

		virtual void writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMeteo,
		                            const std::string& name="");

		virtual void readAssimilationData(const Date&, Grid2DObject& da_out);
		virtual void readPOI(std::vector<Coords>& pts);
		virtual void write2DGrid(const Grid2DObject& grid_in, const std::string& filename);
		virtual void write2DGrid(const Grid2DObject& grid_in, const MeteoGrids::Parameters& parameter, const Date& date);

	private:
69
		void parseInputOutputSection();
70
71
72
73
		void create_parameters(const int& ncid, const int& did_time, const int& did_points, const size_t& number_of_records,
		                       const size_t& number_of_stations, std::map<size_t, std::string>& map_param_name,
		                       std::map<std::string, double*>& map_data_2D, std::map<std::string, int>& varid);
		void create_meta_data(const int& ncid, const int& did, std::map<std::string, double*>& map_data_1D, std::map<std::string, int>& varid);
Thomas Egger's avatar
Thomas Egger committed
74
75
		void get_parameters(const std::vector< std::vector<MeteoData> >& vecMeteo, std::map<size_t, std::string>& map_param_name,
		                    std::map<std::string, double*>& map_data_1D, double*& dates);
76
		void get_parameters(const int& ncid, std::map<std::string, size_t>& map_parameters, MeteoData& meteo_data);
77
78
79
		size_t get_dates(const std::vector< std::vector<MeteoData> >& vecMeteo, double*& dates);
		void copy_data(const size_t& number_of_stations, const size_t& number_of_records, const std::vector< std::vector<MeteoData> >& vecMeteo,
                         const std::map<size_t, std::string>& map_param_name, std::map<std::string, double*>& map_data_2D);
80
81
		void copy_data(const int& ncid, const std::map<std::string, size_t>& map_parameters, const std::map<std::string, double*> map_data, 
		               const size_t& number_of_stations, const size_t& number_of_records, std::vector< std::vector<MeteoData> >& vecMeteo);
82
83
		void readData(const int& ncid, const size_t& index_start, const std::vector<Date>& vec_date, const std::map<std::string, size_t>& map_parameters,
		              const MeteoData& meteo_data, std::vector< std::vector<MeteoData> >& vecMeteo);
84
85
86
		void readMetaData(const int& ncid, std::vector<StationData>& vecStation);
		void copy_grid(const size_t& latlen, const size_t& lonlen, double*& lat, double*& lon, double*& grid, Grid2DObject& grid_out);
		std::string get_varname(const MeteoGrids::Parameters& parameter);
87
88
		void get_indices(const int& ncid, const Date& dateStart, const Date& dateEnd, size_t& indexStart, size_t& indexEnd, std::vector<Date>& vecDate);
		void calculate_offset(const std::string& units, NetCDFIO::TimeUnit& time_unit, Date& offset);
89
90
		void check_consistency(const int& ncid, const Grid2DObject& grid, double*& lat_array, double*& lon_array,
		                       int& did_lat, int& did_lon, int& vid_lat, int& vid_lon);
91
		void read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_name, const std::string& varname, const Date& date=Date());
92
		void write2DGrid_internal(const Grid2DObject& grid_in, const std::string& filename, const std::string& varname, const Date& date=Date());
93
94
		void fill_data(const Grid2DObject& grid, double*& data);
		double calculate_cellsize(const size_t& latlen, const size_t& lonlen, 
95
                                    double const * lat, double const* lon, double& factor_x, double& factor_y);
96
97
98
99
100
		void calculate_dimensions(const Grid2DObject& grid, double*& lat_array, double*& lon_array);
		void add_attributes_for_variable(const int& ncid, const int& varid, const std::string& varname);
		void create_latlon_dimensions(const int& ncid, const Grid2DObject& grid, int& did_lat, int& did_lon, int& vid_lat, int& vid_lon);
		void create_time_dimension(const int& ncid, int& did_time, int& vid_time);

Thomas Egger's avatar
Thomas Egger committed
101

102
		/* libnetcdf wrappers */
Thomas Egger's avatar
Thomas Egger committed
103
104

		//Opening, creating, closing dataset
105
		void open_file(const std::string& filename, const int& omode, int& ncid);
106
		void create_file(const std::string& filename, const int& cmode, int& ncid);
Thomas Egger's avatar
Thomas Egger committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
		void start_definitions(const std::string& filename, const int& ncid);
		void end_definitions(const std::string& filename, const int& ncid);
		void close_file(const std::string& filename, const int& ncid);

		//Adding variables
		void add_0D_variable(const int& ncid, const std::string& varname, const nc_type& xtype, int& varid);
		void add_1D_variable(const int& ncid, const std::string& varname, const nc_type& xtype, const int& dimid, int& varid);
		void add_2D_variable(const int& ncid, const std::string& varname, const nc_type& xtype, const int& dimid1, const int& dimid2, int& varid);
		void add_3D_variable(const int& ncid, const std::string& varname, const nc_type& xtype, const int& dimid_record,
		                     const int& dimid1, const int& dimid2, int& varid);

		//Adding attributes
		void add_attribute(const int& ncid, const int& varid, const std::string& attr_name, const std::string& attr_value);
		void add_attribute(const int& ncid, const int& varid, const std::string& attr_name, const double& attr_value);
121
		void get_attribute(const int& ncid, const std::string& varname, const int& varid, const std::string& attr_name, std::string& attr_value);
122
123
124
125
126
127

		//Adding dimensions
		void add_dimension(const int& ncid, const std::string& dimname, const size_t& length, int& dimid);


		//Reading data from NetCDF file
128
129
		void read_data(const int& ncid, const std::string& varname, const int& varid,
		               const size_t& pos, const size_t& latlen, const size_t& lonlen, double*& data);
130
131
		void read_data_2D(const int& ncid, const std::string& varname, const int& varid,
		                  const size_t& record, const size_t& count, const size_t& length, double*& data);
132
		void read_value(const int& ncid, const std::string& varname, const int& varid, double& data);
133
		void read_data(const int& ncid, const std::string& varname, const int& varid, double*& data);
134
135

		//Writing data to NetCDF file
136
		void write_data(const int& ncid, const std::string& varname, const int& varid, double*& data);
137
		void write_data(const int& ncid, const std::string& varname, const int& varid, const Grid2DObject& grid, const size_t& pos_start, double*& data);
138
139

		//Dealing with variables that have dimension NC_UNLIMITED
140
141
		size_t find_record(const int& ncid, const std::string& varname, const int& varid, const double& data);
		size_t append_record(const int& ncid, const std::string& varname, const int& varid, const double& data);
142
		void write_record(const int& ncid, const std::string& varname, const int& varid, const size_t& length, double*& data);
Thomas Egger's avatar
Thomas Egger committed
143

144
145
146
147
148
149
150
151
152
		//Dealing with variables and dimensions
		bool check_dim_var(const int& ncid, const std::string& dimname);
		bool check_variable(const int& ncid, const std::string& varname);
		size_t get_1D_var_len(const int& ncid, const std::string& varname);
		void get_variable(const int& ncid, const std::string& varname, int& varid);
		void get_dimension(const int& ncid, const std::string& dimname, int& dimid);
		void get_dimension(const int& ncid, const std::string& dimname, int& dimid, size_t& dimlen);
		void get_dimension(const int& ncid, const std::string& varname, const int& varid, 
		                   std::vector<int>& dimid, std::vector<int>& dim_varid, std::vector<std::string>& dimname, std::vector<size_t>& dimlen);
Thomas Egger's avatar
Thomas Egger committed
153

154

Thomas Egger's avatar
Thomas Egger committed
155
		// Private variables
156
		static const double plugin_nodata; //plugin specific nodata value, e.g. -999
157
158
		static const std::string lat_str, lon_str, z_str, ta_str, rh_str;
		static const std::string cf_time, cf_units, cf_days, cf_seconds;
159
		static const std::string cnrm_points, cnrm_latitude, cnrm_longitude, cnrm_altitude, cnrm_aspect, cnrm_slope, cnrm_ta, cnrm_rh, cnrm_vw, cnrm_dw, cnrm_qair;
Thomas Egger's avatar
Thomas Egger committed
160
		static const std::string cnrm_co2air, cnrm_theorsw, cnrm_neb, cnrm_hnw, cnrm_snowf, cnrm_swr_direct, cnrm_swr_diffuse, cnrm_p, cnrm_ilwr, cnrm_timestep;
161

162
		static std::map<std::string, size_t> paramname; ///<Associate a name with meteo parameters in Parameters
163
		static std::map<std::string, std::string> map_name; ///Associate MeteoIO parameter names with CNRM parameter names
164
165
166
		static const bool __init;    ///<helper variable to enable the init of static collection data
		static bool initStaticData();///<initialize the static map

Thomas Egger's avatar
Thomas Egger committed
167
		const Config cfg;
168
		std::string coordin, coordinparam, coordout, coordoutparam; //projection parameters
169
		double in_dflt_TZ, out_dflt_TZ;     //default time zones
170
		std::vector<StationData> vecMetaData;
171
172
173
174
};

} //namespace
#endif