Skip to content

Commit

Permalink
Add back add_string_variable
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusuMET committed Jan 17, 2025
1 parent 1e4d7d6 commit 195ab57
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions netcdf/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,21 @@ impl FileMut {
super::variable::add_variable_from_identifiers(ncid, name, dims, xtype)
}

/// Create a Variable containing strings into the dataset, with no data written into it
///
/// Dimensions are identified using the name of the dimension, and will recurse upwards
/// if not found in the current group.
pub fn add_string_variable<'f>(
&mut self,
name: &str,
dims: &[&str],
) -> error::Result<VariableMut<'f>>
{
let typ = crate::types::NcVariableType::String;
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
VariableMut::add_from_str(ncid, &typ, name, dims)
}

/// Flush pending buffers to disk to minimise data loss in case of termination.
///
/// Note: When writing and reading from the same file from multiple processes
Expand Down
16 changes: 16 additions & 0 deletions netcdf/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ impl<'f> GroupMut<'f> {
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.id(), name)?;
VariableMut::add_from_str(ncid, &T::type_descriptor(), name, dims)
}

/// Adds a variable from a set of unique identifiers, recursing upwards
/// from the current group if necessary.
pub fn add_variable_from_identifiers<'g, T>(
Expand Down Expand Up @@ -269,6 +270,21 @@ impl<'f> GroupMut<'f> {
};
super::variable::add_variable_from_identifiers(ncid, name, dims, xtype)
}

/// Create a Variable containing strings into the dataset, with no data written into it
///
/// Dimensions are identified using the name of the dimension, and will recurse upwards
/// if not found in the current group.
pub fn add_string_variable(
&mut self,
name: &str,
dims: &[&str],
) -> error::Result<VariableMut<'f>>
{
let typ = crate::types::NcVariableType::String;
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.id(), name)?;
VariableMut::add_from_str(ncid, &typ, name, dims)
}
}

pub(crate) fn groups_at_ncid<'f>(ncid: nc_type) -> error::Result<impl Iterator<Item = Group<'f>>> {
Expand Down
20 changes: 20 additions & 0 deletions netcdf/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,23 @@ fn no_subtype() {
file.add_type::<Bar>().unwrap();
file.add_type::<FooBar>().unwrap();
}

#[test]
fn add_string_variable() {
let d = tempfile::tempdir().unwrap();
let path = d.path().join("stringy.nc");
{
let mut file = netcdf::create(path.clone()).unwrap();
file.add_string_variable("s", &[]).unwrap();

let mut group = file.add_group("g").unwrap();
group.add_string_variable("str", &[]).unwrap();
}
{
let file = netcdf::open(path).unwrap();
let var = file.variable("s").unwrap();
assert_eq!(var.vartype(), NcVariableType::String);
let var = file.variable("g/str").unwrap();
assert_eq!(var.vartype(), NcVariableType::String);
}
}

0 comments on commit 195ab57

Please sign in to comment.