grib/def/grib2/
template5.rs

1use grib_template_derive::{Dump, TryFromSlice};
2
3/// Data representation template 5.0 - Grid point data - simple packing.
4#[derive(Debug, PartialEq, TryFromSlice, Dump)]
5pub struct Template5_0 {
6    pub simple: param_set::SimplePacking,
7    /// Type of original field values (see Code table 5.1).
8    pub orig_field_type: u8,
9}
10
11/// Data representation template 5.1 - Matrix value at grid point - simple
12/// packing.
13#[derive(Debug, PartialEq, TryFromSlice, Dump)]
14pub struct Template5_1 {
15    pub simple: param_set::SimplePacking,
16    /// Type of original field values (see Code table 5.1).
17    pub orig_field_type: u8,
18    /// 0, no matrix bit maps present; 1-matrix bit maps present.
19    pub matrix_bitmap_present: u8,
20    /// Number of data values encoded in Section 7.
21    pub num_encoded_vals: u32,
22    /// NR - first dimension (rows) of each matrix.
23    pub num_dim_1: u16,
24    /// NC - second dimension (columns) of each matrix.
25    pub num_dim_2: u16,
26    /// First dimension coordinate value definition (Code table 5.2).
27    pub dim_1_coord_def: u8,
28    /// NC1 - number of coefficients or values used to specify first dimension
29    /// coordinate function.
30    pub num_dim_1_coeffs: u8,
31    /// Second dimension coordinate value definition (Code table 5.2).
32    pub dim_2_coord_def: u8,
33    /// NC2 - number of coefficients or values used to specify second dimension
34    /// coordinate function.
35    pub num_dim_2_coeffs: u8,
36    /// First dimension physical significance (Code table 5.3).
37    pub dim_1_significance: u8,
38    /// Second dimension physical significance (Code table 5.3).
39    pub dim_2_significance: u8,
40    /// Coefficients to define first dimension coordinate values in functional
41    /// form, or the explicit coordinate values (IEEE 32-bit floating-point
42    /// value).
43    #[grib_template(len = "num_dim_1_coeffs")]
44    pub dim_1_coeffs: Vec<f32>,
45    /// Coefficients to define second dimension coordinate values in functional
46    /// form, or the explicit coordinate values (IEEE 32-bit floating-point
47    /// value).
48    #[grib_template(len = "num_dim_2_coeffs")]
49    pub dim_2_coeffs: Vec<f32>,
50}
51
52/// Data representation template 5.2 - Grid point data - complex packing.
53#[derive(Debug, PartialEq, TryFromSlice, Dump)]
54pub struct Template5_2 {
55    pub simple: param_set::SimplePacking,
56    /// Type of original field values (see Code table 5.1).
57    pub orig_field_type: u8,
58    pub complex: param_set::ComplexPacking,
59}
60
61/// Data representation template 5.3 - Grid point data - complex packing and
62/// spatial differencing.
63#[derive(Debug, PartialEq, TryFromSlice, Dump)]
64pub struct Template5_3 {
65    pub simple: param_set::SimplePacking,
66    /// Type of original field values (see Code table 5.1).
67    pub orig_field_type: u8,
68    pub complex: param_set::ComplexPacking,
69    /// Order of spatial differencing (see Code table 5.6).
70    pub spatial_diff_order: u8,
71    /// Number of octets required in the data section to specify extra
72    /// descriptors needed for spatial differencing (octets 6-ww in data
73    /// template 7.3).
74    pub num_extra_desc_octets: u8,
75}
76
77/// Data representation template 5.4 - Grid point data - IEEE floating point
78/// data.
79#[derive(Debug, PartialEq, TryFromSlice, Dump)]
80pub struct Template5_4 {
81    /// Precision (see Code table 5.7).
82    pub precision: u8,
83}
84
85/// Data representation template 5.40 - Grid point data - JPEG 2000 code stream
86/// format.
87#[derive(Debug, PartialEq, TryFromSlice, Dump)]
88pub struct Template5_40 {
89    pub simple: param_set::SimplePacking,
90    /// Type of original field values (see Code table 5.1).
91    pub orig_field_type: u8,
92    /// Type of compression used (see Code table 5.40).
93    pub compression_type: u8,
94    /// Target compression ratio, M:1 (with respect to the bit-depth specified
95    /// in octet 20), when octet 22 indicates lossy compression. Otherwise, set
96    /// to missing (see Note 3).
97    pub compression_ratio: u8,
98}
99
100/// Data representation template 5.41 - Grid point data - Portable Network
101/// Graphics (PNG).
102#[derive(Debug, PartialEq, TryFromSlice, Dump)]
103pub struct Template5_41 {
104    pub simple: param_set::SimplePacking,
105    /// Type of original field values (see Code table 5.1).
106    pub orig_field_type: u8,
107}
108
109/// Data representation template 5.42 - Grid point data - CCSDS recommended
110/// lossless compression.
111#[derive(Debug, PartialEq, TryFromSlice, Dump)]
112pub struct Template5_42 {
113    pub simple: param_set::SimplePacking,
114    /// Type of original field values (see Code table 5.1).
115    pub orig_field_type: u8,
116    /// CCSDS compression options mask (see Note 3).
117    pub mask: u8,
118    /// Block size.
119    pub block_size: u8,
120    /// Reference sample interval.
121    pub ref_sample_interval: u16,
122}
123
124/// Data representation template 5.50 - Spectral data - simple packing.
125#[derive(Debug, PartialEq, TryFromSlice, Dump)]
126pub struct Template5_50 {
127    pub simple: param_set::SimplePacking,
128    /// Real part of (0.0) coefficient (IEEE 32-bit floating-point value).
129    pub real_part_zero: f32,
130}
131
132/// Data representation template 5.51 - Spherical harmonics data - complex
133/// packing.
134#[derive(Debug, PartialEq, TryFromSlice, Dump)]
135pub struct Template5_51 {
136    pub simple: param_set::SimplePacking,
137    /// P - Laplacian scaling factor (expressed in 10-6 units).
138    pub p: i32,
139    /// JS - pentagonal resolution parameter of the unpacked subset (see Note
140    /// 1).
141    pub js: u16,
142    /// KS - pentagonal resolution parameter of the unpacked subset (see Note
143    /// 1).
144    pub ks: u16,
145    /// MS - pentagonal resolution parameter of the unpacked subset (see Note
146    /// 1).
147    pub ms: u16,
148    /// TS - total number of values in the unpacked subset (see Note 1).
149    pub ts: u32,
150    /// Precision of the unpacked subset (see Code table 5.7).
151    pub precision: u8,
152}
153
154/// Data representation template 5.53 - Spectral data for limited area models -
155/// complex packing.
156#[derive(Debug, PartialEq, TryFromSlice, Dump)]
157pub struct Template5_53 {
158    pub simple: param_set::SimplePacking,
159    /// Bi-Fourier sub-truncation type (see Code table 5.25).
160    pub bi_fourier_subtrunc_type: u8,
161    /// Packing mode for axes (see Code table 5.26).
162    pub bi_fourier_pack_mode: u8,
163    /// P - Laplacian scaling factor (expressed in 10-6 units).
164    pub p: i32,
165    /// NS - bi-Fourier resolution parameter of the unpacked subset (see Note
166    /// 1).
167    pub ns: u16,
168    /// MS - bi-Fourier resolution parameter of the unpacked subset (see Note
169    /// 1).
170    pub ms: u16,
171    /// TS - total number of values in the unpacked subset (see Note 1).
172    pub ts: u32,
173    /// Precision of the unpacked subset (see Code table 5.7).
174    pub precision: u8,
175}
176
177/// Data representation template 5.61 - Grid point data - simple packing with
178/// logarithm pre-processing.
179#[derive(Debug, PartialEq, TryFromSlice, Dump)]
180pub struct Template5_61 {
181    pub simple: param_set::SimplePacking,
182    /// Pre-processing parameter (B) (IEEE 32-bit floating-point value).
183    pub preprocess_param: f32,
184}
185
186/// Data representation template 5.200 - Run length packing with level values.
187#[derive(Debug, PartialEq, Eq, TryFromSlice, Dump)]
188pub struct Template5_200 {
189    /// Number of bits used for each packed value in the run length packing with
190    /// level value.
191    pub num_bits: u8,
192    /// MV - maximum value within the levels that are used in the packing.
193    pub max_val: u16,
194    /// MVL - maximum value of level (predefined).
195    pub max_level: u16,
196    /// Decimal scale factor of representative value of each level.
197    pub dec: u8,
198    /// List of MVL scaled representative values of each level from lv=1 to MVL.
199    #[grib_template(len = "max_level")]
200    pub level_vals: Vec<u16>,
201}
202
203pub mod param_set {
204    //! Definitions of parameter sets used in GRIB2 templates.
205
206    use grib_template_derive::{Dump, TryFromSlice};
207
208    #[derive(Debug, PartialEq, TryFromSlice, Dump)]
209    pub struct SimplePacking {
210        /// Reference value (R) (IEEE 32-bit floating-point value).
211        pub ref_val: f32,
212        /// Binary scale factor (E).
213        pub exp: i16,
214        /// Decimal scale factor (D).
215        pub dec: i16,
216        /// Number of bits used for each packed value for simple packing, or for
217        /// each group reference value for complex packing or spatial
218        /// differencing.
219        pub num_bits: u8,
220    }
221
222    impl SimplePacking {
223        pub(crate) fn zero_bit_reference_value(&self) -> f32 {
224            self.ref_val * 10_f32.powi(-i32::from(self.dec))
225        }
226    }
227
228    #[derive(Debug, PartialEq, Eq, TryFromSlice, Dump)]
229    pub struct ComplexPacking {
230        /// Group splitting method used (see Code table 5.4).
231        pub group_splitting_method: u8,
232        /// Missing value management used (see Code table 5.5).
233        pub missing_value_management: u8,
234        /// Primary missing value substitute.
235        pub primary_missing_value: u32,
236        /// Secondary missing value substitute.
237        pub secondary_missing_value: u32,
238        /// NG - number of groups of data values into which field is split.
239        pub num_groups: u32,
240        /// Reference for group widths (see Note 12).
241        pub group_width_ref: u8,
242        /// Number of bits used for the group widths (after the reference value
243        /// in octet 36 has been removed).
244        pub num_group_width_bits: u8,
245        /// Reference for group lengths (see Note 13).
246        pub group_len_ref: u32,
247        /// Length increment for the group lengths (see Note 14).
248        pub group_len_inc: u8,
249        /// True length of last group.
250        pub group_len_last: u32,
251        /// Number of bits used for the scaled group lengths (after subtraction
252        /// of the reference value given in octets 38-41 and division by
253        /// the length increment given in octet 42).
254        pub num_group_len_bits: u8,
255    }
256}