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

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

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.