[GRASS-SVN] r63578 - in grass/branches/releasebranch_7_0: . scripts/i.pansharpen

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 17 05:16:40 PST 2014


Author: lucadelu
Date: 2014-12-17 05:16:40 -0800 (Wed, 17 Dec 2014)
New Revision: 63578

Modified:
   grass/branches/releasebranch_7_0/
   grass/branches/releasebranch_7_0/scripts/i.pansharpen/i.pansharpen.py
Log:
i.pansharpen: added group creation for the output, PEP8 cleaning


Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
   - /grass/trunk:60817,61096,61141,61994,62105,62179-62180,62182,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750,62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62831,62838,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-63064,63066,63068,63070-63071,63074,63076,63079,63081,
 63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63276,63279,63281,63283,63287,63290,63292,63302,63307,63315,63319,63330,63332,63339,63342,63345,63362,63367,63391,63393,63408-63409,63416-63417,63425,63427,63429,63431,63433,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63562,63570
   + /grass/trunk:60817,61096,61141,61994,62105,62179-62180,62182,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750,62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62831,62838,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-63064,63066,63068,63070-63071,63074,63076,63079,63081,
 63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63276,63279,63281,63283,63287,63290,63292,63302,63307,63315,63319,63330,63332,63339,63342,63345,63362,63367,63391,63393,63408-63409,63416-63417,63425,63427,63429,63431,63433,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63562,63570,63576

Modified: grass/branches/releasebranch_7_0/scripts/i.pansharpen/i.pansharpen.py
===================================================================
--- grass/branches/releasebranch_7_0/scripts/i.pansharpen/i.pansharpen.py	2014-12-17 10:52:29 UTC (rev 63577)
+++ grass/branches/releasebranch_7_0/scripts/i.pansharpen/i.pansharpen.py	2014-12-17 13:16:40 UTC (rev 63578)
@@ -4,7 +4,7 @@
 #
 # MODULE:	    i.panmethod
 #
-# AUTHOR(S):    Overall script by Michael Barton (ASU)	
+# AUTHOR(S):    Overall script by Michael Barton (ASU)
 #               Brovey transformation in i.fusion.brovey by Markus Neteler <<neteler at osgeo org>>
 #               i.fusion brovey converted to Python by Glynn Clements
 #               IHS and PCA transformation added by Michael Barton (ASU)
@@ -12,7 +12,7 @@
 #               Thanks to Markus Metz for help with PCA inversion
 #               Thanks to Hamish Bowman for parallel processing algorithm
 #
-# PURPOSE:	Sharpening of 3 RGB channels using a high-resolution panchromatic channel 
+# PURPOSE:	Sharpening of 3 RGB channels using a high-resolution panchromatic channel
 #
 # COPYRIGHT:	(C) 2002-2012 by the GRASS Development Team
 #
@@ -21,14 +21,14 @@
 #		for details.
 #
 # REFERENCES:
-#   Roller, N.E.G. and Cox, S., 1980. Comparison of Landsat MSS and merged MSS/RBV 
-#   data for analysis of natural vegetation. Proc. of the 14th International 
+#   Roller, N.E.G. and Cox, S., 1980. Comparison of Landsat MSS and merged MSS/RBV
+#   data for analysis of natural vegetation. Proc. of the 14th International
 #   Symposium on Remote Sensing of Environment, San Jose, Costa Rica, 23-30 April, pp. 1001-1007.
 #
-#   Amarsaikhan, D., & Douglas, T. (2004). Data fusion and multisource image classification. 
+#   Amarsaikhan, D., & Douglas, T. (2004). Data fusion and multisource image classification.
 #   International Journal of Remote Sensing, 25(17), 3529-3539.
 #
-#   Behnia, P. (2005). Comparison between four methods for data fusion of ETM+ 
+#   Behnia, P. (2005). Comparison between four methods for data fusion of ETM+
 #   multispectral and pan images. Geo-spatial Information Science, 8(2), 98-103
 #
 #   for LANDSAT 5: see Pohl, C 1996 and others
@@ -80,7 +80,6 @@
 #%  description: Rebalance blue channel for LANDSAT
 #%end
 
-import sys
 import os
 
 try:
