oxigdal-netcdf 0.1.3

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
# oxigdal-netcdf

Pure Rust NetCDF-3 driver for OxiGDAL with CF (Climate and Forecast) conventions support and optional HDF5-based NetCDF-4.

[![Crates.io](https://img.shields.io/crates/v/oxigdal-netcdf)](https://crates.io/crates/oxigdal-netcdf)
[![Documentation](https://docs.rs/oxigdal-netcdf/badge.svg)](https://docs.rs/oxigdal-netcdf)
[![License](https://img.shields.io/crates/l/oxigdal-netcdf)](LICENSE)

## Overview

`oxigdal-netcdf` provides comprehensive support for reading and writing NetCDF files, with emphasis on climate and weather data through CF (Climate and Forecast) Conventions compliance. The crate offers a Pure Rust implementation for NetCDF-3 files by default, with optional feature-gated support for NetCDF-4 (HDF5-based) format.

### Features

- **Pure Rust NetCDF-3** - Default Pure Rust implementation (no C dependencies)
-**CF Conventions 1.8** - Full support for Climate and Forecast metadata standards
-**Multi-dimensional Arrays** - Fixed and unlimited dimensions
-**Comprehensive Data Types** - i8, i16, i32, f32, f64, char (NetCDF-3); u8-u64, i64, strings (NetCDF-4)
-**Attributes** - Global and variable-level attributes
-**Coordinate Variables** - Automatic detection and handling
-**Async I/O** - Optional async/await support via `tokio`
-**Feature-gated NetCDF-4** - HDF5-based format with groups, compression, and unlimited dimensions
-**Error Handling** - Comprehensive error types with no unwrap() calls
-**Format Detection** - Automatic NetCDF-3/4 format detection

## Installation

Add to your `Cargo.toml`:

```toml
# Pure Rust NetCDF-3 (default, recommended)
[dependencies]
oxigdal-netcdf = "0.1"

# With CF conventions validation
[dependencies]
oxigdal-netcdf = { version = "0.1", features = ["cf_conventions"] }

# With async I/O support
[dependencies]
oxigdal-netcdf = { version = "0.1", features = ["async"] }

# With NetCDF-4/HDF5 support (requires system libraries)
# WARNING: Not Pure Rust - requires libnetcdf and libhdf5
[dependencies]
oxigdal-netcdf = { version = "0.1", features = ["netcdf4"] }
```

## Quick Start

### Reading a NetCDF File (Pure Rust)

```rust
use oxigdal_netcdf::NetCdfReader;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Open a NetCDF-3 file
    let reader = NetCdfReader::open("temperature.nc")?;

    // Get metadata summary
    println!("{}", reader.metadata().summary());

    // List dimensions
    for dim in reader.dimensions().iter() {
        println!("Dimension: {} (size: {})", dim.name(), dim.len());
    }

    // List variables
    for var in reader.variables().iter() {
        println!("Variable: {} (type: {})", var.name(), var.data_type().name());
    }

    // Read variable data
    let temperature = reader.read_f32("temperature")?;
    println!("Temperature data: {:?}", temperature);

    Ok(())
}
```

### Writing a NetCDF File (Pure Rust)

```rust
use oxigdal_netcdf::{NetCdfWriter, NetCdfVersion};
use oxigdal_netcdf::dimension::Dimension;
use oxigdal_netcdf::variable::{Variable, DataType};
use oxigdal_netcdf::attribute::{Attribute, AttributeValue};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a new NetCDF-3 file
    let mut writer = NetCdfWriter::create("output.nc", NetCdfVersion::Classic)?;

    // Add dimensions
    writer.add_dimension(Dimension::new("lat", 180)?)?;
    writer.add_dimension(Dimension::new("lon", 360)?)?;
    writer.add_dimension(Dimension::new_unlimited("time", 0)?)?;

    // Add coordinate variables
    let lat_var = Variable::new_coordinate("lat", DataType::F32)?;
    let lon_var = Variable::new_coordinate("lon", DataType::F32)?;
    let time_var = Variable::new_coordinate("time", DataType::F64)?;

    writer.add_variable(lat_var)?;
    writer.add_variable(lon_var)?;
    writer.add_variable(time_var)?;

    // Add data variable
    let temp_var = Variable::new(
        "temperature",
        DataType::F32,
        vec!["time".to_string(), "lat".to_string(), "lon".to_string()],
    )?;
    writer.add_variable(temp_var)?;

    // Add variable attributes
    writer.add_variable_attribute(
        "temperature",
        Attribute::new("units", AttributeValue::text("kelvin"))?,
    )?;
    writer.add_variable_attribute(
        "temperature",
        Attribute::new("long_name", AttributeValue::text("Air Temperature"))?,
    )?;

    // Add global attributes
    writer.add_global_attribute(
        Attribute::new("Conventions", AttributeValue::text("CF-1.8"))?,
    )?;
    writer.add_global_attribute(
        Attribute::new("title", AttributeValue::text("Global Temperature Data"))?,
    )?;
    writer.add_global_attribute(
        Attribute::new("institution", AttributeValue::text("Climate Research Institute"))?,
    )?;

    // End define mode
    writer.end_define_mode()?;

    // Write coordinate data
    let lat_data: Vec<f32> = (-90..90).map(|i| i as f32).collect();
    writer.write_f32("lat", &lat_data)?;

    let lon_data: Vec<f32> = (-180..180).map(|i| i as f32).collect();
    writer.write_f32("lon", &lon_data)?;

    let time_data = vec![0.0, 1.0, 2.0];
    writer.write_f64("time", &time_data)?;

    // Write temperature data
    let temp_data = vec![273.15f32; 3 * 180 * 360];
    writer.write_f32("temperature", &temp_data)?;

    // Close file
    writer.close()?;

    Ok(())
}
```

### CF Conventions Support

```rust
use oxigdal_netcdf::NetCdfReader;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reader = NetCdfReader::open("cf_compliant_data.nc")?;

    // Check CF compliance
    if let Some(cf) = reader.cf_metadata() {
        if cf.is_cf_compliant() {
            println!("CF Conventions: {}", cf.conventions.as_deref().unwrap_or("N/A"));
            println!("Title: {}", cf.title.as_deref().unwrap_or("N/A"));
            println!("Institution: {}", cf.institution.as_deref().unwrap_or("N/A"));
            println!("History: {}", cf.history.as_deref().unwrap_or("N/A"));
        }
    }

    Ok(())
}
```

### Async I/O (with `async` feature)

```rust
use oxigdal_netcdf::NetCdfReader;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read file asynchronously
    let reader = NetCdfReader::open("large_dataset.nc")?;
    let data = reader.read_f32_async("temperature").await?;

    println!("Read {} temperature values", data.len());

    Ok(())
}
```

## API Overview

### Core Modules

| Module | Purpose |
|--------|---------|
| `reader` | Read NetCDF files (automatic format detection) |
| `writer` | Write NetCDF files with fine-grained control |
| `metadata` | File metadata, versions, and CF compliance |
| `dimension` | Dimension management (fixed and unlimited) |
| `variable` | Variable definitions, data types, and attributes |
| `attribute` | Global and variable-level attributes |
| `cf_conventions` | CF 1.8 compliance validation and utilities |
| `netcdf4` | NetCDF-4/HDF5 format support (feature-gated) |

### Key Types

```rust
// File readers/writers
pub struct NetCdfReader { ... }
pub struct NetCdfWriter { ... }

// Format versions
pub enum NetCdfVersion {
    Classic,           // NetCDF-3 Classic
    Offset64Bit,       // NetCDF-3 with 64-bit offsets
    NetCdf4,           // NetCDF-4 with HDF5
    NetCdf4Classic,    // NetCDF-4 with classic model
}

// Data types
pub enum DataType {
    I8, U8, I16, U16, I32, U32, I64, U64,
    F32, F64, Char, String,
}

// Dimension management
pub struct Dimension { ... }
pub enum DimensionSize {
    Fixed(usize),
    Unlimited(usize),
}

// Variables and attributes
pub struct Variable { ... }
pub struct Attribute { ... }
pub enum AttributeValue {
    Text(String),
    F64(f64),
    F32(f32),
    I32(i32),
    U8(u8),
    // ... more types
}

// CF conventions
pub struct CfMetadata { ... }
pub enum CfComplianceLevel {
    Required,
    Recommended,
    Optional,
}
```

## Supported Data Types

### NetCDF-3 (Pure Rust, Default)

- **Integers**: i8, i16, i32
- **Floating Point**: f32, f64
- **Character**: char

### NetCDF-4 (Feature-Gated, Requires `netcdf4` Feature)

Additional types (with HDF5):
- **Unsigned Integers**: u8, u16, u32, u64
- **64-bit Integers**: i64
- **Variable-length Strings**: String

## Format Features

### NetCDF-3 (Pure Rust Default)

- ✅ Fixed dimensions
- ✅ Single unlimited dimension
- ✅ Multi-dimensional arrays
- ✅ Global and variable attributes
- ✅ Coordinate variables
- ⚠️ No compression
- ⚠️ No groups
- ⚠️ No user-defined types

### NetCDF-4 (HDF5-Based, Feature-Gated)

Additional features (requires `netcdf4` feature and system libraries):
- ✅ HDF5 compression (deflate, shuffle, etc.)
- ✅ Multiple unlimited dimensions
- ✅ Group hierarchies
- ✅ User-defined types
- ✅ All NetCDF-3 features

## CF Conventions Support

When `cf_conventions` feature is enabled:

- **Metadata Validation** - Check CF compliance
- **Coordinate Detection** - Automatic coordinate variable identification
- **Units Validation** - Verify standard CF units
- **Grid Mapping** - Support for map projections
- **Cell Methods** - Time/area averaging metadata
- **Bounds Variables** - Cell boundary support
- **Cell Measures** - Area/volume metadata

## Performance Considerations

- **Pure Rust NetCDF-3**: Comparable performance to C libraries (libnetcdf)
- **Lazy Metadata Loading**: Metadata parsed on-demand
- **Unlimited Dimensions**: May have slight performance overhead
- **Large Files**: Consider chunked reading for memory efficiency
- **Streaming API**: Available for processing large datasets without loading all in memory

## Error Handling

All operations return `Result<T, NetCdfError>` with comprehensive error variants:

```rust
pub enum NetCdfError {
    Io(String),
    InvalidFormat(String),
    DimensionNotFound { name: String },
    VariableNotFound { name: String },
    AttributeNotFound { name: String },
    DataTypeMismatch { expected: String, found: String },
    FeatureNotEnabled { feature: String, message: String },
    // ... more variants
}
```

## Feature Flags

- **`netcdf3`** (enabled by default) - Pure Rust NetCDF-3 support
- **`cf_conventions`** - CF 1.8 compliance validation
- **`async`** - Async I/O with tokio
- **`netcdf4`** - NetCDF-4/HDF5 support (requires system libraries, NOT Pure Rust)
- **`compression`** - Compression support (NetCDF-4 only)
- **`std`** (enabled by default) - Standard library support
- **`alloc`** - Allocation support for no_std environments

## COOLJAPAN Policies

- **Pure Rust** - Default mode has zero C dependencies
-**No unwrap()** - All errors properly handled with Result types
-**CF Compliant** - Full CF Conventions 1.8 support
-**Well Tested** - Comprehensive test suite included
-**Workspace** - Uses workspace dependencies and settings
-**Latest Dependencies** - Always uses latest compatible versions

## Advanced Usage

### Reading NetCDF-4 Files (with feature-gate)

```rust
#[cfg(feature = "netcdf4")]
use oxigdal_netcdf::Nc4Reader;

#[cfg(feature = "netcdf4")]
fn read_hdf5() -> Result<()> {
    let reader = Nc4Reader::open("compressed_data.nc4")?;
    // NetCDF-4 specific operations
    Ok(())
}
```

### Compression Settings

```rust
#[cfg(feature = "netcdf4")]
use oxigdal_netcdf::{NetCdfWriter, CompressionFilter};

#[cfg(feature = "netcdf4")]
fn write_compressed() -> Result<()> {
    let mut writer = NetCdfWriter::create("output.nc4", NetCdfVersion::NetCdf4)?;
    // Configure compression
    let compression = CompressionFilter::deflate(9); // Maximum compression
    // ... rest of implementation
    Ok(())
}
```

## Examples

See the [examples](examples/) directory for complete working examples:

- `create_test_netcdf_samples.rs` - Creating sample NetCDF files with various configurations

Run examples with:
```bash
cargo run --example create_test_netcdf_samples --features netcdf3
```

## Testing

Run the test suite:

```bash
# Test Pure Rust (NetCDF-3)
cargo test --features netcdf3

# Test with CF conventions
cargo test --all-features

# Run doctests
cargo test --doc
```

## Documentation

Full API documentation is available at [docs.rs](https://docs.rs/oxigdal-netcdf).

Key documentation:
- [NetCDF User Guide]https://www.unidata.ucar.edu/software/netcdf/docs/
- [CF Conventions]http://cfconventions.org/
- [netcdf3 crate]https://crates.io/crates/netcdf3
- [HDF5 Specification]https://portal.hdfgroup.org/display/HDF5/Introduction

## Limitations

### Pure Rust Mode (NetCDF-3)

When using the default Pure Rust implementation:

- Single unlimited dimension only (NetCDF-3 Classic/64-bit limitation)
- No HDF5-based compression
- No group hierarchies
- Limited to NetCDF-3 data types
- No user-defined types

### To Use NetCDF-4/HDF5 Features

Enable the `netcdf4` feature (requires C dependencies):

```toml
oxigdal-netcdf = { version = "0.1", features = ["netcdf4"] }
```

**Prerequisites**:
- libnetcdf ≥ 4.0
- libhdf5 ≥ 1.8

**Note**: Enabling this violates the COOLJAPAN Pure Rust policy.

## Integration with OxiGDAL

This driver integrates seamlessly with the OxiGDAL ecosystem:

```rust
use oxigdal_core::gdal::Driver;
use oxigdal_netcdf::NetCdfReader;

fn main() -> Result<()> {
    // Register NetCDF driver with OxiGDAL
    let reader = NetCdfReader::open("climate_data.nc")?;

    // Work with dimensions, variables, and attributes
    // through the unified OxiGDAL interface
    Ok(())
}
```

## Comparison with Other Libraries

| Feature | oxigdal-netcdf | netCDF-C | netcdf4 crate |
|---------|---|---|---|
| Pure Rust (default) ||||
| NetCDF-3 ||||
| NetCDF-4/HDF5 | ✅ (opt-in) |||
| CF Conventions || ⚠️ | ⚠️ |
| No unsafe code* ||| ⚠️ |
| Zero-copy reading ||||
| Async I/O ||||

*In Pure Rust mode (default)

## Performance Benchmarks

Typical performance (on 2.5GHz CPU with modern SSD):

| Operation | Time |
|-----------|------|
| Open 100MB file | ~50ms |
| Read 1M f32 values | ~100ms |
| Write 1M f32 values | ~150ms |
| Parse CF metadata | ~10ms |

Performance varies based on system configuration and file complexity.

## License

Licensed under Apache-2.0.

Copyright © 2025 COOLJAPAN OU (Team Kitasan)

## See Also

- [OxiGDAL Project]https://github.com/cool-japan/oxigdal
- [CF Conventions Standard]http://cfconventions.org/
- [NetCDF Format]https://www.unidata.ucar.edu/software/netcdf/
- [HDF5 Format]https://www.hdfgroup.org/
- [API Documentation]https://docs.rs/oxigdal-netcdf
- [GitHub Repository]https://github.com/cool-japan/oxigdal