oxigdal-netcdf 0.1.6

NetCDF driver for OxiGDAL - Pure Rust NetCDF-3 with optional NetCDF-4 support
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
//! NetCDF variable types and utilities.
//!
//! Variables store the actual data in NetCDF files. They have dimensions,
//! attributes, and a data type.

use serde::{Deserialize, Serialize};

use crate::attribute::Attributes;
use crate::dimension::Dimensions;
use crate::error::{NetCdfError, Result};
use oxigdal_core::error::OxiGdalError;

/// Data types supported by NetCDF.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DataType {
    /// 8-bit signed integer
    I8,
    /// 8-bit unsigned integer
    U8,
    /// 16-bit signed integer
    I16,
    /// 16-bit unsigned integer (NetCDF-4 only)
    U16,
    /// 32-bit signed integer
    I32,
    /// 32-bit unsigned integer (NetCDF-4 only)
    U32,
    /// 64-bit signed integer (NetCDF-4 only)
    I64,
    /// 64-bit unsigned integer (NetCDF-4 only)
    U64,
    /// 32-bit floating point
    F32,
    /// 64-bit floating point
    F64,
    /// Text/character
    Char,
    /// String (NetCDF-4 only)
    String,
}

impl DataType {
    /// Get the size in bytes.
    #[must_use]
    pub const fn size(&self) -> usize {
        match self {
            Self::I8 | Self::U8 | Self::Char => 1,
            Self::I16 | Self::U16 => 2,
            Self::I32 | Self::U32 | Self::F32 => 4,
            Self::I64 | Self::U64 | Self::F64 => 8,
            Self::String => 0, // Variable size
        }
    }

    /// Get the type name.
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::I8 => "i8",
            Self::U8 => "u8",
            Self::I16 => "i16",
            Self::U16 => "u16",
            Self::I32 => "i32",
            Self::U32 => "u32",
            Self::I64 => "i64",
            Self::U64 => "u64",
            Self::F32 => "f32",
            Self::F64 => "f64",
            Self::Char => "char",
            Self::String => "string",
        }
    }

    /// Check if this is a floating point type.
    #[must_use]
    pub const fn is_float(&self) -> bool {
        matches!(self, Self::F32 | Self::F64)
    }

    /// Check if this is an integer type.
    #[must_use]
    pub const fn is_integer(&self) -> bool {
        matches!(
            self,
            Self::I8
                | Self::U8
                | Self::I16
                | Self::U16
                | Self::I32
                | Self::U32
                | Self::I64
                | Self::U64
        )
    }

    /// Check if this is a signed type.
    #[must_use]
    pub const fn is_signed(&self) -> bool {
        matches!(
            self,
            Self::I8 | Self::I16 | Self::I32 | Self::I64 | Self::F32 | Self::F64
        )
    }

    /// Check if this is available in NetCDF-3.
    #[must_use]
    pub const fn is_netcdf3_compatible(&self) -> bool {
        matches!(
            self,
            Self::I8 | Self::I16 | Self::I32 | Self::F32 | Self::F64 | Self::Char
        )
    }
}

/// A variable in a NetCDF file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Variable {
    /// Name of the variable
    name: String,
    /// Data type
    data_type: DataType,
    /// Dimension names (in order)
    dimension_names: Vec<String>,
    /// Attributes
    attributes: Attributes,
    /// Whether this is a coordinate variable
    is_coordinate: bool,
}

impl Variable {
    /// Create a new variable.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the variable
    /// * `data_type` - Data type
    /// * `dimension_names` - Names of dimensions (in order)
    ///
    /// # Errors
    ///
    /// Returns error if the name is empty.
    pub fn new(
        name: impl Into<String>,
        data_type: DataType,
        dimension_names: Vec<String>,
    ) -> Result<Self> {
        let name = name.into();
        if name.is_empty() {
            return Err(NetCdfError::Core(
                OxiGdalError::invalid_parameter_builder("name", "Variable name cannot be empty")
                    .with_operation("create_netcdf_variable")
                    .with_parameter("data_type", format!("{:?}", data_type))
                    .with_parameter("num_dimensions", dimension_names.len().to_string())
                    .with_suggestion("Provide a non-empty variable name")
                    .build(),
            ));
        }
        Ok(Self {
            name,
            data_type,
            dimension_names,
            attributes: Attributes::new(),
            is_coordinate: false,
        })
    }

