Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a .tif file in R [closed]

Tags:

r

I am reading a .tif file in R and getting 4 warnings messages listed below. When I follow the instruction on the 4th message, the first 3 warnings still remain but the values read from file changes drastically at every pixel. Please help me in reading the data correctly from the .tif files. Sample File can be found on the link: ftp://ftp.ntsg.umt.edu/pub/MODIS/NTSG_Products/MOD16/MOD16A2_MONTHLY.MERRA_GMAO_1kmALB/GEOTIFF_0.05degree/

my code:

remove(list=ls()) 

library(tiff)

library(raster)

str_name<-'MOD16A2_ET_0.05deg_GEO_2008M01.tif' 

read_file<-readTIFF(str_name) 

Warning messages:

1: In readTIFF(str_name) :
  TIFFReadDirectory: Unknown field with tag 33550 (0x830e) encountered
2: In readTIFF(str_name) :
  TIFFReadDirectory: Unknown field with tag 33922 (0x8482) encountered
3: In readTIFF(str_name) :
  TIFFReadDirectory: Unknown field with tag 34735 (0x87af) encountered
4: In readTIFF(str_name) :
  tiff package currently only supports unsigned integer or float sample formats in direct mode, but the image contains signed integer format - it will be treated as unsigned (use native=TRUE or convert=TRUE to avoid this issue)

Please help me with this issue of reading tif files correctly. Thanks in advance.

like image 342
Munish Avatar asked May 29 '13 00:05

Munish


People also ask

How do I view a .tif file?

For Windows:Highlight the file you want to open. Double-click. The TIFF file should open in Windows Photo Viewer.

How do I open a Geotiff file in R?

Geotiffs in RThe raster package in R allows us to both open geotiff files and also directly access . tif tags programmatically. You can quickly view the spatial extent, coordinate reference system and resolution of your raster data. NOTE: not all geotiff s contain tif tags!


2 Answers

Did you try simply the raster package raster function (or stack if multiple layered tif)? The raster package was made to deal with geo-referenced raster datasets:

library(raster)
str_name<-'MOD16A2_ET_0.05deg_GEO_2008M01.tif' 
imported_raster=raster(str_name)

The simple code above works and yields a raster object with the following properties:

class       : RasterLayer 
dimensions  : 2800, 7200, 20160000  (nrow, ncol, ncell)
resolution  : 0.05, 0.05  (x, y)
extent      : -180, 180, -60, 80  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\lfortini\Downloads\MOD16A2_ET_0.05deg_GEO_2000M01.tif 
names       : MOD16A2_ET_0.05deg_GEO_2000M01 
values      : -32768, 32767  (min, max)
like image 186
Lucas Fortini Avatar answered Oct 07 '22 13:10

Lucas Fortini


Simply read the pixels as unsigned and convert them to signed:

 t = readTIFF("MOD16A2_ET_0.05deg_GEO_2008M01.tif", as.is=TRUE)
 t[t >= 32738L] = -65536L + t[t >= 32738L]

Looking at the image, you may also want to convert -32768 to NA as that seems to be the use in the file:

 t[t == -32768L] = NA

If you want to convert the integers to [-1,1] reals now, just do

 t = t / 32768

The first three warnings are just telling you that there are additional custom tags in the file.

like image 23
Simon Urbanek Avatar answered Oct 07 '22 13:10

Simon Urbanek