Struct Variable

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

NetCDF-3 variable

Variable instances are managed by the struct DataSet.

DataSets allow to create, get, remove and rename Variables.

§Examples

§Create a variable

use netcdf3::{DataSet, Variable, DataType, DimensionType};

const VAR_NAME: &str = "var_1";
const DIM_NAME_1: &str = "dim_1";
const DIM_NAME_2: &str = "dim_2";
const DIM_SIZE_1: usize = 2;
const DIM_SIZE_2: usize = 3;
const DATA_F32: &'static [f32; 6] = &[0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
const DATA_F32_LEN: usize = DATA_F32.len();

assert_eq!(DATA_F32_LEN, DIM_SIZE_1 * DIM_SIZE_2);

// Create a data set
let mut data_set: DataSet = DataSet::new();
// Define 2 dimensions
data_set.set_unlimited_dim(DIM_NAME_1, DIM_SIZE_1).unwrap();
data_set.add_fixed_dim(DIM_NAME_2, DIM_SIZE_2).unwrap();
// Define a `f32` variable
data_set.add_var_f32(VAR_NAME, &[DIM_NAME_1, DIM_NAME_2]).unwrap();

assert_eq!(true,            data_set.has_var(VAR_NAME));
let var: &Variable = data_set.get_var(VAR_NAME).unwrap();
assert_eq!(VAR_NAME,                        var.name());
assert_eq!(true,                            var.is_record_var());
assert_eq!(2,                               var.num_dims());
assert_eq!(0,                               var.num_attrs());
assert_eq!(vec![DIM_NAME_1, DIM_NAME_2],    var.dim_names());
assert_eq!(DATA_F32_LEN,                    var.len());
assert_eq!(DataType::F32,                   var.data_type());

§Rename a variable

use netcdf3::{DataSet, DataType};
const VAR_NAME_1: &str = "var_1";
const VAR_NAME_2: &str = "var_2";
const DIM_NAME: &str = "dim_1";
const VAR_DATA: [i32; 4] = [1, 2, 3, 4];
const VAR_DATA_LEN: usize = VAR_DATA.len();

// Create a data set and a variable
let mut data_set: DataSet = DataSet::new();
data_set.add_fixed_dim(DIM_NAME, VAR_DATA_LEN).unwrap();
data_set.add_var_i32::<&str>(VAR_NAME_1, &[DIM_NAME]).unwrap();

assert_eq!(1,                               data_set.num_vars());
assert_eq!(true,                            data_set.has_var(VAR_NAME_1));
assert_eq!(Some(VAR_DATA_LEN),              data_set.var_len(VAR_NAME_1));
assert_eq!(Some(DataType::I32),             data_set.var_data_type(VAR_NAME_1));
assert_eq!(false,                           data_set.has_var(VAR_NAME_2));
assert_eq!(None,                            data_set.var_len(VAR_NAME_2));
assert_eq!(None,                            data_set.var_data_type(VAR_NAME_2));

// Rename the variable
data_set.rename_var(VAR_NAME_1, VAR_NAME_2).unwrap();

assert_eq!(1,                               data_set.num_vars());
assert_eq!(false,                           data_set.has_var(VAR_NAME_1));
assert_eq!(None,                            data_set.var_len(VAR_NAME_1));
assert_eq!(None,                            data_set.var_data_type(VAR_NAME_1));
assert_eq!(true,                            data_set.has_var(VAR_NAME_2));
assert_eq!(Some(VAR_DATA_LEN),              data_set.var_len(VAR_NAME_2));
assert_eq!(Some(DataType::I32),             data_set.var_data_type(VAR_NAME_2));

§Remove a variable

use netcdf3::{DataSet, DataType};

const DIM_NAME: &str = "dim_1";
const VAR_NAME: &str = "var_1";
const VAR_DATA: [i32; 4] = [1, 2, 3, 4];
const VAR_DATA_LEN: usize = VAR_DATA.len();

// Create a data set and a variable
let mut data_set: DataSet = DataSet::new();

data_set.add_fixed_dim(DIM_NAME, VAR_DATA_LEN).unwrap();
data_set.add_var_i32::<&str>(VAR_NAME, &[DIM_NAME]).unwrap();

assert_eq!(1,                               data_set.num_vars());
assert_eq!(true,                            data_set.has_var(VAR_NAME));
assert_eq!(Some(VAR_DATA_LEN),              data_set.var_len(VAR_NAME));
assert_eq!(Some(DataType::I32),             data_set.var_data_type(VAR_NAME));

// Remove the variable
data_set.remove_var(VAR_NAME).unwrap();

assert_eq!(0,                               data_set.num_vars());
assert_eq!(false,                           data_set.has_var(VAR_NAME));
assert_eq!(None,                            data_set.var_len(VAR_NAME));
assert_eq!(None,                            data_set.var_data_type(VAR_NAME));

Implementations§

Source§

impl Variable

Source

pub fn name(&self) -> &str

Return the name of the variable.

Source

pub fn data_type(&self) -> DataType

Returns the data type of the variable.

