You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When time is codes as (years), writing with writeCDF leads to a nc without units (and it looks like the time is coded as seconds, which might lead to issues if we have dates in the deep past). If we try to read the file we just generated back with rast, time is then lost. Here is a a simple reprex:
a <- rast(ncols=5, nrows=5, nl=50)
values(a) <- 1:prod(dim(a))
time(a, tstep="years") <- (1:50) * 20
writeCDF(a,"b.nc",varname = "bio01",overwrite=TRUE)
# read back the file
b <- rast("b.nc")
# but b has no time, as we just get NA with
time(b)
Here is an illustration of a simple fix (that could be implemented in writeCDF):
# to fix it, we remove the units before saving (so that we save the raw years, without conversion into other units)
time(a, tstep="") <- time(a)
writeCDF(a,"c.nc",varname = "bio01",overwrite=TRUE)
# and then fix the units in the netcdf file
nc_in <- ncdf4::nc_open("c.nc", write=TRUE)
ncdf4::ncatt_put(nc_in,varid="time",
attname = "units",
attval = "years since 0-01-01 00:00:00.0")
#not strictly necessary, but it helps with some other libraries to recognise the time axis, so it would be a nice addition:
ncdf4::ncatt_put(nc_in, varid="time", attname="axis", attval = "T")
ncdf4::nc_close(nc_in)
# now when we read the file, we have time coded as years and with the same values as in the original raster
c <- rast("c.nc")
time(a)==time(c)
So, I would suggest writing the time in years, and using "years since 0-01-01 00:00:00.0" as the units (thus closely mirroring how time is represented in the original SpatRaster. Thanks!
The text was updated successfully, but these errors were encountered:
When time is codes as (years), writing with
writeCDF
leads to a nc without units (and it looks like the time is coded as seconds, which might lead to issues if we have dates in the deep past). If we try to read the file we just generated back withrast
, time is then lost. Here is a a simple reprex:Here is an illustration of a simple fix (that could be implemented in
writeCDF
):So, I would suggest writing the time in years, and using "years since 0-01-01 00:00:00.0" as the units (thus closely mirroring how time is represented in the original
SpatRaster
. Thanks!The text was updated successfully, but these errors were encountered: