libdballe  7.7
types.h
Go to the documentation of this file.
1 #ifndef DBALLE_TYPES_H
2 #define DBALLE_TYPES_H
3 
8 #include <iosfwd>
9 #include <limits.h>
10 
11 namespace dballe {
12 
16 static constexpr int MISSING_INT = INT_MAX;
17 
24 struct Date
25 {
26  unsigned short year;
27  unsigned char month;
28  unsigned char day;
29 
31  Date();
32 
39  Date(int ye, int mo=1, int da=1);
40 
42  Date(const Date& d) = default;
43 
45  static Date from_julian(int jday);
46 
48  bool is_missing() const;
49 
51  int to_julian() const;
52 
56  void to_stream_iso8601(std::ostream& out) const;
57 
65  int compare(const Date& other) const;
66 
67  bool operator<(const Date& dt) const;
68  bool operator>(const Date& dt) const;
69  bool operator==(const Date& dt) const;
70  bool operator!=(const Date& dt) const;
71 
73  static void validate(int ye, int mo, int da);
75  static int days_in_month(int year, int month);
77  static int calendar_to_julian(int year, int month, int day);
79  static void julian_to_calendar(int jday, unsigned short& year, unsigned char& month, unsigned char& day);
80 };
81 
82 
89 struct Time
90 {
91  unsigned char hour;
92  unsigned char minute;
93  unsigned char second;
94 
96  Time();
97 
104  Time(int ho, int mi=0, int se=0);
105 
106  Time(const Time& t) = default;
107 
109  bool is_missing() const;
110 
115  void to_stream_iso8601(std::ostream& out) const;
116 
124  int compare(const Time& other) const;
125 
126  bool operator<(const Time& dt) const;
127  bool operator>(const Time& dt) const;
128  bool operator==(const Time& dt) const;
129  bool operator!=(const Time& dt) const;
130 
137  static void validate(int ho, int mi, int se);
138 };
139 
140 
147 struct Datetime
148 {
149  unsigned short year;
150  unsigned char month;
151  unsigned char day;
152  unsigned char hour;
153  unsigned char minute;
154  unsigned char second;
155 
157  Datetime();
158  Datetime(const Date& date, const Time& time);
159 
166  Datetime(int ye, int mo=1, int da=1, int ho=0, int mi=0, int se=0);
167 
169  static Datetime from_julian(int jday, int ho=0, int mi=0, int se=0);
170 
175  static Datetime lower_bound(int ye, int mo, int da, int ho, int mi, int se);
176 
181  static Datetime upper_bound(int ye, int mo, int da, int ho, int mi, int se);
182 
184  Date date() const;
185 
187  Time time() const;
188 
190  bool is_missing() const;
191 
193  int to_julian() const;
194 
202  int compare(const Datetime& other) const;
203 
204  bool operator==(const Datetime& dt) const;
205  bool operator!=(const Datetime& dt) const;
206  bool operator<(const Datetime& dt) const;
207  bool operator>(const Datetime& dt) const;
208  bool operator<=(const Datetime& dt) const;
209  bool operator>=(const Datetime& dt) const;
210 
214  void print_iso8601(FILE* out, char sep='T', const char* end="\n") const;
215 
222  void to_stream_iso8601(std::ostream& out, char sep='T', const char* tz="") const;
223 
229  static Datetime from_iso8601(const char* str);
230 
239  static void validate(int ye, int mo, int da, int ho, int mi, int se);
240 
245  static void normalise_h24(int& ye, int& mo, int& da, int& ho, int& mi, int& se);
246 };
247 
248 
256 {
261 
262  DatetimeRange() = default;
263  DatetimeRange(const Datetime& min, const Datetime& max) : min(min), max(max) {}
265  int yemin, int momin, int damin, int homin, int mimin, int semin,
266  int yemax, int momax, int damax, int homax, int mimax, int semax);
267 
269  bool is_missing() const;
270 
271  bool operator==(const DatetimeRange& dtr) const;
272  bool operator!=(const DatetimeRange& dtr) const;
273 
275  void set(const Datetime& min, const Datetime& max);
276 
289  void set(int yemin, int momin, int damin, int homin, int mimin, int semin,
290  int yemax, int momax, int damax, int homax, int mimax, int semax);
291 
296  void merge(const DatetimeRange& range);
297 
299  bool contains(const Datetime& dt) const;
300 
302  bool contains(const DatetimeRange& dtr) const;
303 
305  bool is_disjoint(const DatetimeRange& dtr) const;
306 };
307 
308 
320 struct Coords
321 {
323  int lat;
324 
329  int lon;
330 
332  Coords();
334  Coords(int lat, int lon);
336  Coords(double lat, double lon);
337 
339  bool is_missing() const;
340 
342  void set(int lat, int lon);
343 
345  void set(double lat, double lon);
346 
348  double dlat() const;
349 
351  double dlon() const;
352 
363  int compare(const Coords& o) const;
364 
365  bool operator==(const Coords& dt) const;
366  bool operator!=(const Coords& dt) const;
367  bool operator<(const Coords& dt) const;
368  bool operator>(const Coords& dt) const;
369  bool operator<=(const Coords& dt) const;
370  bool operator>=(const Coords& dt) const;
371 
373  void print(FILE* out, const char* end="\n") const;
374 };
375 
376 
390 struct LatRange
391 {
393  static constexpr int IMIN = -9000000;
395  static constexpr int IMAX = 9000000;
397  static constexpr double DMIN = -90.0;
399  static constexpr double DMAX = 90.0;
400 
402  int imin = IMIN;
404  int imax = IMAX;
405 
407  LatRange() = default;
409  LatRange(int min, int max);
411  LatRange(double min, double max);
412 
413  bool operator==(const LatRange& lr) const;
414  bool operator!=(const LatRange& lr) const;
415 
417  bool is_missing() const;
418 
420  double dmin() const;
421 
423  double dmax() const;
424 
426  void get(double& min, double& max) const;
427 
429  void set(int min, int max);
430 
432  void set(double min, double max);
433 
435  bool contains(int lat) const;
436 
438  bool contains(double lat) const;
439 
441  bool contains(const LatRange& lr) const;
442 };
443 
444 
464 struct LonRange
465 {
467  int imin = MISSING_INT;
469  int imax = MISSING_INT;
470 
472  LonRange() = default;
474  LonRange(int min, int max);
476  LonRange(double min, double max);
477 
478  bool operator==(const LonRange& lr) const;
479  bool operator!=(const LonRange& lr) const;
480 
482  bool is_missing() const;
483 
485  double dmin() const;
486 
488  double dmax() const;
489 
495  void get(double& min, double& max) const;
496 
498  void set(int min, int max);
499 
501  void set(double min, double max);
502 
504  bool contains(int lon) const;
505 
507  bool contains(double lon) const;
508 
510  bool contains(const LonRange& lr) const;
511 };
512 
513 
515 struct Level
516 {
518  int ltype1;
520  int l1;
522  int ltype2;
524  int l2;
525 
526  Level(int ltype1=MISSING_INT, int l1=MISSING_INT, int ltype2=MISSING_INT, int l2=MISSING_INT)
527  : ltype1(ltype1), l1(l1), ltype2(ltype2), l2(l2) {}
528 
530  bool is_missing() const;
531 
532  bool operator==(const Level& l) const;
533  bool operator!=(const Level& l) const;
534  bool operator<(const Level& l) const;
535  bool operator>(const Level& l) const;
536 
544  int compare(const Level& l) const;
545 
549  std::string describe() const;
550 
552  void to_stream(std::ostream& out, const char* undef="-") const;
553 
555  static Level cloud(int ltype2=MISSING_INT, int l2=MISSING_INT);
556 
558  void print(FILE* out, const char* undef="-", const char* end="\n") const;
559 };
560 
561 
565 struct Trange
566 {
568  int pind;
570  int p1;
572  int p2;
573 
574  Trange(int pind=MISSING_INT, int p1=MISSING_INT, int p2=MISSING_INT)
575  : pind(pind), p1(p1), p2(p2) {}
576 
578  bool is_missing() const;
579 
587  int compare(const Trange& t) const;
588 
589  bool operator==(const Trange& tr) const;
590  bool operator!=(const Trange& tr) const;
591  bool operator<(const Trange& t) const;
592  bool operator>(const Trange& t) const;
593 
597  std::string describe() const;
598 
600  void to_stream(std::ostream& out, const char* undef="-") const;
601 
603  static Trange instant();
604 
606  void print(FILE* out, const char* undef="-", const char* end="\n") const;
607 };
608 
609 
610 
611 }
612 
613 #endif
int ltype1
Type of the level or the first layer. See Level type values.
Definition: types.h:518
static constexpr int IMIN
Minimum possible integer value.
Definition: types.h:393
int compare(const Time &other) const
Generic comparison.
LonRange()=default
Construct a range that matches any longitude.
int ltype2
Type of the the second layer. See Level type values.
Definition: types.h:522
bool is_disjoint(const DatetimeRange &dtr) const
Check if the two ranges are completely disjoint.
int compare(const Datetime &other) const
Generic comparison.
Datetime()
Construct a missing datetime.
int to_julian() const
Convert the date to Julian day.
int compare(const Date &other) const
Generic comparison.
bool is_missing() const
Check if this datetime is the missing value.
static void validate(int ye, int mo, int da)
Raise an exception if the three values do not represent a valid date.
Time()
Construct a missing time.
static Trange instant()
Time range for instant values.
static void julian_to_calendar(int jday, unsigned short &year, unsigned char &month, unsigned char &day)
Convert a Julian day into a calendar date.
void to_stream_iso8601(std::ostream &out) const
Write the date to an output stream in ISO8601 date format.
void set(int lat, int lon)
Set from integers in 1/100000 of a degree.
Calendar date.
Definition: types.h:24
static Datetime upper_bound(int ye, int mo, int da, int ho, int mi, int se)
Return a Datetime filling the parts set to MISSING_INT with their highest possible values...
Coordinates.
Definition: types.h:320
static constexpr int IMAX
Maximum possible integer value.
Definition: types.h:395
int p1
Time range P1 indicator. See Time range values.
Definition: types.h:570
void set(int min, int max)
Set the extremes as integers.
static int calendar_to_julian(int year, int month, int day)
Convert a calendar date into a Julian day.
int imin
Initial point of the longitude range.
Definition: types.h:467
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:565
static constexpr double DMAX
Maximum possible double value.
Definition: types.h:399
static void normalise_h24(int &ye, int &mo, int &da, int &ho, int &mi, int &se)
Convert a datetime with an hour of 24:00:00 to hour 00:00:00 of the following day.
bool is_missing() const
Check if these coordinates are undefined.
int pind
Time range type indicator. See Time range values.
Definition: types.h:568
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
static Datetime from_julian(int jday, int ho=0, int mi=0, int se=0)
Set the date from a Julian day.
Coords()
Construct a missing Coords.
double dmax() const
Get the upper extreme as double, or 180.0 if missing.
bool contains(const Datetime &dt) const
Check if a Datetime is inside this range.
void set(int min, int max)
Set the extremes as integers.
double dmax() const
Get the upper extreme as double.
static Date from_julian(int jday)
Create a date from a Julian day.
static constexpr double DMIN
Minimum possible double value.
Definition: types.h:397
void print_iso8601(FILE *out, char sep='T', const char *end="\n") const
Print to an output stream in ISO8601 combined format.
bool is_missing() const
Check if this time is the missing value.
Vertical level or layer.
Definition: types.h:515
int compare(const Coords &o) const
Compare two Coords strutures, for use in sorting.
static Datetime lower_bound(int ye, int mo, int da, int ho, int mi, int se)
Return a Datetime filling the parts set to MISSING_INT with their lowest possible values...
double dmin() const
Get the lower extreme as double, or -180.0 if missing.
double dmin() const
Get the lower extreme as double.
int l2
L2 value of the second layer. See Level type values.
Definition: types.h:524
double dlat() const
Get the latitude in degrees.
Range of datetimes.
Definition: types.h:255
int compare(const Level &l) const
Generic comparison.
int imax
Final point of the longitude range.
Definition: types.h:469
void to_stream_iso8601(std::ostream &out, char sep='T', const char *tz="") const
Write the datetime to an output stream in ISO8601 combined format.
bool contains(int lon) const
Check if a point is inside this range (extremes included)
Datetime max
Upper bound of the range.
Definition: types.h:260
int lat
Latitude in 1/100000 of a degree (5 significant digits preserved)
Definition: types.h:323
int imin
Minimum latitude.
Definition: types.h:402
Range of latitudes.
Definition: types.h:390
static void validate(int ye, int mo, int da, int ho, int mi, int se)
Raise an exception if the three values do not represent a valid date/time.
Time of the day.
Definition: types.h:89
LatRange()=default
Construct a LatRange matching any latitude.
Datetime min
Lower bound of the range.
Definition: types.h:258
int lon
Longitude in 1/100000 of a degree (5 significant digits preserved) and normalised between -180...
Definition: types.h:329
void to_stream_iso8601(std::ostream &out) const
Write the time to an output stream in ISO8601 extended format (hh:mm:ss).
void print(FILE *out, const char *undef="-", const char *end="\n") const
Print to an output stream.
bool is_missing() const
Return true if the LatRange matches any latitude.
bool is_missing() const
Check if this date is the missing value.
int compare(const Trange &t) const
Generic comparison.
bool is_missing() const
Check if this level is fully set to the missing value.
void set(const Datetime &min, const Datetime &max)
Set the extremes.
Date and time.
Definition: types.h:147
int imax
Maximum latitude.
Definition: types.h:404
Time time() const
Return a Time with this time.
static Datetime from_iso8601(const char *str)
Parse an ISO8601 datetime string.
Date()
Construct a missing date.
int to_julian() const
Convert the date to Julian day.
void print(FILE *out, const char *end="\n") const
Print to an output stream.
bool is_missing() const
Check if this range is open on both sides.
double dlon() const
Get the longitude in degrees.
bool is_missing() const
Return true if the LonRange matches any longitude.
void merge(const DatetimeRange &range)
Merge range into this one, resulting in the smallest range that contains both.
void to_stream(std::ostream &out, const char *undef="-") const
Format to an output stream.
void print(FILE *out, const char *undef="-", const char *end="\n") const
Print to an output stream.
static int days_in_month(int year, int month)
Return the number of days in the given month.
Range of longitudes.
Definition: types.h:464
int l1
L1 value of the level or the first layer. See Level type values.
Definition: types.h:520
bool is_missing() const
Check if this level is fully set to the missing value.
static Level cloud(int ltype2=MISSING_INT, int l2=MISSING_INT)
Create a cloud special level. See Level type values.
int p2
Time range P2 indicator. See Time range values.
Definition: types.h:572
Date date() const
Return a Date with this date.
bool contains(int lat) const
Check if a point is inside this range (extremes included)
std::string describe() const
Return a string description of this time range.
std::string describe() const
Return a string description of this level.
void to_stream(std::ostream &out, const char *undef="-") const
Format to an output stream.
static void validate(int ho, int mi, int se)
Raise an exception if the three values do not represent a valid time.