1

I´m trying to convert a binary image of TRMM satellite to Geotiff format using this R code:

setwd("C:/Users/Hugo/Desktop")
trmm.data.file<-"C:/Users/Hugo/Desktop/3B42_daily.2013.04.22.7.bin"
trmm.data<-readBin("3B42_daily.2013.04.22.7.bin", "numeric", size=4, signed = TRUE, n = 1440 * 400, endian = "little")
trmm.array <- array(trmm.data, dim = c(400, 1440))
trmm.raster <- raster(trmm.array, xmn = -180, xmx = 180, ymn = -50, ymx= 50, crs = '+proj=longlat +datum=WGS84 +ellps=WGS84')
trmm.crop<-crop(trmm.raster, e)  # 'e' is the box of my region of interest
plot(trmm.crop)
setwd("C:/Users/Hugo/Desktop/")
writeRaster(trmm.crop,filename="trmmcrop.tiff",format="GTiff",overwrite=TRUE)

But it seems not to work, the pixel values are incoherent and the resolution is wrong.

Any idea about how to do this?

2

I once wrote some functions to automate data download and processing of TRMM 3B42 binary data. downloadTRMM and rasterizeTRMM are included in our working group's Rsenal package. You can either install it via devtools

devtools::install_github("environmentalinformatics-marburg/Rsenal")
library(Rsenal)

or, if installation fails (after all, it's a package in the making), download and install the latest version manually from here. After that, the rasterization is quite straight-forward.

## download TRMM 3B42 data from Jan 1 to Jan 4, 2015
fls_trmm <- downloadTRMM(begin = "2015-01-01", end = "2015-01-04")
fls_trmm

# bin                               xml
# 1 ./3B42_daily.2015.01.02.7.bin ./3B42_daily.2015.01.02.7.bin.xml
# 2 ./3B42_daily.2015.01.03.7.bin ./3B42_daily.2015.01.03.7.bin.xml
# 3 ./3B42_daily.2015.01.04.7.bin ./3B42_daily.2015.01.04.7.bin.xml
# 4 ./3B42_daily.2015.01.05.7.bin ./3B42_daily.2015.01.05.7.bin.xml

## rasterize downloaded data
lst_trmm <- lapply(1:nrow(fls_trmm), function(i) {
  rasterizeTRMM(binary = fls_trmm[i, 1], meta = fls_trmm[i, 2], 
                write_out = FALSE)
})
rst_trmm <- raster::stack(lst_trmm)

trmm_global

Feel free to browse the source code and tell me if anything goes wrong.

  • 1
    The rasterizeTRMM function is very convenient. There were some quirks in the download code so, I rewrote it and removed the "lubridate" dependency, just using the POSIXlt class. It does look like they have a 10 file download limit before the ftp locks you out. Any idea on the authentication credentials for the ftp? – Jeffrey Evans Nov 14 '15 at 1:18
  • Hm, in a different context (MODIS evapotranspiration data), I found such lock-downs were caused by RCurl::getURL and could be circumvented by including something like Sys.sleep(300). I'll have a look at it! – fdetsch Nov 14 '15 at 8:54
  • Thank you very much! I could not install the package. I´m trying to use the functions isolated. But appears this error when I try to download the TRMM data: "Error in download.file(j, tmp_ch_fls_out) : cannot open destfile './3B42_daily.2014.01.02.7.bin.xml ', reason 'Invalid argument'" – Bindini Nov 14 '15 at 21:12
  • 1
    @JeffreyEvans I decided to manually create on-line filepaths in order to avoid RCurl::getURL and it seems to work with more than 10 files now. I also removed the lubridate dependency. @HugoBendini Could you please test the new function version which is now on GitHub and tell me if everything works as expected? – fdetsch Nov 15 '15 at 9:23
  • By the way, what is the difference between the "rasterizeTRMM" and Daniel´s script posted below? – Bindini Nov 15 '15 at 12:31
1

I was not aware of the Rsenal package. It looks very nice and easy. Will have to take a look. What I did here was Robert Hijmans code from r-sig-geo 1

require(raster)
# trmm template
trmm <- raster(xmn=-180, xmx=+180, ymn=-50, ymx=50, ncol=1440, nrow=400)
filename <- "3B43.980101.7.precipitation.accum"
trmm[] <- readBin(filename, 'double', n=576000, size=4, endian='big')
# normalize (North up, -180.. 180)
x <- flip(trmm, 'y')
# write to geotif
x <- writeRaster(x, extension(filename, 'tif'))
plot(x)

You could put that into a loop and process all in batch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.