grib-core 0.7.0

Shared GRIB data model, primitives, and code tables
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
//! Data Representation Section (Section 5) shared model.

use crate::binary::{decode_wmo_i16, decode_wmo_i8};
use crate::error::{Error, Result};

/// GRIB data representation and validated packing parameters.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum DataRepresentation {
    /// Template 5.0: Simple packing.
    SimplePacking(ScaledPackingParams),
    /// GRIB1 grid-point second-order packing.
    ///
    /// GRIB1 stores this layout in Binary Data Section flags rather than a
    /// GRIB2 data representation template.
    Grib1SecondOrderPacking(Grib1SecondOrderPackingParams),
    /// Template 5.2/5.3: Complex packing with optional spatial differencing.
    ComplexPacking(ComplexPackingParams),
    /// Template 5.4: IEEE floating-point values.
    IeeePacking(IeeePackingParams),
    /// Template 5.40: JPEG 2000 code stream packing.
    Jpeg2000Packing(Jpeg2000PackingParams),
    /// Template 5.41: PNG image packing.
    PngPacking(PngPackingParams),
    /// Template 5.42: CCSDS 121.0 adaptive entropy coding.
    CcsdsPacking(CcsdsPackingParams),
    /// Template 5.200: Run-length packing with predefined level values.
    RunLengthPacking(RunLengthPackingParams),
    /// Unsupported template.
    Unsupported(u16),
}

/// Scaling parameters shared by GRIB1 and GRIB2 grid-point packing.
///
/// Decoded values use `(reference + packed * 2^binary_scale) *
/// 10^-decimal_scale`.
#[derive(Debug, Clone, PartialEq)]
pub struct ScaledPackingParams {
    pub encoded_values: usize,
    pub reference_value: f32,
    pub binary_scale: i16,
    pub decimal_scale: i16,
    pub bits_per_value: u8,
    pub original_field_type: u8,
}

/// Standard GRIB1 second-order group layout.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Grib1SecondOrderLayout {
    /// One variable-width group for each scan line; group lengths come from
    /// the grid and optional primary bitmap.
    RowByRow,
    /// Groups are located by a secondary bitmap and share one value width.
    ConstantWidth,
    /// Groups are located by a secondary bitmap and have individual widths.
    VariableWidth,
}

/// Validated parameters for standard GRIB1 grid-point second-order packing.
#[derive(Debug, Clone, PartialEq)]
pub struct Grib1SecondOrderPackingParams {
    pub(crate) packing: ScaledPackingParams,
    pub(crate) layout: Grib1SecondOrderLayout,
    pub(crate) group_count: usize,
    pub(crate) group_widths: Vec<u8>,
    pub(crate) group_lengths: Vec<usize>,
    pub(crate) first_order_values_offset: usize,
    pub(crate) second_order_values_offset: usize,
    pub(crate) unused_bits: u8,
}

impl Grib1SecondOrderPackingParams {
    pub const fn packing(&self) -> &ScaledPackingParams {
        &self.packing
    }

    pub const fn layout(&self) -> Grib1SecondOrderLayout {
        self.layout
    }

    pub const fn group_count(&self) -> usize {
        self.group_count
    }

    /// Returns one width for constant-width packing or one width per group for
    /// row-by-row and variable-width packing.
    pub fn group_widths(&self) -> &[u8] {
        &self.group_widths
    }

    /// Returns secondary-bitmap group lengths. Row-by-row lengths are supplied
    /// from the grid and primary bitmap when the field is indexed.
    pub fn group_lengths(&self) -> &[usize] {
        &self.group_lengths
    }

    /// Byte offset of first-order values relative to GRIB1 BDS octet 12.
    pub const fn first_order_values_offset(&self) -> usize {
        self.first_order_values_offset
    }

    /// Byte offset of second-order values relative to GRIB1 BDS octet 12.
    pub const fn second_order_values_offset(&self) -> usize {
        self.second_order_values_offset
    }

    pub const fn unused_bits(&self) -> u8 {
        self.unused_bits
    }

    /// Returns the second-order width for one group.
    pub fn group_width(&self, group_index: usize) -> Option<u8> {
        match self.layout {
            Grib1SecondOrderLayout::ConstantWidth => self.group_widths.first().copied(),
            Grib1SecondOrderLayout::RowByRow | Grib1SecondOrderLayout::VariableWidth => {
                self.group_widths.get(group_index).copied()
            }
        }
    }
}

/// IEEE floating-point precision from Code Table 5.7.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IeeePrecision {
    /// IEEE 754 binary32.
    Single,
    /// IEEE 754 binary64.
    Double,
    /// IEEE 754 binary128.
    Quadruple,
}

impl IeeePrecision {
    /// Returns the Code Table 5.7 wire value.
    pub const fn code(self) -> u8 {
        match self {
            Self::Single => 1,
            Self::Double => 2,
            Self::Quadruple => 3,
        }
    }

    /// Returns the number of octets occupied by each Section 7 value.
    pub const fn bytes_per_value(self) -> usize {
        match self {
            Self::Single => 4,
            Self::Double => 8,
            Self::Quadruple => 16,
        }
    }

