I have a NETCDF file with attributes being lon,lat,time,precipitation. The data covers a certain spatial domain. It is daily data from 1960 to 2100.
1) I will like to subset the data spatially (e.g lat[45,50] and lon[-78,-85])from the main domain
2) From the subset, I will like to average over all grids and produce a single column daily time series then write it to a .csv file.
NB: my data contains missing values
Something along these lines ought to work
library(raster)
b <- brick('file.nc')
be <- crop(b, extent(-85, -78, 45, 50))
a <- aggregate(be, dim(be)[2:1], na.rm=TRUE)
v <- values(a)
write.csv(v, 'precip.csv', row.names=FALSE)
Normally, to get the date as well:
date <- getZ(be)
Or
date <- names(a)
date <- gsub('X', '', date)
And then
v <- data.frame(date=date, prec=v)
write.csv(v, 'precip.csv', row.names=FALSE)
Whether or not this date is human readable depends on how it is stored in the ncdf file (i.e. whether certain conventions are followed or not).