gistools/readers/grib2/sections/
mod.rs1pub mod _0;
3pub mod _1;
5pub mod _2;
7pub mod _3;
9pub mod _4;
11pub mod _5;
13pub mod _6;
15pub mod _7;
17pub mod _8;
19pub mod other;
21
22use crate::parsers::{BufferReader, Reader};
23pub use _0::*;
24pub use _1::*;
25pub use _2::*;
26pub use _3::*;
27pub use _4::*;
28pub use _5::*;
29pub use _6::*;
30pub use _7::*;
31pub use _8::*;
32pub use other::*;
33
34#[derive(Debug, Default, Clone, PartialEq)]
36pub struct Grib2Sections {
37 pub indicator: Option<Grib2IndicatorSection>,
39 pub identification: Option<Grib2IdentificationSection>,
41 pub local: Option<Grib2LocalUseSection>,
43 pub grid_definition: Option<GridDefinitionSection>,
45 pub product_definition: Option<Grib2ProductDefinitionSection>,
47 pub data_representation: Option<Grib2DataRepresentationSection>,
49 pub bit_map: Option<Grib2BitMapSection>,
51 pub data: Option<Grib2DataSection>,
53 pub end: Option<Grib2EndSection>,
55}
56
57pub fn split_section_chunks(grib_chunk: BufferReader) -> Grib2Sections {
65 let mut sections = Grib2Sections::default();
66
67 let mut current_section = grib_chunk;
68 while current_section.len() != 0 {
70 let section_number = get_section_number(¤t_section);
71
72 let mut length =
74 if section_number == 0 { 16 } else { current_section.uint32_be(Some(0)) } as u64;
75 length = length.min(current_section.len());
76 let section = BufferReader::new(current_section.slice(Some(0), Some(length)));
77 current_section = BufferReader::new(current_section.slice(Some(length), None));
78
79 parse_grib2_section(§ion, &mut sections);
80 }
81
82 sections
83}
84
85fn parse_grib2_section(reader: &BufferReader, sections: &mut Grib2Sections) {
91 let section_number = get_section_number(reader);
92
93 match section_number {
94 0 => sections.indicator = Some(Grib2IndicatorSection::new(reader)),
95 1 => sections.identification = Some(Grib2IdentificationSection::new(reader)),
96 2 => sections.local = Some(Grib2LocalUseSection::new(reader)),
97 3 => sections.grid_definition = Some(GridDefinitionSection::new(reader)),
98 4 => {
99 sections.product_definition = Some(Grib2ProductDefinitionSection::new(reader, sections))
100 }
101 5 => sections.data_representation = Some(Grib2DataRepresentationSection::new(reader)),
102 6 => sections.bit_map = Some(Grib2BitMapSection::new(reader)),
103 7 => sections.data = Some(Grib2DataSection::new(reader)),
104 8 => sections.end = Some(Grib2EndSection::new(reader)),
105 _ => panic!("Unknown section number: {section_number}"),
106 }
107}
108
109pub fn get_section_number(section: &BufferReader) -> u8 {
117 let first4_byte_string = section.parse_string(Some(0), Some(4));
118
119 match first4_byte_string.as_ref() {
120 "GRIB" => 0,
121 "7777" => 8,
122 _ => section.uint8(Some(4)),
123 }
124}