Search code examples
rbinaryraster

Read binary raster files in R


I want to read binary integers in R and convert them into raster grids. The files have the following charterers:

NCols= 4320
NRows= 2160
pixel-size: 1/12=0.833 degrees
upper-left-lat: 90.0-1/24
upper-left-lon: -180.0+1/24
lower-right-lat: -90.0+1/24
lower-right-lon: 180.0
nodata= -5000
scale-factor= 10000
datatype: 16-bit signed integer
byte-order: big endian

Here is what I do:

file <-"http://nasanex.s3.amazonaws.com/AVHRR/GIMMS/3G/1980s/geo81aug15a.n07-VI3g"
dat <- readBin(file,what="integer", size=4, signed = TRUE, n = NRows * NCols, endian = "big")
r <- raster(nrow=2160, ncol=4320)
r[] <- dat

But this doesn't seem to be right, I appreciate any suggestions. .


Solution

  • You can read such files with the greenbrown R package.

    Install it in R with

    install.packages("greenbrown", repos="http://R-Forge.R-project.org")
    

    If that fails because the package needs to be rebuilt by its authors, an alternative is to first download the sources directly from the repo, and then install them manually, as explained in the greenbrown installation instructions. In the latter case you may also have to manually install a couple of packages that greenbrown depends on first: install.packages on Kendall, bfast, strucchange.

    After installation, reading the raster from a URL is as easy as:

    library(greenbrown)
    r <- ReadVI3g("http://nasanex.s3.amazonaws.com/AVHRR/GIMMS/3G/1980s/geo81aug15a.n07-VI3g")
    

    The object returned by greenbrown::ReadVI3g is a RasterLayer. We can display it with

    plot(r)
    

    which gives

    enter image description here