[GRASS-SVN] r56020 - in grass/trunk: lib/python/temporal temporal temporal/t.merge temporal/t.rename

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Apr 28 10:53:20 PDT 2013


Author: huhabla
Date: 2013-04-28 10:53:19 -0700 (Sun, 28 Apr 2013)
New Revision: 56020

Added:
   grass/trunk/temporal/t.merge/
   grass/trunk/temporal/t.merge/Makefile
   grass/trunk/temporal/t.merge/t.merge.html
   grass/trunk/temporal/t.merge/t.merge.py
   grass/trunk/temporal/t.merge/test.t.merge.sh
Modified:
   grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
   grass/trunk/lib/python/temporal/register.py
   grass/trunk/temporal/Makefile
   grass/trunk/temporal/t.rename/t.rename.py
Log:
New module to merge different space time datasets in a single output dataset.


Modified: grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2013-04-28 15:04:28 UTC (rev 56019)
+++ grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2013-04-28 17:53:19 UTC (rev 56020)
@@ -382,12 +382,11 @@
         return gaps
 
     def print_spatio_temporal_relationships(self, maps=None, spatial=None, dbif=None):
-        """!Print the temporal relation matrix of all registered maps to stdout
+        """!Print the spatio-temporal relationships for each map of the space time dataset
+           or for each map of the optional list of maps
 
-           The temporal relation matrix includes the temporal relations between
-           all registered maps. The relations are strings stored in a list of lists.
-
-           @param maps a ordered by start_time list of map objects
+           @param maps a ordered by start_time list of map objects, if None the registred
+                       maps of the space time dataset are used
            @param spatial This indicates if the spatial topology is created as well:
                           spatial can be None (no spatial topology), "2D" using west, east, 
                           south, north or "3D" using west, east, south, north, bottom, top

Modified: grass/trunk/lib/python/temporal/register.py
===================================================================
--- grass/trunk/lib/python/temporal/register.py	2013-04-28 15:04:28 UTC (rev 56019)
+++ grass/trunk/lib/python/temporal/register.py	2013-04-28 17:53:19 UTC (rev 56020)
@@ -300,7 +300,6 @@
 
     # Finally Register the maps in the space time dataset
     if name and map_object_list:
-        statement = ""
         count = 0
         num_maps = len(map_object_list)
         core.message(_("Register maps in the space time raster dataset"))

Modified: grass/trunk/temporal/Makefile
===================================================================
--- grass/trunk/temporal/Makefile	2013-04-28 15:04:28 UTC (rev 56019)
+++ grass/trunk/temporal/Makefile	2013-04-28 17:53:19 UTC (rev 56020)
@@ -6,6 +6,7 @@
 	t.topology \
 	t.list \
 	t.info \
+	t.merge \
 	t.remove \
 	t.sample \
 	t.register \

Added: grass/trunk/temporal/t.merge/Makefile
===================================================================
--- grass/trunk/temporal/t.merge/Makefile	                        (rev 0)
+++ grass/trunk/temporal/t.merge/Makefile	2013-04-28 17:53:19 UTC (rev 56020)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = t.merge
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/t.merge/t.merge.html
===================================================================
--- grass/trunk/temporal/t.merge/t.merge.html	                        (rev 0)
+++ grass/trunk/temporal/t.merge/t.merge.html	2013-04-28 17:53:19 UTC (rev 56020)
@@ -0,0 +1,25 @@
+<h2>DESCRIPTION</h2>
+
+This module is designed to register the maps of several input space time datasets in a single output dataset.
+The datasets to merge can be either space time raster, 3D raster or vector datasets and must
+have the same temporal type (absolute or relative).
+<p>
+Existing space time datasets located in the current mapset can be specified as output as well. The
+maps from the input space time datasets will be added to the output.
+<p>
+Maps from the input space time datasets will be registered only once in the output space time dataset, hence the same maps can be registered in different input space time datasets.
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="t.create.html">t.create</a>,
+<a href="t.support.html">t.support</a>,
+<a href="t.register.html">t.register</a>
+</em>
+
+<h2>AUTHOR</h2>
+
+Sören Gebbert
+
+<p><i>Last changed: $Date: 2012-11-04 16:05:21 +0100 (So, 04. Nov 2012) $</i>
+

Added: grass/trunk/temporal/t.merge/t.merge.py
===================================================================
--- grass/trunk/temporal/t.merge/t.merge.py	                        (rev 0)
+++ grass/trunk/temporal/t.merge/t.merge.py	2013-04-28 17:53:19 UTC (rev 56020)
@@ -0,0 +1,149 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:       t.merge
+# AUTHOR(S):    Soeren Gebbert
+#
+# PURPOSE:      Merge several space time datasets into a single one
+# COPYRIGHT:    (C) 2011 by the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (version 2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+#%module
+#% description: Merge several space time datasets into a single one
+#% keywords: temporal
+#% keywords: merge
+#%end
+
+#%option G_OPT_STDS_INPUTS
+#%end
+
+#%option G_OPT_STDS_OUTPUT
+#%end
+
+#%option G_OPT_STDS_TYPE
+#% guidependency: inputs
+#% guisection: Required
+#%end
+
+import grass.temporal as tgis
+import grass.script as grass
+
+############################################################################
+grass.set_raise_on_error(True)
+
+def main():
+
+    # Get the options
+    inputs = options["inputs"]
+    output = options["output"]
+    type = options["type"]
+
+    # Make sure the temporal database exists
+    tgis.init()
+
+    #Get the current mapset to create the id of the space time dataset
+    mapset = grass.gisenv()["MAPSET"]
+
+    inputs_split = inputs.split(",")
+    input_ids = []
+
+    for input in inputs_split:
+        if input.find("@") >= 0:
+            input_ids.append(input)
+        else:
+            input_ids.append(input + "@" + mapset)
+
+    # Set the output name correct
+    if output.find("@") >= 0:
+        out_mapset = output.split("@")[1]
+        if out_mapset != mapset:
+            grass.fatal(_("Output space time dataset <%s> must be located in this mapset") % (output))
+    else:
+        output_id = output + "@" + mapset
+
+    dbif = tgis.SQLDatabaseInterfaceConnection()
+    dbif.connect()
+
+    stds_list = []
+    first = None
+
+    for id in input_ids:
+        stds = tgis.dataset_factory(type, id)
+        if stds.is_in_db(dbif=dbif) == False:
+            dbif.close()
+            grass.fatal(_("Space time %s dataset <%s> not found") % (
+                stds.get_new_map_instance(None).get_type(), id))
+
+        stds.select(dbif=dbif)
+        if first is None:
+            first = stds
+
+        if first.get_temporal_type() != stds.get_temporal_type():
+            dbif.close()
+            grass.fatal(_("Space time datasets to merge must have the same temporal type"))
+
+        stds_list.append(stds)
+
+    # Do nothing if nothing to merge
+    if first is None:
+        dbif.close()
+        return
+
+    # Check if the new id is in the database
+    output_stds = tgis.dataset_factory(type, output_id)
+    output_exists = output_stds.is_in_db(dbif=dbif)
+
+    if output_exists == True and grass.overwrite() == False:
+        dbif.close()
+        grass.fatal(_("Unable to merge maps into space time %s dataset <%s> "\
+                      "please use the overwrite flag.") % \
+                      (stds.get_new_map_instance(None).get_type(), output_id))
+
+    if not output_exists:
+        output_stds = tgis.create_space_time_dataset(output, type,
+                                   first.get_temporal_type(),
+                                   "Merged space time dataset",
+                                   "Merged space time dataset",
+                                   "mean", dbif=dbif, overwrite=False)
+    else:
+        output_stds.select(dbif=dbif)
+
+    registered_output_maps = {}
+    # Maps that are already registered in an existing dataset 
+    # are not registered again
+    if output_exists == True:
+        rows = output_stds.get_registered_maps(columns="id", dbif=dbif)
+        if rows:
+            for row in rows:
+                registered_output_maps[row["id"]] = row["id"]
+
+    for stds in stds_list:
+        # Avoid merging of already registered maps
+        if stds.get_id() != output_stds.get_id():
+            maps = stds.get_registered_maps_as_objects(dbif=dbif)
+
+            if maps:
+                for map in maps:
+                    # Jump over already registered maps
+                    if map.get_id() in registered_output_maps:
+                        continue
+
+                    map.select(dbif=dbif)
+                    output_stds.register_map(map=map, dbif=dbif)
+                    # Update the registered map list
+                    registered_output_maps[map.get_id()] = map.get_id()
+
+    output_stds.update_from_registered_maps(dbif=dbif)
+
+    if output_exists == True:
+        output_stds.update_command_string(dbif=dbif)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


