批量读取netcdf文件并平均一个变量 [英] Batch read netcdf files and average one variable

查看:317
本文介绍了批量读取netcdf文件并平均一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R新用户.我现在有1979年的每日netcdf数据,例如: sm19790101.1.nc
sm19790102.1.nc
.
.
.
sm19791231.1.nc

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

我需要对一个称为"sm"的变量求平均值,以求月度分辨率.我现在可以这样做:

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)

将所有一月份的数据移植到jan,但是我不知道如何打开每个文件并获取特定的变量(我已经安装了Rnetcdf软件包).如果要手动执行此操作,则应为:

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

我想的问题是,如何读取文件名中包含变量(例如01-31)的文件,然后遍历整个月.

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.

推荐答案

看起来您可以将文件名组件"sm197901",天,.1.nc"粘贴在一起,以构造所需的文件名.

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

这篇关于批量读取netcdf文件并平均一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