[GRASS-SVN] r48883 - in grass/trunk/temporal: . t.list t.support t.time.rel t.topology tr.export tr.import tr.list tr.register tr.series

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 20 14:36:13 EDT 2011


Author: huhabla
Date: 2011-10-20 11:36:13 -0700 (Thu, 20 Oct 2011)
New Revision: 48883

Added:
   grass/trunk/temporal/tr.export/
   grass/trunk/temporal/tr.export/Makefile
   grass/trunk/temporal/tr.export/test.tr.export.sh
   grass/trunk/temporal/tr.export/tr.export.html
   grass/trunk/temporal/tr.export/tr.export.py
   grass/trunk/temporal/tr.import/
   grass/trunk/temporal/tr.import/Makefile
   grass/trunk/temporal/tr.import/test.tr.import.sh
   grass/trunk/temporal/tr.import/tr.import.html
   grass/trunk/temporal/tr.import/tr.import.py
   grass/trunk/temporal/tr.register/ECAD_climate_analysis_1995_2010.py
Removed:
   grass/trunk/temporal/tr.register/register_ECAD_maps_temp_mean_1995_2010.py
Modified:
   grass/trunk/temporal/Makefile
   grass/trunk/temporal/t.list/t.list.py
   grass/trunk/temporal/t.support/t.support.py
   grass/trunk/temporal/t.time.rel/t.time.rel.py
   grass/trunk/temporal/t.topology/test.t.topology.sh
   grass/trunk/temporal/tr.list/test.tr.list.sh
   grass/trunk/temporal/tr.list/tr.list.py
   grass/trunk/temporal/tr.series/test.tr.series.sh
   grass/trunk/temporal/tr.series/tr.series.py
Log:
Implemented import and export modules. More options added to tr.list.


Modified: grass/trunk/temporal/Makefile
===================================================================
--- grass/trunk/temporal/Makefile	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/Makefile	2011-10-20 18:36:13 UTC (rev 48883)
@@ -13,6 +13,8 @@
 	tr.register \
 	tr.list \
 	tr.series \
+	tr.export \
+	tr.import \
 	tr.extract \
 	tr3.register \
 	tv.register \

Modified: grass/trunk/temporal/t.list/t.list.py
===================================================================
--- grass/trunk/temporal/t.list/t.list.py	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/t.list/t.list.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -31,7 +31,7 @@
 #%end
 
 #%option
-#% key: sort
+#% key: order
 #% type: string
 #% description: Sort the space time dataset by category. Columns number_of_maps and granularity only available fpr space time datasets
 #% required: no
@@ -90,7 +90,7 @@
     type = options["type"]
     temporaltype = options["temporaltype"]
     columns = options["columns"]
-    sort = options["sort"]
+    order = options["order"]
     where = options["where"]
     separator = options["fs"]
     colhead = flags['c']
@@ -116,8 +116,8 @@
     else:
         sql = "SELECT * FROM " + table
 
-    if sort:
-        sql += " ORDER BY " + sort
+    if order:
+        sql += " ORDER BY " + order
 
     if where:
         sql += " WHERE " + where

Modified: grass/trunk/temporal/t.support/t.support.py
===================================================================
--- grass/trunk/temporal/t.support/t.support.py	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/t.support/t.support.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -30,18 +30,10 @@
 #%end
 
 #%option
-#% key: granularity
-#% type: string
-#% description: The granularity of the space time dataset (NNN day, NNN week, NNN month)
-#% required: yes
-#% multiple: no
-#%end
-
-#%option
 #% key: semantictype
 #% type: string
 #% description: The semantic type of the space time dataset
-#% required: yes
+#% required: no
 #% multiple: no
 #% options: event, const, continuous
 #% answer: event
@@ -60,7 +52,7 @@
 #% key: title
 #% type: string
 #% description: Title of the space time dataset
-#% required: yes
+#% required: no
 #% multiple: no
 #%end
 
@@ -68,7 +60,7 @@
 #% key: description
 #% type: string
 #% description: Description of the space time dataset
-#% required: yes
+#% required: no
 #% multiple: no
 #%end
 
@@ -90,7 +82,6 @@
     title = options["title"]
     descr = options["description"]
     semantic = options["semantictype"]
-    gran = options["granularity"]
     update = flags["u"]
 
     # Make sure the temporal database exists
@@ -98,8 +89,12 @@
 
     #Get the current mapset to create the id of the space time dataset
     mapset =  grass.gisenv()["MAPSET"]
-    id = name + "@" + mapset
 
+    if name.find("@") >= 0:
+        id = name
+    else:
+        id = name + "@" + mapset
+
     sp = tgis.dataset_factory(type, id)
 
     if sp.is_in_db() == False:
@@ -108,7 +103,7 @@
     sp.select()
     # Temporal type can not be changed
     ttype= sp.get_temporal_type()
-    sp.set_initial_values(granularity=gran, temporal_type=ttype, semantic_type=semantic, title=title, description=descr)
+    sp.set_initial_values(temporal_type=ttype, semantic_type=semantic, title=title, description=descr)
     # Update only non-null entries
     sp.update()
 

Modified: grass/trunk/temporal/t.time.rel/t.time.rel.py
===================================================================
--- grass/trunk/temporal/t.time.rel/t.time.rel.py	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/t.time.rel/t.time.rel.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -68,7 +68,7 @@
 #% type: string
 #% description: Input map type
 #% required: no
-#% options: rast, rast3d, vect
+#% options: rast,rast3d,vect
 #% answer: rast
 #%end
 

Modified: grass/trunk/temporal/t.topology/test.t.topology.sh
===================================================================
--- grass/trunk/temporal/t.topology/test.t.topology.sh	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/t.topology/test.t.topology.sh	2011-10-20 18:36:13 UTC (rev 48883)
@@ -22,9 +22,6 @@
 n3=`g.tempfile pid=3 -d`
 n4=`g.tempfile pid=4 -d`
 n5=`g.tempfile pid=5 -d`
-n6=`g.tempfile pid=6 -d`
-n7=`g.tempfile pid=7 -d`
-n8=`g.tempfile pid=8 -d`
 
 cat > $n1 << EOF
 prec_1

Added: grass/trunk/temporal/tr.export/Makefile
===================================================================
--- grass/trunk/temporal/tr.export/Makefile	                        (rev 0)
+++ grass/trunk/temporal/tr.export/Makefile	2011-10-20 18:36:13 UTC (rev 48883)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = tr.export
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/tr.export/test.tr.export.sh
===================================================================
--- grass/trunk/temporal/tr.export/test.tr.export.sh	                        (rev 0)
+++ grass/trunk/temporal/tr.export/test.tr.export.sh	2011-10-20 18:36:13 UTC (rev 48883)
@@ -0,0 +1,34 @@
+# This is a test to list raster maps 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 a space time raster datasets
+# 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)"
+
+n1=`g.tempfile pid=1 -d` 
+
+cat > $n1 << EOF
+prec_1|2001-01-01|2001-07-01
+prec_2|2001-02-01|2001-04-01
+prec_3|2001-03-01|2001-04-01
+prec_4|2001-04-01|2001-06-01
+prec_5|2001-05-01|2001-06-01
+prec_6|2001-06-01|2001-07-01
+EOF
+
+t.create --o type=strds temporaltype=absolute output=precip_abs1 title="A test with input files" descr="A test with input files"
+
+# The first @test
+tr.register -i input=precip_abs1 file=$n1 start="2001-01-01" increment="1 months"
+tr.export input=precip_abs1 output=strds_export.tar.bz2 compression=bzip2 workdir=/tmp
+
+t.remove type=rast input=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
+t.remove type=strds input=precip_abs1

