oxigdal-netcdf
Pure Rust NetCDF-3 driver for OxiGDAL with CF (Climate and Forecast) conventions support and optional HDF5-based NetCDF-4.
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:
# Pure Rust NetCDF-3 (default, recommended)
[]
= "0.1"
# With CF conventions validation
[]
= { = "0.1", = ["cf_conventions"] }
# With async I/O support
[]
= { = "0.1", = ["async"] }
# With NetCDF-4/HDF5 support (requires system libraries)
# WARNING: Not Pure Rust - requires libnetcdf and libhdf5
[]
= { = "0.1", = ["netcdf4"] }
Quick Start
Reading a NetCDF File (Pure Rust)
use NetCdfReader;
Writing a NetCDF File (Pure Rust)
use ;
use Dimension;
use ;
use ;
CF Conventions Support
use NetCdfReader;
Async I/O (with async feature)
use NetCdfReader;
async
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
// File readers/writers
// Format versions
// Data types
// Dimension management
// Variables and attributes
// CF conventions
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:
Feature Flags
netcdf3(enabled by default) - Pure Rust NetCDF-3 supportcf_conventions- CF 1.8 compliance validationasync- Async I/O with tokionetcdf4- NetCDF-4/HDF5 support (requires system libraries, NOT Pure Rust)compression- Compression support (NetCDF-4 only)std(enabled by default) - Standard library supportalloc- 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)
use Nc4Reader;
Compression Settings
use ;
Examples
See the examples directory for complete working examples:
create_test_netcdf_samples.rs- Creating sample NetCDF files with various configurations
Run examples with:
Testing
Run the test suite:
# Test Pure Rust (NetCDF-3)
# Test with CF conventions
# Run doctests
Documentation
Full API documentation is available at docs.rs.
Key documentation:
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):
= { = "0.1", = ["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:
use Driver;
use NetCdfReader;
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)