DataVector

Enum DataVector 

Source
pub enum DataVector {
    I8(Vec<i8>),
    U8(Vec<u8>),
    I16(Vec<i16>),
    I32(Vec<i32>),
    F32(Vec<f32>),
    F64(Vec<f64>),
}
Expand description

Wraps the six NetCDF-3 data types.

It allows to load variable data from files easily through the methods:

§Example

use std::collections::HashMap;
use netcdf3::{FileReader, DataVector, DataType};

const LATITUDE_VAR_NAME: &str = "latitude";
const LATITUDE_VAR_DATA: [f32; 3] = [0.0, 0.5, 1.0];
const NUM_VARS: usize = 9;

// ...

// Read variable data from the file
// ---------------------------------
let mut file_reader: FileReader = FileReader::open(input_file_path).unwrap();
assert_eq!(NUM_VARS,                        file_reader.data_set().num_vars());
assert_eq!(true,                            file_reader.data_set().has_var(LATITUDE_VAR_NAME));
assert_eq!(DataType::F32,                   file_reader.data_set().var_data_type(LATITUDE_VAR_NAME).unwrap());

let mut data: HashMap<String, DataVector> = file_reader.read_all_vars().unwrap();
file_reader.close();

assert_eq!(NUM_VARS,                        data.len());
assert_eq!(true,                            data.contains_key(LATITUDE_VAR_NAME));
 
let latitude: DataVector = data.remove(LATITUDE_VAR_NAME).unwrap();
assert_eq!(DataType::F32,                   latitude.data_type());
let latitude: Vec<f32> = latitude.get_f32_into().unwrap();
assert_eq!(LATITUDE_VAR_DATA.to_vec(),      latitude);

Variants§

§

I8(Vec<i8>)

§

U8(Vec<u8>)

§

I16(Vec<i16>)

§

I32(Vec<i32>)

§

F32(Vec<f32>)

§

F64(Vec<f64>)

Implementations§

Source§

impl DataVector

Source

pub fn data_type(&self) -> DataType

Return the NetCDF-3 data type.

Source

pub fn len(&self) -> usize

Return the length (the number of elements) of the vector.

Source

pub fn get_i8(&self) -> Option<&[i8]>

Returns a slice to the internal Vec<i8>.

§Example
use netcdf3::{DataVector, DataType};

let data_vec = DataVector::I8(vec![1_i8, 2, 3]);

assert_eq!(DataType::I8,                data_vec.data_type());

assert_eq!(Some(&[1_i8, 2, 3][..]),     data_vec.get_i8());
assert_eq!(None,                        data_vec.get_u8());
assert_eq!(None,                        data_vec.get_i16());
assert_eq!(None,                        data_vec.get_i32());
assert_eq!(None,                        data_vec.get_f32());
assert_eq!(None,                        data_vec.get_f64());
Source

pub fn get_u8(&self) -> Option<&[u8]>

Returns a slice to the internal Vec<u8>.

Also see the method get_i8.

Source

pub fn get_i16(&self) -> Option<&[i16]>

Returns a slice to the internal Vec<i16>.

Also see the method get_i8.

Source

pub fn get_i32(&self) -> Option<&[i32]>

Returns a slice to the internal Vec<i32>.

Also see the method get_i8.

Source

pub fn get_f32(&self) -> Option<&[f32]>

Returns a slice to the internal Vec<f32>.

Also see the method get_i8.

Source

pub fn get_f64(&self) -> Option<&[f64]>

Returns a slice to the internal Vec<f64>.

Also see the method get_i8.

Source

pub fn get_i8_into(self) -> Result<Vec<i8>, DataVector>

Returns the internal Vec<i8> if the DataVector contains one.

Otherwise the instance of the DataVector is returned as an errror.

§Example
use netcdf3::{DataVector, DataType};

let data_1: Vec<i8> = vec![1, 2 ,3];
let ptr_1 : *const i8 = data_1.as_ptr();

// Frirst create a `DataVector::I8`
let data_vec: DataVector = DataVector::I8(data_1);
assert_eq!(DataType::I8,               data_vec.data_type());

// Try to extract the internal vector with the wrong data types
let data_vec: DataVector = data_vec.get_u8_into().unwrap_err();
let data_vec: DataVector = data_vec.get_i16_into().unwrap_err();
let data_vec: DataVector = data_vec.get_i32_into().unwrap_err();
let data_vec: DataVector = data_vec.get_f32_into().unwrap_err();
let data_vec: DataVector = data_vec.get_f64_into().unwrap_err();

// Extract the internal vector with the good data type
let data_2: Vec<i8> = data_vec.get_i8_into().unwrap();
let ptr_2 : *const i8 = data_2.as_ptr();

assert_eq!(vec![1, 2, 3],           data_2);

// No copy of the buffer has been done
assert_eq!(ptr_1,                   ptr_2);
Source

pub fn get_u8_into(self) -> Result<Vec<u8>, DataVector>

Source

pub fn get_i16_into(self) -> Result<Vec<i16>, DataVector>

Source

pub fn get_i32_into(self) -> Result<Vec<i32>, DataVector>

Source

pub fn get_f32_into(self) -> Result<Vec<f32>, DataVector>

Source

pub fn get_f64_into(self) -> Result<Vec<f64>, DataVector>

Trait Implementations§

Source§

impl Clone for DataVector

Source§

fn clone(&self) -> DataVector

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 DataVector

Source§

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

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

impl PartialEq for DataVector

Source§

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

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.