Property changes on: grass/trunk/temporal/t.merge/t.merge.py
___________________________________________________________________
Added: svn:executable
   + *

Added: grass/trunk/temporal/t.merge/test.t.merge.sh
===================================================================
--- grass/trunk/temporal/t.merge/test.t.merge.sh	                        (rev 0)
+++ grass/trunk/temporal/t.merge/test.t.merge.sh	2013-04-28 17:53:19 UTC (rev 56020)
@@ -0,0 +1,51 @@
+#!/bin/sh
+# Tests the merging of space time datasets 
+
+# We need to set a specific region in the
+# @preprocess step of this test. 
+# The region setting should work for UTM and LL test locations
+g.region s=0 n=80 w=0 e=120 b=0 t=50 res=10 res3=10 -p3
+
+r.mapcalc --o expr="prec_1 = rand(10, 150)"
+r.mapcalc --o expr="prec_2 = rand(20, 250)"
+r.mapcalc --o expr="prec_3 = rand(30, 350)"
+r.mapcalc --o expr="prec_4 = rand(40, 450)"
+r.mapcalc --o expr="prec_5 = rand(50, 550)"
+r.mapcalc --o expr="prec_6 = rand(60, 650)"
+
+# Register maps in temporal database
+t.register -i --o maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 \
+    start="2001-01-01" increment="1 month"
+
+# We need to create three space time dataset and registe the maps
+# in several space time datasets
+t.create --o type=strds temporaltype=absolute output=precip_abs1 \
+	title="Test" descr="This is the 1 test strds" semantictype=sum
+t.register -i --o input=precip_abs1 maps=prec_1,prec_2,prec_3 
+
+t.create --o type=strds temporaltype=absolute output=precip_abs2 \
+	title="Test" descr="This is the 2 test strds" semantictype=sum
+t.register --o input=precip_abs2 maps=prec_3,prec_4,prec_5
+
+t.create --o type=strds temporaltype=absolute output=precip_abs3 \
+	title="Test" descr="This is the 3 test strds" semantictype=sum
+t.register --o input=precip_abs3 maps=prec_4,prec_5,prec_6
+
+
+# @test to merge two and three space time datasets
+t.merge inputs=precip_abs1 output=precip_abs4
+t.info precip_abs4
+t.rast.list precip_abs4
+
+t.unregister type=rast maps=prec_1,prec_2,prec_3 input=precip_abs4
+t.merge inputs=precip_abs1,precip_abs2,precip_abs3 output=precip_abs4
+t.info precip_abs4
+t.rast.list precip_abs4
+
+t.merge inputs=precip_abs1,precip_abs2,precip_abs3,precip_abs4 output=precip_abs4
+t.info precip_abs4
+t.rast.list precip_abs4
+
+t.remove type=strds input=precip_abs1,precip_abs2,precip_abs3,precip_abs4
+t.unregister type=rast maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
+g.remove rast=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6


Property changes on: grass/trunk/temporal/t.merge/test.t.merge.sh
___________________________________________________________________
Added: svn:executable
   + *

Modified: grass/trunk/temporal/t.rename/t.rename.py
===================================================================
--- grass/trunk/temporal/t.rename/t.rename.py	2013-04-28 15:04:28 UTC (rev 56019)
+++ grass/trunk/temporal/t.rename/t.rename.py	2013-04-28 17:53:19 UTC (rev 56020)
@@ -2,7 +2,7 @@
 # -*- coding: utf-8 -*-
 ############################################################################
 #
-# MODULE:       t.support
+# MODULE:       t.rename
 # AUTHOR(S):    Soeren Gebbert
 #
 # PURPOSE:      Renames a space time dataset



More information about the grass-commit mailing list