|
| 1 | +use bitvec::prelude::*; |
| 2 | + |
| 3 | +use super::data_representation_template::DataRepresentationTemplate; |
| 4 | +use super::tables::OriginalFieldValue; |
| 5 | +use crate::utils::read_f32_from_bytes; |
| 6 | +use crate::{ |
| 7 | + error::GribberishError, |
| 8 | + templates::template::{Template, TemplateType}, |
| 9 | + utils::{ |
| 10 | + extract_ccsds_data, iter::ScaleGribValueIterator, read_u16_from_bytes, read_u32_from_bytes, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +pub struct CCSDSDataRepresentationTemplate { |
| 15 | + data: Vec<u8>, |
| 16 | +} |
| 17 | + |
| 18 | +impl Template for CCSDSDataRepresentationTemplate { |
| 19 | + fn data(&self) -> &[u8] { |
| 20 | + self.data.as_slice() |
| 21 | + } |
| 22 | + |
| 23 | + fn template_number(&self) -> u16 { |
| 24 | + 42 |
| 25 | + } |
| 26 | + |
| 27 | + fn template_type(&self) -> TemplateType { |
| 28 | + TemplateType::DataRepresentation |
| 29 | + } |
| 30 | + |
| 31 | + fn template_name(&self) -> &str { |
| 32 | + "grid point and spectral data - CCSDS recommended lossless compression" |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl CCSDSDataRepresentationTemplate { |
| 37 | + pub fn new(data: Vec<u8>) -> CCSDSDataRepresentationTemplate { |
| 38 | + CCSDSDataRepresentationTemplate { data } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn data_point_count(&self) -> usize { |
| 42 | + read_u32_from_bytes(self.data.as_slice(), 5).unwrap_or(0) as usize |
| 43 | + } |
| 44 | + |
| 45 | + pub fn reference_value(&self) -> f32 { |
| 46 | + read_f32_from_bytes(self.data.as_slice(), 11).unwrap_or(0.0) |
| 47 | + } |
| 48 | + |
| 49 | + pub fn binary_scale_factor(&self) -> i16 { |
| 50 | + as_signed!( |
| 51 | + read_u16_from_bytes(self.data.as_slice(), 15).unwrap_or(0), |
| 52 | + 16, |
| 53 | + i16 |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + pub fn decimal_scale_factor(&self) -> i16 { |
| 58 | + as_signed!( |
| 59 | + read_u16_from_bytes(self.data.as_slice(), 17).unwrap_or(0), |
| 60 | + 16, |
| 61 | + i16 |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + // Nbits |
| 66 | + pub fn bit_count(&self) -> u8 { |
| 67 | + self.data[19] |
| 68 | + } |
| 69 | + |
| 70 | + pub fn original_field_value(&self) -> OriginalFieldValue { |
| 71 | + self.data[20].into() |
| 72 | + } |
| 73 | + |
| 74 | + pub fn ccsds_compression_options_mask(&self) -> u8 { |
| 75 | + self.data[21] |
| 76 | + } |
| 77 | + |
| 78 | + pub fn block_size(&self) -> u8 { |
| 79 | + self.data[22] |
| 80 | + } |
| 81 | + |
| 82 | + // restart interval |
| 83 | + pub fn reference_sample_interval(&self) -> u16 { |
| 84 | + read_u16_from_bytes(self.data.as_slice(), 23).unwrap_or(0) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl DataRepresentationTemplate<f64> for CCSDSDataRepresentationTemplate { |
| 89 | + fn compression_type(&self) -> String { |
| 90 | + "CCSDS".into() |
| 91 | + } |
| 92 | + |
| 93 | + fn bit_count_per_datapoint(&self) -> usize { |
| 94 | + self.bit_count() as usize |
| 95 | + } |
| 96 | + |
| 97 | + fn unpack(&self, bits: &BitSlice<u8, Msb0>) -> Result<Vec<f64>, GribberishError> { |
| 98 | + let bits_per_val: usize = self.bit_count().into(); |
| 99 | + if bits_per_val == 0 { |
| 100 | + return Ok(vec![]); |
| 101 | + } |
| 102 | + |
| 103 | + let bytes: Vec<u8> = bits.to_bitvec().into(); |
| 104 | + |
| 105 | + let nbytes_per_sample: usize = (bits_per_val + 7) / 8; |
| 106 | + |
| 107 | + let size = self.data_point_count() * nbytes_per_sample; |
| 108 | + let outputwr = extract_ccsds_data( |
| 109 | + bytes, |
| 110 | + self.block_size(), |
| 111 | + self.ccsds_compression_options_mask(), |
| 112 | + size, |
| 113 | + self.reference_sample_interval(), |
| 114 | + bits_per_val, |
| 115 | + ); |
| 116 | + |
| 117 | + match outputwr { |
| 118 | + Ok(output_value) => { |
| 119 | + // Ok(output_value) |
| 120 | + Ok(output_value |
| 121 | + .into_iter() |
| 122 | + .scale_value_by( |
| 123 | + self.binary_scale_factor(), |
| 124 | + self.decimal_scale_factor(), |
| 125 | + self.reference_value(), |
| 126 | + ) |
| 127 | + .collect()) |
| 128 | + } |
| 129 | + Err(e) => Err(e), |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments