MeteoIODoc 2.11.0
PSQLIO.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2012 Mountain-eering Srl, Trento/Bolzano, Italy */
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 PSQLIO_H
20#define PSQLIO_H
21
22#include <meteoio/IOInterface.h>
23
24#include <libpq-fe.h>
25#include <string>
26#include <map>
27
28namespace mio {
29
39class PSQLIO : public IOInterface {
40 public:
41 PSQLIO(const std::string& configfile);
42 PSQLIO(const PSQLIO&);
43 PSQLIO(const Config& cfg);
44
45 PSQLIO& operator=(const PSQLIO& in);
46
47 virtual void readStationData(const Date& date, std::vector<StationData>& vecStation);
48 virtual void readMeteoData(const Date& dateStart, const Date& dateEnd,
49 std::vector< std::vector<MeteoData> >& vecMeteo);
50
51 virtual void writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMeteo,
52 const std::string& name="");
53
54 private:
55 void getParameters(const Config& cfg);
56 void open_connection(const bool& input=true);
57 PGresult* sql_exec(const std::string& sqlcommand, const bool& input=true);
58 static bool replace(std::string& str, const std::string& from, const std::string& to);
59 void readData(const Date& dateStart, const Date& dateEnd, std::vector<MeteoData>& vecMeteo, const size_t& stationindex);
60 void readMetaData(const std::string& query, std::vector<StationData>& vecStation, const bool& input=true);
61 void add_meta_data(const unsigned int& index, const StationData& sd);
62 static void map_parameters(const PGresult* result, MeteoData& md, std::vector<size_t>& index);
63 void parse_row(const PGresult* result, const int& row, const int& cols,
64 MeteoData& md, const std::vector<size_t>& index, std::vector<mio::MeteoData>& vecMeteo) const;
65 void close_connection(PGconn *conn);
66 static bool checkConsistency(const std::vector<MeteoData>& vecMeteo, StationData& sd);
67 static size_t checkExistence(const std::vector<StationData>& vec_stations, const StationData& sd);
68 static void convertUnits(MeteoData& meteo);
69 static void convertUnitsBack(MeteoData& meteo);
70 static void checkForUsedParameters(const std::vector<MeteoData>& vecMeteo, std::vector<bool>& vecParamInUse, std::vector<std::string>& vecColumnName);
71 void add_sensors(const unsigned int& index, const std::vector<std::string>& vecColumnName, std::map<size_t, std::string>& map_sensor_id);
72 int get_sensor_index();
73 int get_measurement_index();
74 void get_sensors(const std::string& index, const std::vector<std::string>& vecColumnName, std::map<size_t, std::string>& map_sensor_id);
75
76 std::string coordin, coordinparam, coordout, coordoutparam; //projection parameters
77 std::string in_endpoint, in_port, in_dbname, in_userid, in_passwd;
78 std::string out_endpoint, out_port, out_dbname, out_userid, out_passwd;
79 bool input_configured, output_configured;
80
81 PGconn *psql;
82 double default_timezone;
83 std::vector<StationData> vecMeta;
84 std::vector<std::string> vecFixedStationID, vecMobileStationID;
85 std::string sql_meta, sql_data;
86
87 static const std::string sqlInsertMetadata, sqlInsertSensor, sqlInsertMeasurement;
88 static const double plugin_nodata; //plugin specific nodata value, e.g. -999
89};
90
91} //namespace
92#endif
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 timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
A class representing the IO Layer of the software Alpine3D. For each type of IO (File,...
Definition: IOInterface.h:98
A class to represent a singular measurement received from one station at a certain time (represented ...
Definition: MeteoData.h:107
This plugin connects to a generic PostgreSQL server to retrieve its meteorological data.
Definition: PSQLIO.h:39
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: PSQLIO.cc:263
virtual void readStationData(const Date &date, std::vector< StationData > &vecStation)
Fill vecStation with StationData objects for a certain date of interest.
Definition: PSQLIO.cc:227
PSQLIO & operator=(const PSQLIO &in)
Definition: PSQLIO.cc:118
PSQLIO(const std::string &configfile)
Definition: PSQLIO.cc:88
virtual void writeMeteoData(const std::vector< std::vector< MeteoData > > &vecMeteo, const std::string &name="")
Write vecMeteo time series to a certain destination.
Definition: PSQLIO.cc:600
A class to represent meteo stations with attributes like longitude, latitude, etc.
Definition: StationData.h:41
Definition: Config.cc:31