    /// Returns the number of bits occupied by each Section 7 value.
    pub const fn bits_per_value(self) -> u8 {
        match self {
            Self::Single => 32,
            Self::Double => 64,
            Self::Quadruple => 128,
        }
    }
}

/// Parameters for IEEE floating-point packing (Template 5.4).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IeeePackingParams {
    pub encoded_values: usize,
    pub precision: IeeePrecision,
}

/// Parameters for run-length packing with level values (Template 5.200).
///
/// Packed level zero represents a missing value. Codes from one through
/// `max_level_value` index the corresponding scaled entry in `level_values`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunLengthPackingParams {
    pub encoded_values: usize,
    pub bits_per_value: u8,
    pub max_level_value: u16,
    pub decimal_scale: i16,
    pub level_values: Vec<u16>,
}

impl RunLengthPackingParams {
    /// Returns the decoded representative value for a nonzero level code.
    pub fn representative_value(&self, level: u16) -> Option<f64> {
        if level == 0 || level > self.max_level_value {
            return None;
        }
        let scaled = *self.level_values.get(usize::from(level - 1))?;
        Some(f64::from(scaled) * 10.0_f64.powi(-i32::from(self.decimal_scale)))
    }
}

/// Parameters for JPEG 2000 code stream packing (Template 5.40).
#[derive(Debug, Clone, PartialEq)]
pub struct Jpeg2000PackingParams {
    pub packing: ScaledPackingParams,
    pub compression_type: u8,
    pub target_compression_ratio: u8,
}

/// Parameters for PNG image packing (Template 5.41).
#[derive(Debug, Clone, PartialEq)]
pub struct PngPackingParams {
    pub packing: ScaledPackingParams,
}

/// CCSDS/AEC option mask stored by data representation template 5.42.
///
/// The bit assignments match the `libaec` API referenced by the WMO template.
/// Bit 7 is reserved and therefore rejected by [`CcsdsFlags::from_bits`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CcsdsFlags(u8);

impl CcsdsFlags {
    /// No optional codec behavior.
    pub const NONE: Self = Self(0);
    /// Treat sample bit patterns as signed during AEC preprocessing.
    ///
    /// GRIB reconstruction still interprets the decoded samples as unsigned
    /// scaled differences from the field reference value.
    pub const SIGNED: Self = Self(1 << 0);
    /// Samples with 17 through 24 bits use three-byte containers.
    pub const THREE_BYTE: Self = Self(1 << 1);
    /// Sample containers store their most-significant byte first.
    pub const MSB: Self = Self(1 << 2);
    /// Apply the CCSDS unit-delay preprocessor.
    pub const PREPROCESS: Self = Self(1 << 3);
    /// Use the restricted code-option set for samples no wider than four bits.
    pub const RESTRICTED: Self = Self(1 << 4);
    /// Decode legacy streams whose reference sample intervals are byte padded.
    pub const PAD_RSI: Self = Self(1 << 5);
    /// Permit non-standard even block sizes.
    pub const NOT_ENFORCE: Self = Self(1 << 6);

    const KNOWN_BITS: u8 = Self::SIGNED.0
        | Self::THREE_BYTE.0
        | Self::MSB.0
        | Self::PREPROCESS.0
        | Self::RESTRICTED.0
        | Self::PAD_RSI.0
        | Self::NOT_ENFORCE.0;

    /// ecCodes' canonical template 5.42 options.
    pub const DEFAULT: Self = Self(Self::THREE_BYTE.0 | Self::MSB.0 | Self::PREPROCESS.0);

    /// Constructs flags when no reserved bits are set.
    pub const fn from_bits(bits: u8) -> Option<Self> {
        if bits & !Self::KNOWN_BITS == 0 {
            Some(Self(bits))
        } else {
            None
        }
    }

    /// Returns the template 5.42 wire representation.
    pub const fn bits(self) -> u8 {
        self.0
    }

    /// Returns whether all bits in `other` are present.
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }
}

impl Default for CcsdsFlags {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl std::ops::BitOr for CcsdsFlags {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

impl std::ops::BitOrAssign for CcsdsFlags {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

/// Number of samples in a CCSDS/AEC coding block.
///
/// Standard streams use 8, 16, 32, or 64 samples. Other non-zero even sizes
/// are representable because `libaec` permits them when
/// [`CcsdsFlags::NOT_ENFORCE`] is present.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CcsdsBlockSize(u8);

impl CcsdsBlockSize {
    pub const EIGHT: Self = Self(8);
    pub const SIXTEEN: Self = Self(16);
    pub const THIRTY_TWO: Self = Self(32);
    pub const SIXTY_FOUR: Self = Self(64);

    /// Constructs a block size accepted by the AEC codec.
    pub const fn new(samples: u8) -> Option<Self> {
        if samples != 0 && samples.is_multiple_of(2) {
            Some(Self(samples))
        } else {
            None
        }
    }

    pub const fn samples(self) -> u8 {
        self.0
    }

