tensogram 0.15.0

Fast binary N-tensor message format for scientific data — encode, decode, file I/O, streaming
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
// (C) Copyright 2026- ECMWF and individual contributors.
//
// This software is licensed under the terms of the Apache Licence Version 2.0
// which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
// In applying this licence, ECMWF does not waive the privileges and immunities
// granted to it by virtue of its status as an intergovernmental organisation nor
// does it submit to any jurisdiction.

//! Shared encoding/filter/compression pipeline helpers for converters.
//!
//! Both `tensogram-grib` and `tensogram-netcdf` accept the same set of CLI
//! flags (`--encoding` / `--bits` / `--filter` / `--compression` /
//! `--compression-level`) and translate them into
//! [`DataObjectDescriptor`] fields + `params` map entries in exactly the
//! same way. This module centralises that translation so the two
//! converters cannot drift out of sync.
//!
//! ## Usage
//!
//! ```no_run
//! use tensogram::pipeline::{apply_pipeline, DataPipeline};
//! use tensogram::types::{ByteOrder, DataObjectDescriptor};
//! use tensogram::Dtype;
//! use std::collections::BTreeMap;
//!
//! let mut desc = DataObjectDescriptor {
//!     obj_type: "ntensor".to_string(),
//!     ndim: 1,
//!     shape: vec![4],
//!     strides: vec![1],
//!     dtype: Dtype::Float64,
//!     byte_order: ByteOrder::Little,
//!     encoding: "none".to_string(),
//!     filter: "none".to_string(),
//!     compression: "none".to_string(),
//!     params: BTreeMap::new(),
//!     hash: None,
//! };
//!
//! let pipeline = DataPipeline {
//!     compression: "zstd".to_string(),
//!     ..Default::default()
//! };
//!
//! let values = [1.0_f64, 2.0, 3.0, 4.0];
//! apply_pipeline(&mut desc, Some(&values), &pipeline, "my_var").unwrap();
//! assert_eq!(desc.compression, "zstd");
//! ```

use ciborium::Value as CborValue;
use tensogram_encodings::simple_packing;

use crate::types::DataObjectDescriptor;

/// Encoding/filter/compression configuration for data objects.
///
/// Defaults to all `"none"` — produces uncompressed raw little-endian
/// payloads identical to the pre-pipeline behaviour. This is the shared
/// type used by both `tensogram-grib` and `tensogram-netcdf`; the two
/// crates re-export it from their own `lib.rs` for convenience.
#[derive(Debug, Clone)]
pub struct DataPipeline {
    /// Encoding stage: `"none"` (default) or `"simple_packing"`.
    pub encoding: String,
    /// Bits per value for `simple_packing`. Defaults to 16 when `None`.
    pub bits: Option<u32>,
    /// Filter stage: `"none"` (default) or `"shuffle"`.
    pub filter: String,
    /// Compression codec: `"none"` (default), `"zstd"`, `"lz4"`,
    /// `"blosc2"`, or `"szip"`.
    pub compression: String,
    /// Optional compression level (used by `zstd` and `blosc2`; ignored
    /// by other codecs).
    pub compression_level: Option<i32>,
}

impl Default for DataPipeline {
    fn default() -> Self {
        Self {
            encoding: "none".to_string(),
            bits: None,
            filter: "none".to_string(),
            compression: "none".to_string(),
            compression_level: None,
        }
    }
}