Added: grass/trunk/temporal/tr.export/tr.export.html
===================================================================
Added: grass/trunk/temporal/tr.export/tr.export.py
===================================================================
--- grass/trunk/temporal/tr.export/tr.export.py	                        (rev 0)
+++ grass/trunk/temporal/tr.export/tr.export.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -0,0 +1,204 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	tr.export
+# AUTHOR(S):	Soeren Gebbert
+#               
+# PURPOSE:	Export a 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: Export space time raster dataset 
+#% keywords: dataset
+#% keywords: spacetime
+#% keywords: raster
+#% keywords: export
+#%end
+
+#%option
+#% key: input
+#% type: string
+#% description: Name of a space time raster dataset
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% description: Name of a space time raster dataset archive
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: workdir
+#% type: string
+#% description: Path to the work directory, default is /tmp
+#% required: no
+#% multiple: no
+#% answer: /tmp
+#%end
+
+
+#%option
+#% key: compression
+#% type: string
+#% description: Chose the compression of the tar archive
+#% required: no
+#% multiple: no
+#% options: no,gzip,bzip2
+#% answer: bzip
+#%end
+
+#%option
+#% key: where
+#% type: string
+#% description: A where statement for selected listing e.g: start_time < '2001-01-01' and end_time > '2001-01-01"'
+#% required: no
+#% multiple: no
+#%end
+
+import shutil
+import os
+import tarfile
+import tempfile
+import grass.script as grass
+import grass.temporal as tgis
+
+proj_file_name = "proj.txt"
+init_file_name = "init.txt"
+read_file_name = "readme.txt"
+list_file_name = "list.txt"
+tmp_tar_file_name = "archive" 
+
+############################################################################
+
+def main():
+
+    # Get the options
+    input = options["input"]
+    output = options["output"]
+    compression = options["compression"]
+    workdir = options["workdir"]
+    where = options["where"]
+
+    # 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))
+
+    # Save current working directory path
+    old_cwd = os.getcwd()
+
+    # Create the temporary directory and jump into it
+    new_cwd = tempfile.mkdtemp(dir=workdir)
+    os.chdir(new_cwd)
+
+    sp.select()
+       
+    columns = "name,start_time,end_time"
+    rows = sp.get_registered_maps(columns, where, "start_time", None)
+
+    if compression == "gzip":
+        flag = "w:gz"
+    elif compression == "bzip2":
+        flag = "w:bz2"
+    else:
+        flag = "w"
+
+    # Open the tar archive to add the files
+    tar = tarfile.open(tmp_tar_file_name, flag)
+    list_file = open(list_file_name, "w")
+
+    fs = "|"
+
+    if rows:
+        for row in rows:
+            name = row["name"]
+            start = row["start_time"]
+            end = row["end_time"]
+            if not end:
+                end = start
+            string = "%s%s%s%s%s\n" % (name, fs, start, fs, end)
+            # Write the filename, the start_time and the end_time
+            list_file.write(string)
+
+            out_name = name + ".tif"
+
+            # Export the raster map with r.out.gdal
+            ret = grass.run_command("r.out.gdal", input=name, output=out_name, format="GTiff")
+            if ret != 0:
+                shutil.rmtree(new_cwd)
+                tar.close()
+                grass.fatal(_("Unable to export raster map <%s>" % name))
+
+            tar.add(out_name)
+
+    list_file.close()
+   
+    # Write projection and metadata
+    proj = grass.read_command("g.proj", flags="j")
+
+    proj_file = open(proj_file_name, "w")
+    proj_file.write(proj)
+    proj_file.close()
+
+    init_file = open(init_file_name, "w")
+    # Create the init string
+    string = ""
+    string += "%s=%s\n" % ("temporal_type", sp.get_temporal_type())
+    string += "%s=%s\n" % ("semantic_type", sp.get_semantic_type())
+    string += "%s=%s\n" % ("number_of_maps", sp.metadata.get_number_of_maps())
+    north, south, east, west, top, bottom = sp.get_spatial_extent()
+    string += "%s=%s\n" % ("north", north)
+    string += "%s=%s\n" % ("south", south)
+    string += "%s=%s\n" % ("east", east)
+    string += "%s=%s\n" % ("west", west)
+    init_file.write(string)
+    init_file.close()
+
+    read = grass.read_command("t.info", input=id)
+    read_file = open(read_file_name, "w")
+    read_file.write("This space time raster dataset was exported with tr.export of GRASS GIS 7\n")
+    read_file.write(read)
+    read_file.close()
+
+    # Append the file list
+    tar.add(list_file_name)
+    tar.add(proj_file_name)
+    tar.add(init_file_name)
+    tar.add(read_file_name)
+    tar.close()
+
+    os.chdir(old_cwd)
+
+    # Move the archive to its destination
+    if output.find("/") >= 0 or output.find("\\") >= 0:
+        shutil.move(tmp_tar_file_name, output)
+    else:
+        shutil.move(os.path.join(new_cwd, tmp_tar_file_name), output)
+
+    # Remove the temporary created working directory
+    shutil.rmtree(new_cwd)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


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

Added: grass/trunk/temporal/tr.import/Makefile
===================================================================
--- grass/trunk/temporal/tr.import/Makefile	                        (rev 0)
+++ grass/trunk/temporal/tr.import/Makefile	2011-10-20 18:36:13 UTC (rev 48883)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = tr.import
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/tr.import/test.tr.import.sh
===================================================================
--- grass/trunk/temporal/tr.import/test.tr.import.sh	                        (rev 0)
+++ grass/trunk/temporal/tr.import/test.tr.import.sh	2011-10-20 18:36:13 UTC (rev 48883)
@@ -0,0 +1,39 @@
+# This is a test to list raster maps 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 a space time raster datasets
+# 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
+
+mkdir test
+
+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)"
+
+n1=`g.tempfile pid=1 -d` 
+
+cat > $n1 << EOF
+prec_1|2001-01-01|2001-07-01
+prec_2|2001-02-01|2001-04-01
+prec_3|2001-03-01|2001-04-01
+prec_4|2001-04-01|2001-06-01
+prec_5|2001-05-01|2001-06-01
+prec_6|2001-06-01|2001-07-01
+EOF
+
+t.create --o type=strds temporaltype=absolute output=precip_abs1 title="A test with input files" descr="A test with input files"
+
+# The first @test
+tr.register -i input=precip_abs1 file=$n1 start="2001-01-01" increment="1 months"
+tr.export input=precip_abs1 output=strds_export.tar.bz2 compression=bzip2 workdir=test
+
+tr.import --o input=strds_export.tar.bz2 output=precip_abs1 extrdir=test -oe
+tr.import --o input=strds_export.tar.bz2 output=precip_abs1 extrdir=test -loe
+
+tr.import --o input=strds_export.tar.bz2 output=precip_abs1 extrdir=test 
+tr.import --o input=strds_export.tar.bz2 output=precip_abs1 extrdir=test -l

