Struct FileWriter

Source
pub struct FileWriter<'a> { /* private fields */ }
Expand description

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

§Example

use std::path::PathBuf;
use std::io::Read;
use copy_to_tmp_file::NC3_LIGHT_CLASSIC_FILE_BYTES;

use netcdf3::{FileWriter, DataSet, Version};
const LATITUDE_DIM_NAME: &str = "latitude";
const LATITUDE_VAR_NAME: &str = LATITUDE_DIM_NAME;
const LATITUDE_VAR_DATA: [f32; 3] = [0.0, 1.0, 2.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; 4] = [0.0, 1.0, 2.0, 3.0];
const LONGITUDE_VAR_LEN: usize = LONGITUDE_VAR_DATA.len();

const TEMPERATURE_VAR_NAME: &str = "temperature";
const TEMPERATURE_DATA: [f64; 12] = [0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.];
const TEMPERATURE_VAR_LEN: usize = TEMPERATURE_DATA.len();

// Create the NetCDF-3 definition
// ------------------------------
let data_set: DataSet = {
    let mut data_set: DataSet = DataSet::new();
    // Define the dimensions
    data_set.add_fixed_dim(LATITUDE_DIM_NAME, LATITUDE_VAR_LEN).unwrap();
    data_set.add_fixed_dim(LONGITUDE_DIM_NAME, LONGITUDE_VAR_LEN).unwrap();
    // Define the variable
    data_set.add_var_f32(LATITUDE_VAR_NAME, &[LATITUDE_DIM_NAME]).unwrap();
    data_set.add_var_f32(LONGITUDE_VAR_NAME, &[LONGITUDE_VAR_NAME]).unwrap();
    data_set.add_var_f64(TEMPERATURE_VAR_NAME, &[LATITUDE_DIM_NAME, LONGITUDE_VAR_NAME]).unwrap();

    data_set
};

// ...

// Create and write the NetCDF-3 file
// ----------------------------------
assert_eq!(false,                                   output_file_path.exists());
let mut file_writer: FileWriter = FileWriter::create_new(&output_file_path).unwrap();
// Set the NetCDF-3 definition
file_writer.set_def(&data_set, Version::Classic, 0).unwrap();
assert_eq!(TEMPERATURE_VAR_LEN,                     LATITUDE_VAR_LEN * LONGITUDE_VAR_LEN);
file_writer.write_var_f32(LATITUDE_VAR_NAME, &LATITUDE_VAR_DATA[..]).unwrap();
file_writer.write_var_f32(LONGITUDE_VAR_NAME, &LONGITUDE_VAR_DATA[..]).unwrap();
file_writer.write_var_f64(TEMPERATURE_VAR_NAME, &TEMPERATURE_DATA[..]).unwrap();
file_writer.close().unwrap();
assert_eq!(true,                                    output_file_path.exists());

// Binary comparaison with the "same" NeTCDF-3 file created with the [Python library netCDF4](https://github.com/Unidata/netcdf4-python).
// -------------------------------------------------------------------------------------------------------------------------------------------------------
let nc3_file_bytes: Vec<u8> = {
    let mut written_bytes: Vec<u8> = vec![];
    let mut written_file: std::fs::File = std::fs::File::open(&output_file_path).unwrap();
    written_file.read_to_end(&mut written_bytes).unwrap();
    written_bytes
};
assert_eq!(NC3_LIGHT_CLASSIC_FILE_BYTES.len(),      nc3_file_bytes.len());
assert_eq!(NC3_LIGHT_CLASSIC_FILE_BYTES,            &nc3_file_bytes[..]);

Implementations§

Source§

impl<'a> FileWriter<'a>

Source

pub fn open_seek_write( file_name: &str, output: Box<dyn SeekWrite>, ) -> Result<Self, WriteError>

Opens output for writing.

Source

pub fn open<P: AsRef<Path>>( output_file_path: P, ) -> Result<FileWriter<'a>, WriteError>

Opens and overwrites an existing NetCDF-3 file or creates one.

Source

pub fn create_new<P: AsRef<Path>>( output_file_path: P, ) -> Result<FileWriter<'a>, WriteError>

Creates a new NetCDF-3 file.

§Error

An error occures if the NetCDF-3 file already exists.

Source

pub fn file_path(&self) -> &Path

Path of the output file.

Source

