Skip to content

Commit

Permalink
Merge pull request #113 from AntoineRenaud91/path_subgroup_access
Browse files Browse the repository at this point in the history
Path subgroup access
  • Loading branch information
magnusuMET authored Sep 6, 2023
2 parents 21be785 + a1d052f commit 8d52da6
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 117 deletions.
1 change: 1 addition & 0 deletions netcdf/src/extent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ mod test {
}

#[test]
#[allow(clippy::reversed_empty_ranges)]
fn test_extent() -> error::Result<()> {
let _ = take_extent(1)?;
let _ = take_extent(1..)?;
Expand Down
61 changes: 44 additions & 17 deletions netcdf/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Open, create, and append netcdf files

#![allow(clippy::similar_names)]
use crate::{get_parent_ncid_and_stem, try_get_ncid, try_get_parent_ncid_and_stem};

use super::attribute::{AttrValue, Attribute};
use super::dimension::{self, Dimension};
use super::error;
Expand Down Expand Up @@ -179,18 +181,20 @@ impl File {

/// Get a variable from the group
pub fn variable<'f>(&'f self, name: &str) -> Option<Variable<'f>> {
Variable::find_from_name(self.ncid(), name).unwrap()
let (ncid, name) =
super::group::try_get_parent_ncid_and_stem(self.ncid(), name).unwrap()?;
Variable::find_from_name(ncid, name).unwrap()
}
/// Iterate over all variables in a group
pub fn variables(&self) -> impl Iterator<Item = Variable> {
super::variable::variables_at_ncid(self.ncid())
.unwrap()
.map(Result::unwrap)
}

/// Get a single attribute
pub fn attribute<'f>(&'f self, name: &str) -> Option<Attribute<'f>> {
Attribute::find_from_name(self.ncid(), None, name).unwrap()
let (ncid, name) = try_get_parent_ncid_and_stem(self.ncid(), name).unwrap()?;
Attribute::find_from_name(ncid, None, name).unwrap()
}
/// Get all attributes in the root group
pub fn attributes(&self) -> impl Iterator<Item = Attribute> {
Expand All @@ -201,7 +205,9 @@ impl File {

/// Get a single dimension
pub fn dimension<'f>(&self, name: &str) -> Option<Dimension<'f>> {
super::dimension::dimension_from_name(self.ncid(), name).unwrap()
let (ncid, name) =
super::group::try_get_parent_ncid_and_stem(self.ncid(), name).unwrap()?;
super::dimension::dimension_from_name(ncid, name).unwrap()
}
/// Iterator over all dimensions in the root group
pub fn dimensions(&self) -> impl Iterator<Item = Dimension> {
Expand All @@ -216,7 +222,13 @@ impl File {
///
/// Not a `netCDF-4` file
pub fn group<'f>(&'f self, name: &str) -> error::Result<Option<Group<'f>>> {
super::group::group_from_name(self.ncid(), name)
let (ncid, name) = get_parent_ncid_and_stem(self.ncid(), name)?;
try_get_ncid(ncid, name).map(|ncid: Option<i32>| {
ncid.map(|ncid| Group {
ncid,
_file: PhantomData,
})
})
}
/// Iterator over all subgroups in the root group
///
Expand Down Expand Up @@ -251,7 +263,6 @@ impl MutableFile {
pub fn root_mut(&mut self) -> Option<GroupMut> {
self.root().map(|root| GroupMut(root, PhantomData))
}

/// Get a mutable variable from the group
pub fn variable_mut<'f>(&'f mut self, name: &str) -> Option<VariableMut<'f>> {
self.variable(name).map(|var| VariableMut(var, PhantomData))
Expand Down Expand Up @@ -295,12 +306,14 @@ impl MutableFile {
where
T: Into<AttrValue>,
{
Attribute::put(self.ncid(), NC_GLOBAL, name, val.into())
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
Attribute::put(ncid, NC_GLOBAL, name, val.into())
}

/// Adds a dimension with the given name and size. A size of zero gives an unlimited dimension
pub fn add_dimension<'f>(&'f mut self, name: &str, len: usize) -> error::Result<Dimension<'f>> {
super::dimension::add_dimension_at(self.ncid(), name, len)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
super::dimension::add_dimension_at(ncid, name, len)
}
/// Adds a dimension with unbounded size
pub fn add_unlimited_dimension(&mut self, name: &str) -> error::Result<Dimension> {
Expand All @@ -309,7 +322,13 @@ impl MutableFile {

/// Add an empty group to the dataset
pub fn add_group<'f>(&'f mut self, name: &str) -> error::Result<GroupMut<'f>> {
GroupMut::add_group_at(self.ncid(), name)
Ok(GroupMut(
Group {
ncid: super::group::add_group_at_path(self.ncid(), name)?,
_file: PhantomData,
},
PhantomData,
))
}

/// Create a Variable into the dataset, with no data written into it
Expand All @@ -324,7 +343,8 @@ impl MutableFile {
where
T: NcPutGet,
{
VariableMut::add_from_str(self.ncid(), T::NCTYPE, name, dims)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
VariableMut::add_from_str(ncid, T::NCTYPE, name, dims)
}

/// Create a variable with the specified type
Expand All @@ -334,7 +354,8 @@ impl MutableFile {
dims: &[&str],
typ: &super::types::VariableType,
) -> error::Result<VariableMut<'f>> {
VariableMut::add_from_str(self.ncid(), typ.id(), name, dims)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
VariableMut::add_from_str(ncid, typ.id(), name, dims)
}

/// Add an opaque datatype, with `size` bytes
Expand All @@ -343,30 +364,34 @@ impl MutableFile {
name: &str,
size: usize,
) -> error::Result<super::types::OpaqueType> {
super::types::OpaqueType::add(self.ncid(), name, size)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
super::types::OpaqueType::add(ncid, name, size)
}
/// Add a variable length datatype
pub fn add_vlen_type<T: NcPutGet>(
&mut self,
name: &str,
) -> error::Result<super::types::VlenType> {
super::types::VlenType::add::<T>(self.ncid(), name)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
super::types::VlenType::add::<T>(ncid, name)
}
/// Add an enum datatype
pub fn add_enum_type<T: NcPutGet>(
&mut self,
name: &str,
mappings: &[(&str, T)],
) -> error::Result<super::types::EnumType> {
super::types::EnumType::add::<T>(self.ncid(), name, mappings)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
super::types::EnumType::add::<T>(ncid, name, mappings)
}

/// Build a compound type
pub fn add_compound_type(
&mut self,
name: &str,
) -> error::Result<super::types::CompoundBuilder> {
super::types::CompoundType::add(self.ncid(), name)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
super::types::CompoundType::add(ncid, name)
}

/// Adds a variable with a basic type of string
Expand All @@ -375,7 +400,8 @@ impl MutableFile {
name: &str,
dims: &[&str],
) -> error::Result<VariableMut<'f>> {
VariableMut::add_from_str(self.ncid(), NC_STRING, name, dims)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
VariableMut::add_from_str(ncid, NC_STRING, name, dims)
}
/// Adds a variable from a set of unique identifiers, recursing upwards
/// from the current group if necessary.
Expand All @@ -387,7 +413,8 @@ impl MutableFile {
where
T: NcPutGet,
{
super::variable::add_variable_from_identifiers(self.ncid(), name, dims, T::NCTYPE)
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
super::variable::add_variable_from_identifiers(ncid, name, dims, T::NCTYPE)
}
}

Expand Down
Loading

0 comments on commit 8d52da6

Please sign in to comment.