[R] netCDF to TIFF

Daniel Steinberg d.c.steinberg at gmail.com
Wed Jul 9 14:34:16 CEST 2008

Greetings R users!

I am working with the ENSEMBLE climate data (10 min resolution daily
temperatures images for all of Europe 1950-2006). The data comes
packaged in a single netCDF file. I would like to read the data in and
export a subset (2002-2006) as geotiffs (one image per day). So far, I
can successfully read in the data and view the images within an R
display window. However, I have yet to figure out how to export the
images as tiffs or geotiffs. Does anyone out there have experience
converting netCDF grids to TIFFs?

After reading the data into R, I have it stored in a dataframe with
longitude values as the column names, latitude values as the row
names, and temperatures as the actual values. Below is my code thus
far that I adapted from an example on the UCAR website.

Thanks in advance!


# Set working directory and load library


# Read netCDF file

tg.ncdf = open.ncdf('tg_0.25deg_CRU_version1.0.nc')
lonmat  = get.var.ncdf(nc=tg.ncdf,varid="longitude")   # reads entire matrix
latmat  = get.var.ncdf(nc=tg.ncdf,varid="latitude")    # ditto
timearr = get.var.ncdf(nc=tg.ncdf,varid="time")        # reads entire time array

targettime = julian(x=1, d=1, y=2002,  origin=c(month = 1, day = 1,
year = 1950))
inds       = (1:dim(timearr))
tind       = inds[targettime == timearr]

ndims    = tg.ncdf$var[['data']]$ndims
varsize  = tg.ncdf$var[['data']]$varsize
start = c(    1,          1,      tind)
count = c(varsize[1], varsize[2],    1)

# Read in data slice:
tg.data = get.var.ncdf(nc=tg.ncdf,varid="data",start,count)
tg.data[tg.data == -9999] = NA
tg.data = tg.data/100.0
x = 1:nrow(tg.data)   # R plots rows along X axis!
y = 1:ncol(tg.data)

test.data = data.frame(tg.data, row.names = lonmat)
colnames(test.data) = latmat

More information about the R-help mailing list