Skip to main content

grib/def/
grib2.rs

1//! Definitions of parameters contained in GRIB2 data.
2
3use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};
4
5#[derive(Debug, PartialEq, TryFromSlice, Dump)]
6pub struct Section<T>
7where
8    T: PartialEq + grib_template_helpers::TryFromSlice + grib_template_helpers::Dump,
9{
10    pub header: SectionHeader,
11    pub payload: T,
12}
13
14#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
15pub struct SectionHeader {
16    /// Length of section in octets (nn).
17    pub len: u32,
18    /// Number of section.
19    pub sect_num: u8,
20}
21
22/// Section 1 - Identification section.
23pub type Section1 = Section<Section1Payload>;
24
25#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
26pub struct Section1Payload {
27    /// Identification of originating/generating centre (see Common Code table
28    /// C-11).
29    pub centre_id: u16,
30    /// Identification of originating/generating subcentre (allocated by
31    /// originating/generating centre).
32    pub subcentre_id: u16,
33    /// GRIB master table version number (see Common Code table C-0 and Note 1).
34    pub master_table_version: u8,
35    /// Version number of GRIB Local tables used to augment Master tables (see
36    /// Code table 1.1 and Note 2).
37    pub local_table_version: u8,
38    /// Significance of reference time (see Code table 1.2).
39    pub ref_time_significance: u8,
40    /// Reference time of data.
41    pub ref_time: RefTime,
42    /// Production status of processed data in this GRIB message (see Code table
43    /// 1.3).
44    pub prod_status: u8,
45    /// Type of processed data in this GRIB message (see Code table 1.4).
46    pub data_type: u8,
47    pub optional: Option<Section1PayloadOptional>,
48}
49
50#[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
51pub struct RefTime {
52    /// Year (4 digits).
53    pub year: u16,
54    /// Month.
55    pub month: u8,
56    /// Day.
57    pub day: u8,
58    /// Hour.
59    pub hour: u8,
60    /// Minute.
61    pub minute: u8,
62    /// Second.
63    pub second: u8,
64}
65
66#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
67pub struct Section1PayloadOptional {
68    /// Identification template number (optional, see Code table 1.5).
69    pub template_num: u16,
70    /// Identification template (optional, see template 1.X, where X is the
71    /// identification template number given in octets 22-23).
72    #[grib_template(variant = "template_num")]
73    pub template: IdentificationTemplate,
74}
75
76#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
77#[repr(u16)]
78pub enum IdentificationTemplate {
79    _1_0(template1::Template1_0) = 0,
80    _1_1(template1::Template1_1) = 1,
81    _1_2(template1::Template1_2) = 2,
82}
83
84/// Section 3 - Grid definition section.
85pub type Section3 = Section<Section3Payload>;
86
87#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
88pub struct Section3Payload {
89    /// Source of grid definition (see Code table 3.0 and Note 1).
90    pub grid_def_source: u8,
91    /// Number of data points.
92    pub num_points: u32,
93    /// Number of octets for optional list of numbers (see Note 2).
94    pub num_point_list_octets: u8,
95    /// Interpretation of list of numbers (see Code table 3.11).
96    pub point_list_interpretation: u8,
97    /// Grid definition template number (= N) (see Code table 3.1).
98    pub template_num: u16,
99    /// Grid Definition Template (see Template 3.N, where N is the Grid
100    /// Definition Template Number given in octets 13-14).
101    #[grib_template(variant = "template_num")]
102    pub template: GridDefinitionTemplate,
103}
104
105#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
106#[repr(u16)]
107pub enum GridDefinitionTemplate {
108    _3_0(template3::Template3_0) = 0,
109    _3_1(template3::Template3_1) = 1,
110    _3_20(template3::Template3_20) = 20,
111    _3_30(template3::Template3_30) = 30,
112    _3_40(template3::Template3_40) = 40,
113    _3_101(template3::Template3_101) = 101,
114}
115
116/// Section 4 - Product definition section.
117pub type Section4 = Section<Section4Payload>;
118
119#[derive(Debug, PartialEq, TryFromSlice, Dump)]
120pub struct Section4Payload {
121    /// Number of coordinate values after template or number of information
122    /// according to 3D vertical coordinate GRIB2 message (see Notes 1 and 5).
123    pub num_coord_values: u16,
124    /// Product definition template number (see Code table 4.0).
125    pub template_num: u16,
126}
127
128/// Section 5 - Data representation section.
129pub type Section5 = Section<Section5Payload>;
130
131#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
132pub struct Section5Payload {
133    /// Number of data points where one or more values are specified in Section
134    /// 7 when a bit map is present, total number of data points when a bit map
135    /// is absent.
136    pub num_encoded_points: u32,
137    /// Data representation template number (see Code table 5.0).
138    pub template_num: u16,
139    /// Data representation template (see template 5.X, where X is the data
140    /// representation template number given in octets 10-11).
141    #[grib_template(variant = "template_num")]
142    pub template: DataRepresentationTemplate,
143}
144
145#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
146#[repr(u16)]
147pub enum DataRepresentationTemplate {
148    _5_0(template5::Template5_0) = 0,
149    _5_1(template5::Template5_1) = 1,
150    _5_2(template5::Template5_2) = 2,
151    _5_3(template5::Template5_3) = 3,
152    _5_4(template5::Template5_4) = 4,
153    _5_40(template5::Template5_40) = 40,
154    _5_41(template5::Template5_41) = 41,
155    _5_42(template5::Template5_42) = 42,
156    _5_50(template5::Template5_50) = 50,
157    _5_51(template5::Template5_51) = 51,
158    _5_53(template5::Template5_53) = 53,
159    _5_61(template5::Template5_61) = 61,
160    _5_200(template5::Template5_200) = 200,
161}
162
163/// Section 6 - Bit-map section.
164pub type Section6 = Section<Section6Payload>;
165
166#[derive(Debug, PartialEq, TryFromSlice, Dump)]
167pub struct Section6Payload {
168    /// Bit-map indicator (see Code table 6.0 and the Note).
169    pub bitmap_indicator: u8,
170}
171
172pub mod template {
173    //! GRIB2 template definitions.
174
175    pub use super::{template1::*, template3::*, template5::*};
176
177    pub mod param_set {
178        //! Definitions of parameter sets used in GRIB2 templates.
179
180        pub use super::super::{template3::param_set::*, template5::param_set::*};
181    }
182}
183
184mod template1;
185mod template3;
186mod template5;