Skip to main content

grib/def/
grib2.rs

1//! Definitions of parameters contained in GRIB2 data.
2//!
3//! For parameter values represented as unsigned or signed integers,
4//! [`MissingValue`](crate::MissingValue) trait can be used to check whether
5//! they are missing values.
6
7use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};
8
9#[derive(Debug, PartialEq, Clone, TryFromSlice, Dump)]
10pub struct Section<T>
11where
12    T: PartialEq + crate::TryFromSlice + crate::Dump,
13{
14    pub header: SectionHeader,
15    pub payload: T,
16}
17
18#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
19pub struct SectionHeader {
20    /// Length of section in octets (nn).
21    pub len: u32,
22    /// Number of section.
23    pub sect_num: u8,
24}
25
26/// SECTION 0 - Indicator section.
27#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
28pub struct Section0 {
29    /// "GRIB" (coded according to the International Alphabet No. 5).
30    pub identifier: [u8; 4],
31    /// Reserved.
32    pub reserved: [u8; 2],
33    /// Discipline - GRIB Master Table Number (see Code Table 0.0).
34    pub discipline: u8,
35    /// GRIB Edition Number (currently 2).
36    pub edition_num: u8,
37    /// Total length of GRIB message in octets (including Section 0).
38    pub total_len: u64,
39}
40
41/// Section 1 - Identification section.
42pub type Section1 = Section<Section1Payload>;
43
44#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
45pub struct Section1Payload {
46    /// Identification of originating/generating centre (see Common Code table
47    /// C-11).
48    pub centre_id: u16,
49    /// Identification of originating/generating subcentre (allocated by
50    /// originating/generating centre).
51    pub subcentre_id: u16,
52    /// GRIB master table version number (see Common Code table C-0 and Note 1).
53    pub master_table_version: u8,
54    /// Version number of GRIB Local tables used to augment Master tables (see
55    /// Code table 1.1 and Note 2).
56    pub local_table_version: u8,
57    /// Significance of reference time (see Code table 1.2).
58    pub ref_time_significance: u8,
59    /// Reference time of data.
60    #[dump(doc(
61        year = "Year (4 digits) - Reference time of data.",
62        month = "Month - Reference time of data.",
63        day = "Day - Reference time of data.",
64        hour = "Hour - Reference time of data.",
65        minute = "Minute - Reference time of data.",
66        second = "Second - Reference time of data.",
67    ))]
68    pub ref_time: template::param_set::DateTime,
69    /// Production status of processed data in this GRIB message (see Code table
70    /// 1.3).
71    pub prod_status: u8,
72    /// Type of processed data in this GRIB message (see Code table 1.4).
73    pub data_type: u8,
74    pub optional: Option<Section1PayloadOptional>,
75}
76
77#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
78pub struct Section1PayloadOptional {
79    /// Identification template number (optional, see Code table 1.5).
80    pub template_num: u16,
81    /// Identification template (optional, see template 1.X, where X is the
82    /// identification template number given in octets 22-23).
83    #[grib_template(variant = "template_num")]
84    pub template: IdentificationTemplate,
85}
86
87#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
88#[non_exhaustive]
89#[repr(u16)]
90pub enum IdentificationTemplate {
91    _1_0(template1::Template1_0) = 0,
92    _1_1(template1::Template1_1) = 1,
93    _1_2(template1::Template1_2) = 2,
94}
95
96/// Section 3 - Grid definition section.
97pub type Section3 = Section<Section3Payload>;
98
99#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
100pub struct Section3Payload {
101    /// Source of grid definition (see Code table 3.0 and Note 1).
102    pub grid_def_source: u8,
103    /// Number of data points.
104    pub num_points: u32,
105    /// Number of octets for optional list of numbers (see Note 2).
106    pub num_point_list_octets: u8,
107    /// Interpretation of list of numbers (see Code table 3.11).
108    pub point_list_interpretation: u8,
109    /// Grid definition template number (= N) (see Code table 3.1).
110    pub template_num: u16,
111    /// Grid Definition Template (see Template 3.N, where N is the Grid
112    /// Definition Template Number given in octets 13-14).
113    #[grib_template(variant = "template_num")]
114    pub template: GridDefinitionTemplate,
115}
116
117#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
118#[non_exhaustive]
119#[repr(u16)]
120pub enum GridDefinitionTemplate {
121    _3_0(template3::Template3_0) = 0,
122    _3_1(template3::Template3_1) = 1,
123    _3_2(template3::Template3_2) = 2,
124    _3_3(template3::Template3_3) = 3,
125    _3_10(template3::Template3_10) = 10,
126    _3_20(template3::Template3_20) = 20,
127    _3_30(template3::Template3_30) = 30,
128    _3_40(template3::Template3_40) = 40,
129    _3_41(template3::Template3_41) = 41,
130    _3_42(template3::Template3_42) = 42,
131    _3_43(template3::Template3_43) = 43,
132    _3_101(template3::Template3_101) = 101,
133}
134
135/// Section 4 - Product definition section.
136pub type Section4 = Section<Section4Payload>;
137
138#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
139pub struct Section4Payload {
140    /// Number of coordinate values after template or number of information
141    /// according to 3D vertical coordinate GRIB2 message (see Notes 1 and 5).
142    pub num_coord_values: u16,
143    /// Product definition template number (see Code table 4.0).
144    pub template_num: u16,
145    /// Product Definition Template (see Template 4.X, where X is the Product
146    /// Definition Template Number given in octets 8-9).
147    #[grib_template(variant = "template_num")]
148    pub template: ProductDefinitionTemplate,
149}
150
151#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
152#[non_exhaustive]
153#[repr(u16)]
154pub enum ProductDefinitionTemplate {
155    _4_0(template4::Template4_0) = 0,
156    _4_1(template4::Template4_1) = 1,
157    _4_2(template4::Template4_2) = 2,
158    _4_5(template4::Template4_5) = 5,
159    _4_6(template4::Template4_6) = 6,
160}
161
162/// Section 5 - Data representation section.
163pub type Section5 = Section<Section5Payload>;
164
165#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
166pub struct Section5Payload {
167    /// Number of data points where one or more values are specified in Section
168    /// 7 when a bit map is present, total number of data points when a bit map
169    /// is absent.
170    pub num_encoded_points: u32,
171    /// Data representation template number (see Code table 5.0).
172    pub template_num: u16,
173    /// Data representation template (see template 5.X, where X is the data
174    /// representation template number given in octets 10-11).
175    #[grib_template(variant = "template_num")]
176    pub template: DataRepresentationTemplate,
177}
178
179#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
180#[non_exhaustive]
181#[repr(u16)]
182pub enum DataRepresentationTemplate {
183    _5_0(template5::Template5_0) = 0,
184    _5_1(template5::Template5_1) = 1,
185    _5_2(template5::Template5_2) = 2,
186    _5_3(template5::Template5_3) = 3,
187    _5_4(template5::Template5_4) = 4,
188    _5_40(template5::Template5_40) = 40,
189    _5_41(template5::Template5_41) = 41,
190    _5_42(template5::Template5_42) = 42,
191    _5_50(template5::Template5_50) = 50,
192    _5_51(template5::Template5_51) = 51,
193    _5_53(template5::Template5_53) = 53,
194    _5_61(template5::Template5_61) = 61,
195    _5_200(template5::Template5_200) = 200,
196    _5_50002(template5::Template5_50002) = 50002,
197}
198
199/// Section 6 - Bit-map section.
200pub type Section6 = Section<Section6Payload>;
201
202#[derive(Debug, PartialEq, Clone, TryFromSlice, Dump)]
203pub struct Section6Payload {
204    /// Bit-map indicator (see Code table 6.0 and the Note).
205    pub bitmap_indicator: u8,
206}
207
208pub mod template {
209    //! GRIB2 template definitions.
210
211    pub use super::{template1::*, template3::*, template4::*, template5::*};
212
213    pub mod param_set {
214        //! Definitions of parameter sets used in GRIB2 templates.
215
216        use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};
217
218        pub use super::super::{
219            template3::param_set::*, template4::param_set::*, template5::param_set::*,
220        };
221
222        /// Date and time.
223        #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
224        pub struct DateTime {
225            /// Year (4 digits).
226            pub year: u16,
227            /// Month.
228            pub month: u8,
229            /// Day.
230            pub day: u8,
231            /// Hour.
232            pub hour: u8,
233            /// Minute.
234            pub minute: u8,
235            /// Second.
236            pub second: u8,
237        }
238
239        /// Value with a scaling.
240        #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
241        pub struct ScaledValue<F, V>
242        where
243            F: crate::TryFromSlice + crate::WriteToBuffer + grib_template_helpers::DumpField,
244            V: crate::TryFromSlice + crate::WriteToBuffer + grib_template_helpers::DumpField,
245        {
246            /// Scale factor.
247            pub scale_factor: F,
248            /// Scaled value.
249            pub scaled_value: V,
250        }
251
252        /// Time range.
253        #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
254        pub struct TimeRange<L>
255        where
256            L: crate::TryFromSlice + crate::WriteToBuffer + grib_template_helpers::DumpField,
257        {
258            /// Unit of time range (see Code Table 4.4).
259            pub unit: u8,
260            /// Length of the time range in units defined by `unit`.
261            pub len: L,
262        }
263    }
264}
265
266mod template1;
267mod template3;
268mod template4;
269mod template5;