1

I have a conditional in Python script from GRASS but I can not get it to work. No matter if I set m to 1 or 0 it always prints "m must be 1 or 0." like it won't enter the conditional.

Here's the script:

m=options['m']
if m==1:
 grass.mapcalc("up=$c+tan($phy)*(($gamma*$z*(cos(slope)^2))-($k*$gamma*$z*sin(slope)*cos(slope))-($z*$gammaw))",
 z = options['z'],
 c = options['c'],
 gamma = options['gamma'],
 phy = options['phy'],
 k = options['k'],
 gammaw = 9.81)
 print ('m is 1')
elif m==0:
 grass.mapcalc("up=$c+tan($phy)*(($gamma*$z*(cos(slope)^2))-($k*$gamma*$z*sin(slope)*cos(slope)))",
 z = options['z'],
 c = options['c'],
 gamma = options['gamma'],
 phy = options['phy'],
 k = options['k'])
 print ('m is 0')
else:
 print("m must be 1 or 0.")
xunilk
30.5k4 gold badges44 silver badges83 bronze badges
asked Feb 19, 2018 at 16:16

1 Answer 1

2

You probably need to convert it to number:

m = int(options['m'])

But it looks like you want to define it as a flag instead of option, since it can be only True or False, so then you would use it as:

if flags['m']:
 # do something
else:
 # do something else

See also the manual: https://grass.osgeo.org/grass74/manuals/g.parser.html

answered Feb 20, 2018 at 1:22
1
  • Thank you Anna, i did thought it was something simple but I was really stuck. Thanks for your help! Commented Feb 20, 2018 at 14:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.