1use crate::{
2 helpers::{read_as, GribInt},
3 GribError,
4};
5
6pub(crate) struct SimplePackingParam {
7 pub(crate) ref_val: f32,
8 pub(crate) exp: i16,
9 pub(crate) dig: i16,
10 pub(crate) nbit: u8,
11}
12
13impl SimplePackingParam {
14 pub(crate) fn from_buf(buf: &[u8]) -> Result<Self, GribError> {
15 let ref_val = read_as!(f32, buf, 0);
16 let exp = read_as!(u16, buf, 4).as_grib_int();
17 let dig = read_as!(u16, buf, 6).as_grib_int();
18 let nbit = read_as!(u8, buf, 8);
19 let original_field_type_value = read_as!(u8, buf, 9);
20
21 if original_field_type_value != 0 {
22 return Err(GribError::DecodeError(
23 crate::DecodeError::SimplePackingDecodeError(
24 super::simple::SimplePackingDecodeError::OriginalFieldValueTypeNotSupported,
25 ),
26 ));
27 }
28
29 Ok(Self {
30 ref_val,
31 exp,
32 dig,
33 nbit,
34 })
35 }
36
37 pub(crate) fn zero_bit_reference_value(&self) -> f32 {
38 self.ref_val * 10_f32.powi(-i32::from(self.dig))
39 }
40}
41
42pub(crate) struct ComplexPackingParam {
43 pub(crate) group_splitting_method_used: u8,
44 pub(crate) missing_value_management_used: u8,
45 pub(crate) ngroup: u32,
46 pub(crate) group_width_ref: u8,
47 pub(crate) group_width_nbit: u8,
48 pub(crate) group_len_ref: u32,
49 pub(crate) group_len_inc: u8,
50 pub(crate) group_len_last: u32,
51 pub(crate) group_len_nbit: u8,
52}
53
54impl ComplexPackingParam {
55 pub(crate) fn from_buf(buf: &[u8]) -> Self {
56 let group_splitting_method_used = read_as!(u8, buf, 0);
57 let missing_value_management_used = read_as!(u8, buf, 1);
58 let ngroup = read_as!(u32, buf, 10);
59 let group_width_ref = read_as!(u8, buf, 14);
60 let group_width_nbit = read_as!(u8, buf, 15);
61 let group_len_ref = read_as!(u32, buf, 16);
62 let group_len_inc = read_as!(u8, buf, 20);
63 let group_len_last = read_as!(u32, buf, 21);
64 let group_len_nbit = read_as!(u8, buf, 25);
65 Self {
66 group_splitting_method_used,
67 missing_value_management_used,
68 ngroup,
69 group_width_ref,
70 group_width_nbit,
71 group_len_ref,
72 group_len_inc,
73 group_len_last,
74 group_len_nbit,
75 }
76 }
77}