I'm actually doing more than just grabbing a single layer/band in R. I've got a script set up to extract specific layers from the time dimension, get the average, then write it out.  Ideally there would be a structure similar to this (is this possible?): 
 
b <- brick("Extraction_Tavg.nc", band = c(1, 13, 15))  #grab only time steps 1, 13, 15
 
This was why I resorted to using "get.var.ncdf" 
Perhaps there's a way I can initialize a rasterbrick or stack using your suggestions and then return to get.var.ncdf to get the layers I want. 
Tim

>>> Tim Howard 4/9/2012 1:29 PM >>>
Robert,
Thank you.  That does pull the extent and resolution information over, but perhaps the netCDF files I am using are structured funny as it throws an error for the levels:
 
> b <- brick("Extraction_Tavg.nc")
Warning message:
In .rasterObjectFromCDF(x, type = objecttype, band = band, ...) :
  "level" set to 1 (there are 3 levels)
> projection(b) <- "+proj=longlat +datum=WGS84"
> r3 <- b[[3]]
Error in .local(x, ...) : not a valid subset
> r3 <- b[[2]]
Error in .local(x, ...) : not a valid subset
> b
class       : RasterBrick 
dimensions  : 2, 2, 4, 1  (nrow, ncol, ncell, nlayers)
resolution  : 0.125, 0.125  (x, y)
extent      : -74.25, -74, 42, 42.25  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 
values      : <<mypath>>Extraction_Tavg.nc 
zvar        : Tavg 
level       : 1 
 
Tim

>>> "Robert J. Hijmans" <r.hijmans@gmail.com> 4/9/2012 1:19 PM >>>
Tim,

You can do:

library(raster)
b <- brick("Extraction_Tavg.nc")
projection(b) <- '+proj=longlat +datum=WGS84'

# multilayer object
b <- writeRaster(b, 'boo.tif') 

# for single layer (time 3)
r3 <- b[[3]]
r3 <- writeRaster(r3, 'boo3.tif') 


# or
r2 <- raster("Extraction_Tavg.nc", band=2)
projection(r2) <- '+proj=longlat +datum=WGS84'
r2 <- writeRaster(r2, 'boo3.tif') 

Or something similar with writeRaster following you approach. 
Oh, and I thought that ArcGIS can now also open ncdf files

Robert



On Mon, Apr 9, 2012 at 10:02 AM, Tim Howard <tghoward@gw.dec.state.ny.us> wrote:


I've got most of the steps for manipulating netCDF files using packages ncdf and raster but I'm currently stuck getting the correct projection information (lat/long) over to the raster and then exporting it.

Here's a VERY simply netCDF raster (2 cells lat X 2 cells long X 3 time periods) and how I might grab one of those time periods (I tried using dput to actually provide the data, but couldn't get dget to re-load it, I'd be happy to supply data if someone can suggest how).

> library(ncdf)
> library(raster)
> ncTemp2 <- open.ncdf("Extraction_Tavg.nc")
> ncTemp2
[1] "file Extraction_Tavg.nc has 4 dimensions:"
[1] "time Size: 3"
[1] "latitude Size: 2"
[1] "longitude Size: 2"
[1] "projection Size: 1"
[1] "------------------------"
[1] "file Extraction_Tavg.nc has 1 variables:"
[1] "float Tavg[longitude,latitude,time,projection] Longname:Tavg Missval:1.00000002004088e+20"
> str(ncTemp2)

<<snip to save space>>>>

My Question: How do I now export the single raster, m, so that I can bring along all the relevant spatial information contained in the netCDF (which would be the lat long coordinates?). I'd like to export the raster into any format accessible by esri products (boo!).

Thanks in advance,
Tim




[[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



	[[alternative HTML version deleted]]

