gribberish 0.22.0

Parse grib 2 files with 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
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
use crate::error::GribberishError;
use crate::sections::{indicator::Discipline, section::Section, section::SectionIterator};
use crate::templates::grid_definition::GridDefinitionTemplate;
use crate::templates::product::product_template::ProductTemplate;
use crate::templates::product::tables::{
    DerivedForecastType, FixedSurfaceType, GeneratingProcess, TimeUnit, TypeOfStatisticalProcessing,
};
use crate::utils::iter::projection::LatLngProjection;
use bitvec::view::BitView;
use chrono::{DateTime, Utc};
use gribberish_types::Parameter;
use std::collections::HashMap;
use std::vec::Vec;

pub fn scan_messages<'a>(data: &'a [u8]) -> HashMap<String, (usize, usize)> {
    let message_iter = MessageIterator::from_data(data, 0);

    message_iter
        .enumerate()
        .map(|(index, m)| match m.key() {
            Ok(var) => (var, (index, m.byte_offset())),
            Err(_) => ("unknown".into(), (index, m.byte_offset())),
        })
        .collect()
}

pub fn read_messages<'a>(data: &'a [u8]) -> MessageIterator<'a> {
    MessageIterator { data, offset: 0 }
}

pub fn read_message<'a>(data: &'a [u8], offset: usize) -> Option<Message<'a>> {
    Message::from_data(data, offset)
}

pub struct MessageIterator<'a> {
    data: &'a [u8],
    offset: usize,
}

impl<'a> MessageIterator<'a> {
    pub fn from_data(data: &'a [u8], offset: usize) -> Self {
        MessageIterator { data, offset }
    }

    pub fn current_offset(&self) -> usize {
        self.offset
    }
}

impl<'a> Iterator for MessageIterator<'a> {
    type Item = Message<'a>;

    fn next(&mut self) -> std::option::Option<<Self as std::iter::Iterator>::Item> {
        if self.offset >= self.data.len() {
            return None;
        }

        match Message::from_data(&self.data, self.offset) {
            Some(m) => {
                self.offset += m.len();
                Some(m)
            }
            None => None,
        }
    }
}

pub struct Message<'a> {
    data: &'a [u8],
    offset: usize,
}

impl<'a> Message<'a> {
    pub fn from_data(data: &'a [u8], offset: usize) -> Option<Message> {
        let mut sections = SectionIterator { data: data, offset };

        match sections.next() {
            Some(Section::Indicator(_)) => Some(Message {
                data: &data,
                offset: offset,
            }),
            _ => None,
        }
    }

