[GRASS-SVN] r48707 - in grass/trunk/temporal: . t.create tr.aggregate tr.register

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Oct 10 14:57:21 EDT 2011


Author: huhabla
Date: 2011-10-10 11:57:21 -0700 (Mon, 10 Oct 2011)
New Revision: 48707

Added:
   grass/trunk/temporal/tr.aggregate/
   grass/trunk/temporal/tr.aggregate/Makefile
   grass/trunk/temporal/tr.aggregate/test.tr.aggregate.sh
   grass/trunk/temporal/tr.aggregate/tr.aggregate.html
   grass/trunk/temporal/tr.aggregate/tr.aggregate.py
Modified:
   grass/trunk/temporal/Makefile
   grass/trunk/temporal/t.create/t.create.py
   grass/trunk/temporal/tr.register/register_ECAD_maps_temp_mean_1995_2010.py
Log:
New aggregation module for space time raster datasets


Modified: grass/trunk/temporal/Makefile
===================================================================
--- grass/trunk/temporal/Makefile	2011-10-10 18:55:57 UTC (rev 48706)
+++ grass/trunk/temporal/Makefile	2011-10-10 18:57:21 UTC (rev 48707)
@@ -7,6 +7,7 @@
 	t.remove \
 	t.time.abs \
 	t.time.rel \
+	tr.aggregate \
 	tr.register \
 	tr.series \
 	tr.extract \

Modified: grass/trunk/temporal/t.create/t.create.py
===================================================================
--- grass/trunk/temporal/t.create/t.create.py	2011-10-10 18:55:57 UTC (rev 48706)
+++ grass/trunk/temporal/t.create/t.create.py	2011-10-10 18:57:21 UTC (rev 48707)
@@ -117,14 +117,14 @@
 
     if sp.is_in_db(dbif) and grass.overwrite() == False:
         dbif.close()
-        grass.fatal("Space time " + sp.get_new_map_instance(None).get_type() + " dataset <" + name + "> is already in the database. Use the overwrite flag.")
+        grass.fatal(_("Space time %s dataset <%s> is already in the database. Use the overwrite flag.") % name)
 
     if sp.is_in_db(dbif) and grass.overwrite() == True:
-        grass.info("Overwrite space time " + sp.get_new_map_instance(None).get_type() + " dataset <" + name + "> and unregister all maps.")
+        grass.info(_("Overwrite space time %s dataset <%s> and unregister all maps.") % (sp.get_new_map_instance(None).get_type(), name))
         sp.delete(dbif)
         sp = sp.get_new_instance(id)
 
-    grass.info("Create space time " + sp.get_new_map_instance(None).get_type() + " dataset.")
+    grass.verbose(_("Create space time %s dataset.") % sp.get_new_map_instance(None).get_type())
 
     sp.set_initial_values(granularity=gran, temporal_type=temporaltype, semantic_type=semantic, title=title, description=descr)
     sp.insert(dbif)

Added: grass/trunk/temporal/tr.aggregate/Makefile
===================================================================
--- grass/trunk/temporal/tr.aggregate/Makefile	                        (rev 0)
+++ grass/trunk/temporal/tr.aggregate/Makefile	2011-10-10 18:57:21 UTC (rev 48707)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = tr.aggregate
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/tr.aggregate/test.tr.aggregate.sh
===================================================================
--- grass/trunk/temporal/tr.aggregate/test.tr.aggregate.sh	                        (rev 0)
+++ grass/trunk/temporal/tr.aggregate/test.tr.aggregate.sh	2011-10-10 18:57:21 UTC (rev 48707)
@@ -0,0 +1,36 @@
+# 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
+# raster with r.mapcalc and create two space time raster datasets
+# with relative and absolute time
+# 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(0, 550)"
+r.mapcalc --o expr="prec_2 = rand(0, 450)"
+r.mapcalc --o expr="prec_3 = rand(0, 320)"
+r.mapcalc --o expr="prec_4 = rand(0, 510)"
+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-15 12:05:45" increment="14 days"
+
+# The first @test
+# We create the space time raster datasets and register the raster maps with absolute time interval
+
+t.info type=strds dataset=precip_abs1
+
+tr.aggregate --o --v input=precip_abs1 output=precip_abs2 base=prec_sum granularity="2 days" method=average
+t.info type=strds dataset=precip_abs2
+tr.aggregate --o --v input=precip_abs1 output=precip_abs2 base=prec_sum granularity="1 months" method=maximum
+t.info type=strds dataset=precip_abs2
+tr.aggregate --o --v input=precip_abs1 output=precip_abs2 base=prec_sum granularity="2 months" method=minimum
+t.info type=strds dataset=precip_abs2
+tr.aggregate --o --v input=precip_abs1 output=precip_abs2 base=prec_sum granularity="3 months" method=sum
+t.info type=strds dataset=precip_abs2
+
+
+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

