Skip to content

Commit

Permalink
Merge pull request #51 from magnusuMET/ownership
Browse files Browse the repository at this point in the history
Ownership
  • Loading branch information
magnusuMET committed Jan 7, 2020
2 parents 3ef1570 + 69d3c53 commit a9bb3ff
Show file tree
Hide file tree
Showing 11 changed files with 1,299 additions and 1,013 deletions.
28 changes: 15 additions & 13 deletions examples/ncdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,43 @@ fn main() {
fn run(path: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> {
let file = netcdf::open(path)?;

println!("{}", file.name());
print_group(&file)
println!("{}", file.path()?);
print_group(&file.root())
}

fn print_group(g: &netcdf::group::Group) -> Result<(), Box<dyn std::error::Error>> {
println!("Group: {}", g.name());
println!("Group: {}", g.name()?);
println!("Dimensions:");
for d in g.dimensions() {
for d in g.dimensions()? {
let d = d?;
if d.is_unlimited() {
println!("\t{} : Unlimited ({})", d.name(), d.len());
println!("\t{} : Unlimited ({})", d.name()?, d.len());
} else {
println!("\t{} : ({})", d.name(), d.len());
println!("\t{} : ({})", d.name()?, d.len());
}
}
println!("Variables:");
for v in g.variables() {
print!("\t{}", v.name());
for v in g.variables()? {
let v = v?;
print!("\t{}", v.name()?);
print!("(");
for d in v.dimensions() {
print!(" {} ", d.name());
print!(" {} ", d.name()?);
}
println!(")");
for a in v.attributes()? {
let a = a?;
println!("\t\t{} = {:?}", a.name().unwrap(), a.value()?);
println!("\t\t{} = {:?}", a.name()?, a.value()?);
}
}
println!("Attributes:");
for a in g.attributes()? {
let a = a?;
println!("\t\t{} = {:?}", a.name().unwrap(), a.value()?);
println!("\t\t{} = {:?}", a.name()?, a.value()?);
}
for g in g.groups() {
for g in g.groups()? {
println!();
print_group(g)?;
print_group(&g)?;
}

Ok(())
Expand Down
33 changes: 18 additions & 15 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use super::error;
use super::LOCK;
use netcdf_sys::*;
use std::convert::TryInto;
use std::marker::PhantomData;

/// Extra properties of a variable or a group can be represented
Expand All @@ -14,10 +15,12 @@ pub struct Attribute<'a> {
pub(crate) name: [u8; NC_MAX_NAME as usize + 1],
/// Group or file this attribute is in
pub(crate) ncid: nc_type,
/// Variable/global this id is connected to
/// Variable/global this id is connected to. This is
/// set to NC_GLOBAL when attached to a group
pub(crate) varid: nc_type,
/// Holds the variable/group to prevent the
/// attribute being deleted or modified
/// attribute being deleted or modified from
/// under us
pub(crate) _marker: PhantomData<&'a nc_type>,
}

Expand All @@ -44,9 +47,10 @@ impl<'a> Attribute<'a> {
.name
.iter()
.position(|&x| x == 0)
.unwrap_or(self.name.len());
.unwrap_or_else(|| self.name.len());
std::str::from_utf8(&self.name[..zeropos])
}
/// Number of elements in this attribute
fn num_elems(&self) -> error::Result<usize> {
let _l = LOCK.lock().unwrap();
let mut nelems = 0;
Expand All @@ -60,6 +64,7 @@ impl<'a> Attribute<'a> {
}
Ok(nelems as _)
}
/// Type of this attribute
fn typ(&self) -> error::Result<nc_type> {
let mut atttype = 0;
unsafe {
Expand All @@ -73,6 +78,10 @@ impl<'a> Attribute<'a> {
Ok(atttype)
}
/// Get the value of the attribute
///
/// # Errors
///
/// Unsupported type or netcdf error
#[allow(clippy::too_many_lines)]
pub fn value(&self) -> error::Result<AttrValue> {
let attlen = self.num_elems()?;
Expand Down Expand Up @@ -365,6 +374,7 @@ impl<'a> Attribute<'a> {
}
}

/// Iterator over all attributes for a location
pub(crate) struct AttributeIterator<'a> {
ncid: nc_type,
varid: Option<nc_type>,
Expand All @@ -386,7 +396,7 @@ impl<'a> AttributeIterator<'a> {
Ok(Self {
ncid,
varid,
natts: natts as _,
natts: natts.try_into()?,
current_natt: 0,
_marker: PhantomData,
})
Expand All @@ -405,7 +415,7 @@ impl<'a> Iterator for AttributeIterator<'a> {
if let Err(e) = error::checked(nc_inq_attname(
self.ncid,
self.varid.unwrap_or(NC_GLOBAL),
self.current_natt as _,
self.current_natt.try_into().unwrap(),
name.as_mut_ptr() as *mut _,
)) {
return Some(Err(e));
Expand Down Expand Up @@ -454,22 +464,15 @@ pub enum AttrValue {

impl<'a> Attribute<'a> {
#[allow(clippy::needless_pass_by_value)] // All values will be small
#[allow(clippy::too_many_lines)]
pub(crate) fn put(
ncid: nc_type,
varid: nc_type,
name: &str,
val: AttrValue,
) -> error::Result<Self> {
let cname = {
if name.len() > NC_MAX_NAME as usize {
return Err(error::Error::Netcdf(NC_EMAXNAME));
}
let mut attname = [0_u8; NC_MAX_NAME as usize + 1];
attname[..name.len()].copy_from_slice(name.as_bytes());
attname
};
let cname = super::utils::short_name_to_bytes(name)?;

let _l = LOCK.lock().unwrap();
error::checked(unsafe {
match val {
AttrValue::Uchar(x) => {
Expand Down Expand Up @@ -629,7 +632,7 @@ impl<'a> Attribute<'a> {

Ok(Some(Attribute {
name: attname,
ncid: ncid,
ncid,
varid: varid.unwrap_or(NC_GLOBAL),
_marker: PhantomData,
}))
Expand Down
Loading

0 comments on commit a9bb3ff

Please sign in to comment.