[GRASS-SVN] r48619 - in grass/trunk: lib/python/temporal temporal/tr.extract

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Oct 4 18:54:58 EDT 2011


Author: huhabla
Date: 2011-10-04 15:54:57 -0700 (Tue, 04 Oct 2011)
New Revision: 48619

Modified:
   grass/trunk/lib/python/temporal/abstract_datasets.py
   grass/trunk/temporal/tr.extract/test.tr.extract.sh
   grass/trunk/temporal/tr.extract/tr.extract.py
Log:
New module to extract a subset of a space time raster dataset using where queries and r.mapcalc expressions.

Modified: grass/trunk/lib/python/temporal/abstract_datasets.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_datasets.py	2011-10-04 21:19:23 UTC (rev 48618)
+++ grass/trunk/lib/python/temporal/abstract_datasets.py	2011-10-04 22:54:57 UTC (rev 48619)
@@ -465,6 +465,22 @@
         self.metadata.set_title(title)
         self.metadata.set_description(description)
 
+    def get_initial_values(self):
+        """Return the initial values: granularity, temporal_type, semantic_type, title, description"""
+        
+        temporal_type = self.get_temporal_type()
+
+        if temporal_type == "absolute":
+            granularity   = self.absolute_time.get_granularity()
+        elif temporal_type == "relative":
+            granularity = self.relative_time.get_granularity()
+
+        semantic_type = self.base.get_semantic_type()
+        title = self.metadata.get_title()
+        description = self.metadata.get_description()
+
+        return granularity, temporal_type, semantic_type, title, description
+
     def get_temporal_relation_matrix(self, dbif=None):
         """Return the temporal relation matrix between all registered maps
         """
