libdballe  7.29
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 struct CSVWriter;
13 
17 static constexpr int MISSING_INT = INT_MAX;
18 
25 struct Date
26 {
27  unsigned short year;
28  unsigned char month;
29  unsigned char day;
30 
32  Date();
33 
40  Date(int ye, int mo=1, int da=1);
41 
43  Date(const Date& d) = default;
44 
46  static Date from_julian(int jday);
47 
49  bool is_missing() const;
50 
52  int to_julian() const;
53 
57  void to_stream_iso8601(std::ostream& out) const;
58 
62  void to_csv_iso8601(CSVWriter& out) const;
63 
71  int compare(const Date& other) const;
72 
73  bool operator<(const Date& dt) const;
74  bool operator>(const Date& dt) const;
75  bool operator==(const Date& dt) const;
76  bool operator!=(const Date& dt) const;
77 
79  static void validate(int ye, int mo, int da);
81  static int days_in_month(int year, int month);
83  static int calendar_to_julian(int year, int month, int day);
85  static void julian_to_calendar(int jday, unsigned short& year, unsigned char& month, unsigned char& day);
86 };
87 
88 
95 struct Time
96 {
97  unsigned char hour;
98  unsigned char minute;
99  unsigned char second;
100 
102  Time();
103 
110  Time(int ho, int mi=0, int se=0);
111 
112  Time(const Time& t) = default;
113 
115  bool is_missing() const;
116 
121  void to_stream_iso8601(std::ostream& out) const;
122 
126  void to_csv_iso8601(CSVWriter& out) const;
127 
135  int compare(const Time& other) const;
136 
137  bool operator<(const Time& dt) const;
138  bool operator>(const Time& dt) const;
139  bool operator==(const Time& dt) const;
140  bool operator!=(const Time& dt) const;
141 
148  static void validate(int ho, int mi, int se);
149 };
150 
151 
158 struct Datetime
159 {
160  unsigned short year;
161  unsigned char month;
162  unsigned char day;
163  unsigned char hour;
164  unsigned char minute;
165  unsigned char second;
166 
168  Datetime();
169  Datetime(const Date& date, const Time& time);
170 
177  Datetime(int ye, int mo=1, int da=1, int ho=0, int mi=0, int se=0);
178 
180  static Datetime from_julian(int jday, int ho=0, int mi=0, int se=0);
181 
186  static Datetime lower_bound(int ye, int mo, int da, int ho, int mi, int se);
187 
192  static Datetime upper_bound(int ye, int mo, int da, int ho, int mi, int se);
193 
195  Date date() const;
196 
198  Time time() const;
199 
201  bool is_missing() const;
202 
204  int to_julian() const;
205 
213  int compare(const Datetime& other) const;
214 
215  bool operator==(const Datetime& dt) const;
216  bool operator!=(const Datetime& dt) const;
217  bool operator<(const Datetime& dt) const;
218  bool operator>(const Datetime& dt) const;
219  bool operator<=(const Datetime& dt) const;
220  bool operator>=(const Datetime& dt) const;
221 
225  int print_iso8601(FILE* out, char sep='T', const char* end="\n") const;
226 
234  void to_stream_iso8601(std::ostream& out, char sep='T', const char* tz="") const;
235 
239  void to_csv_iso8601(CSVWriter& out, char sep='T', const char* tz="") const;
240 
246  static Datetime from_iso8601(const char* str);
247 
256  static void validate(int ye, int mo, int da, int ho, int mi, int se);
257 
262  static void normalise_h24(int& ye, int& mo, int& da, int& ho, int& mi, int& se);
263 };
264 
265 
273 {
278 
279  DatetimeRange() = default;
280  DatetimeRange(const Datetime& min, const Datetime& max) : min(min), max(max) {}
282  int yemin, int momin, int damin, int homin, int mimin, int semin,
283  int yemax, int momax, int damax, int homax, int mimax, int semax);
284 
286  bool is_missing() const;
287 
288  bool operator==(const DatetimeRange& dtr) const;
289  bool operator!=(const DatetimeRange& dtr) const;
290 
292  void set(const Datetime& min, const Datetime& max);
293 
306  void set(int yemin, int momin, int damin, int homin, int mimin, int semin,
307  int yemax, int momax, int damax, int homax, int mimax, int semax);
308 
313  void merge(const DatetimeRange& range);
314 
316  bool contains(const Datetime& dt) const;
317 
319  bool contains(const DatetimeRange& dtr) const;
320 
322  bool is_disjoint(const DatetimeRange& dtr) const;
323 };
324 
325 
337 struct Coords
338 {
340  int lat;
341 
346  int lon;
347 
349  Coords();
351  Coords(int lat, int lon);
353  Coords(double lat, double lon);
354 
356  bool is_missing() const;
357 
359  void set(int lat, int lon);
360 
362  void set(double lat, double lon);
363 
365  double dlat() const;
366 
368  double dlon() const;
369 
380  int compare(const Coords& o) const;
381 
382  bool operator==(const Coords& dt) const;
383  bool operator!=(const Coords& dt) const;
384  bool operator<(const Coords& dt) const;
385  bool operator>(const Coords& dt) const;
386  bool operator<=(const Coords& dt) const;
387  bool operator>=(const Coords& dt) const;
388 
390  int print(FILE* out, const char* end="\n") const;
391 };
392 
393 
407 struct LatRange
408 {
410  static constexpr int IMIN = -9000000;
412  static constexpr int IMAX = 9000000;
414  static constexpr double DMIN = -90.0;
416  static constexpr double DMAX = 90.0;
417 
419  int imin = IMIN;
421  int imax = IMAX;
422 
424  LatRange() = default;
426  LatRange(int min, int max);
428  LatRange(double min, double max);
429 
430  bool operator==(const LatRange& lr) const;
431  bool operator!=(const LatRange& lr) const;
432 
434  bool is_missing() const;
435 
437  double dmin() const;
438 
440  double dmax() const;
441 
443  void get(double& min, double& max) const;
444 
446  void set(int min, int max);
447 
449  void set(double min, double max);
450 
452  bool contains(int lat) const;
453 
455  bool contains(double lat) const;
456 
458  bool contains(const LatRange& lr) const;
459 };
460 
461 
481 struct LonRange
482 {
484  int imin = MISSING_INT;
486  int imax = MISSING_INT;
487 
489  LonRange() = default;
491  LonRange(int min, int max);
493  LonRange(double min, double max);
494 
495  bool operator==(const LonRange& lr) const;
496  bool operator!=(const LonRange& lr) const;
497 
499  bool is_missing() const;
500 
502  double dmin() const;
503 
505  double dmax() const;
506 
512  void get(double& min, double& max) const;
513 
515  void set(int min, int max);
516 
518  void set(double min, double max);
519 
521  bool contains(int lon) const;
522 
524  bool contains(double lon) const;
525 
527  bool contains(const LonRange& lr) const;
528 };
529 
530 
532 struct Level
533 {
535  int ltype1;
537  int l1;
539  int ltype2;
541  int l2;
542 
543  Level(int ltype1=MISSING_INT, int l1=MISSING_INT, int ltype2=MISSING_INT, int l2=MISSING_INT)
544  : ltype1(ltype1), l1(l1), ltype2(ltype2), l2(l2) {}
545 
547  bool is_missing() const;
548 
549  bool operator==(const Level& l) const;
550  bool operator!=(const Level& l) const;
551  bool operator<(const Level& l) const;
552  bool operator>(const Level& l) const;
553 
561  int compare(const Level& l) const;
562 
566  std::string describe() const;
567 
569  void to_stream(std::ostream& out, const char* undef="-") const;
570 
574  void to_csv(CSVWriter& out) const;
575 
577  static Level cloud(int ltype2=MISSING_INT, int l2=MISSING_INT);
578 
580  int print(FILE* out, const char* undef="-", const char* end="\n") const;
581 };
582 
583 
587 struct Trange
588 {
590  int pind;
592  int p1;
594  int p2;
595 
596  Trange(int pind=MISSING_INT, int p1=MISSING_INT, int p2=MISSING_INT)
597  : pind(pind), p1(p1), p2(p2) {}
598 
600  bool is_missing() const;
601 
609  int compare(const Trange& t) const;
610 
611  bool operator==(const Trange& tr) const;
612  bool operator!=(const Trange& tr) const;
613  bool operator<(const Trange& t) const;
614  bool operator>(const Trange& t) const;
615 
619  std::string describe() const;
620 
622  void to_stream(std::ostream& out, const char* undef="-") const;
623 
627  void to_csv(CSVWriter& out) const;
628 
630  static Trange instant();
631 
633  int print(FILE* out, const char* undef="-", const char* end="\n") const;
634 };
635 
636 
637 
638 }
639 
640 #endif
Definition: csv.h:140
int ltype1
Type of the level or the first layer. See Level type values.
Definition: types.h:535
static constexpr int IMIN
Minimum possible integer value.
Definition: types.h:410
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:539
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:25
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:337
static constexpr int IMAX
Maximum possible integer value.
Definition: types.h:412
int p1
Time range P1 indicator.
Definition: types.h:592
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:484
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:587
static constexpr double DMAX
Maximum possible double value.
Definition: types.h:416
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.
Definition: types.h:590
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 to_csv(CSVWriter &out) const
Write the datetime to a CSV writer as 3 fields.
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:414
bool is_missing() const
Check if this time is the missing value.
Vertical level or layer.
Definition: types.h:532
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:541
double dlat() const
Get the latitude in degrees.
void to_csv_iso8601(CSVWriter &out) const
Write the date as a CSV field in ISO8601 date format.
Range of datetimes.
Definition: types.h:272
int compare(const Level &l) const
Generic comparison.
int imax
Final point of the longitude range.
Definition: types.h:486
void to_csv_iso8601(CSVWriter &out, char sep='T', const char *tz="") const
Write the datetime as a CSV field in ISO8601 date format.
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.
int print(FILE *out, const char *undef="-", const char *end="\n") const
Print to an output stream.
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:277
int lat
Latitude in 1/100000 of a degree (5 significant digits preserved)
Definition: types.h:340
int imin
Minimum latitude.
Definition: types.h:419
int print(FILE *out, const char *end="\n") const
Print to an output stream.
Range of latitudes.
Definition: types.h:407
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:95
LatRange()=default
Construct a LatRange matching any latitude.
Datetime min
Lower bound of the range.
Definition: types.h:275
int lon
Longitude in 1/100000 of a degree (5 significant digits preserved) and normalised between -180...
Definition: types.h:346
void to_stream_iso8601(std::ostream &out) const
Write the time to an output stream in ISO8601 extended format (hh:mm:ss).
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:158
int print_iso8601(FILE *out, char sep='T', const char *end="\n") const
Print to an output stream in ISO8601 combined format.
int imax
Maximum latitude.
Definition: types.h:421
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.
bool is_missing() const
Check if this range is open on both sides.
void to_csv_iso8601(CSVWriter &out) const
Write the time as a CSV field in ISO8601 date format.
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.
static int days_in_month(int year, int month)
Return the number of days in the given month.
Range of longitudes.
Definition: types.h:481
void to_csv(CSVWriter &out) const
Write the datetime to a CSV writer as 4 fields.
int l1
L1 value of the level or the first layer. See Level type values.
Definition: types.h:537
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.
Definition: types.h:594
Date date() const
Return a Date with this date.
int print(FILE *out, const char *undef="-", const char *end="\n") const
Print to an output stream.
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.