[GRASS-SVN] r48903 - grass/trunk/lib/python/temporal
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Oct 21 14:33:50 EDT 2011
Author: huhabla
Date: 2011-10-21 11:33:50 -0700 (Fri, 21 Oct 2011)
New Revision: 48903
Added:
grass/trunk/lib/python/temporal/aggregation.py
Modified:
grass/trunk/lib/python/temporal/Makefile
grass/trunk/lib/python/temporal/__init__.py
grass/trunk/lib/python/temporal/space_time_datasets_tools.py
Log:
Moved code from list and aggregation modules into the library.
Modified: grass/trunk/lib/python/temporal/Makefile
===================================================================
--- grass/trunk/lib/python/temporal/Makefile 2011-10-21 11:55:26 UTC (rev 48902)
+++ grass/trunk/lib/python/temporal/Makefile 2011-10-21 18:33:50 UTC (rev 48903)
@@ -8,7 +8,7 @@
GDIR = $(PYDIR)/grass
DSTDIR = $(GDIR)/temporal
-MODULES = base core abstract_dataset abstract_map_dataset abstract_space_time_dataset space_time_datasets space_time_datasets_tools metadata spatial_extent temporal_extent datetime_math temporal_granularity unit_tests
+MODULES = base core abstract_dataset abstract_map_dataset abstract_space_time_dataset space_time_datasets space_time_datasets_tools metadata spatial_extent temporal_extent datetime_math temporal_granularity unit_tests aggregation
PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)
Modified: grass/trunk/lib/python/temporal/__init__.py
===================================================================
--- grass/trunk/lib/python/temporal/__init__.py 2011-10-21 11:55:26 UTC (rev 48902)
+++ grass/trunk/lib/python/temporal/__init__.py 2011-10-21 18:33:50 UTC (rev 48903)
@@ -11,3 +11,4 @@
from datetime_math import *
from temporal_granularity import *
from unit_tests import *
+from aggregation import *
Added: grass/trunk/lib/python/temporal/aggregation.py
===================================================================
--- grass/trunk/lib/python/temporal/aggregation.py (rev 0)
+++ grass/trunk/lib/python/temporal/aggregation.py 2011-10-21 18:33:50 UTC (rev 48903)
@@ -0,0 +1,100 @@
+"""!@package grass.temporal
+
+ at brief GRASS Python scripting module (temporal GIS functions)
+
+Temporal GIS related functions to be used in Python scripts.
+
+Usage:
+
+ at code
+import grass.temporal as tgis
+
+tgis.aggregate_raster_maps(dataset, mapset, inputs, base, start, end, count, method, register_null, dbif)
+
+...
+ at endcode
+
+(C) 2008-2011 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+ at author Soeren Gebbert
+"""
+
+from space_time_datasets import *
+
+def collect_map_names(sp, dbif, start, end):
+
+ where = " start_time >= \'%s\' and start_time < \'%s\'" % (start, end)
+
+ print where
+
+ rows = sp.get_registered_maps("id", where, "start_time", dbif)
+
+ if not rows:
+ return None
+
+ names = []
+ for row in rows:
+ names.append(row["id"])
+
+ return names
+
+
+def aggregate_raster_maps(dataset, mapset, inputs, base, start, end, count, method, register_null, dbif):
+
+ core.verbose(_("Aggregate %s raster maps") %(len(inputs)))
+ output = "%s_%i" % (base, count)
+
+ map_id = output + "@" + mapset
+
+ new_map = dataset.get_new_map_instance(map_id)
+
+ # Check if new map is in the temporal database
+ if new_map.is_in_db(dbif):
+ if core.overwrite() == True:
+ # Remove the existing temporal database entry
+ new_map.delete(dbif)
+ new_map = dataset.get_new_map_instance(map_id)
+ else:
+ core.error(_("Raster map <%s> is already in temporal database, use overwrite flag to overwrite"))
+ return
+
+ core.verbose(_("Compute aggregation of maps between %s - %s" % (str(start), str(end))))
+
+ # Create the r.series input file
+ filename = core.tempfile(True)
+ file = open(filename, 'w')
+
+ for name in inputs:
+ string = "%s\n" % (name)
+ file.write(string)
+
+ file.close()
+ # Run r.series
+ ret = core.run_command("r.series", flags="z", file=filename, output=output, overwrite=core.overwrite(), method=method)
+
+ if ret != 0:
+ dbif.close()
+ core.fatal(_("Error while r.series computation"))
+
+ # Read the raster map data
+ new_map.load()
+
+ # In case of a null map continue, do not register null maps
+ if new_map.metadata.get_min() == None and new_map.metadata.get_max() == None:
+ if not register_null:
+ core.run_command("g.remove", rast=output)
+ return
+
+ # Set the time stamp
+ if dataset.is_time_absolute():
+ new_map.set_absolute_time(start, end, None)
+ else:
+ new_map.set_relative_time(start, end)
+
+ # Insert map in temporal database
+ new_map.insert(dbif)
+
+ dataset.register_map(new_map, dbif)
Modified: grass/trunk/lib/python/temporal/space_time_datasets_tools.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets_tools.py 2011-10-21 11:55:26 UTC (rev 48902)
+++ grass/trunk/lib/python/temporal/space_time_datasets_tools.py 2011-10-21 18:33:50 UTC (rev 48903)
@@ -91,7 +91,7 @@
if sp.is_in_db(dbif) == False:
dbif.close()
- core.fatal(_("Space time %s dataset <%s> no found") % (sp.get_new_map_instance(None).get_type(). name))
+ core.fatal(_("Space time %s dataset <%s> no found") % (sp.get_new_map_instance(None).get_type(), name))
maplist = []
@@ -541,3 +541,128 @@
return sp
+###############################################################################
+
+def list_maps_of_stds(type, input, columns, order, where, separator, method, header):
+ """ List the maps of a space time dataset using diffetent methods
+
+ @param type: The type of the maps raster, raster3d or vector
+ @param input: Name of a space time raster dataset
+ @param columns: A comma separated list of columns to be printed to stdout
+ @param order: A comma seoarated list of columns to order the space time dataset by category
+ @param where: A where statement for selected listing without "WHERE" e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
+ @param separator: The field separator character between the columns
+ @param method: String identifier to select a method out of cols,comma,delta or deltagaps
+ * "cols": Print preselected columns specified by columns
+ * "comma": Print the map ids (name at mapset) as comma separated string
+ * "delta": Print the map ids (name at mapset) with start time, end time, relative length of intervals and the relative distance to the begin
+ * "deltagaps": Same as "delta" with addtitionakl listing of gaps. Gaps can be simply identified as the id is "None"
+ @param header: Set True to print column names
+ """
+ mapset = core.gisenv()["MAPSET"]
+
+ if input.find("@") >= 0:
+ id = input
+ else:
+ id = input + "@" + mapset
+
+ sp = dataset_factory(type, id)
+
+ if sp.is_in_db() == False:
+ core.fatal(_("Dataset <%s> not found in temporal database") % (id))
+
+ sp.select()
+
+ if separator == None or separator == "":
+ separator = "\t"
+
+ # This method expects a list of objects for gap detection
+ if method == "delta" or method == "deltagaps":
+ columns = "id,start_time,end_time"
+ if method == "deltagaps":
+ maps = sp.get_registered_maps_as_objects_with_gaps(where, None)
+ else:
+ maps = sp.get_registered_maps_as_objects(where, "start_time", None)
+
+ if header:
+ string = ""
+ string += "%s%s" % ("id", separator)
+ string += "%s%s" % ("start_time", separator)
+ string += "%s%s" % ("end_time", separator)
+ string += "%s%s" % ("interval_length", separator)
+ string += "%s" % ("distance_from_begin")
+ print string
+
+ if maps and len(maps) > 0:
+
+ first_time, dummy = maps[0].get_valid_time()
+
+ for map in maps:
+ start, end = map.get_valid_time()
+ if end:
+ delta = end -start
+ else:
+ delta = None
+ delta_first = start - first_time
+
+ if map.is_time_absolute():
+ if end:
+ delta = time_delta_to_relative_time(delta)
+ delta_first = time_delta_to_relative_time(delta_first)
+
+ string = ""
+ string += "%s%s" % (map.get_id(), separator)
+ string += "%s%s" % (start, separator)
+ string += "%s%s" % (end, separator)
+ string += "%s%s" % (delta, separator)
+ string += "%s" % (delta_first)
+ print string
+
+ else:
+ # In comma separated mode only map ids are needed
+ if method == "comma":
+ columns = "id"
+
+ rows = sp.get_registered_maps(columns, where, order, None)
+
+ if rows:
+ if method == "comma":
+ string = ""
+ count = 0
+ for row in rows:
+ if count == 0:
+ string += row["id"]
+ else:
+ string += ",%s" % row["id"]
+ count += 1
+ print string
+
+ elif method == "cols":
+ # Print the column names if requested
+ if header:
+ output = ""
+ count = 0
+
+ collist = columns.split(",")
+
+ for key in collist:
+ if count > 0:
+ output += separator + str(key)
+ else:
+ output += str(key)
+ count += 1
+ print output
+
+ for row in rows:
+ output = ""
+ count = 0
+ for col in row:
+ if count > 0:
+ output += separator + str(col)
+ else:
+ output += str(col)
+ count += 1
+
+ print output
+
+
More information about the grass-commit
mailing list