[GRASS-SVN] r61888 - in grass/trunk/lib/python: script temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 12 16:45:28 PDT 2014


Author: annakrat
Date: 2014-09-12 16:45:28 -0700 (Fri, 12 Sep 2014)
New Revision: 61888

Modified:
   grass/trunk/lib/python/script/utils.py
   grass/trunk/lib/python/temporal/aggregation.py
   grass/trunk/lib/python/temporal/extract.py
   grass/trunk/lib/python/temporal/mapcalc.py
   grass/trunk/lib/python/temporal/stds_import.py
Log:
temporal: better formatting of suffixes of new maps - include padding zeros

Modified: grass/trunk/lib/python/script/utils.py
===================================================================
--- grass/trunk/lib/python/script/utils.py	2014-09-12 23:21:57 UTC (rev 61887)
+++ grass/trunk/lib/python/script/utils.py	2014-09-12 23:45:28 UTC (rev 61888)
@@ -218,3 +218,20 @@
         return string.encode(enc)
 
     return string
+
+
+def get_num_suffix(number, max_number):
+    """Returns formatted number with number of padding zeros
+    depending on maximum number, used for creating suffix for data series.
+    Does not include the suffix separator.
+
+    :param number: number to be formated as map suffix
+    :param max_number: maximum number of the series to get number of digits
+
+    >>> get_num_suffix(10, 1000)
+    '0010'
+    >>> get_num_suffix(10, 10)
+    '10'
+    """
+    return '{number:0{width}d}'.format(width=len(str(max_number)),
+                                       number=number)

Modified: grass/trunk/lib/python/temporal/aggregation.py
===================================================================
--- grass/trunk/lib/python/temporal/aggregation.py	2014-09-12 23:21:57 UTC (rev 61887)
+++ grass/trunk/lib/python/temporal/aggregation.py	2014-09-12 23:45:28 UTC (rev 61888)
@@ -275,7 +275,8 @@
                 suffix = create_suffix_from_datetime(granule.temporal_extent.get_start_time(), 
                                                      granularity)
             else:
-                suffix = str(count + int(offset))
+                suffix = gscript.get_num_suffix(count + int(offset),
+                                                len(granularity_list) + int(offset))
             output_name = "%s_%s"%(basename,  suffix)
 
             map_layer = RasterDataset("%s@%s"%(output_name,

Modified: grass/trunk/lib/python/temporal/extract.py
===================================================================
--- grass/trunk/lib/python/temporal/extract.py	2014-09-12 23:21:57 UTC (rev 61887)
+++ grass/trunk/lib/python/temporal/extract.py	2014-09-12 23:45:28 UTC (rev 61888)
@@ -12,7 +12,7 @@
 @author Soeren Gebbert
 """
 
-from grass.script import core
+from grass.script.utils import get_num_suffix
 from space_time_datasets import *
 from open_stds import *
 from multiprocessing import Process
@@ -88,7 +88,8 @@
                 if count % 10 == 0:
                     msgr.percent(count, num_rows, 1)
 
-                map_name = "%s_%i" % (base, count)
+                map_name = "{base}_{suffix}".format(base=base,
+                                                    suffix=get_num_suffix(count, num_rows))
 
                 # We need to modify the r(3).mapcalc expression
                 if type != "vector":

Modified: grass/trunk/lib/python/temporal/mapcalc.py
===================================================================
--- grass/trunk/lib/python/temporal/mapcalc.py	2014-09-12 23:21:57 UTC (rev 61887)
+++ grass/trunk/lib/python/temporal/mapcalc.py	2014-09-12 23:45:28 UTC (rev 61888)
@@ -201,7 +201,8 @@
                 msgr.percent(count, num, 1)
 
             # Create the r.mapcalc statement for the current time step
-            map_name = "%s_%i" % (base, count)
+            map_name = "{base}_{suffix}".format(base=base,
+                                                suffix=gscript.get_num_suffix(count, num))
             # Remove spaces and new lines
             expr = expression.replace(" ", "")
 

Modified: grass/trunk/lib/python/temporal/stds_import.py
===================================================================
--- grass/trunk/lib/python/temporal/stds_import.py	2014-09-12 23:21:57 UTC (rev 61887)
+++ grass/trunk/lib/python/temporal/stds_import.py	2014-09-12 23:45:28 UTC (rev 61888)
@@ -288,6 +288,13 @@
         list_file = open(list_file_name, "r")
         new_list_file = open(new_list_file_name, "w")
 
+        # get number of lines to correctly form the suffix
+        max_count = -1
+        for max_count, l in enumerate(list_file):
+            pass
+        max_count += 1
+        list_file.seek(0)
+
         # Read the map list from file
         line_count = 0
         while True:
@@ -301,7 +308,7 @@
             # that must be extended by the file suffix
             filename = line_list[0].strip().split(":")[0]
             if base:
-                mapname = "%s_%i"%(base, line_count)
+                mapname = "%s_%s" % (base, gscript.get_num_suffix(line_count + 1, max_count))
                 mapid= "%s@%s"%(mapname, mapset)
             else:
                 mapname = filename



More information about the grass-commit mailing list