Sfoglia il codice sorgente

temporal: better formatting of suffixes of new maps - include padding zeros

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61888 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 10 anni fa
parent
commit
77d93163cc

+ 17 - 0
lib/python/script/utils.py

@@ -218,3 +218,20 @@ def encode(string):
         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)

+ 2 - 1
lib/python/temporal/aggregation.py

@@ -275,7 +275,8 @@ def aggregate_by_topology(granularity_list,  granularity,  map_list,  topo_list,
                 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,

+ 3 - 2
lib/python/temporal/extract.py

@@ -12,7 +12,7 @@ for details.
 @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 @@ def extract_dataset(input, output, type, where, expression, base, nprocs=1,
                 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":

+ 2 - 1
lib/python/temporal/mapcalc.py

@@ -201,7 +201,8 @@ def dataset_mapcalculator(inputs, output, type, expression, base, method,
                 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(" ", "")
 

+ 8 - 1
lib/python/temporal/stds_import.py

@@ -288,6 +288,13 @@ def import_stds(input, output, extrdir, title=None, descr=None, location=None,
         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 @@ def import_stds(input, output, extrdir, title=None, descr=None, location=None,
             # 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