-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_raster_precision.sh
executable file
·51 lines (46 loc) · 1.81 KB
/
translate_raster_precision.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
#
# Raster space saver, because I never set precision when working with raster::
# writeRaster in 'R', and it default saves everything I make with Float64
# precision, resulting in huge rasters. This will bulk process all rasters in
# the CWD. P.s., never run this on rasters where you need significant figures.
# It will ALWAYS drop the precision of your rasters, sometimes dramatically.
#
if [ $# -eq 0 ]; then
PROCESS_DIR=`ls -1 $PWD | grep -E ".tif$|.img|.bil"`
fi
if [ -d $1 ]; then
PROCESS_DIR=`ls -1 $1 | grep -E ".tif$|.img|.bil"`
else # if this isn't a directory, assume the user passed a path with wildcards with ls comprehension
PROCESS_FLAGS=`ls -1 $1`
fi
for f in `ls -1 *_30m.tif`; do
MAX=`gdalinfo -stats $f | grep _MAX | awk '{ gsub(" STATISTICS_MAXIMUM=",""); print }'`
MIN=`gdalinfo -stats $f | grep _MIN | awk '{ gsub(" STATISTICS_MINIMUM=",""); print }'`
rm -rf output.tif
if [[ $MIN > 0 && $MAX < 256 ]]; then
echo "-- using byte precision"
gdal_translate -ot Byte -a_nodata 0 $f output.tif;
mv output.tif $f
elif [[ $MIN < 0 && $MAX < 32768 ]]; then
echo "-- using traditional Int16 precision"
gdal_translate -ot Int16 $f output.tif;
mv output.tif $f
elif [[ $MIN > 0 && $MAX < 65536 ]]; then
echo "-- using UInt16 precision"
gdal_translate -ot UInt16 -a_nodata 0 $f output.tif;
mv output.tif $f
elif [[ $MIN > 0 && $MAX < 4294967296 ]]; then
echo "-- using Uint32 precision"
gdal_translate -ot UInt32 -a_nodata 0 $f output.tif;
mv output.tif $f
elif [[ $MIN < 0 && $MAX < 2147483648 ]]; then
echo "-- using traditional Int32 precision"
gdal_translate -ot Int32 $f output.tif;
mv output.tif $f
else
echo "-- assuming precision at Float32"
gdal_translate -ot Float32 $f output.tif;
mv output.tif $f
fi
done