Batch read netcdf files and average one variable

懵懂的女人 提交于 2019-12-08 10:42:39

问题


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.


回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/15956034/batch-read-netcdf-files-and-average-one-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!