FileReader

Struct FileReader 

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

Allows to read NetCDF-3 files (the classic and the 64-bit offset versions).

§Example

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

const LATITUDE_DIM_NAME: &str = "latitude";
const LATITUDE_VAR_NAME: &str = LATITUDE_DIM_NAME;
const LATITUDE_VAR_DATA: [f32; 3] = [0.0, 0.5, 1.0];
const LATITUDE_VAR_LEN: usize = LATITUDE_VAR_DATA.len();

const LONGITUDE_DIM_NAME: &str = "longitude";
const LONGITUDE_VAR_NAME: &str = LONGITUDE_DIM_NAME;
const LONGITUDE_VAR_DATA: [f32; 5] = [0.0, 0.5, 1.0, 1.5, 2.0];
const LONGITUDE_VAR_LEN: usize = LONGITUDE_VAR_DATA.len();

const TIME_DIM_NAME: &str = "time";
const TIME_VAR_NAME: &str = TIME_DIM_NAME;
const TIME_VAR_DATA: [f32; 2] = [438_300.0, 438_324.0];
const TIME_VAR_LEN: usize = TIME_VAR_DATA.len();

const TEMP_I8_VAR_NAME: &str = "temperature_i8";
const TEMP_I8_VAR_DATA: [i8; 30] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
const TEMP_I8_VAR_LEN: usize = TEMP_I8_VAR_DATA.len();

const TEMP_U8_VAR_NAME: &str = "temperature_u8";
const TEMP_U8_VAR_DATA: [u8; 30] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
const TEMP_U8_VAR_LEN: usize = TEMP_U8_VAR_DATA.len();

const TEMP_I16_VAR_NAME: &str = "temperature_i16";
const TEMP_I16_VAR_DATA: [i16; 30] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
const TEMP_I16_VAR_LEN: usize = TEMP_I16_VAR_DATA.len();

const TEMP_I32_VAR_NAME: &str = "temperature_i32";
const TEMP_I32_VAR_DATA: [i32; 30] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
const TEMP_I32_VAR_LEN: usize = TEMP_I32_VAR_DATA.len();

const TEMP_F32_VAR_NAME: &str = "temperature_f32";
const TEMP_F32_VAR_DATA: [f32; 30] = [0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.];
const TEMP_F32_VAR_LEN: usize = TEMP_F32_VAR_DATA.len();

const TEMP_F64_VAR_NAME: &str = "temperature_f64";
const TEMP_F64_VAR_DATA: [f64; 30] = [0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.];
const TEMP_F64_VAR_LEN: usize = TEMP_F64_VAR_DATA.len();

// ...

// Open the file and read the header
// ---------------------------------
let mut file_reader: FileReader = FileReader::open(input_file_path).unwrap();

let data_set: &DataSet = file_reader.data_set();

// Get the NetCDf-3 version
// ------------------------
assert_eq!(Version::Classic,                    file_reader.version());
 
// --------------------------
assert_eq!(2,                                   data_set.num_global_attrs());
assert_eq!("Example of NETCDF3_CLASSIC file",   data_set.get_global_attr_as_string("title").unwrap());
assert_eq!("CF-1.8",                            data_set.get_global_attr_as_string("Conventions").unwrap());

// Get the dimensions
// ------------------
assert_eq!(3,                                   data_set.num_dims());

assert_eq!(true,                                data_set.has_dim(LATITUDE_DIM_NAME));
assert_eq!(Some(LATITUDE_VAR_LEN),              data_set.dim_size(LATITUDE_DIM_NAME));
assert_eq!(Some(DimensionType::FixedSize),      data_set.dim_type(LATITUDE_DIM_NAME));

assert_eq!(true,                                data_set.has_dim(LONGITUDE_DIM_NAME));
assert_eq!(Some(LONGITUDE_VAR_LEN),             data_set.dim_size(LONGITUDE_DIM_NAME));
assert_eq!(Some(DimensionType::FixedSize),      data_set.dim_type(LONGITUDE_DIM_NAME));

assert_eq!(true,                                data_set.has_dim(TIME_DIM_NAME));
assert_eq!(Some(TIME_VAR_LEN),                  data_set.dim_size(TIME_DIM_NAME));
assert_eq!(Some(DimensionType::UnlimitedSize),  data_set.dim_type(TIME_DIM_NAME));

// Get the variable definitions
// ----------------------------
assert_eq!(9,                                   data_set.num_vars());

assert_eq!(true,                                data_set.has_var(LATITUDE_VAR_NAME));
assert_eq!(Some(DataType::F32),                 data_set.var_data_type(LATITUDE_VAR_NAME));
assert_eq!(Some(false),                         data_set.is_record_var(LATITUDE_VAR_NAME));
assert_eq!(Some(LATITUDE_VAR_LEN),              data_set.var_len(LATITUDE_VAR_NAME));

