[R-sig-Geo] reading .grd file in R?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Oct 12 20:36:05 CEST 2010


Anybody still reading this?

I'm sometimes so stuck with sp-objects and rgdal that I forget that
the easiest way to read raw binary data is with readBin. Wrap it into
a function, make it a matrix, now I can easily read time slices:

readTime <-
  function(f,tm,x=69,y=65,size=4,type="double",NAvalue=-999){
    c = file(f,open="rb")
    seek(c,x*y*(tm-1)*size)
    m=matrix(readBin(c,type,x*y,size),x,y)
    close(c)
    m[abs(m-NAvalue)<.Machine$double.eps] = NA
    m
  }

 par(mfrow=c(2,2))
 for(p in 1:4){
 image(readTime("rf0.5_1975.grd",p))
 }

 I didn't realise it was India because my gdal virtual raster was sideways.

Barry



More information about the R-sig-Geo mailing list