DataSet

Struct DataSet 

Source
pub struct DataSet { /* private fields */ }
Expand description

Allows to define the NetCDF-3 data sets

§Examples

§Define a data set

use std::rc::Rc;
use netcdf3::{DataSet, Dimension, DataType, InvalidDataSet};

const LATITUDE_DIM_SIZE: usize = 180;
const LONGITUDE_DIM_SIZE: usize = 360;
const TIME_DIM_SIZE: usize = 24;
const AIR_TEMPERATURE_VAR_LEN: usize = LATITUDE_DIM_SIZE * LONGITUDE_DIM_SIZE * TIME_DIM_SIZE;

// First create the data set
// -------------------------
let mut data_set = DataSet::new();

// Define global attributes
// ------------------------
data_set.add_global_attr_u8("title",        String::from("Air temperature measurements").into_bytes().to_vec()).unwrap();
data_set.add_global_attr_u8("Conventions",  String::from("CF-1.8").into_bytes().to_vec()).unwrap();

// Define dimensions
// -----------------
data_set.add_fixed_dim("latitude",          LATITUDE_DIM_SIZE).unwrap();
data_set.add_fixed_dim("longitude",         LONGITUDE_DIM_SIZE).unwrap();
data_set.set_unlimited_dim("time",          TIME_DIM_SIZE).unwrap();

// Define variables and their attributes
// -------------------------------------
// latitude
data_set.add_var_f32("latitude",            &["latitude"]).unwrap();
data_set.add_var_attr_u8("latitude",        "standard_name", String::from("latitude").into_bytes()).unwrap();
data_set.add_var_attr_u8("latitude",        "long_name", String::from("LATITUDE").into_bytes()).unwrap();
data_set.add_var_attr_u8("latitude",        "units", String::from("degrees_north").into_bytes()).unwrap();
data_set.add_var_attr_u8("latitude",        "axis", String::from("Y").into_bytes()).unwrap();
// longitude
data_set.add_var_f32("longitude",           &["longitude"]).unwrap();
data_set.add_var_attr_u8("longitude",       "standard_name", String::from("longitude").into_bytes()).unwrap();
data_set.add_var_attr_u8("longitude",       "long_name", String::from("LONGITUDE").into_bytes()).unwrap();
data_set.add_var_attr_u8("longitude",       "units", String::from("degrees_east").into_bytes()).unwrap();
data_set.add_var_attr_u8("longitude",       "axis", String::from("X").into_bytes()).unwrap();
// time
data_set.add_var_f32("time",                &["time"]).unwrap();
data_set.add_var_attr_u8("time",            "standard_name", String::from("time").into_bytes()).unwrap();
data_set.add_var_attr_u8("time",            "long_name", String::from("TIME").into_bytes()).unwrap();
data_set.add_var_attr_u8("time",            "units", String::from("hours since 1970-01-01 00:00:00").into_bytes()).unwrap();
data_set.add_var_attr_u8("time",            "calendar", String::from("gregorian").into_bytes()).unwrap();
data_set.add_var_attr_u8("time",            "axis", String::from("T").into_bytes()).unwrap();
// air_temperature
data_set.add_var_f64("air_temperature",     &["time", "latitude", "longitude"]).unwrap();
data_set.add_var_attr_u8("air_temperature", "standard_name", String::from("air_temperature").into_bytes()).unwrap();
data_set.add_var_attr_u8("air_temperature", "long_name", String::from("AIR TEMPERATURE").into_bytes()).unwrap();
data_set.add_var_attr_u8("air_temperature", "units", String::from("Celsius").into_bytes()).unwrap();

Implementations§

Source§

impl DataSet

Source

pub fn new() -> DataSet

Source

pub fn add_fixed_dim<T: AsRef<str>>( &mut self, dim_name: T, dim_size: usize, ) -> Result<(), InvalidDataSet>

Appends a new fixed size dimension in the dataset.

Returns a error if an other dimension with the same name is already defined.

Source

pub fn set_unlimited_dim<T: AsRef<str>>( &mut self, dim_name: T, dim_size: usize, ) -> Result<(), InvalidDataSet>

Initializes the unlimited size dimension of the dataset.

Returns a error if :

  1. the unlimited size is already defined
  2. if an other dimension with the same name is already defined
Source

pub fn num_dims(&self) -> usize

Returns the number of dimensions defined in the data set.

Source

pub fn has_dim(&self, dim_name: &str) -> bool

