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

function sfc2xy drops other dimensions to attributes #733

Closed
higgicd opened this issue Feb 5, 2025 · 2 comments
Closed

function sfc2xy drops other dimensions to attributes #733

higgicd opened this issue Feb 5, 2025 · 2 comments

Comments

@higgicd
Copy link

higgicd commented Feb 5, 2025

Hi @edzer et al., I have been putting some time into learning this wonderful package and finding it very useful. One thing I have encountered is when switching between an sfc geometry to xy raster cells, that the other dimensions get dropped back to attributes.

For example:

# read nc sample file
f <- system.file("nc/reduced.nc", package = "stars")
reduced_nc <- read_ncdf(f)
reduced_nc

stars object with 4 dimensions and 4 attributes
attribute(s):
               Min. 1st Qu. Median       Mean 3rd Qu.  Max.  NA's
sst [°C]      -1.80   -0.03 13.655 12.9940841 24.8125 32.97  4448
anom [°C]     -7.95   -0.58 -0.080 -0.1847324  0.2100  2.99  4449
err [°C]       0.11    0.16  0.270  0.2626872  0.3200  0.84  4448
ice [percent]  0.01    0.47  0.920  0.7178118  0.9600  1.00 13266
dimension(s):
     from  to offset delta         refsys         values x/y
lon     1 180     -1     2 WGS 84 (CRS84)           NULL [x]
lat     1  90    -90     2 WGS 84 (CRS84)           NULL [y]
zlev    1   1     NA    NA             NA              0    
time    1   1     NA    NA        POSIXct 1981-12-31 UTC 

Converting the cells to points works as expected, maintaining zlev and time as dimensions:

reduced_nc_points <- reduced_nc |> stars::st_xy2sfc(as_points = TRUE)
reduced_nc_points

stars object with 3 dimensions and 4 attributes
attribute(s):
               Min. 1st Qu. Median       Mean 3rd Qu.  Max. NA's
sst [°C]      -1.80   -0.03 13.655 12.9940841 24.8125 32.97    8
anom [°C]     -7.95   -0.58 -0.080 -0.1847324  0.2100  2.99    9
err [°C]       0.11    0.16  0.270  0.2626872  0.3200  0.84    8
ice [percent]  0.01    0.47  0.920  0.7178118  0.9600  1.00 8826
dimension(s):
         from    to         refsys point                             values
geometry    1 11760 WGS 84 (CRS84)  TRUE POINT (164 -79),...,POINT (358 89)
zlev        1     1             NA    NA                                  0
time        1     1        POSIXct    NA                     1981-12-31 UTC

However, going back the other direction moves zlev and time to attributes:

reduced_nc_rast <- reduced_nc_points |> stars::st_sfc2xy()
reduced_nc_rast

stars object with 2 dimensions and 6 attributes
attribute(s):
              Min.      1st Qu.        Median          Mean      3rd Qu.         Max.  NA's
zlev          0.00         0.00         0.000  0.000000e+00 0.000000e+00         0.00  3540
time  378604800.00 378604800.00 378604800.000  3.786048e+08 3.786048e+08 378604800.00  3540
sst          -1.80        -0.03        13.655  1.299408e+01 2.481250e+01        32.97  3548
anom         -7.95        -0.58        -0.080 -1.847324e-01 2.100000e-01         2.99  3549
err           0.11         0.16         0.270  2.626872e-01 3.200000e-01         0.84  3548
ice           0.01         0.47         0.920  7.178118e-01 9.600000e-01         1.00 12366
dimension(s):
  from  to offset delta         refsys x/y
X    1 180     -1     2 WGS 84 (CRS84) [x]
Y    1  85     90    -2 WGS 84 (CRS84) [y]

Just sparking my curiosity about whether that is expected behaviour, if I am missing something in the options for st_sfc2xy, or how one might put the time and zlev dimensions back. Thanks everyone.

edzer added a commit that referenced this issue Feb 5, 2025
@edzer
Copy link
Member

edzer commented Feb 5, 2025

Thanks; example(st_xy2sfc) now has your example working

@higgicd
Copy link
Author

higgicd commented Feb 5, 2025

That is excellent, a quick fix! For anyone coming across this issue in the future, passing the dims argument through the ... in the updated function now maintains the other dimensions:

reduced_nc_rast <- reduced_nc_points |> st_sfc2xy(dims=c("X", "Y", "zlev", "time"))
reduced_nc_rast

stars object with 4 dimensions and 4 attributes
attribute(s):
       Min. 1st Qu. Median       Mean 3rd Qu.  Max.  NA's
sst   -1.80   -0.03 13.655 12.9940841 24.8125 32.97  3548
anom  -7.95   -0.58 -0.080 -0.1847324  0.2100  2.99  3549
err    0.11    0.16  0.270  0.2626872  0.3200  0.84  3548
ice    0.01    0.47  0.920  0.7178118  0.9600  1.00 12366
dimension(s):
     from  to offset delta         refsys         values x/y
X       1 180     -1     2 WGS 84 (CRS84)           NULL [x]
Y       1  85     90    -2 WGS 84 (CRS84)           NULL [y]
zlev    1   1     NA    NA             NA         [0,NA)    
time    1   1     NA    NA        POSIXct 1981-12-31 UTC   

@higgicd higgicd closed this as completed Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants