libdballe  6.8
msg.h
Go to the documentation of this file.
1 /*
2  * dballe/msg - Hold an interpreted weather bulletin
3  *
4  * Copyright (C) 2005--2011 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_MSG_H
23 #define DBA_MSG_H
24 
67 #include <dballe/core/var.h>
68 #include <dballe/core/defs.h>
69 #include <dballe/core/matcher.h>
70 #include <dballe/msg/vars.h>
71 #include <stdio.h>
72 #include <vector>
73 #include <memory>
74 #include <iosfwd>
75 
76 struct lua_State;
77 
78 namespace dballe {
79 
80 struct Record;
81 struct CSVReader;
82 
83 namespace msg {
84 struct Context;
85 }
86 
90 enum MsgType {
91  MSG_GENERIC,
92  MSG_SYNOP,
93  MSG_PILOT,
94  MSG_TEMP,
95  MSG_TEMP_SHIP,
96  MSG_AIREP,
97  MSG_AMDAR,
98  MSG_ACARS,
99  MSG_SHIP,
100  MSG_BUOY,
101  MSG_METAR,
102  MSG_SAT,
103  MSG_POLLUTION
104 };
105 
114 const char* msg_type_name(MsgType type);
115 
119 class Msg
120 {
121 protected:
125  int find_index(const Level& lev, const Trange& tr) const;
126 
127 public:
129  MsgType type;
130 
132  std::vector<msg::Context*> data;
133 
139  Msg();
140  ~Msg();
141 
142  Msg(const Msg& m);
143  Msg& operator=(const Msg& m);
144 
146  void clear();
147 
153  void add_context(std::auto_ptr<msg::Context> ctx);
154 
160  bool remove_context(const Level& lev, const Trange& tr);
161 
172  const msg::Context* find_context(const Level& lev, const Trange& tr) const;
173 
180  const msg::Context* find_station_context() const;
181 
192  msg::Context* edit_context(const Level& lev, const Trange& tr);
193 
205  msg::Context& obtain_context(const Level& lev, const Trange& tr);
206 
209 
222  const wreport::Var* find(wreport::Varcode code, const Level& lev, const Trange& tr) const;
223 
236  wreport::Var* edit(wreport::Varcode code, const Level& lev, const Trange& tr);
237 
248  const wreport::Var* find_by_id(int id) const;
249 
260  const msg::Context* find_context_by_id(int id) const;
261 
272  wreport::Var* edit_by_id(int id);
273 
287  void set(const wreport::Var& var, wreport::Varcode code, const Level& lev, const Trange& tr);
288 
297  void set_by_id(const wreport::Var& var, int shortcut);
298 
311  void set(std::auto_ptr<wreport::Var> var, const Level& lev, const Trange& tr);
312 
329  void seti(wreport::Varcode code, int val, int conf, const Level& lev, const Trange& tr);
330 
347  void setd(wreport::Varcode code, double val, int conf, const Level& lev, const Trange& tr);
348 
365  void setc(wreport::Varcode code, const char* val, int conf, const Level& lev, const Trange& tr);
366 
368  void set_date(const char* date);
369 
374  //void filter(const Record& filter, Msg& dest) const;
375 
384  void sounding_pack_levels(Msg& dst) const;
385 
386 #if 0
387 
396  void sounding_unpack_levels(Msg& dst) const;
397 #endif
398 
409  void parse_date(int* values) const;
410 
418  bool from_csv(CSVReader& in);
419 
423  void to_csv(std::ostream& out) const;
424 
426  static void csv_header(std::ostream& out);
427 
434  void print(FILE* out) const;
435 
447  unsigned diff(const Msg& msg) const;
448 
452  static MsgType type_from_repmemo(const char* repmemo);
453 
457  static const char* repmemo_from_type(MsgType type);
458 
459 #include <dballe/msg/msg-extravars.h>
460 
461 
465  void lua_push(struct lua_State* L);
466 
472  static Msg* lua_check(struct lua_State* L, int idx);
473 };
474 
479 {
480  virtual ~MsgConsumer() {}
481  virtual void operator()(std::auto_ptr<Msg>) = 0;
482 };
483 
487 struct MatchedMsg : public Matched
488 {
489  const Msg& m;
490 
491  MatchedMsg(const Msg& r);
492  ~MatchedMsg();
493 
494  virtual matcher::Result match_var_id(int val) const;
495  virtual matcher::Result match_station_id(int val) const;
496  virtual matcher::Result match_station_wmo(int block, int station=-1) const;
497  virtual matcher::Result match_date(const int* min, const int* max) const;
498  virtual matcher::Result match_coords(int latmin, int latmax, int lonmin, int lonmax) const;
499  virtual matcher::Result match_rep_memo(const char* memo) const;
500 };
501 
502 #if 0
503 
515 dba_err dba_msg_set_by_id(dba_msg msg, dba_var var, int id);
516 
531 dba_err dba_msg_set_nocopy_by_id(dba_msg msg, dba_var var, int id);
532 
541 dba_msg_type dba_msg_get_type(dba_msg msg);
542 #endif
543 
544 }
545 
546 // vim:set ts=4 sw=4:
547 #endif
Store an array of physical data all on the same level.
Definition: msg/context.h:44
void sounding_pack_levels(Msg &dst) const
Copy to dest all the variable in this message that match filter TODO: to be implemented.
wreport::Var * edit(wreport::Varcode code, const Level &lev, const Trange &tr)
Find a variable given its description.
void seti(wreport::Varcode code, int val, int conf, const Level &lev, const Trange &tr)
Add or replace an integer value in the dba_msg.
void to_csv(std::ostream &out) const
Output in CSV format.
void setc(wreport::Varcode code, const char *val, int conf, const Level &lev, const Trange &tr)
Add or replace a string value in the dba_msg.
Common interface for things that are matched.
Definition: matcher.h:48
Storage for related physical data.
Definition: msg.h:119
msg::Context * edit_context(const Level &lev, const Trange &tr)
Find a msg::Context given its description.
Implement ::dba_var, an encapsulation of a measured variable.
Shortcut IDs and functions to quickly refer to commonly used values inside a ::dba_msg.
virtual matcher::Result match_coords(int latmin, int latmax, int lonmin, int lonmax) const
Match coordinates, with bounds in 1/100000 of degree.
wreport::Var * edit_by_id(int id)
Find a datum given its shortcut ID.
Definition: defs.h:113
Common definitions.
MsgType type
Source of the data.
Definition: msg.h:129
std::vector< msg::Context * > data
Context in the message.
Definition: msg.h:132
static Msg * lua_check(struct lua_State *L, int idx)
Check that the element at idx is a dba_msg.
static const char * repmemo_from_type(MsgType type)
Get the report code corresponding to the given message source type.
const msg::Context * find_context(const Level &lev, const Trange &tr) const
Find a msg::Context given its description.
void parse_date(int *values) const
Parse the date set in the Msg.
msg::Context & obtain_station_context()
Shortcut to obtain_context(Level::ana(), Trange::ana());.
void set_by_id(const wreport::Var &var, int shortcut)
Add or replace a value.
Definition: cmdline.h:34
static void csv_header(std::ostream &out)
Output the CSV header.
const wreport::Var * find_by_id(int id) const
Find a datum given its shortcut ID.
Definition: defs.h:54
Msg()
Create a new dba_msg.
Match adapter for Msg.
Definition: msg.h:487
void set_date(const char *date)
Set the date from a string in the format "YYYY-MM-DD HH:MM:SS".
virtual matcher::Result match_station_id(int val) const
Match station ID.
void add_context(std::auto_ptr< msg::Context > ctx)
Add a missing context, taking care of its memory management.
int find_index(const Level &lev, const Trange &tr) const
Return the index of the given context, or -1 if it was not found.
virtual matcher::Result match_station_wmo(int block, int station=-1) const
Match station WMO code.
virtual matcher::Result match_rep_memo(const char *memo) const
Match rep_memo.
const msg::Context * find_context_by_id(int id) const
Find a contexts given level and timerange found in a shortcut ID.
void lua_push(struct lua_State *L)
Push the variable as an object in the lua stack.
bool remove_context(const Level &lev, const Trange &tr)
Remove a context from the message.
void clear()
Remove all information from Msg.
const wreport::Var * find(wreport::Varcode code, const Level &lev, const Trange &tr) const
Find a variable given its description.
virtual matcher::Result match_var_id(int val) const
Match variable ID.
void set(const wreport::Var &var, wreport::Varcode code, const Level &lev, const Trange &tr)
Add or replace a value.
bool from_csv(CSVReader &in)
Read data from a CSV input.
const msg::Context * find_station_context() const
Find the station info context.
Consumer interface used to stream messages as they are produced.
Definition: msg.h:478
msg::Context & obtain_context(const Level &lev, const Trange &tr)
Find a msg::Context given its description, creating it if it does not exist.
virtual matcher::Result match_date(const int *min, const int *max) const
Match date.
Definition: csv.h:51
static MsgType type_from_repmemo(const char *repmemo)
Get the message source type corresponding to the given report code.
unsigned diff(const Msg &msg) const
Compute the differences between two Msg.
void setd(wreport::Varcode code, double val, int conf, const Level &lev, const Trange &tr)
Add or replace a double value in the dba_msg.
void print(FILE *out) const
Dump all the contents of the message to the given stream.