Returns :

  • true if the dimension is defined.
  • false otherwise.
Source

pub fn get_dim(&self, dim_name: &str) -> Option<Rc<Dimension>>

Returns a reference to the dimension.

Returns None if the dimension is not defined.

Source

pub fn get_dims(&self) -> Vec<Rc<Dimension>>

Returns the references of all the dimensions defined in the data set.

Source

pub fn dim_names(&self) -> Vec<String>

Returns the names all the dimensions defined in the data set.

Source

pub fn has_unlimited_dim(&self) -> bool

Returns true if the unlimited-size dimension exists.

Source

pub fn get_unlimited_dim(&self) -> Option<Rc<Dimension>>

Returns the unlimited-size dimension if it is defined, otherwise return None.

Returns None if the unlimited-size dimension does not exist.

Source

pub fn dim_size(&self, dim_name: &str) -> Option<usize>

Returns the length of the dimension.

Returns None if the dimension does not exist.

Source

pub fn dim_type(&self, dim_name: &str) -> Option<DimensionType>

Returns the type of the dimension (fixed-size or unlimited-size).

Returns None if the dimension does not exist.

Source

pub fn remove_dim( &mut self, dim_name: &str, ) -> Result<Rc<Dimension>, InvalidDataSet>

Removes and returns the dimension.

Returns an error if:

  • the dimension is not already defined
  • the dimension is yet used by a variable of the dataset
Source

pub fn rename_dim( &mut self, old_dim_name: &str, new_dim_name: &str, ) -> Result<(), InvalidDataSet>

Rename the dimension or return en error if :

  • no dimension named old_dim_name already exists
  • an other dimension named new_dim_name already exists
  • the new_dim_name is not a NetCDF-3 valid name

Nothing is done if old_dim_name and new_dim_name are the same.

Source

pub fn get_dims_from_dim_ids( &self, dim_ids: &[usize], ) -> Result<Vec<Rc<Dimension>>, InvalidDataSet>

Source

pub fn add_var<T: AsRef<str>>( &mut self, var_name: &str, dims_name: &[T], data_type: DataType, ) -> Result<(), InvalidDataSet>

Add a new variable in the dataset defined over named dimensions.

§Examples

Add a 2D variable

use netcdf3::{DataSet, DataType};

let mut data_set = DataSet::new();
let _ = data_set.add_fixed_dim("latitude", 181).unwrap();
let _ = data_set.add_fixed_dim("longitude", 361).unwrap();
let _ = data_set.set_unlimited_dim("time", 2).unwrap();

assert_eq!(0, data_set.num_vars());
let _ = data_set.add_var("sea_level_temperature", &["latitude", "longitude"], DataType::F64).unwrap();
assert_eq!(1, data_set.num_vars());

Add a scalar variable

use netcdf3::{DataSet, DataType};

const SCALAR_VAR_NAME: &str = "scalar_var";

let mut data_set = DataSet::new();

assert_eq!(0,                   data_set.num_vars());
assert_eq!(None,                data_set.var_len(SCALAR_VAR_NAME));

let _ = data_set.add_var(SCALAR_VAR_NAME, &[] as &[&str] /* no dimensions*/, DataType::I32).unwrap();

assert_eq!(1,                   data_set.num_vars());
assert_eq!(Some(1),             data_set.var_len(SCALAR_VAR_NAME));
Source

pub fn add_var_i8<T: AsRef<str>>( &mut self, var_name: &str, dims_name: &[T], ) -> Result<(), InvalidDataSet>

Add a new i8 type variable defined over named dimensions (see the add_var method).

Source

pub fn add_var_u8<T: AsRef<str>>( &mut self, var_name: &str, dims_name: &[T], ) -> Result<(), InvalidDataSet>

Add a new u8 type variable defined over named dimensions (see the add_var method).

Source

pub fn add_var_i16<T: AsRef<str>>( &mut self, var_name: &str, dims_name: &[T], ) -> Result<(), InvalidDataSet>

Add a new i16 type variable defined over named dimensions (see the add_var method).

Source

pub fn add_var_i32<T: AsRef<str>>( &mut self, var_name: &str, dims_name: &[T], ) -> Result<(), InvalidDataSet>

Add a new i32 type variable defined over named dimensions (see the add_var method).

Source

pub fn add_var_f32<T: AsRef<str>>( &mut self, var_name: &str, dims_name: &[T], ) -> Result<(), InvalidDataSet>

