libdballe  8.3
msg.h
1 #ifndef DBALLE_MSG_H
2 #define DBALLE_MSG_H
3 
4 #include <dballe/message.h>
5 #include <dballe/var.h>
6 #include <dballe/core/fwd.h>
7 #include <dballe/msg/fwd.h>
8 #include <dballe/core/defs.h>
9 #include <dballe/core/matcher.h>
10 #include <dballe/msg/context.h>
11 #include <dballe/importer.h>
12 #include <dballe/exporter.h>
13 #include <stdio.h>
14 #include <vector>
15 #include <memory>
16 #include <iosfwd>
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;
30  ImporterOptions(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 shared_ptr
50 typedef std::vector<std::shared_ptr<dballe::Message>> Messages;
51 
52 namespace msg {
53 
59 Messages messages_from_csv(CSVReader& in);
60 
64 void messages_to_csv(const Messages& msgs, CSVWriter& out);
65 
75 unsigned messages_diff(const Messages& msgs1, const Messages& msgs2);
76 
78 void messages_print(const Messages& msgs, FILE* out);
79 
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 const_reverse_iterator;
87  typedef std::vector<msg::Context>::reverse_iterator reverse_iterator;
88 
89 protected:
90  std::vector<msg::Context> m_contexts;
91 
92  iterator insert_new(const Level& level, const Trange& trange);
93 
94 public:
95  Contexts() = default;
96  Contexts(const Contexts&) = default;
97  Contexts(Contexts&&) = default;
98  Contexts& operator=(const Contexts&) = default;
99  Contexts& operator=(Contexts&&) = default;
100 
101  const_iterator begin() const { return m_contexts.begin(); }
102  const_iterator end() const { return m_contexts.end(); }
103  iterator begin() { return m_contexts.begin(); }
104  iterator end() { return m_contexts.end(); }
105  const_reverse_iterator rbegin() const { return m_contexts.rbegin(); }
106  const_reverse_iterator rend() const { return m_contexts.rend(); }
107  const_iterator cbegin() const { return m_contexts.cbegin(); }
108  const_iterator cend() const { return m_contexts.cend(); }
109 
110  const_iterator find(const Level& level, const Trange& trange) const;
111  iterator find(const Level& level, const Trange& trange);
112 
113  iterator obtain(const Level& level, const Trange& trange);
114  bool drop(const Level& level, const Trange& trange);
115 
116  size_t size() const { return m_contexts.size(); }
117  bool empty() const { return m_contexts.empty(); }
118  void clear() { return m_contexts.clear(); }
119  void reserve(typename std::vector<Value>::size_type size) { m_contexts.reserve(size); }
120  iterator erase(iterator pos) { return m_contexts.erase(pos); }
121  // iterator erase(const_iterator pos) { return m_contexts.erase(pos); }
122 };
123 
124 }
125 
126 
130 class Message : public dballe::Message
131 {
132 protected:
136  int find_index(const Level& lev, const Trange& tr) const;
137 
138  const wreport::Var* get_impl(const Level& lev, const Trange& tr, wreport::Varcode code) const override;
139  void set_impl(const Level& lev, const Trange& tr, std::unique_ptr<wreport::Var> var) override;
140 
141  void seti(const Level& lev, const Trange& tr, wreport::Varcode code, int val, int conf);
142  void setd(const Level& lev, const Trange& tr, wreport::Varcode code, double val, int conf);
143  void setc(const Level& lev, const Trange& tr, wreport::Varcode code, const char* val, int conf);
144 
145 public:
147  MessageType type = MessageType::GENERIC;
148  Values station_data;
149  msg::Contexts data;
150 
151  Message() = default;
152  Message(const Message&) = default;
153  Message(Message&&) = default;
154  Message& operator=(const Message& m) = default;
155  Message& operator=(Message&& m) = default;
156 
162  static const Message& downcast(const dballe::Message& o);
163 
169  static Message& downcast(dballe::Message& o);
170 
176  static std::shared_ptr<Message> downcast(std::shared_ptr<dballe::Message> o);
177 
178  std::unique_ptr<dballe::Message> clone() const override;
179  Datetime get_datetime() const override;
180  Coords get_coords() const override;
181  Ident get_ident() const override;
182  std::string get_report() const override;
183  MessageType get_type() const override { return type; }
184  bool foreach_var(std::function<bool(const Level&, const Trange&, const wreport::Var&)>) const override;
185  void print(FILE* out) const override;
186  unsigned diff(const dballe::Message& msg) const override;
187 
189  void clear();
190 
191  using dballe::Message::get;
192  using dballe::Message::set;
193 
202  const wreport::Var* get(const Shortcut& shortcut) const;
203 
212  void set(const Shortcut& shortcut, const wreport::Var& var);
213 
217  void set_datetime(const Datetime& dt);
218 
224  bool remove_context(const Level& lev, const Trange& tr);
225 
236  const msg::Context* find_context(const Level& lev, const Trange& tr) const;
237 
244  const Values& find_station_context() const;
245 
256  msg::Context* edit_context(const Level& lev, const Trange& tr);
257 
269  msg::Context& obtain_context(const Level& lev, const Trange& tr);
270 
283  wreport::Var* edit(wreport::Varcode code, const Level& lev, const Trange& tr);
284 
285 #if 0
286 
298  bool remove(wreport::Varcode code, const Level& lev, const Trange& tr);
299 #endif
300 
309  void sounding_pack_levels();
310 
318  bool from_csv(CSVReader& in);
319 
321  void to_csv(CSVWriter& out) const;
322 
323  std::unique_ptr<dballe::CursorStation> query_stations(const Query& query) const override;
324  std::unique_ptr<dballe::CursorStationData> query_station_data(const Query& query) const override;
325  std::unique_ptr<dballe::CursorData> query_data(const Query& query) const override;
326  std::unique_ptr<dballe::CursorData> query_station_and_data(const Query& query) const;
327 
329  static void csv_header(CSVWriter& out);
330 
334  static MessageType type_from_repmemo(const char* repmemo);
335 
339  static const char* repmemo_from_type(MessageType type);
340 
341 #include <dballe/msg/msg-extravars.h>
342 };
343 
344 
348 struct MatchedMsg : public Matched
349 {
350  const impl::Message& m;
351 
352  MatchedMsg(const impl::Message& r);
353  ~MatchedMsg();
354 
355  matcher::Result match_var_id(int val) const override;
356  matcher::Result match_station_id(int val) const override;
357  matcher::Result match_station_wmo(int block, int station=-1) const override;
358  matcher::Result match_datetime(const DatetimeRange& range) const override;
359  matcher::Result match_coords(const LatRange& latrange, const LonRange& lonrange) const override;
360  matcher::Result match_rep_memo(const char* memo) const override;
361 };
362 
363 
367 struct MatchedMessages : public Matched
368 {
369  const std::vector<std::shared_ptr<dballe::Message>>& m;
370 
371  MatchedMessages(const std::vector<std::shared_ptr<dballe::Message>>& m);
372  ~MatchedMessages();
373 
374  matcher::Result match_var_id(int val) const override;
375  matcher::Result match_station_id(int val) const override;
376  matcher::Result match_station_wmo(int block, int station=-1) const override;
377  matcher::Result match_datetime(const DatetimeRange& range) const override;
378  matcher::Result match_coords(const LatRange& latrange, const LonRange& lonrange) const override;
379  matcher::Result match_rep_memo(const char* memo) const override;
380 };
381 
382 }
383 }
384 #endif
Definition: csv.h:140
Sorted storage for all the dba_msg_datum present on one level.
Common interface for things that are matched.
Definition: matcher.h:29
Create wreport variables from the DB-All.e B table.
void print(FILE *out)
Print a summary of the options to out.
Coordinates.
Definition: types.h:365
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:683
A bulletin that has been decoded and physically interpreted.
Definition: message.h:28
Definition: cmdline.h:18
Vertical level or layer.
Definition: types.h:621
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: types.h:744
Match adapter for Messages.
Definition: msg.h:367
Store an array of physical data all on the same level.
Definition: context.h:23
uint16_t Varcode
Range of datetimes.
Definition: types.h:291
MessageType get_type() const override
Return the type of the data in the message.
Definition: msg.h:183
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.
Range of latitudes.
Definition: types.h:464
Options to control message import.
Definition: importer.h:24
ImporterOptions with default constructor usable.
Definition: msg.h:25
void set(const Level &lev, const Trange &tr, wreport::Varcode code, const wreport::Var &var)
Add or replace a value.
Query used to filter DB-All.e data.
Definition: query.h:14
Date and time.
Definition: types.h:164
Definition: shortcuts.h:11
ExporterOptions with default constructor usable.
Definition: msg.h:38
Options to control message export.
Definition: exporter.h:24
Range of longitudes.
Definition: types.h:548
Common definitions.
Definition: csv.h:51
Match adapter for impl::Message.
Definition: msg.h:348
Storage for related physical data.
Definition: msg.h:130
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:176
Definition: msg.h:81