    pub const fn is_standard(self) -> bool {
        matches!(self.0, 8 | 16 | 32 | 64)
    }
}

impl Default for CcsdsBlockSize {
    fn default() -> Self {
        Self::THIRTY_TWO
    }
}

/// Validated parameters for CCSDS 121.0 packing (template 5.42).
#[derive(Debug, Clone, PartialEq)]
pub struct CcsdsPackingParams {
    packing: ScaledPackingParams,
    flags: CcsdsFlags,
    block_size: CcsdsBlockSize,
    reference_sample_interval: u16,
}

impl CcsdsPackingParams {
    /// Constructs a parameter set after validating the interdependent AEC
    /// width, flag, block-size, and reference-interval constraints.
    pub fn new(
        packing: ScaledPackingParams,
        flags: CcsdsFlags,
        block_size: CcsdsBlockSize,
        reference_sample_interval: u16,
    ) -> Result<Self> {
        validate_ccsds_parameters(
            packing.bits_per_value,
            flags,
            block_size,
            reference_sample_interval,
        )
        .map_err(Error::ValueOutOfRange)?;

        Ok(Self {
            packing,
            flags,
            block_size,
            reference_sample_interval,
        })
    }

    pub const fn packing(&self) -> &ScaledPackingParams {
        &self.packing
    }

    pub const fn flags(&self) -> CcsdsFlags {
        self.flags
    }

    pub const fn block_size(&self) -> CcsdsBlockSize {
        self.block_size
    }

    pub const fn reference_sample_interval(&self) -> u16 {
        self.reference_sample_interval
    }
}

/// Parameters for complex packing (Templates 5.2 and 5.3).
///
/// Decoders represent embedded primary and secondary missing codes as `NaN`.
/// The template's substitute metadata is preserved separately and can be
/// queried with [`ComplexPackingParams::primary_missing_substitute`] and
/// [`ComplexPackingParams::secondary_missing_substitute`].
#[derive(Debug, Clone, PartialEq)]
pub struct ComplexPackingParams {
    pub encoded_values: usize,
    pub reference_value: f32,
    pub binary_scale: i16,
    pub decimal_scale: i16,
    pub group_reference_bits: u8,
    pub original_field_type: u8,
    pub group_splitting_method: u8,
    pub missing_value_management: u8,
    pub primary_missing_substitute: u32,
    pub secondary_missing_substitute: u32,
    pub num_groups: usize,
    pub group_width_reference: u8,
    pub group_width_bits: u8,
    pub group_length_reference: u32,
    pub group_length_increment: u8,
    pub true_length_last_group: u32,
    pub scaled_group_length_bits: u8,
    pub spatial_differencing: Option<SpatialDifferencingParams>,
}

impl ComplexPackingParams {
    /// Returns the raw four-octet primary substitute when primary missing
    /// values are enabled by Code Table 5.5.
    ///
    /// The decoder intentionally emits `NaN` instead of applying this value.
    /// Callers that need producer-specific substitution can inspect
    /// [`ComplexPackingParams::original_field_type`] and interpret this raw
    /// Template 5.2/5.3 value accordingly.
    pub const fn primary_missing_substitute(&self) -> Option<u32> {
        match self.missing_value_management {
            1 | 2 => Some(self.primary_missing_substitute),
            _ => None,
        }
    }

    /// Returns the raw four-octet secondary substitute when both primary and
    /// secondary missing values are enabled by Code Table 5.5.
    ///
    /// The decoder intentionally emits `NaN` instead of applying this value.
    /// Callers that need producer-specific substitution can inspect
    /// [`ComplexPackingParams::original_field_type`] and interpret this raw
    /// Template 5.2/5.3 value accordingly.
    pub const fn secondary_missing_substitute(&self) -> Option<u32> {
        match self.missing_value_management {
            2 => Some(self.secondary_missing_substitute),
            _ => None,
        }
    }
}

/// Parameters specific to template 5.3 spatial differencing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SpatialDifferencingParams {
    pub order: u8,
    pub descriptor_octets: u8,
}

impl DataRepresentation {
    /// Returns the GRIB2 template number or a diagnostic identifier for a
    /// GRIB1 representation.
    pub const fn template_number(&self) -> u16 {
        match self {
            Self::SimplePacking(_) => 0,
            // GRIB1 has no template number. Keep this identifier disjoint from
            // WMO GRIB2 template numbers for diagnostics and writer rejection.
            Self::Grib1SecondOrderPacking(_) => 1005,
            Self::ComplexPacking(params) => {
                if params.spatial_differencing.is_some() {
                    3
                } else {
                    2
                }
            }
            Self::IeeePacking(_) => 4,
            Self::Jpeg2000Packing(_) => 40,
            Self::PngPacking(_) => 41,
            Self::CcsdsPacking(_) => 42,
            Self::RunLengthPacking(_) => 200,
            Self::Unsupported(template) => *template,
        }
    }

    pub fn parse(section_bytes: &[u8]) -> Result<Self> {
        if section_bytes.len() < 11 {
            return Err(Error::InvalidSection {
                section: 5,
                reason: format!("expected at least 11 bytes, got {}", section_bytes.len()),
            });
        }
        if section_bytes[4] != 5 {
            return Err(Error::InvalidSection {
                section: section_bytes[4],
                reason: "not a data representation section".into(),
            });
        }

        let template = u16::from_be_bytes(section_bytes[9..11].try_into().unwrap());
        match template {
            0 => parse_simple_packing(section_bytes),
            2 => parse_complex_packing(section_bytes, false),
            3 => parse_complex_packing(section_bytes, true),
            4 => parse_ieee_packing(section_bytes),
            40 => parse_jpeg2000_packing(section_bytes),
            41 => parse_png_packing(section_bytes),
            42 => parse_ccsds_packing(section_bytes),
            200 => parse_run_length_packing(section_bytes),
            _ => Ok(Self::Unsupported(template)),
        }
    }

