revrt 0.1.1

A library for optimizing transmission infrastructure for electrical grid.
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
//! Samples to support tests that require Features
//!
//! All arrays are 2-D with shape `[ni, nj]` and dimension names `["y", "x"]`.
use std::sync::Arc;

use ndarray::Array2;
use object_store::local::LocalFileSystem;
use rand::RngExt;
use tempfile::TempDir;
use zarrs::array::{ArrayBuilder, DataType, FillValue};
use zarrs::array_subset::ArraySubset;
use zarrs::filesystem::FilesystemStore;
use zarrs::group::GroupBuilder;
use zarrs::storage::{AsyncReadableListableStorage, ReadableWritableListableStorage};
use zarrs_object_store::AsyncObjectStore;

// -----------------------------------------------------------------------
// Fill strategy
// -----------------------------------------------------------------------

/// How to populate the values of a single feature layer.
#[derive(Debug, Clone)]
pub(crate) enum FillStrategy {
    /// Every cell gets the same value.
    Constant(f32),
    /// Cells are numbered `1, 2, …, ni*nj` in row-major order.
    Sequential,
    /// Each cell receives an independent uniform random draw from `[min, max]`.
    Random(f32, f32),
    /// Values are derived from `(row, col)` via a user-supplied function.
    Custom(fn(u64, u64) -> f32),
    /// Use an explicit flat vector (must have length `ni * nj`).
    Values(Vec<f32>),
}

// -----------------------------------------------------------------------
// Data type
// -----------------------------------------------------------------------

/// Zarr data type to use when writing a feature layer.
///
/// Fill strategies are always expressed as `f32` values; this enum
/// controls only how those values are cast and stored on disk.
/// Floating-point NaN is used as the Zarr fill value for float types;
/// the type's maximum value (`i8::MAX`, `u32::MAX`, etc.) is used for
/// integer types, making unwritten cells easy to distinguish from valid data.
#[derive(Debug, Clone, Copy, Default)]
#[allow(dead_code)]
pub(crate) enum FeatureDataType {
    #[default]
    Float32,
    Float64,
    Int8,
    Int16,
    Int32,
    Int64,
    UInt8,
    UInt16,
    UInt32,
    UInt64,
}

impl FeatureDataType {
    fn zarrs_dtype(self) -> DataType {
        match self {
            Self::Float32 => DataType::Float32,
            Self::Float64 => DataType::Float64,
            Self::Int8 => DataType::Int8,
            Self::Int16 => DataType::Int16,
            Self::Int32 => DataType::Int32,
            Self::Int64 => DataType::Int64,
            Self::UInt8 => DataType::UInt8,
            Self::UInt16 => DataType::UInt16,
            Self::UInt32 => DataType::UInt32,
            Self::UInt64 => DataType::UInt64,
        }
    }

    fn fill_value(self) -> FillValue {
        match self {
            Self::Float32 => FillValue::from(zarrs::array::ZARR_NAN_F32),
            Self::Float64 => FillValue::from(zarrs::array::ZARR_NAN_F64),
            Self::Int8 => FillValue::from(i8::MAX),
            Self::Int16 => FillValue::from(i16::MAX),
            Self::Int32 => FillValue::from(i32::MAX),
            Self::Int64 => FillValue::from(i64::MAX),
            Self::UInt8 => FillValue::from(u8::MAX),
            Self::UInt16 => FillValue::from(u16::MAX),
            Self::UInt32 => FillValue::from(u32::MAX),
            Self::UInt64 => FillValue::from(u64::MAX),
        }
    }
}

// -----------------------------------------------------------------------
// Layer configuration
// -----------------------------------------------------------------------

/// Configuration for a single named feature layer.
#[derive(Debug, Clone)]
pub(crate) struct LayerConfig {
    /// Layer name, e.g. `"A"`, `"elevation"`, `"land_cover"`.
    pub(crate) name: String,
    /// Strategy used to fill this layer's values.
    pub(crate) fill: FillStrategy,
    /// Zarr element type to use when writing this layer (default: `Float32`).
    pub(crate) dtype: FeatureDataType,
}

