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