Added: grass/trunk/temporal/tr.import/tr.import.html
===================================================================
Added: grass/trunk/temporal/tr.import/tr.import.py
===================================================================
--- grass/trunk/temporal/tr.import/tr.import.py	                        (rev 0)
+++ grass/trunk/temporal/tr.import/tr.import.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -0,0 +1,235 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:	tr.import
+# AUTHOR(S):	Soeren Gebbert
+#               
+# PURPOSE:	Import a 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: Import space time raster dataset 
+#% keywords: dataset
+#% keywords: spacetime
+#% keywords: raster
+#% keywords: import
+#%end
+
+#%option
+#% key: input
+#% type: string
+#% description: Name of a space time raster dataset archive file
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% description: Name of a space time raster dataset
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: extrdir
+#% type: string
+#% description: Path to the extraction directory
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: title
+#% type: string
+#% description: Title of the new space time dataset
+#% required: no
+#% multiple: no
+#%end
+
+#%option
+#% key: description
+#% type: string
+#% description: Description of the new space time dataset
+#% required: no
+#% multiple: no
+#%end
+
+#%flag
+#% key: l
+#% description: Link the raster files using r.external
+#%end
+
+#%flag
+#% key: e
+#% description: Extend location extents based on new dataset
+#%end
+
+#%flag
+#% key: o
+#% description: verride projection (use location's projection)
+#%end
+
+
+import shutil
+import os
+import tarfile
+import tempfile
+import grass.script as grass
+import grass.temporal as tgis
+
+proj_file_name = "proj.txt"
+init_file_name = "init.txt"
+list_file_name = "list.txt"
+
+############################################################################
+
+def main():
+
+    # Get the options
+    input = options["input"]
+    output = options["output"]
+    extrdir = options["extrdir"]
+    title = options["title"]
+    descr = options["description"]
+    link = flags["l"]
+    exp = flags["e"]
+    overr = flags["o"]
+
+    # Make sure the temporal database exists
+    tgis.create_temporal_database()
+    
+    # Check if input file and extraction directory exits
+    if not os.path.exists(input): 
+        grass.fatal(_("Space time raster dataset archive <%s> not found.") % input)
+    if not os.path.exists(extrdir): 
+        grass.fatal(_("Extraction directory <%s> not found.") % extrdir)
+
+    tar = tarfile.open(input)
+    
+    # Check for important files
+    members = tar.getnames()
+
+    if init_file_name not in members: 
+        grass.fatal(_("Unable to find init file <%s>.") % init_file_name)
+    if list_file_name not in members: 
+        grass.fatal(_("Unable to find list file <%s>.") % list_file_name)
+    if proj_file_name not in members: 
+        grass.fatal(_("Unable to find projection file <%s>.") % proj_file_name)
+
+    tar.extractall(path=extrdir)
+    tar.close()
+
+    # Switch into the extraction directory and check for files
+    os.chdir(extrdir)
+
+    fs = "|"
+    maplist = []
+    mapset =  grass.gisenv()["MAPSET"]
+    list_file = open(list_file_name, "r")
+
+    # Read the map list from file
+    line_count = 0
+    while True:
+        line = list_file.readline()
+        if not line:
+            break
+
+        line_list = line.split(fs)
+
+        mapname = line_list[0].strip()
+        mapid = mapname + "@" + mapset
+
+        row = {}
+        row["name"] = mapname
+        row["id"] = mapid
+        row["start"] = line_list[1].strip()
+        row["end"] = line_list[2].strip()
+
+        maplist.append(row)
+        line_count += 1
+
+    list_file.close()
+    
+    # Check if geotiff files exists
+    for row in maplist:
+        filename = str(row["name"]) + ".tif"
+        if not os.path.exists(filename): 
+            grass.fatal(_("Unable to find geotiff raster file <%s> in archive.") % filename)
+
+    # Read the init file
+    fs = "="
+    init = {}
+    init_file = open(init_file_name, "r")
+    while True:
+        line = init_file.readline()
+        if not line:
+            break
+
+        kv = line.split(fs)
+        init[kv[0]] = kv[1].strip()
+    
+    init_file.close()
+
+    if not init.has_key("temporal_type") or \
+       not init.has_key("semantic_type") or \
+       not init.has_key("number_of_maps"):
+        grass.fatal(_("Key words %s, %s or %s not found in init file.") % ("temporal_type", "semantic_type", "number_of_maps"))
+
+    if line_count != int(init["number_of_maps"]):
+        grass.fatal(_("Number of maps mismatch in init and list file."))
+
+    # Check the space time dataset
+
+    id = output + "@" + mapset
+
+    sp = tgis.space_time_raster_dataset(id)
+    
+    if sp.is_in_db() and grass.overwrite() == False:
+        grass.fatal(_("Space time %s dataset <%s> is already in the database. Use the overwrite flag.") % name)
+
+    # Try to import/link the raster files
+    for row in maplist:
+        name = row["name"]
+        filename = str(row["name"]) + ".tif"
+        impflags=""
+        if overr:
+            impflags += "o"
+        if exp:
+            impflags += "e"
+        
+        if link:
+            ret = grass.run_command("r.external", input=filename, output=name, flags=impflags, overwrite=grass.overwrite())
+        else:
+            ret = grass.run_command("r.in.gdal", input=filename, output=name, flags=impflags, overwrite=grass.overwrite())
+
+        if ret != 0:
+            grass.fatal(_("Unable to import/link raster map <%s>.") % name)
+    
+    # Create the space time raster dataset
+    if sp.is_in_db() and grass.overwrite() == True:
+        grass.info(_("Overwrite space time %s dataset <%s> and unregister all maps.") % (sp.get_new_map_instance(None).get_type(), name))
+        sp.delete()
+        sp = sp.get_new_instance(id)
+
+    temporal_type = init["temporal_type"]
+    semantic_type = init["semantic_type"]
+    grass.verbose(_("Create space time %s dataset.") % sp.get_new_map_instance(None).get_type())
+
+    sp.set_initial_values(temporal_type=temporal_type, semantic_type=semantic_type, title=title, description=descr)
+    sp.insert()
+
+    # register the raster maps
+    fs="|"
+    tgis.register_maps_in_space_time_dataset(type="strds", name=output, file=list_file_name, start="file", end="file", dbif=None, fs=fs)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


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

