[R] Clip smaller domain from large domain netCDF file

Roy Mendelssohn roy.mendelssohn at noaa.gov
Thu Aug 28 06:40:53 CEST 2014


Hi Aseem:

I will try to keep this as short as possible, because a lot of the issue here is more with understanding netcdf file structure than with R, because once you understand the former using ncdf4 is pretty straightforward..  So if you want a more detailed response we should probably take it off line.

Netcdf gridded field are comprised of dimensions or coordinate variables  (which are booth index values and variables that hold the values of the dimensions), and of parameters defined on those dimensions.

From what you send me I see:

>  $ ndims     : num 3

which means your file has three dimensions, "lon", "lat" and "time", dimension as lon(1068), lat(510) and time(22280).

The file says you have 6 variables:

>  nvars: int 6

but 3 of those are the variables holding the lon,lat and time values, the others  (with their dimensions):
pr(lon,lat,time)
tasmax(lon,lat,time)
lasmin(lon,lat,time)

When you called nc_open, you actually already have the coordinate values in the structure, for example  (and I hope I don't skip a leveling the structure), 
the longitude are at ncold$dim$lon$vals, and similar for the others.  So say we have set lon,lat and time as these values, the steps to subset the data would be:

1.  Find the indexes of the variable lon that fall in the subset you want  (lets say for samples 10:19
2. Find the indexes of the variable lat that fall int subset you want  (let's say 20:29)
3. FInd the indexes of the times that fall int subset you want  (let's say 1:5)

than to subset the variable "pr" you would make a call like:

pr<-ncvar_get(ncold,'pr',start=c(10,20,1), count=c(10,10,5))

Note the values of "start" are the same as the first values of the indices given above for the three dimensions, and the count values are the number of elements to read in each dimensions  (i.e. 19-10+1, 29-20+1, 5-1+1).  pr will then contain the subset of the data.

Be certain to close the file:

nc_close(ncold)

Hope this gives you some idea of how to proceed.

-Roy

On Aug 27, 2014, at 3:29 PM, Aseem Sharma <nature.aseem at gmail.com> wrote:


**********************
"The contents of this message do not reflect any position of the U.S. Government or NOAA."
**********************
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***
110 Shaffer Road
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.



More information about the R-help mailing list