[GRASS-SVN] r45338 - grass-addons/raster/r.diversity
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Feb 8 09:43:01 EST 2011
Author: lucadelu
Date: 2011-02-08 06:43:00 -0800 (Tue, 08 Feb 2011)
New Revision: 45338
Modified:
grass-addons/raster/r.diversity/description.html
grass-addons/raster/r.diversity/r.diversity.py
Log:
some change in code for size
Modified: grass-addons/raster/r.diversity/description.html
===================================================================
--- grass-addons/raster/r.diversity/description.html 2011-02-08 08:00:46 UTC (rev 45337)
+++ grass-addons/raster/r.diversity/description.html 2011-02-08 14:43:00 UTC (rev 45338)
@@ -9,6 +9,8 @@
<h2>NOTES</h2>
The user does not need to create a "conf" file with <em>r.li.setup</em> because this file
will be created automatically by the script.<br>
+In size option it is possible use more values: the user can set more values with comma (,) and a range with minus (-)<br>
+If you calculate Renyi entropy remember to add the alpha option. <br>
If the input raster contains NULL value cells, <em>r.diversity</em>
returns -1 for these cells.<br>
If the user wants to keep NULL values instead, run subsequently on the resulting map:
@@ -29,7 +31,16 @@
r.diversity input=ndvi_map out=test alpha=0.5 size=7
</pre></div>
+To calculate only Pielou and Simpson indices from a NDVI map, with several moving window (3 x 3, 5 x 5, 7 x 7, 9 x 9), run:
+<div class="code"><pre>
+r.diversity input=ndvi_map out=test size=3-9 method=pielou,simpson
+</pre></div>
+To calculate all methods excluding Pielou from a NDVI map, with two moving window (3 x 3, 9 x 9), run:
+<div class="code"><pre>
+r.diversity input=ndvi_map out=test size=3,9 exclude=pielou alpha=2
+</pre></div>
+
<h2>SEE ALSO</h2>
<em>
Modified: grass-addons/raster/r.diversity/r.diversity.py
===================================================================
--- grass-addons/raster/r.diversity/r.diversity.py 2011-02-08 08:00:46 UTC (rev 45337)
+++ grass-addons/raster/r.diversity/r.diversity.py 2011-02-08 14:43:00 UTC (rev 45338)
@@ -41,19 +41,41 @@
#% type: double
#% gisprompt: alpha value
#% key_desc: alpha value for Renyi entropy
-#% description: Alpha value is the order of the generalized entropy, it'll be > 0 and != 1
-#% required: yes
+#% description: Order of generalized entropy (> 0.0; undefined for 1.0)
+#% required: no
#%end
#%option
#% key: size
#% type: integer
#% gisprompt: resolution
#% key_desc: moving window
-#% description: Number of pixel used for the moving window, it must be odd number, if not set the module set your resolution to value 3
+#% multiple: yes
+#% description: Size of processing window (odd number only)
+#% answer: 3
#% required: no
#%end
-
+#%option
+#% key: method
+#% type: string
+#% gisprompt: method to use
+#% key_desc: method
+#% options: simpson,shannon,pielou,renyi
+#% multiple: yes
+#% description: Name of methods to use
+#% required: no
+#%end
+#%option
+#% key: exclude
+#% type: string
+#% gisprompt: exclude method
+#% key_desc: exclude method
+#% options: simpson,shannon,pielou,renyi
+#% multiple: yes
+#% description: Exclude methods
+#% required: no
+#%end
+
# import library
import os, sys, re
import grass.script as grass
@@ -62,8 +84,6 @@
def main():
# set the home path
home=os.path.expanduser('~')
- # set the name of conf file
- confilename = home+'/.r.li/history/conf_diversity'
# check if GISBASE is set
if "GISBASE" not in os.environ:
# return an error advice
@@ -78,52 +98,123 @@
res = options['size']
# alpha value for r.renyi
alpha_value = options['alpha']
- # resolution
- if res=='':
- res=3
- else:
- res = int(options['size'])
+ # method to use
+ methods = options['method']
+ # excluded method
+ excludes = options['exclude']
- # check if is a odd number
- if res%2==0:
- # return the error advice
- print "Your size option must be an odd number"
- sys.exit(1)
+
+ resolution = checkResValues(res)
+
# check if ~/.r.li path exists
- if os.path.exists(home+'/.r.li/'):
- # check if ~/.r.li/history path exists
- if os.path.exists(home+'/.r.li/history'):
- # create configuration file
- createConfFile(res,map_in,confilename)
- else:
- # create ~/.r.li/history path
- os.path.mkdir(home+'/.r.li/history')
- # create configuration file
- createConfFile(res,map_in,confilename)
- else:
+ if not os.path.exists(home+'/.r.li/'):
# create ~/.r.li
os.path.mkdir(home+'/.r.li/')
# create ~/.r.li/history
os.path.mkdir(home+'/.r.li/history')
- # create configuration file
- createConfFile(res,map_in,confilename)
-
-
- ### calculate r.li indices
- # if overwrite it is set
+ else:
+ if not os.path.exists(home+'/.r.li/history'):
+ # create ~/.r.li/history
+ os.path.mkdir(home+'/.r.li/history')
+ # set overwrite
if grass.overwrite():
env['GRASS_OVERWRITE'] = '1'
+
+ # if method and exclude option are not null return an error
+ if methods != '' and excludes != '':
+ print "You can use method or exclude option not both"
+ sys.exit(1)
+ # if method and exclude option are null calculate all module
+ elif methods == '' and excludes == '':
+ # check if alpha_value is set, else return an error
+ if alpha_value == '':
+ print "Please you must set alpha value for Renyi entropy"
+ sys.exit(1)
+ calculateAll(home, map_in, map_out, resolution, alpha_value)
+ # calculate method
+ elif methods != '':
+ methods = methods.split(',')
+ checkAlpha(methods,alpha_value)
+ calculateM(home, map_in, map_out, resolution, alpha_value,methods)
+ # calculate not excluded index
+ elif excludes != '':
+ excludes = excludes.split(',')
+ checkAlpha(excludes,alpha_value,True)
+ calculateE(home, map_in, map_out, resolution, alpha_value,excludes)
- simpson = grass.run_command('r.li.simpson', map = map_in, out = map_out + '_simpson', conf = 'conf_diversity')
- shannon = grass.run_command('r.li.shannon', map = map_in, out = map_out+ '_shannon', conf = 'conf_diversity')
- pielou = grass.run_command('r.li.pielou', map = map_in, out = map_out+ '_pielou', conf = 'conf_diversity')
- renyi = grass.run_command('r.li.renyi', map = map_in, out = map_out+ '_renyi_' + str(alpha_value), conf = 'conf_diversity', alpha = alpha_value)
+ # remove configuration files
+ removeConfFile(resolution,home)
+ print 'All works are terminated'
- os.remove(confilename)
- print 'All works are terminated'
+# calculate all index
+def calculateAll(home, map_in, map_out, res, alpha):
+ # for each resolution create the config file and calculate all index
+ for r in res:
+ createConfFile(r,map_in,home)
+ r = str(r)
+ grass.run_command('r.li.simpson', map = map_in, out = map_out +
+ '_simpson_size_' + r, conf = 'conf_diversity_' + r)
+ grass.run_command('r.li.shannon', map = map_in, out = map_out+
+ '_shannon_size_' + r, conf = 'conf_diversity_' + r)
+ grass.run_command('r.li.pielou', map = map_in, out = map_out+
+ '_pielou_size_' + r, conf = 'conf_diversity_' + r)
+ grass.run_command('r.li.renyi', map = map_in, out = map_out+
+ '_renyi_size_' + r + '_alpha_'+ str(alpha), conf = 'conf_diversity_' + r,
+ alpha = alpha)
+# calculate only method included in method option
+def calculateM(home, map_in, map_out, res, alpha, method):
+ # for each resolution create the config file
+ for r in res:
+ createConfFile(r,map_in,home)
+ r = str(r)
+ # for each method in method option calculate index
+ for i in method:
+ if i == 'renyi':
+ grass.run_command('r.li.renyi', map = map_in, out = map_out +
+ '_renyi_size_' + r + '_alpha_' + str(alpha), conf =
+ 'conf_diversity_' + r, alpha = alpha)
+ else:
+ grass.run_command('r.li.' + i, map = map_in, out = map_out +
+ '_' + i + '_size_' + r, conf = 'conf_diversity_' + r)
+
+# calculate only method excluded with exclude option
+def calculateE(home, map_in, map_out, res, alpha, method):
+ # set a tuple with all index
+ methods = ('simpson','shannon','pielou','renyi')
+ # for each resolution create the config file
+ for r in res:
+ createConfFile(r,map_in,home)
+ r = str(r)
+ # for each method
+ for i in methods:
+ # if method it isn't in exclude option it is possible to calculate
+ if method.count(i) == 0:
+ if i == 'renyi':
+ grass.run_command('r.li.renyi', map = map_in, out =
+ map_out + '_renyi_size_' + r + '_alpha_' + str(alpha),
+ conf = 'conf_diversity_' + r, alpha = alpha)
+ else:
+ grass.run_command('r.li.' + i, map = map_in, out = map_out +
+ '_' + i+ '_size_' + r, conf = 'conf_diversity_' + r)
+
+# check if alpha value it's set when renyi entropy must be calculate
+def checkAlpha(method,alpha,negative=False):
+ # it's used when we check the exclude option
+ if negative:
+ if method.count('renyi') != 1 and alpha == '':
+ print "Please you must set alpha value for Renyi entropy"
+ sys.exit(1)
+ # it's used when we check the method option
+ else:
+ if method.count('renyi') == 1 and alpha == '':
+ print "Please you must set alpha value for Renyi entropy"
+ sys.exit(1)
+
#create configuration file instead using r.li.setup
-def createConfFile(res,inpumap,filename):
+def createConfFile(res,inpumap,home):
+ # set the name of conf file
+ confilename = home + '/.r.li/history/conf_diversity_' + str(res)
# start the text for the conf file
outputLine = ['SAMPLINGFRAME 0|0|1|1\n']
# return r.info about input file
@@ -133,19 +224,51 @@
# calculate number of columns
columns = (rinfo['east']-rinfo['west'])/rinfo['ewres']
# value for row
- rV = res/rows
+ rV = int(res)/rows
# value for column
- cV = res/columns
+ cV = int(res)/columns
# append the text for the conf file
outputLine.append('SAMPLEAREA -1|-1|'+str(rV)+'|'+str(cV)+'\n')
outputLine.append('MOVINGWINDOW\n')
# open configuration file
- fileConf=open(filename,'w')
+ fileConf=open(confilename,'w')
# write file
fileConf.writelines(outputLine)
# close file
fileConf.close()
+# return a list of resolution
+def checkResValues(res):
+ # check if more values are passed
+ if res.count(',') == 1:
+ typ = 'values'
+ reso = res.split(',')
+ # check if a range of values are passed
+ elif res.count('-') == 1:
+ typ = 'range'
+ reso = res.split('-')
+ # else only a value is passed
+ else:
+ typ = 'value'
+ reso = [res]
+ # trasforn string to int and check if is a odd number
+ for i in range(len(reso)):
+ # check if is a odd number
+ reso[i] = int(reso[i])
+ if reso[i] % 2 == 0:
+ # return the error advice
+ print "Your size option could not contain odd number"
+ sys.exit(1)
+ # create a range
+ if typ == 'range':
+ reso = range(reso[0],reso[1]+1,2)
+ return reso
+
+def removeConfFile(res,home):
+ for r in res:
+ confilename = home + '/.r.li/history/conf_diversity_' + str(r)
+ os.remove(confilename)
+
if __name__ == "__main__":
options, flags = grass.parser()
sys.exit(main())
More information about the grass-commit
mailing list