WSL/SLF GitLab Repository

Buffer.h 6.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/***********************************************************************************/
/*  Copyright 2014 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 __BUFFER_H__
#define __BUFFER_H__

21
22
#include <meteoio/dataClasses/Grid2DObject.h>
#include <meteoio/dataClasses/DEMObject.h>
23
24
25
26
27
#include <meteoio/dataClasses/Date.h>
#include <meteoio/dataClasses/MeteoData.h>

namespace mio {

28
29
30
31
32
33
34
35
36
37
/**
 * @class MeteoBuffer
 * @brief A class to buffer meteorological data.
 * This class buffers MeteoData objects. It is currently NOT a proper ring buffer, this should come
 * in a later implementation.
 *
 * @ingroup data_str
 * @author Mathias Bavay
 * @date   2014-12-01
*/
38
39
40
41
class MeteoBuffer {
	public:
		MeteoBuffer() : ts_buffer(), ts_start(), ts_end() {};

42
43
44
45
46
47
		/**
		 * @brief Get buffer data for a specific date
		 * @param date        A Date object representing the date/time for the sought MeteoData objects
		 * @param vecMeteo    A vector of MeteoData objects to be filled with data
		 * @return            true if the data was in the buffer
		 */
48
		bool get(const Date& date, METEO_SET &vecMeteo) const;
49
50
51
52
53
54
55
56

		/**
		 * @brief Get buffer data between two dates
		 * @param date_start      A Date object representing the beginning of an interval (inclusive)
		 * @param date_end        A Date object representing the end of an interval (inclusive)
		 * @param vecMeteo        A vector of vector<MeteoData> objects to be filled with data
		 * @return            true if the data was in the buffer
		 */
57
		bool get(const Date& date_start, const Date& date_end, std::vector< METEO_SET > &vecMeteo) const;
58
59
60
61
62
63
64
65
66
67

		/**
		 * @brief Returns the average sampling rate in the data.
		 * This computes the average sampling rate of the data that is contained in the buffer. This is a quick
		 * estimate, centered on how often a station measures "something" (ie, how many timestamps do we have
		 * for this station in the buffer). if the station measures TA at h+0 and h+30 and
		 * RH at h+15 and h+45, it would return 4 measurements per hour. If the station measures TA and RH at h+0 and h+30,
		 * it would return 2 measurements per hour.
		 * @return average sampling rate in Hz, nodata if the buffer is empty
		 */
68
		double getAvgSamplingRate() const;
69
70
71
72
73
74
75

		/**
		 * @brief Returns the begining of the buffer.
		 * This is the start date of the <b>request</b> that was given to the IOHandler. If there was no data
		 * at this date, then the date of the first data would be greater.
		 * @return start date of the buffer
		 */
76
		Date getBufferStart() const;
77
78
79
80
81
82
83

		/**
		 * @brief Returns the end of the buffer.
		 * This is the end date of the <b>request</b> that was given to the IOHandler. If there was no data
		 * at this date, then the date of the last data would be less.
		 * @return end date of the buffer
		 */
84
85
		Date getBufferEnd() const;

86
87
88
89
		/**
		* @brief Check if the buffer is empty
		* @return true if the buffer is empty
		*/
90
		bool empty() const;
91
92
93
94

		/**
		* @brief Clear the buffer; the data is deleted and the start and end dates reset to <i>undef</i>
		*/
95
		void clear();
96
97
98
99
100
101
102

		/**
		 * @brief Add data representing the available data between two dates.
		 * @param date_start      A Date object representing the beginning of an interval (inclusive)
		 * @param date_end        A Date object representing the end of an interval (inclusive)
		 * @param vecMeteo        A vector of vector<MeteoData> objects providing the data
		 */
103
104
105
106
107
		void push(const Date& date_start, const Date& date_end, const std::vector< METEO_SET >& vecMeteo);
		//void push(const Date& date, const METEO_SET& vecMeteo);

		const std::string toString() const;

108
		//HACK: these should be removed in order to hide the internals! But this requires a re-write of MeteoProcessor
109
		std::vector< METEO_SET >& getBuffer();
110
111
		void setBufferStart(const Date& date);
		void setBufferEnd(const Date& date);
112
113
114
115
116
	private:
		std::vector< METEO_SET > ts_buffer; ///< stores raw data
		Date ts_start, ts_end; ///< store the beginning and the end date of the ts_buffer
};

117
118
119
120
121
122
123
124
125
126
127
128
/**
 * @class GridBuffer
 * @brief A class to buffer gridded data.
 * This class buffers Grid2D objects. It implements a proper ring buffer, thus removing old bufered grids
 * when necessary.
 *
 * @ingroup data_str
 * @author Mathias Bavay
 * @date   2015-02-06
*/
class GridBuffer {
	public:
129
		GridBuffer(const size_t& in_max_grids);
130
131
		
		bool empty() const {return IndexBufferedGrids.empty();};
132
		void clear() {mapBufferedGrids.clear(); mapBufferedInfos.clear(); IndexBufferedGrids.clear();};
133
134
135
136
		size_t size() const {return IndexBufferedGrids.size();};
		
		void setMaxGrids(const size_t& in_max_grids) {max_grids=in_max_grids;};
		
137
		bool get(DEMObject& grid, const std::string& grid_hash) const;
138
		bool get(Grid2DObject& grid, const std::string& grid_hash) const;
139
		bool get(Grid2DObject& grid, const std::string& grid_hash, std::string& grid_info) const;
140
141
		bool get(Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date) const;
		
142
		void push(const DEMObject& grid, const std::string& grid_hash);
143
144
145
		void push(const Grid2DObject& grid, const std::string& grid_hash);
		void push(const Grid2DObject& grid, const std::string& grid_hash, const std::string& grid_info);
		void push(const Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date);
146
147
		
		const std::string toString() const;
148
149
	private:		
		std::map<std::string, Grid2DObject> mapBufferedGrids;  ///< Buffer interpolated grids
150
		std::map<std::string, DEMObject> mapBufferedDEMs;  ///< Buffer interpolated grids
151
		std::map<std::string, std::string> mapBufferedInfos; ///< Buffer interpolations info messages
152
		std::vector<std::string> IndexBufferedGrids; // this is required in order to know which grid is the oldest one
153
		std::vector<std::string> IndexBufferedDEMs; // this is required in order to know which grid is the oldest one
154
155
156
		size_t max_grids; ///< How many grids to buffer (grids, dem, landuse and assimilation grids together)
};

157
158
}
#endif