libdballe  8.3
types.h
Go to the documentation of this file.
1 #ifndef DBALLE_TYPES_H
2 #define DBALLE_TYPES_H
3 
8 #include <dballe/fwd.h>
9 #include <wreport/varinfo.h>
10 #include <memory>
11 #include <iosfwd>
12 #include <functional>
13 
14 namespace wreport {
15 class Var;
16 }
17 
18 namespace dballe {
19 struct CSVWriter;
20 
27 struct Date
28 {
29  unsigned short year;
30  unsigned char month;
31  unsigned char day;
32 
34  Date();
35 
42  Date(int ye, int mo=1, int da=1);
43 
45  Date(const Date& d) = default;
46 
48  static Date from_julian(int jday);
49 
51  bool is_missing() const;
52 
54  int to_julian() const;
55 
59  void to_stream_iso8601(std::ostream& out) const;
60 
64  void to_csv_iso8601(CSVWriter& out) const;
65 
73  int compare(const Date& other) const;
74 
75  bool operator<(const Date& dt) const;
76  bool operator>(const Date& dt) const;
77  bool operator==(const Date& dt) const;
78  bool operator!=(const Date& dt) const;
79 
81  static void validate(int ye, int mo, int da);
83  static int days_in_month(int year, int month);
85  static int calendar_to_julian(int year, int month, int day);
87  static void julian_to_calendar(int jday, unsigned short& year, unsigned char& month, unsigned char& day);
88 };
89 
90 std::ostream& operator<<(std::ostream& out, const Date& dt);
91 
92 
99 struct Time
100 {
101  unsigned char hour;
102  unsigned char minute;
103  unsigned char second;
104 
106  Time();
107 
114  Time(int ho, int mi=0, int se=0);
115 
116  Time(const Time& t) = default;
117 
119  bool is_missing() const;
120 
125  void to_stream_iso8601(std::ostream& out) const;
126 
130  void to_csv_iso8601(CSVWriter& out) const;
131 
139  int compare(const Time& other) const;
140 
141  bool operator<(const Time& dt) const;
142  bool operator>(const Time& dt) const;
143  bool operator==(const Time& dt) const;
144  bool operator!=(const Time& dt) const;
145 
152  static void validate(int ho, int mi, int se);
153 };
154 
155 std::ostream& operator<<(std::ostream& out, const Time& t);
156 
157 
164 struct Datetime
165 {
166  unsigned short year;
167  unsigned char month;
168  unsigned char day;
169  unsigned char hour;
170  unsigned char minute;
171  unsigned char second;
172 
174  Datetime();
175  Datetime(const Date& date, const Time& time);
176 
183  Datetime(int ye, int mo=1, int da=1, int ho=0, int mi=0, int se=0);
184 
186  static Datetime from_julian(int jday, int ho=0, int mi=0, int se=0);
187 
192  static Datetime lower_bound(int ye, int mo, int da, int ho, int mi, int se);
193 
198  static Datetime upper_bound(int ye, int mo, int da, int ho, int mi, int se);
199 
201  void set_lower_bound();
202 
204  void set_upper_bound();
205 
207  Date date() const;
208 
210  Time time() const;
211 
213  bool is_missing() const;
214 
216  int to_julian() const;
217 
225  int compare(const Datetime& other) const;
226 
227  bool operator==(const Datetime& o) const;
228  bool operator!=(const Datetime& o) const;
229  bool operator<(const Datetime& o) const;
230  bool operator>(const Datetime& o) const;
231  bool operator<=(const Datetime& o) const;
232  bool operator>=(const Datetime& o) const;
233 
237  int print_iso8601(FILE* out, char sep='T', const char* end="\n") const;
238 
242  int print(FILE* out, const char* end="\n") const;
243 
251  void to_stream_iso8601(std::ostream& out, char sep='T', const char* tz="") const;
252 
256  void to_csv_iso8601(CSVWriter& out, char sep='T', const char* tz="") const;
257 
263  static Datetime from_iso8601(const char* str);
264 
273  static void validate(int ye, int mo, int da, int ho, int mi, int se);
274 
279  static void normalise_h24(int& ye, int& mo, int& da, int& ho, int& mi, int& se);
280 };
281 
282 std::ostream& operator<<(std::ostream& out, const Datetime& dt);
283 
284 
292 {
297 
298  DatetimeRange() = default;
299  DatetimeRange(const Datetime& min, const Datetime& max) : min(min), max(max) {}
301  int yemin, int momin, int damin, int homin, int mimin, int semin,
302  int yemax, int momax, int damax, int homax, int mimax, int semax);
303 
305  bool is_missing() const;
306 
307  bool operator==(const DatetimeRange& o) const;
308  bool operator!=(const DatetimeRange& o) const;
309  bool operator<(const DatetimeRange& o) const;
310  bool operator<=(const DatetimeRange& o) const;
311  bool operator>(const DatetimeRange& o) const;
312  bool operator>=(const DatetimeRange& o) const;
313 
315  void set(const Datetime& min, const Datetime& max);
316 
329  void set(int yemin, int momin, int damin, int homin, int mimin, int semin,
330  int yemax, int momax, int damax, int homax, int mimax, int semax);
331 
336  void merge(const DatetimeRange& range);
337 
339  bool contains(const Datetime& dt) const;
340 
342  bool contains(const DatetimeRange& dtr) const;
343 
345  bool is_disjoint(const DatetimeRange& dtr) const;
346 
348  int print(FILE* out, const char* end="\n") const;
349 };
350 
351 std::ostream& operator<<(std::ostream& out, const DatetimeRange& dtr);
352 
353 
365 struct Coords
366 {
368  int lat = MISSING_INT;
369 
374  int lon = MISSING_INT;
375 
377  Coords() = default;
379  Coords(int lat, int lon);
381  Coords(double lat, double lon);
382 
384  bool is_missing() const;
385 
387  void set_lat(double lat);
388 
390  void set_lon(double lon);
391 
393  void set_lat(int lat);
394 
396  void set_lon(int lon);
397 
399  void set(int lat, int lon);
400 
402  void set(double lat, double lon);
403 
405  double dlat() const;
406 
408  double dlon() const;
409 
420  int compare(const Coords& o) const;
421 
422  bool operator==(const Coords& o) const;
423  bool operator!=(const Coords& o) const;
424  bool operator<(const Coords& o) const;
425  bool operator>(const Coords& o) const;
426  bool operator<=(const Coords& o) const;
427  bool operator>=(const Coords& o) const;
428 
430  int print(FILE* out, const char* end="\n") const;
431 
433  std::string to_string(const char* undef="-") const;
434 
436  static int lat_to_int(double lat);
437 
439  static int lon_to_int(double lat);
440 
442  static double lat_from_int(int lat);
443 
445  static double lon_from_int(int lon);
446 };
447 
448 std::ostream& operator<<(std::ostream&, const Coords&);
449 
450 
464 struct LatRange
465 {
467  static constexpr int IMIN = -9000000;
469  static constexpr int IMAX = 9000000;
471  static constexpr double DMIN = -90.0;
473  static constexpr double DMAX = 90.0;
474 
476  int imin = IMIN;
478  int imax = IMAX;
479 
481  LatRange() = default;
483  LatRange(int min, int max);
485  LatRange(double min, double max);
486 
487  bool operator==(const LatRange& lr) const;
488  bool operator!=(const LatRange& lr) const;
489 
491  bool is_missing() const;
492 
494  double dmin() const;
495 
497  double dmax() const;
498 
500  void get(double& min, double& max) const;
501 
503  void set(int min, int max);
504 
506  void set(double min, double max);
507 
509  bool contains(int lat) const;
510 
512  bool contains(double lat) const;
513 
515  bool contains(const LatRange& lr) const;
516 
523  int print(FILE* out, const char* end="\n") const;
524 };
525 
526 std::ostream& operator<<(std::ostream& out, const LatRange& lr);
527 
528 
548 struct LonRange
549 {
551  int imin = MISSING_INT;
553  int imax = MISSING_INT;
554 
556  LonRange() = default;
558  LonRange(int min, int max);
560  LonRange(double min, double max);
561 
562  bool operator==(const LonRange& lr) const;
563  bool operator!=(const LonRange& lr) const;
564 
566  bool is_missing() const;
567 
569  double dmin() const;
570 
572  double dmax() const;
573 
579  void get(double& min, double& max) const;
580 
585  void set(int min, int max);
586 
591  void set(double min, double max);
592 
597  void set(const LonRange& lr);
598 
600  bool contains(int lon) const;
601 
603  bool contains(double lon) const;
604 
606  bool contains(const LonRange& lr) const;
607 
614  int print(FILE* out, const char* end="\n") const;
615 };
616 
617 std::ostream& operator<<(std::ostream& out, const LonRange& lr);
618 
619 
621 struct Level
622 {
624  int ltype1;
626  int l1;
628  int ltype2;
630  int l2;
631 
632  Level(int ltype1=MISSING_INT, int l1=MISSING_INT, int ltype2=MISSING_INT, int l2=MISSING_INT)
633  : ltype1(ltype1), l1(l1), ltype2(ltype2), l2(l2) {}
634 
636  bool is_missing() const;
637 
638  bool operator==(const Level& o) const;
639  bool operator!=(const Level& o) const;
640  bool operator<(const Level& o) const;
641  bool operator>(const Level& o) const;
642  bool operator<=(const Level& o) const;
643  bool operator>=(const Level& o) const;
644 
652  int compare(const Level& l) const;
653 
657  std::string describe() const;
658 
660  void to_stream(std::ostream& out, const char* undef="-") const;
661 
663  std::string to_string(const char* undef="-") const;
664 
668  void to_csv(CSVWriter& out) const;
669 
671  static Level cloud(int ltype2=MISSING_INT, int l2=MISSING_INT);
672 
674  int print(FILE* out, const char* undef="-", const char* end="\n") const;
675 };
676 
677 std::ostream& operator<<(std::ostream& out, const Level& l);
678 
679 
683 struct Trange
684 {
686  int pind;
688  int p1;
690  int p2;
691 
692  Trange(int pind=MISSING_INT, int p1=MISSING_INT, int p2=MISSING_INT)
693  : pind(pind), p1(p1), p2(p2) {}
694 
696  bool is_missing() const;
697 
705  int compare(const Trange& t) const;
706 
707  bool operator==(const Trange& o) const;
708  bool operator!=(const Trange& o) const;
709  bool operator<(const Trange& o) const;
710  bool operator>(const Trange& o) const;
711  bool operator<=(const Trange& o) const;
712  bool operator>=(const Trange& o) const;
713 
717  std::string describe() const;
718 
720  void to_stream(std::ostream& out, const char* undef="-") const;
721 
723  std::string to_string(const char* undef="-") const;
724 
728  void to_csv(CSVWriter& out) const;
729 
731  static Trange instant();
732 
734  int print(FILE* out, const char* undef="-", const char* end="\n") const;
735 };
736 
737 std::ostream& operator<<(std::ostream& out, const Trange& l);
738 
739 
744 class Ident
745 {
746 protected:
747  char* value = nullptr;
748 
749 public:
750  Ident() = default;
751  Ident(const char* value);
752  Ident(const std::string& value);
753  Ident(const Ident& o);
754  Ident(Ident&& o);
755  ~Ident();
756  Ident& operator=(const Ident& o);
757  Ident& operator=(Ident&& o);
758  Ident& operator=(const char* o);
759  Ident& operator=(const std::string& o);
760 
762  const char* get() const { return value; }
763 
765  void clear();
766 
767  int compare(const Ident& o) const;
768  int compare(const char* o) const;
769  int compare(const std::string& o) const;
770  template<typename T> bool operator==(const T& o) const { return compare(o) == 0; }
771  template<typename T> bool operator!=(const T& o) const { return compare(o) != 0; }
772  template<typename T> bool operator<(const T& o) const { return compare(o) < 0; }
773  template<typename T> bool operator<=(const T& o) const { return compare(o) <= 0; }
774  template<typename T> bool operator>(const T& o) const { return compare(o) > 0; }
775  template<typename T> bool operator>=(const T& o) const { return compare(o) >= 0; }
776 
778  bool is_missing() const;
779 
780  operator const char*() const { return value; }
781  operator std::string() const;
782 };
783 
784 std::ostream& operator<<(std::ostream&, const Ident&);
785 
786 
790 struct Station
791 {
793  std::string report;
794 
797 
800 
801 
802  Station() = default;
803 
805  bool is_missing() const;
806 
807  bool operator==(const Station& o) const
808  {
809  return std::tie(report, coords, ident) == std::tie(o.report, o.coords, o.ident);
810  }
811  bool operator!=(const Station& o) const
812  {
813  return std::tie(report, coords, ident) != std::tie(o.report, o.coords, o.ident);
814  }
815  bool operator<(const Station& o) const
816  {
817  return std::tie(report, coords, ident) < std::tie(o.report, o.coords, o.ident);
818  }
819  bool operator<=(const Station& o) const
820  {
821  return std::tie(report, coords, ident) <= std::tie(o.report, o.coords, o.ident);
822  }
823  bool operator>(const Station& o) const
824  {
825  return std::tie(report, coords, ident) > std::tie(o.report, o.coords, o.ident);
826  }
827  bool operator>=(const Station& o) const
828  {
829  return std::tie(report, coords, ident) >= std::tie(o.report, o.coords, o.ident);
830  }
831 
838  int print(FILE* out, const char* end="\n") const;
839 
841  std::string to_string(const char* undef="-") const;
842 };
843 
844 std::ostream& operator<<(std::ostream&, const Station&);
845 
846 
847 struct DBStation : public Station
848 {
854  int id = MISSING_INT;
855 
856 
857  DBStation() = default;
858 
860  bool is_missing() const;
861 
862  bool operator==(const DBStation& o) const
863  {
864  return std::tie(id, report, coords, ident) == std::tie(o.id, o.report, o.coords, o.ident);
865  }
866  bool operator!=(const DBStation& o) const
867  {
868  return std::tie(id, report, coords, ident) != std::tie(o.id, o.report, o.coords, o.ident);
869  }
870  bool operator<(const DBStation& o) const
871  {
872  return std::tie(id, report, coords, ident) < std::tie(o.id, o.report, o.coords, o.ident);
873  }
874  bool operator<=(const DBStation& o) const
875  {
876  return std::tie(id, report, coords, ident) <= std::tie(o.id, o.report, o.coords, o.ident);
877  }
878  bool operator>(const DBStation& o) const
879  {
880  return std::tie(id, report, coords, ident) > std::tie(o.id, o.report, o.coords, o.ident);
881  }
882  bool operator>=(const DBStation& o) const
883  {
884  return std::tie(id, report, coords, ident) >= std::tie(o.id, o.report, o.coords, o.ident);
885  }
886 
893  int print(FILE* out, const char* end="\n") const;
894 
896  std::string to_string(const char* undef="-") const;
897 };
898 
899 std::ostream& operator<<(std::ostream&, const DBStation&);
900 
901 }
902 
903 namespace std {
904 
905 template<> struct hash<dballe::Level>
906 {
908  typedef size_t result_type;
909  result_type operator()(argument_type const& o) const noexcept;
910 };
911 
912 template<> struct hash<dballe::Trange>
913 {
915  typedef size_t result_type;
916  result_type operator()(argument_type const& o) const noexcept;
917 };
918 
919 template<> struct hash<dballe::Coords>
920 {
922  typedef size_t result_type;
923  result_type operator()(argument_type const& o) const noexcept;
924 };
925 
926 template<> struct hash<dballe::Ident>
927 {
929  typedef size_t result_type;
930  result_type operator()(argument_type const& o) const noexcept;
931 };
932 
933 template<> struct hash<dballe::Station>
934 {
936  typedef size_t result_type;
937  result_type operator()(argument_type const& o) const noexcept;
938 };
939 
940 template<> struct hash<dballe::DBStation>
941 {
943  typedef size_t result_type;
944  result_type operator()(argument_type const& o) const noexcept;
945 };
946 
947 }
948 
949 #endif
Definition: csv.h:140
int ltype1
Type of the level or the first layer. See Level type values.
Definition: types.h:624
int ltype2
Type of the the second layer. See Level type values.
Definition: types.h:628
Ident ident
Mobile station identifier.
Definition: types.h:799
Station information.
Definition: types.h:790
Calendar date.
Definition: types.h:27
Coords coords
Station coordinates.
Definition: types.h:796
Coordinates.
Definition: types.h:365
int p1
Time range P1 indicator.
Definition: types.h:688
Definition: utils.h:31
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:683
int pind
Time range type indicator.
Definition: types.h:686
Definition: cmdline.h:18
Vertical level or layer.
Definition: types.h:621
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: types.h:744
int l2
L2 value of the second layer. See Level type values.
Definition: types.h:630
Range of datetimes.
Definition: types.h:291
Datetime max
Upper bound of the range.
Definition: types.h:296
std::string report
Report name for this station.
Definition: types.h:793
int id
Database ID of the station.
Definition: types.h:854
Range of latitudes.
Definition: types.h:464
Time of the day.
Definition: types.h:99
Datetime min
Lower bound of the range.
Definition: types.h:294
Date and time.
Definition: types.h:164
Definition: types.h:847
Range of longitudes.
Definition: types.h:548
int l1
L1 value of the level or the first layer. See Level type values.
Definition: types.h:626
int p2
Time range P2 indicator.
Definition: types.h:690