    /// Create a coordinate variable.
    ///
    /// A coordinate variable has the same name as its dimension.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the variable and dimension
    /// * `data_type` - Data type
    ///
    /// # Errors
    ///
    /// Returns error if the name is empty.
    pub fn new_coordinate(name: impl Into<String>, data_type: DataType) -> Result<Self> {
        let name = name.into();
        if name.is_empty() {
            return Err(NetCdfError::Core(
                OxiGdalError::invalid_parameter_builder(
                    "name",
                    "Coordinate variable name cannot be empty",
                )
                .with_operation("create_coordinate_variable")
                .with_parameter("data_type", format!("{:?}", data_type))
                .with_suggestion("Provide a non-empty coordinate variable name")
                .build(),
            ));
        }
        let dimension_names = vec![name.clone()];
        Ok(Self {
            name,
            data_type,
            dimension_names,
            attributes: Attributes::new(),
            is_coordinate: true,
        })
    }

    /// Get the variable name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the data type.
    #[must_use]
    pub const fn data_type(&self) -> DataType {
        self.data_type
    }

    /// Get the dimension names.
    #[must_use]
    pub fn dimension_names(&self) -> &[String] {
        &self.dimension_names
    }

    /// Get the number of dimensions.
    #[must_use]
    pub fn ndims(&self) -> usize {
        self.dimension_names.len()
    }

    /// Check if this is a scalar variable (no dimensions).
    #[must_use]
    pub fn is_scalar(&self) -> bool {
        self.dimension_names.is_empty()
    }

    /// Check if this is a coordinate variable.
    #[must_use]
    pub const fn is_coordinate(&self) -> bool {
        self.is_coordinate
    }

    /// Set whether this is a coordinate variable.
    pub fn set_coordinate(&mut self, is_coordinate: bool) {
        self.is_coordinate = is_coordinate;
    }

    /// Get the attributes.
    #[must_use]
    pub const fn attributes(&self) -> &Attributes {
        &self.attributes
    }

    /// Get mutable access to attributes.
    pub fn attributes_mut(&mut self) -> &mut Attributes {
        &mut self.attributes
    }

    /// Get the shape based on dimensions.
    ///
    /// # Errors
    ///
    /// Returns error if any dimension is not found.
    pub fn shape(&self, dimensions: &Dimensions) -> Result<Vec<usize>> {
        self.dimension_names
            .iter()
            .map(|name| {
                dimensions
                    .get(name)
                    .map(|d| d.len())
                    .ok_or_else(|| NetCdfError::DimensionNotFound { name: name.clone() })
            })
            .collect()
    }

    /// Get the total size based on dimensions.
    ///
    /// # Errors
    ///
    /// Returns error if any dimension is not found or if size overflows.
    pub fn size(&self, dimensions: &Dimensions) -> Result<usize> {
        if self.is_scalar() {
            return Ok(1);
        }

        let shape = self.shape(dimensions)?;
        shape
            .iter()
            .try_fold(1usize, |acc, &size| acc.checked_mul(size))
            .ok_or_else(|| {
                NetCdfError::Core(
                    OxiGdalError::io_error_builder("NetCDF variable size overflow")
                        .with_operation("calculate_variable_size")
                        .with_parameter("variable", &self.name)
                        .with_parameter("ndims", self.dimension_names.len().to_string())
                        .with_suggestion(
                            "Variable dimensions result in size overflow. Check dimension sizes",
                        )
                        .build(),
                )
            })
    }

    /// Get the size in bytes based on dimensions.
    ///
    /// # Errors
    ///
    /// Returns error if any dimension is not found or if size overflows.
    pub fn size_bytes(&self, dimensions: &Dimensions) -> Result<usize> {
        let element_size = self.data_type.size();
        if element_size == 0 {
            return Err(NetCdfError::Core(
                OxiGdalError::not_supported_builder("Variable-length data type size calculation")
                    .with_operation("calculate_variable_size_bytes")
                    .with_parameter("variable", &self.name)
                    .with_parameter("data_type", format!("{:?}", self.data_type))
                    .with_suggestion("Variable-length types require special handling")
                    .build(),
            ));
        }

        let num_elements = self.size(dimensions)?;
        num_elements.checked_mul(element_size).ok_or_else(|| {
            NetCdfError::Core(
                OxiGdalError::io_error_builder("NetCDF variable byte size overflow")
                    .with_operation("calculate_variable_size_bytes")
                    .with_parameter("variable", &self.name)
                    .with_parameter("element_size", element_size.to_string())
                    .with_parameter("num_elements", num_elements.to_string())
                    .with_suggestion(
                        "Variable size exceeds maximum. Reduce dimensions or use chunking",
                    )
                    .build(),
            )
        })
    }

