1#ifndef DBALLE_DB_SUMMARY_UTILS_H
2#define DBALLE_DB_SUMMARY_UTILS_H
4#include <dballe/core/cursor.h>
5#include <dballe/core/query.h>
6#include <dballe/core/smallset.h>
7#include <dballe/db/summary.h>
29 : var(var), dtrange(dtrange), count(count)
33 VarEntry(
const VarEntry&) =
default;
35 bool operator==(
const VarEntry& o)
const
37 return std::tie(var, dtrange, count) ==
38 std::tie(o.var, o.dtrange, o.count);
40 bool operator!=(
const VarEntry& o)
const
42 return std::tie(var, dtrange, count) !=
43 std::tie(o.var, o.dtrange, o.count);
48 this->dtrange.
merge(dtrange);
55 DBALLE_TEST_ONLY
void dump(FILE* out)
const;
68template <
typename Station>
70 :
protected core::SmallSet<VarEntry, VarDesc, station_entry_get_value>
73 using SmallSet::begin;
74 using SmallSet::const_iterator;
75 using SmallSet::const_reverse_iterator;
76 using SmallSet::empty;
78 using SmallSet::iterator;
79 using SmallSet::rbegin;
81 using SmallSet::reverse_iterator;
83 bool operator==(
const StationEntry& o)
const
85 return SmallSet::operator==(o);
87 bool operator!=(
const StationEntry& o)
const
89 return SmallSet::operator!=(o);
94 StationEntry() =
default;
96 template <
typename OStation>
97 StationEntry(
const Station& station,
const StationEntry<OStation>& entry)
100 for (
const auto& item : entry)
108 add(vd, dtrange, count);
111 StationEntry(
const StationEntry& entries,
const dballe::Query& query)
112 : station(entries.station)
114 add_filtered(entries, query);
117 StationEntry(
const StationEntry&) =
default;
121 template <
typename OStation>
122 void add(
const StationEntry<OStation>& entries);
123 void add_filtered(
const StationEntry& entries,
const dballe::Query& query);
133 DBALLE_TEST_ONLY
void dump(FILE* out)
const;
136template <
typename Station>
148template <
typename Station>
151 station_entries_get_value<Station>>
154 station_entries_get_value<Station>>
156 typedef typename Parent::iterator iterator;
157 typedef typename Parent::const_iterator const_iterator;
158 typedef typename Parent::reverse_iterator reverse_iterator;
159 typedef typename Parent::const_reverse_iterator const_reverse_iterator;
163 using Parent::rbegin;
168 return Parent::operator==(o);
172 return Parent::operator!=(o);
180 template <
typename OStation>
191 bool has(
const Station& station)
const
193 return this->find(station) != this->end();
199 this->rearrange_dirty();
210extern template class StationEntry<dballe::Station>;
211extern template class StationEntry<dballe::DBStation>;
213extern template class StationEntries<dballe::Station>;
217extern template class StationEntries<dballe::DBStation>;
221template <
typename S1,
typename S2>
inline S1 convert_station(
const S2& s)
226template <>
inline Station convert_station<Station, Station>(
const Station& s)
231inline DBStation convert_station<DBStation, DBStation>(
const DBStation& s)
237inline Station convert_station<Station, DBStation>(
const DBStation& station)
239 Station res(station);
244inline DBStation convert_station<DBStation, Station>(
const Station& station)
247 res.report = station.report;
248 res.coords = station.coords;
249 res.ident = station.ident;
253struct StationFilterBase
256 bool has_flt_rep_memo;
259 bool has_flt_station;
268 has_flt_rep_memo = !q.report.empty();
269 has_flt_ident = !q.ident.is_missing();
270 has_flt_area = !q.latrange.is_missing() || !q.lonrange.is_missing();
271 has_flt_station = has_flt_rep_memo || has_flt_area || has_flt_ident;
274 template <
typename Station>
bool matches_station(
const Station& station)
278 if (!q.latrange.contains(station.coords.lat) ||
279 !q.lonrange.contains(station.coords.lon))
283 if (has_flt_rep_memo && q.report != station.report)
286 if (has_flt_ident && q.ident != station.ident)
297 using StationFilterBase::StationFilterBase;
298 bool matches_station(
const Station& station)
300 return StationFilterBase::matches_station(station);
304template <>
struct StationFilter<dballe::DBStation> :
public StationFilterBase
306 StationFilter(
const dballe::Query& query) : StationFilterBase(query)
308 has_flt_station |= (q.ana_id != MISSING_INT);
311 bool matches_station(
const DBStation& station)
313 if (q.ana_id != MISSING_INT and station.id != q.ana_id)
315 return StationFilterBase::matches_station(station);
322 typename summary::StationEntries<Station>::const_iterator station_entry;
323 typename summary::StationEntry<Station>::const_iterator var_entry;
324 bool at_start =
true;
332 return !at_start && station_entry != results.end();
341 station_entry = results.begin();
342 if (station_entry != results.end())
343 var_entry = station_entry->begin();
346 else if (station_entry == results.end())
350 if (var_entry != station_entry->end())
352 if (var_entry == station_entry->end())
355 if (station_entry != results.end())
357 var_entry = station_entry->begin();
362 return station_entry != results.end();
365 void discard()
override { station_entry = results.end(); }
371 res.
report = station.report;
372 res.
coords = station.coords;
373 res.
ident = station.ident;
376 static int _get_station_id(
const DBStation& s) {
return s.
id; }
377 static int _get_station_id(
const dballe::Station& s) {
return MISSING_INT; }
381 return _get_dbstation(station_entry->station);
388 return var_entry->var.varcode;
392 return var_entry->dtrange;
394 size_t get_count()
const override {
return var_entry->count; }
Base class for cursors that iterate over DB query results.
Definition cursor.h:16
Query used to filter DB-All.e data.
Definition query.h:17
JSON serializer.
Definition json.h:30
High level objects for working with DB-All.e DB summaries.
Definition summary.h:71
int id
Database ID of the station.
Definition types.h:946
Range of datetimes.
Definition types.h:297
void merge(const DatetimeRange &range)
Merge range into this one, resulting in the smallest range that contains both.
Vertical level or layer.
Definition types.h:625
Station information.
Definition types.h:879
Ident ident
Mobile station identifier.
Definition types.h:887
Coords coords
Station coordinates.
Definition types.h:884
Report report
Report name for this station.
Definition types.h:881
Information on how a value has been sampled or computed with regards to time.
Definition types.h:689
Standard dballe::Query implementation.
Definition core/query.h:36
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:21
wreport::Varcode get_varcode() const override
Get the variable code.
Definition summary_utils.h:386
DBStation get_station() const override
Get the whole station data in a single call.
Definition summary_utils.h:379
int remaining() const override
Get the number of rows still to be fetched.
Definition summary_utils.h:335
bool has_value() const override
Check if the cursor points to a valid value.
Definition summary_utils.h:330
Level get_level() const override
Get the level.
Definition summary_utils.h:384
size_t get_count() const override
Get the count of elements.
Definition summary_utils.h:394
void discard() override
Discard the results that have not been read yet.
Definition summary_utils.h:365
DatetimeRange get_datetimerange() const override
Get the datetime range.
Definition summary_utils.h:390
bool next() override
Get a new item from the results of a query.
Definition summary_utils.h:337
Trange get_trange() const override
Get the time range.
Definition summary_utils.h:385
Index of all stations known to a summary.
Definition summary_utils.h:152
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:71
Definition summary_utils.h:293
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:73
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.