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

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

--
View this message in context: http://osgeo-org.1560.n6.nabble.com/pyhton-script-how-to-get-the-pixel-value-from-r-what-tp4665870p4665870.html
Sent from the Grass - Users mailing list archive at Nabble.com.

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

Adam Dershowitz, Ph.D., P.E. wrote:

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))

Note that you can pass lists and tuples directly, e.g.:

ts_f= grass.read_command("r.what", input=Ts, east_north=(eastF,northF))

read_command etc will perform the necessary conversion.

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

That still doesn't solve the issue of trying to perform arithmetic on
strings; use int() or float() to convert strings to numbers, e.g.

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

--
Glynn Clements <glynn@gclements.plus.com>

thank you very much! I already do

Adriana
IPICyT, México

--
View this message in context: http://osgeo-org.1560.n6.nabble.com/pyhton-script-how-to-get-the-pixel-value-from-r-what-tp4665870p4672574.html
Sent from the Grass - Users mailing list archive at Nabble.com.