[GRASS-SVN] r58575 - grass/trunk/lib/python/temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jan 1 11:54:09 PST 2014


Author: huhabla
Date: 2014-01-01 11:54:09 -0800 (Wed, 01 Jan 2014)
New Revision: 58575

Modified:
   grass/trunk/lib/python/temporal/c_libraries_interface.py
   grass/trunk/lib/python/temporal/register.py
   grass/trunk/lib/python/temporal/unittests_register.py
Log:
Fixed relative time converstion bug in the C-interface, fixed unit request in the register function, added more register unittests.


Modified: grass/trunk/lib/python/temporal/c_libraries_interface.py
===================================================================
--- grass/trunk/lib/python/temporal/c_libraries_interface.py	2014-01-01 19:09:18 UTC (rev 58574)
+++ grass/trunk/lib/python/temporal/c_libraries_interface.py	2014-01-01 19:54:09 UTC (rev 58575)
@@ -514,8 +514,7 @@
                             byref(dt1),
                             byref(dt2),
                             byref(count))
-
-
+    
     if dt1.mode == libdate.DATETIME_ABSOLUTE:
         pdt1 = None
         pdt2 = None
@@ -535,7 +534,7 @@
         unit = None
         start = None
         end = None
-        if count >= 1:
+        if count.value >= 1:
             if dt1.year > 0:
                 unit = "years"
                 start = dt1.year
@@ -554,7 +553,7 @@
             elif dt1.second > 0:
                 unit = "seconds"
                 start = dt1.second
-        if count == 2:
+        if count.value == 2:
             if dt2.year > 0:
                 end = dt2.year
             elif dt2.month > 0:

Modified: grass/trunk/lib/python/temporal/register.py
===================================================================
--- grass/trunk/lib/python/temporal/register.py	2014-01-01 19:09:18 UTC (rev 58574)
+++ grass/trunk/lib/python/temporal/register.py	2014-01-01 19:54:09 UTC (rev 58575)
@@ -97,7 +97,7 @@
     if name:
         sp = open_old_space_time_dataset(name, type, dbif)
 
-        if sp.is_time_relative() and not unit:
+        if sp.is_time_relative() and (start or end) and not unit:
             dbif.close()
             msgr.fatal(_("Space time %(sp)s dataset <%(name)s> with relative"
                          " time found, but no relative unit set for %(sp)s "

Modified: grass/trunk/lib/python/temporal/unittests_register.py
===================================================================
--- grass/trunk/lib/python/temporal/unittests_register.py	2014-01-01 19:09:18 UTC (rev 58574)
+++ grass/trunk/lib/python/temporal/unittests_register.py	2014-01-01 19:54:09 UTC (rev 58575)
@@ -35,17 +35,18 @@
         ret += grass.run_command("r.mapcalc", overwrite=True, quiet=True, 
                           expression="register_map_2 = 2")
         self.assertEqual(ret, 0)
+        
+        
+        self.strds_abs = tgis.open_new_space_time_dataset(name="register_test_abs", type="strds", temporaltype="absolute", 
+                                            title="Test strds", descr="Test strds", semantic="field")
+        self.strds_rel = tgis.open_new_space_time_dataset(name="register_test_rel", type="strds", temporaltype="relative", 
+                                            title="Test strds", descr="Test strds", semantic="field")
 
-    def test_absolute_time_strds(self):
+    def test_absolute_time_strds_1(self):
         """!Test the registration of maps with absolute time in a
            space time raster dataset
         """
-        ret = grass.run_command("t.create", output="register_test", 
-                                title="Test strds", description="Test strds",
-                                temporaltype="absolute")
-        self.assertEqual(ret, 0)
-
-        tgis.register_maps_in_space_time_dataset(type="rast", name="register_test", 
+        tgis.register_maps_in_space_time_dataset(type="rast", name=self.strds_abs.get_name(), 
                  maps="register_map_1,register_map_2",
                  start="2001-01-01", increment="1 day", interval=True)
 
@@ -61,15 +62,46 @@
         self.assertEqual(start, datetime.datetime(2001, 1, 2))
         self.assertEqual(end, datetime.datetime(2001, 1, 3))
 
-        strds = tgis.SpaceTimeRasterDataset("register_test@" + tgis.get_current_mapset())
-        strds.select()
-        start, end = strds.get_absolute_time()
+        self.strds_abs.select()
+        start, end = self.strds_abs.get_absolute_time()
         self.assertEqual(start, datetime.datetime(2001, 1, 1))
         self.assertEqual(end, datetime.datetime(2001, 1, 3))
 
-        ret = grass.run_command("t.remove", input="register_test") 
-        self.assertEqual(ret, 0)
+        self.strds_abs.print_info()
 
+    def test_absolute_time_strds_2(self):
+        """!Test the registration of maps with absolute time in a
+           space time raster dataset.
+           The timestamps are set using the C-Interface beforehand, so that the register function needs
+           to read the timetsamp from the map metadata.
+        """
+
+        ciface = tgis.get_tgis_c_library_interface()
+        ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1 Jan 2001/2 Jan 2001")
+        ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "2 Jan 2001/3 Jan 2001")
+
+        tgis.register_maps_in_space_time_dataset(type="rast", name=self.strds_abs.get_name(), 
+                                                 maps="register_map_1,register_map_2")
+
+        map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 2))
+
+        map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 2))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+        self.strds_abs.select()
+        start, end = self.strds_abs.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1))
+        self.assertEqual(end, datetime.datetime(2001, 1, 3))
+
+        self.strds_abs.print_info()
+
     def test_absolute_time_1(self):
         """!Test the registration of maps with absolute time
         """
@@ -106,20 +138,38 @@
         start, end = map.get_absolute_time()
         self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
 
