Question

I ve a netCDF file with 3 Dimensions. The first dimension is the longitude and reaches from 1-464. The second dimension is the latitude and reaches from 1-201. The third dimension is time and reaches from 1-5479.

Now I want to extract certain values out of the file. I think one can handle it with the start argument. I tried this command.

test = open.ncdf("rr_0.25deg_reg_1980-1994_v8.0.nc")

data = get.var.ncdf(test,start=c(1:464,1:201,1:365))

But somehow it doesnt work. Has anybody a solution?

Thanks in advance...

Was it helpful?

Solution

It looks like you are using the ncdf package in R. If you can, I recommend using the updated ncdf4 package, which is based on Unidata's netcdf version 4 library (link).

Back to your problem. I use the ncdf4 package, but I think the ncdf package works the same way. When you call the function get.var.ncdf, you also need to explicitly supply the name of the variable that you want to extract. I think you can get the names of the variables using names(test$var).

So you need to do something like this:

# Open the nc file
test = open.ncdf("rr_0.25deg_reg_1980-1994_v8.0.nc")

# Now get the names of the variables in the nc file
names(test$var)

# Get the data from the first variable listed above
# (May not fit in memory)
data = get.var.ncdf(test,varid=names(test$var)[1])

# If you only want a certain range of data. 
# The following will probably not fit in memory either
# data = get.var.ncdf(test,varid=names(test$var)[1])[1:464,1:201,1:365]

For your problem, you would need to replace varid=names(test$var)[1] above with varid='VARIABLE_NAME', where VARIABLE_NAME is the variable you want to extract.

Hope that helps.

EDIT:

I installed the ncdf package on my system, and the above code works for me!

OTHER TIPS

You could also do the extracting of timesteps/dates and locations outside of R before reading it into to R for plotting etc, by using CDO. This has the advantage that you can work directly in the coordinate space and specify timesteps or dates directly:

e.g.

cdo seldate,20100101,20121031 in.nc out.nc
cdo sellonlatbox,lon1,lon2,lat1,lat2 in.nc out.nc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top