impl LayerConfig {
    /// Create a layer with an arbitrary fill strategy and the default data type (`Float32`).
    pub(crate) fn new(name: impl Into<String>, fill: FillStrategy) -> Self {
        Self {
            name: name.into(),
            fill,
            dtype: FeatureDataType::default(),
        }
    }

    /// Override the Zarr data type for this layer.
    ///
    /// The fill strategy values are cast to the requested type at write
    /// time.  For integer types the conversion truncates toward zero, so
    /// keep fill values within the target type's range.
    pub(crate) fn with_dtype(mut self, dtype: FeatureDataType) -> Self {
        self.dtype = dtype;
        self
    }

    /// Constant-valued layer.
    pub(crate) fn constant(name: impl Into<String>, value: f32) -> Self {
        Self::new(name, FillStrategy::Constant(value))
    }

    /// Layer filled with `1.0` everywhere.
    pub(crate) fn ones(name: impl Into<String>) -> Self {
        Self::constant(name, 1.0)
    }

    /// Layer filled with `0.0` everywhere.
    #[allow(dead_code)]
    pub(crate) fn zeros(name: impl Into<String>) -> Self {
        Self::constant(name, 0.0)
    }

    /// Layer whose cells are numbered `1, 2, …, ni*nj` in row-major order.
    pub(crate) fn sequential(name: impl Into<String>) -> Self {
        Self::new(name, FillStrategy::Sequential)
    }

    /// Layer filled with independent uniform draws from `[min, max]`.
    pub(crate) fn random(name: impl Into<String>, min: f32, max: f32) -> Self {
        Self::new(name, FillStrategy::Random(min, max))
    }

    /// Layer whose values are computed cell-by-cell with `f(row, col)`.
    pub(crate) fn custom(name: impl Into<String>, f: fn(u64, u64) -> f32) -> Self {
        Self::new(name, FillStrategy::Custom(f))
    }
}

// -----------------------------------------------------------------------
// Builder
// -----------------------------------------------------------------------

/// Builds an in-memory-backed (temp-dir) Zarr store suitable for use with
/// [`Features`](super::Features).
///
/// Each layer is stored as a 2-D array with shape `[ni, nj]` and chunk
/// shape `[ci, cj]`, with dimension names `["y", "x"]`.  The element
/// type defaults to `Float32` but can be overridden per layer via
/// [`LayerConfig::with_dtype`].
///
/// # Example
/// ```rust
/// let (tmp, storage) = FeaturesTestBuilder::new()
///     .dimensions(8, 8)
///     .chunks(4, 4)
///     .layer(LayerConfig::ones("elevation"))
///     .layer(LayerConfig::sequential("land_cover"))
///     .build()
///     .unwrap();
/// ```
pub(crate) struct FeaturesTestBuilder {
    ni: u64,
    nj: u64,
    ci: u64,
    cj: u64,
    layers: Vec<LayerConfig>,
}

impl Default for FeaturesTestBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl FeaturesTestBuilder {
    /// Create a new builder with default dimensions `8×8` and chunk size `4×4`.
    pub(crate) fn new() -> Self {
        Self {
            ni: 8,
            nj: 8,
            ci: 4,
            cj: 4,
            layers: Vec::new(),
        }
    }

    /// Set array dimensions (rows, columns).
    pub(crate) fn dimensions(mut self, ni: u64, nj: u64) -> Self {
        self.ni = ni;
        self.nj = nj;
        self
    }

    /// Set chunk dimensions (rows, columns).
    pub(crate) fn chunks(mut self, ci: u64, cj: u64) -> Self {
        self.ci = ci;
        self.cj = cj;
        self
    }

    /// Add a single layer.
    pub(crate) fn layer(mut self, layer: LayerConfig) -> Self {
        self.layers.push(layer);
        self
    }

