gribberish 0.30.3

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
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
use crate::error::GribberishError;
use crate::grib1::Grib1Message;
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, ProbabilityType, 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(data: &[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> {
        // Scan forward to find the next GRIB magic header
        while self.offset + 4 <= self.data.len() {
            if &self.data[self.offset..self.offset + 4] == b"GRIB" {
                // Found a potential GRIB message
                match Message::from_data(self.data, self.offset) {
                    Some(m) => {
                        self.offset += m.len();
                        return Some(m);
                    }
                    None => {
                        // Failed to parse, skip past this "GRIB" and keep scanning
                        self.offset += 1;
                    }
                }
            } else {
                self.offset += 1;
            }
        }
        None
    }
}

pub enum Message<'a> {
    Grib1 {
        data: &'a [u8],
        offset: usize,
        message: Grib1Message,
    },
    Grib2 {
        data: &'a [u8],
        offset: usize,
    },
}

impl<'a> Message<'a> {
    pub fn from_data(data: &'a [u8], offset: usize) -> Option<Message<'a>> {
        // Check if there's enough data for GRIB header
        if offset + 8 > data.len() {
            return None;
        }

        // Check for GRIB magic
        if &data[offset..offset + 4] != b"GRIB" {
            return None;
        }

        // Edition is at byte 7 (0-indexed)
        let edition = data[offset + 7];

        match edition {
            1 => {
                // Parse as GRIB1
                match Grib1Message::from_data(data, offset) {
                    Ok(message) => Some(Message::Grib1 {
                        data,
                        offset,
                        message,
                    }),
                    Err(_) => None,
                }
            }
            2 => {
                // Parse as GRIB2 (existing logic)
                let mut sections = SectionIterator { data, offset };

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

    pub fn byte_data(&self) -> &'a [u8] {
        match self {
            Message::Grib1 { data, .. } => data,
            Message::Grib2 { data, .. } => data,
        }
    }

    pub fn byte_offset(&self) -> usize {
        match self {
            Message::Grib1 { offset, .. } => *offset,
            Message::Grib2 { offset, .. } => *offset,
        }
    }

    pub fn sections(&self) -> SectionIterator<'_> {
        match self {
            Message::Grib1 { data, offset, .. } => SectionIterator {
                data,
                offset: *offset,
            },
            Message::Grib2 { data, offset } => SectionIterator {
                data,
                offset: *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 derived_forecast_type = if let Some(dft) = self.derived_forecast_type()? {
            dft.abbv()
        } else {
            "".into()
        };
        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
            )
        };

        // Include perturbation number for ensemble members
        let perturbation = self
            .perturbation_number()
            .unwrap_or(None)
            .map_or("".to_string(), |p| format!(":ens{p}"));

        let percentile = self
            .percentile_value()
            .unwrap_or(None)
            .map_or("".to_string(), |p| format!(":pctl{p}"));

        let probability = match self.probability_type().unwrap_or(None) {
            Some(pt) => {
                let lower = self
                    .probability_lower_limit()
                    .unwrap_or(None)
                    .map_or("".to_string(), |v| format!("_{v:.0}"));
                let upper = self
                    .probability_upper_limit()
                    .unwrap_or(None)
                    .map_or("".to_string(), |v| format!("_{v:.0}"));
                format!(":probt{}{lower}{upper}", pt as u8)
            }
            None => "".to_string(),
        };

        let anomaly = if self.is_anomaly().unwrap_or(false) {
            ":anom"
        } else {
            ""
        };

        Ok(format!(
            "{var}{time}{first_level}{second_level}{perturbation}{percentile}{probability}{anomaly}:{statistical_process}{generating_process}{derived_forecast_type}"
        ))
    }

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

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

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

    pub fn parameters(messages: Vec<Message>) -> Vec<Option<Parameter>> {
        messages
            .iter()
            .map(|m| m.parameter())
            .map(|r| r.ok())
            .collect()
    }

    pub fn forecast_dates(messages: Vec<Message>) -> Vec<Option<DateTime<Utc>>> {
        messages
            .iter()
            .map(|m| m.forecast_date())
            .map(|r| r.ok())
            .collect()
    }

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

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

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

    pub fn discipline(&self) -> Result<Discipline, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(Discipline::Meteorological),
            Message::Grib2 { .. } => 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> {
        match self {
            Message::Grib1 { message, .. } => {
                // For GRIB1, get the grid from the message
                let grid = message.grid().ok_or_else(|| {
                    GribberishError::MessageError(
                        "GRIB1 message missing grid description section".to_string(),
                    )
                })?;
                Ok(Box::new(grid.clone()) as Box<dyn GridDefinitionTemplate>)
            }
            Message::Grib2 { .. } => {
                // For GRIB2, use the existing section-based approach
                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()
            ))
        );

        Ok(parameter)
    }

    pub fn category(&self) -> Result<String, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok("unknown".to_string()),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.category().to_owned())
            }
        }
    }

    pub fn discipline_value(&self) -> Result<u8, GribberishError> {
        let discipline = self.discipline()?;
        Ok(discipline as u8)
    }

    pub fn category_value(&self) -> Result<u8, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(255),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.category_value())
            }
        }
    }

    pub fn parameter_value(&self) -> Result<u8, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(255),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.parameter_value())
            }
        }
    }

    pub fn variable_name(&self) -> Result<String, GribberishError> {
        match self {
            Message::Grib1 { message, .. } => {
                let (_, name, _) = message.parameter().ok_or_else(|| {
                    GribberishError::MessageError("Parameter not found".to_string())
                })?;
                Ok(name)
            }
            Message::Grib2 { .. } => {
                let parameter = self.parameter()?;
                Ok(parameter.name)
            }
        }
    }

    pub fn variable_abbrev(&self) -> Result<String, GribberishError> {
        match self {
            Message::Grib1 { message, .. } => {
                let (abbrev, _, _) = message.parameter().ok_or_else(|| {
                    GribberishError::MessageError("Parameter not found".to_string())
                })?;
                Ok(abbrev)
            }
            Message::Grib2 { .. } => {
                let parameter = self.parameter()?;
                Ok(parameter.abbrev)
            }
        }
    }

    pub fn unit(&self) -> Result<String, GribberishError> {
        match self {
            Message::Grib1 { message, .. } => {
                let (_, _, unit) = message.parameter().ok_or_else(|| {
                    GribberishError::MessageError("Parameter not found".to_string())
                })?;
                Ok(unit)
            }
            Message::Grib2 { .. } => {
                let parameter = self.parameter()?;
                Ok(parameter.unit)
            }
        }
    }

    pub fn reference_date(&self) -> Result<DateTime<Utc>, GribberishError> {
        match self {
            Message::Grib1 { message, .. } => message
                .reference_datetime()
                .map_err(GribberishError::MessageError),
            Message::Grib2 { .. } => {
                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> {
        match self {
            Message::Grib1 { .. } => Ok(GeneratingProcess::Forecast),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.generating_process())
            }
        }
    }

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

    pub fn is_anomaly(&self) -> Result<bool, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(false),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.is_anomaly())
            }
        }
    }

    pub fn perturbation_number(&self) -> Result<Option<u8>, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(None),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.perturbation_number())
            }
        }
    }

    pub fn number_of_ensemble_members(&self) -> Result<Option<u8>, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(None),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.number_of_ensemble_members())
            }
        }
    }

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

    pub fn percentile_value(&self) -> Result<Option<u8>, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(None),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.percentile_value())
            }
        }
    }

    pub fn probability_type(&self) -> Result<Option<ProbabilityType>, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(None),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.probability_type())
            }
        }
    }

    pub fn forecast_probability_number(&self) -> Result<Option<u8>, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(None),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.forecast_probability_number())
            }
        }
    }

    pub fn probability_lower_limit(&self) -> Result<Option<f64>, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(None),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.probability_lower_limit())
            }
        }
    }

    pub fn probability_upper_limit(&self) -> Result<Option<f64>, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(None),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.probability_upper_limit())
            }
        }
    }

    pub fn forecast_date(&self) -> Result<DateTime<Utc>, GribberishError> {
        match self {
            Message::Grib1 { message, .. } => message
                .forecast_datetime()
                .map_err(GribberishError::MessageError),
            Message::Grib2 { .. } => {
                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> {
        match self {
            Message::Grib1 { .. } => {
                // GRIB1 messages don't have a separate forecast end date
                // They only have a single forecast time
                Ok(None)
            }
            Message::Grib2 { .. } => {
                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> {
        match self {
            Message::Grib1 { .. } => Ok(TimeUnit::Hour),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.time_unit())
            }
        }
    }

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

    pub fn time_interval(&self) -> Result<u32, GribberishError> {
        match self {
            Message::Grib1 { .. } => Ok(0),
            Message::Grib2 { .. } => {
                let product_template = self.product_template()?;
                Ok(product_template.time_interval())
            }
        }
    }

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

    pub fn first_fixed_surface(&self) -> Result<(FixedSurfaceType, Option<f64>), GribberishError> {
        match self {
            Message::Grib1 { message, .. } => {
                let (level_type_name, level_value, _) = message.level_info();

                // Map GRIB1 level types to GRIB2 FixedSurfaceType
                let fixed_surface_type = match level_type_name.as_str() {
                    "surface" => FixedSurfaceType::GroundOrWater,
                    "isobaric" => FixedSurfaceType::IsobaricSurface,
                    "fixed_height" => FixedSurfaceType::SpecificAltitudeAboveMeanSeaLevel,
                    "fixed_height_above_ground" => {
                        FixedSurfaceType::SpecifiedHeightLevelAboveGround
                    }
                    "mean_sea_level" => FixedSurfaceType::MeanSeaLevel,
                    "sigma" => FixedSurfaceType::SigmaLevel,
                    "hybrid" => FixedSurfaceType::HybridLevel,
                    "depth_below_surface" => FixedSurfaceType::DepthBelowLandSurface,
                    "layer_between_depths" => FixedSurfaceType::DepthBelowLandSurface,
                    "isotherm_zero" => FixedSurfaceType::ZeroDegreeIsotherm,
                    "cloud_base" => FixedSurfaceType::CloudBase,
                    "cloud_top" => FixedSurfaceType::CloudTop,
                    "entire_atmosphere" => FixedSurfaceType::EntireAtmosphere,
                    _ => FixedSurfaceType::Missing,
                };

                let first_fixed_surface_value = if level_value > 0.0 {
                    Some(level_value)
                } else {
                    None
                };

                Ok((fixed_surface_type, first_fixed_surface_value))
            }
            Message::Grib2 { .. } => {
                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> {
        match self {
            Message::Grib1 { .. } => Ok((FixedSurfaceType::Missing, None)),
            Message::Grib2 { .. } => {
                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> {
        match self {
            Message::Grib1 { message, .. } => {
                let (ni, nj) = message.grid_shape();
                Ok((nj, ni)) // Note: GRIB1 returns (ni, nj) but we need (y, x)
            }
            Message::Grib2 { .. } => {
                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> {
        match self {
            Message::Grib1 { message, .. } => message.data().map_err(GribberishError::MessageError),
            Message::Grib2 { .. } => {
                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() {
                    bitmap_section.map_data(scaled_unpacked_data)
                } else {
                    scaled_unpacked_data
                };

                let count = unwrap_or_return!(
                    self.sections().find_map(|s| match s {
                        Section::GridDefinition(grid_definition) => {
                            Some(grid_definition.data_point_count())
                        }
                        _ => None,
                    }),
                    GribberishError::MessageError(
                        "Grid definition section not found when reading message data".into()
                    )
                );
                data.resize(count, 0.0);
                Ok(data)
            }
        }
    }
}