Using IF statements with maths where the input is not an integer

Hi All

I've made a few scripts which using GDAL extract the value of a pixel within a given raster. The purpose is to work out the combine value of every pixel. I thought there may have been an easier way to do this but alas!

The code below extracts the pixel value at position X Y. The value is a floating point rather than an integer and thats where my problem lies. The if statement simply exports the value to a txt file. Other scripts take care of the rest ie change values of X Y and summing the resultant txt files.

#!/bin/sh
input=`gdallocationinfo -valonly 210_1515_volume.env X Y`  # The value of the pixel at coordinate X Y
if [ $input -gt 0 ] ;then
	echo $input > colrow.txt
fi

However, I get the following message back when ever my statement is true because my value isn't an integer;

extract2.sh: line 4: [: 2.15787172317505: integer expression expected

Please note the raster file mostly contains zero values, and its only the values greater than zero I'm interested in, which is why I added the expression.

Any ideas how I can alter this code so the IF statement excepts integers? Alternatively if anyone knows of a faster method of quickly summing the values of a raster file, that would be equally useful.

Many thanks in advance for any assistance you can offer.

Andy

Try string comparison:

if [ "$input" != "0" ]; then
...
1 Like

Yes this works, Thank you Yazu!

Whilst re-examining what I'm trying to do I've realised that I may be over complicating things, I can turn my original raster file into a ascii txt file with one command. So now all I need to do is work out how to sum all the values within a txt file.... I am sure there must be a post on that around here somewhere!

More practice at shell scripting anyway!

---------- Post updated at 02:39 PM ---------- Previous update was at 01:50 PM ----------

Ok maybe this is harder than I thought it might be. It seems its not easy to sum up floating point values in a txt file as many programs are designed for use with integers.

A sample of my txt file;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.7223560810089111328 2.4221210479736328125 0 0 2.0098361968994140625 2.9117717742919921875 2.4130835533142089844 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

So I'm trying to find the total of all these values. Ive tried a few things picked up from other forum posts;

Code:

awk '{s+=$1} END {print s}' 210.txt

This produces a value of zero, so I'm guessing its not recognising floating point values.

Code:

i=0
for n in `cat 210.txt`
do
    i=`expr $i + $n`
done
echo $i

But I get a syntax error.

Still searching for other solutions... any help would be appreciated!

---------- Post updated at 04:22 PM ---------- Previous update was at 02:39 PM ----------

Ok I think I've found a solution, in case anyone else has a similar problem in the future.

Create the following script 'sumall.awk'.

#sumall.awk
{   for (i=1; i<=NF; i++) { sum+= $i }   }

END { for (i=1; i<=NF; i++ ) 
	{ print sum } 
          }

then 

awk -f sumall.awk < 210.txt > output.txt

to output the sum of each col into an output.txt file.

then

awk '{ print "a = a + " $1 "; a" }' output.txt | bc -l | tail -1 > 210_final.txt

to print the sum of output.txt into a new file '210_final.txt'

This method is much much faster than my original plan. AWK ftw!

Andy

You really over complicated things. :slight_smile: Awk can work with floating numbers (even "old" awk, afaik).

You just need this:

awk  '{ for (i=1; i<=NF; i++)  s+= $i  } END { print s }'

In your first attempt you just add zero with the first field and this field was zero.