@@ -91,47 +90,48 @@
 
 import grass.script as grass
 
+
 def main():
     if not hasNumPy:
         grass.fatal(_("Required dependency NumPy not found. Exiting."))
 
-    sharpen   = options['method'] # sharpening algorithm
-    ms1       = options['blue'] # blue channel
-    ms2       = options['green'] # green channel
-    ms3       = options['red'] # red channel
-    pan       = options['pan'] # high res pan channel
-    out       = options['output'] # prefix for output RGB maps
-    bladjust  = flags['l'] # adjust blue channel
-    sproc     = flags['s'] # serial processing
-    
+    sharpen = options['method']  # sharpening algorithm
+    ms1 = options['blue']  # blue channel
+    ms2 = options['green']  # green channel
+    ms3 = options['red']  # red channel
+    pan = options['pan']  # high res pan channel
+    out = options['output']  # prefix for output RGB maps
+    bladjust = flags['l']  # adjust blue channel
+    sproc = flags['s']  # serial processing
+
     outb = grass.core.find_file('%s_blue' % out)
     outg = grass.core.find_file('%s_green' % out)
     outr = grass.core.find_file('%s_red' % out)
-        
+
     if (outb['name'] != '' or outg['name'] != '' or outr['name'] != '') and not grass.overwrite():
-        grass.warning(_('Maps with selected output prefix names already exist. \
-                        Delete them or use overwrite flag'))
+        grass.warning(_('Maps with selected output prefix names already exist.'
+                        ' Delete them or use overwrite flag'))
         return
-    
+
     pid = str(os.getpid())
 
-    #get PAN resolution:
-    kv = grass.raster_info(map = pan)
+    # get PAN resolution:
+    kv = grass.raster_info(map=pan)
     nsres = kv['nsres']
     ewres = kv['ewres']
     panres = (nsres + ewres) / 2
-    
+
     # clone current region
     grass.use_temp_region()
 
-    grass.run_command('g.region', res = panres, align = pan)
-    
+    grass.run_command('g.region', res=panres, align=pan)
+
     grass.message(_("Performing pan sharpening with hi res pan image: %f" % panres))
 
     if sharpen == "brovey":
         grass.verbose(_("Using Brovey algorithm"))
 
-        #pan/intensity histogram matching using linear regression
+        # pan/intensity histogram matching using linear regression
         outname = 'tmp%s_pan1' % pid
         panmatch1 = matchhist(pan, ms1, outname)
 
@@ -140,91 +140,95 @@
 
         outname = 'tmp%s_pan3' % pid
         panmatch3 = matchhist(pan, ms3, outname)
-        
+
         outr = '%s_red' % out
         outg = '%s_green' % out
         outb = '%s_blue' % out
 
-        #calculate brovey transformation
+        # calculate brovey transformation
         grass.message(_("Calculating Brovey transformation..."))
-        
+
         if sproc:
             # serial processing
             e = '''eval(k = "$ms1" + "$ms2" + "$ms3")
                 "$outr" = 1.0 * "$ms3" * "$panmatch3" / k
                 "$outg" = 1.0 * "$ms2" * "$panmatch2" / k
                 "$outb" = 1.0 * "$ms1" * "$panmatch1" / k'''
-            grass.mapcalc(e, outr=outr, outg=outg, outb=outb, 
-                          panmatch1=panmatch1, panmatch2=panmatch2, panmatch3=panmatch3, 
-                          ms1=ms1, ms2=ms2, ms3=ms3, overwrite=True)
+            grass.mapcalc(e, outr=outr, outg=outg, outb=outb,
+                          panmatch1=panmatch1, panmatch2=panmatch2,
+                          panmatch3=panmatch3, ms1=ms1, ms2=ms2, ms3=ms3,
+                          overwrite=True)
         else:
             # parallel processing
             pb = grass.mapcalc_start('%s_blue = (1.0 * %s * %s) / (%s + %s + %s)' %
-                                (out, ms1, panmatch1, ms1, ms2, ms3),
-                                overwrite=True)   
+                                     (out, ms1, panmatch1, ms1, ms2, ms3),
+                                     overwrite=True)
             pg = grass.mapcalc_start('%s_green = (1.0 * %s * %s) / (%s + %s + %s)' %
-                                (out, ms2, panmatch2, ms1, ms2, ms3),
-                                overwrite=True)   
+                                     (out, ms2, panmatch2, ms1, ms2, ms3),
+                                     overwrite=True)
             pr = grass.mapcalc_start('%s_red = (1.0 * %s * %s) / (%s + %s + %s)' %
-                                (out, ms3, panmatch3, ms1, ms2, ms3),
-                                overwrite=True)   
-               
+                                     (out, ms3, panmatch3, ms1, ms2, ms3),
+                                     overwrite=True)
+
             pb.wait()
             pg.wait()
             pr.wait()
-            
 
         # Cleanup
-        grass.run_command('g.remove', quiet=True, flags='f', type='rast', name='%s,%s,%s' % (panmatch1, panmatch2, panmatch3))
+        grass.run_command('g.remove', flags='f', quiet=True, type='rast',
+                          name='%s,%s,%s' % (panmatch1, panmatch2, panmatch3))
 
     elif sharpen == "ihs":
         grass.verbose(_("Using IHS<->RGB algorithm"))
-        #transform RGB channels into IHS color space
+        # transform RGB channels into IHS color space
         grass.message(_("Transforming to IHS color space..."))
         grass.run_command('i.rgb.his', overwrite=True,
-                          red_input=ms3, 
-                          green_input=ms2, 
-                          blue_input=ms1, 
-                          hue_output="tmp%s_hue" % pid, 
-                          intensity_output="tmp%s_int" % pid, 
+                          red_input=ms3,
+                          green_input=ms2,
+                          blue_input=ms1,
+                          hue_output="tmp%s_hue" % pid,
+                          intensity_output="tmp%s_int" % pid,
                           saturation_output="tmp%s_sat" % pid)
-                
-        #pan/intensity histogram matching using linear regression
+
+        # pan/intensity histogram matching using linear regression
         target = "tmp%s_int" % pid
         outname = "tmp%s_pan_int" % pid
         panmatch = matchhist(pan, target, outname)
-        
-        #substitute pan for intensity channel and transform back to RGB color space
+
+        # substitute pan for intensity channel and transform back to RGB color space
         grass.message(_("Transforming back to RGB color space and sharpening..."))
