[GRASS-SVN] r66334 - in grass-addons/grass7: . temporal temporal/t.rast.whatcsv temporal/t.rast.whatcsv/testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 25 08:39:47 PDT 2015


Author: neteler
Date: 2015-09-25 08:39:47 -0700 (Fri, 25 Sep 2015)
New Revision: 66334

Added:
   grass-addons/grass7/temporal/
   grass-addons/grass7/temporal/Makefile
   grass-addons/grass7/temporal/t.rast.whatcsv/
   grass-addons/grass7/temporal/t.rast.whatcsv/Makefile
   grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.html
   grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.py
   grass-addons/grass7/temporal/t.rast.whatcsv/testsuite/
   grass-addons/grass7/temporal/t.rast.whatcsv/testsuite/test_whatcsv.py
Modified:
   grass-addons/grass7/Makefile
Log:
t.rast.whatcsv Addon: new prototype module by Soeren Gebbert

Modified: grass-addons/grass7/Makefile
===================================================================
--- grass-addons/grass7/Makefile	2015-09-25 13:59:13 UTC (rev 66333)
+++ grass-addons/grass7/Makefile	2015-09-25 15:39:47 UTC (rev 66334)
@@ -7,7 +7,8 @@
 	general \
 	imagery \
 	raster \
-	vector
+	vector \
+	temporal
 
 include $(MODULE_TOPDIR)/include/Make/Dir.make
 

Added: grass-addons/grass7/temporal/Makefile
===================================================================
--- grass-addons/grass7/temporal/Makefile	                        (rev 0)
+++ grass-addons/grass7/temporal/Makefile	2015-09-25 15:39:47 UTC (rev 66334)
@@ -0,0 +1,8 @@
+MODULE_TOPDIR = ..
+
+SUBDIRS = \
+	t.rast.whatcsv
+
+include $(MODULE_TOPDIR)/include/Make/Dir.make
+
+default: subdirs


Property changes on: grass-addons/grass7/temporal/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/grass7/temporal/t.rast.whatcsv/Makefile
===================================================================
--- grass-addons/grass7/temporal/t.rast.whatcsv/Makefile	                        (rev 0)
+++ grass-addons/grass7/temporal/t.rast.whatcsv/Makefile	2015-09-25 15:39:47 UTC (rev 66334)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = t.rast.whatcsv
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)


