libdballe  6.8
wr_codec.h
1 /*
2  * dballe/wr_codec - BUFR/CREX import and export
3  *
4  * Copyright (C) 2005--2010 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 DBALLE_MSG_WR_CODEC_H
23 #define DBALLE_MSG_WR_CODEC_H
24 
25 #include <dballe/msg/codec.h>
26 #include <dballe/msg/msg.h>
27 #include <wreport/varinfo.h>
28 #include <stdint.h>
29 #include <map>
30 #include <string>
31 
32 namespace wreport {
33 struct Bulletin;
34 struct Subset;
35 }
36 
37 namespace dballe {
38 struct Msg;
39 
40 namespace msg {
41 
42 class WRImporter : public Importer
43 {
44 public:
45  WRImporter(const Options& opts);
46 
50  virtual void from_bulletin(const wreport::Bulletin& msg, Msgs& msgs) const;
51 };
52 
53 class BufrImporter : public WRImporter
54 {
55 public:
56  BufrImporter(const Options& opts=Options());
57  virtual ~BufrImporter();
58 
59  virtual void from_rawmsg(const Rawmsg& msg, Msgs& msgs) const;
60 };
61 
62 class CrexImporter : public WRImporter
63 {
64 public:
65  CrexImporter(const Options& opts=Options());
66  virtual ~CrexImporter();
67 
68  virtual void from_rawmsg(const Rawmsg& msg, Msgs& msgs) const;
69 };
70 
71 
72 class WRExporter : public Exporter
73 {
74 public:
75  WRExporter(const Options& opts);
76 
80  virtual void to_bulletin(const Msgs& msgs, wreport::Bulletin& msg) const;
81 
85  std::string infer_template(const Msgs& msgs) const;
86 };
87 
88 class BufrExporter : public WRExporter
89 {
90 public:
91  BufrExporter(const Options& opts=Options());
92  virtual ~BufrExporter();
93 
94  virtual void to_rawmsg(const Msgs& msgs, Rawmsg& msg) const;
95  virtual std::auto_ptr<wreport::Bulletin> make_bulletin() const;
96 };
97 
98 class CrexExporter : public WRExporter
99 {
100 public:
101  CrexExporter(const Options& opts=Options());
102  virtual ~CrexExporter();
103 
104  virtual void to_rawmsg(const Msgs& msgs, Rawmsg& msg) const;
105  virtual std::auto_ptr<wreport::Bulletin> make_bulletin() const;
106 };
107 
108 namespace wr {
109 
110 struct TemplateRegistry;
111 
112 class Template
113 {
114 protected:
115  virtual void setupBulletin(wreport::Bulletin& bulletin);
116  virtual void to_subset(const Msg& msg, wreport::Subset& subset);
117 
118  void add(wreport::Varcode code, const msg::Context* ctx, int shortcut) const;
119  void add(wreport::Varcode code, const msg::Context* ctx, wreport::Varcode srccode) const;
120  void add(wreport::Varcode code, const msg::Context* ctx) const;
121  void add(wreport::Varcode code, int shortcut) const;
122  void add(wreport::Varcode code, wreport::Varcode srccode, const Level& level, const Trange& trange) const;
123  void add(wreport::Varcode code, const wreport::Var* var) const;
124  // Set station name, truncating it if it's too long
125  void add_st_name(wreport::Varcode dstcode, const msg::Context* ctx) const;
126 
127  void do_ecmwf_past_wtr() const;
128  void do_D01001() const;
129  void do_D01004() const;
130  void do_D01011() const;
131  int do_D01012() const; // Return the number of hours
132  void do_D01013() const;
133  void do_D01021() const;
134  void do_D01022() const;
135 
136 public:
137  const Exporter::Options& opts;
138  const Msgs& msgs;
139  const Msg* msg; // Msg being read
140  const msg::Context* c_station;
141  const msg::Context* c_gnd_instant;
142  wreport::Subset* subset; // Subset being written
143 
144  Template(const Exporter::Options& opts, const Msgs& msgs)
145  : opts(opts), msgs(msgs), msg(0), subset(0) {}
146  virtual ~Template() {}
147 
148  virtual const char* name() const = 0;
149  virtual const char* description() const = 0;
150  virtual void to_bulletin(wreport::Bulletin& bulletin);
151 };
152 
154 {
155  std::string name;
156  std::string description;
157 
158  virtual ~TemplateFactory() {}
159  virtual std::auto_ptr<Template> make(const Exporter::Options& opts, const Msgs& msgs) const = 0;
160 };
161 
162 struct TemplateRegistry : public std::map<std::string, const TemplateFactory*>
163 {
164  static const TemplateRegistry& get();
165  static const TemplateFactory& get(const std::string& name);
166 
167  void register_factory(const TemplateFactory* fac);
168 };
169 
170 } // namespace wr
171 
172 
173 #if 0
174 
180 #include <dballe/core/rawmsg.h>
181 #include <dballe/msg/msgs.h>
182 #include <dballe/msg/codec.h>
183 #include <dballe/bufrex/msg.h>
184 
199 dba_err bufrex_encode_bufr(dba_msgs msgs, int type, int subtype, int localsubtype, dba_rawmsg* raw);
200 
215 dba_err bufrex_encode_crex(dba_msgs msgs, int type, int subtype, dba_rawmsg* raw);
216 
217 
228 dba_err bufrex_msg_from_dba_msg(bufrex_msg raw, dba_msg msg);
229 
240 dba_err bufrex_msg_from_dba_msgs(bufrex_msg raw, dba_msgs msgs);
241 
254 dba_err bufrex_msg_to_dba_msgs(bufrex_msg raw, dba_msg_codec_options opts, dba_msgs* msgs);
255 
256 
269 dba_err bufrex_infer_type_subtype(dba_msg msg, int* type, int* subtype, int* localsubtype);
270 
271 
272 #endif
273 
274 } // namespace msg
275 } // namespace dballe
276 
277 /* vim:set ts=4 sw=4: */
278 #endif
Store an array of physical data all on the same level.
Definition: msg/context.h:44
Definition: wr_codec.h:153
Definition: codec.h:114
Definition: wr_codec.h:42
Storage for related physical data.
Definition: msg.h:119
virtual void from_rawmsg(const Rawmsg &msg, Msgs &msgs) const
Decode a message from its raw encoded representation.
Definition: codec.h:56
General codec options.
Definition: defs.h:113
Message importer.
Definition: codec.h:53
Definition: cmdline.h:34
virtual std::auto_ptr< wreport::Bulletin > make_bulletin() const
Create a bulletin that works with this exporter.
Definition: wr_codec.h:162
virtual void from_rawmsg(const Rawmsg &msg, Msgs &msgs) const
Decode a message from its raw encoded representation.
Definition: defs.h:54
Definition: wr_codec.h:112
virtual void to_rawmsg(const Msgs &msgs, Rawmsg &msg) const
Encode a message.
Abstraction for a weather report message which is independent from the encoding, used to make sense o...
virtual std::auto_ptr< wreport::Bulletin > make_bulletin() const
Create a bulletin that works with this exporter.
In-memory storage of an encoded weather report.
Definition: wr_codec.h:62
Definition: wr_codec.h:98
Definition: conversion.h:31
Dynamic array of dba_msg.
Definition: msgs.h:38
Definition: wr_codec.h:53
Annotated string buffer for encoded messages.
Definition: rawmsg.h:38
virtual void from_bulletin(const wreport::Bulletin &msg, Msgs &msgs) const
Import a decoded BUFR/CREX message.
Definition: wr_codec.h:88
Message exporter.
Definition: codec.h:111
Definition: wr_codec.h:72
std::string infer_template(const Msgs &msgs) const
Infer a template name from the message contents.
Dynamic array of dba_msg.
virtual void to_bulletin(const Msgs &msgs, wreport::Bulletin &msg) const
Import a decoded BUFR/CREX message.
virtual void to_rawmsg(const Msgs &msgs, Rawmsg &msg) const
Encode a message.