Modified: grass/trunk/temporal/tr.list/test.tr.list.sh
===================================================================
--- grass/trunk/temporal/tr.list/test.tr.list.sh	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/tr.list/test.tr.list.sh	2011-10-20 18:36:13 UTC (rev 48883)
@@ -12,14 +12,95 @@
 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 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="2 months"
-t.info type=strds dataset=precip_abs1
+n1=`g.tempfile pid=1 -d` 
+n2=`g.tempfile pid=2 -d`
+n3=`g.tempfile pid=3 -d`
+n4=`g.tempfile pid=4 -d`
+n5=`g.tempfile pid=5 -d`
 
+cat > $n1 << EOF
+prec_1
+prec_2
+prec_3
+prec_4
+prec_5
+prec_6
+EOF
+
+cat > $n2 << EOF
+prec_1|2001-01-01
+prec_2|2001-02-01
+prec_3|2001-03-01
+prec_4|2001-04-01
+prec_5|2001-05-01
+prec_6|2001-06-01
+EOF
+
+cat > $n3 << EOF
+prec_1|2001-01-01|2001-04-01
+prec_2|2001-05-01|2001-07-01
+prec_3|2001-08-01|2001-10-01
+prec_4|2001-11-01|2002-01-01
+prec_5|2002-02-01|2002-04-01
+prec_6|2002-05-01|2002-07-01
+EOF
+
+cat > $n4 << EOF
+prec_1|2001-01-01|2001-07-01
+prec_2|2001-02-01|2001-04-01
+prec_3|2001-03-01|2001-04-01
+prec_4|2001-04-01|2001-06-01
+prec_5|2001-05-01|2001-06-01
+prec_6|2001-06-01|2001-07-01
+EOF
+
+cat > $n5 << EOF
+prec_1|2001-01-01|2001-03-11
+prec_2|2001-02-01|2001-04-01
+prec_3|2001-03-01|2001-06-02
+prec_4|2001-04-01|2001-04-01
+prec_5|2001-05-01|2001-05-01
+prec_6|2001-06-01|2001-07-01
+EOF
+
+t.create --o type=strds temporaltype=absolute output=precip_abs1 title="A test with input files" descr="A test with input files"
+t.create --o type=strds temporaltype=absolute output=precip_abs2 title="A test with input files" descr="A test with input files"
+t.create --o type=strds temporaltype=absolute output=precip_abs3 title="A test with input files" descr="A test with input files"
+t.create --o type=strds temporaltype=absolute output=precip_abs4 title="A test with input files" descr="A test with input files"
+t.create --o type=strds temporaltype=absolute output=precip_abs5 title="A test with input files" descr="A test with input files"
+
 # The first @test
-tr.list -c input=precip_abs1
+tr.register    input=precip_abs1 file=$n1 start="2001-01-01" increment="1 months"
+tr.list    fs=" | " method=comma     input=precip_abs1
+tr.list -h input=precip_abs1
+tr.list -h fs=" | " method=cols      input=precip_abs1
+tr.list -h fs=" | " method=delta     input=precip_abs1
+tr.list -h fs=" | " method=deltagaps input=precip_abs1
 
-t.remove type=strds dataset=precip_abs1
-t.remove type=raster dataset=prec_1,prec_2,prec_3
-t.remove type=raster dataset=prec_4,prec_5,prec_6
+tr.register -i input=precip_abs2 file=$n2 start=file
+tr.list    fs=" | " method=comma     input=precip_abs2
+tr.list -h input=precip_abs2
+tr.list -h fs=" | " method=cols      input=precip_abs2
+tr.list -h fs=" | " method=delta     input=precip_abs2
+tr.list -h fs=" | " method=deltagaps input=precip_abs2
+
+tr.register -i input=precip_abs3 file=$n3 start=file end=file
+tr.list    fs=" | " method=comma     input=precip_abs3
+tr.list -h fs=" | " method=delta     input=precip_abs3
+tr.list -h fs=" | " method=deltagaps input=precip_abs3
+
+tr.register -i input=precip_abs4 file=$n4 start=file end=file
+tr.list    fs=" | " method=comma     input=precip_abs4
+tr.list -h fs=" | " method=delta     input=precip_abs4
+tr.list -h fs=" | " method=deltagaps input=precip_abs4
+
+tr.register -i input=precip_abs5 file=$n5 start=file end=file
+tr.list    fs=" | " method=comma     input=precip_abs5
+tr.list -h input=precip_abs5
+tr.list -h fs=" | " method=cols      input=precip_abs5
+tr.list -h fs=" | " method=delta     input=precip_abs5
+tr.list -h fs=" | " method=deltagaps input=precip_abs5
+
+t.remove type=rast input=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
+t.remove type=strds input=precip_abs1,precip_abs2,precip_abs3,precip_abs4,precip_abs5

Modified: grass/trunk/temporal/tr.list/tr.list.py
===================================================================
--- grass/trunk/temporal/tr.list/tr.list.py	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/tr.list/tr.list.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -5,7 +5,7 @@
 # MODULE:	tr.list
 # AUTHOR(S):	Soeren Gebbert
 #               
-# PURPOSE:	List registered maps of a spae time raster dataset 
+# PURPOSE:	List registered maps of a space time raster dataset 
 # COPYRIGHT:	(C) 2011 by the GRASS Development Team
 #
 #		This program is free software under the GNU General Public
@@ -15,7 +15,7 @@
 #############################################################################
 
 #%module
-#% description: List registered maps of a spae time raster dataset 
+#% description: List registered maps of a space time raster dataset 
 #% keywords: dataset
 #% keywords: spacetime
 #% keywords: raster
@@ -31,22 +31,22 @@
 #%end
 
 #%option
-#% key: sort
+#% key: order
 #% type: string
-#% description: Sort the space time dataset by category. Columns number_of_maps and granularity only available fpr space time datasets
+#% description: Order the space time dataset by category. 
 #% required: no
 #% multiple: yes
-#% options: id, name, creator, mapset, temporal_type, creation_time, start_time, end_time, north, south, west, east, nsres, ewres, cols, rows, number_of_cells, min, max 
+#% options: id,name,creator,mapset,temporal_type,creation_time,start_time,end_time,north,south,west,east,nsres,ewres,cols,rows,number_of_cells,min,max 
 #% answer: start_time
 #%end
 
 #%option
 #% key: columns
 #% type: string
-#% description: Which columns should be printed to stdout. Columns number_of_maps and granularity only available fpr space time datasets
+#% description: Select columns to be printed to stdout 
 #% required: no
 #% multiple: yes
-#% options: id, name, creator, mapset, temporal_type, creation_time, start_time, end_time, north, south, west, east, nsres, ewres, cols, rows, number_of_cells, min, max 
+#% options: id,name,creator,mapset,temporal_type,creation_time,start_time,end_time,north,south,west,east,nsres,ewres,cols,rows,number_of_cells,min,max 
 #% answer: name,mapset,start_time,end_time
 #%end
 
@@ -59,6 +59,16 @@
 #%end
 
 #%option
+#% key: method
+#% type: string
+#% description: Which method should be used fot data listing
+#% required: no
+#% multiple: no
+#% options: cols,comma,delta,deltagaps
+#% answer: cols
+#%end
+
+#%option
 #% key: fs
 #% type: string
 #% description: The field separator character between the columns, default is tabular "\t"
@@ -66,13 +76,11 @@
 #%end
 
 #%flag
-#% key: c
-#% description: Print the column names as first row
-#%end
+#% key: h
+#% description: Print column names 
 
 import grass.script as grass
 import grass.temporal as tgis
-
 ############################################################################
 
 def main():
@@ -80,10 +88,11 @@
     # Get the options
     input = options["input"]
     columns = options["columns"]
-    sort = options["sort"]
+    order = options["order"]
     where = options["where"]
     separator = options["fs"]
-    colhead = flags['c']
+    method = options["method"]
+    header = flags["h"]
 
     # Make sure the temporal database exists
     tgis.create_temporal_database()
@@ -98,45 +107,102 @@
     sp = tgis.space_time_raster_dataset(id)
     
     if sp.is_in_db() == False:
-        dbif.close()
         grass.fatal(_("Dataset <%s> not found in temporal database") % (id))
 
     sp.select()
 
-    rows = sp.get_registered_maps(columns, where, sort, None)
+    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)
 
-    # Print the query result to stout
-    if rows:
-        if separator == None or separator == "":
-            separator = "\t"
+        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
 
-        # Print the column names if requested
-        if colhead == True:
-            output = ""
-            count = 0
+        if maps and len(maps) > 0:
 
-            collist = columns.split(",")
+            first_time, dummy = maps[0].get_valid_time()
 
-            for key in collist:
-                if count > 0:
-                    output += separator + str(key)
+            for map in maps:
+                start, end = map.get_valid_time()
+                if end:
+                    delta = end -start
                 else:
-                    output += str(key)
-                count += 1
-            print output
+                    delta = None
+                delta_first = start - first_time
 
-        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
+                if map.is_time_absolute():
+                    if end:
+                        delta = tgis.time_delta_to_relative_time(delta)
+                    delta_first = tgis.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
+
 if __name__ == "__main__":
     options, flags = grass.parser()
     main()

Copied: grass/trunk/temporal/tr.register/ECAD_climate_analysis_1995_2010.py (from rev 48846, grass/trunk/temporal/tr.register/register_ECAD_maps_temp_mean_1995_2010.py)
===================================================================
--- grass/trunk/temporal/tr.register/ECAD_climate_analysis_1995_2010.py	                        (rev 0)
+++ grass/trunk/temporal/tr.register/ECAD_climate_analysis_1995_2010.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -0,0 +1,153 @@
+# This is an example script how to register imported ECA&D data for Europe
+# in the temporal database of grass setting valid time interval 
+
+import grass.script as grass
+
+# You need to download the european climate date from the ECA&D server http://eca.knmi.nl/ as netCDF time series
+# We only use the data from 1995 - 2010
+# First import the ECA&D data into GRASS GIS 
+input = "tg_0.25deg_reg_1995-2010_v4.0.nc"
+output = "temp_mean"
+grass.run_command("r.in.gdal", flags="o", input=input, output=output, overwrite=True)
+input = "tn_0.25deg_reg_1995-2010_v4.0.nc"
+output = "temp_min"
+grass.run_command("r.in.gdal", flags="o", input=input, output=output, overwrite=True)
+input = "tx_0.25deg_reg_1995-2010_v4.0.nc"
+output = "temp_max"
+grass.run_command("r.in.gdal", flags="o", input=input, output=output, overwrite=True)
+input = "rr_0.25deg_reg_1995-2010_v4.0.nc"
+output = "precip"
+grass.run_command("r.in.gdal", flags="o", input=input, output=output, overwrite=True)
+
+
+# This should be the number of maps to register
+num_maps = 5844
+
+# Daily mean temperatue
+
+dataset = "temp_mean_1995_2010_daily"
+
+grass.run_command("t.create", type="strds", output=dataset,\
+                  semantic="continuous", temporal="absolute", \
+		  title="European mean temperature 1995-2010", \
+		  description="The european daily mean temperature 1995 - 2010 from ECA&D ", \
+		  overwrite=True)
+
+name = "temp_mean."
+filename = grass.tempfile()
+file = open(filename, "w")
+for i in range(num_maps):
+    inc = i + 1
+    map_name = name + str(inc)
+    string = map_name + "\n"
+    file.write(string)
+
+file.close()
+
+grass.run_command("tr.register", flags="i", input=dataset, file=filename, start="1995-01-01", increment="1 days", overwrite=True)
+
+# Daily min temperatue
+
+dataset = "temp_min_1995_2010_daily"
+
+grass.run_command("t.create", type="strds", output=dataset,\
+                  semantic="continuous", temporal="absolute", \
+		  title="European min temperature 1995-2010", \
+		  description="The european daily min temperature 1995 - 2010 from ECA&D ", \
+		  overwrite=True)
+
+name = "temp_min."
+filename = grass.tempfile()
+file = open(filename, "w")
+for i in range(num_maps):
+    inc = i + 1
+    map_name = name + str(inc)
+    string = map_name + "\n"
+    file.write(string)
+
+file.close()
+
+grass.run_command("tr.register", flags="i", input=dataset, file=filename, start="1995-01-01", increment="1 days", overwrite=True)
+
+# Daily max temperatue
+
+dataset = "temp_max_1995_2010_daily"
+
+grass.run_command("t.create", type="strds", output=dataset,\
+                  semantic="continuous", temporal="absolute", \
+		  title="European max temperature 1995-2010", \
+		  description="The european daily max temperature 1995 - 2010 from ECA&D ", \
+		  overwrite=True)
+
+name = "temp_max."
+filename = grass.tempfile()
+file = open(filename, "w")
+for i in range(num_maps):
+    inc = i + 1
+    map_name = name + str(inc)
+    string = map_name + "\n"
+    file.write(string)
+
+file.close()
+
+grass.run_command("tr.register", flags="i", input=dataset, file=filename, start="1995-01-01", increment="1 days", overwrite=True)
+
+# Daily precipitation
+
+dataset = "precipitation_1995_2010_daily"
+
+grass.run_command("t.create", type="strds", output=dataset,\
+                  semantic="event", temporal="absolute", \
+		  title="European precipitation 1995-2010", \
+		  description="The european daily precipitation 1995 - 2010 from ECA&D ", \
+		  overwrite=True)
+
+name = "precip."
+filename = grass.tempfile()
+file = open(filename, "w")
+for i in range(num_maps):
+    inc = i + 1
+    map_name = name + str(inc)
+    string = map_name + "\n"
+    file.write(string)
+
+file.close()
+
+grass.run_command("tr.register", flags="i", input=dataset, file=filename, start="1995-01-01", increment="1 days", overwrite=True)
+
+# Now aggregate the data
+
+grass.run_command("g.region", rast="precip.1", flags="p")
+
+input = "temp_mean_1995_2010_daily"
+output = "temp_mean_1995_2010_monthly"
+basename = "temp_mean_month"
+grass.run_command("tr.aggregate", input=input, method="average", output=output, base=basename, granularity="1 months", overwrite=True)
+output = "temp_mean_1995_2010_three_monthly"
+basename = "temp_mean_three_month"
+grass.run_command("tr.aggregate", input=input, method="average", output=output, base=basename, granularity="3 months", overwrite=True)
+
+input = "temp_min_1995_2010_daily"
+output = "temp_min_1995_2010_monthly"
+basename = "temp_min_month"
+grass.run_command("tr.aggregate", input=input, method="minimum", output=output, base=basename, granularity="1 months", overwrite=True)
+output = "temp_min_1995_2010_three_monthly"
+basename = "temp_min_three_month"
+grass.run_command("tr.aggregate", input=input, method="minimum", output=output, base=basename, granularity="3 months", overwrite=True)
+
+input = "temp_max_1995_2010_daily"
+output = "temp_max_1995_2010_monthly"
+basename = "temp_max_month"
+grass.run_command("tr.aggregate", input=input, method="maximum", output=output, base=basename, granularity="1 months", overwrite=True)
+output = "temp_max_1995_2010_three_monthly"
+basename = "temp_max_three_month"
+grass.run_command("tr.aggregate", input=input, method="maximum", output=output, base=basename, granularity="3 months", overwrite=True)
+
+input = "precipitation_1995_2010_daily"
+output = "precipitation_1995_2010_monthly"
+basename = "precip_month"
+grass.run_command("tr.aggregate", input=input, method="sum", output=output, base=basename, granularity="1 months", overwrite=True)
+output = "precipitation_1995_2010_three_monthly"
+basename = "precip_three_month"
+grass.run_command("tr.aggregate", input=input, method="sum", output=output, base=basename, granularity="3 months", overwrite=True)
+

