What I intend to do is rather simple. I downloaded binary files (such as this one: ftp://ftp.star.nesdis.noaa.gov/pub/smcd/jhuang/npp.viirs.aerosol.data/edraot550/monthly/2015/) and want to convert them into a regular raster format (such as tif) using R. The files contain gridded global data on aerosol optical thickness.
I tried to read the files using x <- readBin(filename, "double", endian = "little")
. However, the resulting object only contains a single number. Plugging in another what
option instead of double
solely changes the given number but does not solve the problem.
There is no header file provided by the data source. The documentation (ftp://ftp.star.nesdis.noaa.gov/pub/smcd/jhuang/npp.viirs.aerosol.data/edraot550/1README.txt) provides comments on Fortran and and IDL in which I am entirely unexperienced. Therefore, it would be great to do this rather simple task in R. Intuitively, the solution should be a matter of one or two commands.
Thank you for any comments and suggestions.
For this file:
g <- 'npp_aot550_edr_gridded_0.25_201503_201505_am.bin.gz'
First unzip
library(R.utils)
gunzip(g)
Now you can use list.files
to get bin files
f <- list.files(pattern='\\.bin$')[1]
But there I'll just hard-code it:
f <- 'npp_aot550_edr_gridded_0.25_201503_201505_am.bin'
Make a template raster that matches the described resolutoon for extent and resolution.
library(raster)
r <- raster(res=.25)
Save to file. The datatype sets the file size, that is how I could see that it had to be 'FLT8S'.
writeRaster(r, 'test.grd', datatype='FLT8S', overwrite=T)
Copy the header
file.copy('test.grd', extension(f, '.grd'))
Rename the file with the values
file.rename(f, extension(f, '.gri'))
Create a RasterLayer object and set the proper NA flag value
r <- raster(extension(f, '.gri'))
NAvalue(r) <- -999.9
The values are stored from bottom to top, so they need to be 'flipped'. I save them to a tif file in the same step
x <- flip(r, 'y', filename=extension(f, 'tif'))