    pub fn encoded_values(&self) -> Option<usize> {
        match self {
            Self::SimplePacking(params) => Some(params.encoded_values),
            Self::Grib1SecondOrderPacking(params) => Some(params.packing.encoded_values),
            Self::ComplexPacking(params) => Some(params.encoded_values),
            Self::IeeePacking(params) => Some(params.encoded_values),
            Self::Jpeg2000Packing(params) => Some(params.packing.encoded_values),
            Self::PngPacking(params) => Some(params.packing.encoded_values),
            Self::CcsdsPacking(params) => Some(params.packing().encoded_values),
            Self::RunLengthPacking(params) => Some(params.encoded_values),
            Self::Unsupported(_) => None,
        }
    }
}

fn parse_simple_packing(data: &[u8]) -> Result<DataRepresentation> {
    Ok(DataRepresentation::SimplePacking(
        parse_scaled_packing_base(data, 0, 21)?,
    ))
}

fn parse_ieee_packing(data: &[u8]) -> Result<DataRepresentation> {
    if data.len() < 12 {
        return Err(Error::InvalidSection {
            section: 5,
            reason: format!("template 5.4 requires 12 bytes, got {}", data.len()),
        });
    }

    let precision = match data[11] {
        1 => IeeePrecision::Single,
        2 => IeeePrecision::Double,
        3 => IeeePrecision::Quadruple,
        code => {
            return Err(Error::InvalidSection {
                section: 5,
                reason: format!("template 5.4 has invalid IEEE precision code {code}"),
            });
        }
    };

    Ok(DataRepresentation::IeeePacking(IeeePackingParams {
        encoded_values: u32::from_be_bytes(data[5..9].try_into().unwrap()) as usize,
        precision,
    }))
}

fn parse_run_length_packing(data: &[u8]) -> Result<DataRepresentation> {
    if data.len() < 17 {
        return Err(Error::InvalidSection {
            section: 5,
            reason: format!(
                "template 5.200 requires at least 17 bytes, got {}",
                data.len()
            ),
        });
    }

    let bits_per_value = data[11];
    let max_level_value = u16::from_be_bytes(data[12..14].try_into().unwrap());
    let number_of_level_values = usize::from(u16::from_be_bytes(data[14..16].try_into().unwrap()));
    let required = number_of_level_values
        .checked_mul(2)
        .and_then(|level_bytes| 17_usize.checked_add(level_bytes))
        .ok_or(Error::ArithmeticOverflow {
            operation: "computing template 5.200 length",
        })?;
    if data.len() < required {
        return Err(Error::InvalidSection {
            section: 5,
            reason: format!(
                "template 5.200 declares {number_of_level_values} level values and requires {required} bytes, got {}",
                data.len()
            ),
        });
    }

    validate_run_length_parameters(bits_per_value, max_level_value, number_of_level_values)
        .map_err(|reason| Error::InvalidSection {
            section: 5,
            reason: format!("invalid template 5.200 parameters: {reason}"),
        })?;

    let mut level_values = Vec::new();
    level_values
        .try_reserve_exact(number_of_level_values)
        .map_err(|error| {
            Error::allocation("template 5.200 level values", number_of_level_values, error)
        })?;
    for bytes in data[17..required].chunks_exact(2) {
        level_values.push(u16::from_be_bytes(
            bytes.try_into().expect("two-byte level value"),
        ));
    }

    Ok(DataRepresentation::RunLengthPacking(
        RunLengthPackingParams {
            encoded_values: u32::from_be_bytes(data[5..9].try_into().unwrap()) as usize,
            bits_per_value,
            max_level_value,
            decimal_scale: decode_wmo_i8(data[16]),
            level_values,
        },
    ))
}

fn validate_run_length_parameters(
    bits_per_value: u8,
    max_level_value: u16,
    number_of_level_values: usize,
) -> std::result::Result<(), String> {
    if bits_per_value > 64 {
        return Err(format!(
            "bits per value must not exceed 64, got {bits_per_value}"
        ));
    }
    if max_level_value == 0 {
        if number_of_level_values != 0 {
            return Err(format!(
                "zero maximum level requires an empty level table, got {number_of_level_values} entries"
            ));
        }
        return Ok(());
    }
    if bits_per_value == 0 {
        return Err(format!(
            "bits per value must be between 1 and 64, got {bits_per_value}"
        ));
    }
    if usize::from(max_level_value) > number_of_level_values {
        return Err(format!(
            "maximum level {max_level_value} exceeds the {number_of_level_values}-entry level table"
        ));
    }
    let max_code = if bits_per_value == 64 {
        u64::MAX
    } else {
        (1_u64 << bits_per_value) - 1
    };
    if u64::from(max_level_value) >= max_code {
        return Err(format!(
            "maximum level {max_level_value} leaves no run-length codes at {bits_per_value} bits"
        ));
    }
    Ok(())
}

fn parse_scaled_packing_base(
    data: &[u8],
    template: u16,
    required: usize,
) -> Result<ScaledPackingParams> {
    if data.len() < required {
        return Err(Error::InvalidSection {
            section: 5,
            reason: format!(
                "template 5.{template} requires {required} bytes, got {}",
                data.len()
            ),
        });
    }

    Ok(ScaledPackingParams {
        encoded_values: u32::from_be_bytes(data[5..9].try_into().unwrap()) as usize,
        reference_value: f32::from_be_bytes(data[11..15].try_into().unwrap()),
        binary_scale: decode_wmo_i16(&data[15..17]).unwrap(),
        decimal_scale: decode_wmo_i16(&data[17..19]).unwrap(),
        bits_per_value: data[19],
        original_field_type: data[20],
    })
}

fn parse_jpeg2000_packing(data: &[u8]) -> Result<DataRepresentation> {
    Ok(DataRepresentation::Jpeg2000Packing(Jpeg2000PackingParams {
        packing: parse_scaled_packing_base(data, 40, 23)?,
        compression_type: data[21],
        target_compression_ratio: data[22],
    }))
}

fn parse_png_packing(data: &[u8]) -> Result<DataRepresentation> {
    Ok(DataRepresentation::PngPacking(PngPackingParams {
        packing: parse_scaled_packing_base(data, 41, 21)?,
    }))
}

fn parse_ccsds_packing(data: &[u8]) -> Result<DataRepresentation> {
    let packing = parse_scaled_packing_base(data, 42, 25)?;
    let flags = CcsdsFlags::from_bits(data[21]).ok_or_else(|| Error::InvalidSection {
        section: 5,
        reason: format!(
            "template 5.42 has reserved CCSDS flag bits set: 0x{:02x}",
            data[21]
        ),
    })?;
    let block_size = CcsdsBlockSize::new(data[22]).ok_or_else(|| Error::InvalidSection {
        section: 5,
        reason: format!(
            "template 5.42 block size must be a non-zero even value, got {}",
            data[22]
        ),
    })?;
    let reference_sample_interval = u16::from_be_bytes(data[23..25].try_into().unwrap());

    validate_ccsds_parameters(
        packing.bits_per_value,
        flags,
        block_size,
        reference_sample_interval,
    )
    .map_err(|reason| Error::InvalidSection {
        section: 5,
        reason: format!("invalid template 5.42 parameters: {reason}"),
    })?;

    Ok(DataRepresentation::CcsdsPacking(CcsdsPackingParams {
        packing,
        flags,
        block_size,
        reference_sample_interval,
    }))
}

fn validate_ccsds_parameters(
    bits_per_value: u8,
    flags: CcsdsFlags,
    block_size: CcsdsBlockSize,
    reference_sample_interval: u16,
) -> std::result::Result<(), String> {
    if bits_per_value > 32 {
        return Err(format!(
            "CCSDS bits per value must be at most 32, got {bits_per_value}"
        ));
    }
    if !block_size.is_standard() && !flags.contains(CcsdsFlags::NOT_ENFORCE) {
        return Err(format!(
            "CCSDS block size {} requires the NOT_ENFORCE flag",
            block_size.samples()
        ));
    }
    if !(1..=4096).contains(&reference_sample_interval) {
        return Err(format!(
            "CCSDS reference sample interval must be between 1 and 4096, got {reference_sample_interval}"
        ));
    }
    if flags.contains(CcsdsFlags::RESTRICTED) && bits_per_value > 4 {
        return Err(format!(
            "CCSDS RESTRICTED flag requires at most 4 bits per value, got {bits_per_value}"
        ));
    }
    Ok(())
}

fn parse_complex_packing(
    data: &[u8],
    with_spatial_differencing: bool,
) -> Result<DataRepresentation> {
    let required = if with_spatial_differencing { 49 } else { 47 };
    if data.len() < required {
        return Err(Error::InvalidSection {
            section: 5,
            reason: format!(
                "template 5.{} requires {required} bytes, got {}",
                if with_spatial_differencing { 3 } else { 2 },
                data.len()
            ),
        });
    }

    let group_splitting_method = data[21];
    if !matches!(group_splitting_method, 0 | 1) {
        return Err(Error::UnsupportedGroupSplittingMethod(
            group_splitting_method,
        ));
    }

    let missing_value_management = data[22];
    if missing_value_management > 2 {
        return Err(Error::UnsupportedMissingValueManagement(
            missing_value_management,
        ));
    }

    let spatial_differencing = if with_spatial_differencing {
        let order = data[47];
        if !matches!(order, 1 | 2) {
            return Err(Error::UnsupportedSpatialDifferencingOrder(order));
        }
        Some(SpatialDifferencingParams {
            order,
            descriptor_octets: data[48],
        })
    } else {
        None
    };
    let group_length_reference = u32::from_be_bytes(data[37..41].try_into().unwrap());
    let group_length_increment = data[41];
    let true_length_last_group = u32::from_be_bytes(data[42..46].try_into().unwrap());
    let scaled_group_length_bits = data[46];
    if group_splitting_method == 0
        && (group_length_reference != 0
            || group_length_increment != 0
            || true_length_last_group != 0
            || scaled_group_length_bits != 0)
    {
        return Err(Error::InvalidSection {
            section: 5,
            reason: "complex packing group splitting method 0 requires all group-length fields to be zero"
                .into(),
        });
    }

    Ok(DataRepresentation::ComplexPacking(ComplexPackingParams {
        encoded_values: u32::from_be_bytes(data[5..9].try_into().unwrap()) as usize,
        reference_value: f32::from_be_bytes(data[11..15].try_into().unwrap()),
        binary_scale: decode_wmo_i16(&data[15..17]).unwrap(),
        decimal_scale: decode_wmo_i16(&data[17..19]).unwrap(),
        group_reference_bits: data[19],
        original_field_type: data[20],
        group_splitting_method,
        missing_value_management,
        primary_missing_substitute: u32::from_be_bytes(data[23..27].try_into().unwrap()),
        secondary_missing_substitute: u32::from_be_bytes(data[27..31].try_into().unwrap()),
        num_groups: u32::from_be_bytes(data[31..35].try_into().unwrap()) as usize,
        group_width_reference: data[35],
        group_width_bits: data[36],
        group_length_reference,
        group_length_increment,
        true_length_last_group,
        scaled_group_length_bits,
        spatial_differencing,
    }))
}

#[cfg(test)]
mod tests {
    use super::{
        CcsdsBlockSize, CcsdsFlags, CcsdsPackingParams, ComplexPackingParams, DataRepresentation,
        IeeePackingParams, IeeePrecision, Jpeg2000PackingParams, PngPackingParams,
        RunLengthPackingParams, ScaledPackingParams, SpatialDifferencingParams,
    };
    use crate::binary::{encode_wmo_i16, encode_wmo_i8};

