0

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.

EDU
  • 267
  • 1
  • 5
  • 13
  • Excuse me. You tagged this question with "batch-file" tag. Do you want a Windows-DOS Batch .bat file solution? If yes, please post the format (contents) of the daily files and an indication of where the "sm" variable is located in them. If not, please remove the "batch-file" tag from your question. – Aacini Apr 12 '13 at 01:56

3 Answers3

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
Dave X
  • 4,831
  • 4
  • 31
  • 42
0

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)
})
kith
  • 5,486
  • 1
  • 21
  • 21
  • Thanks Kith for the prompt help! So in the last part of your script, what should I put in open.nc() as the filename? – EDU Apr 11 '13 at 21:28
  • filename is a variable containing the constructed filename. When you call open.nc(filename) you're passing the constructed name to the function. Uncomment the print(filename) line, and it will print out the constructed filenames. – kith Apr 11 '13 at 21:33
  • Right, I then wanted to get variable from the list, like: `var.get.nc(ncfiles,"sm",na.mode=0)` But it says class(ncfile)=="NetCDF" is not TRUE. Looks like ncfiles is a list of file we just constructed, which can't be access directly be the nc file operator. – EDU Apr 11 '13 at 22:26
  • The reason I asked is that `var.get.nc()` can only read object of class "NetCDF" returned from `open.nc()` – EDU Apr 11 '13 at 23:45
  • I'm not sure if the var.get.nc is a vectorized function. Have you tried sapply(ncfiles, function(file){var.get.nc(file, "sm", na.mode=0)}) instead? This applies the var.get.nc function to each element of the ncfiles list and returns the result. – kith Apr 12 '13 at 16:05
  • Yes the this way the `var.get.nc` indeed reads sm data as vector from day 1 to 31. It's easier for me to do the averaging then. Thank you so much for helping me out, Kith. You have a great weekend! – EDU Apr 12 '13 at 16:28
  • No problem. If you hit the accept answer button I'll get some rep. This site is like a game where you gain points for being helpful. And you can spend some of your points to ask a question that gets seen by lots of people. – kith Apr 12 '13 at 16:34
0

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.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86