// ..

// Get the variable attributes
// ---------------------------
assert_eq!(Some(4),                             data_set.num_var_attrs(LATITUDE_VAR_NAME));
assert_eq!("latitude",                          data_set.get_var_attr_as_string(LATITUDE_VAR_NAME, "standard_name").unwrap());
assert_eq!("LATITUDE",                          data_set.get_var_attr_as_string(LATITUDE_VAR_NAME, "long_name").unwrap());
assert_eq!("degrees_north",                     data_set.get_var_attr_as_string(LATITUDE_VAR_NAME, "units").unwrap());
assert_eq!("Y",                                 data_set.get_var_attr_as_string(LATITUDE_VAR_NAME, "axis").unwrap());

assert_eq!(Some(3),                             data_set.num_var_attrs(TEMP_F32_VAR_NAME));
assert_eq!("air_temperature",                   data_set.get_var_attr_as_string(TEMP_F32_VAR_NAME, "standard_name").unwrap());
assert_eq!("TEMPERATURE",                       data_set.get_var_attr_as_string(TEMP_F32_VAR_NAME, "long_name").unwrap());
assert_eq!("Celsius",                           data_set.get_var_attr_as_string(TEMP_F32_VAR_NAME, "units").unwrap());

// ...

// Read all the variables
// ----------------------
let variables: HashMap<String, DataVector> = file_reader.read_all_vars().unwrap();
let data_set: &DataSet = file_reader.data_set();
assert_eq!(9,                                   variables.len());

 
assert_eq!(true,                                variables.contains_key(LATITUDE_VAR_NAME));
assert_eq!(DataType::F32,                       variables[LATITUDE_VAR_NAME].data_type());
assert_eq!(Some(&LATITUDE_VAR_DATA[..]),        variables[LATITUDE_VAR_NAME].get_f32());

assert_eq!(true,                                variables.contains_key(LONGITUDE_VAR_NAME));
assert_eq!(DataType::F32,                       variables[LONGITUDE_VAR_NAME].data_type());
assert_eq!(Some(&LONGITUDE_VAR_DATA[..]),       variables[LONGITUDE_VAR_NAME].get_f32());
 
assert_eq!(true,                                variables.contains_key(TIME_VAR_NAME));
assert_eq!(DataType::F32,                       variables[TIME_VAR_NAME].data_type());
assert_eq!(Some(&TIME_VAR_DATA[..]),            variables[TIME_VAR_NAME].get_f32());
 
assert_eq!(true,                                variables.contains_key(TEMP_I8_VAR_NAME));
assert_eq!(DataType::I8,                        variables[TEMP_I8_VAR_NAME].data_type());
assert_eq!(Some(&TEMP_I8_VAR_DATA[..]),         variables[TEMP_I8_VAR_NAME].get_i8());
 
assert_eq!(true,                                variables.contains_key(TEMP_U8_VAR_NAME));
assert_eq!(DataType::U8,                        variables[TEMP_U8_VAR_NAME].data_type());
assert_eq!(Some(&TEMP_U8_VAR_DATA[..]),         variables[TEMP_U8_VAR_NAME].get_u8());
 
assert_eq!(true,                                variables.contains_key(TEMP_I16_VAR_NAME));
assert_eq!(DataType::I16,                       variables[TEMP_I16_VAR_NAME].data_type());
assert_eq!(Some(&TEMP_I16_VAR_DATA[..]),        variables[TEMP_I16_VAR_NAME].get_i16());
 
assert_eq!(true,                                variables.contains_key(TEMP_I32_VAR_NAME));
assert_eq!(DataType::I32,                       variables[TEMP_I32_VAR_NAME].data_type());
assert_eq!(Some(&TEMP_I32_VAR_DATA[..]),        variables[TEMP_I32_VAR_NAME].get_i32());
 
assert_eq!(true,                                variables.contains_key(TEMP_F32_VAR_NAME));
assert_eq!(DataType::F32,                       variables[TEMP_F32_VAR_NAME].data_type());
assert_eq!(Some(&TEMP_F32_VAR_DATA[..]),        variables[TEMP_F32_VAR_NAME].get_f32());

assert_eq!(true,                                variables.contains_key(TEMP_F64_VAR_NAME));
assert_eq!(DataType::F64,                       variables[TEMP_F64_VAR_NAME].data_type());
assert_eq!(Some(&TEMP_F64_VAR_DATA[..]),        variables[TEMP_F64_VAR_NAME].get_f64());
// ...

Implementations§

Source§

