Add back add_string_variable

This commit is contained in:
Magnus Ulimoen 2025-01-17 16:17:35 +01:00
parent 1e4d7d6be2
commit 3d281b1b4b
3 changed files with 49 additions and 0 deletions

View File

@ -472,6 +472,20 @@ 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

View File

@ -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>(
@ -269,6 +270,20 @@ 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>>> {

View File

@ -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);
}
}