Expand description
Rust bindings for Unidata’s libnetcdf
This crate allows one to store and retrieve multi-dimensional arrays from a
netCDF
supported format, which can be a netCDF
file, a subset of hdf5
files,
or from a DAP url.
netCDF
files are self-contained, they contain metadata about the data contained in them.
See the CF Conventions
for conventions used for climate and
forecast models.
To explore the documentation, see the Functions
section, in particular
open()
, create()
, and append()
.
For more information see:
Examples
How to read a variable from a file:
// Open the file `simple_xy.nc`:
let file = netcdf::open("simple_xy.nc")?;
// Get the variable in this file with the name "data"
let var = &file.variable("data").expect("Could not find variable 'data'");
// Read a single datapoint from the variable as a numeric type
let data_i32 = var.value::<i32>(None)?;
let data_f32 : f32 = var.value(None)?;
// If your variable is multi-dimensional you might want to get a certain index
let data_i32 = var.value::<i32>(Some(&[40, 0, 0]))?;
// You can use `values()` to get all the data from the variable.
// Passing `None` as both arguments will give you the entire slice
let data = var.values::<i32>(None, None)?;
// A subset can also be selected, the following will extract the slice at
// `(40, 0, 0)` and get a dataset of size `100, 100` from this
let data = var.values::<i32>(Some(&[40, 0 ,0]), Some(&[1, 100, 100]))?;
How to create a new file and write to it:
// Create a new file with default settings
let mut file = netcdf::create("crabs.nc")?;
// We must create a dimension which corresponds to our data
file.add_dimension("ncrabs", 10)?;
// These dimensions can also be unlimited and will be resized when writing
file.add_unlimited_dimension("time")?;
// A variable can now be declared, and must be created from the dimension names.
let mut var = file.add_variable::<i32>(
"crab_coolness_level",
&["time", "ncrabs"],
)?;
// Metadata can be added to the variable
var.add_attribute("units", "Kelvin");
var.add_attribute("add_offset", 273.15_f32);
// Data can then be created and added to the variable
let data : Vec<i32> = vec![42; 10];
var.put_values(&data, Some(&[0, 0]), None);
// (This puts data at offset (0, 0) until all the data has been consumed)
// Values can be added along the unlimited dimension, which
// resizes along the `time` axis
var.put_values(&data, Some(&[1, 0]), None);
Re-exports
Modules
Add and read attributes from netcdf groups and variables
Interact with netcdf dimensions
Errors that can appear when interacting with netcdf files. This module contains conversion traits and the result type used in this crate.
Open, create, and append netcdf files
All netcdf items belong in the root group, which can be interacted with to get the underlying data
Contains functions and enums describing variable types
Variables in the netcdf file
Functions
Open a netCDF
file in append mode
Open a netCDF
file in append mode with the given options
Open a netcdf file in create mode
Open a netCDF
file in create mode with the given options
Open a netCDF
file in read mode
Open a netCDF
file in read mode with the given options