impl FileReader

Source

pub fn data_set(&self) -> &DataSet

Returns the data set managed by the reader.

Source

pub fn version(&self) -> Version

Source

pub fn file_path(&self) -> &Path

Returns the data set managed by the reader.

Source

pub fn open_seek_read( input_file_name: &str, input_file: Box<dyn SeekRead>, ) -> Result<Self, ReadError>

Source

pub fn open<P: AsRef<Path>>(input_file_path: P) -> Result<Self, ReadError>

Opens the file and parses the header of the NetCDF-3.

Source

pub fn close(self) -> (DataSet, Version)

Closes the file and releases the data set and the file version.

Source

pub fn read_all_vars( &mut self, ) -> Result<HashMap<String, DataVector>, ReadError>

Allows to read all variable data easily.

Also see an example here.

Source

pub fn read_var(&mut self, var_name: &str) -> Result<DataVector, ReadError>

Reads the typed variable and returns its values into Vec.

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

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

// ...

let mut file_reader: FileReader = FileReader::open(input_file_path).unwrap();

// Open the file
// -------------
assert_eq!(true,                    file_reader.data_set().has_var(LATITUDE_VAR_NAME));
assert_eq!(Some(DataType::F32),     file_reader.data_set().var_data_type(LATITUDE_VAR_NAME));

// Read the variable
// -----------------
// using the method `FileReader::read_var`
{
    let latitudes: DataVector = file_reader.read_var(LATITUDE_VAR_NAME).unwrap();
    assert_eq!(DataType::F32,                           latitudes.data_type());
 
    assert_eq!(None,                                    latitudes.get_i8());
    assert_eq!(None,                                    latitudes.get_u8());
    assert_eq!(None,                                    latitudes.get_i16());
    assert_eq!(None,                                    latitudes.get_i32());
    assert_eq!(Some(&LATITUDE_VAR_DATA[..]),            latitudes.get_f32());
    assert_eq!(None,                                    latitudes.get_f64());
}
 
// using the method `FileReader::read_var_f32`
{
    let latitudes: Vec<f32> = file_reader.read_var_f32(LATITUDE_VAR_NAME).unwrap();
    assert_eq!(&LATITUDE_VAR_DATA[..],                  &latitudes[..]);
}
Source

pub fn read_var_i8(&mut self, var_name: &str) -> Result<Vec<i8>, ReadError>

Reads the typed variable and returns its values into a typed Vec.

Source

pub fn read_var_u8(&mut self, var_name: &str) -> Result<Vec<u8>, ReadError>

Reads the typed variable and returns its values into a typed Vec.

Source

pub fn read_var_i16(&mut self, var_name: &str) -> Result<Vec<i16>, ReadError>

Reads the typed variable and returns its values into a typed Vec.

Source

pub fn read_var_i32(&mut self, var_name: &str) -> Result<Vec<i32>, ReadError>

Reads the typed variable and returns its values into a typed Vec.

Source

pub fn read_var_f32(&mut self, var_name: &str) -> Result<Vec<f32>, ReadError>

Reads the typed variable and returns its values into a typed Vec.

Source

pub fn read_var_f64(&mut self, var_name: &str) -> Result<Vec<f64>, ReadError>

Reads the typed variable and returns its values into a typed Vec.

Source

pub fn read_record( &mut self, var_name: &str, record_index: usize, ) -> Result<DataVector, ReadError>

Reads the typed records and returns its values into a typedVec.

Source

pub fn read_record_i8( &mut self, var_name: &str, record_index: usize, ) -> Result<Vec<i8>, ReadError>

Reads the typed records and returns its values into a typedVec.

Source

pub fn read_record_u8( &mut self, var_name: &str, record_index: usize, ) -> Result<Vec<u8>, ReadError>

Reads the typed records and returns its values into a typedVec.

Source

pub fn read_record_i16( &mut self, var_name: &str, record_index: usize, ) -> Result<Vec<i16>, ReadError>

Reads the typed records and returns its values into a typedVec.

Source

pub fn read_record_i32( &mut self, var_name: &str, record_index: usize, ) -> Result<Vec<i32>, ReadError>

Reads the typed records and returns its values into a typedVec.

Source

pub fn read_record_f32( &mut self, var_name: &str, record_index: usize, ) -> Result<Vec<f32>, ReadError>

Reads the typed records and returns its values into a typedVec.

Source

pub fn read_record_f64( &mut self, var_name: &str, record_index: usize, ) -> Result<Vec<f64>, ReadError>

Reads the typed records and returns its values into a typedVec.

Trait Implementations§

Source§

impl Debug for FileReader

Source§

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

Formats the value using the given formatter. Read more

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> 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.