    #[test]
    fn parses_simple_packing_template() {
        let mut section = vec![0u8; 21];
        section[..4].copy_from_slice(&(21u32).to_be_bytes());
        section[4] = 5;
        section[5..9].copy_from_slice(&3u32.to_be_bytes());
        section[9..11].copy_from_slice(&0u16.to_be_bytes());
        section[11..15].copy_from_slice(&10.0f32.to_be_bytes());
        section[15..17].copy_from_slice(&2i16.to_be_bytes());
        section[17..19].copy_from_slice(&1i16.to_be_bytes());
        section[19] = 8;
        section[20] = 0;

        let representation = DataRepresentation::parse(&section).unwrap();
        assert_eq!(representation.template_number(), 0);
        assert_eq!(
            representation,
            DataRepresentation::SimplePacking(ScaledPackingParams {
                encoded_values: 3,
                reference_value: 10.0,
                binary_scale: 2,
                decimal_scale: 1,
                bits_per_value: 8,
                original_field_type: 0,
            })
        );
    }

    #[test]
    fn parses_all_ieee_packing_precisions() {
        for (code, precision) in [
            (1, IeeePrecision::Single),
            (2, IeeePrecision::Double),
            (3, IeeePrecision::Quadruple),
        ] {
            let representation = DataRepresentation::parse(&ieee_section(code)).unwrap();
            assert_eq!(representation.template_number(), 4);
            assert_eq!(
                representation,
                DataRepresentation::IeeePacking(IeeePackingParams {
                    encoded_values: 6,
                    precision,
                })
            );
            assert_eq!(representation.encoded_values(), Some(6));
        }
    }

    #[test]
    fn rejects_invalid_or_truncated_ieee_packing_parameters() {
        for code in [0, 4, 255] {
            let error = DataRepresentation::parse(&ieee_section(code)).unwrap_err();
            assert!(error.to_string().contains("invalid IEEE precision code"));
        }

        let error = DataRepresentation::parse(&ieee_section(1)[..11]).unwrap_err();
        assert!(error.to_string().contains("requires 12 bytes"));
    }

    #[test]
    fn parses_run_length_packing_template() {
        let section = run_length_section(3, 2, -1, &[10, 20, 30]);
        let representation = DataRepresentation::parse(&section).unwrap();

        assert_eq!(representation.template_number(), 200);
        assert_eq!(
            representation,
            DataRepresentation::RunLengthPacking(RunLengthPackingParams {
                encoded_values: 9,
                bits_per_value: 3,
                max_level_value: 2,
                decimal_scale: -1,
                level_values: vec![10, 20, 30],
            })
        );
        let DataRepresentation::RunLengthPacking(params) = representation else {
            unreachable!();
        };
        assert_eq!(params.representative_value(0), None);
        assert_eq!(params.representative_value(1), Some(100.0));
        assert_eq!(params.representative_value(2), Some(200.0));
        assert_eq!(params.representative_value(3), None);
    }

    #[test]
    fn parses_all_missing_run_length_parameters() {
        let representation = DataRepresentation::parse(&run_length_section(0, 0, 0, &[])).unwrap();
        assert_eq!(
            representation,
            DataRepresentation::RunLengthPacking(RunLengthPackingParams {
                encoded_values: 9,
                bits_per_value: 0,
                max_level_value: 0,
                decimal_scale: 0,
                level_values: Vec::new(),
            })
        );
    }

