georust / netcdf

High-level netCDF bindings for Rust
Apache License 2.0
81 stars 28 forks source link

Wrong values fetched #110

Closed danielboros1990 closed 1 year ago

danielboros1990 commented 1 year ago

Hi guys,

I try to fetch the variables from the a .nc file but I got totally different values instead of the expected. Do you have any idea how can I solve?

        let nc_file = netcdf::open(file).unwrap();
        let longitude = nc_file.variable("longitude").unwrap();
        let latitude = nc_file.variable("latitude").unwrap();
        let time = nc_file.variable("time").unwrap();
        let value = nc_file.variables().last().unwrap();

        let longitude_values = longitude.values::<f32, _>(..).unwrap();
        let latitude_values = latitude.values::<f32, _>(..).unwrap();
        let time_values = time.values::<i32, _>(..).unwrap();
        let value_values = value.values::<f32, _>(..).unwrap();

This is the data structure of the netcdf file:

Képernyőfotó 2023-06-14 - 8 48 44

This is what I get from the fetch:

Képernyőfotó 2023-06-14 - 8 49 49

This is the expectation:

Képernyőfotó 2023-06-14 - 8 50 18
mulimoen commented 1 year ago

We don't apply scale_factor and add_offset and this is up to the user to account for

danielboros1990 commented 1 year ago

@mulimoen is it possible to read them from the file?

mulimoen commented 1 year ago

You should be able to read them using var.attribute()

danielboros1990 commented 1 year ago

@mulimoen thanks for your help. One question is left, is it possible to convert this value into f32, I couldn't find the Into trait implementation for AttrValue?

magnusuMET commented 1 year ago

The following should work

fn convert(a: &AttrValue) -> Option<f64> {
    use AttrValue::*;
    Some(match a {
        Uchar(x) => *x as f64,
        Schar(x) => *x as f64,
        Short(x) => *x as f64,
        Ushort(x) => *x as f64,
        Uint(x) => *x as f64,
        Int(x) => *x as f64,
        Ulonglong(x) => *x as f64,
        Longlong(x) => *x as f64,
        Float(x) => *x as f64,
        Double(x) => *x as f64,
        _ => return None
    })
}

Future improvement/note to self: Might be worth including such code in this library

danielboros1990 commented 1 year ago

I really appreciate your help.