Added: grass/trunk/temporal/tr.aggregate/tr.aggregate.html
===================================================================
Added: grass/trunk/temporal/tr.aggregate/tr.aggregate.py
===================================================================
--- grass/trunk/temporal/tr.aggregate/tr.aggregate.py	                        (rev 0)
+++ grass/trunk/temporal/tr.aggregate/tr.aggregate.py	2011-10-10 18:57:21 UTC (rev 48707)
@@ -0,0 +1,233 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	tr.aggregate
+# AUTHOR(S):	Soeren Gebbert
+#
+# PURPOSE:	Create a new space time raster dataset from the aggregated data of an existing space time raster dataset
+# 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: Create a new space time raster dataset from the aggregated data of an existing space time raster dataset
+#% keywords: spacetime raster dataset
+#% keywords: raster
+#% keywords: aggregation
+#%end
+
+#%option
+#% key: input
+#% type: string
+#% description: Name of a space time raster dataset
+#% required: yes
+#% multiple: no
+#%end
+
+#%option G_OPT_DB_WHERE
+#%end
+
+#%option
+#% key: granularity
+#% type: string
+#% description: The aggregation granularity, format absolue time "x years, x months, x weeks, x days, x hours, x minutes, x seconds" or a double value for relative time
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% description: Name of the output space time raster dataset
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: method
+#% type: string
+#% description: Aggregate operation to be peformed on the raster maps
+#% required: yes
+#% multiple: no
+#% options: average,count,median,mode,minimum,min_raster,maximum,max_raster,stddev,range,sum,variance,diversity,slope,offset,detcoeff,quart1,quart3,perc90,quantile,skewness,kurtosis
+#% answer: average
+#%end
+
+#%option
+#% key: base
+#% type: string
+#% description: Base name of the new created raster maps
+#% required: yes
+#% multiple: no
+#%end
+
+#%flag
+#% key: n
+#% description: Register Null maps
+#%end
+
+import grass.script as grass
+import grass.temporal as tgis
+
+############################################################################
+
+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(where, "start_time", dbif)
+
+    if not rows:
+        return None
+
+    names = []
+    for row in rows:
+        names.append(row["id"])
+
+    return names    
+
+
+def main():
+
+    # Get the options
+    input = options["input"]
+    output = options["output"]
+    where = options["where"]
+    gran = options["granularity"]
+    base = options["base"]
+    register_null = flags["n"]
+    method = options["method"]
+
+    # Make sure the temporal database exists
+    tgis.create_temporal_database()
+    
+    mapset =  grass.gisenv()["MAPSET"]
+
+    if input.find("@") >= 0:
+        id = input
+    else:
+        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))
+
+    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)
+            new_sp = tgis.space_time_raster_dataset(out_id)
+        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(gran, temporal_type, semantic_type, title, description)
+    new_sp.insert(dbif)
+
+    rows = sp.get_registered_maps(where, "start_time", dbif)
+
+    if not rows:
+            grass.fatal(_("Space time raster dataset <%s> is empty") % out_id)
+
+    # Modify the start time to fit the granularity
+    first_start_time = tgis.adjust_datetime_to_granularity( rows[0]["start_time"], gran)
+    last_start_time = rows[len(rows) - 1]["start_time"]
+    next_start_time = first_start_time
+
+    count = 0
+    while next_start_time <= last_start_time:
+        start = next_start_time
+        if sp.is_time_absolute():
+            end = tgis.increment_datetime_by_string(next_start_time, gran)
+        else:
+            end = next_start_time + gran
+        next_start_time = end
+
+        input_map_names = collect_map_names(sp, dbif, start, end)
+
+        if input_map_names:
+            grass.verbose(_("Aggregate %s raster maps") %(len(input_map_names)))
+            count += 1
+        
+            output_map_name = "%s_%i" % (base, count)
+
+            map_id = output_map_name + "@" + mapset
+
+            new_map = new_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:
+                    dbif.close()
+                    grass.error(_("Raster map <%s> is already in temporal database, use overwrite flag to overwrite"))
+                    continue
+
+            grass.verbose(_("Compute aggregation of maps between %s - %s" % (str(start), str(end))))
+
+            # Create the r.series input file
+            filename = grass.tempfile(True)
+            file = open(filename, 'w')
+
+            for name in input_map_names:
+                string = "%s\n" % (name)
+                file.write(string)
+
+            file.close()
+            # Run r.series
+            ret = grass.run_command("r.series", flags="z", file=filename, output=output_map_name, overwrite=grass.overwrite(), method=method)
+
+            if ret != 0:
+                dbif.close()
+                grass.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:
+                    continue
+
+            # Set the time stamp
+            if new_sp.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)
+
+            new_sp.register_map(new_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()
+


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

Modified: grass/trunk/temporal/tr.register/register_ECAD_maps_temp_mean_1995_2010.py
===================================================================
--- grass/trunk/temporal/tr.register/register_ECAD_maps_temp_mean_1995_2010.py	2011-10-10 18:55:57 UTC (rev 48706)
+++ grass/trunk/temporal/tr.register/register_ECAD_maps_temp_mean_1995_2010.py	2011-10-10 18:57:21 UTC (rev 48707)
@@ -4,7 +4,8 @@
 import grass.script as grass
 
 # Create the space time raster dataset with t.create
-dataset = "temp_mean_1995_2010_daily"
+#dataset = "temp_mean_1995_2010_daily"
+dataset = "temp_mean_1995_1996_daily"
 
 grass.run_command("t.create", type="strds", dataset=dataset, granularity="1 days", \
                   semantic="continuous", temporal="absolute", \
@@ -14,7 +15,8 @@
 
 name = "temp_mean."
 maps=""
-for i in range(5844):
+#for i in range(5844):
+for i in range(365):
     inc = i + 1
     map_name = name + str(inc)
     if i == 0:



More information about the grass-commit mailing list