Skip to content

Commit 3d281b1

Browse files
committed
Add back add_string_variable
1 parent 1e4d7d6 commit 3d281b1

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

netcdf/src/file.rs

+14
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,20 @@ impl FileMut {
472472
super::variable::add_variable_from_identifiers(ncid, name, dims, xtype)
473473
}
474474

475+
/// Create a Variable containing strings into the dataset, with no data written into it
476+
///
477+
/// Dimensions are identified using the name of the dimension, and will recurse upwards
478+
/// if not found in the current group.
479+
pub fn add_string_variable<'f>(
480+
&mut self,
481+
name: &str,
482+
dims: &[&str],
483+
) -> error::Result<VariableMut<'f>> {
484+
let typ = crate::types::NcVariableType::String;
485+
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
486+
VariableMut::add_from_str(ncid, &typ, name, dims)
487+
}
488+
475489
/// Flush pending buffers to disk to minimise data loss in case of termination.
476490
///
477491
/// Note: When writing and reading from the same file from multiple processes

netcdf/src/group.rs

+15
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ impl<'f> GroupMut<'f> {
227227
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.id(), name)?;
228228
VariableMut::add_from_str(ncid, &T::type_descriptor(), name, dims)
229229
}
230+
230231
/// Adds a variable from a set of unique identifiers, recursing upwards
231232
/// from the current group if necessary.
232233
pub fn add_variable_from_identifiers<'g, T>(
@@ -269,6 +270,20 @@ impl<'f> GroupMut<'f> {
269270
};
270271
super::variable::add_variable_from_identifiers(ncid, name, dims, xtype)
271272
}
273+
274+
/// Create a Variable containing strings into the dataset, with no data written into it
275+
///
276+
/// Dimensions are identified using the name of the dimension, and will recurse upwards
277+
/// if not found in the current group.
278+
pub fn add_string_variable(
279+
&mut self,
280+
name: &str,
281+
dims: &[&str],
282+
) -> error::Result<VariableMut<'f>> {
283+
let typ = crate::types::NcVariableType::String;
284+
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.id(), name)?;
285+
VariableMut::add_from_str(ncid, &typ, name, dims)
286+
}
272287
}
273288

274289
pub(crate) fn groups_at_ncid<'f>(ncid: nc_type) -> error::Result<impl Iterator<Item = Group<'f>>> {

netcdf/tests/types.rs

+20
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,23 @@ fn no_subtype() {
483483
file.add_type::<Bar>().unwrap();
484484
file.add_type::<FooBar>().unwrap();
485485
}
486+
487+
#[test]
488+
fn add_string_variable() {
489+
let d = tempfile::tempdir().unwrap();
490+
let path = d.path().join("stringy.nc");
491+
{
492+
let mut file = netcdf::create(path.clone()).unwrap();
493+
file.add_string_variable("s", &[]).unwrap();
494+
495+
let mut group = file.add_group("g").unwrap();
496+
group.add_string_variable("str", &[]).unwrap();
497+
}
498+
{
499+
let file = netcdf::open(path).unwrap();
500+
let var = file.variable("s").unwrap();
501+
assert_eq!(var.vartype(), NcVariableType::String);
502+
let var = file.variable("g/str").unwrap();
503+
assert_eq!(var.vartype(), NcVariableType::String);
504+
}
505+
}

0 commit comments

Comments
 (0)