Property changes on: grass-addons/grass7/temporal/t.rast.whatcsv/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.html
===================================================================
--- grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.html	                        (rev 0)
+++ grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.html	2015-09-25 15:39:47 UTC (rev 66334)
@@ -0,0 +1,133 @@
+<h2>DESCRIPTION</h2>
+
+<em>t.rast.what</em> is designed to sample space time raster datasets
+at specific point coordinates using <a href="r.what.html">r.what</a> internally. 
+The output of <a href="r.what.html">r.what</a> 
+is transformed to different output layouts.
+The output layouts can be specified using the <em>layout</em> option.
+<p>
+Three layouts can be specified:
+<ul>
+    <li><em>row</em> - Row order, one vector sample point value per row</li>
+    <li><em>col</em> - Column order, create a column for each vector sample point of a single time step/raster layer</li>
+    <li><em>timerow</em> - Time order, create a column for each time step, this order is the original r.what output, except that the column names are the time stamps</li>
+</ul>
+
+Please have a look at the example to see the supported layouts.
+<p>
+This module is designed to run several instances of r.what to sample
+subsets of a space time raster dataset in parallel. Several intermediate
+text files will be created that are merged into a single file at the
+end of the processing.
+<p>
+Coordinates can be provided as vector map using the <em>points</em> option
+or as comma separated coordinate list with the <em>coordinates </em>option.
+<p>
+An output file can be specified using the <em>output</em> option. 
+Stdout will be used if no output is specified or if the 
+<em>output</em> option is set to "-".
+    
+<h2>EXAMPLES</h2>
+
+<h3>Data preparation</h3>
+In the following examples we sample a space time raster dataset that contains
+4 raster map layers. First we create the STRDS that will be sampled with t.rast.what.
+
+<div class="code"><pre>
+g.region s=0 n=80 w=0 e=120 b=0 t=50 res=10
+
+# Generate data
+r.mapcalc expression="a_1 = 1" -s
+r.mapcalc expression="a_2 = 2" -s
+r.mapcalc expression="a_3 = 3" -s
+r.mapcalc expression="a_4 = 4" -s
+
+t.create type=strds output=A title="A test" descr="A test"
+
+t.register -i type=raster input=A maps=a_1,a_2,a_3,a_4 \
+    start='1990-01-01' increment="1 month"
+</pre></div>
+
+<h3>Example 1</h3>
+
+The first approach uses text coordinates as input and stdout as output,
+the layout is one coordinate(point per column:
+
+
+<div class="code"><pre>
+t.rast.what strds=A coordinates="115,36,79,45" layout=col -n
+
+start|end|115.0000000000;36.0000000000|79.0000000000;45.0000000000
+1990-01-01 00:00:00|1990-02-01 00:00:00|1|1
+1990-02-01 00:00:00|1990-03-01 00:00:00|2|2
+1990-03-01 00:00:00|1990-04-01 00:00:00|3|3
+1990-04-01 00:00:00|1990-05-01 00:00:00|4|4
+</pre></div>
+
+<h3>Example 2</h3>
+
+A vector map layer can be used as input to sample the STRDS. All
+three available layouts are demonstrated using the vector map for sampling.
+
+<div class="code"><pre>
+# First create the vector map layer based on random points
+v.random output=points n=3 seed=1
+
+# Row layout using a text file as output
+t.rast.what strds=A points=points output=result.txt layout=row -n
+
+cat result.txt
+
+115.0043586274|36.3593955783|1990-01-01 00:00:00|1990-02-01 00:00:00|1
+115.0043586274|36.3593955783|1990-02-01 00:00:00|1990-03-01 00:00:00|2
+115.0043586274|36.3593955783|1990-03-01 00:00:00|1990-04-01 00:00:00|3
+115.0043586274|36.3593955783|1990-04-01 00:00:00|1990-05-01 00:00:00|4
+79.6816763826|45.2391522853|1990-01-01 00:00:00|1990-02-01 00:00:00|1
+79.6816763826|45.2391522853|1990-02-01 00:00:00|1990-03-01 00:00:00|2
+79.6816763826|45.2391522853|1990-03-01 00:00:00|1990-04-01 00:00:00|3
+79.6816763826|45.2391522853|1990-04-01 00:00:00|1990-05-01 00:00:00|4
+97.4892579600|79.2347263950|1990-01-01 00:00:00|1990-02-01 00:00:00|1
+97.4892579600|79.2347263950|1990-02-01 00:00:00|1990-03-01 00:00:00|2
+97.4892579600|79.2347263950|1990-03-01 00:00:00|1990-04-01 00:00:00|3
+97.4892579600|79.2347263950|1990-04-01 00:00:00|1990-05-01 00:00:00|4
+
+
+# Column layout order using stdout as output
+t.rast.what strds=A points=points layout=col -n
+
+start|end|115.0043586274;36.3593955783|79.6816763826;45.2391522853|97.4892579600;79.2347263950
+1990-01-01 00:00:00|1990-02-01 00:00:00|1|1|1
+1990-02-01 00:00:00|1990-03-01 00:00:00|2|2|2
+1990-03-01 00:00:00|1990-04-01 00:00:00|3|3|3
+1990-04-01 00:00:00|1990-05-01 00:00:00|4|4|4
+
+# Timerow layout, one time series per row 
+# using the where statement to select a subset of the STRDS
+# and stdout as output
+t.rast.what strds=A points=points \
+    where="start_time >= '1990-03-01'" layout=timerow -n
+
+x|y|1990-03-01 00:00:00;1990-04-01 00:00:00|1990-04-01 00:00:00;1990-05-01 00:00:00
+115.004358627375|36.3593955782903|3|4
+79.681676382576|45.2391522852909|3|4
+97.4892579600048|79.2347263950131|3|4
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.what.html">r.what</a> ,
+<a href="r.neighbors.html">r.neighbors</a>,
+<a href="t.rast.aggregate.ds.html">t.rast.aggregate.ds</a>,
+<a href="t.rast.extract.html">t.rast.extract</a>,
+<a href="t.info.html">t.info</a>,
+<a href="g.region.html">g.region</a>,
+<a href="r.mask.html">r.mask</a>
+</em>
+
+
+<h2>AUTHOR</h2>
+
+Sören Gebbert, Thünen Institute of Climate-Smart Agriculture
+
+<p><i>Last changed: $Date$</i>


Property changes on: grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.py
===================================================================
--- grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.py	                        (rev 0)
+++ grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.py	2015-09-25 15:39:47 UTC (rev 66334)
@@ -0,0 +1,158 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:       t.rast.whatcsv
+# AUTHOR(S):    Soeren Gebbert
+#
+# PURPOSE:      Sample a space time raster dataset at specific vector point
+#               coordinates and write the output to stdout using different
+#               layouts
+#
+# COPYRIGHT:    (C) 2015 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: Sample a space time raster dataset at specific space-time point coordinates from a csv file and write the output to stdout
+#% keyword: temporal
+#% keyword: raster
+#% keyword: sampling
+#% keyword: time
+#%end
+
+#%option G_OPT_F_INPUT
+#% key: csv
+#% description: Name for the output input csv file
+#%end
+
+#%option G_OPT_STRDS_INPUT
+#% key: strds
+#%end
+
+#%option G_OPT_F_OUTPUT
+#% required: no
+#% description: Name for the output file or "-" in case stdout should be used
+#% answer: -
+#%end
+
+#%option G_OPT_T_WHERE
+#%end
+
+#%option G_OPT_M_NULL_VALUE
+#%end
+
+#%option G_OPT_F_SEP
+#%end
+
+#%option
+#% key: skip
+#% type: integer
+#% description: Number of header lines to skip in the csv file
+#% required: yes
+#%end
+
+#%flag
+#% key: n
+#% description: Output header row
+#%end
+
+## Temporary disabled the r.what flags due to test issues
+##%flag
+##% key: f
+##% description: Show the category labels of the grid cell(s)
+##%end
+
+##%flag
+##% key: r
+##% description: Output color values as RRR:GGG:BBB
+##%end
+
+##%flag
+##% key: i
+##% description: Output integer category values, not cell values
+##%end
+
+import sys
+import copy
+import csv
+import grass.script as gscript
+import grass.temporal as tgis
+import grass.pygrass.modules as pymod
+from grass.gunittest.gmodules import SimpleModule
+
+
+############################################################################
+
+def main(options, flags):
+
+    # Get the options
+    csv_file = options["csv"]
+    strds = options["strds"]
+    output = options["output"]
+    where = options["where"]
+    null_value = options["null_value"]
+    separator = options["separator"]
+
+    write_header = flags["n"]
+
+    #output_cat_label = flags["f"]
+    #output_color = flags["r"]
+    #output_cat = flags["i"]
+
+    overwrite = gscript.overwrite()
+
+    # Make sure the temporal database exists
+    tgis.init()
+    # We need a database interface
+    dbif = tgis.SQLDatabaseInterfaceConnection()
+    dbif.connect()
+
+    sp = tgis.open_old_stds(strds, "strds", dbif)
+
+    # Setup separator
+    if separator == "pipe":
+        separator = "|"
+    if separator == "comma":
+        separator = ","
+    if separator == "space":
+        separator = " "
+    if separator == "tab":
+        separator = "\t"
+    if separator == "newline":
+        separator = "\n"
+
+
+    r_what = SimpleModule("r.what", map="dummy",
+                                    output="-",
+                                    separator=separator,
+                                    quiet=True)
+
+    reader = csv.reader(open(csv_file, "r"), delimiter=separator)
+
+    for line in reader:
+        id_, x, y, timestamp = line
+
+        start = tgis.string_to_datetime(timestamp)
+        where = "start_time <= \'" + str(start) + "\' AND end_time > \'" + str(start) + "\'"
+        rows = sp.get_registered_maps(columns="id", where=where,
+                                      dbif=dbif)
+        for entry in rows:
+            r_what.inputs.map = entry[0]
+            r_what.inputs.coordinates = [x,y]
+            r_what.run()
+            out = "%s%s%s"%(id_, separator, r_what.outputs.stdout)
+
+            sys.stdout.write(out)
+
+    dbif.close()
+
+
+
+if __name__ == "__main__":
+    options, flags = gscript.parser()
+    main(options, flags)


Property changes on: grass-addons/grass7/temporal/t.rast.whatcsv/t.rast.whatcsv.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native

Added: grass-addons/grass7/temporal/t.rast.whatcsv/testsuite/test_whatcsv.py
===================================================================
--- grass-addons/grass7/temporal/t.rast.whatcsv/testsuite/test_whatcsv.py	                        (rev 0)
+++ grass-addons/grass7/temporal/t.rast.whatcsv/testsuite/test_whatcsv.py	2015-09-25 15:39:47 UTC (rev 66334)
@@ -0,0 +1,66 @@
+"""Test t.rast.whatcsv
+
+(C) 2014 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+ at author Soeren Gebbert
+"""
+
+from grass.gunittest.case import TestCase
+from grass.gunittest.gmodules import SimpleModule
+
+class TestRasterWhatCSV(TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        cls.use_temp_region()
+        cls.runModule("g.region",  s=0,  n=80,  w=0,  e=120,  b=0,  t=50,  res=10,  res3=10)
+
+        cls.runModule("r.mapcalc", expression="a_1 = 100",  overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_2 = 200",  overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_3 = 300",  overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_4 = 400",  overwrite=True)
+
+        csv_file = open("test.csv", "w")
+        csv_file.write("1|115.0043586274|36.3593955783|2001-01-01 00:00:00\n")
+        csv_file.write("2|79.6816763826|45.2391522853|2001-04-01 00:00:00\n")
+        csv_file.write("3|97.4892579600|79.2347263950|2001-07-01 00:00:00\n")
+        csv_file.write("4|115.0043586274|36.3593955783|2001-11-01 00:00:00\n")
+        csv_file.close()
+
+        cls.runModule("t.create",  type="strds",  temporaltype="absolute",
+                                 output="A",  title="A test",  description="A test",
+                                 overwrite=True)
+        cls.runModule("t.register",  flags="i",  type="raster",  input="A",
+                                     maps="a_1,a_2,a_3,a_4",  start="2001-01-01",
+                                     increment="3 months",  overwrite=True)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the temporary region
+        """
+        cls.runModule("t.remove",  flags="rf",  type="strds",
+                                   inputs="A")
+        cls.del_temp_region()
+
+    def test_1t(self):
+
+        t_rast_whatcsv = SimpleModule("t.rast.whatcsv",  strds="A",
+                                      csv="test.csv", overwrite=True,
+                                      skip=0, verbose=True)
+        self.assertModule(t_rast_whatcsv)
+
+        text="""1|115.0043586274|36.3593955783||100
+2|79.6816763826|45.2391522853||200
+3|97.4892579600|79.2347263950||300
+4|115.0043586274|36.3593955783||400
+"""
+        self.assertLooksLike(text,  t_rast_whatcsv.outputs.stdout)
+
+if __name__ == '__main__':
+    from grass.gunittest.main import test
+    test()


Property changes on: grass-addons/grass7/temporal/t.rast.whatcsv/testsuite/test_whatcsv.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list