#![warn(missing_docs)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::wildcard_imports)]
use lazy_static::lazy_static;
use netcdf_sys::nc_type;
use std::sync::Mutex;
pub mod attribute;
pub mod dimension;
pub mod error;
pub mod file;
pub mod group;
pub mod types;
pub mod variable;
pub use attribute::*;
pub use dimension::*;
pub use file::*;
pub use group::*;
pub use variable::*;
pub fn create<P>(name: P) -> error::Result<MutableFile>
where
P: AsRef<std::path::Path>,
{
RawFile::create_with(name.as_ref(), Options::NETCDF4)
}
pub fn create_with<P>(name: P, options: Options) -> error::Result<MutableFile>
where
P: AsRef<std::path::Path>,
{
RawFile::create_with(name.as_ref(), options)
}
pub fn append<P>(name: P) -> error::Result<MutableFile>
where
P: AsRef<std::path::Path>,
{
append_with(name, Options::default())
}
pub fn append_with<P>(name: P, options: Options) -> error::Result<MutableFile>
where
P: AsRef<std::path::Path>,
{
RawFile::append_with(name.as_ref(), options)
}
pub fn open<P>(name: P) -> error::Result<File>
where
P: AsRef<std::path::Path>,
{
open_with(name, Options::default())
}
pub fn open_with<P>(name: P, options: Options) -> error::Result<File>
where
P: AsRef<std::path::Path>,
{
RawFile::open_with(name.as_ref(), options)
}
#[cfg(feature = "memory")]
pub fn open_mem<'a>(name: Option<&str>, mem: &'a [u8]) -> error::Result<MemFile<'a>> {
RawFile::open_from_memory(name, mem)
}
lazy_static! {
pub(crate) static ref LOCK: Mutex<()> = Mutex::new(());
}
pub(crate) fn with_lock<F: FnMut() -> nc_type>(mut f: F) -> nc_type {
let _l = LOCK.lock().unwrap();
f()
}
pub(crate) mod utils {
use super::error;
use netcdf_sys::{NC_EMAXNAME, NC_MAX_NAME};
pub(crate) fn short_name_to_bytes(name: &str) -> error::Result<[u8; NC_MAX_NAME as usize + 1]> {
if name.len() > NC_MAX_NAME as _ {
Err(NC_EMAXNAME.into())
} else {
let len = name.bytes().position(|x| x == 0).unwrap_or(name.len());
let mut bytes = [0_u8; NC_MAX_NAME as usize + 1];
bytes[..len].copy_from_slice(name.as_bytes());
Ok(bytes)
}
}
}