WSL/SLF GitLab Repository

NetCDFIO.h 8.09 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

25
#include <netcdf.h>
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>

namespace mio {

/**
 * @class NetCDFIO
 * @brief This (empty) class is to be used as a template for developing new plugins
 *
 * @ingroup plugins
 * @author Thomas Egger
 * @date   2014-03-13
 */
class NetCDFIO : public IOInterface {
	public:
40
41
		enum TimeUnit { seconds, hours, days };

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
		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:
66
		void parseInputOutputSection();
67
68
69
		void get_parameters(const int& ncid, std::map<std::string, size_t>& map_parameters, MeteoData& meteo_data);
		void copy_data(const int& ncid, 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);
		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);
70
71
72
		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);
73
74
		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);
75
76
		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);
77
		void open_file(const std::string& filename, const int& omode, int& ncid);
78
		void create_file(const std::string& filename, const int& cmode, int& ncid);
79
80
		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);
81
		void get_variable(const int& ncid, const std::string& varname, int& varid);
82
		bool check_variable(const int& ncid, const std::string& varname);
83
		bool check_dim_var(const int& ncid, const std::string& dimname);
84
		size_t get_1D_var_len(const int& ncid, const std::string& varname);
85
86
		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);
87
88
		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);
89
		void get_attribute(const int& ncid, const std::string& varname, const int& varid, const std::string& attr_name, std::string& attr_value);
90
91
		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);
92
93
		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);
94
		void read_value(const int& ncid, const std::string& varname, const int& varid, double& data);
95
		void read_data(const int& ncid, const std::string& varname, const int& varid, double*& data);
96
		void write_data(const int& ncid, const std::string& varname, const int& varid, double*& data);
97
98
99
		void write_data(const int& ncid, const std::string& varname, const int& varid, const Grid2DObject& grid, const size_t& pos_start, double*& data);
		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);
100
101
102
103
		void define_dimension(const int& ncid, const std::string& dimname, const size_t& length, int& dimid);
		void add_attribute(const int& ncid, const int& varid, const std::string& attr_name, const std::string& attr_value);
		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);
104
		void add_2D_record(const int& ncid, const std::string& varname, const nc_type& xtype, const int& dimid_record, const int& dimid1, const int& dimid2, int& varid);
105
		void add_attributes_for_variable(const int& ncid, const int& varid, const std::string& varname);
106
		void start_definitions(const std::string& filename, const int& ncid);
107
		void end_definitions(const std::string& filename, const int& ncid);
108
109
		void close_file(const std::string& filename, const int& ncid);
		void read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_name, const std::string& varname);
110
111
112
113
		double calculate_cellsize(const size_t& latlen, const size_t& lonlen, 
                                    double* const& lat, double* const& lon, double& factor);
		void calculate_dimensions(const Grid2DObject& grid, double*& lat_array, double*& lon_array);
		void fill_data(const Grid2DObject& grid, double*& data);
114
115
116

		const Config cfg;
		static const double plugin_nodata; //plugin specific nodata value, e.g. -999
117
118
		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;
119
120
		static const std::string cnrm_altitude, cnrm_aspect, cnrm_slope;
		static std::map<std::string, size_t> paramname; ///<Associate a name with meteo parameters in Parameters
121
122
123
		static const bool __init;    ///<helper variable to enable the init of static collection data
		static bool initStaticData();///<initialize the static map

124
		std::string coordin, coordinparam, coordout, coordoutparam; //projection parameters
125
		double in_dflt_TZ, out_dflt_TZ;     //default time zones
126
		std::vector<StationData> vecMetaData;
127
128
129
130
};

} //namespace
#endif