0

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

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
burton030
  • 405
  • 4
  • 8
  • 23

2 Answers2

2

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!

ialm
  • 8,510
  • 4
  • 36
  • 48
  • Hm, it says the name of my variable is "rr". I put it in the command like that data = get.var.ncdf(test,varid="rr"(test$var)[1])[1:464,1:201,1:365] But it doesnt work. Am I stupid? – burton030 Jul 11 '13 at 19:00
  • Sorry, my code must have been a bit confusing - you do not need the `(test$var)[1]` part! Try this: `get.var.ncdf(test,varid="rr")[1:464,1:201,1:365]` – ialm Jul 11 '13 at 19:07
  • Also, I recommend that you first try `get.var.ncdf(test,varid="rr")` without the `[1:464,1:201,1:365]` part to make sure that your dimensions ([long, lat, time]) are correct! – ialm Jul 11 '13 at 19:11
  • This command does not work:get.var.ncdf(test,varid="rr") This is exactly the point why I m trying all this stuff because when I try to get all data I m running out of memory. And also if I try this command get.var.ncdf(test,varid="rr")[1:464,1:201,1:365] I m running out of memory. Strange... Maybe I will ask again something... – burton030 Jul 11 '13 at 19:18
  • Ok, I tried it with another file and it worked pretty fine. Seems like my file is to big 947 MB. Do you have any other suggestions? – burton030 Jul 11 '13 at 19:26
  • Sorry, my mistake, I wasn't paying attention to the size of the array being read in. If you read the end of `help(get.var.ncdf)`, there is an example of reading in the data 1 time step at a time, which should prove to be useful for you. – ialm Jul 11 '13 at 19:28
0

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
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86