-
Notifications
You must be signed in to change notification settings - Fork 5
/
metaf.hpp
7045 lines (6357 loc) · 228 KB
/
metaf.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018-2020 Nick Naumenko (https://gitlab.com/nnaumenko)
* All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#ifndef METAF_HPP
#define METAF_HPP
#if defined(__clang__)
// No issues with clang
// But clang defines both __clang__ and __GNUC__
#elif defined(__GNUC__)
// GCC gives false positives in GroupParser::parse
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
// GCC gives numerous false positives in switch/return methods
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif
#include <string>
#include <vector>
#include <variant>
#include <optional>
#include <regex>
#include <cmath>
namespace metaf {
// Metaf library version
struct Version {
inline static const int major = 4;
inline static const int minor = 4;
inline static const int patch = 1;
inline static const char tag [] = "";
};
class FixedGroup;
class LocationGroup;
class ReportTimeGroup;
class TrendGroup;
class WindGroup;
class VisibilityGroup;
class CloudGroup;
class WeatherGroup;
class TemperatureGroup;
class TemperatureForecastGroup;
class PressureGroup;
class RunwayVisualRangeGroup;
class RunwayStateGroup;
class SecondaryLocationGroup;
class RainfallGroup;
class SeaSurfaceGroup;
class ColourCodeGroup;
class MinMaxTemperatureGroup;
class PrecipitationGroup;
class LayerForecastGroup;
class PressureTendencyGroup;
class CloudTypesGroup;
class CloudLayersGroup;
class LightningGroup;
class VicinityGroup;
class MiscGroup;
class UnknownGroup;
using Group = std::variant<
FixedGroup,
LocationGroup,
ReportTimeGroup,
TrendGroup,
WindGroup,
VisibilityGroup,
CloudGroup,
WeatherGroup,
TemperatureGroup,
TemperatureForecastGroup,
PressureGroup,
RunwayVisualRangeGroup,
RunwayStateGroup,
SecondaryLocationGroup,
RainfallGroup,
SeaSurfaceGroup,
ColourCodeGroup,
MinMaxTemperatureGroup,
PrecipitationGroup,
LayerForecastGroup,
PressureTendencyGroup,
CloudTypesGroup,
CloudLayersGroup,
LightningGroup,
VicinityGroup,
MiscGroup,
UnknownGroup
>;
///////////////////////////////////////////////////////////////////////////
class Runway {
public:
enum class Designator {
NONE,
LEFT,
CENTER,
RIGHT,
};
unsigned int number() const { return rNumber; }
Designator designator() const { return rDesignator; }
bool isValid() const {
return (rNumber <= maxRunwayNumber ||
(rNumber == allRunwaysNumber && rDesignator == Designator::NONE) ||
(rNumber == messageRepetitionNumber && rDesignator == Designator::NONE));
}
bool isAllRunways() const {
return (rNumber == allRunwaysNumber && rDesignator == Designator::NONE);
}
bool isMessageRepetition() const {
return (rNumber == messageRepetitionNumber && rDesignator == Designator::NONE);
}
Runway() = default;
static inline std::optional<Runway> fromString(const std::string & s, bool enableRwy = false);
static Runway makeAllRunways() {
Runway rw;
rw.rNumber = allRunwaysNumber;
return rw;
}
private:
static inline std::optional<Designator> designatorFromChar(char c);
unsigned int rNumber = 0;
Designator rDesignator = Designator::NONE;
static const unsigned int allRunwaysNumber = 88;
static const unsigned int messageRepetitionNumber = 99;
static const auto maxRunwayNumber = 360 / 10;
};
class MetafTime {
public:
struct Date {
Date(unsigned int y, unsigned int m, unsigned int d) :
year(y), month(m), day(d) {}
unsigned int year;
unsigned int month;
unsigned int day;
};
std::optional<unsigned int> day() const { return dayValue; }
unsigned int hour() const { return hourValue; }
unsigned int minute() const { return minuteValue; }
inline bool isValid() const;
inline bool is3hourlyReportTime() const;
inline bool is6hourlyReportTime() const;
inline Date dateBeforeRef(const Date & refDate) const;
MetafTime() = default;
MetafTime(unsigned int hour, unsigned int minute) :
hourValue(hour), minuteValue(minute) {}
static inline std::optional<MetafTime> fromStringDDHHMM(const std::string & s);
static inline std::optional<MetafTime> fromStringDDHH(const std::string & s);
private:
std::optional<unsigned int> dayValue;
unsigned int hourValue = 0;
unsigned int minuteValue = 0;
static const inline unsigned int dayNotReported = 0;
static const inline unsigned int maxDay = 31;
static const inline unsigned int maxHour = 24;
static const inline unsigned int maxMinute = 59;
};
class Speed;
class Temperature {
public:
enum class Unit {
C,
F,
};
std::optional<float> temperature() const {
if (!tempValue.has_value()) return tempValue;
return (precise ?
(tempValue.value() * preciseValuePrecision) :
tempValue.value());
}
Unit unit() const { return tempUnit; }
std::optional<float> inline toUnit(Unit unit) const;
bool isFreezing() const { return freezing; }
bool isPrecise() const { return precise; }
bool isReported() const { return tempValue.has_value(); }
inline static std::optional<float> relativeHumidity(
const Temperature & airTemperature,
const Temperature & dewPoint);
inline static Temperature heatIndex(const Temperature & airTemperature,
float relativeHumidity);
inline static Temperature heatIndex(const Temperature & airTemperature,
const Temperature & dewPoint);
inline static Temperature windChill(const Temperature & airTemperature,
const Speed & windSpeed);
Temperature () = default;
static inline std::optional<Temperature> fromString(const std::string & s);
static inline std::optional<Temperature> fromRemarkString(const std::string & s);
private:
inline Temperature (float value);
std::optional<int> tempValue;
bool freezing = false;
static const Unit tempUnit = Unit::C;
bool precise = false; //True = tenth of degrees C, false = degrees C
static inline const float preciseValuePrecision = 0.1;
};
class Speed {
public:
enum class Unit {
KNOTS,
METERS_PER_SECOND,
KILOMETERS_PER_HOUR,
MILES_PER_HOUR
};
std::optional<unsigned int> speed() const { return speedValue; }
Unit unit() const { return speedUnit; }
std::optional<float> inline toUnit(Unit unit) const;
bool isReported() const { return speedValue.has_value(); }
Speed() = default;
static inline std::optional<Speed> fromString(const std::string & s, Unit unit);
static inline std::optional<Unit> unitFromString(const std::string & s);
private:
std::optional<unsigned int> speedValue;
Unit speedUnit = Unit::KNOTS;
static inline std::optional<float> knotsToUnit(float valueKnots, Unit otherUnit);
static inline std::optional<float> mpsToUnit(float valueMps, Unit otherUnit);
static inline std::optional<float> kmhToUnit(float valueKmh, Unit otherUnit);
static inline std::optional<float> mphToUnit(float valueMph, Unit otherUnit);
};
class Distance {
public:
enum class Unit {
METERS,
STATUTE_MILES,
FEET
};
enum class Modifier {
NONE,
LESS_THAN,
MORE_THAN,
DISTANT,
VICINITY
};
inline std::optional<unsigned int> integer() const { return distValueInt; }
inline std::optional<unsigned int> numerator() const { return distValueNum; }
inline std::optional<unsigned int> denominator() const { return distValueDen; }
Modifier modifier() const { return distModifier; }
Unit unit() const { return distUnit; }
bool isInteger() const {
return (integer().has_value() &&
!numerator().has_value() && !denominator().has_value());
}
bool isFraction() const {
return (!integer().has_value() &&
numerator().has_value() && denominator().has_value());
}
bool isValue() const {
return (integer().has_value() ||
(numerator().has_value() && denominator().has_value()));
}
bool isReported() const {
return (isValue() ||
modifier() == Modifier::DISTANT ||
modifier() == Modifier::VICINITY);
}
bool hasInteger() const { return integer().has_value(); }
bool hasFraction() const {
return (numerator().has_value() && denominator().has_value());
}
inline std::optional<float> toUnit(Unit unit) const;
bool isValid() const {
if (distValueDen.has_value() && !distValueDen.value()) return false;
if (distValueNum.has_value() && !distValueNum.value()) return false;
return true;
}
Distance() = default;
Distance(int d, Unit u) : distValueInt(d), distUnit(u) {} //Init distance without fraction
Distance(unsigned int numerator, unsigned int denominator) : //Init fractional distance in miles
distValueNum(numerator), distValueDen(denominator),
distUnit(Unit::STATUTE_MILES) {}
Distance(Unit u) : distUnit(u) {} // Init non-reported distance
static inline std::optional<Distance> fromIntegerAndFraction(const Distance & integer,
const Distance & fraction);
static inline std::optional<Distance> fromMeterString(const std::string & s);
static inline std::optional<Distance> fromMileString(const std::string & s);
static inline std::optional<Distance> fromHeightString(const std::string & s);
static inline std::optional<Distance> fromRvrString(const std::string & s, bool unitFeet);
static inline std::optional< std::pair<Distance,Distance> > fromLayerString(
const std::string & s);
static inline Distance cavokVisibility(bool unitMiles = false);
static inline std::optional<Distance> fromKmString(const std::string & s);
static inline Distance makeDistant();
static inline Distance makeVicinity();
private:
Modifier distModifier = Modifier::NONE;
std::optional<unsigned int> distValueInt;
std::optional<unsigned int> distValueNum;
std::optional<unsigned int> distValueDen;
Unit distUnit = Unit::METERS;
static const unsigned int heightFactor = 100; //height unit is 100s of feet
static const inline unsigned int cavokVisibilityMiles = 6;
static const inline unsigned int cavokVisibilityMeters = 10000;
// Icing or turbulence layer depth is given in 1000s of feet
static const unsigned int layerDepthFactor = 1000;
static inline std::optional<Modifier> modifierFromChar(char c);
static inline std::optional<float> metersToUnit(float value, Unit unit);
static inline std::optional<float> milesToUnit(float value, Unit unit);
static inline std::optional<float> feetToUnit(float value, Unit unit);
};
class Direction {
public:
enum class Cardinal {
NONE, // Not reported or no corresponding cardinal direction
NDV, // No Directional Variation
N,
S,
W,
E,
NW,
NE,
SW,
SE,
TRUE_N,
TRUE_W,
TRUE_S,
TRUE_E,
OHD, // Overhead
ALQDS, // All quadrants
UNKNOWN // Unknown
};
enum class Status {
OMMITTED, // Direction is ommitted (not specified at all)
NOT_REPORTED, // Direction is specified as not reported
VARIABLE, // Direction is reported as variable
NDV, // Direction is reported as No Directional Variation
VALUE_DEGREES, // Direction is reported as value in degrees
VALUE_CARDINAL, // Direction is reported as cardinal value
OVERHEAD, // Phenomena occurring directly over the location
ALQDS, // Direction is reported as all quadrants (in all directions)
UNKNOWN // Direction is reported as unknown explicitly
};
Status status() const { return dirStatus; }
inline Cardinal cardinal(bool trueDirections = false) const;
std::optional<unsigned int> degrees() const {
if (!isValue()) return std::optional<unsigned int>();
return dirDegrees;
}
bool isValue() const {
return (dirStatus == Status::VALUE_DEGREES ||
dirStatus == Status::VALUE_CARDINAL);
}
static inline std::vector<Cardinal> sectorCardinalDirToVector(
const Direction & dir1from,
const Direction & dir1to);
bool isValid() const {
if (isValue() && dirDegrees > maxDegrees) return false;
return true;
}
static inline Cardinal rotateOctantClockwise(Cardinal cardinal);
Direction() = default;
static inline std::optional<Direction> fromCardinalString(const std::string & s,
bool enableOhdAlqds = false,
bool enableUnknown = false);
static inline std::optional<Direction> fromDegreesString(const std::string & s);
static inline std::optional<std::pair<Direction, Direction>> fromSectorString(
const std::string & s);
private:
unsigned int dirDegrees = 0;
Status dirStatus = Status::OMMITTED;
private:
static const inline unsigned int maxDegrees = 360;
static const inline unsigned int octantSize = 45u;
static const inline unsigned int degreesTrueNorth = 360;
static const inline unsigned int degreesTrueWest = 270;
static const inline unsigned int degreesTrueSouth = 180;
static const inline unsigned int degreesTrueEast = 90;
static const inline unsigned int degreesNorthWest = 315;
static const inline unsigned int degreesNorthEast = 45;
static const inline unsigned int degreesSouthWest = 225;
static const inline unsigned int degreesSouthEast = 135;
};
class Pressure {
public:
enum class Unit {
HECTOPASCAL,
INCHES_HG,
MM_HG
};
std::optional<float> pressure() const { return pressureValue; }
Unit unit() const { return pressureUnit; }
inline std::optional<float> toUnit(Unit unit) const;
bool isReported() const { return pressureValue.has_value(); }
Pressure() = default;
static inline std::optional<Pressure> fromString(const std::string & s);
static inline std::optional<Pressure> fromForecastString(const std::string & s);
static inline std::optional<Pressure> fromSlpString(const std::string & s);
static inline std::optional<Pressure> fromQfeString(const std::string & s);
static inline std::optional<Pressure> fromTendencyString(const std::string & s);
private:
std::optional<float> pressureValue;
Unit pressureUnit = Unit::HECTOPASCAL;
static inline const float inHgDecimalPointShift = 0.01;
static inline const float tendencyDecimalPointShift = 0.1;
};
class Precipitation {
public:
enum class Unit {
MM,
INCHES,
};
std::optional<float> precipitation() const { return precipValue; }
Unit unit() const { return precipUnit; }
inline std::optional<float> toUnit(Unit unit) const;
bool isReported() const { return precipValue.has_value(); }
Precipitation() = default;
static inline std::optional<Precipitation> fromRainfallString(const std::string & s);
static inline std::optional<Precipitation> fromRunwayDeposits(const std::string & s);
static inline std::optional<Precipitation> fromRemarkString(const std::string & s,
float factor = 1,
Unit unit = Unit::INCHES,
bool allowNotReported = false);
static inline std::optional<std::pair<Precipitation, Precipitation> >
fromSnincrString(const std::string & s);
private:
std::optional<float> precipValue;
Unit precipUnit = Unit::MM;
private:
// Special value for runway deposits depth, see Table 1079 in Manual on Codes (WMO No. 306).
enum Reserved {
RESERVED = 91,
DEPTH_10CM = 92,
DEPTH_15CM = 93,
DEPTH_20CM = 94,
DEPTH_25CM = 95,
DEPTH_30CM = 96,
DEPTH_35CM = 97,
DEPTH_40CM = 98,
RUNWAY_NOT_OPERATIONAL = 99
};
};
class SurfaceFriction {
public:
enum class Status {
NOT_REPORTED,
SURFACE_FRICTION_REPORTED,
BRAKING_ACTION_REPORTED,
UNRELIABLE // Value unreliable or unmeasurable.
};
enum class BrakingAction {
NONE, // Not reported or unreliable
POOR, // Friction coef <0.26
MEDIUM_POOR, // Friction coef 0.26 to 0.29
MEDIUM, // Friction coef 0.30 to 0.35
MEDIUM_GOOD, // Friction coef 0.36 to 0.39
GOOD, // Friction coef >0.39
};
Status status() const { return sfStatus; }
std::optional<float> coefficient() const {
if (sfStatus == Status::NOT_REPORTED ||
sfStatus == Status::UNRELIABLE) return std::optional<float>();
return (sfCoefficient * coefficientDecimalPointShift);
}
inline BrakingAction brakingAction() const;
bool isReported() const { return (status() != Status::NOT_REPORTED); }
bool isUnreliable() const { return (status() == Status::UNRELIABLE); }
SurfaceFriction() = default;
static inline std::optional<SurfaceFriction> fromString(const std::string & s);
private:
Status sfStatus = Status::NOT_REPORTED;
unsigned int sfCoefficient = 0; //0 to 100, multiply by 0.01 to get actual value
static const inline auto coefficientDecimalPointShift = 0.01;
private:
// Special values for braking action, see Table 0366 in Manual on Codes (WMO No. 306).
enum Reserved {
BRAKING_ACTION_POOR = 91,
BRAKING_ACTION_MEDIUM_POOR = 92,
BRAKING_ACTION_MEDIUM = 93,
BRAKING_ACTION_MEDIUM_GOOD = 94,
BRAKING_ACTION_GOOD = 95,
RESERVED_96 = 96,
RESERVED_97 = 97,
RESERVED_98 = 98,
UNRELIABLE = 99,
};
static const inline auto baPoorLowLimit = 0u;
static const inline auto baMediumPoorLowLimit = 26u;
static const inline auto baMediumLowLimit = 30u;
static const inline auto baMediumGoodLowLimit = 36u;
static const inline auto baGoodLowLimit = 40u;
};
class WaveHeight {
public:
enum class Type {
STATE_OF_SURFACE, // Descriptive state of surface is specified
WAVE_HEIGHT, // Actual wave height is specified
};
enum class Unit {
METERS,
FEET,
};
// State of sea surface, see Table 3700 in Manual on Codes (WMO No. 306).
enum class StateOfSurface {
NOT_REPORTED,
CALM_GLASSY,
CALM_RIPPLED,
SMOOTH,
SLIGHT,
MODERATE,
ROUGH,
VERY_ROUGH,
HIGH,
VERY_HIGH,
PHENOMENAL,
};
Type type() const { return whType; }
inline StateOfSurface stateOfSurface() const;
Unit unit() const { return whUnit; }
std::optional<float> waveHeight() const {
if (!whValue.has_value()) return std::optional<float>();
return (whValue.value() * waveHeightDecimalPointShift);
}
bool isReported() const { return whValue.has_value(); }
inline std::optional<float> toUnit(Unit unit) const;
WaveHeight() = default;
static inline std::optional<WaveHeight> fromString(const std::string & s);
private:
Type whType = Type::STATE_OF_SURFACE;
std::optional<unsigned int> whValue; //in decimeters, muliply by 0.1 to get value in meters
static const inline auto waveHeightDecimalPointShift = 0.1;
static const Unit whUnit = Unit::METERS;
private:
static inline std::optional<unsigned int> waveHeightFromStateOfSurfaceChar(char c);
private:
//Values below are in decimeters, muliply by 0.1 to get value in meters
static const inline auto maxWaveHeightCalmGlassy = 0;
static const inline auto maxWaveHeightCalmRippled = 1;
static const inline auto maxWaveHeightSmooth = 5;
static const inline auto maxWaveHeightSlight = 12;
static const inline auto maxWaveHeightModerate = 25;
static const inline auto maxWaveHeightRough = 40;
static const inline auto maxWaveHeightVeryRough = 60;
static const inline auto maxWaveHeightHigh = 90;
static const inline auto maxWaveHeightVeryHigh = 140;
static const inline auto minWaveHeightPhenomenal = 141;
};
// Describes recent, current, or forecast weather phenomena
class WeatherPhenomena {
public:
enum class Qualifier {
NONE,
RECENT,
VICINITY,
LIGHT,
MODERATE,
HEAVY
};
enum class Descriptor {
NONE,
SHALLOW,
PARTIAL,
PATCHES,
LOW_DRIFTING,
BLOWING,
SHOWERS,
THUNDERSTORM,
FREEZING
};
enum class Weather {
OMMITTED,
NOT_REPORTED,
DRIZZLE,
RAIN,
SNOW,
SNOW_GRAINS,
ICE_CRYSTALS,
ICE_PELLETS,
HAIL,
SMALL_HAIL,
UNDETERMINED,
MIST,
FOG,
SMOKE,
VOLCANIC_ASH,
DUST,
SAND,
HAZE,
SPRAY,
DUST_WHIRLS,
SQUALLS,
FUNNEL_CLOUD,
SANDSTORM,
DUSTSTORM
};
enum class Event {
NONE,
BEGINNING,
ENDING
};
Qualifier qualifier() const { return q; }
Descriptor descriptor() const { return d; }
inline std::vector<Weather> weather() const;
Event event() const { return ev; }
std::optional<MetafTime> time() const { return tm; }
bool isOmmitted() const {
return (w[0] == Weather::OMMITTED &&
qualifier() == Qualifier::NONE &&
descriptor() == Descriptor::NONE &&
event() == Event::NONE &&
!tm.has_value());
}
inline bool isValid() const;
WeatherPhenomena() = default;
static inline std::optional <WeatherPhenomena> fromString(const std::string & s,
bool enableQualifiers = false);
static inline std::optional <WeatherPhenomena> fromWeatherBeginEndString(
const std::string & s,
const MetafTime & reportTime,
const WeatherPhenomena & previous);
static WeatherPhenomena notReported(bool recent) {
WeatherPhenomena result;
result.w[0] = Weather::NOT_REPORTED;
if (recent) result.q = Qualifier::RECENT;
return result;
}
private:
Qualifier q = Qualifier::NONE;
Descriptor d = Descriptor::NONE;
static const inline size_t wSize = 3;
Weather w[wSize] = { Weather::OMMITTED };
Event ev = Event::NONE;
std::optional<MetafTime> tm;
WeatherPhenomena(Weather wthr1,
Weather wthr2,
Descriptor dscr = Descriptor::NONE,
Qualifier qlf = Qualifier::NONE) : q(qlf), d(dscr)
{
w[0] = wthr1; w[1] = wthr2;
w[2] = Weather::OMMITTED;
}
WeatherPhenomena(Weather wthr,
Descriptor dscr = Descriptor::NONE,
Qualifier qlf = Qualifier::NONE) : q(qlf), d(dscr)
{
w[0] = wthr; w[1] = Weather::OMMITTED;
}
static inline bool isDescriptorShAllowed (Weather w);
static inline bool isDescriptorTsAllowed (Weather w);
static inline bool isDescriptorFzAllowed (Weather w);
};
///////////////////////////////////////////////////////////////////////////
enum class ReportType {
UNKNOWN,
METAR,
TAF
};
enum class ReportPart {
UNKNOWN,
HEADER,
METAR,
TAF,
RMK
};
enum class ReportError {
NONE,
EMPTY_REPORT,
EXPECTED_REPORT_TYPE_OR_LOCATION,
EXPECTED_LOCATION,
EXPECTED_REPORT_TIME,
EXPECTED_TIME_SPAN,
UNEXPECTED_REPORT_END,
UNEXPECTED_GROUP_AFTER_NIL,
UNEXPECTED_GROUP_AFTER_CNL,
UNEXPECTED_NIL_OR_CNL_IN_REPORT_BODY,
AMD_ALLOWED_IN_TAF_ONLY,
CNL_ALLOWED_IN_TAF_ONLY,
MAINTENANCE_INDICATOR_ALLOWED_IN_METAR_ONLY,
REPORT_TOO_LARGE
};
struct ReportMetadata {
ReportType type = ReportType::UNKNOWN;
ReportError error = ReportError::NONE;
std::optional<MetafTime> reportTime;
};
static const inline ReportMetadata missingMetadata;
///////////////////////////////////////////////////////////////////////////
// Result of appending string to an existing group
enum class AppendResult {
// String was appended to the group (i.e. the string is a continuation of
// the previous group); all info from the string is already absorbed into
// the group; no need to parse the string.
// E.g. visibility group "1" will append string "1/2SM" since together
// they form visibility group "1 1/2SM".
APPENDED,
// String could not be appended to this group (i.e. the string is a
// separate group independent from last group); the parser must attempt
// to parse the string as a group
// E.g. group "BKN040" will not append string "22/20" since these two
// groups represent unrelated data (clouds and temperature).
NOT_APPENDED,
// The group expected particular string to follow and this expectation
// was not met; the group is not valid without the expected string and
// must be converted to raw string; the following string must still be
// attempted to be parsed.
// E.g. group "PK" expects next group string "WND", and if instead
// string "AO2" follows, it means that the original group "PK" is not
// valid and must be converted to raw string, whereas "AO2" may still
// represent a valid group and must be parsed separately.
GROUP_INVALIDATED
};
///////////////////////////////////////////////////////////////////////////
// Default delimiter between groups
// Note: only used to append raw strings, see also groupDelimiterRegex
static const inline char groupDelimiterChar = ' ';
// This is the regex is used by parser to split report into separate
// group strings
static const inline std::regex groupDelimiterRegex("\\s+");
// Everything after this char is ignored by parser
static const inline char reportEndChar = '=';
// Fallback group is the group which stores raw string in
// case when no other group is able to recognise the content
// (i.e. if anything else fails)
using FallbackGroup = UnknownGroup;
///////////////////////////////////////////////////////////////////////////
class FixedGroup {
public:
enum class Type {
METAR,
SPECI,
TAF,
AMD,
NIL,
CNL,
COR,
AUTO,
R_SNOCLO,
CAVOK,
NSW,
RMK,
WSCONDS,
MAINTENANCE_INDICATOR,
AO1,
AO2,
AO1A,
AO2A,
NOSPECI,
RVRNO,
PWINO,
PNO,
FZRANO,
TSNO,
SLPNO,
FROIN,
CLD_MISG,
ICG_MISG,
PCPN_MISG,
PRES_MISG,
RVR_MISG,
T_MISG,
TD_MISG,
VIS_MISG, //Deprecated and not used anymore, use VisibilityGroup::Type:VIS_MISG
WND_MISG,
WX_MISG,
TS_LTNG_TEMPO_UNAVBL
};
Type type() const { return t; }
bool isValid() const { return (incompleteText == IncompleteText::NONE); }
FixedGroup() = default;
static inline std::optional<FixedGroup> parse(const std::string & group,
ReportPart reportPart,
const ReportMetadata & reportMetadata = missingMetadata);
AppendResult inline append(const std::string & group,
ReportPart reportPart = ReportPart::UNKNOWN,
const ReportMetadata & reportMetadata = missingMetadata);
private:
Type t;
enum class IncompleteText {
NONE,
CLD,
ICG,
PCPN,
PRES,
RVR,
T,
TD,
WND,
WX,
TS_LTNG,
TS_LTNG_TEMPO
};
IncompleteText incompleteText = IncompleteText::NONE;
FixedGroup(Type type) :t (type) {}
FixedGroup(IncompleteText it, Type t) :t (t), incompleteText(it) {}
static inline Type typeFromIncomplete(IncompleteText it);
};
class LocationGroup {
public:
std::string toString() const {return std::string(location);}
inline bool isValid() const { return true; }
LocationGroup() = default;
static inline std::optional<LocationGroup> parse(const std::string & group,
ReportPart reportPart,
const ReportMetadata & reportMetadata = missingMetadata);
AppendResult inline append(const std::string & group,
ReportPart reportPart = ReportPart::UNKNOWN,
const ReportMetadata & reportMetadata = missingMetadata);
private:
static const inline auto locationLength = 4;
char location [locationLength + 1] = "\0";
};
class ReportTimeGroup {
public:
MetafTime time() const { return t; };
bool isValid() const { return (t.isValid() && t.day().has_value()); }
ReportTimeGroup() = default;
static inline std::optional<ReportTimeGroup> parse(
const std::string & group,
ReportPart reportPart,
const ReportMetadata & reportMetadata = missingMetadata);
AppendResult inline append(const std::string & group,
ReportPart reportPart = ReportPart::UNKNOWN,
const ReportMetadata & reportMetadata = missingMetadata);
private:
MetafTime t;
};
class TrendGroup {
public:
enum class Type {
NONE, // Incomplete groups or their combination.
NOSIG,
BECMG,
TEMPO,
INTER,
FROM,
TIME_SPAN,
};
enum class Probability {
NONE, // Probability not specified.
PROB_30,
PROB_40,
};
Type type() const { return t; }
Probability probability() const { return prob; }
std::optional<MetafTime> timeFrom() const { return tFrom; }
std::optional<MetafTime> timeTill() const { return tTill; }
std::optional<MetafTime> timeAt() const { return tAt; }
bool isValid() const {
if (tFrom.has_value() && !tFrom->isValid()) return false;
if (tTill.has_value() && !tTill->isValid()) return false;
if (tAt.has_value() && !tAt->isValid()) return false;
return (type() != Type::NONE); // Incomplete groups are considered invalid
}
inline bool isTimeSpanGroup() const;
TrendGroup() = default;
static inline std::optional<TrendGroup> parse(
const std::string & group,
ReportPart reportPart,
const ReportMetadata & reportMetadata = missingMetadata);
AppendResult inline append(const std::string & group,
ReportPart reportPart = ReportPart::UNKNOWN,
const ReportMetadata & reportMetadata = missingMetadata);
private:
TrendGroup(Type type) : t(type) {}
TrendGroup(Probability p) : t(Type::NONE), prob(p) {}
static inline std::optional<TrendGroup> fromTimeSpan(const std::string & s);
static inline std::optional<TrendGroup> fromFm(const std::string & s);
static inline std::optional<TrendGroup> fromTrendTime(const std::string & s);
inline bool combineProbAndTrendTypeGroups(const TrendGroup & nextTrendGroup);
inline bool combineTrendTypeAndTimeGroup(const TrendGroup & nextTrendGroup);
inline bool combineProbAndTimeSpanGroups(const TrendGroup & nextTrendGroup);
inline bool combineIncompleteGroups(const TrendGroup & nextTrendGroup);
static inline bool canCombineTime(const TrendGroup & g1, const TrendGroup & g2);
inline void combineTime(const TrendGroup & nextTrendGroup);
inline bool isProbabilityGroup() const;
inline bool isTrendTypeGroup() const;
inline bool isTrendTimeGroup() const;
Type t = Type::NONE;
Probability prob = Probability::NONE;
std::optional<MetafTime> tFrom; // Time span beginning.
std::optional<MetafTime> tTill; // Time span end time.
std::optional<MetafTime> tAt; // Precise time.
};
class WindGroup {
public:
enum class Type {
SURFACE_WIND,
SURFACE_WIND_CALM,
VARIABLE_WIND_SECTOR,
SURFACE_WIND_WITH_VARIABLE_SECTOR,
WIND_SHEAR,
WIND_SHIFT,
WIND_SHIFT_FROPA,
PEAK_WIND
};
Type type() const { return windType; }
Direction direction() const { return windDir; }
Speed windSpeed() const { return wSpeed; }
Speed gustSpeed() const { return gSpeed; }
Distance height() const { return wShHeight; }
Direction varSectorBegin() const { return vsecBegin; }
Direction varSectorEnd() const { return vsecEnd; }
std::optional<MetafTime> eventTime() const { return evTime; }
inline bool isValid() const;
WindGroup() = default;
static inline std::optional<WindGroup> parse(
const std::string & group,
ReportPart reportPart,
const ReportMetadata & reportMetadata = missingMetadata);
AppendResult inline append(const std::string & group,
ReportPart reportPart = ReportPart::UNKNOWN,
const ReportMetadata & reportMetadata = missingMetadata);
private:
enum class IncompleteText {
NONE,
PK,
PK_WND
};
inline AppendResult parsePeakWind(const std::string & group,
const ReportMetadata & reportMetadata);
Type windType;
Direction windDir;
Speed wSpeed;
Speed gSpeed;
Distance wShHeight;
Direction vsecBegin;