Skip to content

Commit

Permalink
addon r.mess: change to using format for python string formatting an …
Browse files Browse the repository at this point in the history
…update email address
  • Loading branch information
ecodiv committed Jun 8, 2019
1 parent b2d5632 commit ddd8bcc
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 11 deletions.
2 changes: 1 addition & 1 deletion grass7/raster/r.mess/r.mess.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ <h2>REFERENCES</h2>

<h2>AUTHOR</h2>

Paulo van Breugel, paulo at ecodiv.org
Paulo van Breugel, paulo at ecodiv.earth


<p><i>Last changed: $Date$</i>
20 changes: 10 additions & 10 deletions grass7/raster/r.mess/r.mess.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
########################################################################
#
# MODULE: r.mess
# AUTHOR(S): Paulo van Breugel <p.vanbreugel AT gmail.com>
# AUTHOR(S): Paulo van Breugel <paulo ecodiv earth>
# PURPOSE: Calculate the multivariate environmental similarity
# surface (MESS) as proposed by Elith et al., 2010,
# Methods in Ecology & Evolution, 1(330–342).
Expand Down Expand Up @@ -201,8 +201,8 @@ def main(options, flags):
reftype['max'] != 1):
gs.fatal(_("The ref_rast map must be a binary raster,"
" i.e. it should contain only values 0 and 1 or 1 only"
" (now the minimum is %d and maximum is %d)")
% (reftype['min'], reftype['max']))
" (now the minimum is {} and maximum is {})".
format(reftype['min'], reftype['max'])))

# old environmental layers & variable names
REF = options['env']
Expand All @@ -222,8 +222,8 @@ def main(options, flags):
raster_exists(PROJ)
if len(PROJ) != len(REF) and len(PROJ) != 0:
gs.fatal(_("The number of reference and predictor variables"
" should be the same. You provided %d reference and %d"
" projection variables") % (len(REF), len(PROJ)))
" should be the same. You provided {} reference and {}"
" projection variables".format(len(REF), len(PROJ))))

# output layers
opl = options['output']
Expand Down Expand Up @@ -368,7 +368,7 @@ def main(options, flags):
gs.run_command("r.mask", quiet=True, flags="r")

# Upload raster values and get value in python as frequency table
sql1 = "SELECT cat FROM %s" % tmpf0
sql1 = "SELECT cat FROM {}".format(str(tmpf0))
cn = len(np.hstack(db.db_select(sql=sql1)))
for m in xrange(len(REF)):

Expand All @@ -382,12 +382,12 @@ def main(options, flags):
mid = str(m)
laytype = gs.raster_info(REF[m])['datatype']
if laytype == 'CELL':
columns = "envvar_%s integer" % mid
columns = "envvar_{} integer".format(str(mid))
else:
columns = "envvar_%s double precision" % mid
gs.run_command("v.db.addcolumn", map=tmpf0, columns=columns,
quiet=True)
sql2 = "UPDATE %s SET envvar_%s = NULL" % (tmpf0, mid)
sql2 = "UPDATE {} SET envvar_{} = NULL".format(str(tmpf0), str(mid))
gs.run_command("db.execute", sql=sql2, quiet=True)
coln = "envvar_%s" % mid
gs.run_command("v.what.rast", quiet=True, map=tmpf0,
Expand All @@ -402,9 +402,9 @@ def main(options, flags):

# Check for point without values
if b < cn:
gs.info(_("Please note that there were %d points without "
gs.info(_("Please note that there were {} points without "
"value. This is probably because they are outside "
"the computational region or mask") % (cn - b))
"the computational region or mask".format((cn - b))))

# Set region to env_proj layers (if different from env) and remove
# mask (if set above)
Expand Down
115 changes: 115 additions & 0 deletions grass7/raster/r.mess/tests/r_mess_tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#-----------------------------------------------------------------------
# Test 1 - same area, without mask
#-----------------------------------------------------------------------
g.remove type=raster pattern=Ex_* -f
g.region raster=bio1
r.mess2 env=bio1,bio12,bio15 \
output=Ex_01
r.mess2 -c -k -m -n env=bio1,bio12,bio15 \
ref_rast=ppa \
output=Ex_02
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=IPSL_bio1,IPSL_bio12,IPSL_bio15 \
output=Ex_03
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
ref_vect=sample_locations@mess \
output=Ex_04
r.mess2 -c -k -m -n env=bio1,bio12,bio15 \
env_proj=IPSL_bio1,IPSL_bio12,IPSL_bio15 \
ref_vect=sample_locations@mess \
output=Ex_05

#-----------------------------------------------------------------------
# Test 2 - same area, with mask
#-----------------------------------------------------------------------
g.remove type=raster pattern=Ex_* -f
g.region raster=bio1
r.mask raster=pnvmap
r.mess2 env=bio1,bio12,bio15 \
output=Ex_06
r.mask raster=pnvmap
r.mess2 -c -k -m -n env=bio1,bio12,bio15 \
ref_rast=ppa \
output=Ex_07
r.mask raster=pnvmap
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=IPSL_bio1,IPSL_bio12,IPSL_bio15 \
output=Ex_08
r.mask raster=pnvmap
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
ref_vect=sample_locations@mess \
output=Ex_09
r.mask raster=pnvmap
r.mess2 -c -k -m -n env=bio1,bio12,bio15 \
env_proj=IPSL_bio1,IPSL_bio12,IPSL_bio15 \
ref_vect=sample_locations@mess \
output=Ex_10

#-----------------------------------------------------------------------
# Test 3 - different reference and projection area, without mask
#-----------------------------------------------------------------------
g.remove type=raster pattern=Ex_* -f
r.mask -r
g.region raster=bio1
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=bio1_Eth,bio12_Eth,bio15_Eth \
output=Ex_11
g.region raster=bio1
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=bio1_Eth,bio12_Eth,bio15_Eth \
ref_rast=ppa \
output=Ex_12
g.region raster=bio1
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=bio1_Eth,bio12_Eth,bio15_Eth \
ref_vect=sample_locations@mess \
output=Ex_13

# Test 3 - different reference and projection area, with mask
#-----------------------------------------------------------------------
g.remove type=raster pattern=Ex_* -f
g.region raster=bio1
r.mask raster=pnvmap
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=bio1_Eth,bio12_Eth,bio15_Eth \
output=Ex_14
g.region raster=bio1
r.mask raster=pnvmap
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=bio1_Eth,bio12_Eth,bio15_Eth \
ref_rast=ppa \
output=Ex_15
g.region raster=bio1
r.mask raster=pnvmap
r.mess2 -c -k -m -n \
env=bio1,bio12,bio15 \
env_proj=bio1_Eth,bio12_Eth,bio15_Eth \
ref_vect=sample_locations@mess \
output=Ex_16

# Test 4 - Compare tests that should give the same results
#-----------------------------------------------------------------------
g.region raster=bio1
r.mask -r
r.mess2 -c -k -m -n env=bio1,bio12,bio15 \
ref_rast=ppa \
output=Ex_02

r.mask raster=ppa
r.mess2 -c -k -m -n env=bio1,bio12,bio15 output=Ex_17







0 comments on commit ddd8bcc

Please sign in to comment.