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