@@ -842,6 +858,10 @@
         """
         core.verbose(_("Update metadata, spatial and temporal extent from all registered maps of <%s>") % (self.get_id()))
 
+        # Nothing to do if the register is not present
+        if not self.get_map_register():
+            return
+
         connect = False
 
         if dbif == None:
@@ -849,6 +869,7 @@
             dbif.connect()
             connect = True
 
+
         use_start_time = False
 
         # Get basic info

Modified: grass/trunk/temporal/tr.extract/test.tr.extract.sh
===================================================================
--- grass/trunk/temporal/tr.extract/test.tr.extract.sh	2011-10-04 21:19:23 UTC (rev 48618)
+++ grass/trunk/temporal/tr.extract/test.tr.extract.sh	2011-10-04 22:54:57 UTC (rev 48619)
@@ -1,7 +1,4 @@
-# This is a test to register and unregister raster maps in
-# space time raster dataset.
-# The raster maps will be registered in different space time raster
-# datasets
+# Test the extraction of a subset of a space time raster dataset
 
 # We need to set a specific region in the
 # @preprocess step of this test. We generate
@@ -17,45 +14,15 @@
 r.mapcalc --o expr="prec_5 = rand(0, 300)"
 r.mapcalc --o expr="prec_6 = rand(0, 650)"
 
+t.create --o type=strds temporaltype=absolute dataset=precip_abs1 gran="3 months" title="A test" descr="A test"
+tr.register -i dataset=precip_abs1 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="3 months"
+
 # The first @test
 # We create the space time raster datasets and register the raster maps with absolute time interval
 
-t.create --v --o type=strds temporaltype=absolute dataset=precip_abs1 gran="1 senconds" title="A test" descr="A test"
-t.create --v --o type=strds temporaltype=absolute dataset=precip_abs2 gran="1 minutes" title="A test" descr="A test"
-t.create --v --o type=strds temporaltype=absolute dataset=precip_abs3 gran="1 hours" title="A test" descr="A test"
-t.create --v --o type=strds temporaltype=absolute dataset=precip_abs4 gran="1 days" title="A test" descr="A test"
-t.create --v --o type=strds temporaltype=absolute dataset=precip_abs5 gran="1 weeks" title="A test" descr="A test"
-t.create --v --o type=strds temporaltype=absolute dataset=precip_abs6 gran="1 months" title="A test" descr="A test"
-t.create --v --o type=strds temporaltype=absolute dataset=precip_abs7 gran="1 years" title="A test" descr="A test"
+tr.extract --o input=precip_abs1 output=precip_abs2 where='start_time > "2001-06-01"' expression=" if(precip_abs1 > 400, precip_abs1, null())" base=new_prec
 
-tr.register --v dataset=precip_abs1 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="1 seconds"
-t.info type=strds dataset=precip_abs1
-tr.unregister --v dataset=precip_abs1 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
-t.info type=strds dataset=precip_abs1
-
-tr.register --v dataset=precip_abs2 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="20 seconds, 5 minutes"
 t.info type=strds dataset=precip_abs2
 
-tr.register --v dataset=precip_abs3 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="8 hours"
-t.info type=strds dataset=precip_abs3
-tr.unregister --v maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
-t.info type=strds dataset=precip_abs3
-
-tr.register dataset=precip_abs4 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="3 days"
-t.info type=strds dataset=precip_abs4
-
-tr.register dataset=precip_abs5 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="4 weeks"
-t.info type=strds dataset=precip_abs5
-
-tr.register dataset=precip_abs6 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-08-01" increment="2 months"
-t.info type=strds dataset=precip_abs6
-
-tr.register dataset=precip_abs7 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="20 years, 3 months, 1 days, 4 hours"
-t.info type=strds dataset=precip_abs7
-# Register with different valid time again
-tr.register dataset=precip_abs7 maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="99 years, 9 months, 9 days, 9 hours"
-t.info type=strds dataset=precip_abs7
-
-t.remove --v type=raster dataset=prec_1,prec_2,prec_3
-t.remove --v type=strds dataset=precip_abs1,precip_abs2,precip_abs3,precip_abs4,precip_abs5,precip_abs6,precip_abs7
-t.remove --v type=raster dataset=prec_4,prec_5,prec_6
+t.remove type=raster dataset=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
+t.remove type=strds dataset=precip_abs1,precip_abs2

Modified: grass/trunk/temporal/tr.extract/tr.extract.py
===================================================================
--- grass/trunk/temporal/tr.extract/tr.extract.py	2011-10-04 21:19:23 UTC (rev 48618)
+++ grass/trunk/temporal/tr.extract/tr.extract.py	2011-10-04 22:54:57 UTC (rev 48619)
@@ -5,7 +5,7 @@
 # MODULE:	tr.extract
 # AUTHOR(S):	Soeren Gebbert
 #
-# PURPOSE:	Register raster maps in a space time raster dataset
+# PURPOSE:	Extract a subset of a space time raster dataset
 # COPYRIGHT:	(C) 2011 by the GRASS Development Team
 #
 #		This program is free software under the GNU General Public
@@ -24,9 +24,9 @@
 #%option
 #% key: input
 #% type: string
-#% description: Name of an existing space time raster dataset
+#% description: Name of a space time raster dataset
 #% required: yes
-#% multiple: yes
+#% multiple: no
 #%end
 
 #%option G_OPT_DB_WHERE
@@ -51,11 +51,15 @@
 #%option
 #% key: base
 #% type: string
-#% description: Base name  of the new created raster maps
-#% required: yes
+#% description: Base name of the new created raster maps
+#% required: no
 #% multiple: no
 #%end
 
+#%flag
+#% key: n
+#% description: Register Null maps
+#%end
 
 import grass.script as grass
 import grass.temporal as tgis
@@ -69,44 +73,119 @@
     output = options["output"]
     where = options["where"]
     expression = options["expression"]
+    base = options["base"]
+    register_null = flags["n"]
 
     # Make sure the temporal database exists
     tgis.create_temporal_database()
+    
+    mapset =  grass.gisenv()["MAPSET"]
 
     if input.find("@") >= 0:
         id = input
     else:
-        mapset =  grass.gisenv()["MAPSET"]
         id = input + "@" + mapset
 
     sp = tgis.space_time_raster_dataset(id)
-
+    
     if sp.is_in_db() == False:
         grass.fatal(_("Dataset <%s> not found in temporal database") % (id))
 
-    sp.select()
+    if expression and base == None:
+        grass.fatal(_("A raster map base name must be provided"))
 
-    rows = sp.get_registered_maps(None, where, sort)
+    dbif = tgis.sql_database_interface()
+    dbif.connect()
 
+    sp.select(dbif)
+
+    if output.find("@") >= 0:
+        out_id = output
+    else:
+        out_id = output + "@" + mapset
+
+    # The new space time raster dataset
+    new_sp = tgis.space_time_raster_dataset(out_id)
+    if new_sp.is_in_db():
+        if grass.overwrite() == True:
+            new_sp.delete(dbif)
+        else:
+            grass.fatal(_("Space time raster dataset <%s> is already in database, use overwrite flag to overwrite") % out_id)
+
+    granularity, temporal_type, semantic_type, title, description = sp.get_initial_values()
+    new_sp.set_initial_values(granularity, temporal_type, semantic_type, title, description)
+    new_sp.insert(dbif)
+
+    rows = sp.get_registered_maps(dbif, where, "start_time")
+
     if rows:
-        inputs = ""
-
         count = 0
         for row in rows:
-            if count == 0:
-                inputs += row["id"]
-            else:
-                inputs += "," + row["id"]
             count += 1
 
-        print inputs
+            old_map = sp.get_new_map_instance(row["id"])
+            old_map.select(dbif)
+            
+            if expression:
 
-        if grass.overwrite() == True:
-            grass.run_command("r.mapcalc", expression=expression, overwrite=True)
-        else:
-            grass.run_command("r.mapcalc", expression=expression, overwrite=False)
+                map_name = "%s_%i" % (base, count)
 
+                expr = "%s = %s" % (map_name, expression.replace(sp.get_id(), row["id"]))
+                expr = expr.replace(sp.base.get_name(), row["id"])
 
+                map_id = map_name + "@" + mapset
+
+                new_map = sp.get_new_map_instance(map_id)
+
+                # Check if new map is in the temporal database
+                if new_map.is_in_db(dbif):
+                    if grass.overwrite() == True:
+                        # Remove the existing temporal database entry
+                        new_map.delete(dbif)
+                        new_map = sp.get_new_map_instance(map_id)
+                    else:
+                        grass.error(_("Raster map <%s> is already in temporal database, use overwrite flag to overwrite"))
+                        continue
+
+                grass.verbose(_("Apply r.mapcalc expression: \"%s\"") % expr)
+
+                if grass.overwrite() == True:
+                    ret = grass.run_command("r.mapcalc", expression=expr, overwrite=True)
+                else:
+                    ret = grass.run_command("r.mapcalc", expression=expr, overwrite=False)
+
+                if ret != 0:
+                    grass.error(_("Error while r.mapcalc computation, continue with next map"))
+                    continue
+
+                # 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:
+                        continue
+
+                # Set the time stamp
+                if old_map.is_time_absolute():
+                    start, end, tz = old_map.get_absolute_time()
+                    new_map.set_absolute_time(start, end, tz)
+                else:
+                    start, end = old_map.get_relative_time()
+                    new_map.set_relative_time(start, end)
+
+                # Insert map in temporal database
+                new_map.insert(dbif)
+
+                new_sp.register_map(new_map, dbif)
+            else:
+                new_sp.register_map(old_map, dbif)
+
+        # Update the spatio-temporal extent and the raster metadata table entries
+        new_sp.update_from_registered_maps(dbif)
+        
+    dbif.close()
+
 if __name__ == "__main__":
     options, flags = grass.parser()
     main()



More information about the grass-commit mailing list