grib/def/grib2/template5.rs
1use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};
2
3/// Data representation template 5.0 - Grid point data - simple packing.
4#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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, Clone, TryFromSlice, WriteToBuffer, 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
203/// Data representation template 5.50002 - Second order packing.
204///
205/// This local template is defined by Météo-France.
206///
207/// # Examples
208///
209/// ```
210/// use std::io::Read;
211///
212/// use grib::{
213/// TryFromSlice,
214/// def::grib2::template::{Template5_50002, Template5_50002Optional, param_set},
215/// };
216///
217/// fn main() -> Result<(), Box<dyn std::error::Error>> {
218/// let mut buf = Vec::new();
219///
220/// let f = std::fs::File::open("testdata/constant-polyn0025.grib2")?;
221/// let mut f = std::io::BufReader::new(f);
222/// f.read_to_end(&mut buf)?;
223///
224/// let mut pos = 0x9a;
225/// let actual = Template5_50002::try_from_slice(&buf, &mut pos)?;
226/// let expected = Template5_50002 {
227/// simple: param_set::SimplePacking {
228/// ref_val: -0.46913582,
229/// exp: -13,
230/// dec: 0,
231/// num_bits: 24,
232/// },
233/// first_order_values_width: 23,
234/// num_groups: 379,
235/// second_order_packed_values_width: 264145,
236/// widths_width: 5,
237/// widths_len: 14,
238/// ordering_flags: param_set::OrderingFlags(0b00000000),
239/// spd_order: 2,
240/// optional: Some(Template5_50002Optional {
241/// num_spd_width_bits: 24,
242/// }),
243/// };
244/// assert_eq!(actual, expected);
245///
246/// Ok(())
247/// }
248/// ```
249#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
250pub struct Template5_50002 {
251 pub simple: param_set::SimplePacking,
252 /// Width of first order values.
253 pub first_order_values_width: u8,
254 /// Number of groups.
255 pub num_groups: u32,
256 /// Number of second order packed values.
257 pub second_order_packed_values_width: u32,
258 /// Width of widths.
259 pub widths_width: u8,
260 /// Width of lengths.
261 pub widths_len: u8,
262 pub ordering_flags: param_set::OrderingFlags,
263 /// Order of spatial differencing.
264 pub spd_order: u8,
265 pub optional: Option<Template5_50002Optional>,
266}
267
268#[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
269pub struct Template5_50002Optional {
270 /// Width of spatial differencing.
271 pub num_spd_width_bits: u8,
272}
273
274pub(crate) mod param_set {
275 use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};
276
277 #[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
278 pub struct SimplePacking {
279 /// Reference value (R) (IEEE 32-bit floating-point value).
280 pub ref_val: f32,
281 /// Binary scale factor (E).
282 pub exp: i16,
283 /// Decimal scale factor (D).
284 pub dec: i16,
285 /// Number of bits used for each packed value for simple packing, or for
286 /// each group reference value for complex packing or spatial
287 /// differencing.
288 pub num_bits: u8,
289 }
290
291 impl SimplePacking {
292 pub(crate) fn zero_bit_reference_value(&self) -> f32 {
293 self.ref_val * 10_f32.powi(-i32::from(self.dec))
294 }
295 }
296
297 #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
298 pub struct ComplexPacking {
299 /// Group splitting method used (see Code table 5.4).
300 pub group_splitting_method: u8,
301 /// Missing value management used (see Code table 5.5).
302 pub missing_value_management: u8,
303 /// Primary missing value substitute.
304 pub primary_missing_value: u32,
305 /// Secondary missing value substitute.
306 pub secondary_missing_value: u32,
307 /// NG - number of groups of data values into which field is split.
308 pub num_groups: u32,
309 /// Reference for group widths (see Note 12).
310 pub group_width_ref: u8,
311 /// Number of bits used for the group widths (after the reference value
312 /// in octet 36 has been removed).
313 pub num_group_width_bits: u8,
314 /// Reference for group lengths (see Note 13).
315 pub group_len_ref: u32,
316 /// Length increment for the group lengths (see Note 14).
317 pub group_len_inc: u8,
318 /// True length of last group.
319 pub group_len_last: u32,
320 /// Number of bits used for the scaled group lengths (after subtraction
321 /// of the reference value given in octets 38-41 and division by
322 /// the length increment given in octet 42).
323 pub num_group_len_bits: u8,
324 }
325
326 #[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromSlice, WriteToBuffer, Dump)]
327 pub struct OrderingFlags(
328 /// Ordering flags (Flag table 5.50002).
329 pub u8,
330 );
331}