Reputation: 267
I'm a new R user. I now have daily netcdf data for year 1979 such as these:
sm19790101.1.nc
sm19790102.1.nc
.
.
.
sm19791231.1.nc
I need to average a variable called "sm" to monthly resolution. I can now do this:
glob2rx("sm197901*.1.nc")
jan<-list.files(pattern=glob2rx("sm197901*.1.nc"),full.names=TRUE)
to port all January data to jan, but I don't know how to open each file and get specific variable (I've had Rnetcdf package installed) . If I were to do this manually, it should be:
s19790101<-open.nc("sm19790101.1.nc")
sm19790101<-var.get.nc(s19790101,"sm",na.mode=0)
and then average them...
I guess the question is how to read files with a variable (e.g. 01-31) as part of the file name and then loop through the whole month.
Upvotes: 0
Views: 2189
Reputation: 8087
parallel to Dave's ncra answer you can also do it with cdo
cdo mergetime sm1979????.1.nc year.nc
# you only need this next step if there is more than one variable in the file:
cdo selvar,sm year.nc yearsm.nc
cdo monmean year.nc month.nc
On some systems the number of open files is limited to 256 - if this is your case you can replace "mergetime" with "cat" and I think it should still work since the files will be listed in time order.
Upvotes: 0
Reputation: 5137
If you have a lot of data to summarize, you could summarize the daily data into monthly means with the NetCDF Operator tool http://nco.sourceforge.net/nco.html#ncra-netCDF-Record-Averager
ncra DAILY/sm197901[*].1.nc MONTHLY/sm197901.1.nc
Upvotes: 2
Reputation: 5566
It looks like you can paste together the filename components "sm197901", day, ".1.nc" construct the desired filename.
#make sure it has a leading 0
days = formatC(1:31, width=2, flag="0")
ncfiles = lapply(days, function(d){
filename = paste("sm197901", d, ".1.nc", sep="")
#print(filename)
open.nc(filename)
})
Upvotes: 0