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

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Oct 12 00:35:33 CEST 2010


On Sat, Oct 9, 2010 at 8:49 PM, Rainer Hurling <rhurlin at gwdg.de> wrote:

> http://cookbooks.opengrads.org/index.php?title=Recipe-002:_Saving_GrADS_variable_data_to_a_text_file

Grads .grd files are raw binary files, and so can be read using the
GDAL .vrt mechanism once you know the basics of the binary format and
the metadata in the .ctl file.

Looking at the .ctl file I guess the grids are 69x65 pixels square,
and that there are 365 of them (daily data for a year). From the size
of the file I see this means 4 bytes per pixel (69x65*365*4 = size of
file exactly). There's no header and no padding. Its four bytes per
value.

 However we don't know the format of the value. It could be floating
point, 32 bit integers etc etc.

 Here's a .vrt file that reads in the second day's data (which is what
the offset of 17940=69*65*4 skips to):

<VRTDataset rasterXSize="69" rasterYSize="65">
  <VRTRasterBand dataType="UInt32" band="1" subClass="VRTRawRasterBand">
    <SourceFilename relativeToVRT="1">rf0.5_1975.grd</SourceFilename>
    <ImageOffset>17940</ImageOffset>
    <PixelOffset>4</PixelOffset>
    <LineOffset>276</LineOffset>
    <ByteOrder>LSB</ByteOrder>
  </VRTRasterBand>
</VRTDataset>

If you read that in with v2=readGDAL("file.vrt") (put it in the same
folder as the .grd file) then you will get something reasonable if you
do image(v2), however I'm not sure we got the dataType right, since
the values are all over the place. Further day's data can be read by
changing the image offset in multiples of 17940.

 I'd fix it more but it's bedtime.

Barry



More information about the R-sig-Geo mailing list