[GRASS-user] pyhton script-how to get the pixel value from r.what

Adam Dershowitz, Ph.D., P.E. adershowitz at exponent.com
Wed Mar 28 17:47:53 EDT 2012



On Mar 28, 2012, at 2:38 PM, tigrida wrote:

> Hello!
> 
> I am writing a python script, and I want to do mathematical operations with
> some pixels, but I can't. Can anyone help me?
> 
> 
> script:
> ts_f= grass.read_command("r.what",input=Ts, east_north= "%s,%s" %
> (eastF,northF))
> print "Ts pixel frio=",ts_f
> lambda_f=(2.501-(0.00236*(ts_f-273.15)))*1000000
> 
> error:
> Ts pixel frio= 180740.539419|2399806.55602||290.266289112492
> 
> Traceback (most recent call last):
>  File "vslp2.py", line 284, in <module>
>    lambda_f=(2.501-(0.00236*(ts_f-273.15)))*1000000
> TypeError: unsupported operand type(s) for -: 'str' and 'float'
> 
> 
> I really appreciate any suggestions
> 
> 
> 



It looks like the grass.read_command returns a string, and you are then trying to do math on it.
You probably want something like:

import string
ts_f= grass.read_command("r.what",input=Ts, east_north= "%s,%s" %
(eastF,northF))
print "Ts pixel frio=",ts_f
ts_values=string.split(ts_f,'|')

I am not sure which value you then want.  Probably ts_values[3]:

lambda_f=(2.501-(0.00236*(ts_values[3]-273.15)))*1000000


More information about the grass-user mailing list