Add a new f32 type variable defined over named dimensions (see the add_var method).

Source

pub fn add_var_f64<T: AsRef<str>>( &mut self, var_name: &str, dims_name: &[T], ) -> Result<(), InvalidDataSet>

Add a new f64 type variable defined over named dimensions (see the add_var method).

Source

pub fn num_vars(&self) -> usize

Returns the number of defined variables.

Source

pub fn has_var(&self, var_name: &str) -> bool

Returns :

  • true if the variable is defined.
  • false otherwise.
Source

pub fn is_record_var(&self, var_name: &str) -> Option<bool>

Source

pub fn var_len(&self, var_name: &str) -> Option<usize>

Returns the length (total number of elements) of the variable.

Source

pub fn var_data_type(&self, var_name: &str) -> Option<DataType>

Returns the data type of the variable, or None.

Source

pub fn get_var(&self, var_name: &str) -> Option<&Variable>

Returns a reference to the variable, or None.

Source

pub fn get_var_mut(&mut self, var_name: &str) -> Option<&mut Variable>

Returns a mutable reference to the variable

Source

pub fn get_vars(&self) -> Vec<&Variable>

Returns the references all the variables defined in the dataset.

Source

pub fn get_var_names(&self) -> Vec<String>

Returns the names all the variables defined in the dataset.

Source

pub fn rename_var( &mut self, old_var_name: &str, new_var_name: &str, ) -> Result<(), InvalidDataSet>

Renames a variable.

Nothing is do if old_var_name and new_var_name the same.

Returns an error if :

  • no variable old_var_name exists
  • an other variable new_var_name already exists
  • new_var_name is a NetCDF-3 valid name
Source

pub fn remove_var(&mut self, var_name: &str) -> Result<Variable, InvalidDataSet>

Remove the variable.

Source

pub fn add_var_attr_i8( &mut self, var_name: &str, attr_name: &str, var_attr_value: Vec<i8>, ) -> Result<(), InvalidDataSet>

Source

pub fn add_var_attr_u8( &mut self, var_name: &str, attr_name: &str, var_attr_value: Vec<u8>, ) -> Result<(), InvalidDataSet>

Source

pub fn add_var_attr_string<T: AsRef<str>>( &mut self, var_name: &str, attr_name: &str, var_attr_value: T, ) -> Result<(), InvalidDataSet>

Source

pub fn add_var_attr_i16( &mut self, var_name: &str, attr_name: &str, var_attr_value: Vec<i16>, ) -> Result<(), InvalidDataSet>

Source

pub fn add_var_attr_i32( &mut self, var_name: &str, attr_name: &str, var_attr_value: Vec<i32>, ) -> Result<(), InvalidDataSet>

Source

pub fn add_var_attr_f32( &mut self, var_name: &str, attr_name: &str, var_attr_value: Vec<f32>, ) -> Result<(), InvalidDataSet>

Source

pub fn add_var_attr_f64( &mut self, var_name: &str, attr_name: &str, var_attr_value: Vec<f64>, ) -> Result<(), InvalidDataSet>

Source

pub fn get_var_attr( &self, var_name: &str, attr_name: &str, ) -> Option<&Attribute>

Returns a reference of variable attribute.

Source

pub fn get_var_attr_len(&self, var_name: &str, attr_name: &str) -> Option<usize>

Returns the length (number of elements) of the variable attribute.

Source

pub fn get_var_attr_data_type( &self, var_name: &str, attr_name: &str, ) -> Option<DataType>

Returns the data type of the variable attribute.

Source

pub fn get_var_attrs(&self, var_name: &str) -> Option<Vec<&Attribute>>

Returns all attributes of a variable.

Returns None if the variable is not defined.

Source

pub fn has_var_attr(&self, var_name: &str, attr_name: &str) -> Option<bool>

Returns :

  • true if the variable attribute is defined.
  • false otherwise.
Source

pub fn num_var_attrs(&self, var_name: &str) -> Option<usize>

Returns the number of attributes of the variable.

Returns None if the variable does not exist.

Source

pub fn rename_var_attr( &mut self, var_name: &str, old_attr_name: &str, new_attr_name: &str, ) -> Result<(), InvalidDataSet>

Rename the variable attribute.

Source

pub fn remove_var_attr( &mut self, var_name: &str, attr_name: &str, ) -> Result<Attribute, InvalidDataSet>

Remove the attribute from the variable.

Source

pub fn get_var_attr_i8(&self, var_name: &str, attr_name: &str) -> Option<&[i8]>

Returns the attribute value as a &[i8].

Also see the method Attribute::get_i8.

Source

pub fn get_var_attr_u8(&self, var_name: &str, attr_name: &str) -> Option<&[u8]>

Returns the attribute value as a &[u8].

Also see the method Attribute::get_u8.8))

Source

pub fn get_var_attr_as_string( &self, var_name: &str, attr_name: &str, ) -> Option<String>

Returns the attribute value as a String.

Also see the method Attribute::get_as_string

Source

pub fn get_var_attr_i16( &self, var_name: &str, attr_name: &str, ) -> Option<&[i16]>

Returns the attribute value as a &[i16].

Also see the method Attribute::get_i16.

Source

pub fn get_var_attr_i32( &self, var_name: &str, attr_name: &str, ) -> Option<&[i32]>

Returns the attribute value as a &[i32].

Also see the method Attribute::get_i32.

Source

pub fn get_var_attr_f32( &self, var_name: &str, attr_name: &str, ) -> Option<&[f32]>

Returns the attribute value as a &[f32].

Also see the method Attribute::get_f32.

Source

pub fn get_var_attr_f64( &self, var_name: &str, attr_name: &str, ) -> Option<&[f64]>

Returns the attribute value as a &[f64].