    /// Build the store.
    ///
    /// Returns the [`TempDir`] (keeping it alive keeps the files on disk)
    /// and an [`AsyncReadableListableStorage`] pointing at it.
    pub(crate) fn build(
        self,
    ) -> Result<(TempDir, AsyncReadableListableStorage), Box<dyn std::error::Error>> {
        let tmp = TempDir::new()?;

        let sync_store: ReadableWritableListableStorage =
            Arc::new(FilesystemStore::new(tmp.path())?);

        GroupBuilder::new()
            .build(sync_store.clone(), "/")?
            .store_metadata()?;

        for layer_config in &self.layers {
            self.write_layer(&sync_store, layer_config)?;
        }

        let async_store: AsyncReadableListableStorage = Arc::new(AsyncObjectStore::new(
            LocalFileSystem::new_with_prefix(tmp.path())?,
        ));

        Ok((tmp, async_store))
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    fn write_layer(
        &self,
        store: &ReadableWritableListableStorage,
        config: &LayerConfig,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let array = ArrayBuilder::new(
            vec![self.ni, self.nj],
            vec![self.ci, self.cj],
            config.dtype.zarrs_dtype(),
            config.dtype.fill_value(),
        )
        .dimension_names(["y", "x"].into())
        .build(store.clone(), &format!("/{}", config.name))?;

        array.store_metadata()?;

        let flat = self.generate_flat(&config.fill)?;
        let shape = (self.ni as usize, self.nj as usize);
        let subset = ArraySubset::new_with_ranges(&[
            0..self.ni.div_ceil(self.ci),
            0..self.nj.div_ceil(self.cj),
        ]);

        macro_rules! store_as {
            ($T:ty) => {{
                let data: Array2<$T> =
                    Array2::from_shape_vec(shape, flat.into_iter().map(|v| v as $T).collect())?;
                array.store_chunks_ndarray(&subset, data)?;
            }};
        }

        match config.dtype {
            FeatureDataType::Float32 => store_as!(f32),
            FeatureDataType::Float64 => store_as!(f64),
            FeatureDataType::Int8 => store_as!(i8),
            FeatureDataType::Int16 => store_as!(i16),
            FeatureDataType::Int32 => store_as!(i32),
            FeatureDataType::Int64 => store_as!(i64),
            FeatureDataType::UInt8 => store_as!(u8),
            FeatureDataType::UInt16 => store_as!(u16),
            FeatureDataType::UInt32 => store_as!(u32),
            FeatureDataType::UInt64 => store_as!(u64),
        }

        Ok(())
    }

    /// Generate the flat `f32` values described by `fill`.
    ///
    /// All fill strategies produce `f32` values.  The caller is
    /// responsible for casting to the target element type before writing.
    fn generate_flat(&self, fill: &FillStrategy) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
        let size = (self.ni * self.nj) as usize;

        let flat = match fill {
            FillStrategy::Constant(v) => vec![*v; size],

            FillStrategy::Sequential => (1..=size).map(|x| x as f32).collect(),

            FillStrategy::Random(min, max) => {
                let mut rng = rand::rng();
                (0..size).map(|_| rng.random_range(*min..=*max)).collect()
            }

            FillStrategy::Custom(f) => {
                let mut values = Vec::with_capacity(size);
                for i in 0..self.ni {
                    for j in 0..self.nj {
                        values.push(f(i, j));
                    }
                }
                values
            }

            FillStrategy::Values(vals) => {
                if vals.len() != size {
                    return Err(format!(
                        "Values length {} does not match array size {}",
                        vals.len(),
                        size
                    )
                    .into());
                }
                vals.clone()
            }
        };

        Ok(flat)
    }
}

// -----------------------------------------------------------------------
// Preset builders
// -----------------------------------------------------------------------

