[GRASS-user] latest r.width.funct.py appears to be broken
Bartolomei.Chris
Bartolomei.Chris at ensco.com
Thu Nov 1 11:10:46 PDT 2018
Hi Markus
As always thank you for the prompt response 😊
I think maybe the fix is not a fix then as we had done this upgrade to GRASS 7.4.2 and the re-installation of the addons and testing just a few hours ago … (???)
I’ll include the code of the broken r.width.funct.py below just in case.
😊
Chris
Broken r.width.funct.py:
#!/usr/bin/env python
################################################################################
#
# MODULE: r.width.funct
#
# AUTHOR(S): Massimo Di Stefano, Francesco Di Stefano, Margherita Di Leo
#
# PURPOSE: The module produces the Width Function of a basin. The Width
# Function W(x) gives the number of the cells in a basin at a
# flow distance x from the outlet (distance-area function)
#
# COPYRIGHT: (c) 2010 Massimo Di Stefano, Francesco Di Stefano, Margherita Di Leo
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
# REQUIRES: Matplotlib
# http://matplotlib.sourceforge.net/
#
#
################################################################################
#%module
#% description: Calculates the Width Function of a watershed basin.
#% keyword: raster
#% keyword: hydrology
#%end
#%option G_OPT_R_INPUT
#% key: map
#% description: Distance to outlet map (from r.stream.distance)
#% required: yes
#%end
#%option G_OPT_F_OUTPUT
#% key: image
#% key_desc: image
#% description: Name for output graph file (png)
#% required: yes
#%END
import sys
import os
import matplotlib #required by windows
matplotlib.use('wx') #required by windows
import matplotlib.pyplot as plt
import grass.script as grass
import numpy as np
from operator import itemgetter
def main():
stats = grass.read_command('r.stats', input = options['map'], sep = 'space', nv = '*', nsteps = '255', flags = 'Anc').split('\n')[:-1]
# res = cellsize
res = grass.region()['nsres']
zn = np.zeros((len(stats),4),float)
kl = np.zeros((len(stats),2),float)
prc = np.zeros((9,2),float)
for i in range(len(stats)):
if i == 0:
zn[i,0], zn[i,1] = map(float, stats[i].split(' '))
zn[i,1] = zn[i,1]
zn[i,2] = zn[i,1] * res
if i != 0:
zn[i,0], zn[i,1] = map(float, stats[i].split(' '))
zn[i,2] = zn[i,1] + zn[i-1,2]
zn[i,3] = zn[i,1] * (res**2)
totcell = sum(zn[:,1])
print "Tot. cells", totcell
totarea = totcell * (res**2)
print "Tot. area", totarea
maxdist = max(zn[:,0])
print "Max distance", maxdist
for i in range(len(stats)):
kl[i,0] = zn[i,0]
kl[i,1] = zn[i,2] / totcell
# quantiles
prc[0,0] , prc[0,1] = findint(kl,0.05) , 0.05
prc[1,0] , prc[1,1] = findint(kl,0.15) , 0.15
prc[2,0] , prc[2,1] = findint(kl,0.3) , 0.3
prc[3,0] , prc[3,1] = findint(kl,0.4) , 0.4
prc[4,0] , prc[4,1] = findint(kl,0.5) , 0.5
prc[5,0] , prc[5,1] = findint(kl,0.6) , 0.6
prc[6,0] , prc[6,1] = findint(kl,0.7) , 0.7
prc[7,0] , prc[7,1] = findint(kl,0.85) , 0.85
prc[8,0] , prc[8,1] = findint(kl,0.95) , 0.95
# plot
plotImage(zn[:,0], zn[:,3], options['image']+'_width_function.png','-','x','W(x)','Width Function')
print "==========================="
print "Width Function | quantiles"
print "==========================="
print '%.0f' %findint(kl,0.05) , "|", 0.05
print '%.0f' %findint(kl,0.15) , "|", 0.15
print '%.0f' %findint(kl,0.3) , "|", 0.3
print '%.0f' %findint(kl,0.4) , "|", 0.4
print '%.0f' %findint(kl,0.5) , "|", 0.5
print '%.0f' %findint(kl,0.6) , "|", 0.6
print '%.0f' %findint(kl,0.7) , "|", 0.7
print '%.0f' %findint(kl,0.85) , "|", 0.85
print '%.0f' %findint(kl,0.95) , "|", 0.95
print '\n'
print 'Done!'
def plotImage(x,y,image,type,xlabel,ylabel,title):
plt.plot(x, y, type)
plt.ylabel(ylabel)
plt.xlabel(xlabel)
plt.xlim( min(x), max(x) )
plt.ylim( min(y), max(y) )
plt.title(title)
plt.grid(True)
plt.savefig(image)
plt.close('all')
def findint(kl,f):
Xf = np.abs(kl-f);
Xf = np.where(Xf==Xf.min())
item = itemgetter(0)(Xf)
Xf = item[0]
z1, z2, f1, f2 = kl[int(Xf[0])][0], kl[int(Xf[0]-1)][0], kl[int(Xf[0])][1], kl[int(Xf[0]-1)][1]
z = z1 + ((z2 - z1) / (f2 - f1)) * (f - f1)
return z
if __name__ == "__main__":
options, flags = grass.parser()
sys.exit(main())
From: Markus Neteler <neteler at osgeo.org>
Sent: Thursday, November 1, 2018 2:02 PM
To: Bartolomei.Chris <Bartolomei.Chris at ensco.com>
Cc: GRASS developers list <grass-dev at lists.osgeo.org>; GRASS user list <grass-user at lists.osgeo.org>
Subject: Re: latest r.width.funct.py appears to be broken
*** WARNING ***
EXTERNAL EMAIL -- This message originates from outside ENSCO Inc.
On Thu, Nov 1, 2018 at 6:35 PM Bartolomei.Chris <Bartolomei.Chris at ensco.com<mailto:Bartolomei.Chris at ensco.com>> wrote:
Good afternoon Markus & Co. 😊
It appears that the latest version of r.width.funct.py<https://urldefense.proofpoint.com/v2/url?u=http-3A__r.width.funct.py&d=DwMFaQ&c=DsZY2bea7iNIzyp-7sZ0t0F2UfNQZUfZhEPCv_2wBI0&r=O31ltou6ygJL2Y01kQyNJJD2kiILIsbyz2V0Hn4lFUY&m=XhRpuNDp6HFiTJYSL5gD0-64qjw6Kdg34dAHLBt512o&s=VeM6lBqMFVRt3wxr_4NxdnE8FRa5n8MzEzqzjPN_3hI&e=> is not functioning properly ... We have a few Windows10 systems here running GRASS; I'm on v7.4.0 and another person has (had) 7.2.2. She was trying to run r.basin on a 30m resolution DEM and kept getting the following error:
Traceback (most recent call last):
File "C:\Users\XXX\AppData\Roaming\GRASS7\addons/scripts/r.width.funct.py<https://urldefense.proofpoint.com/v2/url?u=http-3A__r.width.funct.py&d=DwMFaQ&c=DsZY2bea7iNIzyp-7sZ0t0F2UfNQZUfZhEPCv_2wBI0&r=O31ltou6ygJL2Y01kQyNJJD2kiILIsbyz2V0Hn4lFUY&m=XhRpuNDp6HFiTJYSL5gD0-64qjw6Kdg34dAHLBt512o&s=VeM6lBqMFVRt3wxr_4NxdnE8FRa5n8MzEzqzjPN_3hI&e=>", line 135, in <module>
sys.exit(main())
File "C:\Users\XXX\AppData\Roaming\GRASS7\addons/scripts/r.width.funct.py<https://urldefense.proofpoint.com/v2/url?u=http-3A__r.width.funct.py&d=DwMFaQ&c=DsZY2bea7iNIzyp-7sZ0t0F2UfNQZUfZhEPCv_2wBI0&r=O31ltou6ygJL2Y01kQyNJJD2kiILIsbyz2V0Hn4lFUY&m=XhRpuNDp6HFiTJYSL5gD0-64qjw6Kdg34dAHLBt512o&s=VeM6lBqMFVRt3wxr_4NxdnE8FRa5n8MzEzqzjPN_3hI&e=>", line 84, in main
prc[0,0] , prc[0,1] = findint(kl,0.05) , 0.05
File "C:\Users\XXX\AppData\Roaming\GRASS7\addons/scripts/r.width.funct.py<https://urldefense.proofpoint.com/v2/url?u=http-3A__r.width.funct.py&d=DwMFaQ&c=DsZY2bea7iNIzyp-7sZ0t0F2UfNQZUfZhEPCv_2wBI0&r=O31ltou6ygJL2Y01kQyNJJD2kiILIsbyz2V0Hn4lFUY&m=XhRpuNDp6HFiTJYSL5gD0-64qjw6Kdg34dAHLBt512o&s=VeM6lBqMFVRt3wxr_4NxdnE8FRa5n8MzEzqzjPN_3hI&e=>", line 128, in findint
z1, z2, f1, f2 = kl[int(Xf[0])][0], kl[int(Xf[0]-1)][0], kl[int(Xf[0])][1], kl[int(Xf[0]-1)][1]
IndexError: invalid index to scalar variable.
This was fixed the other day by madi :)
2018-10-23 03:26:54 -0700 (Tue, 23 Oct 2018)
New Revision: 73602
To get that, just reinstall the addon with g.extension.
Best
Markus
PS: thanks anyway for the updated script! The author solved it in a slightly different way.
--
Markus Neteler, PhD
http://www.mundialis.de<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.mundialis.de&d=DwMFaQ&c=DsZY2bea7iNIzyp-7sZ0t0F2UfNQZUfZhEPCv_2wBI0&r=O31ltou6ygJL2Y01kQyNJJD2kiILIsbyz2V0Hn4lFUY&m=XhRpuNDp6HFiTJYSL5gD0-64qjw6Kdg34dAHLBt512o&s=FvF_73Tv4UlH_EK_kL7lUtvOXXrdSar0Mky6c_7Wy2Q&e=> - free data with free software
http://grass.osgeo.org<https://urldefense.proofpoint.com/v2/url?u=http-3A__grass.osgeo.org&d=DwMFaQ&c=DsZY2bea7iNIzyp-7sZ0t0F2UfNQZUfZhEPCv_2wBI0&r=O31ltou6ygJL2Y01kQyNJJD2kiILIsbyz2V0Hn4lFUY&m=XhRpuNDp6HFiTJYSL5gD0-64qjw6Kdg34dAHLBt512o&s=-N2L_zQo78vOZVe5P6HJHWh57_tF0wxmstz5xHrjPik&e=>
http://courses.neteler.org/blog<https://urldefense.proofpoint.com/v2/url?u=http-3A__courses.neteler.org_blog&d=DwMFaQ&c=DsZY2bea7iNIzyp-7sZ0t0F2UfNQZUfZhEPCv_2wBI0&r=O31ltou6ygJL2Y01kQyNJJD2kiILIsbyz2V0Hn4lFUY&m=XhRpuNDp6HFiTJYSL5gD0-64qjw6Kdg34dAHLBt512o&s=IBoPPRP1JM5TZZ0Dx8qozMXD-0EXlx0fy45x4eVfy-o&e=>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20181101/5fe6f02f/attachment-0001.html>
More information about the grass-user
mailing list