Also see the method Attribute::get_f64(struct.Attribute.html#method.get_f64

Source

pub fn get_global_attr(&self, attr_name: &str) -> Option<&Attribute>

Returns a reference to the global attribute.

Source

pub fn get_global_attrs(&self) -> Vec<&Attribute>

Returns a reference of all global attributes.

Source

pub fn get_global_attr_len(&self, attr_name: &str) -> Option<usize>

Returns the length (number of elements) of the global attribute.

Source

pub fn get_global_attr_data_type(&self, attr_name: &str) -> Option<DataType>

Returns the data type of the global attribute.

Source

pub fn num_global_attrs(&self) -> usize

Returns the number of global attributes.

Source

pub fn has_global_attr(&self, attr_name: &str) -> bool

Returns :

  • true if the global attribute is defined.
  • false otherwise.
Source

pub fn get_global_attr_names(&self) -> Vec<String>

Returns the number of global attributes.

Source

pub fn add_global_attr_i8( &mut self, attr_name: &str, attr_data: Vec<i8>, ) -> Result<(), InvalidDataSet>

Adds a global i8 type attribute in the data set.

Source

pub fn add_global_attr_u8( &mut self, attr_name: &str, attr_data: Vec<u8>, ) -> Result<(), InvalidDataSet>

Adds a global u8 type attribute in the data set.

Source

pub fn add_global_attr_string<T: AsRef<str>>( &mut self, attr_name: &str, attr_data: T, ) -> Result<(), InvalidDataSet>

Adds a global u8 type attribute in the data set.

Source

pub fn add_global_attr_i16( &mut self, attr_name: &str, attr_data: Vec<i16>, ) -> Result<(), InvalidDataSet>

Adds a global i16 type attribute in the data set.

Source

pub fn add_global_attr_i32( &mut self, attr_name: &str, attr_data: Vec<i32>, ) -> Result<(), InvalidDataSet>

Adds a global i32 type attribute in the data set.

Source

pub fn add_global_attr_f32( &mut self, attr_name: &str, attr_data: Vec<f32>, ) -> Result<(), InvalidDataSet>

Adds a global f32 type attribute in the data set.

Source

pub fn add_global_attr_f64( &mut self, attr_name: &str, attr_data: Vec<f64>, ) -> Result<(), InvalidDataSet>

Add a global f64 type attribute in the data set.

Source

pub fn rename_global_attr( &mut self, old_attr_name: &str, new_attr_name: &str, ) -> Result<(), InvalidDataSet>

Source

pub fn remove_global_attr( &mut self, attr_name: &str, ) -> Result<Attribute, InvalidDataSet>

Source

pub fn get_global_attr_i8(&self, attr_name: &str) -> Option<&[i8]>

Returns the attribute value as a &[i8].

Also see the method Attribute::get_i8.

Source

pub fn get_global_attr_u8(&self, attr_name: &str) -> Option<&[u8]>

Returns the attribute value as a &[u8].

Also see the method Attribute::get_u8.

Source

pub fn get_global_attr_as_string(&self, attr_name: &str) -> Option<String>

Returns the global attribute value as a String.

Also see the method Attribute::get_as_string

Source

pub fn get_global_attr_i16(&self, attr_name: &str) -> Option<&[i16]>

Returns the attribute value as a &[i16].

Also see the method Attribute::get_i16(struct.Attribute.html#method.get_i16

Source

pub fn get_global_attr_i32(&self, attr_name: &str) -> Option<&[i32]>

Returns the attribute value as a &[i32].

Also see the method Attribute::get_i32.

Source

pub fn get_global_attr_f32(&self, attr_name: &str) -> Option<&[f32]>

Returns the attribute value as a &[f32].

Also see the method Attribute::get_f32.)

Source

pub fn get_global_attr_f64(&self, attr_name: &str) -> Option<&[f64]>

Returns the attribute value as a &[f64].

Also see the method Attribute::get_f64

Source

pub fn record_size(&self) -> Option<usize>

Returns the size (number of bytes) required by each record stored in the data file.

Returns None if the data set has not a unlimited-size dimension.

§Example
use netcdf3::{DataSet, Variable};
const UNLIM_DIM_NAME: &str = "unlim_dim";
const UNLIM_DIM_SIZE: usize = 10;

const FIXED_DIM_NAME: &str = "fixed_dim";
const FIXED_DIM_SIZE: usize = 20;

const VAR_1D_NAME: &str = "var_1D";
const VAR_2D_NAME: &str = "var_2D";

// No *unlimited-size* dimension is defined
let mut data_set: DataSet = DataSet::new();
assert_eq!(false,   data_set.has_unlimited_dim());
assert_eq!(None,    data_set.record_size());

// The *unlimited-size* dimension is defined here, but no variable uses it
data_set.set_unlimited_dim(UNLIM_DIM_NAME, UNLIM_DIM_SIZE).unwrap();
assert_eq!(true,                                            data_set.has_unlimited_dim());
assert_eq!(Some(0),                                         data_set.record_size());

// First : Add a 1D variable (a vector) over the *unlimited-size* dimension
data_set.add_var_i8(VAR_1D_NAME, &[UNLIM_DIM_NAME]).unwrap();
const VAR_1D_CHUNK_SIZE: usize = 4;  // 1 useful byte + 3 zero-padding bytes
assert_eq!(true,                                            data_set.has_unlimited_dim());
assert_eq!(Some(VAR_1D_CHUNK_SIZE),                         data_set.record_size());

// Second : Add a 2D variable (a matrix) over the* unlimited-size* dimension
data_set.add_fixed_dim(FIXED_DIM_NAME, FIXED_DIM_SIZE).unwrap();
data_set.add_var_i8(VAR_2D_NAME, &[UNLIM_DIM_NAME, FIXED_DIM_NAME]).unwrap();
const VAR_2D_CHUNK_SIZE: usize = 20;  // 20 bytes

// Then: the record size has increased
assert_eq!(true,                                            data_set.has_unlimited_dim());
assert_eq!(Some(VAR_1D_CHUNK_SIZE + VAR_2D_CHUNK_SIZE),     data_set.record_size());
Source

pub fn num_records(&self) -> Option<usize>

Returns the number of records stored in data file.

Returns None if the data set has not an unlimited-size dimension.

§Example
use netcdf3::DataSet;
const UNLIM_DIM_NAME: &str = "unlim_dim";
const UNLIM_DIM_SIZE: usize = 10;

let mut data_set: DataSet = DataSet::new();

// No *unlimited-size* dimension is defined
assert_eq!(false,   data_set.has_unlimited_dim());
assert_eq!(None,    data_set.num_records());

// The *unlimited-size* dimension is defined here
data_set.set_unlimited_dim(UNLIM_DIM_NAME, UNLIM_DIM_SIZE);
assert_eq!(true,                    data_set.has_unlimited_dim());
assert_eq!(Some(UNLIM_DIM_SIZE),    data_set.num_records());

Trait Implementations§

Source§

impl Debug for DataSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for DataSet

Source§

fn eq(&self, other: &DataSet) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for DataSet

Auto Trait Implementations§

§

impl Freeze for DataSet

§

impl !RefUnwindSafe for DataSet

§

impl !Send for DataSet

§

impl !Sync for DataSet

§

impl Unpin for DataSet

§

impl !UnwindSafe for DataSet

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.