[GRASS-SVN] r49151 - in grass/trunk/temporal: . tr.to.rast3

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 8 18:12:40 EST 2011


Author: huhabla
Date: 2011-11-08 15:12:40 -0800 (Tue, 08 Nov 2011)
New Revision: 49151

Added:
   grass/trunk/temporal/tr.to.rast3/
   grass/trunk/temporal/tr.to.rast3/Makefile
   grass/trunk/temporal/tr.to.rast3/test.tr.to.rast3.sh
   grass/trunk/temporal/tr.to.rast3/tr.to.rast3.html
   grass/trunk/temporal/tr.to.rast3/tr.to.rast3.py
Modified:
   grass/trunk/temporal/Makefile
Log:
New module to convert space time raster datasets into raster3d maps


Modified: grass/trunk/temporal/Makefile
===================================================================
--- grass/trunk/temporal/Makefile	2011-11-08 22:33:51 UTC (rev 49150)
+++ grass/trunk/temporal/Makefile	2011-11-08 23:12:40 UTC (rev 49151)
@@ -7,11 +7,13 @@
 	t.list \
 	t.info \
 	t.remove \
+	t.sample \
 	t.time.abs \
 	t.time.rel \
 	tr.aggregate \
 	tr.aggregate.ds \
 	tr.register \
+	tr.to.rast3 \
 	tr.list \
 	tv.list \
 	tr3.list \

Added: grass/trunk/temporal/tr.to.rast3/Makefile
===================================================================
--- grass/trunk/temporal/tr.to.rast3/Makefile	                        (rev 0)
+++ grass/trunk/temporal/tr.to.rast3/Makefile	2011-11-08 23:12:40 UTC (rev 49151)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = tr.to.rast3
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/tr.to.rast3/test.tr.to.rast3.sh
===================================================================
--- grass/trunk/temporal/tr.to.rast3/test.tr.to.rast3.sh	                        (rev 0)
+++ grass/trunk/temporal/tr.to.rast3/test.tr.to.rast3.sh	2011-11-08 23:12:40 UTC (rev 49151)
@@ -0,0 +1,33 @@
+# This is a test to register and unregister raster maps in
+# space time raster input.
+# The raster maps will be registered in different space time raster
+# inputs
+
+# 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 inputs
+# 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=1 res=10 res3=1 -p3
+
+r.mapcalc --o expr="prec_1 = 100"
+r.mapcalc --o expr="prec_2 = 200"
+r.mapcalc --o expr="prec_3 = 300"
+r.mapcalc --o expr="prec_4 = 400"
+r.mapcalc --o expr="prec_5 = 500"
+r.mapcalc --o expr="prec_6 = 600"
+
+# The first @test
+# We create the space time raster inputs and register the raster maps with absolute time interval
+
+t.create --o type=strds temporaltype=absolute output=precip_abs title="A test" descr="A test"
+
+tr.register -i input=precip_abs maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="1 months"
+
+tr.to.rast3 --o input=precip_abs output=prec_sum
+
+t.remove type=rast input=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
+t.remove type=strds input=precip_abs
+
+r3.info prec_sum
+g.region s=0 n=80 w=0 e=120 b=0 t=1 res=10 res3=1

Added: grass/trunk/temporal/tr.to.rast3/tr.to.rast3.html
===================================================================
Added: grass/trunk/temporal/tr.to.rast3/tr.to.rast3.py
===================================================================
--- grass/trunk/temporal/tr.to.rast3/tr.to.rast3.py	                        (rev 0)
+++ grass/trunk/temporal/tr.to.rast3/tr.to.rast3.py	2011-11-08 23:12:40 UTC (rev 49151)
@@ -0,0 +1,114 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	tr.to.rast3
+# AUTHOR(S):	Soeren Gebbert
+#               
+# PURPOSE:	Convert a space time raster dataset into a rast3d map
+# 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: Convert a space time raster dataset into a rast3d map
+#% keywords: dataset
+#% keywords: spacetime
+#% keywords: raster
+#% keywords: raster3d
+#%end
+
+#%option
+#% key: input
+#% type: string
+#% description: Name of a space time raster dataset
+#% required: yes
+#% multiple: no
+#%end
+
+#%option G_OPT_R3_OUTPUT
+#%end
+
+import os
+import grass.script as grass
+import grass.temporal as tgis
+
+############################################################################
+
+def main():
+
+    # Get the options
+    input = options["input"]
+    output = options["output"]
+
+    # 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))
+
+    sp.select()
+
+    # Compute relative granularity and set bottom, top and resolution
+    granularity = sp.get_granularity()
+
+    if sp.is_time_absolute():
+        start, end = sp.get_valid_time()
+        end = tgis.increment_datetime_by_string(start, granularity)
+        granularity = tgis.time_delta_to_relative_time(end - start)
+
+    maps = sp.get_registered_maps_as_objects_by_granularity()
+
+    num_maps = len(maps)
+    bottom = 0
+    top = granularity * num_maps
+
+    ret = grass.run_command("g.region", t=top, b=bottom, tbres=granularity)
+    
+    if ret != 0:
+        grass.fatal(_("Unable to set 3d region"))
+
+    # Create a NULL map in case of granularity support
+    null_map = "temporary_null_map_%i" % os.getpid()
+    grass.mapcalc("%s = null()" % (null_map))
+
+    if maps:
+    	count = 0
+	map_names = ""
+        for map in maps:
+	    # Use the first map
+            id = map[0].get_id()
+            # None ids will be replaced by NULL maps
+            if id == None:
+                id = null_map
+
+	    if count == 0:
+	        map_names = id
+            else:
+                map_names += ",%s" % id
+
+            count += 1
+
+        ret = grass.run_command("r.to.rast3", input=map_names, output=output, overwrite=grass.overwrite())
+     
+        if ret != 0:
+            grass.fatal(_("Unable to create raster3d map <%s>" % output))
+
+    grass.run_command("g.remove", rast=null_map)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


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



More information about the grass-commit mailing list