libdballe  9.14
msg.h
1 #ifndef DBALLE_MSG_H
2 #define DBALLE_MSG_H
3 
5 #include <dballe/core/fwd.h>
6 #include <dballe/core/matcher.h>
7 #include <dballe/exporter.h>
8 #include <dballe/importer.h>
9 #include <dballe/message.h>
10 #include <dballe/msg/context.h>
11 #include <dballe/msg/fwd.h>
12 #include <dballe/var.h>
13 #include <iosfwd>
14 #include <memory>
15 #include <stdio.h>
16 #include <vector>
17 
18 namespace dballe {
19 struct CSVReader;
20 struct CSVWriter;
21 
22 namespace impl {
23 
26 {
27  ImporterOptions() = default;
28  ImporterOptions(const std::string& s) : dballe::ImporterOptions(s) {}
29  ImporterOptions(const ImporterOptions&) = default;
31  ImporterOptions& operator=(const ImporterOptions&) = default;
32  ImporterOptions& operator=(ImporterOptions&&) = default;
33  using dballe::ImporterOptions::operator==;
34  using dballe::ImporterOptions::operator!=;
35 };
36 
39 {
40  ExporterOptions() = default;
41  ExporterOptions(const ExporterOptions&) = default;
42  ExporterOptions(ExporterOptions&&) = default;
43  ExporterOptions& operator=(const ExporterOptions&) = default;
44  ExporterOptions& operator=(ExporterOptions&&) = default;
45  using dballe::ExporterOptions::operator==;
46  using dballe::ExporterOptions::operator!=;
47 };
48 
49 // Compatibility/shortcut from old Messages implementation to new vector of
50 // shared_ptr
51 typedef std::vector<std::shared_ptr<dballe::Message>> Messages;
52 
53 namespace msg {
54 
60 Messages messages_from_csv(CSVReader& in);
61 
65 void messages_to_csv(const Messages& msgs, CSVWriter& out);
66 
76 unsigned messages_diff(const Messages& msgs1, const Messages& msgs2);
77 
79 void messages_print(const Messages& msgs, FILE* out);
80 
81 class Contexts
82 {
83 public:
84  typedef std::vector<msg::Context>::const_iterator const_iterator;
85  typedef std::vector<msg::Context>::iterator iterator;
86  typedef std::vector<msg::Context>::const_reverse_iterator
87  const_reverse_iterator;
88  typedef std::vector<msg::Context>::reverse_iterator reverse_iterator;
89 
90 protected:
91  std::vector<msg::Context> m_contexts;
92 
93  iterator insert_new(const Level& level, const Trange& trange);
94 
95 public:
96  Contexts() = default;
97  Contexts(const Contexts&) = default;
98  Contexts(Contexts&&) = default;
99  Contexts& operator=(const Contexts&) = default;
100  Contexts& operator=(Contexts&&) = default;
101 
102  const_iterator begin() const { return m_contexts.begin(); }
103  const_iterator end() const { return m_contexts.end(); }
104  iterator begin() { return m_contexts.begin(); }
105  iterator end() { return m_contexts.end(); }
106  const_reverse_iterator rbegin() const { return m_contexts.rbegin(); }
107  const_reverse_iterator rend() const { return m_contexts.rend(); }
108  const_iterator cbegin() const { return m_contexts.cbegin(); }
109  const_iterator cend() const { return m_contexts.cend(); }
111  const_iterator find(const Level& level, const Trange& trange) const;
112  iterator find(const Level& level, const Trange& trange);
113 
114  iterator obtain(const Level& level, const Trange& trange);
115  bool drop(const Level& level, const Trange& trange);
117  size_t size() const { return m_contexts.size(); }
118  bool empty() const { return m_contexts.empty(); }
119  void clear() { return m_contexts.clear(); }
120  void reserve(typename std::vector<Value>::size_type size)
121  {
122  m_contexts.reserve(size);
123  }
124  iterator erase(iterator pos) { return m_contexts.erase(pos); }
125  // iterator erase(const_iterator pos) { return m_contexts.erase(pos); }
126 };
127 
128 } // namespace msg
129 
133 class Message : public dballe::Message
134 {
135 protected:
139  int find_index(const Level& lev, const Trange& tr) const;
141  const wreport::Var* get_impl(const Level& lev, const Trange& tr,
142  wreport::Varcode code) const override;
143  void set_impl(const Level& lev, const Trange& tr,
144  std::unique_ptr<wreport::Var> var) override;
145 
146  void seti(const Level& lev, const Trange& tr, wreport::Varcode code,
147  int val, int conf);
148  void setd(const Level& lev, const Trange& tr, wreport::Varcode code,
149  double val, int conf);
150  void setc(const Level& lev, const Trange& tr, wreport::Varcode code,
151  const char* val, int conf);
153 public:
154  Message() = default;
155  Message(const Message&) = default;
156  Message(Message&&) = default;
157  Message& operator=(const Message& m) = default;
158  Message& operator=(Message&& m) = default;
159 
161  MessageType type = MessageType::GENERIC;
162  Values station_data;
163  msg::Contexts data;
165  static std::shared_ptr<Message> create();
172  static const Message& downcast(const dballe::Message& o);
173 
179  static Message& downcast(dballe::Message& o);
186  static std::shared_ptr<Message>
187  downcast(std::shared_ptr<dballe::Message> o);
189  std::shared_ptr<dballe::Message> clone() const override;
190  Datetime get_datetime() const override;
191  Coords get_coords() const override;
192  Ident get_ident() const override;
193  std::string get_report() const override;
194  MessageType get_type() const override { return type; }
195  bool foreach_var(std::function<bool(const Level&, const Trange&,
196  const wreport::Var&)>) const override;
197  void print(FILE* out) const override;
198  unsigned diff(const dballe::Message& msg) const override;
199 
201  void clear();
202 
203  using dballe::Message::get;
205 
214  const wreport::Var* get(const Shortcut& shortcut) const;
215 
224  void set(const Shortcut& shortcut, const wreport::Var& var);
225 
229  void set_datetime(const Datetime& dt);
236  bool remove_context(const Level& lev, const Trange& tr);
237 
248  const msg::Context* find_context(const Level& lev, const Trange& tr) const;
249 
256  const Values& find_station_context() const;
257 
268  msg::Context* edit_context(const Level& lev, const Trange& tr);
269 
281  msg::Context& obtain_context(const Level& lev, const Trange& tr);
282 
295  wreport::Var* edit(wreport::Varcode code, const Level& lev,
296  const Trange& tr);
297 
298 #if 0
299 
311  bool remove(wreport::Varcode code, const Level& lev, const Trange& tr);
312 #endif
313 
322  void sounding_pack_levels();
323 
331  bool from_csv(CSVReader& in);
334  void to_csv(CSVWriter& out) const;
335 
336  std::shared_ptr<dballe::CursorStation>
337  query_stations(const Query& query) const override;
338  std::shared_ptr<dballe::CursorStationData>
339  query_station_data(const Query& query) const override;
340  std::shared_ptr<dballe::CursorData>
341  query_data(const Query& query) const override;
342  std::shared_ptr<dballe::CursorData>
343  query_station_and_data(const Query& query) const;
346  static void csv_header(CSVWriter& out);
347 
351  static MessageType type_from_repmemo(const char* repmemo);
356  static const char* repmemo_from_type(MessageType type);
357 
358 #include <dballe/msg/msg-extravars.h>
359 };
364 struct MatchedMsg : public Matched
365 {
366  const impl::Message& m;
367 
369  ~MatchedMsg();
370 
371  matcher::Result match_var_id(int val) const override;
372  matcher::Result match_station_id(int val) const override;
373  matcher::Result match_station_wmo(int block,
374  int station = -1) const override;
375  matcher::Result match_datetime(const DatetimeRange& range) const override;
376  matcher::Result match_coords(const LatRange& latrange,
377  const LonRange& lonrange) const override;
378  matcher::Result match_rep_memo(const char* memo) const override;
379 };
384 struct MatchedMessages : public Matched
385 {
386  const std::vector<std::shared_ptr<dballe::Message>>& m;
387 
388  MatchedMessages(const std::vector<std::shared_ptr<dballe::Message>>& m);
389  ~MatchedMessages();
391  matcher::Result match_var_id(int val) const override;
392  matcher::Result match_station_id(int val) const override;
393  matcher::Result match_station_wmo(int block,
394  int station = -1) const override;
395  matcher::Result match_datetime(const DatetimeRange& range) const override;
396  matcher::Result match_coords(const LatRange& latrange,
397  const LonRange& lonrange) const override;
398  matcher::Result match_rep_memo(const char* memo) const override;
399 };
401 } // namespace impl
402 } // namespace dballe
403 #endif
Definition: csv.h:139
msg::Context & obtain_context(const Level &lev, const Trange &tr)
Find a msg::Context given its description, creating it if it does not exist.
const msg::Context * find_context(const Level &lev, const Trange &tr) const
Find a msg::Context given its description.
void print(FILE *out) const override
Print all the contents of this message to an output stream.
matcher::Result match_datetime(const DatetimeRange &range) const override
Match datetime.
matcher::Result match_coords(const LatRange &latrange, const LonRange &lonrange) const override
Match coordinates, with bounds in 1/100000 of degree.
void set_datetime(const Datetime &dt)
Shortcut to set year...second variables in a single call.
Sorted storage for all the dba_msg_datum present on one level.
static void csv_header(CSVWriter &out)
Output the CSV header.
Common interface for things that are matched.
Definition: matcher.h:29
msg::Context * edit_context(const Level &lev, const Trange &tr)
Find a msg::Context given its description.
bool foreach_var(std::function< bool(const Level &, const Trange &, const wreport::Var &)>) const override
Iterate the contents of the message.
static const Message & downcast(const dballe::Message &o)
Return a reference to o downcasted as an impl::Message.
std::shared_ptr< dballe::CursorData > query_data(const Query &query) const override
Query the variables in the message.
void clear()
Reset the messages as if it was just created.
Create wreport variables from the DB-All.e B table.
Coordinates.
Definition: types.h:371
const wreport::Var * get_impl(const Level &lev, const Trange &tr, wreport::Varcode code) const override
Implementation of get(const Level&, const Trange&, wreport::Varcode)
void to_csv(CSVWriter &out) const
Output in CSV format.
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:688
std::string get_report() const override
Get the report for this message.
int find_index(const Level &lev, const Trange &tr) const
Return the index of the given context, or -1 if it was not found.
void sounding_pack_levels()
Remove the sounding significance from the level descriptions and pack together the data at the same p...
Definition: cmdline.h:18
Options to control message export.
Definition: exporter.h:24
matcher::Result match_station_id(int val) const override
Match station ID.
const Values & find_station_context() const
Find the station info context.
matcher::Result match_datetime(const DatetimeRange &range) const override
Match datetime.
Options to control message import.
Definition: importer.h:24
Vertical level or layer.
Definition: types.h:624
matcher::Result match_coords(const LatRange &latrange, const LonRange &lonrange) const override
Match coordinates, with bounds in 1/100000 of degree.
void set(const Level &lev, const Trange &tr, wreport::Varcode code, const wreport::Var &var)
Add or replace a value.
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: types.h:750
Match adapter for Messages.
Definition: msg.h:384
std::shared_ptr< dballe::CursorStationData > query_station_data(const Query &query) const override
Query the station variables in the message.
std::shared_ptr< dballe::CursorStation > query_stations(const Query &query) const override
Return a Cursor to access the station information in the message.
matcher::Result match_station_wmo(int block, int station=-1) const override
Match station WMO code.
Store an array of physical data all on the same level.
Definition: context.h:23
bool from_csv(CSVReader &in)
Read data from a CSV input.
A bulletin that has been decoded and physically interpreted.
Definition: message.h:28
Range of datetimes.
Definition: types.h:296
MessageType get_type() const override
Return the type of the data in the message.
Definition: msg.h:194
bool remove_context(const Level &lev, const Trange &tr)
Remove a context from the message.
Coords get_coords() const override
Get the reference coordinates for this message.
Range of latitudes.
Definition: types.h:469
ImporterOptions with default constructor usable.
Definition: msg.h:25
MessageType type
Source of the data.
Definition: msg.h:161
matcher::Result match_var_id(int val) const override
Match variable ID.
matcher::Result match_rep_memo(const char *memo) const override
Match rep_memo.
unsigned diff(const dballe::Message &msg) const override
Compute the differences between two Messages.
Date and time.
Definition: types.h:163
Definition: shortcuts.h:11
static MessageType type_from_repmemo(const char *repmemo)
Get the message source type corresponding to the given report code.
wreport::Var * edit(wreport::Varcode code, const Level &lev, const Trange &tr)
Find a variable given its description.
const wreport::Var * get(const Level &lev, const Trange &tr, wreport::Varcode code) const
Get a variable given its code, level and time range information.
void set_impl(const Level &lev, const Trange &tr, std::unique_ptr< wreport::Var > var) override
Implementation of set(const Level& const Trange&, std::unique_ptr<wreport::Var>)
ExporterOptions with default constructor usable.
Definition: msg.h:38
static const char * repmemo_from_type(MessageType type)
Get the report code corresponding to the given message source type.
Datetime get_datetime() const override
Get the reference Datetime for this message.
matcher::Result match_station_id(int val) const override
Match station ID.
Range of longitudes.
Definition: types.h:552
Common definitions.
Definition: csv.h:50
Match adapter for impl::Message.
Definition: msg.h:364
matcher::Result match_station_wmo(int block, int station=-1) const override
Match station WMO code.
matcher::Result match_rep_memo(const char *memo) const override
Match rep_memo.
Storage for related physical data.
Definition: msg.h:133
matcher::Result match_var_id(int val) const override
Match variable ID.
Query used to filter DB-All.e data.
Definition: query.h:16
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:215
Definition: msg.h:81
std::shared_ptr< dballe::Message > clone() const override
Return a copy of this message.
Ident get_ident() const override
Get the station identifier for this message.