Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back add_string_variable #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions netcdf/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 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,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>>> {
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);
}
}
Loading