0

By using R ill try to open my NetCDF data that contain 5 dimensional space with 15 variables. (variable for calculation is in matrix 1000X920 )

This problem actually look like the same with the other question before.

I got explanation from here and the others

At first I used RNetCDF package, but after some trial i found unconsistensy when the package read my data. And then finally better after used ncdf package.

there is no problem for opening data in a single file, but after ill try for looping in more than hundred data inside folder for a spesific variable (for example: var no 15) the program was failed.

>  days = formatC(001:004, width=3, flag="0") 
>  ncfiles = lapply (days,
> function(d){  filename = paste("data",d,".nc",sep="")
>       open.ncdf(filename) })

also when i try the command like this for a spesific variable

> sapply(ncfiles,function(file,{get.var.ncdf(file,"var15")})

so my question is, any solution to read all netcdf file with special variable then make calculation in one frame. From the solution before i was failed for generating the variable no 15 on whole netcdf data.

thanks for any solution to this problem.

UPDATE:

this is the last what i have done

when i write

    library(ncdf)

files=list.files("allnc/",pattern='*nc',full.names=TRUE)

for(i in seq_along(files)) {
nc <- lapply(files[i],open.ncdf)
lw = get.var.ncdf(nc,"var15")
x=dim(lw)

rbind(df,data.frame(lw))->df }

i can get all netcdf data by > nc

so i how i can get variable data with new name automatically like lw1,lw2...etc i cant apply

var1 <- lapply(files, FUN = get.var.ncdf, variable = "var15")

then i can do calculation with all data.

the other technique i try used RNetCDF package n doing a looping

# Declare data frame
df=NULL

#Open all files
files= list.files("allnc/",pattern='*.nc',full.names=TRUE)

# Loop over files
for(i in seq_along(files)) {
nc = open.nc(files[i])

# Read the whole nc file and read the length of the varying dimension (here, the 3rd dimension, specifically time)
lw = var.get.nc(nc,'DBZH')
x=dim(lw)

# Vary the time dimension for each file as required
lw = var.get.nc(nc,'var15')

# Add the values from each file to a single data.frame
}

i can take a variable data but i just got one data from my all file nc. note: sampe of my data name ( data20150102001.nc,data20150102002.nc.....etc)

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
totti saga
  • 3
  • 1
  • 4
  • I really am not sure I have understood what you are asking. But I strongly suggest you to take a look at the raster package. Read all the files, stack them (raster can do that for you automagically!) and perform the computation you need to do with stackApply() or calc() or another of the provided functions. – AF7 Mar 01 '15 at 09:58
  • thanks, but probably my problem found when i try to looping my variable data like > var1 <- lapply(files, FUN = get.var.ncdf, variable = "var15") – totti saga Mar 02 '15 at 12:00
  • Do you try ncdf4 package? – Bangyou Mar 02 '15 at 12:27
  • oh i see, not even try. is it better? – totti saga Mar 02 '15 at 12:29

2 Answers2

2

This solution uses NCO, not R. You may use it to check your R solution:

ncra -v var15 data20150102*.nc out.nc

That is all. Full documentation in NCO User Guide.

Charlie Zender
  • 5,929
  • 14
  • 19
0

You can use the ensemble statistics capabilities of CDO, but note that on some systems the number of files is limited to 256:

cdo ensmean data20150102*.nc ensmean.nc

you can replace "mean" with the statistic of your choice, max, std, var, min etc...

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86