    #[test]
    fn rejects_invalid_run_length_parameters() {
        for (section, reason) in [
            (
                run_length_section(3, 3, 0, &[10, 20]),
                "exceeds the 2-entry level table",
            ),
            (
                run_length_section(2, 3, 0, &[10, 20, 30]),
                "leaves no run-length codes",
            ),
            (
                run_length_section(0, 1, 0, &[10]),
                "bits per value must be between 1 and 64",
            ),
            (
                run_length_section(65, 1, 0, &[10]),
                "bits per value must not exceed 64",
            ),
            (
                run_length_section(65, 0, 0, &[]),
                "bits per value must not exceed 64",
            ),
            (
                run_length_section(3, 0, 0, &[10]),
                "zero maximum level requires an empty level table",
            ),
        ] {
            let error = DataRepresentation::parse(&section).unwrap_err();
            assert!(error.to_string().contains(reason), "{error}");
        }

        let section = run_length_section(3, 2, 0, &[10, 20]);
        let error = DataRepresentation::parse(&section[..section.len() - 1]).unwrap_err();
        assert!(error.to_string().contains("requires 21 bytes"));
    }

    #[test]
    fn parses_jpeg2000_packing_template() {
        let mut section = vec![0u8; 23];
        section[..4].copy_from_slice(&(23u32).to_be_bytes());
        section[4] = 5;
        section[5..9].copy_from_slice(&4u32.to_be_bytes());
        section[9..11].copy_from_slice(&40u16.to_be_bytes());
        section[11..15].copy_from_slice(&3.5f32.to_be_bytes());
        section[15..17].copy_from_slice(&encode_wmo_i16(-1).unwrap());
        section[17..19].copy_from_slice(&2i16.to_be_bytes());
        section[19] = 12;
        section[20] = 0;
        section[21] = 1;
        section[22] = 255;

        let representation = DataRepresentation::parse(&section).unwrap();
        assert_eq!(representation.template_number(), 40);
        assert_eq!(
            representation,
            DataRepresentation::Jpeg2000Packing(Jpeg2000PackingParams {
                packing: ScaledPackingParams {
                    encoded_values: 4,
                    reference_value: 3.5,
                    binary_scale: -1,
                    decimal_scale: 2,
                    bits_per_value: 12,
                    original_field_type: 0,
                },
                compression_type: 1,
                target_compression_ratio: 255,
            })
        );
    }

    #[test]
    fn parses_png_packing_template() {
        let mut section = vec![0u8; 21];
        section[..4].copy_from_slice(&(21u32).to_be_bytes());
        section[4] = 5;
        section[5..9].copy_from_slice(&6u32.to_be_bytes());
        section[9..11].copy_from_slice(&41u16.to_be_bytes());
        section[11..15].copy_from_slice(&1.25f32.to_be_bytes());
        section[15..17].copy_from_slice(&1i16.to_be_bytes());
        section[17..19].copy_from_slice(&encode_wmo_i16(-2).unwrap());
        section[19] = 16;
        section[20] = 1;

        let representation = DataRepresentation::parse(&section).unwrap();
        assert_eq!(representation.template_number(), 41);
        assert_eq!(
            representation,
            DataRepresentation::PngPacking(PngPackingParams {
                packing: ScaledPackingParams {
                    encoded_values: 6,
                    reference_value: 1.25,
                    binary_scale: 1,
                    decimal_scale: -2,
                    bits_per_value: 16,
                    original_field_type: 1,
                },
            })
        );
    }

    #[test]
    fn exposes_only_enabled_complex_missing_value_substitutes() {
        let mut params = ComplexPackingParams {
            encoded_values: 1,
            reference_value: 0.0,
            binary_scale: 0,
            decimal_scale: 0,
            group_reference_bits: 1,
            original_field_type: 0,
            group_splitting_method: 1,
            missing_value_management: 0,
            primary_missing_substitute: 0x1122_3344,
            secondary_missing_substitute: 0x5566_7788,
            num_groups: 1,
            group_width_reference: 0,
            group_width_bits: 0,
            group_length_reference: 1,
            group_length_increment: 1,
            true_length_last_group: 1,
            scaled_group_length_bits: 0,
            spatial_differencing: None,
        };

        assert_eq!(params.primary_missing_substitute(), None);
        assert_eq!(params.secondary_missing_substitute(), None);

        params.missing_value_management = 1;
        assert_eq!(params.primary_missing_substitute(), Some(0x1122_3344));
        assert_eq!(params.secondary_missing_substitute(), None);

        params.missing_value_management = 2;
        assert_eq!(params.primary_missing_substitute(), Some(0x1122_3344));
        assert_eq!(params.secondary_missing_substitute(), Some(0x5566_7788));
    }

    #[test]
    fn reports_complex_and_unknown_template_numbers() {
        let mut params = ComplexPackingParams {
            encoded_values: 0,
            reference_value: 0.0,
            binary_scale: 0,
            decimal_scale: 0,
            group_reference_bits: 0,
            original_field_type: 0,
            group_splitting_method: 1,
            missing_value_management: 0,
            primary_missing_substitute: 0,
            secondary_missing_substitute: 0,
            num_groups: 0,
            group_width_reference: 0,
            group_width_bits: 0,
            group_length_reference: 0,
            group_length_increment: 0,
            true_length_last_group: 0,
            scaled_group_length_bits: 0,
            spatial_differencing: None,
        };

        assert_eq!(
            DataRepresentation::ComplexPacking(params.clone()).template_number(),
            2
        );
        params.spatial_differencing = Some(SpatialDifferencingParams {
            order: 1,
            descriptor_octets: 1,
        });
        assert_eq!(
            DataRepresentation::ComplexPacking(params).template_number(),
            3
        );
        assert_eq!(DataRepresentation::Unsupported(500).template_number(), 500);
    }

    #[test]
    fn parses_ccsds_packing_template() {
        let section = ccsds_section(20, CcsdsFlags::DEFAULT.bits(), 32, 128);

        let representation = DataRepresentation::parse(&section).unwrap();
        assert_eq!(representation.template_number(), 42);
        assert_eq!(
            representation,
            DataRepresentation::CcsdsPacking(
                CcsdsPackingParams::new(
                    ScaledPackingParams {
                        encoded_values: 6,
                        reference_value: 1.25,
                        binary_scale: -1,
                        decimal_scale: 2,
                        bits_per_value: 20,
                        original_field_type: 0,
                    },
                    CcsdsFlags::DEFAULT,
                    CcsdsBlockSize::THIRTY_TWO,
                    128,
                )
                .unwrap()
            )
        );
    }

    #[test]
    fn parses_unenforced_even_ccsds_block_size() {
        let flags = CcsdsFlags::DEFAULT | CcsdsFlags::NOT_ENFORCE;
        let section = ccsds_section(8, flags.bits(), 12, 1);
        let parsed = DataRepresentation::parse(&section).unwrap();
        let DataRepresentation::CcsdsPacking(params) = parsed else {
            panic!("expected CCSDS representation");
        };

        assert_eq!(params.flags(), flags);
        assert_eq!(params.block_size().samples(), 12);
        assert_eq!(params.reference_sample_interval(), 1);
    }

    #[test]
    fn rejects_reserved_ccsds_flag_bit() {
        let error = DataRepresentation::parse(&ccsds_section(8, 0x80, 32, 128)).unwrap_err();
        assert!(matches!(
            error,
            crate::Error::InvalidSection { section: 5, .. }
        ));
        assert!(error.to_string().contains("reserved CCSDS flag bits"));
    }

    #[test]
    fn rejects_nonstandard_ccsds_block_without_flag() {
        let error = DataRepresentation::parse(&ccsds_section(8, 0, 12, 128)).unwrap_err();
        assert!(error.to_string().contains("requires the NOT_ENFORCE flag"));
    }

    #[test]
    fn rejects_restricted_ccsds_width_above_four_bits() {
        let error =
            DataRepresentation::parse(&ccsds_section(5, CcsdsFlags::RESTRICTED.bits(), 32, 128))
                .unwrap_err();
        assert!(error.to_string().contains("requires at most 4 bits"));
    }

    #[test]
    fn rejects_ccsds_reference_interval_outside_codec_range() {
        for interval in [0, 4097] {
            let error = DataRepresentation::parse(&ccsds_section(8, 0, 32, interval)).unwrap_err();
            assert!(error
                .to_string()
                .contains("reference sample interval must be between 1 and 4096"));
        }
    }

    fn ccsds_section(bits_per_value: u8, flags: u8, block_size: u8, rsi: u16) -> Vec<u8> {
        let mut section = vec![0u8; 25];
        section[..4].copy_from_slice(&25u32.to_be_bytes());
        section[4] = 5;
        section[5..9].copy_from_slice(&6u32.to_be_bytes());
        section[9..11].copy_from_slice(&42u16.to_be_bytes());
        section[11..15].copy_from_slice(&1.25f32.to_be_bytes());
        section[15..17].copy_from_slice(&encode_wmo_i16(-1).unwrap());
        section[17..19].copy_from_slice(&2i16.to_be_bytes());
        section[19] = bits_per_value;
        section[20] = 0;
        section[21] = flags;
        section[22] = block_size;
        section[23..25].copy_from_slice(&rsi.to_be_bytes());
        section
    }

    fn ieee_section(precision: u8) -> Vec<u8> {
        let mut section = vec![0u8; 12];
        section[..4].copy_from_slice(&12u32.to_be_bytes());
        section[4] = 5;
        section[5..9].copy_from_slice(&6u32.to_be_bytes());
        section[9..11].copy_from_slice(&4u16.to_be_bytes());
        section[11] = precision;
        section
    }

    fn run_length_section(
        bits_per_value: u8,
        max_level_value: u16,
        decimal_scale: i16,
        level_values: &[u16],
    ) -> Vec<u8> {
        let length = 17 + 2 * level_values.len();
        let mut section = vec![0u8; length];
        section[..4].copy_from_slice(&(length as u32).to_be_bytes());
        section[4] = 5;
        section[5..9].copy_from_slice(&9u32.to_be_bytes());
        section[9..11].copy_from_slice(&200u16.to_be_bytes());
        section[11] = bits_per_value;
        section[12..14].copy_from_slice(&max_level_value.to_be_bytes());
        section[14..16].copy_from_slice(&(level_values.len() as u16).to_be_bytes());
        section[16] = encode_wmo_i8(decimal_scale).unwrap();
        for (bytes, level) in section[17..].chunks_exact_mut(2).zip(level_values) {
            bytes.copy_from_slice(&level.to_be_bytes());
        }
        section
    }
}