-    def test_relative_time_strds(self):
+    def test_absolute_time_3(self):
+        """!Test the registration of maps with absolute time. 
+           The timestamps are set using the C-Interface beforehand, so that the register function needs
+           to read the timetsamp from the map metadata.
+        """
+                 
+        ciface = tgis.get_tgis_c_library_interface()
+        ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1 Jan 2001 10:30:01")
+        ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "1 Jan 2001 18:30:01")
+
+        tgis.register_maps_in_space_time_dataset(type="rast", name=None, 
+                 maps="register_map_1,register_map_2")
+                 
+        map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1))
+
+        map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+        map.select()
+        start, end = map.get_absolute_time()
+        self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
+
+    def test_relative_time_strds_1(self):
         """!Test the registration of maps with relative time in a
            space time raster dataset
         """
-        ret = grass.run_command("t.create", output="register_test", 
-                                title="Test strds", description="Test strds",
-                                temporaltype="relative")
-        self.assertEqual(ret, 0)
 
+        tgis.register_maps_in_space_time_dataset(type="rast", name=self.strds_rel.get_name(), 
+                                                 maps="register_map_1,register_map_2", start=0, 
+                                                 increment=1, unit="day", interval=True)
 
-        tgis.register_maps_in_space_time_dataset(type="rast", name="register_test", 
-                 maps="register_map_1,register_map_2",
-                 start=0, increment=1, unit="day", interval=True)
-
         map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
         map.select()
         start, end, unit = map.get_relative_time()
@@ -134,18 +184,48 @@
         self.assertEqual(end, 2)
         self.assertEqual(unit, "day")
 
-        strds = tgis.SpaceTimeRasterDataset("register_test@" + tgis.get_current_mapset())
-        strds.select()
-        start, end, unit = strds.get_relative_time()
+        self.strds_rel.select()
+        start, end, unit = self.strds_rel.get_relative_time()
         self.assertEqual(start, 0)
         self.assertEqual(end, 2)
         self.assertEqual(unit, "day")
         
-        strds.print_info()
+        self.strds_rel.print_info()
 
-        ret = grass.run_command("t.remove", input="register_test") 
-        self.assertEqual(ret, 0)
+    def test_relative_time_strds_2(self):
+        """!Test the registration of maps with relative time in a
+           space time raster dataset. The timetsamps are set for the maps using the 
+           C-interface before registration.
+        """
+        ciface = tgis.get_tgis_c_library_interface()
+        ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1000000 seconds/1500000 seconds")
+        ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "1500000 seconds/2000000 seconds")
 
+        tgis.register_maps_in_space_time_dataset(type="rast", name=self.strds_rel.get_name(), 
+                                                 maps="register_map_1,register_map_2")
+
+        map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+        map.select()
+        start, end, unit = map.get_relative_time()
+        self.assertEqual(start, 1000000)
+        self.assertEqual(end, 1500000)
+        self.assertEqual(unit, "seconds")
+
+        map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+        map.select()
+        start, end, unit = map.get_relative_time()
+        self.assertEqual(start, 1500000)
+        self.assertEqual(end, 2000000)
+        self.assertEqual(unit, "seconds")
+
+        self.strds_rel.select()
+        start, end, unit = self.strds_rel.get_relative_time()
+        self.assertEqual(start, 1000000)
+        self.assertEqual(end, 2000000)
+        self.assertEqual(unit, "seconds")
+        
+        self.strds_rel.print_info()
+        
     def test_relative_time_1(self):
         """!Test the registration of maps with relative time
         """
@@ -172,27 +252,55 @@
         """
         tgis.register_maps_in_space_time_dataset(type="rast", name=None, 
                  maps="register_map_1,register_map_2",
-                 start=1000000, increment=500000, unit="second", interval=True)
+                 start=1000000, increment=500000, unit="seconds", interval=True)
 
         map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
         map.select()
         start, end, unit = map.get_relative_time()
         self.assertEqual(start, 1000000)
         self.assertEqual(end, 1500000)
-        self.assertEqual(unit, "second")
+        self.assertEqual(unit, "seconds")
 
         map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
         map.select()
         start, end, unit = map.get_relative_time()
         self.assertEqual(start, 1500000)
         self.assertEqual(end, 2000000)
-        self.assertEqual(unit, "second")
+        self.assertEqual(unit, "seconds")
 
+    def test_relative_time_3(self):
+        """!Test the registration of maps with relative time. The timetsamps are set beforhand using
+           the C-interface.
+        """
+        ciface = tgis.get_tgis_c_library_interface()
+        ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1000000 seconds/1500000 seconds")
+        ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "1500000 seconds/2000000 seconds")
+        
+        tgis.register_maps_in_space_time_dataset(type="rast", name=None, 
+                 maps="register_map_1,register_map_2")
+
+        map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset())
+        map.select()
+        start, end, unit = map.get_relative_time()
+        self.assertEqual(start, 1000000)
+        self.assertEqual(end, 1500000)
+        self.assertEqual(unit, "seconds")
+
+        map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset())
+        map.select()
+        start, end, unit = map.get_relative_time()
+        self.assertEqual(start, 1500000)
+        self.assertEqual(end, 2000000)
+        self.assertEqual(unit, "seconds")
+
     def tearDown(self):
         """!Remove maps from temporal database
         """
-        ret = grass.run_command("t.unregister", maps="register_map_1,register_map_2")
+        ret = grass.run_command("t.unregister", maps="register_map_1,register_map_2", quiet=True)
+        ret = grass.run_command("g.remove", rast="register_map_1,register_map_2", quiet=True)
         self.assertEqual(ret, 0)
+        self.strds_abs.delete()
+        self.strds_rel.delete()
 
     @classmethod
     def tearDownClass(cls):



More information about the grass-commit mailing list