Date: Wed, 28 Jan 2009 09:58:23 +0800
Subject: Re: [R-sig-Geo] reading *.grd files - revisited
From: r.hijmans at gmail.com
To: pisicandru at hotmail.com
CC: r-sig-geo at stat.math.ethz.ch
Dear Monica,
You probably refer to this thread:
https://stat.ethz.ch/pipermail/r-sig-geo/2008-October/004326.html
which is about Surfer (.grd) files. Unfortunately .grd is used for
many different formats, and your file is not a Surfer .grd file.
In fact it is a netCDF file. ( .cdf or .nc are more common extensions
for that format !)
I was able to read your file and plot the values like this (I use the
raster package, but you can adapt the last bit if you do not want
that):
# you probably need to install these two packages:
install.packages("ncdf")
install.packages("raster", repos="http://R-Forge.R-project.org")
require(ncdf)
require(raster)
f <- "d:/test_grid.grd"
nc <- open.ncdf(f)
xr <- get.var.ncdf(nc, nc$var[[1]])
yr <- get.var.ncdf(nc, nc$var[[2]])
res <- get.var.ncdf(nc, nc$var[[4]])
dims <- get.var.ncdf(nc, nc$var[[5]])
v <- as.vector(get.var.ncdf(nc, nc$var[[6]]))
close.ncdf(nc)
xm <- xr[1] - 0.5 * res[1]
xx <- xr[2] + 0.5 * res[1]
ym <- yr[1] - 0.5 * res[2]
yx <- yr[2] + 0.5 * res[2]
r <- newRaster(xmn=xm, xmx=xx, ymn=ym, ymx=yx, ncols=dims[1], nrows=dims[2])
r <- setValues(r, v)
plot(r)
# if you want an sp grid object
sp <- asSpGrid(r)
Robert
On Wed, Jan 28, 2009 at 12:21 AM, Monica Pisica wrote:
Hi,
I've read already the thread about how to read *.grd files in R. What i want to do is to re-save the file in a geotiff format so i can load it in ArcGIS. I tried readRGDAL and i got the following error:
x <- readRGDAL("test_grid.grd")
Error in .local(.Object, ....):
GDAL Error 4: "test_grid.grd' not recognized as a suported file format.
Next step was to do what Alexander Brenning suggested using RSAGA but after installing the library and trying the following command i got this error:
rsaga.get.usage("io_grid",3)
Error in setwd (env$workspace) : character argument expected
In addition: warning message:
In rsaga.env()
SAGA command line program 'saga.cmd.exe' not found in any of the paths
c:/PROGRA~2/R/R~28~1.1PA/library/RSAGA/saga_vc
.....
Following afterwards a list of directories in my computer. Do i need to install any other additional package and load it in order to use RSAGA???
I receive almost same error for the command
rsaga.geoprocessor("io_grid",3, param = list(GRID = "temp.sgrd", FILE = "test_grid.grd"))
I have installed R 2.8.1 patched and all packages are updated, on a Windows XP professional x64 machine 64 bit.
If anybody wants to give it a try you will find the file (probably around noon - it takes about 15 - 30 min to upload on the site) at ftp://stpfiles.er.usgs.gov/Monica/GRID/test_grid.grd
Thanks for all the help,
Monica