Skip to content

WriteRaster as Float

5 messages · diamant zebrat, Loïc Dutrieux, Philippi, Tom

#
Hello,
I am working with R processing Meteosat imagery, which mean a lot of images
every day, and thus a lot of available storage space
It looks like the write raster function of the raster package only allows
to write a Geotiff as integer or as double. I only need float precision, so
I don't need a double as the resulting file gets too big. Is there any way
to store a geotiff with float precision?
Thank you
Zebrat
#
Zebrat--
Without example code it is difficult to guess what you tried, and what you
desire.  By "float" do you mean FLT4S 4-byte values?  If so, try
datatype='FLT4S' in your call to writeRaster(), which should be the same as
'Float32' in gdal.  If you mean something else, I guessed wrong, so you
might specify a bit more about what you have, want, and have tried.

Tom 2

On Mon, Nov 10, 2014 at 4:38 AM, diamant zebrat <diamant.zebrat at gmail.com>
wrote:

  
  
#
Tom, here goes the details
The Meteosat images are composed by a matrix of pixels with values of
surface temperature (or cloud top temperature) in Kelvin, which means
values like 273.15
I wanted to keep the decimals, thus I need a FLT4S 4-byte values. When using
writeRaster(TTemp,filename=nom,format="GTiff",overwrite=T,datatype="FLT4S")
I get a file with a size of 7.495.158 bytes.  My particular image is a
matrix of 1201 lines x 803 columns = 964.403 pixels,
with this option (datatype="FLT4S") it looks like we get an image that uses
8 bytes to store each pixel. In fact, we only need 4 bytes (float) to store
values like 273.15, thus I am getting images which are too big. Keep in
mind there is a Meteosat image each 15 min -> 96 a day, etc. These means a
lot of storage space and I should care about it
When opening one of these GEOTIFF (LTS4S) with a GIS package, it get an
image that is "double precision", confirming that the writeRaster is using
8 bytes to store each pixel when creating the GEOTIFF with the
datatype="FLT4S" option
The conclusion is that the writeRaster routine is not optimized to use the
minimum space when writing a geotiff
The only alternative by now is to multiply my kelvins by ten and save it as
an integer, this way I only keep one decimal but I get small size files
writeRaster(TTemp,filename=nom,format="GTiff",overwrite=T,datatype="INT1U")
However, I still want to keep the real values using 4 byte for each pixel...
thank you
Zebrat

2014-11-11 4:32 GMT+00:00 Tom Philippi <tephilippi at gmail.com>:

  
  
#
Hello Zebrat,

The datatype= argument appears to work well on my system. See if you can reproduce the example below:

library(raster)
r <- raster(system.file("external/test.grd", package="raster"))

writeRaster(r, filename = 'rasterDefault.tif')
GDALinfo('rasterDefault.tif')
# By default raster writes to Float64

writeRaster(r, filename = 'rasterFloatSingle.tif', datatype = 'FLT4S')
GDALinfo('rasterFloatSingle.tif')
# Float 32

Make sure you do not capitalize the "t" in datatype. datatype is the writeRaster() argument and dataType is the function to retrieve data type.

Cheers,
Lo?c

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of diamant zebrat
Sent: Tuesday, November 11, 2014 10:13
To: Tom Philippi
Cc: r-sig-geo
Subject: Re: [R-sig-Geo] WriteRaster as Float

Tom, here goes the details
The Meteosat images are composed by a matrix of pixels with values of surface temperature (or cloud top temperature) in Kelvin, which means values like 273.15 I wanted to keep the decimals, thus I need a FLT4S 4-byte values. When using
writeRaster(TTemp,filename=nom,format="GTiff",overwrite=T,datatype="FLT4S")
I get a file with a size of 7.495.158 bytes.  My particular image is a matrix of 1201 lines x 803 columns = 964.403 pixels, with this option (datatype="FLT4S") it looks like we get an image that uses
8 bytes to store each pixel. In fact, we only need 4 bytes (float) to store values like 273.15, thus I am getting images which are too big. Keep in mind there is a Meteosat image each 15 min -> 96 a day, etc. These means a lot of storage space and I should care about it When opening one of these GEOTIFF (LTS4S) with a GIS package, it get an image that is "double precision", confirming that the writeRaster is using
8 bytes to store each pixel when creating the GEOTIFF with the datatype="FLT4S" option The conclusion is that the writeRaster routine is not optimized to use the minimum space when writing a geotiff The only alternative by now is to multiply my kelvins by ten and save it as an integer, this way I only keep one decimal but I get small size files
writeRaster(TTemp,filename=nom,format="GTiff",overwrite=T,datatype="INT1U")
However, I still want to keep the real values using 4 byte for each pixel...
thank you
Zebrat

2014-11-11 4:32 GMT+00:00 Tom Philippi <tephilippi at gmail.com>:
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
#
Zebrat--
Loic's example works and generates different sized files on my system.
sessionInfo():
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] raster_2.2-31 rgdal_0.8-16  sp_1.0-15

loaded via a namespace (and not attached):
[1] grid_3.1.1      lattice_0.20-29

If it doesn't work on your system, you might post your sessionInfo().

Try both FLT4S and FLT8S to see if the files have different sizes.  Then,
use GDALinfo() instead of another GIS program to inspect the file, as at
least one GIS program silently converts 4-byte floats to 8-byte doubles.

Other than that, if space is really an issue, your approach of rescaling
and storing as an integer is good. You could divide by 100 and use INT2S if
your temperatures don't get above 327deg K.

Tom 2



On Tue, Nov 11, 2014 at 5:02 AM, Dutrieux, Loic <loic.dutrieux at wur.nl>
wrote: