[GRASS-SVN] r68904 - grass-addons/grass7/raster/r.mess

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jul 9 05:03:37 PDT 2016


Author: pvanbosgeo
Date: 2016-07-09 05:03:37 -0700 (Sat, 09 Jul 2016)
New Revision: 68904

Modified:
   grass-addons/grass7/raster/r.mess/r.mess.py
Log:
r.mess: improved how history is written to layers metadata (r.support), refactoring guisections

Modified: grass-addons/grass7/raster/r.mess/r.mess.py
===================================================================
--- grass-addons/grass7/raster/r.mess/r.mess.py	2016-07-09 12:03:18 UTC (rev 68903)
+++ grass-addons/grass7/raster/r.mess/r.mess.py	2016-07-09 12:03:37 UTC (rev 68904)
@@ -9,7 +9,7 @@
 #               surface (MESS) as proposed by Elith et al., 2010,
 #               Methods in Ecology & Evolution, 1(330–342).
 #
-# COPYRIGHT: (C) 1997-2016 by the GRASS Development Team
+# COPYRIGHT: (C) 2014-2016 by Paulo van Breugel and the GRASS Development Team
 #
 #            This program is free software under the GNU General Public
 #            License (>=v2). Read the file COPYING that comes with GRASS
@@ -19,48 +19,49 @@
 #
 #%Module
 #% description: Computes multivariate environmental similarity surface (MES)
+#% keyword: similarity
 #% keyword: raster
 #% keyword: modelling
 #%End
 
+#%option G_OPT_R_INPUTS
+#% key: env
+#% description: Reference conditions
+#% key_desc: names
+#% required: yes
+#% guisection: Input
+#%end
+
+#%option G_OPT_R_INPUTS
+#% key: env_proj
+#% description: Projected conditions
+#% key_desc: names
+#% required: no
+#% guisection: Input
+#%end
+
 #%option G_OPT_R_INPUT
 #% key: ref_rast
-#% label: Reference/sample area (raster)
-#% description: Reference areas (1 = presence, 0 = absence)
+#% label: Reference area (raster)
+#% description: Reference areas (1 = presence, 0 or null = absence)
 #% key_desc: name
 #% required: no
-#% guisection: reference distribution
+#% guisection: Input
 #%end
 
 #%option G_OPT_V_MAP
 #% key: ref_vect
-#% label: Reference/sample points (vector)
+#% label: Reference points (vector)
 #% description: Point vector layer with presence locations
 #% key_desc: name
 #% required: no
-#% guisection: reference distribution
+#% guisection: Input
 #%end
 
 #%rules
 #%exclusive: ref_rast,ref_vect
 #%end
 
-#%option G_OPT_R_INPUTS
-#% key: env
-#% description: Reference / baseline environmental data layers
-#% key_desc: names
-#% required: yes
-#% guisection: predictors
-#%end
-
-#%option G_OPT_R_INPUTS
-#% key: env_proj
-#% description: Projected / test environmental data layers
-#% key_desc: names
-#% required: no
-#% guisection: predictors
-#%end
-
 #%option G_OPT_R_BASENAME_OUTPUT
 #% description: Root name of the output MESS data layers
 #% key_desc: name
@@ -106,10 +107,6 @@
 #% guisection: Output
 #%end
 
-#----------------------------------------------------------------------------
-# Standard
-#----------------------------------------------------------------------------
-
 # import libraries
 import os
 import sys
@@ -185,11 +182,7 @@
     gs.write_command("r.colors", map=INipi, rules='-',
                      stdin=COLORS_MES, quiet=True)
 
-#----------------------------------------------------------------------------
-# main function
-#----------------------------------------------------------------------------
 
-
 def main(options, flags):
 
     gisbase = os.getenv('GISBASE')
@@ -197,24 +190,19 @@
         gs.fatal(_('$GISBASE not defined'))
         return 0
 
-    #-------------------------------------------------------------------------
-    # Variables, options and flags into variables
-    #-------------------------------------------------------------------------
-
-    # reference layer
+    # Reference / sample area or points
     ref_rast = options['ref_rast']
     ref_vect = options['ref_vect']
-
-    # Check if ref_rast map is of type cell and values are limited to 1 and 0
     if ref_rast:
         reftype = gs.raster_info(ref_rast)
         if reftype['datatype'] != "CELL":
             gs.fatal(_("The ref_rast map must have type CELL (integer)"))
-        if reftype['min'] != 0 or reftype['max'] != 1:
-            grass.fatal(_("The ref_rast map must be a binary raster,"
-                          " i.e. it should contain only values 0 and 1"
-                          " (now the minimum is %d and maximum is %d)")
-                        % (reftype['min'], reftype['max']))
+        if ((reftype['min'] != 0 and reftype['min'] != 1) or
+           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']))
 
     # old environmental layers & variable names
     REF = options['env']
@@ -256,47 +244,10 @@
     # get current region settings, to compare to new ones later
     region_1 = gs.parse_command("g.region", flags="g")
 
-    #-------------------------------------------------------------------------
     # Text for history in metadata
-    #-------------------------------------------------------------------------
-
-    if flm:
-        a1 = " -m"
-    else:
-        a1 = ""
-    if flk:
-        a2 = " -k"
-    else:
-        a2 = ""
-    if fln:
-        a3 = " -n"
-    else:
-        a3 = ""
-    if fli:
-        a4 = " -i"
-    else:
-        a4 = ""
-    if flc:
-        a5 = " -c\n"
-    else:
-        a5 = "\n"
-    a6 = "       env={}\n".format(options['env'])
-    if options['env_proj']:
-        a7 = "       env_proj={}\n".format(options['env_proj'])
-    else:
-        a7 = ""
-    if ref_rast:
-        a8 = "       ref_rast={}\n".format(ref_rast)
-    else:
-        a8 = ""
-    if ref_vect:
-        a9 = "       ref_vect={}\n".format(ref_vect)
-    else:
-        a9 = ""
-    a10 = "       output={}\n".format(options['output'])
-    hist = "r.mess{}{}{}{}{}{}{}{}{}" \
-           .format(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
-
+    opt2 = dict((k, v) for k, v in options.iteritems() if v)
+    hist = ' '.join("{!s}={!r}".format(k, v) for (k, v) in opt2.iteritems())
+    hist = "r.mess {}".format(hist)
     unused, tmphist = tempfile.mkstemp()
     text_file = open(tmphist, "w")
     text_file.write(hist)
@@ -308,10 +259,7 @@
         gs.mapcalc("$i = if(isnull($r),null(),1)", i=ref_rast, r=REF[0],
                    quiet=True)
 
-    #-------------------------------------------------------------------------
     # Create the recode table - Reference distribution is raster
-    #-------------------------------------------------------------------------
-
     citiam = gs.find_file(name='MASK', element='cell',
                           mapset=gs.gisenv()['MAPSET'])
     if citiam['fullname']:
@@ -407,10 +355,7 @@
             # Change region back to original
             gs.del_temp_region()
 
-    #-------------------------------------------------------------------------
     # Create the recode table - Reference distribution is vector
-    #-------------------------------------------------------------------------
-
     else:
         vtl = ref_vect
 
@@ -525,10 +470,7 @@
             # Change region back to original
             gs.del_temp_region()
 
-    #-----------------------------------------------------------------------
     # Calculate MESS statistics
-    #-----------------------------------------------------------------------
-
     # Set region to env_proj layers (if different from env)
     # Note: this changes the region, to ensure the newly created layers
     # are actually visible to the user. This goes against normal practise



More information about the grass-commit mailing list