    /// Check if compatible with NetCDF-3.
    #[must_use]
    pub fn is_netcdf3_compatible(&self) -> bool {
        self.data_type.is_netcdf3_compatible()
    }
}

/// Collection of variables.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Variables {
    variables: Vec<Variable>,
}

impl Variables {
    /// Create a new empty variable collection.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            variables: Vec::new(),
        }
    }

    /// Create from a vector of variables.
    #[must_use]
    pub const fn from_vec(variables: Vec<Variable>) -> Self {
        Self { variables }
    }

    /// Add a variable.
    ///
    /// # Errors
    ///
    /// Returns error if a variable with the same name already exists.
    pub fn add(&mut self, variable: Variable) -> Result<()> {
        if self.contains(variable.name()) {
            return Err(NetCdfError::Core(
                OxiGdalError::invalid_parameter_builder("variable", "Variable already exists")
                    .with_operation("add_netcdf_variable")
                    .with_parameter("variable_name", variable.name())
                    .with_parameter("data_type", format!("{:?}", variable.data_type()))
                    .with_suggestion("Use a unique variable name or retrieve existing variable")
                    .build(),
            ));
        }
        self.variables.push(variable);
        Ok(())
    }

    /// Get a variable by name.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&Variable> {
        self.variables.iter().find(|v| v.name() == name)
    }

    /// Get a mutable reference to a variable by name.
    pub fn get_mut(&mut self, name: &str) -> Option<&mut Variable> {
        self.variables.iter_mut().find(|v| v.name() == name)
    }

    /// Get a variable by index.
    #[must_use]
    pub fn get_by_index(&self, index: usize) -> Option<&Variable> {
        self.variables.get(index)
    }

    /// Check if a variable exists.
    #[must_use]
    pub fn contains(&self, name: &str) -> bool {
        self.variables.iter().any(|v| v.name() == name)
    }

    /// Get the number of variables.
    #[must_use]
    pub fn len(&self) -> usize {
        self.variables.len()
    }

    /// Check if empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.variables.is_empty()
    }

    /// Get an iterator over variables.
    pub fn iter(&self) -> impl Iterator<Item = &Variable> {
        self.variables.iter()
    }

    /// Get names of all variables.
    #[must_use]
    pub fn names(&self) -> Vec<&str> {
        self.variables.iter().map(|v| v.name()).collect()
    }

    /// Get coordinate variables.
    pub fn coordinates(&self) -> impl Iterator<Item = &Variable> {
        self.variables.iter().filter(|v| v.is_coordinate())
    }

    /// Get data variables (non-coordinate).
    pub fn data_variables(&self) -> impl Iterator<Item = &Variable> {
        self.variables.iter().filter(|v| !v.is_coordinate())
    }
}

impl IntoIterator for Variables {
    type Item = Variable;
    type IntoIter = std::vec::IntoIter<Variable>;

    fn into_iter(self) -> Self::IntoIter {
        self.variables.into_iter()
    }
}

impl<'a> IntoIterator for &'a Variables {
    type Item = &'a Variable;
    type IntoIter = std::slice::Iter<'a, Variable>;

    fn into_iter(self) -> Self::IntoIter {
        self.variables.iter()
    }
}

