Skip to content

Commit 542bb93

Browse files
Added Ecmwf CCSDS Decompression (#66)
* Ecmwf work * major improvements * continued * Partially working * Working! * formatting * finalization! * remove old lib --------- Co-authored-by: Matthew Iannucci <mpiannucci@gmail.com>
1 parent bdda123 commit 542bb93

File tree

9 files changed

+2959
-7
lines changed

9 files changed

+2959
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
*.sublime-workspace
33
*.DS_Store
44
target/
5-
temp/
5+
temp/
6+
testdata/

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gribberish/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ itertools = "0.10.5"
2020
mappers = "0.7.1"
2121
bitvec = "1.0.1"
2222
thiserror = "1.0.60"
23+
bitflags = "2.6.0"
2324

2425
[features]
2526
default = ["png", "jpeg"]

gribberish/src/sections/data_representation.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::utils::{read_u16_from_bytes, read_u32_from_bytes};
2-
use crate::templates::data_representation::{DataRepresentationTemplate, SimplePackingDataRepresentationTemplate, ComplexPackingDataRepresentationTemplate, ComplexSpatialPackingDataRepresentationTemplate};
2+
use crate::templates::data_representation::{DataRepresentationTemplate, SimplePackingDataRepresentationTemplate, ComplexPackingDataRepresentationTemplate, ComplexSpatialPackingDataRepresentationTemplate, CCSDSDataRepresentationTemplate};
3+
34
#[cfg(feature = "jpeg")]
45
use crate::templates::data_representation::JPEGDataRepresentationTemplate;
56
#[cfg(feature = "png")]
@@ -36,6 +37,7 @@ impl <'a> DataRepresentationSection<'a> {
3637
40 => Some(Box::new(JPEGDataRepresentationTemplate::new(self.data.to_vec()))),
3738
#[cfg(feature = "png")]
3839
41 => Some(Box::new(PNGDataRepresentationTemplate::new(self.data.to_vec()))),
40+
42 => Some(Box::new(CCSDSDataRepresentationTemplate::new(self.data.to_vec()))),
3941
_ => None,
4042
}
4143
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

gribberish/src/templates/data_representation/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod data_representation_template;
33
pub mod simple_packing_template;
44
pub mod complex_packing_template;
55
pub mod complex_spatial_packing_template;
6-
6+
pub mod ccsds_template;
77
#[cfg(feature = "jpeg")]
88
pub mod jpeg_template;
99

@@ -19,4 +19,6 @@ pub use complex_spatial_packing_template::ComplexSpatialPackingDataRepresentatio
1919
pub use jpeg_template::JPEGDataRepresentationTemplate;
2020

2121
#[cfg(feature = "png")]
22-
pub use png_template::PNGDataRepresentationTemplate;
22+
pub use png_template::PNGDataRepresentationTemplate;
23+
24+
pub use ccsds_template::CCSDSDataRepresentationTemplate;

0 commit comments

Comments
 (0)