Taking a raster file of monthly temperature data for multiple years which has a name attached accessible via names(object)
in the following format 'Jan.1981', 'Feb.1981' etc (example files for two years that works with code below here - adding all files makes it too big.
Reading in and writing this to NetCDF using the following code:
#Load Packages
library(raster)
library(ncdf4)
#Read in temperature files
r1 <- brick('TavgM_1981.grd')
r2 <- brick('TavgM_1982.grd')
#stack them together
TempStack = stack(r1, r2)
#set the coordinate system (as it was missing)
crs(TempStack) <- ('+proj=lcc +lat_1=53.5 +lat_2=53.5 +lat_0=46.834 +lon_0=5 +x_0=1488375 +y_0=-203375 +datum=WGS84 +to_meter=2500 +no_defs +ellps=WGS84 +towgs84=0,0,0')
#reproject to get in lat/lon instead of meters
TempStack<-projectRaster(TempStack, crs=CRS("+init=epsg:4326"))
#Extract monthly data names to assign to netCDf later
names <- names(TempStack)
#write the raster file to NetCDF
writeRaster(TempStack, "Temp.nc", overwrite=TRUE, format="CDF", varname="Temperature", varunit="degC",
longname="Temperature -- raster stack to netCDF, monthly average", xname="Longitude", yname="Latitude", zname='Time', zunit=names)
When I write this to NetCDF and plot the monthly data it is organised from month 1 to month 24, but I want it to have 'Jan 1981', 'Feb 1981' etc.
I thought by adding the zunit argument in writeRaster would work, but it doesn't, the numbers are all still 1-24 instead of Jan, Feb etc.