grib/decoder/
complex.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use std::iter;

use num::ToPrimitive;

use self::{
    diff::{
        FirstOrderSpatialDifferencingDecodeIterator, SecondOrderSpatialDifferencingDecodeIterator,
        SpatialDifferencingDecodeIterator,
    },
    missing::DecodedValue::{self, Missing1, Missing2, Normal},
};
use crate::{
    codetables::grib2::Table5_6,
    decoder::{
        param::{ComplexPackingParam, SimplePackingParam},
        simple::*,
        stream::{BitStream, NBitwiseIterator},
        DecodeError, Grib2SubmessageDecoder,
    },
    error::*,
    helpers::{read_as, GribInt},
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ComplexPackingDecodeError {
    NotSupported,
    LengthMismatch,
}

pub(crate) fn decode_7_2(
    target: &Grib2SubmessageDecoder,
) -> Result<
    SimplePackingDecodeIteratorWrapper<impl Iterator<Item = DecodedValue<i32>> + '_>,
    GribError,
> {
    let sect5_data = &target.sect5_payload;
    let simple_param = SimplePackingParam::from_buf(&sect5_data[6..16])?;
    let complex_param = ComplexPackingParam::from_buf(&sect5_data[16..42]);

    if complex_param.group_splitting_method_used != 1
        || complex_param.missing_value_management_used > 2
    {
        return Err(GribError::DecodeError(
            DecodeError::ComplexPackingDecodeError(ComplexPackingDecodeError::NotSupported),
        ));
    }

    let sect7_data = &target.sect7_payload;

    let unpacked_data = decode_complex_packing(complex_param, sect7_data, 0, simple_param.nbit, 0);
    let decoder = SimplePackingDecodeIterator::new(unpacked_data, &simple_param);
    let decoder = SimplePackingDecodeIteratorWrapper::SimplePacking(decoder);
    Ok(decoder)
}

pub(crate) fn decode_7_3(
    target: &Grib2SubmessageDecoder,
) -> Result<
    SimplePackingDecodeIteratorWrapper<impl Iterator<Item = DecodedValue<i32>> + '_>,
    GribError,
> {
    let sect5_data = &target.sect5_payload;
    let simple_param = SimplePackingParam::from_buf(&sect5_data[6..16])?;
    let complex_param = ComplexPackingParam::from_buf(&sect5_data[16..42]);
    let spdiff_order = read_as!(u8, sect5_data, 42);
    let spdiff_order = Table5_6::try_from(spdiff_order).map_err(|e| {
        let number = e.number;
        GribError::NotSupported(format!("Code Table 5.6 value '{number}' is not supported"))
    })?;
    let spdiff_param_octet = read_as!(u8, sect5_data, 43);

    if complex_param.group_splitting_method_used != 1
        || complex_param.missing_value_management_used > 2
        || matches!(spdiff_order, Table5_6::Missing)
    {
        return Err(GribError::DecodeError(
            DecodeError::ComplexPackingDecodeError(ComplexPackingDecodeError::NotSupported),
        ));
    }

    let sect7_data = &target.sect7_payload;
    let sect7_params = diff::SpatialDifferencingExtraDescriptors::new(
        sect7_data,
        u8::from(spdiff_order.clone()),
        spdiff_param_octet,
    )?;

    let unpacked_data = decode_complex_packing(
        complex_param,
        sect7_data,
        sect7_params.len(),
        simple_param.nbit,
        sect7_params.minimum(),
    );
    let first_values = sect7_params.first_values();
    let first_values = first_values.collect::<Vec<_>>().into_iter();
    let spdiff_unpacked = match spdiff_order {
        Table5_6::FirstOrderSpatialDifferencing => SpatialDifferencingDecodeIterator::FirstOrder(
            FirstOrderSpatialDifferencingDecodeIterator::new(unpacked_data, first_values),
        ),
        Table5_6::SecondOrderSpatialDifferencing => SpatialDifferencingDecodeIterator::SecondOrder(
            SecondOrderSpatialDifferencingDecodeIterator::new(unpacked_data, first_values),
        ),
        Table5_6::Missing => unreachable!(),
    };
    let decoder = SimplePackingDecodeIterator::new(spdiff_unpacked, &simple_param);
    let decoder = SimplePackingDecodeIteratorWrapper::SimplePacking(decoder);
    Ok(decoder)
}

fn decode_complex_packing(
    complex_param: ComplexPackingParam,
    sect7_data: &[u8],
    sect7_offset: usize,
    nbit: u8,
    z_min: i32,
) -> impl Iterator<Item = DecodedValue<i32>> + '_ {
    fn get_octet_length(nbit: u8, ngroup: u32) -> usize {
        let total_bit: u32 = ngroup * u32::from(nbit);
        let total_octet = (total_bit + 0b111) >> 3;
        total_octet as usize
    }

    let params_end_octet = sect7_offset;
    let group_refs_end_octet = params_end_octet + get_octet_length(nbit, complex_param.ngroup);
    let group_widths_end_octet = group_refs_end_octet
        + get_octet_length(complex_param.group_width_nbit, complex_param.ngroup);
    let group_lens_end_octet = group_widths_end_octet
        + get_octet_length(complex_param.group_len_nbit, complex_param.ngroup);

    let group_refs_iter = BitStream::new(
        &sect7_data[params_end_octet..group_refs_end_octet],
        usize::from(nbit),
        complex_param.ngroup as usize,
    );
    let group_refs_iter = group_refs_iter.take(complex_param.ngroup as usize);

    let group_widths_iter = BitStream::new(
        &sect7_data[group_refs_end_octet..group_widths_end_octet],
        usize::from(complex_param.group_width_nbit),
        complex_param.ngroup as usize,
    );
    let group_widths_iter = group_widths_iter
        .take(complex_param.ngroup as usize)
        .map(move |v| u32::from(complex_param.group_width_ref) + v);

    let group_lens_iter = BitStream::new(
        &sect7_data[group_widths_end_octet..group_lens_end_octet],
        usize::from(complex_param.group_len_nbit),
        (complex_param.ngroup - 1) as usize,
    );
    let group_lens_iter = group_lens_iter
        .take((complex_param.ngroup - 1) as usize)
        .map(move |v| complex_param.group_len_ref + u32::from(complex_param.group_len_inc) * v)
        .chain(iter::once(complex_param.group_len_last));

    ComplexPackingValueDecodeIterator::new(
        group_refs_iter,
        group_widths_iter,
        group_lens_iter,
        complex_param.missing_value_management_used,
        nbit,
        z_min,
        sect7_data[group_lens_end_octet..].to_vec(),
    )
    .flatten()
}

#[derive(Clone)]
struct ComplexPackingValueDecodeIterator<I, J, K> {
    ref_iter: I,
    width_iter: J,
    length_iter: K,
    missing_value_management: u8,
    nbit: u8,
    z_min: i32,
    data: Vec<u8>,
    pos: usize,
    start_offset_bits: usize,
}

impl<I, J, K> ComplexPackingValueDecodeIterator<I, J, K> {
    pub(crate) fn new(
        ref_iter: I,
        width_iter: J,
        length_iter: K,
        missing_value_management: u8,
        nbit: u8,
        z_min: i32,
        data: Vec<u8>,
    ) -> Self {
        Self {
            ref_iter,
            width_iter,
            length_iter,
            missing_value_management,
            nbit,
            z_min,
            data,
            pos: 0,
            start_offset_bits: 0,
        }
    }
}

impl<I, J, K, N, O, P> Iterator for ComplexPackingValueDecodeIterator<I, J, K>
where
    I: Iterator<Item = N>,
    J: Iterator<Item = O>,
    K: Iterator<Item = P>,
    N: ToPrimitive,
    O: ToPrimitive,
    P: ToPrimitive,
{
    type Item = Vec<DecodedValue<i32>>;

    fn next(&mut self) -> Option<Self::Item> {
        match (
            self.ref_iter.next(),
            self.width_iter.next(),
            self.length_iter.next(),
        ) {
            (Some(_ref), Some(width), Some(length)) if width.to_usize().unwrap() == 0 => {
                // The specification states as follows: "For groups with a constant value,
                // associated field width is 0, and no incremental data are physically present."
                let _ref = _ref.to_i32().unwrap();
                let length = length.to_usize().unwrap();
                let missing1 = (1 << self.nbit) - 1;
                let missing2 = missing1 - 1;

                if self.missing_value_management > 0 && _ref == missing1 {
                    Some(vec![Missing1; length])
                } else if self.missing_value_management == 2 && _ref == missing2 {
                    Some(vec![Missing2; length])
                } else {
                    Some(vec![Normal(_ref + self.z_min); length])
                }
            }
            (Some(_ref), Some(width), Some(length)) => {
                let (_ref, width, length) = (
                    _ref.to_i32().unwrap(),
                    width.to_usize().unwrap(),
                    length.to_usize().unwrap(),
                );
                let bits = self.start_offset_bits + width * length;
                let (pos_end, offset_bits) = (self.pos + bits / 8, bits % 8);
                let offset_byte = usize::from(offset_bits > 0);
                let missing1 = (1 << width) - 1;
                let missing2 = missing1 - 1;
                let group_values =
                    NBitwiseIterator::new(&self.data[self.pos..pos_end + offset_byte], width)
                        .with_offset(self.start_offset_bits)
                        .take(length)
                        .map(|v| {
                            if self.missing_value_management > 0 && v == missing1 {
                                Missing1
                            } else if self.missing_value_management == 2 && v == missing2 {
                                Missing2
                            } else {
                                Normal(v.as_grib_int() + _ref + self.z_min)
                            }
                        })
                        .collect::<Vec<_>>();
                self.pos = pos_end;
                self.start_offset_bits = offset_bits;
                Some(group_values)
            }
            _ => None,
        }
    }
}

mod diff;
mod missing;