1#ifndef DBALLE_DB_SUMMARY_UTILS_H
2#define DBALLE_DB_SUMMARY_UTILS_H
4#include <dballe/core/smallset.h>
5#include <dballe/core/query.h>
6#include <dballe/core/cursor.h>
7#include <dballe/db/summary.h>
28 : var(var), dtrange(dtrange), count(count)
34 bool operator==(
const VarEntry& o)
const {
return std::tie(var, dtrange, count) == std::tie(o.var, o.dtrange, o.count); }
35 bool operator!=(
const VarEntry& o)
const {
return std::tie(var, dtrange, count) != std::tie(o.var, o.dtrange, o.count); }
39 this->dtrange.
merge(dtrange);
46 DBALLE_TEST_ONLY
void dump(FILE* out)
const;
50inline const VarDesc& station_entry_get_value(
const VarEntry& item) {
return item.var; }
57template<
typename Station>
60 using SmallSet::iterator;
61 using SmallSet::const_iterator;
62 using SmallSet::reverse_iterator;
63 using SmallSet::const_reverse_iterator;
64 using SmallSet::begin;
66 using SmallSet::rbegin;
69 using SmallSet::empty;
71 bool operator==(
const StationEntry& o)
const {
return SmallSet::operator==(o); }
72 bool operator!=(
const StationEntry& o)
const {
return SmallSet::operator!=(o); }
78 template<
typename OStation>
82 for (
const auto& item: entry)
89 add(vd, dtrange, count);
93 : station(entries.station)
95 add_filtered(entries, query);
101 template<
typename OStation>
109 DBALLE_TEST_ONLY
void dump(FILE* out)
const;
113template<
typename Station>
121template<
typename Station>
125 typedef typename Parent::iterator iterator;
126 typedef typename Parent::const_iterator const_iterator;
127 typedef typename Parent::reverse_iterator reverse_iterator;
128 typedef typename Parent::const_reverse_iterator const_reverse_iterator;
131 using Parent::rbegin;
142 template<
typename OStation>
153 bool has(
const Station& station)
const {
return this->find(station) != this->end(); }
155 const StationEntries& sorted()
const {
if (this->dirty) this->rearrange_dirty();
return *
this; }
161extern template class StationEntry<dballe::Station>;
162extern template class StationEntry<dballe::DBStation>;
164extern template class StationEntries<dballe::Station>;
167extern template class StationEntries<dballe::DBStation>;
171template<
typename S1,
typename S2>
172inline S1 convert_station(
const S2& s)
177template<>
inline Station convert_station<Station, Station>(
const Station& s) {
return s; }
178template<>
inline DBStation convert_station<DBStation, DBStation>(
const DBStation& s) {
return s; }
181inline Station convert_station<Station, DBStation>(
const DBStation& station)
183 Station res(station);
188inline DBStation convert_station<DBStation, Station>(
const Station& station)
191 res.report = station.report;
192 res.coords = station.coords;
193 res.ident = station.ident;
201 bool has_flt_rep_memo;
204 bool has_flt_station;
212 has_flt_rep_memo = !q.report.empty();
215 has_flt_station = has_flt_rep_memo || has_flt_area || has_flt_ident;
218 template<
typename Station>
219 bool matches_station(
const Station& station)
223 if (!q.latrange.
contains(station.coords.lat) ||
224 !q.lonrange.
contains(station.coords.lon))
228 if (has_flt_rep_memo && q.report != station.report)
231 if (has_flt_ident && q.ident != station.ident)
238template<
class Station>
244 using StationFilterBase::StationFilterBase;
245 bool matches_station(
const Station& station)
247 return StationFilterBase::matches_station(station);
257 has_flt_station |= (q.ana_id != MISSING_INT);
260 bool matches_station(
const DBStation& station)
262 if (q.ana_id != MISSING_INT and station.id != q.ana_id)
264 return StationFilterBase::matches_station(station);
268template<
typename Station>
272 typename summary::StationEntries<Station>::const_iterator station_entry;
273 typename summary::StationEntry<Station>::const_iterator var_entry;
274 bool at_start =
true;
280 bool has_value()
const {
return !at_start && station_entry != results.end(); }
291 station_entry = results.begin();
292 if (station_entry != results.end())
293 var_entry = station_entry->begin();
295 }
else if (station_entry == results.end())
298 if (var_entry != station_entry->end())
300 if (var_entry == station_entry->end())
303 if (station_entry != results.end())
305 var_entry = station_entry->begin();
310 return station_entry != results.end();
315 station_entry = results.end();
322 res.
report = station.report;
323 res.
coords = station.coords;
324 res.
ident = station.ident;
327 static int _get_station_id(
const DBStation& s) {
return s.
id; }
328 static int _get_station_id(
const dballe::Station& s) {
return MISSING_INT; }
332 return _get_dbstation(station_entry->station);
339 size_t get_count()
const override {
return var_entry->count; }
Base class for cursors that iterate over DB query results.
Definition: cursor.h:16
bool is_missing() const
Check if the Ident is set to the missing value.
Query used to filter DB-All.e data.
Definition: query.h:15
JSON serializer.
Definition: json.h:30
High level objects for working with DB-All.e DB summaries.
Definition: summary.h:45
int id
Database ID of the station.
Definition: types.h:857
Range of datetimes.
Definition: types.h:295
void merge(const DatetimeRange &range)
Merge range into this one, resulting in the smallest range that contains both.
bool contains(int lat) const
Check if a point is inside this range (extremes included)
bool is_missing() const
Return true if the LatRange matches any latitude.
Vertical level or layer.
Definition: types.h:625
bool is_missing() const
Return true if the LonRange matches any longitude.
bool contains(int lon) const
Check if a point is inside this range (extremes included)
Station information.
Definition: types.h:794
Ident ident
Mobile station identifier.
Definition: types.h:802
Coords coords
Station coordinates.
Definition: types.h:799
std::string report
Report name for this station.
Definition: types.h:796
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:687
Standard dballe::Query implementation.
Definition: core/query.h:35
static const Query & downcast(const dballe::Query &query)
Return a reference to query downcasted as core::Query.
Set structure optimized for a small number of items.
Definition: smallset.h:17
Definition: summary_utils.h:270
wreport::Varcode get_varcode() const override
Get the variable code.
Definition: summary_utils.h:337
DBStation get_station() const override
Get the whole station data in a single call.
Definition: summary_utils.h:330
int remaining() const override
Get the number of rows still to be fetched.
Definition: summary_utils.h:282
bool has_value() const
Check if the cursor points to a valid value.
Definition: summary_utils.h:280
Level get_level() const override
Get the level.
Definition: summary_utils.h:335
size_t get_count() const override
Get the count of elements.
Definition: summary_utils.h:339
void discard() override
Discard the results that have not been read yet.
Definition: summary_utils.h:313
DatetimeRange get_datetimerange() const override
Get the datetime range.
Definition: summary_utils.h:338
bool next() override
Get a new item from the results of a query.
Definition: summary_utils.h:287
Trange get_trange() const override
Get the time range.
Definition: summary_utils.h:336
Index of all stations known to a summary.
Definition: summary_utils.h:123
void add(const StationEntries< Station > &entry)
Merge the given entries.
void add(const StationEntry< Station > &entry)
Merge the given entry.
void add(const StationEntries< OStation > &entry)
Merge the given entries.
void add(const Station &station, const VarDesc &vd, const dballe::DatetimeRange &dtrange, size_t count)
Merge the given entry.
Information about a station, and statistics about its variables.
Definition: summary_utils.h:59
Definition: summary_utils.h:199
Definition: summary_utils.h:239
Description of a variable, independent of where and when it was measured.
Definition: summary.h:18
Statistics about a variable.
Definition: summary_utils.h:19
Cursor iterating over summary entries.
Definition: core/cursor.h:67
Class passed to key-value accessors to set values in an invoker-defined way.
Definition: core/enq.h:18
Common base types used by most of DB-All.e code.