Generate timeseries vector (spatial average) from

2019-06-13 04:38发布

问题:

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

回答1:

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).



回答2:

This would do the cut and the averaging but writes the answer to another netcdf. If you really need CSV, then you would need to use that part of the solution above.

cdo fldmean -sellonlatbox,-85,-78,45,50 in.nc out.nc