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