[R] Lat Lon NetCDF subset

hgreatrex h.greatrex at reading.ac.uk
Mon Mar 21 19:10:50 CET 2011


Hi,

I'm trying to read a subset of a netcdf file into R, but although I'm
relatively experienced using R, I'm still new to netCDF files, so this may
be a very simple/stupid question!  

I've included an example of the type of file I'm looking at here.  

www.met.reading.ac.uk/~swp06hg/ccd1983_01-dk1_20.nc   (~7Mb)

It's a 2D array of the variable CCD along with its lat and long coordinates.
This is its R summary
#[1] "file ccd1983_01-dk1_20.nc has 2 dimensions:"
#[1] "lat   Size: 1974"
#[1] "lon   Size: 1894"
#[1] "------------------------"
#[1] "file ccd1983_01-dk1_20.nc has 1 variables:"
#[1] "short data[lon,lat]  Longname:data Missval:NA"


I want to be able to do 2 things:
1) Extract a lat-long defined box
2) Extract a selection of pixels whose lat-long coordinates are defined in a
separate data.frame (basically I want the value of CCD at specified
rain-gauge locations)

So far my code is as follows

#----------------------------------------------------#
library(ncdf)
 CCD20 = open.ncdf("ccd1983_01-dk1_20.nc")
 lat = get.var.ncdf(CCD20, "lat") # coordinate variable
 lon = get.var.ncdf(CCD20, "lon") # coordinate variable
 z = get.var.ncdf(CCD20, "data") 
#----------------------------------------------------#

I'm then manually searching for the long/lat I want.  I know the ncdf
package will let me define a user defined box in terms of pixels e.g.

 z = get.var.ncdf(CCD20, "data", start=c(11,1), count=c( 5,-1) 
 
However, as netCDF files are often used to store gridded spatial data, if
anyone knows of a package or function which would let me do the equivalent
of

 z = get.var.ncdf(CCD20,"data",lat=c(30,40),lon=c(5,10))

or let me extract individual pixels directly from lat-long coordinates.

I've managed to extract the lat / lon variables, so I know that I could work
out manually which pixels correspond to which location. However, it's easy
to make a mistake in such a calculation, so I thought I would check here
before reinventing the wheel.  

Sorry if I'm asking something which isn't possible or doesn't make sense,
but I very much look forward to reading any replies!  I'm also not aware of
an RnetCDF mailing list, but sorry if this is the wrong list to ask this
question.

Best wishes
Helen Greatrex

PS Also, thank you to the writers of 
http://www.image.ucar.edu/GSP/Software/Netcdf/ for getting me started with
the package.



--
View this message in context: http://r.789695.n4.nabble.com/Lat-Lon-NetCDF-subset-tp3394326p3394326.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list