§Example
use netcdf3::{DataSet, Variable, DataType};
const VAR_NAME: &str = "var_1";

let data_set: DataSet = {
    let mut data_set = DataSet::new();
    data_set.add_var_i32::<&str>(VAR_NAME, &[]).unwrap();
    data_set
};

let var: &Variable = data_set.get_var(VAR_NAME).unwrap();
assert_eq!(DataType::I32,               var.data_type());
Source

pub fn len(&self) -> usize

Returns the total number of elements.

If the variable is a record variable then len = num_chunks * chunk_len.

Source

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

Source

pub fn num_dims(&self) -> usize

Returns the number of dimensions (the rank) the the variables

Source

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

Returns the list of the dimensions

Source

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

Returns the list of the dimension names

Source

pub fn is_record_var(&self) -> bool

Returns :

  • true if the variable is defined over the unlimited size dimension, then has several records
  • false otherwise
Source

pub fn num_attrs(&self) -> usize

Returns the number of attributes.

Source

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

Returns :

  • true if the variable has the attribute
  • false if not
Source

pub fn chunk_len(&self) -> usize

Returns the number of elements per chunk.

If the variable id a fixed-size variable then chunk_len = len.

Source

pub fn chunk_size(&self) -> usize

Returns the size of each chunk (the number of bytes) including the padding bytes.

§Example
use netcdf3::{DataSet, Variable};

const VAR_I8_NAME: &str = "scalar_var_i8";
const VAR_U8_NAME: &str = "scalar_var_u8";
const VAR_I16_NAME: &str = "scalar_var_i16";
const VAR_I32_NAME: &str = "scalar_var_i32";
const VAR_F32_NAME: &str = "scalar_var_f32";
const VAR_F64_NAME: &str = "scalar_var_f64";

let data_set: DataSet = {
    let mut data_set = DataSet::new();
    data_set.add_var_i8::<&str>(VAR_I8_NAME, &[]).unwrap();
    data_set.add_var_u8::<&str>(VAR_U8_NAME, &[]).unwrap();
    data_set.add_var_i16::<&str>(VAR_I16_NAME, &[]).unwrap();
    data_set.add_var_i32::<&str>(VAR_I32_NAME, &[]).unwrap();
    data_set.add_var_f32::<&str>(VAR_F32_NAME, &[]).unwrap();
    data_set.add_var_f64::<&str>(VAR_F64_NAME, &[]).unwrap();
    data_set
};

let scalar_var_i8: &Variable = data_set.get_var(VAR_I8_NAME).unwrap();
let scalar_var_u8: &Variable = data_set.get_var(VAR_U8_NAME).unwrap();
let scalar_var_i16: &Variable = data_set.get_var(VAR_I16_NAME).unwrap();
let scalar_var_i32: &Variable = data_set.get_var(VAR_I32_NAME).unwrap();
let scalar_var_f32: &Variable = data_set.get_var(VAR_F32_NAME).unwrap();
let scalar_var_f64: &Variable = data_set.get_var(VAR_F64_NAME).unwrap();

assert_eq!(4,           scalar_var_i8.chunk_size());
assert_eq!(4,           scalar_var_u8.chunk_size());
assert_eq!(4,           scalar_var_i16.chunk_size());
assert_eq!(4,           scalar_var_i32.chunk_size());
assert_eq!(4,           scalar_var_f32.chunk_size());
assert_eq!(8,           scalar_var_f64.chunk_size());
Source

pub fn num_chunks(&self) -> usize

Returns the number of chunks.

Source

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

Returns all attributs defined in the dataset or in the variable.

Source

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

Returns all attributs defined in the dataset or in the variable.

Source

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

Returns a reference counter to the named attribute, return an error if the attribute is not already defined.

Source

pub fn get_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_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_attr_as_string(&self, attr_name: &str) -> Option<String>

Returns the attribute value as a String.

Also see the method Attribute::get_as_string.

Source

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

Returns the attribute value as a &[i16].

Also see the method Attribute::get_i16.

Source

pub fn get_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_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_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 add_attr_i8( &mut self, attr_name: &str, i8_data: Vec<i8>, ) -> Result<(), InvalidDataSet>

Append a new i8 attribute.

An error is returned if an other attribute with the same name has already been added.

Source

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

Append a new u8 attribute.

An error is returned if an other attribute with the same name has already been added.

Source

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

Append a new u8 attribute.

An error is returned if an other attribute with the same name has already been added.

Source

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

Append a new i16 attribute.

An error is returned if an other attribute with the same name has already been added.

Source

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

Append a new i32 attribute.

An error is returned if an other attribute with the same name has already been added.

Source

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

Append a new f32 attribute.

An error is returned if an other attribute with the same name has already been added.

Source

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

Append a new f64 attribute.

An error is returned if an other attribute with the same name has already been added.

Source

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

Trait Implementations§

Source§

impl Clone for Variable

Source§

fn clone(&self) -> Variable

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Variable

Source§

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

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

impl PartialEq for Variable

Source§

fn eq(&self, other: &Variable) -> 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 Variable

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.