pub fn set_def( &mut self, data_set: &'a DataSet, version: Version, header_min_size: usize, ) -> Result<(), WriteError>

Set the NetCDF-3 definition.

§Arguments
  • data_set: the NetCDF-3 defintion set (also see DataSet).
  • version: the NetCDF-3 version (also see Version).
  • header_min_size: the mininum number of bytes reserved for header of the NetCDF-3 file.
§Example
use std::path::PathBuf;
use netcdf3::{FileWriter, DataSet, Version};
use tempdir::TempDir;

const TMP_DIR_PREFIX: &str = "netcdf3_tests_";
const FILE_NAME_1: &str = "empty_data_set_1.nc";
const FILE_NAME_2: &str = "empty_data_set_2.nc";
const HEADER_MIN_SIZE_1: usize = 0;
const HEADER_MIN_SIZE_2: usize = 1024;

let tmp_dir: TempDir = TempDir::new(TMP_DIR_PREFIX).unwrap();

// Create 2 NetCDF-3 files containing empty data sets but with different `header_min_size`
// ---------------------------------------------------------------------------------------
let empty_data_set: DataSet = DataSet::new();
let file_path_1: PathBuf = tmp_dir.path().join(FILE_NAME_1);
{
    let mut file_writer_1: FileWriter = FileWriter::create_new(&file_path_1).unwrap();
    file_writer_1.set_def(&empty_data_set, Version::Classic, HEADER_MIN_SIZE_1).unwrap();
    file_writer_1.close();
}
let file_path_2: PathBuf = tmp_dir.path().join(FILE_NAME_2);
{
    let mut file_writer_1: FileWriter = FileWriter::create_new(&file_path_2).unwrap();
    file_writer_1.set_def(&empty_data_set, Version::Classic, HEADER_MIN_SIZE_2).unwrap();
    file_writer_1.close();
}

// Compare the size beetween the 2 NetCDF-3 files
// ----------------------------------------------
assert_eq!(32,                  std::fs::metadata(&file_path_1).unwrap().len());
assert_eq!(1024,                std::fs::metadata(&file_path_2).unwrap().len());
Source

pub fn header_is_defined(&self) -> bool

Source

pub fn data_set(&self) -> Option<&'a DataSet>

Source

pub fn version(&self) -> Option<Version>

Source

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

Source

pub fn close(self) -> Result<(), WriteError>

Fills the unwritten data, and closes the NetCDF-3 file.

Source

pub fn write_var_i8( &mut self, var_name: &str, data: &[i8], ) -> Result<(), WriteError>

Source

pub fn write_var_u8( &mut self, var_name: &str, data: &[u8], ) -> Result<(), WriteError>

Source

pub fn write_var_i16( &mut self, var_name: &str, data: &[i16], ) -> Result<(), WriteError>

Source

pub fn write_var_i32( &mut self, var_name: &str, data: &[i32], ) -> Result<(), WriteError>

Source

pub fn write_var_f32( &mut self, var_name: &str, data: &[f32], ) -> Result<(), WriteError>

Source

pub fn write_var_f64( &mut self, var_name: &str, data: &[f64], ) -> Result<(), WriteError>

Source

pub fn write_record_i8( &mut self, var_name: &str, record_index: usize, record: &[i8], ) -> Result<(), WriteError>

Source

pub fn write_record_u8( &mut self, var_name: &str, record_index: usize, record: &[u8], ) -> Result<(), WriteError>

Source

pub fn write_record_i16( &mut self, var_name: &str, record_index: usize, record: &[i16], ) -> Result<(), WriteError>

Source

pub fn write_record_i32( &mut self, var_name: &str, record_index: usize, record: &[i32], ) -> Result<(), WriteError>

Source

pub fn write_record_f32( &mut self, var_name: &str, record_index: usize, record: &[f32], ) -> Result<(), WriteError>

Source

pub fn write_record_f64( &mut self, var_name: &str, record_index: usize, record: &[f64], ) -> Result<(), WriteError>

Trait Implementations§

Source§

impl<'a> Debug for FileWriter<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for FileWriter<'a>

§

impl<'a> !RefUnwindSafe for FileWriter<'a>

§

impl<'a> !Send for FileWriter<'a>

§

impl<'a> !Sync for FileWriter<'a>

§

impl<'a> Unpin for FileWriter<'a>

§

impl<'a> !UnwindSafe for FileWriter<'a>

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.