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
use uom::si::f64::{Angle, Length, Pressure, TemperatureInterval, Velocity};

macro_rules! enum_with_str_repr {
    (
        $(#[$enum_attr:meta])*
        $ident: ident {
            $(
                $(#[$variant_attr:meta])*
                $variant: ident => $val: literal $(| $alt: literal)*,
            )*
    }) => {
        #[derive(Clone, Copy, PartialEq, Eq, Debug)]
        $(#[$enum_attr])*
        pub enum $ident {
            $(
                $(#[$variant_attr])*
                $variant,
            )*
        }

        impl Into<&'static str> for $ident {
            fn into(self) -> &'static str {
                use $ident::*;
                match self {
                    $(
                        $variant => $val,
                    )*
                }
            }
        }

        impl<'input> std::convert::TryFrom<&'input str> for $ident {
            type Error = ();

            fn try_from(val: &'input str) -> Result<Self, Self::Error> {
                use $ident::*;
                match val {
                    $(
                        $val $(| $alt)* => Ok($variant),
                    )*
                    _ => Err(())
                }
            }
        }
    };
}

enum_with_str_repr! {
    ObservationType {
        Auto => "AUTO",
        Correction => "COR",
        CorrectionA => "CCA",
        CorrectionB => "CCB",
        Delayed => "RTD",
    }
}

enum_with_str_repr! {
    CloudCoverage {
        NoCloud => "SKC",
        NilCloud => "NCD",
        Clear => "CLR",
        NoSignificantCloud => "NSC",
        Few => "FEW" | "FW",
        Scattered => "SCT" | "SC",
        Broken => "BKN",
        Overcast => "OVC",
        VerticalVisibility => "VV",
    }
}

enum_with_str_repr! {
    Intensity {
        Light => "-",
        Moderate => "",
        Heavy => "+",
    }
}

enum_with_str_repr! {
    Descriptor {
        Shallow => "MI",
        Partial => "PR",
        Patches => "BC",
        LowDrifting => "DR",
        Blowing => "BL",
        Showers => "SH",
        Thunderstorm => "TS",
        Freezing => "FZ",
    }
}

enum_with_str_repr! {
    Precipitation {
        Rain => "RA",
        Drizzle => "DZ",
        Snow => "SN",
        SnowGrains => "SG",
        IceCrystals => "IC",
        IcePellets => "PL",
        Hail => "GR",
        Graupel => "GS",
        Unknown => "UP",
    }
}

enum_with_str_repr! {
    Obscuration {
        Fog => "FG",
        Mist => "BR",
        Haze => "HZ",
        VolcanicAsh => "VA",
        WidespreadDust => "DU",
        Smoke => "FU",
        Sand => "SA",
        Spray => "PY",
    }
}

enum_with_str_repr! {
    Other {
        Squall => "SQ",
        SandWhirls => "PO",
        Duststorm => "DS",
        Sandstorm => "SS",
        FunnelCloud => "FC",
    }
}

enum_with_str_repr! {
    CloudType {
        Cumulonimbus => "CB",
        ToweringCumulus => "TCU",
        Cumulus => "CU",
        Cirrus => "CI",
        Altocumulus => "AC",
        Stratus => "ST",
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ZuluDateTime {
    pub day_of_month: u8,
    pub hour: u8,
    pub minute: u8,
    /// Some stations omit the Z. It is unclear whether this means
    /// that the timestamp is not zulu, or if it is incorrect implementation.
    pub is_zulu: bool,
}

impl ZuluDateTime {
    #[cfg(feature = "chrono_helpers")]
    pub fn as_datetime(&self, year: i32, month: u32) -> chrono::DateTime<chrono_tz::Tz> {
        chrono::TimeZone::ymd(&chrono_tz::Greenwich, year, month, self.day_of_month as u32).and_hms(
            self.hour as u32,
            self.minute as u32,
            0,
        )
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Wind {
    /// A lack of direction indicates variable
    pub direction: Option<Angle>,
    pub speed: Velocity,
    pub peak_gust: Option<Velocity>,
    pub variance: Option<(Angle, Angle)>,
}

impl Wind {
    pub fn is_calm(&self) -> bool {
        self.speed.value < f64::EPSILON
            && self
                .direction
                .map(|angle| angle.value < f64::EPSILON)
                .unwrap_or(false)
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct RunwayVisibility<'input> {
    pub designator: &'input str,
    pub visibility: VisibilityType,
    pub trend: Option<VisibilityTrend>,
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum VisibilityType {
    Varying {
        lower: RawRunwayVisibility,
        upper: RawRunwayVisibility,
    },
    Fixed(RawRunwayVisibility),
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct RawRunwayVisibility {
    pub value: Length,
    pub out_of_range: Option<OutOfRange>,
}

enum_with_str_repr! {
    VisibilityTrend {
        Up => "U",
        Down => "D",
        NoChange => "N",
    }
}

enum_with_str_repr! {
    OutOfRange {
        Above => "P",
        Below => "M",
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct RunwayReport<'input> {
    pub designator: &'input str,
    pub report_info: RunwayReportInfo,
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum RunwayReportInfo {
    Cleared { friction: Option<f64> },
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Weather {
    pub intensity: Intensity,
    pub vicinity: bool,
    pub descriptor: Option<Descriptor>,
    /// Condition should always be present, unless this is a VCTS or VCSH
    pub condition: Option<Condition>,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Condition {
    Precipitation(Vec<Precipitation>),
    Obscuration(Obscuration),
    Other(Other),
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct CloudCover {
    pub coverage: CloudCoverage,
    /// The absence of a base indicates it is below station level or an inability to assess
    pub base: Option<Length>,
    pub cloud_type: Option<CloudType>,
}

/// If negative, these are rounded up to the more positive whole degree
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Temperatures {
    pub air: TemperatureInterval,
    /// Some stations don't report this
    pub dewpoint: Option<TemperatureInterval>,
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct AccumulatedRainfall {
    /// In the last 10 minutes
    pub recent: Length,
    /// Since 0900 (presumably local time)
    pub past: Length,
}

enum_with_str_repr! {
    /// [Color states](https://en.wikipedia.org/wiki/Colour_state) quickly provide information about visibility and cloud height
    ///
    /// Often used by military fields.
    ColorState {
        BluePlus => "BLU+",
        Blue => "BLU",
        White => "WHT",
        Green => "GRN",
        YellowOne => "YLO1" | "YLO",
        YellowTwo => "YLO2",
        Amber => "AMB",
        Red => "RED",
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Visibility {
    pub prevailing: Length,
    pub minimum_directional: Option<DirectionalVisibility>,
    pub maximum_directional: Option<DirectionalVisibility>,
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct DirectionalVisibility {
    pub direction: CompassDirection,
    pub distance: Length,
}

enum_with_str_repr! {
    /// The eight [compass points](https://en.wikipedia.org/wiki/Points_of_the_compass#8-wind_compass_rose)
    CompassDirection {
        NorthEast => "NE",
        NorthWest => "NW",
        North => "N",
        SouthEast => "SE",
        SouthWest => "SW",
        South => "S",
        East => "E",
        West => "W",
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct SeaConditions {
    /// Seawater temperature
    pub temperature: Option<TemperatureInterval>,
    /// On a unit-less scale of 0 = lowest to 9 = highest
    pub wave_intensity: Option<u8>,
}

#[derive(Clone, PartialEq, Debug)]
pub enum Trend {
    /// No significant change in weather expected for the next 2 hours
    NoSignificantChange,
    Becoming(TrendReport),
    Temporarily(TrendReport),
}

#[derive(Clone, PartialEq, Debug)]
pub struct TrendReport {
    pub time: Option<TrendTime>,
    pub wind: Option<Wind>,
    pub visibility: Option<Visibility>,
    pub weather: Vec<Weather>,
    pub cloud_cover: Vec<CloudCover>,
    pub color_state: Option<ColorState>,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct TrendTime {
    pub time_type: TrendTimeType,
    pub time: u16,
}

enum_with_str_repr! {
    TrendTimeType {
        At => "AT",
        From => "FM",
        Until => "TL",
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct MetarReport<'input> {
    pub identifier: &'input str,
    pub observation_time: ZuluDateTime,
    pub observation_type: Option<ObservationType>,
    pub wind: Option<Wind>,
    pub visibility: Option<Visibility>,
    pub runway_visibility: Vec<RunwayVisibility<'input>>,
    pub runway_reports: Vec<RunwayReport<'input>>,
    pub weather: Vec<Weather>,
    pub cloud_cover: Vec<CloudCover>,
    /// Indicative of OK ceiling and visibility
    ///
    /// While in the international standard, some countries do not use this. Notably, Canada
    pub cavok: bool,
    pub temperatures: Option<Temperatures>,
    pub pressure: Option<Pressure>,
    pub accumulated_rainfall: Option<AccumulatedRainfall>,
    /// `BLACK` in a METAR report indicates the airfield is closed
    pub is_closed: bool,
    pub color_state: Option<ColorState>,
    /// Some stations will report the next color state
    pub next_color_state: Option<ColorState>,
    pub recent_weather: Vec<Weather>,
    pub sea_conditions: Option<SeaConditions>,
    pub trends: Vec<Trend>,
    pub remark: Option<&'input str>,
    /// Some automated METAR reports indicate if the system needs maintenance
    ///
    /// This may indicate unreliable measurements.
    pub maintenance_needed: bool,
}