Adding dates to rasterstack at one year interval instead of days for NetCDF time field?

28 Views Asked by At

I'm trying to assign dates to a rasterstack to then convert to a NetCDF for use in multidimensional rasters/ space-time cubes. This is for a time span for years 1986-2021, where each layer in the stack is a year. I need the time interval in the CDF to be year, but can only get it to give me days.

If using as.date I can get things to run in Arc with it recognizing the time, but it's in intervals of days (so it just adds 35 days and then the time period ends up being 1986-12-31 to 1987-02-04) instead of years:

> time(afg_stack)<-as.Date("1986-12-31")+1:nlyr(afg_stack)

I can get it to interval by year using as.numeric like this:

time(afg_stack)<-as.numeric("1985")+1:nlyr(afg_stack)

But that doesn't result in a valid time field to use in the space-time cube.

From here it goes into this to generate the NetCDF:

if(require(ncdf4)){writeCDF(afg_stack,"C:\\tmp\\GIS_projects_tmp\\GJFO_RAP_AFG_CDFtest8.nc",overwrite=TRUE, unit="% Cover", zname="Year", varname="RAP_AFG", longname="RAP Annual Forb & Grass Cover") }


How can I get it to interval by year instead of just counting days since 1986-12-31?

using raster, terra, etc.

0

There are 0 best solutions below