    pub fn byte_data(&self) -> &'a [u8] {
        self.data
    }

    pub fn byte_offset(&self) -> usize {
        self.offset
    }

    pub fn sections(&self) -> SectionIterator {
        SectionIterator {
            data: self.data,
            offset: self.offset,
        }
    }

    pub fn key(&self) -> Result<String, GribberishError> {
        let time = self
            .forecast_date()
            .map(|t| format!(":{}", t.format("%Y%m%d%H%M")))
            .unwrap_or("".into())
            .to_string();
        let var = self.variable_abbrev()?;
        let generating_process = self.generating_process()?.to_string();
        let statistical_process = self
            .statistical_process_type()
            .unwrap_or(None)
            .map_or("".to_string(), |s| format!("{s} "));
        let first_fixed_surface = self.first_fixed_surface()?;
        let first_level = if first_fixed_surface.0 == FixedSurfaceType::Missing {
            "".into()
        } else {
            let level_value = if let Some(value) = first_fixed_surface.1 {
                format!("{:.0}", value)
            } else {
                "".into()
            };

            format!(
                ":{level_value} in {}",
                Parameter::from(first_fixed_surface.0).name
            )
        };

        let second_fixed_surface = self.second_fixed_surface()?;
        let second_level = if second_fixed_surface.0 == FixedSurfaceType::Missing {
            "".into()
        } else {
            let level_value = if let Some(value) = second_fixed_surface.1 {
                format!("{:.0}", value)
            } else {
                "".into()
            };

            format!(
                ":{level_value} in {}",
                Parameter::from(second_fixed_surface.0).name
            )
        };

        Ok(format!(
            "{var}{time}{first_level}{second_level}:{statistical_process}{generating_process}"
        ))
    }

    pub fn variable_names(messages: Vec<Message>) -> Vec<Option<String>> {
        Message::parameters(messages)
            .iter()
            .map(|p| match p {
                Some(p) => Some(p.name.clone()),
                None => None,
            })
            .collect()
    }

    pub fn variable_abbrevs(messages: Vec<Message>) -> Vec<Option<String>> {
        Message::parameters(messages)
            .iter()
            .map(|p| match p {
                Some(p) => Some(p.abbrev.clone()),
                None => None,
            })
            .collect()
    }

    pub fn units(messages: Vec<Message>) -> Vec<Option<String>> {
        Message::parameters(messages)
            .iter()
            .map(|p| match p {
                Some(p) => Some(p.unit.clone()),
                None => None,
            })
            .collect()
    }

    pub fn parameters(messages: Vec<Message>) -> Vec<Option<Parameter>> {
        messages
            .iter()
            .map(|m| m.parameter())
            .map(|r| match r {
                Ok(parameter) => Some(parameter),
                Err(_) => None,
            })
            .collect()
    }

    pub fn forecast_dates(messages: Vec<Message>) -> Vec<Option<DateTime<Utc>>> {
        messages
            .iter()
            .map(|m| m.forecast_date())
            .map(|r| match r {
                Ok(date) => Some(date),
                Err(_) => None,
            })
            .collect()
    }

    pub fn len(&self) -> usize {
        match self.sections().next() {
            Some(Section::Indicator(i)) => i.total_length() as usize,
            Some(_) => 0,
            None => 0,
        }
    }

    pub fn section_count(&self) -> usize {
        self.sections().count()
    }

    pub fn discipline(&self) -> Result<Discipline, GribberishError> {
        match self.sections().next().unwrap() {
            Section::Indicator(indicator) => Ok(indicator.discipline()),
            _ => Err(GribberishError::MessageError(
                "Indicator section not found when reading discipline".into(),
            )),
        }
        .clone()
    }

    pub fn product_template_id(&self) -> Result<u16, GribberishError> {
        let mut sections = self.sections();

        let product_definition = unwrap_or_return!(
            sections.find_map(|s| match s {
                Section::ProductDefinition(product_definition) => Some(product_definition),
                _ => None,
            }),
            GribberishError::MessageError(
                "Product definition section not found when reading variable data".into()
            )
        );

        Ok(product_definition.product_definition_template_number())
    }

    pub fn product_template(&self) -> Result<Box<dyn ProductTemplate>, GribberishError> {
        let mut sections = self.sections();

        let discipline = unwrap_or_return!(
            sections.find_map(|s| match s {
                Section::Indicator(indicator) => Some(indicator.discipline_value()),
                _ => None,
            }),
            GribberishError::MessageError(
                "Failed to read discipline value from indicator section".into()
            )
        );

        let product_definition = unwrap_or_return!(
            sections.find_map(|s| match s {
                Section::ProductDefinition(product_definition) => Some(product_definition),
                _ => None,
            }),
            GribberishError::MessageError(
                "Product definition section not found when reading variable data".into()
            )
        );

        let product_template = unwrap_or_return!(
            product_definition.product_definition_template(discipline),
            GribberishError::MessageError(
                "Only HorizontalAnalysisForecast templates are supported at this time".into()
            )
        );

        Ok(product_template)
    }

    pub fn grid_template(&self) -> Result<Box<dyn GridDefinitionTemplate>, GribberishError> {
        let grid_definition = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::GridDefinition(grid_definition) => Some(grid_definition),
                _ => None,
            }),
            GribberishError::MessageError("Grid definition section not found when reading variable data".into())
        );

        let grid_template = unwrap_or_return!(
            grid_definition.grid_definition_template(),
            GribberishError::MessageError("Only latitude longitude templates supported at this time".into())
        );

        Ok(grid_template)
    }

    pub fn parameter_index(&self) -> Result<String, GribberishError> {
        let discipline = self.discipline()?;
        let product_template = self.product_template()?;
        let category = product_template.category_value();
        let parameter = product_template.parameter_value();

        Ok(format!("({}, {}, {})", discipline, category, parameter))
    }

    pub fn parameter(&self) -> Result<Parameter, GribberishError> {
        let product_template = self.product_template()?;

        let parameter = unwrap_or_return!(
            product_template.parameter(),
            GribberishError::MessageError(
                format!(
                    "This Product and Parameter is currently not supported: ({}, {})",
                    product_template.category_value(),
                    product_template.parameter_value()
                )
                .into()
            )
        );

        Ok(parameter)
    }

    pub fn category(&self) -> Result<String, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.category().to_owned())
    }

    pub fn variable_name(&self) -> Result<String, GribberishError> {
        let parameter = self.parameter()?;
        Ok(parameter.name)
    }

    pub fn variable_abbrev(&self) -> Result<String, GribberishError> {
        let parameter = self.parameter()?;
        Ok(parameter.abbrev)
    }

    pub fn unit(&self) -> Result<String, GribberishError> {
        let parameter = self.parameter()?;
        Ok(parameter.unit)
    }

    pub fn reference_date(&self) -> Result<DateTime<Utc>, GribberishError> {
        let reference_date = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::Identification(identification) => Some(identification.reference_date()),
                _ => None,
            }),
            GribberishError::MessageError(
                "Identification section not found when reading reference date".into()
            )
        );
        Ok(reference_date)
    }

    pub fn generating_process(&self) -> Result<GeneratingProcess, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.generating_process())
    }

    pub fn derived_forecast_type(&self) -> Result<Option<DerivedForecastType>, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.derived_forecast_type())
    }

    pub fn statistical_process_type(
        &self,
    ) -> Result<Option<TypeOfStatisticalProcessing>, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.statistical_process_type())
    }

    pub fn forecast_date(&self) -> Result<DateTime<Utc>, GribberishError> {
        let product_template = self.product_template()?;
        let reference_date = self.reference_date()?;
        Ok(product_template.forecast_datetime(reference_date))
    }

    pub fn forecast_end_date(&self) -> Result<Option<DateTime<Utc>>, GribberishError> {
        let product_template = self.product_template()?;
        let reference_date = self.reference_date()?;
        Ok(product_template.forecast_end_datetime(reference_date))
    }

    pub fn time_unit(&self) -> Result<TimeUnit, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.time_unit())
    }

    pub fn time_increment_unit(&self) -> Result<Option<TimeUnit>, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.time_increment_unit())
    }

    pub fn time_interval(&self) -> Result<u32, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.time_interval())
    }

    pub fn time_increment_interval(&self) -> Result<Option<u32>, GribberishError> {
        let product_template = self.product_template()?;
        Ok(product_template.time_increment_interval())
    }

    pub fn first_fixed_surface(&self) -> Result<(FixedSurfaceType, Option<f64>), GribberishError> {
        let product_template = self.product_template()?;
        let surface_type = product_template.first_fixed_surface_type();
        let surface_value = product_template.first_fixed_surface_value();
        Ok((surface_type, surface_value))
    }

    pub fn second_fixed_surface(&self) -> Result<(FixedSurfaceType, Option<f64>), GribberishError> {
        let product_template = self.product_template()?;
        let surface_type = product_template.second_fixed_surface_type();
        let surface_value = product_template.second_fixed_surface_value();
        Ok((surface_type, surface_value))
    }

    pub fn proj_string(&self) -> Result<String, GribberishError> {
        let grid_template = self.grid_template()?;
        Ok(grid_template.proj_string())
    }

    pub fn grid_template_id(&self) -> Result<u16, GribberishError> {
        let grid_definition = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::GridDefinition(grid_definition) => Some(grid_definition),
                _ => None,
            }),
            GribberishError::MessageError(
                "Grid definition section not found when reading variable data".into()
            )
        );

        Ok(grid_definition.grid_definition_template_number())
    }

    pub fn is_regular_grid(&self) -> Result<bool, GribberishError> {
        let grid_template = self.grid_template()?;
        Ok(grid_template.is_regular_grid())
    }

    pub fn crs(&self) -> Result<String, GribberishError> {
        let grid_template = self.grid_template()?;
        Ok(grid_template.crs())
    }

    pub fn grid_dimensions(&self) -> Result<(usize, usize), GribberishError> {
        let grid_template = self.grid_template()?;
        Ok((grid_template.y_count(), grid_template.x_count()))
    }

    pub fn latlng_projector(&self) -> Result<LatLngProjection, GribberishError> {
        let grid_template = self.grid_template()?;
        Ok(grid_template.projector())
    }

    pub fn data_template_number(&self) -> Result<u16, GribberishError> {
        let data_representation = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::DataRepresentation(data_representation) => Some(data_representation),
                _ => None,
            }),
            GribberishError::MessageError(
                "Product definition section not found when reading variable data".into()
            )
        );

        Ok(data_representation.data_representation_template_number())
    }

    pub fn data_compression_type(&self) -> Result<String, GribberishError> {
        let data_representation = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::DataRepresentation(data_representation) => Some(data_representation),
                _ => None,
            }),
            GribberishError::MessageError(
                "Product definition section not found when reading variable data".into()
            )
        );

        let data_representation_template = unwrap_or_return!(
            data_representation.data_representation_template(),
            GribberishError::MessageError(
                "Failed to unpack the data representation template".into()
            )
        );

        Ok(data_representation_template.compression_type())
    }

    pub fn data_point_count(&self) -> Result<usize, GribberishError> {
        let data_representation = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::DataRepresentation(data_representation) => Some(data_representation),
                _ => None,
            }),
            GribberishError::MessageError(
                "Product definition section not found when reading variable data".into()
            )
        );

        Ok(data_representation.data_point_count())
    }

    pub fn has_bitmap(&self) -> bool {
        let bitmap_section = self.sections().find_map(|s| match s {
            Section::Bitmap(bitmap_section) => Some(bitmap_section),
            _ => None,
        });

        match bitmap_section {
            Some(b) => b.has_bitmap(),
            None => false,
        }
    }

    pub fn data(&self) -> Result<Vec<f64>, GribberishError> {
        let data_section = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::Data(data_section) => Some(data_section),
                _ => None,
            }),
            GribberishError::MessageError(
                "Data section not found when reading message data".into()
            )
        );

        let raw_packed_data = data_section.raw_data_array().view_bits();

        let data_representation_section = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::DataRepresentation(data_representation_section) =>
                    Some(data_representation_section),
                _ => None,
            }),
            GribberishError::MessageError(
                "Data representation section not found when reading message data".into()
            )
        );

        let data_representation_template = unwrap_or_return!(
            data_representation_section.data_representation_template(),
            GribberishError::MessageError(
                "Failed to unpack the data representation template".into()
            )
        );

        let scaled_unpacked_data = data_representation_template.unpack(raw_packed_data)?;

        let bitmap_section = unwrap_or_return!(
            self.sections().find_map(|s| match s {
                Section::Bitmap(bitmap_section) => Some(bitmap_section),
                _ => None,
            }),
            GribberishError::MessageError(
                "Bitmap section not found when reading message data".into()
            )
        );

        let mut data = if bitmap_section.has_bitmap() {
            let mapped_scaled_data = bitmap_section.map_data(scaled_unpacked_data);
            mapped_scaled_data
        } else {
            scaled_unpacked_data
        };

        let shape = self.grid_dimensions()?;
        let count = shape.0 * shape.1;
        data.resize(count, 0.0);
        Ok(data)
    }
}