-
Notifications
You must be signed in to change notification settings - Fork 96
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
netcdf dimensions get messed up if they are out of order and if axis attributes are available #680
Comments
Nice ;-) We also have: (x1 = stars::read_ncdf(file2))
# no 'var' specified, using temperature
# other available variables:
# lat, lon, time
# Will return stars object with 54 cells.
# stars object with 3 dimensions and 1 attribute
# attribute(s):
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# temperature 1 15.25 110 110 204.75 219
# dimension(s):
# from to offset delta x/y
# lat 1 9 37.5 5 [x]
# time 1 2 1 1 [y]
# lon 1 3 -102.5 5
# Warning messages:
# 1: In .get_nc_time(dimensions, make_time, coord_var, rep_var, meta) :
# ignoring units of time dimension, not able to interpret
# 2: In .get_nc_projection(meta$attribute, rep_var, cv) :
# No projection information found in nc file.
(x2 = stars::read_stars(file2))
# stars object with 3 dimensions and 1 attribute
# attribute(s):
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# tas_example_5cbe857ee56c4.nc 1 15.25 110 110 204.75 219
# dimension(s):
# from to offset delta x/y
# x 1 9 0 1 [x]
# y 1 2 2 -1 [y]
# lat 1 3 40 5
(x3 = stars::read_mdim(file2))
# stars object with 3 dimensions and 1 attribute
# attribute(s):
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# temperature 1 15.25 110 110 204.75 219
# dimension(s):
# from to offset delta x/y
# time 1 9 0.5 1 [x]
# lon 1 2 -102.5 5 [y]
# lat 1 3 40 5
identical(as.vector(x1[[1]]), as.vector(x2[[1]]))
# [1] FALSE
identical(as.vector(x1[[1]]), as.vector(x3[[1]]))
# [1] TRUE |
But also identical(as.vector(x1[[1]]), as.vector(x2[[1]][,2:1,]))
# [1] TRUE so that is because GDAL reverts the y axis direction; and stars::read_mdim(file2, raster = c("lon", "lat"))
# stars object with 3 dimensions and 1 attribute
# attribute(s):
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# temperature 1 15.25 110 110 204.75 219
# dimension(s):
# from to offset delta x/y
# time 1 9 0.5 1
# lon 1 2 -102.5 5 [x]
# lat 1 3 40 5 [y] where offset is ok for lon, but not for time and lat. |
@mdsumner or @dblodgett-usgs could one of you take a look at the |
I think this is all good -- thanks for the thorough report @dschlaep !! I incorporated your reprex into a test and made the change. Question for @edzer I'm looking at the For the base example (without the axis attribute) we have:
In this example, there's nothing to go on to figure out that the 'lat' and 'lon' variables should be interpreted as latitude and longitude, so the
I see:
Should the dimension order be returning in x, y, t order here since we know what the order is? I'm don't remember how many places in |
honor axis attribute if different from native order -- fixes #680
@dblodgett-usgs thanks!
If you change > stars::read_ncdf("x.nc")
no 'var' specified, using temperature
other available variables:
lat, lon, time
Will return stars object with 54 cells.
No projection information found in nc file.
Coordinate variable units found to be degrees,
assuming WGS84 Lat/Lon.
stars object with 3 dimensions and 1 attribute
attribute(s):
Min. 1st Qu. Median Mean 3rd Qu. Max.
temperature 1 15.25 110 110 204.75 219
dimension(s):
from to offset delta refsys x/y
lon 1 3 -102.5 5 WGS 84 (CRS84) [x]
lat 1 9 37.5 5 WGS 84 (CRS84) [y]
time 1 2 1900-01-02 UTC 1 days POSIXct which gets the names right, but the dimensions wrong ( |
This adds some dim name matching to stars::read_mdim("x.nc")
# stars object with 3 dimensions and 1 attribute
# attribute(s):
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# temperature 1 15.25 110 110 204.75 219
# dimension(s):
# from to offset delta refsys x/y
# time 1 9 1900-01-02 1 days Date
# lon 1 2 -102.5 5 WGS 84 (CRS84) [x]
# lat 1 3 40 5 WGS 84 (CRS84) [y]
stars::read_mdim("x.nc") |> aperm(c(2,3,1))
# stars object with 3 dimensions and 1 attribute
# attribute(s):
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# temperature 1 15.25 110 110 204.75 219
# dimension(s):
# from to offset delta refsys x/y
# lon 1 2 -102.5 5 WGS 84 (CRS84) [x]
# lat 1 3 40 5 WGS 84 (CRS84) [y]
# time 1 9 1900-01-02 1 days Date |
@edzer -- see one more minor PR updating the test. |
Still,
|
Sorry, what do you see that's wrong there? |
|
(note to self: |
I see -- ok. I'll get in and see where that went wrong as soon as I can. |
Function
.get_dims()
sorts dimensions according to the canonical axis order if "axis" attributes are provided by the netcdf.However, this doesn't seem to work as expected if the dimensions in the netcdf are not already sorted. This seems to be caused by line 521 of file R/ncdf.R (https://github.com/r-spatial/stars/blob/f0156c9266ba16a8c4efc8da9702f053e98c871a/R/ncdf.R#L521C5-L521C48) which uses
instead of
(or similar).
It would be great if stars could read such netcdfs, thanks!
Created on 2024-04-17 with reprex v2.1.0
The text was updated successfully, but these errors were encountered: