[GRASS-SVN] r61926 - in grass/branches/releasebranch_7_0: . lib/python/script lib/python/temporal
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Sep 13 16:13:56 PDT 2014
Author: annakrat
Date: 2014-09-13 16:13:56 -0700 (Sat, 13 Sep 2014)
New Revision: 61926
Modified:
grass/branches/releasebranch_7_0/
grass/branches/releasebranch_7_0/lib/python/script/utils.py
grass/branches/releasebranch_7_0/lib/python/temporal/extract.py
grass/branches/releasebranch_7_0/lib/python/temporal/mapcalc.py
grass/branches/releasebranch_7_0/lib/python/temporal/stds_import.py
Log:
temporal: better formatting of suffixes of new maps - include padding zeros (merge from trunk, r61888)
Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
- /grass/trunk:60289,60696,61269,61380,61420,61422,61480,61500,61764,61808,61831,61854,61891,61905,61907,61913-61914,61916,61918,61921
+ /grass/trunk:60289,60696,61269,61380,61420,61422,61480,61500,61764,61808,61831,61854,61888,61891,61905,61907,61913-61914,61916,61918,61921
Modified: grass/branches/releasebranch_7_0/lib/python/script/utils.py
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/script/utils.py 2014-09-13 22:54:48 UTC (rev 61925)
+++ grass/branches/releasebranch_7_0/lib/python/script/utils.py 2014-09-13 23:13:56 UTC (rev 61926)
@@ -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/branches/releasebranch_7_0/lib/python/temporal/extract.py
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/temporal/extract.py 2014-09-13 22:54:48 UTC (rev 61925)
+++ grass/branches/releasebranch_7_0/lib/python/temporal/extract.py 2014-09-13 23:13:56 UTC (rev 61926)
@@ -12,9 +12,11 @@
@author Soeren Gebbert
"""
+from grass.script.utils import get_num_suffix
from space_time_datasets import *
from open_stds import *
from multiprocessing import Process
+import grass.script as gscript
############################################################################
@@ -61,7 +63,7 @@
sp = open_old_space_time_dataset(input, type, dbif)
# Check the new stds
new_sp = check_new_space_time_dataset(output, type, dbif,
- core.overwrite())
+ gscript.overwrite())
if type == "vector":
rows = sp.get_registered_maps(
"id,name,mapset,layer", where, "start_time", dbif)
@@ -86,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":
@@ -104,7 +107,7 @@
# Check if new map is in the temporal database
if new_map.is_in_db(dbif):
- if core.overwrite():
+ if gscript.overwrite():
# Remove the existing temporal database entry
new_map.delete(dbif)
new_map = sp.get_new_map_instance(map_id)
@@ -169,7 +172,7 @@
sp.get_temporal_type(),
title, description,
semantic_type, dbif,
- core.overwrite())
+ gscript.overwrite())
# collect empty maps to remove them
empty_maps = []
@@ -234,11 +237,11 @@
names += ",%s" % (map.get_name())
count += 1
if type == "raster":
- core.run_command("g.remove", rast=names, quiet=True)
+ gscript.run_command("g.remove", rast=names, quiet=True)
elif type == "raster3d":
- core.run_command("g.remove", rast3d=names, quiet=True)
+ gscript.run_command("g.remove", rast3d=names, quiet=True)
elif type == "vector":
- core.run_command("g.remove", vect=names, quiet=True)
+ gscript.run_command("g.remove", vect=names, quiet=True)
dbif.close()
@@ -247,18 +250,18 @@
def run_mapcalc2d(expr):
"""Helper function to run r.mapcalc in parallel"""
- exit(core.run_command("r.mapcalc", expression=expr,
- overwrite=core.overwrite(), quiet=True))
+ exit(gscript.run_command("r.mapcalc", expression=expr,
+ overwrite=gscript.overwrite(), quiet=True))
def run_mapcalc3d(expr):
"""Helper function to run r3.mapcalc in parallel"""
- exit(core.run_command("r3.mapcalc", expression=expr,
- overwrite=core.overwrite(), quiet=True))
+ exit(gscript.run_command("r3.mapcalc", expression=expr,
+ overwrite=gscript.overwrite(), quiet=True))
def run_vector_extraction(input, output, layer, type, where):
"""Helper function to run r.mapcalc in parallel"""
- exit(core.run_command("v.extract", input=input, output=output,
+ exit(gscript.run_command("v.extract", input=input, output=output,
layer=layer, type=type, where=where,
- overwrite=core.overwrite(), quiet=True))
+ overwrite=gscript.overwrite(), quiet=True))
Modified: grass/branches/releasebranch_7_0/lib/python/temporal/mapcalc.py
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/temporal/mapcalc.py 2014-09-13 22:54:48 UTC (rev 61925)
+++ grass/branches/releasebranch_7_0/lib/python/temporal/mapcalc.py 2014-09-13 23:13:56 UTC (rev 61926)
@@ -15,6 +15,7 @@
from space_time_datasets import *
from open_stds import *
from multiprocessing import Process
+import grass.script as gscript
############################################################################
@@ -98,7 +99,7 @@
input_list.append(copy.copy(sp))
new_sp = check_new_space_time_dataset(output, type, dbif,
- core.overwrite())
+ gscript.overwrite())
# Sample all inputs by the first input and create a sample matrix
if spatial:
@@ -200,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(" ", "")
@@ -227,7 +229,7 @@
# Check if new map is in the temporal database
if new_map.is_in_db(dbif):
- if core.overwrite():
+ if gscript.overwrite():
# Remove the existing temporal database entry
new_map.delete(dbif)
new_map = first_input.get_new_map_instance(map_id)
@@ -284,7 +286,7 @@
new_sp = open_new_space_time_dataset(output, type,
temporal_type, title, description,
semantic_type, dbif,
- core.overwrite())
+ gscript.overwrite())
count = 0
# collect empty maps to remove them
@@ -329,9 +331,9 @@
names += ",%s" % (map.get_name())
count += 1
if type == "raster":
- core.run_command("g.remove", rast=names, quiet=True)
+ gscript.run_command("g.remove", rast=names, quiet=True)
elif type == "raster3d":
- core.run_command("g.remove", rast3d=names, quiet=True)
+ gscript.run_command("g.remove", rast3d=names, quiet=True)
dbif.close()
@@ -340,16 +342,16 @@
def _run_mapcalc2d(expr):
"""Helper function to run r.mapcalc in parallel"""
- exit(core.run_command("r.mapcalc", expression=expr,
- overwrite=core.overwrite(), quiet=True))
+ exit(gscript.run_command("r.mapcalc", expression=expr,
+ overwrite=gscript.overwrite(), quiet=True))
###############################################################################
def _run_mapcalc3d(expr):
"""Helper function to run r3.mapcalc in parallel"""
- exit(core.run_command("r3.mapcalc", expression=expr,
- overwrite=core.overwrite(), quiet=True))
+ exit(gscript.run_command("r3.mapcalc", expression=expr,
+ overwrite=gscript.overwrite(), quiet=True))
###############################################################################
Modified: grass/branches/releasebranch_7_0/lib/python/temporal/stds_import.py
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/temporal/stds_import.py 2014-09-13 22:54:48 UTC (rev 61925)
+++ grass/branches/releasebranch_7_0/lib/python/temporal/stds_import.py 2014-09-13 23:13:56 UTC (rev 61926)
@@ -40,6 +40,7 @@
from register import *
import factory
from factory import *
+import grass.script as gscript
proj_file_name = "proj.txt"
init_file_name = "init.txt"
@@ -70,33 +71,33 @@
impflags += "o"
if link:
- ret = core.run_command("r.external", input=filename,
+ ret = gscript.run_command("r.external", input=filename,
output=name,
flags=impflags,
- overwrite=core.overwrite())
+ overwrite=gscript.overwrite())
else:
- ret = core.run_command("r.in.gdal", input=filename,
+ ret = gscript.run_command("r.in.gdal", input=filename,
output=name,
flags=impflags,
- overwrite=core.overwrite())
+ overwrite=gscript.overwrite())
if ret != 0:
- core.fatal(_("Unable to import/link raster map <%s> from file %s.") %(name,
+ gscript.fatal(_("Unable to import/link raster map <%s> from file %s.") %(name,
filename))
# Set the color rules if present
filename = row["filename"] + ".color"
if os.path.isfile(filename):
- ret = core.run_command("r.colors", map=name,
+ ret = gscript.run_command("r.colors", map=name,
rules=filename,
- overwrite=core.overwrite())
+ overwrite=gscript.overwrite())
if ret != 0:
- core.fatal(_("Unable to set the color rules for "
+ gscript.fatal(_("Unable to set the color rules for "
"raster map <%s>.") % name)
# Set the computational region from the last map imported
if set_current_region is True:
- core.run_command("g.region", rast=name)
+ gscript.run_command("g.region", rast=name)
############################################################################
@@ -108,19 +109,19 @@
for row in maplist:
name = row["name"]
filename = row["filename"] + ".pack"
- ret = core.run_command("r.unpack", input=filename,
+ ret = gscript.run_command("r.unpack", input=filename,
output=name,
flags=impflags,
- overwrite=core.overwrite(),
+ overwrite=gscript.overwrite(),
verbose=True)
if ret != 0:
- core.fatal(_("Unable to unpack raster map <%s> from file %s.") % (name,
+ gscript.fatal(_("Unable to unpack raster map <%s> from file %s.") % (name,
filename))
# Set the computational region from the last map imported
if set_current_region is True:
- core.run_command("g.region", rast=name)
+ gscript.run_command("g.region", rast=name)
############################################################################
@@ -133,13 +134,13 @@
name = row["name"]
filename = row["filename"] + ".xml"
- ret = core.run_command("v.in.ogr", dsn=filename,
+ ret = gscript.run_command("v.in.ogr", dsn=filename,
output=name,
flags=impflags,
- overwrite=core.overwrite())
+ overwrite=gscript.overwrite())
if ret != 0:
- core.fatal(_("Unable to import vector map <%s> from file %s.") % (name,
+ gscript.fatal(_("Unable to import vector map <%s> from file %s.") % (name,
filename))
############################################################################
@@ -156,14 +157,14 @@
if name in imported_maps:
continue
filename = row["filename"] + ".pack"
- ret = core.run_command("v.unpack", input=filename,
+ ret = gscript.run_command("v.unpack", input=filename,
output=name,
flags=impflags,
- overwrite=core.overwrite(),
+ overwrite=gscript.overwrite(),
verbose=True)
if ret != 0:
- core.fatal(_("Unable to unpack vector map <%s> from file %s.") % (name,
+ gscript.fatal(_("Unable to unpack vector map <%s> from file %s.") % (name,
filename))
imported_maps[name] = name
@@ -196,15 +197,15 @@
"""
global raise_on_error
- old_state = core.raise_on_error
- core.set_raise_on_error(True)
+ old_state = gscript.raise_on_error
+ gscript.set_raise_on_error(True)
# Check if input file and extraction directory exits
if not os.path.exists(input):
- core.fatal(_("Space time raster dataset archive <%s> not found")
+ gscript.fatal(_("Space time raster dataset archive <%s> not found")
% input)
if not create and not os.path.exists(extrdir):
- core.fatal(_("Extraction directory <%s> not found") % extrdir)
+ gscript.fatal(_("Extraction directory <%s> not found") % extrdir)
tar = tarfile.open(name=input, mode='r')
@@ -212,11 +213,11 @@
members = tar.getnames()
if init_file_name not in members:
- core.fatal(_("Unable to find init file <%s>") % init_file_name)
+ gscript.fatal(_("Unable to find init file <%s>") % init_file_name)
if list_file_name not in members:
- core.fatal(_("Unable to find list file <%s>") % list_file_name)
+ gscript.fatal(_("Unable to find list file <%s>") % list_file_name)
if proj_file_name not in members:
- core.fatal(_("Unable to find projection file <%s>") % proj_file_name)
+ gscript.fatal(_("Unable to find projection file <%s>") % proj_file_name)
tar.extractall(path=extrdir)
tar.close()
@@ -231,31 +232,31 @@
# Check projection information
if not location:
- temp_name = core.tempfile()
+ temp_name = gscript.tempfile()
temp_file = open(temp_name, "w")
proj_name = os.path.abspath(proj_file_name)
- p = core.start_command("g.proj", flags="j", stdout=temp_file)
+ p = gscript.start_command("g.proj", flags="j", stdout=temp_file)
p.communicate()
temp_file.close()
- if not core.compare_key_value_text_files(temp_name, proj_name, sep="="):
+ if not gscript.compare_key_value_text_files(temp_name, proj_name, sep="="):
if overr:
- core.warning(_("Projection information does not match. "
+ gscript.warning(_("Projection information does not match. "
"Proceeding..."))
else:
- diff = ''.join(core.diff_files(temp_name, proj_name))
- core.warning(_("Difference between PROJ_INFO file of imported map "
+ diff = ''.join(gscript.diff_files(temp_name, proj_name))
+ gscript.warning(_("Difference between PROJ_INFO file of imported map "
"and of current location:\n{diff}").format(diff=diff))
- core.fatal(_("Projection information does not match. Aborting."))
+ gscript.fatal(_("Projection information does not match. Aborting."))
# Create a new location based on the projection information and switch
# into it
- old_env = core.gisenv()
+ old_env = gscript.gisenv()
if location:
try:
proj4_string = open(proj_file_name, 'r').read()
- core.create_location(dbase=old_env["GISDBASE"],
+ gscript.create_location(dbase=old_env["GISDBASE"],
location=location,
proj4=proj4_string)
# Just create a new location and return
@@ -263,18 +264,18 @@
os.chdir(old_cwd)
return
except Exception as e:
- core.fatal(_("Unable to create location %(l)s. Reason: %(e)s")
+ gscript.fatal(_("Unable to create location %(l)s. Reason: %(e)s")
% {'l': location, 'e': str(e)})
# Switch to the new created location
- ret = core.run_command("g.mapset", mapset="PERMANENT",
+ ret = gscript.run_command("g.mapset", mapset="PERMANENT",
location=location,
gisdbase=old_env["GISDBASE"])
if ret != 0:
- core.fatal(_("Unable to switch to location %s") % location)
+ gscript.fatal(_("Unable to switch to location %s") % location)
# create default database connection
- ret = core.run_command("t.connect", flags="d")
+ ret = gscript.run_command("t.connect", flags="d")
if ret != 0:
- core.fatal(_("Unable to create default temporal database "
+ gscript.fatal(_("Unable to create default temporal database "
"in new location %s") % location)
try:
@@ -287,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:
@@ -300,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
@@ -339,13 +347,13 @@
if "temporal_type" not in init or \
"semantic_type" not in init or \
"number_of_maps" not in init:
- core.fatal(_("Key words %(t)s, %(s)s or %(n)s not found in init"
+ gscript.fatal(_("Key words %(t)s, %(s)s or %(n)s not found in init"
" file.") % {'t': "temporal_type",
's': "semantic_type",
'n': "number_of_maps"})
if line_count != int(init["number_of_maps"]):
- core.fatal(_("Number of maps mismatch in init and list file."))
+ gscript.fatal(_("Number of maps mismatch in init and list file."))
format_ = "GTiff"
type_ = "strds"
@@ -356,26 +364,26 @@
format_ = init["format"]
if stds_type != type_:
- core.fatal(_("The archive file is of wrong space time dataset type"))
+ gscript.fatal(_("The archive file is of wrong space time dataset type"))
# Check the existence of the files
if format_ == "GTiff":
for row in maplist:
filename = row["filename"] + ".tif"
if not os.path.exists(filename):
- core.fatal(_("Unable to find GeoTIFF raster file "
+ gscript.fatal(_("Unable to find GeoTIFF raster file "
"<%s> in archive.") % filename)
elif format_ == "AAIGrid":
for row in maplist:
filename = row["filename"] + ".asc"
if not os.path.exists(filename):
- core.fatal(_("Unable to find AAIGrid raster file "
+ gscript.fatal(_("Unable to find AAIGrid raster file "
"<%s> in archive.") % filename)
elif format_ == "GML":
for row in maplist:
filename = row["filename"] + ".xml"
if not os.path.exists(filename):
- core.fatal(_("Unable to find GML vector file "
+ gscript.fatal(_("Unable to find GML vector file "
"<%s> in archive.") % filename)
elif format_ == "pack":
for row in maplist:
@@ -384,16 +392,16 @@
else:
filename = row["filename"] + ".pack"
if not os.path.exists(filename):
- core.fatal(_("Unable to find GRASS package file "
+ gscript.fatal(_("Unable to find GRASS package file "
"<%s> in archive.") % filename)
else:
- core.fatal(_("Unsupported input format"))
+ gscript.fatal(_("Unsupported input format"))
# Check the space time dataset
id = output + "@" + mapset
sp = dataset_factory(type_, id)
- if sp.is_in_db() and core.overwrite() == False:
- core.fatal(_("Space time %(t)s dataset <%(sp)s> is already in the "
+ if sp.is_in_db() and gscript.overwrite() == False:
+ gscript.fatal(_("Space time %(t)s dataset <%(sp)s> is already in the "
"database. Use the overwrite flag.") % {'t': type_,
'sp': sp.get_id()})
@@ -412,8 +420,8 @@
_import_vector_maps(maplist)
# Create the space time dataset
- if sp.is_in_db() and core.overwrite() == True:
- core.info(_("Overwrite space time %(sp)s dataset "
+ if sp.is_in_db() and gscript.overwrite() == True:
+ gscript.info(_("Overwrite space time %(sp)s dataset "
"<%(id)s> and unregister all maps.") % {
'sp': sp.get_new_map_instance(None).get_type(),
'id': sp.get_id()})
@@ -425,11 +433,11 @@
relative_time_unit = None
if temporal_type == "relative":
if "relative_time_unit" not in init:
- core.fatal(_("Key word %s not found in init file.") % ("relative_time_unit"))
+ gscript.fatal(_("Key word %s not found in init file.") % ("relative_time_unit"))
relative_time_unit = init["relative_time_unit"]
sp.set_relative_time_unit(relative_time_unit)
- core.verbose(_("Create space time %s dataset.") %
+ gscript.verbose(_("Create space time %s dataset.") %
sp.get_new_map_instance(None).get_type())
sp.set_initial_values(temporal_type=temporal_type,
@@ -453,8 +461,8 @@
finally:
if location:
# Switch to the old location
- ret = core.run_command("g.mapset", mapset=old_env["MAPSET"],
+ ret = gscript.run_command("g.mapset", mapset=old_env["MAPSET"],
location=old_env["LOCATION_NAME"],
gisdbase=old_env["GISDBASE"])
- core.set_raise_on_error(old_state)
+ gscript.set_raise_on_error(old_state)
More information about the grass-commit
mailing list