/// Preset: small 4×4 grid (2×2 chunks). Good for quick unit tests.
pub(crate) fn preset_small() -> FeaturesTestBuilder {
    FeaturesTestBuilder::new().dimensions(4, 4).chunks(2, 2)
}

/// Preset: medium 16×16 grid (4×4 chunks). Good for integration tests.
#[allow(dead_code)]
pub(crate) fn preset_medium() -> FeaturesTestBuilder {
    FeaturesTestBuilder::new().dimensions(16, 16).chunks(4, 4)
}

/// Preset: large 128×128 grid (32×32 chunks). Good for performance tests.
#[allow(dead_code)]
pub(crate) fn preset_large() -> FeaturesTestBuilder {
    FeaturesTestBuilder::new()
        .dimensions(128, 128)
        .chunks(32, 32)
}

// -----------------------------------------------------------------------
// Convenience functions
// -----------------------------------------------------------------------

/// Single layer of all-ones, 8×8 with 4×4 chunks.
///
/// Useful when the actual feature values are irrelevant to the test.
#[allow(dead_code)]
pub(crate) fn single_ones_layer(
    name: &str,
) -> Result<(TempDir, AsyncReadableListableStorage), Box<dyn std::error::Error>> {
    FeaturesTestBuilder::new()
        .layer(LayerConfig::ones(name))
        .build()
}

/// Classic three-layer setup (`A`, `B`, `C`) filled with sequential values.
///
/// Mirrors the spirit of `multi_variable_random` from the dataset samples,
/// but uses predictable deterministic values.
pub(crate) fn multi_variable_sequential(
    ni: u64,
    nj: u64,
    ci: u64,
    cj: u64,
) -> Result<(TempDir, AsyncReadableListableStorage), Box<dyn std::error::Error>> {
    FeaturesTestBuilder::new()
        .dimensions(ni, nj)
        .chunks(ci, cj)
        .layer(LayerConfig::sequential("A"))
        .layer(LayerConfig::sequential("B"))
        .layer(LayerConfig::sequential("C"))
        .build()
}

/// Three-layer setup (`A`, `B`, `C`) filled with independent random values.
///
/// Each call produces a different dataset. Prefer
/// [`multi_variable_sequential`] when reproducibility matters.
pub(crate) fn multi_variable_random(
    ni: u64,
    nj: u64,
    ci: u64,
    cj: u64,
) -> Result<(TempDir, AsyncReadableListableStorage), Box<dyn std::error::Error>> {
    FeaturesTestBuilder::new()
        .dimensions(ni, nj)
        .chunks(ci, cj)
        .layer(LayerConfig::random("A", 0.0, 1.0))
        .layer(LayerConfig::random("B", 0.0, 1.0))
        .layer(LayerConfig::random("C", 0.0, 1.0))
        .build()
}