-        grass.run_command('i.his.rgb', overwrite=True, 
-                          hue_input="tmp%s_hue" % pid, 
-                          intensity_input="%s" % panmatch, 
+        grass.run_command('i.his.rgb', overwrite=True,
+                          hue_input="tmp%s_hue" % pid,
+                          intensity_input="%s" % panmatch,
                           saturation_input="tmp%s_sat" % pid,
-                          red_output="%s_red" % out, 
-                          green_output="%s_green" % out, 
+                          red_output="%s_red" % out,
+                          green_output="%s_green" % out,
                           blue_output="%s_blue" % out)
 
         # Cleanup
-        grass.run_command('g.remove', quiet=True, flags='f', type='rast', name=panmatch)
-        
+        grass.run_command('g.remove', flags='f', quiet=True, type='rast',
+                          name=panmatch)
+
     elif sharpen == "pca":
         grass.verbose(_("Using PCA/inverse PCA algorithm"))
         grass.message(_("Creating PCA images and calculating eigenvectors..."))
 
-        #initial PCA with RGB channels
-        pca_out = grass.read_command('i.pca', quiet=True, rescale='0,0', input='%s,%s,%s' % (ms1, ms2, ms3), output='tmp%s.pca' % pid)
+        # initial PCA with RGB channels
+        pca_out = grass.read_command('i.pca', quiet=True, rescale='0,0',
+                                     input='%s,%s,%s' % (ms1, ms2, ms3),
+                                     output='tmp%s.pca' % pid)
         if len(pca_out) < 1:
             grass.fatal(_("Input has no data. Check region settings."))
 
         b1evect = []
         b2evect = []
         b3evect = []
-        for l in pca_out.replace('(',',').replace(')',',').splitlines():
+        for l in pca_out.replace('(', ',').replace(')', ',').splitlines():
             b1evect.append(float(l.split(',')[1]))
             b2evect.append(float(l.split(',')[2]))
             b3evect.append(float(l.split(',')[3]))
-            
-        #inverse PCA with hi res pan channel substituted for principal component 1
+
+        # inverse PCA with hi res pan channel substituted for principal component 1
         pca1 = 'tmp%s.pca.1' % pid
         pca2 = 'tmp%s.pca.2' % pid
         pca3 = 'tmp%s.pca.3' % pid
@@ -240,22 +244,24 @@
 
         outname = 'tmp%s_pan' % pid
         panmatch = matchhist(pan, ms1, outname)
-        
+
         grass.message(_("Performing inverse PCA ..."))
-                
-        stats1 = grass.parse_command("r.univar", map=ms1, flags='g', 
-                                           parse=(grass.parse_key_val, { 'sep' : '=' }))
-        stats2 = grass.parse_command("r.univar", map=ms2, flags='g', 
-                                           parse=(grass.parse_key_val, { 'sep' : '=' }))
-        stats3 = grass.parse_command("r.univar", map=ms3, flags='g', 
-                                           parse=(grass.parse_key_val, { 'sep' : '=' }))
-            
+
+        stats1 = grass.parse_command("r.univar", map=ms1, flags='g',
+                                     parse=(grass.parse_key_val,
+                                            {'sep': '='}))
+        stats2 = grass.parse_command("r.univar", map=ms2, flags='g',
+                                     parse=(grass.parse_key_val,
+                                            {'sep': '='}))
+        stats3 = grass.parse_command("r.univar", map=ms3, flags='g',
+                                     parse=(grass.parse_key_val,
+                                            {'sep': '='}))
+
         b1mean = float(stats1['mean'])
         b2mean = float(stats2['mean'])
         b3mean = float(stats3['mean'])
 
-
-        if sproc: 
+        if sproc:
             # serial processing
             e = '''eval(k = "$ms1" + "$ms2" + "$ms3")
                 "$outr" = 1.0 * "$ms3" * "$panmatch3" / k
@@ -269,47 +275,51 @@
             cmd1 = "$outb = (1.0 * $panmatch * $b1evect1) + ($pca2 * $b2evect1) + ($pca3 * $b3evect1) + $b1mean"
             cmd2 = "$outg = (1.0 * $panmatch * $b1evect2) + ($pca2 * $b2evect1) + ($pca3 * $b3evect2) + $b2mean"
             cmd3 = "$outr = (1.0 * $panmatch * $b1evect3) + ($pca2 * $b2evect3) + ($pca3 * $b3evect3) + $b3mean"
-            
-            cmd =  '\n'.join([cmd1, cmd2, cmd3])
-            
+
+            cmd = '\n'.join([cmd1, cmd2, cmd3])
+
             grass.mapcalc(cmd, outb=outb, outg=outg, outr=outr,
-                          panmatch=panmatch, pca2=pca2, pca3=pca3, 
-                          b1evect1=b1evect1, b2evect1=b2evect1, b3evect1=b3evect1, 
-                          b1evect2=b1evect2, b2evect2=b2evect2, b3evect2=b3evect2, 
-                          b1evect3=b1evect3, b2evect3=b2evect3, b3evect3=b3evect3, 
-                          b1mean=b1mean, b2mean=b2mean, b3mean=b3mean, 
+                          panmatch=panmatch, pca2=pca2, pca3=pca3,
+                          b1evect1=b1evect1, b2evect1=b2evect1, b3evect1=b3evect1,
+                          b1evect2=b1evect2, b2evect2=b2evect2, b3evect2=b3evect2,
+                          b1evect3=b1evect3, b2evect3=b2evect3, b3evect3=b3evect3,
+                          b1mean=b1mean, b2mean=b2mean, b3mean=b3mean,
                           overwrite=True)
         else:
             # parallel processing
-            pb = grass.mapcalc_start('%s_blue = (%s * %f) + (%s * %f) + (%s * %f) + %f' 
-                          % (out, panmatch, b1evect1, pca2, b2evect1, pca3, b3evect1, b1mean), 
-                          overwrite=True)
-            
-            pg = grass.mapcalc_start('%s_green = (%s * %f) + (%s * %f) + (%s * %f) + %f' 
-                          % (out, panmatch, b1evect2, pca2, b2evect2, pca3, b3evect2, b2mean), 
-                          overwrite=True)
-            
-            pr = grass.mapcalc_start('%s_red = (%s * %f) + (%s * %f) + (%s * %f) + %f' 
-                          % (out, panmatch, b1evect3, pca2, b2evect3, pca3, b3evect3, b3mean), 
-                          overwrite=True)
-            
+            pb = grass.mapcalc_start('%s_blue = (%s * %f) + (%s * %f) + (%s * %f) + %f'
+                                     % (out, panmatch, b1evect1, pca2,
+                                        b2evect1, pca3, b3evect1, b1mean),
+                                     overwrite=True)
+
+            pg = grass.mapcalc_start('%s_green = (%s * %f) + (%s * %f) + (%s * %f) + %f'
+                                     % (out, panmatch, b1evect2, pca2,
+                                        b2evect2, pca3, b3evect2, b2mean),
+                                     overwrite=True)
+
+            pr = grass.mapcalc_start('%s_red = (%s * %f) + (%s * %f) + (%s * ''%f) + %f'
+                                     % (out, panmatch, b1evect3, pca2,
+                                        b2evect3, pca3, b3evect3, b3mean),
+                                     overwrite=True)
+
             pr.wait()
             pg.wait()
             pb.wait()
 
-        
         # Cleanup
-        grass.run_command('g.remove', flags='f', quiet=True, type="rast", pattern='tmp%s*,%s' % (pid,panmatch))
-        
-    #Could add other sharpening algorithms here, e.g. wavelet transformation
+        grass.run_command('g.remove', flags='f', quiet=True, type="rast",
+                          pattern='tmp%s*,%s' % (pid, panmatch))
 
+    # Could add other sharpening algorithms here, e.g. wavelet transformation
+
     grass.message(_("Assigning grey equalized color tables to output images..."))
-    #equalized grey scales give best contrast
+    # equalized grey scales give best contrast
     for ch in ['red', 'green', 'blue']:
-        grass.run_command('r.colors', quiet=True, map = "%s_%s" % (out, ch), flags="e", col = 'grey')
+        grass.run_command('r.colors', quiet=True, map="%s_%s" % (out, ch),
+                          flags="e", col='grey')
 
-    #Landsat too blue-ish because panchromatic band less sensitive to blue light, 
-    #  so output blue channed can be modified
+    # Landsat too blue-ish because panchromatic band less sensitive to blue
+    # light, so output blue channed can be modified
     if bladjust:
         grass.message(_("Adjusting blue channel color table..."))
         rules = grass.tempfile()
@@ -320,7 +330,7 @@
         grass.run_command('r.colors', map="%s_blue" % out, rules=rules)
         os.remove(rules)
 
-    #output notice
+    # output notice
     grass.verbose(_("The following pan-sharpened output maps have been generated:"))
     for ch in ['red', 'green', 'blue']:
         grass.verbose(_("%s_%s") % (out, ch))
@@ -334,97 +344,105 @@
     for ch in ['red', 'green', 'blue']:
         grass.raster_history("%s_%s" % (out, ch))
 
-    # Cleanup        
-    grass.run_command('g.remove', flags="f", type="rast", pattern="tmp%s*" % pid, quiet=True)
+    # create a group with the three output
+    grass.run_command('i.group', group=out,
+                      input="{n}_red,{n}_blue,{n}_green".format(n=out))
 
-        
+    # Cleanup
+    grass.run_command('g.remove', flags="f", type="rast",
+                      pattern="tmp%s*" % pid, quiet=True)
+
+
 def matchhist(original, target, matched):
-    #pan/intensity histogram matching using numpy arrays
+    # pan/intensity histogram matching using numpy arrays
     grass.message(_("Histogram matching..."))
 
     # input images
     original = original.split('@')[0]
     target = target.split('@')[0]
     images = [original, target]
-            
-    # create a dictionary to hold arrays for each image            
+
+    # create a dictionary to hold arrays for each image
     arrays = {}
-    
+
     for i in images:
         # calculate number of cells for each grey value for for each image
-        stats_out = grass.pipe_command('r.stats', flags='cin', input= i, sep=':')
-        stats =  stats_out.communicate()[0].split('\n')[:-1]
-        stats_dict = dict( s.split(':', 1) for s in stats)
-        total_cells = 0 # total non-null cells
+        stats_out = grass.pipe_command('r.stats', flags='cin', input=i,
+                                       sep=':')
+        stats = stats_out.communicate()[0].split('\n')[:-1]
+        stats_dict = dict(s.split(':', 1) for s in stats)
+        total_cells = 0  # total non-null cells
         for j in stats_dict:
             stats_dict[j] = int(stats_dict[j])
             if j != '*':
-                total_cells += stats_dict[j]        
- 
+                total_cells += stats_dict[j]
+
         if total_cells < 1:
             grass.fatal(_("Input has no data. Check region settings."))
 
-        # Make a 2x256 structured array for each image with a 
+        # Make a 2x256 structured array for each image with a
         #   cumulative distribution function (CDF) for each grey value.
         #   Grey value is the integer (i4) and cdf is float (f4).
 
-        arrays[i] = np.zeros((256,),dtype=('i4,f4'))
-        cum_cells = 0 # cumulative total of cells for sum of current and all lower grey values
-        
-        for n in range(0,256):
+        arrays[i] = np.zeros((256, ), dtype=('i4,f4'))
+        cum_cells = 0  # cumulative total of cells for sum of current and all lower grey values
+
+        for n in range(0, 256):
             if str(n) in stats_dict:
                 num_cells = stats_dict[str(n)]
             else:
                 num_cells = 0
-                
-            cum_cells += num_cells  
-            
+
+            cum_cells += num_cells
+
             # cdf is the the number of cells at or below a given grey value
-            #   divided by the total number of cells              
+            #   divided by the total number of cells
             cdf = float(cum_cells) / float(total_cells)
-            
+
             # insert values into array
-            arrays[i][n] = (n, cdf) 
-    
+            arrays[i][n] = (n, cdf)
+
     # open file for reclass rules
     outfile = open(grass.tempfile(), 'w')
-    new_grey = 0
-        
+
     for i in arrays[original]:
-        # for each grey value and corresponding cdf value in original, find the  
-        #   cdf value in target that is closest to the target cdf value 
+        # for each grey value and corresponding cdf value in original, find the
+        #   cdf value in target that is closest to the target cdf value
         difference_list = []
         for j in arrays[target]:
             # make a list of the difference between each original cdf value and
             #   the target cdf value
             difference_list.append(abs(i[1] - j[1]))
-            
+
         # get the smallest difference in the list
         min_difference = min(difference_list)
 
         for j in arrays[target]:
-            # find the grey value in target that correspondes to the cdf 
+            # find the grey value in target that correspondes to the cdf
             #   closest to the original cdf
             if j[1] == i[1] + min_difference or j[1] == i[1] - min_difference:
                 # build a reclass rules file from the original grey value and
                 #   corresponding grey value from target
                 out_line = "%d = %d\n" % (i[0], j[0])
-                outfile.write(out_line)     
+                outfile.write(out_line)
                 break
-                         
-    outfile.close() 
-    
+
+    outfile.close()
+
     # create reclass of target from reclass rules file
-    result = grass.core.find_file(matched, element = 'cell')
+    result = grass.core.find_file(matched, element='cell')
     if result['fullname']:
-        grass.run_command('g.remove', quiet=True, flags='f', type='rast', name=matched)
-        grass.run_command('r.reclass', input=original, out=matched, rules=outfile.name)
+        grass.run_command('g.remove', flags='f', quiet=True, type='rast',
+                          name=matched)
+        grass.run_command('r.reclass', input=original, out=matched,
+                          rules=outfile.name)
     else:
-        grass.run_command('r.reclass', input=original, out=matched, rules=outfile.name)
+        grass.run_command('r.reclass', input=original, out=matched,
+                          rules=outfile.name)
 
     # Cleanup
     # remove the rules file
-    grass.try_remove(outfile.name)  
+    grass.try_remove(outfile.name)
 
     # return reclass of target with histogram that matches original
     return matched



More information about the grass-commit mailing list