/// Apply a [`DataPipeline`] to a [`DataObjectDescriptor`] by setting its
/// `encoding` / `filter` / `compression` fields and populating `params`.
///
/// `values` carries the float64 payload when available — `simple_packing`
/// is a float64-only encoding, so this parameter is `Some(&[f64])` when
/// the caller has typed f64 values and `None` otherwise (e.g. integer
/// variables in a mixed NetCDF file). When `pipeline.encoding ==
/// "simple_packing"` but `values` is `None`, the encoding stage is
/// skipped with a stderr warning and the conversion continues with
/// `encoding = "none"`.
///
/// `var_label` is embedded in warning/error messages for human-readable
/// diagnostics — typically the variable name (`"temperature"`) or
/// something like `"GRIB message"`.
///
/// # Errors
///
/// Returns a human-readable error string when `pipeline.encoding`,
/// `pipeline.filter`, or `pipeline.compression` is not one of the
/// recognised values. Callers wrap this string into their own
/// converter-specific error type (`GribError::InvalidData` /
/// `NetcdfError::InvalidData`).
///
/// Soft failures (`simple_packing` rejecting `NaN`-containing data, or
/// `simple_packing` requested on a non-f64 variable) are reported as
/// stderr warnings and do NOT return an error — the variable falls
/// back to `encoding = "none"` and the conversion continues.
pub fn apply_pipeline(
    desc: &mut DataObjectDescriptor,
    values: Option<&[f64]>,
    pipeline: &DataPipeline,
    var_label: &str,
) -> Result<(), String> {
    // ── Encoding stage ─────────────────────────────────────────────────
    let mut applied_simple_packing = false;
    match pipeline.encoding.as_str() {
        "none" => {}
        "simple_packing" => match values {
            None => {
                eprintln!(
                    "warning: skipping simple_packing for {var_label} \
                     (not a float64 payload)"
                );
            }
            Some(values) => {
                let bits = pipeline.bits.unwrap_or(16);
                match simple_packing::compute_params(values, bits, 0) {
                    Ok(params) => {
                        desc.encoding = "simple_packing".to_string();
                        desc.params.insert(
                            "reference_value".to_string(),
                            CborValue::Float(params.reference_value),
                        );
                        desc.params.insert(
                            "binary_scale_factor".to_string(),
                            CborValue::Integer((i64::from(params.binary_scale_factor)).into()),
                        );
                        desc.params.insert(
                            "decimal_scale_factor".to_string(),
                            CborValue::Integer((i64::from(params.decimal_scale_factor)).into()),
                        );
                        desc.params.insert(
                            "bits_per_value".to_string(),
                            CborValue::Integer((i64::from(params.bits_per_value)).into()),
                        );
                        applied_simple_packing = true;
                    }
                    Err(e) => {
                        // Common cause: NaN values from unpacked fill_value.
                        // simple_packing rejects NaN; the variable falls
                        // back to encoding="none" so the conversion still
                        // succeeds.
                        eprintln!("warning: skipping simple_packing for {var_label}: {e}");
                    }
                }
            }
        },
        other => {
            return Err(format!(
                "unknown encoding '{other}'; expected 'none' or 'simple_packing'"
            ));
        }
    }

    // ── Filter stage ───────────────────────────────────────────────────
    match pipeline.filter.as_str() {
        "none" => {}
        "shuffle" => {
            desc.filter = "shuffle".to_string();
            // shuffle is run AFTER encoding by the pipeline, so the
            // element size is the *post-encoding* byte width:
            //   - simple_packing applied → ⌈bpv/8⌉
            //   - otherwise → native dtype byte width
            let element_size = if applied_simple_packing {
                let bpv = pipeline.bits.unwrap_or(16) as usize;
                bpv.div_ceil(8).max(1)
            } else {
                desc.dtype.byte_width()
            };
            desc.params.insert(
                "shuffle_element_size".to_string(),
                CborValue::Integer((element_size as i64).into()),
            );
        }
        other => {
            return Err(format!(
                "unknown filter '{other}'; expected 'none' or 'shuffle'"
            ));
        }
    }

    // ── Compression stage ──────────────────────────────────────────────
    match pipeline.compression.as_str() {
        "none" => {}
        "zstd" => {
            desc.compression = "zstd".to_string();
            let level = pipeline.compression_level.unwrap_or(3);
            desc.params.insert(
                "zstd_level".to_string(),
                CborValue::Integer((i64::from(level)).into()),
            );
        }
        "lz4" => {
            desc.compression = "lz4".to_string();
        }
        "blosc2" => {
            desc.compression = "blosc2".to_string();
            let clevel = pipeline.compression_level.unwrap_or(5);
            desc.params.insert(
                "blosc2_clevel".to_string(),
                CborValue::Integer((i64::from(clevel)).into()),
            );
            // Default sub-codec — users wanting a different one should
            // construct a `DataObjectDescriptor` manually.
            desc.params.insert(
                "blosc2_codec".to_string(),
                CborValue::Text("lz4".to_string()),
            );
        }
        "szip" => {
            desc.compression = "szip".to_string();
            // Sensible szip defaults consistent with the rest of the
            // codebase (see `tensogram` tests + examples).
            desc.params
                .insert("szip_rsi".to_string(), CborValue::Integer(128.into()));
            desc.params
                .insert("szip_block_size".to_string(), CborValue::Integer(16.into()));
            desc.params
                .insert("szip_flags".to_string(), CborValue::Integer(8.into()));
        }
        other => {
            return Err(format!(
                "unknown compression '{other}'; expected one of: none, zstd, lz4, blosc2, szip"
            ));
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::*;
    use crate::Dtype;
    use crate::types::ByteOrder;

    fn mk_desc() -> DataObjectDescriptor {
        DataObjectDescriptor {
            obj_type: "ntensor".to_string(),
            ndim: 1,
            shape: vec![4],
            strides: vec![1],
            dtype: Dtype::Float64,
            byte_order: ByteOrder::Little,
            encoding: "none".to_string(),
            filter: "none".to_string(),
            compression: "none".to_string(),
            params: BTreeMap::new(),
            hash: None,
        }
    }

    fn int_param(desc: &DataObjectDescriptor, key: &str) -> i64 {
        match desc.params.get(key) {
            Some(CborValue::Integer(i)) => {
                let n: i128 = (*i).into();
                n as i64
            }
            other => panic!("{key} not an integer: {other:?}"),
        }
    }

    // ── Defaults ────────────────────────────────────────────────────

    #[test]
    fn default_pipeline_is_all_none() {
        let p = DataPipeline::default();
        assert_eq!(p.encoding, "none");
        assert_eq!(p.filter, "none");
        assert_eq!(p.compression, "none");
        assert!(p.bits.is_none());
        assert!(p.compression_level.is_none());
    }

    #[test]
    fn default_pipeline_leaves_descriptor_unchanged() {
        let mut desc = mk_desc();
        let values = [1.0, 2.0, 3.0, 4.0];
        apply_pipeline(&mut desc, Some(&values), &DataPipeline::default(), "x").unwrap();
        assert_eq!(desc.encoding, "none");
        assert_eq!(desc.filter, "none");
        assert_eq!(desc.compression, "none");
        assert!(desc.params.is_empty());
    }

    // ── Encoding ────────────────────────────────────────────────────

    #[test]
    fn simple_packing_populates_four_params() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            encoding: "simple_packing".to_string(),
            bits: Some(16),
            ..Default::default()
        };
        let values = [0.0_f64, 1.0, 2.0, 3.0];
        apply_pipeline(&mut desc, Some(&values), &p, "test").unwrap();
        assert_eq!(desc.encoding, "simple_packing");
        assert_eq!(int_param(&desc, "bits_per_value"), 16);
        assert_eq!(int_param(&desc, "decimal_scale_factor"), 0);
        assert!(desc.params.contains_key("reference_value"));
        assert!(desc.params.contains_key("binary_scale_factor"));
    }

    #[test]
    fn simple_packing_with_no_values_skips_with_warning() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            encoding: "simple_packing".to_string(),
            ..Default::default()
        };
        apply_pipeline(&mut desc, None, &p, "int_var").unwrap();
        assert_eq!(desc.encoding, "none", "should skip, not set");
        assert!(desc.params.is_empty(), "no params should be inserted");
    }

    #[test]
    fn simple_packing_with_nan_values_skips_with_warning() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            encoding: "simple_packing".to_string(),
            ..Default::default()
        };
        let values = [1.0_f64, f64::NAN, 3.0];
        apply_pipeline(&mut desc, Some(&values), &p, "nan_var").unwrap();
        assert_eq!(desc.encoding, "none", "NaN → skip");
    }

    #[test]
    fn unknown_encoding_errors() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            encoding: "magic_packing".to_string(),
            ..Default::default()
        };
        let err = apply_pipeline(&mut desc, None, &p, "x").unwrap_err();
        assert!(err.contains("magic_packing"));
        assert!(err.contains("simple_packing"));
    }

    // ── Filter ──────────────────────────────────────────────────────

    #[test]
    fn shuffle_on_raw_f64_uses_native_byte_width() {
        let mut desc = mk_desc(); // f64 → 8 bytes
        let p = DataPipeline {
            filter: "shuffle".to_string(),
            ..Default::default()
        };
        apply_pipeline(&mut desc, None, &p, "x").unwrap();
        assert_eq!(desc.filter, "shuffle");
        assert_eq!(int_param(&desc, "shuffle_element_size"), 8);
    }

    #[test]
    fn shuffle_on_simple_packed_uses_post_pack_byte_width() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            encoding: "simple_packing".to_string(),
            bits: Some(16),
            filter: "shuffle".to_string(),
            ..Default::default()
        };
        let values = [0.0_f64, 1.0, 2.0, 3.0];
        apply_pipeline(&mut desc, Some(&values), &p, "x").unwrap();
        assert_eq!(desc.filter, "shuffle");
        assert_eq!(
            int_param(&desc, "shuffle_element_size"),
            2,
            "16-bit packed → 2-byte elements"
        );
    }

    #[test]
    fn shuffle_with_24bit_packing_rounds_up() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            encoding: "simple_packing".to_string(),
            bits: Some(24),
            filter: "shuffle".to_string(),
            ..Default::default()
        };
        let values = [0.0_f64, 1.0, 2.0, 3.0];
        apply_pipeline(&mut desc, Some(&values), &p, "x").unwrap();
        assert_eq!(int_param(&desc, "shuffle_element_size"), 3);
    }

    #[test]
    fn unknown_filter_errors() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            filter: "wibble".to_string(),
            ..Default::default()
        };
        let err = apply_pipeline(&mut desc, None, &p, "x").unwrap_err();
        assert!(err.contains("wibble"));
    }

    // ── Compression ─────────────────────────────────────────────────

    #[test]
    fn zstd_with_default_level() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            compression: "zstd".to_string(),
            ..Default::default()
        };
        apply_pipeline(&mut desc, None, &p, "x").unwrap();
        assert_eq!(desc.compression, "zstd");
        assert_eq!(int_param(&desc, "zstd_level"), 3);
    }

    #[test]
    fn zstd_with_custom_level() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            compression: "zstd".to_string(),
            compression_level: Some(9),
            ..Default::default()
        };
        apply_pipeline(&mut desc, None, &p, "x").unwrap();
        assert_eq!(int_param(&desc, "zstd_level"), 9);
    }

    #[test]
    fn lz4_has_no_params() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            compression: "lz4".to_string(),
            ..Default::default()
        };
        apply_pipeline(&mut desc, None, &p, "x").unwrap();
        assert_eq!(desc.compression, "lz4");
        assert!(desc.params.is_empty());
    }

    #[test]
    fn blosc2_with_custom_level() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            compression: "blosc2".to_string(),
            compression_level: Some(7),
            ..Default::default()
        };
        apply_pipeline(&mut desc, None, &p, "x").unwrap();
        assert_eq!(desc.compression, "blosc2");
        assert_eq!(int_param(&desc, "blosc2_clevel"), 7);
        match desc.params.get("blosc2_codec") {
            Some(CborValue::Text(s)) => assert_eq!(s, "lz4"),
            other => panic!("blosc2_codec should be lz4: {other:?}"),
        }
    }

    #[test]
    fn szip_sets_defaults() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            compression: "szip".to_string(),
            ..Default::default()
        };
        apply_pipeline(&mut desc, None, &p, "x").unwrap();
        assert_eq!(desc.compression, "szip");
        assert_eq!(int_param(&desc, "szip_rsi"), 128);
        assert_eq!(int_param(&desc, "szip_block_size"), 16);
        assert_eq!(int_param(&desc, "szip_flags"), 8);
    }

    #[test]
    fn unknown_compression_errors() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            compression: "bogus".to_string(),
            ..Default::default()
        };
        let err = apply_pipeline(&mut desc, None, &p, "x").unwrap_err();
        assert!(err.contains("bogus"));
    }

    // ── Combined ────────────────────────────────────────────────────

    #[test]
    fn full_pipeline_simple_packing_shuffle_zstd() {
        let mut desc = mk_desc();
        let p = DataPipeline {
            encoding: "simple_packing".to_string(),
            bits: Some(24),
            filter: "shuffle".to_string(),
            compression: "zstd".to_string(),
            compression_level: Some(5),
        };
        let values = [1.0_f64, 2.0, 3.0, 4.0];
        apply_pipeline(&mut desc, Some(&values), &p, "x").unwrap();
        assert_eq!(desc.encoding, "simple_packing");
        assert_eq!(desc.filter, "shuffle");
        assert_eq!(desc.compression, "zstd");
        assert_eq!(int_param(&desc, "bits_per_value"), 24);
        assert_eq!(int_param(&desc, "shuffle_element_size"), 3);
        assert_eq!(int_param(&desc, "zstd_level"), 5);
    }
}