grib 0.15.4

GRIB format parser & writer for Rust
Documentation
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
use std::slice::Iter;

use crate::{
    codetables::SUPPORTED_PROD_DEF_TEMPLATE_NUMBERS,
    datatypes::*,
    error::*,
    helpers::{GribInt, read_as},
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Indicator {
    /// Discipline - GRIB Master Table Number (see Code Table 0.0)
    pub discipline: u8,
    /// Total length of GRIB message in octets (including Section 0)
    pub total_length: u64,
}

impl Indicator {
    pub(crate) fn from_slice(slice: &[u8]) -> Result<Self, ParseError> {
        let discipline = slice[6];
        let version = slice[7];
        if version != 2 {
            return Err(ParseError::GRIBVersionMismatch(version));
        }

        let total_length = read_as!(u64, slice, 8);

        Ok(Self {
            discipline,
            total_length,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Identification {
    pub(crate) payload: Box<[u8]>,
}

impl Identification {
    pub fn from_payload(slice: Box<[u8]>) -> Result<Self, BuildError> {
        let size = slice.len();
        if size < 16 {
            Err(BuildError::SectionSizeTooSmall(size))
        } else {
            Ok(Self { payload: slice })
        }
    }

    pub fn iter(&self) -> Iter<'_, u8> {
        self.payload.iter()
    }

    /// Identification of originating/generating centre (see Common Code Table
    /// C-1)
    #[inline]
    pub fn centre_id(&self) -> u16 {
        let payload = &self.payload;
        read_as!(u16, payload, 0)
    }

    /// Identification of originating/generating sub-centre (allocated by
    /// originating/ generating centre)
    #[inline]
    pub fn subcentre_id(&self) -> u16 {
        let payload = &self.payload;
        read_as!(u16, payload, 2)
    }

    /// GRIB Master Tables Version Number (see Code Table 1.0)
    #[inline]
    pub fn master_table_version(&self) -> u8 {
        self.payload[4]
    }

    /// GRIB Local Tables Version Number (see Code Table 1.1)
    #[inline]
    pub fn local_table_version(&self) -> u8 {
        self.payload[5]
    }

    /// Significance of Reference Time (see Code Table 1.2)
    #[inline]
    pub fn ref_time_significance(&self) -> u8 {
        self.payload[6]
    }

    /// Unchecked reference time of the data.
    ///
    /// This method returns unchecked data, so for example, if the data contains
    /// a "date and time" such as "2000-13-32 25:61:62", it will be returned as
    /// is.
    pub fn ref_time_unchecked(&self) -> crate::def::grib2::RefTime {
        let payload = &self.payload;
        crate::def::grib2::RefTime::new(
            read_as!(u16, payload, 7),
            payload[9],
            payload[10],
            payload[11],
            payload[12],
            payload[13],
        )
    }

    /// Production status of processed data in this GRIB message
    /// (see Code Table 1.3)
    #[inline]
    pub fn prod_status(&self) -> u8 {
        self.payload[14]
    }

    /// Type of processed data in this GRIB message (see Code Table 1.4)
    #[inline]
    pub fn data_type(&self) -> u8 {
        self.payload[15]
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocalUse {
    payload: Box<[u8]>,
}

impl LocalUse {
    pub fn from_payload(slice: Box<[u8]>) -> Self {
        Self { payload: slice }
    }

    pub fn iter(&self) -> Iter<'_, u8> {
        self.payload.iter()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GridDefinition {
    pub(crate) payload: Box<[u8]>,
}

impl GridDefinition {
    pub fn from_payload(slice: Box<[u8]>) -> Result<Self, BuildError> {
        let size = slice.len();
        if size < 9 {
            Err(BuildError::SectionSizeTooSmall(size))
        } else {
            Ok(Self { payload: slice })
        }
    }

    pub fn iter(&self) -> Iter<'_, u8> {
        self.payload.iter()
    }

    /// Number of data points
    pub fn num_points(&self) -> u32 {
        let payload = &self.payload;
        read_as!(u32, payload, 1)
    }

    /// Grid Definition Template Number
    pub fn grid_tmpl_num(&self) -> u16 {
        let payload = &self.payload;
        read_as!(u16, payload, 7)
    }
}

const START_OF_PROD_TEMPLATE: usize = 4;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ProdDefinition {
    pub(crate) payload: Box<[u8]>,
}

impl ProdDefinition {
    pub fn from_payload(slice: Box<[u8]>) -> Result<Self, BuildError> {
        let size = slice.len();
        if size < START_OF_PROD_TEMPLATE {
            Err(BuildError::SectionSizeTooSmall(size))
        } else {
            Ok(Self { payload: slice })
        }
    }

    pub fn iter(&self) -> Iter<'_, u8> {
        self.payload.iter()
    }

    /// Number of coordinate values after Template
    pub fn num_coordinates(&self) -> u16 {
        let payload = &self.payload;
        read_as!(u16, payload, 0)
    }

    /// Product Definition Template Number
    pub fn prod_tmpl_num(&self) -> u16 {
        let payload = &self.payload;
        read_as!(u16, payload, 2)
    }

    // pub(crate) templated(&self)-> Box<[u8]> {

    // }

    pub(crate) fn template_supported(&self) -> bool {
        SUPPORTED_PROD_DEF_TEMPLATE_NUMBERS.contains(&self.prod_tmpl_num())
    }

    /// Use [CodeTable4_1](crate::codetables::CodeTable4_1) to get textual
    /// representation of the returned numerical value.
    pub fn parameter_category(&self) -> Option<u8> {
        if self.template_supported() {
            self.payload.get(START_OF_PROD_TEMPLATE).copied()
        } else {
            None
        }
    }

    /// Use [CodeTable4_2](crate::codetables::CodeTable4_2) to get textual
    /// representation of the returned numerical value.
    pub fn parameter_number(&self) -> Option<u8> {
        if self.template_supported() {
            self.payload.get(START_OF_PROD_TEMPLATE + 1).copied()
        } else {
            None
        }
    }

    /// Use [CodeTable4_3](crate::codetables::CodeTable4_3) to get textual
    /// representation of the returned numerical value.
    pub fn generating_process(&self) -> Option<u8> {
        if self.template_supported() {
            let index = match self.prod_tmpl_num() {
                0..=39 => Some(2),
                40..=43 => Some(4),
                44..=46 => Some(15),
                47 => Some(2),
                48..=49 => Some(26),
                51 => Some(2),
                // 53 and 54 is variable and not supported as of now
                55..=56 => Some(8),
                // 57 and 58 is variable and not supported as of now
                59 => Some(8),
                60..=61 => Some(2),
                62..=63 => Some(8),
                // 67 and 68 is variable and not supported as of now
                70..=73 => Some(7),
                76..=79 => Some(5),
                80..=81 => Some(27),
                82 => Some(16),
                83 => Some(2),
                84 => Some(16),
                85 => Some(15),
                86..=91 => Some(2),
                254 => Some(2),
                1000..=1101 => Some(2),
                _ => None,
            }?;
            self.payload.get(START_OF_PROD_TEMPLATE + index).copied()
        } else {
            None
        }
    }

    /// Returns the unit and value of the forecast time wrapped by `Option`.
    /// Use [CodeTable4_4](crate::codetables::CodeTable4_4) to get textual
    /// representation of the unit.
    pub fn forecast_time(&self) -> Option<ForecastTime> {
        if self.template_supported() {
            let unit_index = match self.prod_tmpl_num() {
                0..=15 => Some(8),
                32..=34 => Some(8),
                40..=43 => Some(10),
                44..=47 => Some(21),
                48..=49 => Some(32),
                51 => Some(8),
                // 53 and 54 is variable and not supported as of now
                55..=56 => Some(14),
                // 57 and 58 is variable and not supported as of now
                59 => Some(14),
                60..=61 => Some(8),
                62..=63 => Some(14),
                // 67 and 68 is variable and not supported as of now
                70..=73 => Some(13),
                76..=79 => Some(11),
                80..=81 => Some(33),
                82..=84 => Some(22),
                85 => Some(21),
                86..=87 => Some(8),
                88 => Some(26),
                91 => Some(8),
                1000..=1101 => Some(8),
                _ => None,
            }?;
            let unit_index = START_OF_PROD_TEMPLATE + unit_index;
            let unit = self.payload.get(unit_index).copied();
            let start = unit_index + 1;
            let end = unit_index + 5;
            let time = u32::from_be_bytes(self.payload[start..end].try_into().unwrap());
            unit.map(|v| ForecastTime::from_numbers(v, time))
        } else {
            None
        }
    }

    /// Returns a tuple of two [FixedSurface], wrapped by `Option`.
    pub fn fixed_surfaces(&self) -> Option<(FixedSurface, FixedSurface)> {
        if self.template_supported() {
            let index = match self.prod_tmpl_num() {
                0..=15 => Some(13),
                40..=43 => Some(15),
                44 => Some(24),
                45..=47 => Some(26),
                48..=49 => Some(37),
                51 => Some(13),
                // 53 and 54 is variable and not supported as of now
                55..=56 => Some(19),
                // 57 and 58 is variable and not supported as of now
                59 => Some(19),
                60..=61 => Some(13),
                62..=63 => Some(19),
                // 67 and 68 is variable and not supported as of now
                70..=73 => Some(18),
                76..=79 => Some(16),
                80..=81 => Some(38),
                82..=84 => Some(27),
                85 => Some(26),
                86..=87 => Some(13),
                88 => Some(5),
                91 => Some(13),
                1100..=1101 => Some(13),
                _ => None,
            }?;

            let first_surface = self.read_surface_from(index);
            let second_surface = self.read_surface_from(index + 6);
            first_surface.zip(second_surface)
        } else {
            None
        }
    }

    fn read_surface_from(&self, index: usize) -> Option<FixedSurface> {
        let index = START_OF_PROD_TEMPLATE + index;
        let surface_type = self.payload.get(index).copied();
        let scale_factor = self.payload.get(index + 1).map(|v| (*v).as_grib_int());
        let start = index + 2;
        let end = index + 6;
        let scaled_value =
            u32::from_be_bytes(self.payload[start..end].try_into().unwrap()).as_grib_int();
        surface_type
            .zip(scale_factor)
            .map(|(stype, factor)| FixedSurface::new(stype, factor, scaled_value))
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ReprDefinition {
    pub(crate) payload: Box<[u8]>,
}

impl ReprDefinition {
    pub fn from_payload(slice: Box<[u8]>) -> Result<Self, BuildError> {
        let size = slice.len();
        if size < 6 {
            Err(BuildError::SectionSizeTooSmall(size))
        } else {
            Ok(Self { payload: slice })
        }
    }

    pub fn iter(&self) -> Iter<'_, u8> {
        self.payload.iter()
    }

    /// Number of data points where one or more values are
    /// specified in Section 7 when a bit map is present, total
    /// number of data points when a bit map is absent
    pub fn num_points(&self) -> u32 {
        let payload = &self.payload;
        read_as!(u32, payload, 0)
    }

    /// Data Representation Template Number
    pub fn repr_tmpl_num(&self) -> u16 {
        let payload = &self.payload;
        read_as!(u16, payload, 4)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BitMap {
    /// Bit-map indicator
    pub bitmap_indicator: u8,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn prod_definition_parameters() {
        let data = ProdDefinition::from_payload(
            vec![
                0, 0, 0, 0, 193, 0, 2, 153, 255, 0, 0, 0, 0, 0, 0, 0, 40, 1, 255, 255, 255, 255,
                255, 255, 255, 255, 255, 255, 255,
            ]
            .into_boxed_slice(),
        )
        .unwrap();

        assert_eq!(data.parameter_category(), Some(193));
        assert_eq!(data.parameter_number(), Some(0));
        assert_eq!(
            data.forecast_time(),
            Some(ForecastTime::from_numbers(0, 40))
        );
        assert_eq!(
            data.fixed_surfaces(),
            Some((
                FixedSurface::new(1, -127, -2147483647),
                FixedSurface::new(255, -127, -2147483647)
            ))
        );
    }
}