impl FromIterator<Variable> for Variables {
    fn from_iter<T: IntoIterator<Item = Variable>>(iter: T) -> Self {
        Self {
            variables: iter.into_iter().collect(),
        }
    }
}

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

    #[test]
    fn test_data_type_properties() {
        assert_eq!(DataType::F32.size(), 4);
        assert_eq!(DataType::F64.size(), 8);
        assert!(DataType::F32.is_float());
        assert!(DataType::I32.is_integer());
        assert!(DataType::I32.is_signed());
        assert!(!DataType::U32.is_signed());
    }

    #[test]
    fn test_netcdf3_compatibility() {
        assert!(DataType::I8.is_netcdf3_compatible());
        assert!(DataType::I16.is_netcdf3_compatible());
        assert!(DataType::I32.is_netcdf3_compatible());
        assert!(DataType::F32.is_netcdf3_compatible());
        assert!(DataType::F64.is_netcdf3_compatible());
        assert!(!DataType::U16.is_netcdf3_compatible());
        assert!(!DataType::U32.is_netcdf3_compatible());
        assert!(!DataType::String.is_netcdf3_compatible());
    }

    #[test]
    fn test_variable_creation() {
        let var = Variable::new(
            "temperature",
            DataType::F32,
            vec!["time".to_string(), "lat".to_string(), "lon".to_string()],
        )
        .expect("Failed to create temperature variable");
        assert_eq!(var.name(), "temperature");
        assert_eq!(var.data_type(), DataType::F32);
        assert_eq!(var.ndims(), 3);
        assert!(!var.is_scalar());
        assert!(!var.is_coordinate());
    }

    #[test]
    fn test_coordinate_variable() {
        let var = Variable::new_coordinate("time", DataType::F64)
            .expect("Failed to create coordinate variable");
        assert_eq!(var.name(), "time");
        assert!(var.is_coordinate());
        assert_eq!(var.ndims(), 1);
        assert_eq!(var.dimension_names()[0], "time");
    }

    #[test]
    fn test_scalar_variable() {
        let var = Variable::new("global_average", DataType::F32, vec![])
            .expect("Failed to create scalar variable");
        assert!(var.is_scalar());
        assert_eq!(var.ndims(), 0);
    }

    #[test]
    fn test_variable_shape() {
        let mut dims = Dimensions::new();
        dims.add(Dimension::new("time", 10).expect("Failed to create time dimension"))
            .expect("Failed to add time dimension");
        dims.add(Dimension::new("lat", 180).expect("Failed to create lat dimension"))
            .expect("Failed to add lat dimension");
        dims.add(Dimension::new("lon", 360).expect("Failed to create lon dimension"))
            .expect("Failed to add lon dimension");

        let var = Variable::new(
            "temperature",
            DataType::F32,
            vec!["time".to_string(), "lat".to_string(), "lon".to_string()],
        )
        .expect("Failed to create temperature variable");

        let shape = var.shape(&dims).expect("Failed to get variable shape");
        assert_eq!(shape, vec![10, 180, 360]);

        let size = var.size(&dims).expect("Failed to get variable size");
        assert_eq!(size, 10 * 180 * 360);

        let size_bytes = var
            .size_bytes(&dims)
            .expect("Failed to get variable size in bytes");
        assert_eq!(size_bytes, 10 * 180 * 360 * 4);
    }

    #[test]
    fn test_variable_collection() {
        let mut vars = Variables::new();
        vars.add(
            Variable::new_coordinate("time", DataType::F64)
                .expect("Failed to create time coordinate variable"),
        )
        .expect("Failed to add time coordinate variable");
        vars.add(
            Variable::new("temperature", DataType::F32, vec!["time".to_string()])
                .expect("Failed to create temperature variable"),
        )
        .expect("Failed to add temperature variable");

        assert_eq!(vars.len(), 2);
        assert!(vars.contains("time"));
        assert!(vars.contains("temperature"));

        let coords: Vec<_> = vars.coordinates().collect();
        assert_eq!(coords.len(), 1);
        assert_eq!(coords[0].name(), "time");

        let data_vars: Vec<_> = vars.data_variables().collect();
        assert_eq!(data_vars.len(), 1);
        assert_eq!(data_vars[0].name(), "temperature");
    }

    #[test]
    fn test_empty_variable_name() {
        let result = Variable::new("", DataType::F32, vec![]);
        assert!(result.is_err());
    }

    #[test]
    fn test_duplicate_variable() {
        let mut vars = Variables::new();
        vars.add(
            Variable::new("test", DataType::F32, vec![]).expect("Failed to create test variable"),
        )
        .expect("Failed to add test variable");
        let result = vars.add(
            Variable::new("test", DataType::F64, vec![])
                .expect("Failed to create duplicate test variable"),
        );
        assert!(result.is_err());
    }
}