// -----------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn builder_creates_store() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(4, 4)
            .chunks(2, 2)
            .layer(LayerConfig::ones("test"))
            .build()
            .unwrap();

        assert!(tmp.path().exists());
    }

    #[test]
    fn builder_multiple_layers_exist_on_disk() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(8, 8)
            .chunks(4, 4)
            .layer(LayerConfig::ones("A"))
            .layer(LayerConfig::sequential("B"))
            .layer(LayerConfig::constant("C", 5.0))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        for name in ["A", "B", "C"] {
            assert!(
                zarrs::array::Array::open(sync_store.clone(), &format!("/{name}")).is_ok(),
                "layer {name} should exist on disk"
            );
        }
    }

    #[test]
    fn builder_array_shape_is_2d() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(4, 6)
            .chunks(2, 3)
            .layer(LayerConfig::ones("elevation"))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/elevation").unwrap();
        assert_eq!(array.shape(), &[4, 6]);
    }

    #[test]
    fn builder_sequential_values() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(2, 3)
            .chunks(2, 3)
            .layer(LayerConfig::sequential("seq"))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/seq").unwrap();
        let subset = zarrs::array_subset::ArraySubset::new_with_ranges(&[0..2, 0..3]);
        let vals: Vec<f32> = array.retrieve_array_subset_elements(&subset).unwrap();
        // Sequential starts at 1
        let expected: Vec<f32> = (1..=6).map(|x| x as f32).collect();
        assert_eq!(vals, expected);
    }

    #[test]
    fn builder_custom_fill() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(3, 3)
            .chunks(3, 3)
            .layer(LayerConfig::custom("idx", |i, j| (i * 10 + j) as f32))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/idx").unwrap();
        let subset = zarrs::array_subset::ArraySubset::new_with_ranges(&[0..3, 0..3]);
        let vals: Vec<f32> = array.retrieve_array_subset_elements(&subset).unwrap();

        for i in 0..3u64 {
            for j in 0..3u64 {
                let expected = (i * 10 + j) as f32;
                assert_eq!(
                    vals[(i * 3 + j) as usize],
                    expected,
                    "mismatch at ({i},{j})"
                );
            }
        }
    }

    #[test]
    fn builder_values_wrong_length_errors() {
        let result = FeaturesTestBuilder::new()
            .dimensions(2, 2)
            .chunks(2, 2)
            .layer(LayerConfig::new(
                "bad",
                FillStrategy::Values(vec![1.0, 2.0]),
            )) // needs 4
            .build();

        assert!(result.is_err());
    }

    #[test]
    fn preset_small_produces_correct_shape() {
        let (tmp, _storage) = preset_small()
            .layer(LayerConfig::ones("A"))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/A").unwrap();
        assert_eq!(array.shape(), &[4, 4]);
    }

    #[tokio::test]
    async fn async_storage_opens_array() {
        let (_tmp, storage) = FeaturesTestBuilder::new()
            .dimensions(4, 4)
            .chunks(2, 2)
            .layer(LayerConfig::constant("temperature", 273.15))
            .build()
            .unwrap();

        let array = zarrs::array::Array::async_open(storage, "/temperature")
            .await
            .unwrap();
        assert_eq!(array.shape(), &[4, 4]);
    }

    #[tokio::test]
    async fn async_storage_retrieves_correct_values() {
        let (_tmp, storage) = FeaturesTestBuilder::new()
            .dimensions(4, 4)
            .chunks(2, 2)
            .layer(LayerConfig::constant("elev", 42.0))
            .build()
            .unwrap();

        let array = zarrs::array::Array::async_open(storage, "/elev")
            .await
            .unwrap();
        let chunk: Vec<f32> = array
            .async_retrieve_chunk_elements::<f32>(&[0, 0])
            .await
            .unwrap();
        assert!(chunk.iter().all(|&v| v == 42.0));
    }

    #[tokio::test]
    async fn multi_variable_sequential_has_three_layers() {
        let (_tmp, storage) = multi_variable_sequential(4, 4, 2, 2).unwrap();

        for name in ["A", "B", "C"] {
            let result =
                zarrs::array::Array::async_open(storage.clone(), &format!("/{name}")).await;
            assert!(result.is_ok(), "layer {name} should be openable");
        }
    }

    #[tokio::test]
    async fn multi_variable_random_layers_are_non_empty() {
        let (_tmp, storage) = multi_variable_random(4, 4, 2, 2).unwrap();

        let array = zarrs::array::Array::async_open(storage, "/A")
            .await
            .unwrap();
        let chunk: Vec<f32> = array
            .async_retrieve_chunk_elements::<f32>(&[0, 0])
            .await
            .unwrap();
        assert!(!chunk.is_empty());
    }

    // -----------------------------------------------------------------------
    // Data-type tests
    // -----------------------------------------------------------------------

    #[test]
    fn layer_dtype_float64_has_correct_zarr_type() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(4, 4)
            .chunks(2, 2)
            .layer(LayerConfig::ones("elev").with_dtype(FeatureDataType::Float64))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/elev").unwrap();
        assert!(matches!(array.data_type(), zarrs::array::DataType::Float64));
    }

    #[test]
    fn layer_dtype_int32_has_correct_zarr_type() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(4, 4)
            .chunks(2, 2)
            .layer(LayerConfig::sequential("band").with_dtype(FeatureDataType::Int32))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/band").unwrap();
        assert!(matches!(array.data_type(), zarrs::array::DataType::Int32));
    }

    #[test]
    fn layer_dtype_uint8_has_correct_zarr_type() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(4, 4)
            .chunks(2, 2)
            .layer(LayerConfig::constant("mask", 1.0).with_dtype(FeatureDataType::UInt8))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/mask").unwrap();
        assert!(matches!(array.data_type(), zarrs::array::DataType::UInt8));
    }

    #[test]
    fn layer_dtype_float64_stores_correct_values() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(2, 2)
            .chunks(2, 2)
            .layer(LayerConfig::constant("temp", 1.62).with_dtype(FeatureDataType::Float64))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/temp").unwrap();
        let subset = zarrs::array_subset::ArraySubset::new_with_ranges(&[0..2, 0..2]);
        let vals: Vec<f64> = array.retrieve_array_subset_elements(&subset).unwrap();
        for v in vals {
            let diff = (v - 1.62_f64).abs();
            // The fill value is specified as f32 (1.62f32 ≈ 1.6200001e0),
            // so the round-trip error vs the true f64 value is ~1e-7.
            assert!(diff < 1e-6, "expected ~1.62, got {v}");
        }
    }

    #[test]
    fn layer_dtype_int16_stores_correct_values() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(2, 3)
            .chunks(2, 3)
            .layer(LayerConfig::sequential("idx").with_dtype(FeatureDataType::Int16))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/idx").unwrap();
        let subset = zarrs::array_subset::ArraySubset::new_with_ranges(&[0..2, 0..3]);
        let vals: Vec<i16> = array.retrieve_array_subset_elements(&subset).unwrap();
        let expected: Vec<i16> = (1..=6).collect();
        assert_eq!(vals, expected);
    }

    #[test]
    fn layer_dtype_uint32_stores_correct_values() {
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(2, 2)
            .chunks(2, 2)
            .layer(LayerConfig::constant("cls", 255.0).with_dtype(FeatureDataType::UInt32))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let array = zarrs::array::Array::open(sync_store, "/cls").unwrap();
        let subset = zarrs::array_subset::ArraySubset::new_with_ranges(&[0..2, 0..2]);
        let vals: Vec<u32> = array.retrieve_array_subset_elements(&subset).unwrap();
        assert!(vals.iter().all(|&v| v == 255));
    }

    #[test]
    fn mixed_dtypes_in_same_store() {
        // A realistic scenario: float cost layer + integer classification layer.
        let (tmp, _storage) = FeaturesTestBuilder::new()
            .dimensions(4, 4)
            .chunks(2, 2)
            .layer(LayerConfig::random("cost", 0.0, 100.0)) // Float32 (default)
            .layer(LayerConfig::constant("land_cover", 3.0).with_dtype(FeatureDataType::UInt8))
            .layer(LayerConfig::sequential("elevation").with_dtype(FeatureDataType::Int16))
            .build()
            .unwrap();

        let sync_store = Arc::new(FilesystemStore::new(tmp.path()).unwrap());
        let cost_array = zarrs::array::Array::open(sync_store.clone(), "/cost").unwrap();
        let lc_array = zarrs::array::Array::open(sync_store.clone(), "/land_cover").unwrap();
        let elev_array = zarrs::array::Array::open(sync_store.clone(), "/elevation").unwrap();

        assert!(matches!(
            cost_array.data_type(),
            zarrs::array::DataType::Float32
        ));
        assert!(matches!(
            lc_array.data_type(),
            zarrs::array::DataType::UInt8
        ));
        assert!(matches!(
            elev_array.data_type(),
            zarrs::array::DataType::Int16
        ));
    }
}