Deleted: 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-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/tr.register/register_ECAD_maps_temp_mean_1995_2010.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -1,28 +0,0 @@
-# This is an example script how to register imported ECA&D temperature data for Europe
-# in the temporal database of grass assigning valid time 
-
-import grass.script as grass
-
-# Create the space time raster dataset with t.create
-dataset = "temp_mean_1995_2010_daily"
-#dataset = "temp_mean_1995_1996_daily"
-
-grass.run_command("t.create", type="strds", output=dataset, granularity="1 days", \
-                  semantic="continuous", temporal="absolute", \
-		  title="European mean temperature 1995-2010", \
-		  description="The european daily mean temperature 1995 - 2010 from ECA&D ", \
-		  overwrite=True)
-
-name = "temp_mean."
-maps=""
-for i in range(5844):
-#for i in range(365):
-    inc = i + 1
-    map_name = name + str(inc)
-    if i == 0:
-        maps += map_name
-    else:
-        maps += "," + map_name
-    
-# Register all maps at once
-grass.run_command("tr.register", flags="i", input=dataset, maps=maps, start="1995-01-01", increment="1 days", overwrite=True)

Modified: grass/trunk/temporal/tr.series/test.tr.series.sh
===================================================================
--- grass/trunk/temporal/tr.series/test.tr.series.sh	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/tr.series/test.tr.series.sh	2011-10-20 18:36:13 UTC (rev 48883)
@@ -25,7 +25,7 @@
 tr.register --v input=precip_abs maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6 start="2001-01-01" increment="1 months"
 
 tr.series --o input=precip_abs method=average output=prec_average where="start_time > '2001-03-01'"
-tr.series --o -t input=precip_abs method=maximum output=prec_max sort=start_time
+tr.series --o -t input=precip_abs method=maximum output=prec_max order=start_time
 tr.series --o -t input=precip_abs method=sum output=prec_sum
 
 t.remove --v type=rast input=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6

Modified: grass/trunk/temporal/tr.series/tr.series.py
===================================================================
--- grass/trunk/temporal/tr.series/tr.series.py	2011-10-20 18:28:19 UTC (rev 48882)
+++ grass/trunk/temporal/tr.series/tr.series.py	2011-10-20 18:36:13 UTC (rev 48883)
@@ -40,7 +40,7 @@
 #%end
 
 #%option
-#% key: sort
+#% key: order
 #% type: string
 #% description: Sort the maps by category.
 #% required: no
@@ -71,7 +71,7 @@
     input = options["input"]
     output = options["output"]
     method = options["method"]
-    sort = options["sort"]
+    order = options["order"]
     where = options["where"]
     add_time = flags["t"]
 
@@ -91,7 +91,7 @@
 
     sp.select()
 
-    rows = sp.get_registered_maps("id", where, sort, None)
+    rows = sp.get_registered_maps("id", where, order, None)
 
     if rows:
         # Create the r.series input file



More information about the grass-commit mailing list