libdballe  5.18
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 
135 public:
136  const Exporter::Options& opts;
137  const Msgs& msgs;
138  const Msg* msg; // Msg being read
139  const msg::Context* c_station;
140  const msg::Context* c_gnd_instant;
141  wreport::Subset* subset; // Subset being written
142 
143  Template(const Exporter::Options& opts, const Msgs& msgs)
144  : opts(opts), msgs(msgs), msg(0), subset(0) {}
145  virtual ~Template() {}
146 
147  virtual const char* name() const = 0;
148  virtual const char* description() const = 0;
149  virtual void to_bulletin(wreport::Bulletin& bulletin);
150 };
151 
153 {
154  std::string name;
155  std::string description;
156 
157  virtual ~TemplateFactory() {}
158  virtual std::auto_ptr<Template> make(const Exporter::Options& opts, const Msgs& msgs) const = 0;
159 };
160 
161 struct TemplateRegistry : public std::map<std::string, const TemplateFactory*>
162 {
163  static const TemplateRegistry& get();
164  static const TemplateFactory& get(const std::string& name);
165 
166  void register_factory(const TemplateFactory* fac);
167 };
168 
169 } // namespace wr
170 
171 
172 #if 0
173 
179 #include <dballe/core/rawmsg.h>
180 #include <dballe/msg/msgs.h>
181 #include <dballe/msg/codec.h>
182 #include <dballe/bufrex/msg.h>
183 
198 dba_err bufrex_encode_bufr(dba_msgs msgs, int type, int subtype, int localsubtype, dba_rawmsg* raw);
199 
214 dba_err bufrex_encode_crex(dba_msgs msgs, int type, int subtype, dba_rawmsg* raw);
215 
216 
227 dba_err bufrex_msg_from_dba_msg(bufrex_msg raw, dba_msg msg);
228 
239 dba_err bufrex_msg_from_dba_msgs(bufrex_msg raw, dba_msgs msgs);
240 
253 dba_err bufrex_msg_to_dba_msgs(bufrex_msg raw, dba_msg_codec_options opts, dba_msgs* msgs);
254 
255 
268 dba_err bufrex_infer_type_subtype(dba_msg msg, int* type, int* subtype, int* localsubtype);
269 
270 
271 #endif
272 
273 } // namespace msg
274 } // namespace dballe
275 
276 /* vim:set ts=4 sw=4: */
277 #endif