Skip to main content

grib/def/
grib2.rs

1//! Definitions of parameters contained in GRIB2 data.
2
3use grib_template_derive::{Dump, TryFromSlice};
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, 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, 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, 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, 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, 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, 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, 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}
114
115/// Section 4 - Product definition section.
116pub type Section4 = Section<Section4Payload>;
117
118#[derive(Debug, PartialEq, TryFromSlice, Dump)]
119pub struct Section4Payload {
120    /// Number of coordinate values after template or number of information
121    /// according to 3D vertical coordinate GRIB2 message (see Notes 1 and 5).
122    pub num_coord_values: u16,
123    /// Product definition template number (see Code table 4.0).
124    pub template_num: u16,
125}
126
127/// Section 5 - Data representation section.
128pub type Section5 = Section<Section5Payload>;
129
130#[derive(Debug, PartialEq, TryFromSlice, Dump)]
131pub struct Section5Payload {
132    /// Number of data points where one or more values are specified in Section
133    /// 7 when a bit map is present, total number of data points when a bit map
134    /// is absent.
135    pub num_encoded_points: u32,
136    /// Data representation template number (see Code table 5.0).
137    pub template_num: u16,
138    /// Data representation template (see template 5.X, where X is the data
139    /// representation template number given in octets 10-11).
140    #[grib_template(variant = "template_num")]
141    pub template: DataRepresentationTemplate,
142}
143
144#[derive(Debug, PartialEq, TryFromSlice, Dump)]
145#[repr(u16)]
146pub enum DataRepresentationTemplate {
147    _5_0(template5::Template5_0) = 0,
148    _5_1(template5::Template5_1) = 1,
149    _5_2(template5::Template5_2) = 2,
150    _5_3(template5::Template5_3) = 3,
151    _5_4(template5::Template5_4) = 4,
152    _5_40(template5::Template5_40) = 40,
153    _5_41(template5::Template5_41) = 41,
154    _5_42(template5::Template5_42) = 42,
155    _5_50(template5::Template5_50) = 50,
156    _5_51(template5::Template5_51) = 51,
157    _5_53(template5::Template5_53) = 53,
158    _5_61(template5::Template5_61) = 61,
159    _5_200(template5::Template5_200) = 200,
160}
161
162/// Section 6 - Bit-map section.
163pub type Section6 = Section<Section6Payload>;
164
165#[derive(Debug, PartialEq, TryFromSlice, Dump)]
166pub struct Section6Payload {
167    /// Bit-map indicator (see Code table 6.0 and the Note).
168    pub bitmap_indicator: u8,
169}
170
171pub mod template {
172    //! GRIB2 template definitions.
173
174    pub use super::{template1::*, template3::*, template5::*};
175
176    pub mod param_set {
177        //! Definitions of parameter sets used in GRIB2 templates.
178
179        pub use super::super::{template3::param_set::*, template5::param_set::*};
180    }
181}
182
183mod template1;
184mod template3;
185mod template5;