libdballe  6.8
defs.h
Go to the documentation of this file.
1 /*
2  * msg/defs - Common definitions
3  *
4  * Copyright (C) 2010--2014 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_DEFS_H
23 #define DBA_MSG_DEFS_H
24 
31 #include <limits.h>
32 #include <string>
33 #include <iosfwd>
34 
35 namespace dballe {
36 
40 typedef enum {
41  BUFR = 0,
42  CREX = 1,
43  AOF = 2,
44 } Encoding;
45 
46 const char* encoding_name(Encoding enc);
47 Encoding parse_encoding(const std::string& s);
48 
52 static const int MISSING_INT = INT_MAX;
53 
54 struct Level
55 {
57  int ltype1;
59  int l1;
61  int ltype2;
63  int l2;
64 
65  Level(int ltype1=MISSING_INT, int l1=MISSING_INT, int ltype2=MISSING_INT, int l2=MISSING_INT)
66  : ltype1(ltype1), l1(l1), ltype2(ltype2), l2(l2) {}
67  Level(const char* ltype1, const char* l1=NULL, const char* ltype2=NULL, const char* l2=NULL);
68 
69  bool operator==(const Level& l) const
70  {
71  return ltype1 == l.ltype1 && l1 == l.l1
72  && ltype2 == l.ltype2 && l2 == l.l2;
73  }
74 
75  bool operator!=(const Level& l) const
76  {
77  return ltype1 != l.ltype1 || l1 != l.l1
78  || ltype2 != l.ltype2 || l2 != l.l2;
79  }
80 
81  bool operator<(const Level& l) const { return compare(l) < 0; }
82  bool operator>(const Level& l) const { return compare(l) > 0; }
83 
90  int compare(const Level& l) const
91  {
92  int res;
93  if ((res = ltype1 - l.ltype1)) return res;
94  if ((res = l1 - l.l1)) return res;
95  if ((res = ltype2 - l.ltype2)) return res;
96  return l2 - l.l2;
97  }
98 
102  std::string describe() const;
103 
104  void format(std::ostream& out, const char* undef="-") const;
105 
106  static inline Level cloud(int ltype2=MISSING_INT, int l2=MISSING_INT) { return Level(256, MISSING_INT, ltype2, l2); }
107  static inline Level waves(int ltype2=MISSING_INT, int l2=MISSING_INT) { return Level(264, MISSING_INT, ltype2, l2); }
108  static inline Level ana() { return Level(); }
109 };
110 
111 std::ostream& operator<<(std::ostream& out, const Level& l);
112 
113 struct Trange
114 {
116  int pind;
118  int p1;
120  int p2;
121 
122  Trange(int pind=MISSING_INT, int p1=MISSING_INT, int p2=MISSING_INT)
123  : pind(pind), p1(p1), p2(p2) {}
124  Trange(const char* pind, const char* p1=NULL, const char* p2=NULL);
125 
126  bool operator==(const Trange& tr) const
127  {
128  return pind == tr.pind && p1 == tr.p1 && p2 == tr.p2;
129  }
130 
131  bool operator!=(const Trange& tr) const
132  {
133  return pind != tr.pind || p1 != tr.p1 || p2 != tr.p2;
134  }
135 
136  bool operator<(const Trange& t) const { return compare(t) < 0; }
137  bool operator>(const Trange& t) const { return compare(t) > 0; }
138 
145  int compare(const Trange& t) const
146  {
147  int res;
148  if ((res = pind - t.pind)) return res;
149  if ((res = p1 - t.p1)) return res;
150  return p2 - t.p2;
151  }
152 
156  std::string describe() const;
157 
158  void format(std::ostream& out, const char* undef="-") const;
159 
160  static inline Trange instant() { return Trange(254, 0, 0); }
161  static inline Trange ana() { return Trange(); }
162 };
163 
164 std::ostream& operator<<(std::ostream& out, const Trange& l);
165 
167 struct Coord
168 {
170  int lat;
173  int lon;
174 
175  Coord() {}
176  Coord(int lat, int lon);
177  Coord(double lat, double lon);
178 
179  double dlat() const;
180  double dlon() const;
181 
182  bool operator<(const Coord& o) const { return compare(o) < 0; }
183  bool operator>(const Coord& o) const { return compare(o) > 0; }
184 
185  bool operator==(const Coord& c) const
186  {
187  return lat == c.lat && lon == c.lon;
188  }
189 
190  bool operator!=(const Coord& c) const
191  {
192  return lat != c.lat || lon != c.lon;
193  }
194 
201  int compare(const Coord& o) const
202  {
203  if (int res = lat - o.lat) return res;
204  return lon - o.lon;
205  }
206 
207  // Normalise longitude values to the [-180..180[ interval
208  static int normalon(int lon);
209  static double fnormalon(double lon);
210 };
211 
212 std::ostream& operator<<(std::ostream& out, const Coord& c);
213 
214 // Simple date structure
215 struct Date
216 {
217  unsigned short year;
218  unsigned char month;
219  unsigned char day;
220 
221  Date(unsigned short year, unsigned char month=1, unsigned char day=1)
222  : year(year), month(month), day(day)
223  {
224  }
225  Date(const Date& d) : year(d.year), month(d.month), day(d.day) {}
226  Date(const int* val)
227  : year(val[0]), month(val[1]), day(val[2])
228  {
229  }
230 
231  int compare(const Date& o) const
232  {
233  if (int res = year - o.year) return res;
234  if (int res = month - o.month) return res;
235  return day - o.day;
236  }
237 
238  bool operator<(const Date& dt) const;
239  bool operator>(const Date& dt) const;
240  bool operator==(const Date& dt) const;
241  bool operator!=(const Date& dt) const;
242 };
243 
244 std::ostream& operator<<(std::ostream& out, const Date& dt);
245 
246 struct Time
247 {
248  unsigned char hour;
249  unsigned char minute;
250  unsigned char second;
251 
252  Time(unsigned char hour=0, unsigned char minute=0, unsigned char second=0)
253  : hour(hour), minute(minute), second(second) {}
254  Time(const Time& d) : hour(d.hour), minute(d.minute), second(d.second) {}
255  Time(const int* val)
256  : hour(val[0]), minute(val[1]), second(val[2])
257  {
258  }
259 
260  int compare(const Time& o) const
261  {
262  if (int res = hour - o.hour) return res;
263  if (int res = minute - o.minute) return res;
264  return second - o.second;
265  }
266 
267  bool operator<(const Time& dt) const;
268  bool operator>(const Time& dt) const;
269  bool operator==(const Time& dt) const;
270  bool operator!=(const Time& dt) const;
271 };
272 
273 std::ostream& operator<<(std::ostream& out, const Time& t);
274 
276 struct Datetime
277 {
278  Date date;
279  Time time;
280 
281  Datetime(const Date& date, const Time& time) : date(date), time(time) {}
282  Datetime(unsigned short year, unsigned char month=1, unsigned char day=1,
283  unsigned char hour=0, unsigned char minute=0, unsigned char second=0)
284  : date(year, month, day), time(hour, minute, second) {}
285  Datetime(const int* val) : date(val), time(val+3) {}
286 
287  int compare(const Datetime& o) const
288  {
289  if (int res = date.compare(o.date)) return res;
290  return time.compare(o.time);
291  }
292 
293  bool operator==(const Datetime& dt) const;
294  bool operator!=(const Datetime& dt) const;
295  bool operator<(const Datetime& dt) const;
296  bool operator>(const Datetime& dt) const;
297 };
298 
299 std::ostream& operator<<(std::ostream& out, const Datetime& dt);
300 
301 }
302 
303 // vim:set ts=4 sw=4:
304 #endif
int ltype1
Type of the first level.
Definition: defs.h:57
int ltype2
Type of the second level.
Definition: defs.h:61
Definition: defs.h:215
int p1
Time range P1 indicator.
Definition: defs.h:118
Definition: defs.h:113
int pind
Time range type indicator.
Definition: defs.h:116
Definition: cmdline.h:34
Definition: defs.h:54
int lat
Latitude multiplied by 100000 (5 significant digits preserved)
Definition: defs.h:170
int l2
L2 value of the level.
Definition: defs.h:63
int lon
Longitude normalised from -180.0 to 180.0 and multiplied by 100000 (5 significant digits preserved) ...
Definition: defs.h:173
int compare(const Level &l) const
Compare two Level strutures, for use in sorting.
Definition: defs.h:90
Coordinates.
Definition: defs.h:167
Definition: defs.h:246
int compare(const Trange &t) const
Compare two Trange strutures, for use in sorting.
Definition: defs.h:145
Simple datetime structure.
Definition: defs.h:276
int compare(const Coord &o) const
Compare two Coords strutures, for use in sorting.
Definition: defs.h:201
int l1
L1 value of the level.
Definition: defs.h:59
int p2
Time range P2 indicator.
Definition: defs.h:120
std::string describe() const
Return a string description of this time range.
std::string describe() const
Return a string description of this level.