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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Apr 17 14:19:53 PDT 2013


Author: huhabla
Date: 2013-04-17 14:19:53 -0700 (Wed, 17 Apr 2013)
New Revision: 55857

Modified:
   grass/trunk/lib/python/temporal/abstract_dataset.py
   grass/trunk/lib/python/temporal/abstract_map_dataset.py
   grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
   grass/trunk/lib/python/temporal/abstract_temporal_dataset.py
   grass/trunk/lib/python/temporal/base.py
   grass/trunk/lib/python/temporal/core.py
   grass/trunk/lib/python/temporal/datetime_math.py
   grass/trunk/lib/python/temporal/metadata.py
   grass/trunk/lib/python/temporal/space_time_datasets.py
   grass/trunk/lib/python/temporal/spatial_extent.py
   grass/trunk/lib/python/temporal/temporal_extent.py
   grass/trunk/lib/python/temporal/temporal_granularity.py
   grass/trunk/lib/python/temporal/temporal_relationships.py
   grass/trunk/lib/python/temporal/unit_tests.py
Log:
Fixed granularity computation.
Added and fixed many doc-tests and unit tests.


Modified: grass/trunk/lib/python/temporal/abstract_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_dataset.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/abstract_dataset.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -455,4 +455,4 @@
         
 if __name__ == "__main__":
     import doctest
-    doctest.testmod()
\ No newline at end of file
+    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/abstract_map_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_map_dataset.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/abstract_map_dataset.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -332,6 +332,8 @@
         self.absolute_time.set_start_time(start_time)
         self.absolute_time.set_end_time(end_time)
         self.absolute_time.set_timezone(timezone)
+        
+        return True
 
     def update_absolute_time(self, start_time, end_time=None, 
                              timezone=None, dbif=None):
@@ -361,9 +363,9 @@
            @param start_time: A double value
            @param end_time: A double value
            @param unit: The unit of the relative time. Supported units: 
-                        years, months, days, hours, minutes, seconds
+                        year(s), month(s), day(s), hour(s), minute(s), second(s)
 
-           Return True for success and False otherwise
+           @return True for success and False otherwise
 
         """
 
@@ -612,9 +614,3 @@
             dbif.close()
 
         return rows
-
-###############################################################################
-
-if __name__ == "__main__":
-    import doctest
-    doctest.testmod()
\ No newline at end of file

Modified: grass/trunk/lib/python/temporal/abstract_space_time_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/abstract_space_time_dataset.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -1683,9 +1683,3 @@
 
         if connected:
             dbif.close()
-
-###############################################################################
-
-if __name__ == "__main__":
-    import doctest
-    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/abstract_temporal_dataset.py
===================================================================
--- grass/trunk/lib/python/temporal/abstract_temporal_dataset.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/abstract_temporal_dataset.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -61,10 +61,52 @@
         
         @code
         
-        >>> tmr = AbstractTemporalDataset()
+        >>> import grass.temporal as tgis
+        >>> tgis.init()
+        >>> map = tgis.RasterDataset("a at P")
+        >>> tmr = tgis.AbstractTemporalDataset()
+        >>> tmr.set_next(map)
+        >>> tmr.set_prev(map)
+        >>> tmr.append_equal(map)
+        >>> tmr.append_follows(map)
+        >>> tmr.append_precedes(map)
+        >>> tmr.append_overlapped(map)
+        >>> tmr.append_overlaps(map)
+        >>> tmr.append_during(map)
+        >>> tmr.append_contains(map)
+        >>> tmr.append_starts(map)
+        >>> tmr.append_started(map)
+        >>> tmr.append_finishes(map)
+        >>> tmr.append_finished(map)
         >>> tmr.print_topology_info()
          +-------------------- Temporal Topology -------------------------------------+
+         | Next: ...................... a at P
+         | Previous: .................. a at P
+         | Equal:...................... a at P
+         | Follows: ................... a at P
+         | Precedes: .................. a at P
+         | Overlaps: .................. a at P
+         | Overlapped: ................ a at P
+         | During: .................... a at P
+         | Contains: .................. a at P
+         | Starts:.. .................. a at P
+         | Started:. .................. a at P
+         | Finishes:................... a at P
+         | Finished:................... a at P
         >>> tmr.print_topology_shell_info()
+        next=a at P
+        prev=a at P
+        equal=a at P
+        follows=a at P
+        precedes=a at P
+        overlaps=a at P
+        overlapped=a at P
+        during=a at P
+        contains=a at P
+        starts=a at P
+        started=a at P
+        finishes=a at P
+        finished=a at P
         
         @endcode
     """

Modified: grass/trunk/lib/python/temporal/base.py
===================================================================
--- grass/trunk/lib/python/temporal/base.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/base.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -14,6 +14,7 @@
 @code
 
 >>> import grass.temporal as tgis
+>>> tgis.init()
 >>> rbase = tgis.RasterBase(ident="soil at PERMANENT")
 >>> vbase = tgis.VectorBase(ident="soil:1 at PERMANENT")
 >>> r3base = tgis.Raster3DBase(ident="soil at PERMANENT")
@@ -50,7 +51,7 @@
             Usage:
             
             \code
-            
+            >>> init()
             >>> t = DictSQLSerializer()
             >>> t.D["id"] = "soil at PERMANENT"
             >>> t.D["name"] = "soil"
@@ -212,6 +213,7 @@
        
        \code
        
+        >>> init()
         >>> t = SQLDatabaseInterface("raster", "soil at PERMANENT")
         >>> t.D["name"] = "soil"
         >>> t.D["mapset"] = "PERMANENT"
@@ -511,6 +513,7 @@
         
         \code
 
+        >>> init()
         >>> t = DatasetBase("raster", "soil at PERMANENT", creator="soeren", ctime=datetime(2001,1,1), ttype="absolute")
         >>> t.id
         'soil at PERMANENT'
@@ -849,6 +852,7 @@
 
     \code
     
+    >>> init()
     >>> t = STDSBase("stds", "soil at PERMANENT", semantic_type="average", creator="soeren", ctime=datetime(2001,1,1), ttype="absolute")
     >>> t.semantic_type
     'average'

Modified: grass/trunk/lib/python/temporal/core.py
===================================================================
--- grass/trunk/lib/python/temporal/core.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/core.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -375,6 +375,7 @@
            
            @code
            
+           >>> init()
            >>> dbif = SQLDatabaseInterfaceConnection()
            >>> dbif.mogrify_sql_statement(["SELECT ctime FROM raster_base WHERE id = ?",
            ... ["soil at PERMANENT",]])

Modified: grass/trunk/lib/python/temporal/datetime_math.py
===================================================================
--- grass/trunk/lib/python/temporal/datetime_math.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/datetime_math.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -611,4 +611,4 @@
 
 if __name__ == "__main__":
     import doctest
-    doctest.testmod()
\ No newline at end of file
+    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/metadata.py
===================================================================
--- grass/trunk/lib/python/temporal/metadata.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/metadata.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -9,6 +9,7 @@
 @code
 
 >>> import grass.temporal as tgis
+>>> tgis.init()
 >>> meta = tgis.RasterMetadata()
 >>> meta = tgis.Raster3DMetadata()
 >>> meta = tgis.VectorMetadata()
@@ -37,6 +38,7 @@
         
         @code
         
+        >>> init()
         >>> meta = RasterMetadataBase(table="metadata", ident="soil at PERMANENT",
         ... datatype="CELL", cols=100, rows=100, number_of_cells=10000, nsres=0.1,
         ... ewres=0.1, min=0, max=100)
@@ -276,6 +278,7 @@
         
         @code
        
+        >>> init()
         >>> meta = RasterMetadata(ident="soil at PERMANENT",
         ... datatype="CELL", cols=100, rows=100, number_of_cells=10000, nsres=0.1,
         ... ewres=0.1, min=0, max=100)
@@ -375,6 +378,7 @@
         
         @code
        
+        >>> init()
         >>> meta = Raster3DMetadata(ident="soil at PERMANENT",
         ... datatype="FCELL", cols=100, rows=100, depths=100,
         ... number_of_cells=1000000, nsres=0.1, ewres=0.1, tbres=0.1,
@@ -518,6 +522,7 @@
         
         @code
        
+        >>> init()
         >>> meta = VectorMetadata(ident="lidar at PERMANENT", is_3d=True, 
         ... number_of_points=1, number_of_lines=2, number_of_boundaries=3,
         ... number_of_centroids=4, number_of_faces=5, number_of_kernels=6, 
@@ -871,6 +876,7 @@
         
         @code
         
+        >>> init()
         >>> meta = STDSMetadataBase(ident="soils at PERMANENT",
         ... title="Soils", description="Soils 1950 - 2010")
         >>> meta.id
@@ -886,6 +892,7 @@
          | Soils
          | Description:
          | Soils 1950 - 2010
+         | Command of creation:
         >>> meta.print_shell_info()
         number_of_maps=None
         
@@ -1001,7 +1008,7 @@
         Usage:
         
         @code
-        
+        >>> init()
         >>> meta = STDSRasterMetadataBase(ident="soils at PERMANENT",
         ... title="Soils", description="Soils 1950 - 2010")
         >>> meta.id
@@ -1025,6 +1032,7 @@
          | Soils
          | Description:
          | Soils 1950 - 2010
+         | Command of creation:
          | North-South resolution min:. None
          | North-South resolution max:. None
          | East-west resolution min:... None
@@ -1192,6 +1200,7 @@
         
         @code
         
+        >>> init()
         >>> meta = STRDSMetadata(ident="soils at PERMANENT",
         ... title="Soils", description="Soils 1950 - 2010")
         >>> meta.id
@@ -1217,6 +1226,7 @@
          | Soils
          | Description:
          | Soils 1950 - 2010
+         | Command of creation:
          | North-South resolution min:. None
          | North-South resolution max:. None
          | East-west resolution min:... None
@@ -1293,6 +1303,7 @@
         
         @code
         
+        >>> init()
         >>> meta = STR3DSMetadata(ident="soils at PERMANENT",
         ... title="Soils", description="Soils 1950 - 2010")
         >>> meta.id
@@ -1320,6 +1331,7 @@
          | Soils
          | Description:
          | Soils 1950 - 2010
+         | Command of creation:
          | North-South resolution min:. None
          | North-South resolution max:. None
          | East-west resolution min:... None
@@ -1428,6 +1440,7 @@
         
         @code
         
+        >>> init()
         >>> meta = STVDSMetadata(ident="lidars at PERMANENT",
         ... title="LIDARS", description="LIDARS 2008 - 2010")
         >>> meta.id
@@ -1456,6 +1469,7 @@
          | LIDARS
          | Description:
          | LIDARS 2008 - 2010
+         | Command of creation:
          | Vector register table:...... None
          | Number of points ........... None
          | Number of lines ............ None
@@ -1696,4 +1710,4 @@
 
 if __name__ == "__main__":
     import doctest
-    doctest.testmod()
\ No newline at end of file
+    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/space_time_datasets.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/space_time_datasets.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -46,6 +46,7 @@
         @code
         
         >>> import grass.script as grass
+        >>> init()
         >>> grass.use_temp_region()
         >>> grass.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0, 
         ... t=1.0, b=0.0, res=10.0)

Modified: grass/trunk/lib/python/temporal/spatial_extent.py
===================================================================
--- grass/trunk/lib/python/temporal/spatial_extent.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/spatial_extent.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -9,6 +9,7 @@
 @code
 
 >>> import grass.temporal as tgis
+>>> tgis.init()
 >>> extent = tgis.RasterSpatialExtent( 
 ... ident="raster at PERMANENT", north=90, south=90, east=180, west=180,
 ... top=100, bottom=-20)
@@ -50,6 +51,7 @@
         
         @code
         
+        >>> init()
         >>> extent = SpatialExtent(table="raster_spatial_extent", 
         ... ident="soil at PERMANENT", north=90, south=90, east=180, west=180,
         ... top=100, bottom=-20)
@@ -1604,4 +1606,4 @@
 
 if __name__ == "__main__":
     import doctest
-    doctest.testmod()
\ No newline at end of file
+    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/temporal_extent.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_extent.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/temporal_extent.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -10,6 +10,7 @@
 
 >>> import grass.temporal as tgis
 >>> from datetime import datetime
+>>> tgis.init()
 >>> t = tgis.RasterRelativeTime()
 >>> t = tgis.RasterAbsoluteTime()
 
@@ -40,6 +41,7 @@
         
         @code
         
+        >>> init()
         >>> A = AbstractTemporalExtent(table="raster_absolute_time",
         ... ident="soil at PERMANENT", start_time=datetime(2001, 01, 01),
         ... end_time=datetime(2005,01,01) )
@@ -770,6 +772,7 @@
         
         @code
         
+        >>> init()
         >>> A = STDSAbsoluteTime(table="strds_absolute_time",
         ... ident="strds at PERMANENT", start_time=datetime(2001, 01, 01),
         ... end_time=datetime(2005,01,01), granularity="1 days",
@@ -895,6 +898,7 @@
         
         @code
        
+        >>> init()
         >>> A = RelativeTemporalExtent(table="raster_relative_time",
         ... ident="soil at PERMANENT", start_time=0, end_time=1, unit="years")
         >>> A.id
@@ -1007,6 +1011,7 @@
         
         @code
        
+        >>> init()
         >>> A = STDSRelativeTime(table="strds_relative_time",
         ... ident="strds at PERMANENT", start_time=0, end_time=1, unit="years",
         ... granularity=5, map_time="interval")
@@ -1128,4 +1133,4 @@
 
 if __name__ == "__main__":
     import doctest
-    doctest.testmod()
\ No newline at end of file
+    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/temporal_granularity.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_granularity.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/temporal_granularity.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -31,6 +31,43 @@
         @param granularity The granularity string
         @param temporal_type The temporal type of the granularity relative or absolute
         @return True if valid, False if invalid
+        
+        @code
+        
+        >>> check_granularity_string("1 year", "absolute")
+        True
+        >>> check_granularity_string("1 month", "absolute")
+        True
+        >>> check_granularity_string("1 day", "absolute")
+        True
+        >>> check_granularity_string("1 minute", "absolute")
+        True
+        >>> check_granularity_string("1 hour", "absolute")
+        True
+        >>> check_granularity_string("1 second", "absolute")
+        True
+        >>> check_granularity_string("5 months", "absolute")
+        True
+        >>> check_granularity_string("5 days", "absolute")
+        True
+        >>> check_granularity_string("5 minutes", "absolute")
+        True
+        >>> check_granularity_string("5 years", "absolute")
+        True
+        >>> check_granularity_string("5 hours", "absolute")
+        True
+        >>> check_granularity_string("2 seconds", "absolute")
+        True
+        >>> check_granularity_string("1 secondo", "absolute")
+        False
+        >>> check_granularity_string("bla second", "absolute")
+        False
+        >>> check_granularity_string(1, "relative")
+        True
+        >>> check_granularity_string("bla", "relative")
+        False
+        
+        @endcode
     """
     temporal_type
     
@@ -69,6 +106,94 @@
 
         @param maps: a ordered by start_time list of map objects
         @return An integer
+        
+        
+        @code
+        
+        >>> import grass.temporal as tgis
+        >>> tgis.init()
+        >>> maps = []
+        >>> for i in range(5):
+        ...   map = tgis.RasterDataset("a%i at P"%i)
+        ...   check = map.set_relative_time(i,i + 1,"seconds")
+        ...   if check:
+        ...     maps.append(map)
+        >>> tgis.compute_relative_time_granularity(maps)
+        1
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((0,3), (3,6), (6,9))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_relative_time(t[0],t[1],"years")
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_relative_time_granularity(maps)
+        3
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((0,3), (4,6), (8,11))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_relative_time(t[0],t[1],"years")
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_relative_time_granularity(maps)
+        1
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((0,8), (2,6), (5,9))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_relative_time(t[0],t[1],"months")
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_relative_time_granularity(maps)
+        4
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((0,8), (8,12), (12,18))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_relative_time(t[0],t[1],"days")
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_relative_time_granularity(maps)
+        2
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((0,None), (8,None), (12,None), (24,None))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_relative_time(t[0],t[1],"minutes")
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_relative_time_granularity(maps)
+        4
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((0,None), (8,14), (18,None), (24,None))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_relative_time(t[0],t[1],"hours")
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_relative_time_granularity(maps)
+        2
+        
+        @endcode
     """
 
     # The interval time must be scaled to days resolution
@@ -125,6 +250,74 @@
 
         @param maps: a ordered by start_time list of map objects
         @return The temporal topology as string "integer unit"
+        
+        @code
+
+        >>> import grass.temporal as tgis
+        >>> import datetime
+        >>> dt = datetime.datetime
+        >>> tgis.init()
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((dt(2000,01,01),None), (dt(2000,02,01),None))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_absolute_time(t[0],t[1])
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_absolute_time_granularity(maps)
+        '1 month'
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((dt(2000,01,01),None), (dt(2000,01,02),None), (dt(2000,01,03),None))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_absolute_time(t[0],t[1])
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_absolute_time_granularity(maps)
+        '1 day'
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((dt(2000,01,01),None), (dt(2000,01,02),None), (dt(2000,05,04,0,5,30),None))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_absolute_time(t[0],t[1])
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_absolute_time_granularity(maps)
+        '30 seconds'
+        
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((dt(2000,01,01),dt(2000,05,02)), (dt(2000,05,04,2),None))
+        >>> for t in timelist:
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_absolute_time(t[0],t[1])
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_absolute_time_granularity(maps)
+        '2 hours'
+
+        >>> maps = []
+        >>> count = 0
+        >>> timelist = ((dt(2000,01,01),dt(2000,02,01)), (dt(2005,05,04,12),dt(2007,05,20,6)))
+        >>> for t in timelist:  
+        ...   map = tgis.RasterDataset("a%i at P"%count)
+        ...   check = map.set_absolute_time(t[0],t[1])
+        ...   if check:
+        ...     maps.append(map)
+        ...   count += 1
+        >>> tgis.compute_absolute_time_granularity(maps)
+        '6 hours'
+        
+        @endcode
     """
 
     has_seconds = False
@@ -166,42 +359,47 @@
                     delta.append(start2 - start1)
                     datetime_delta.append(compute_datetime_delta(
                         start1, start2))
-
     # Check what changed
     dlist = []
     for d in datetime_delta:
         if "second" in d and d["second"] > 0:
             has_seconds = True
+            #print "has second"
         if "minute" in d and d["minute"] > 0:
             has_minutes = True
+            #print "has minute"
         if "hour" in d and d["hour"] > 0:
             has_hours = True
+            #print "has hour"
         if "day" in d and d["day"] > 0:
             has_days = True
+            #print "has day"
         if "month" in d and d["month"] > 0:
             has_months = True
+            #print "has month"
         if "year" in d and d["year"] > 0:
             has_years = True
+            #print "has year"
 
     # Create a list with a single time unit only
     if has_seconds:
         for d in datetime_delta:
-            if "second" in d:
+            if "second" in d and d["second"] > 0:
                 dlist.append(d["second"])
-            elif "minute" in d:
+            elif "minute" in d and d["minute"] > 0:
                 dlist.append(d["minute"] * 60)
-            elif "hour" in d:
+            elif "hour" in d and d["hour"] > 0:
                 dlist.append(d["hour"] * 3600)
-            elif "day" in d:
+            elif "day" in d and d["day"] > 0:
                 dlist.append(d["day"] * 24 * 3600)
             else:
                 dlist.append(d["max_days"] * 24 * 3600)
         use_seconds = True
     elif has_minutes:
         for d in datetime_delta:
-            if "minute" in d:
+            if "minute" in d and d["minute"] > 0:
                 dlist.append(d["minute"])
-            elif "hour" in d:
+            elif "hour" in d and d["hour"] > 0:
                 dlist.append(d["hour"] * 60)
             elif "day" in d:
                 dlist.append(d["day"] * 24 * 60)
@@ -210,25 +408,25 @@
         use_minutes = True
     elif has_hours:
         for d in datetime_delta:
-            if "hour" in d:
+            if "hour" in d and d["hour"] > 0:
                 dlist.append(d["hour"])
-            elif "day" in d:
+            elif "day" in d and d["day"] > 0:
                 dlist.append(d["day"] * 24)
             else:
                 dlist.append(d["max_days"] * 24)
         use_hours = True
     elif has_days:
         for d in datetime_delta:
-            if "day" in d:
+            if "day" in d and d["day"] > 0:
                 dlist.append(d["day"])
             else:
                 dlist.append(d["max_days"])
         use_days = True
     elif has_months:
         for d in datetime_delta:
-            if "month" in d:
+            if "month" in d and d["month"] > 0:
                 dlist.append(d["month"])
-            elif "year" in d:
+            elif "year" in d and d["year"] > 0:
                 dlist.append(d["year"] * 12)
         use_months = True
     elif has_years:
@@ -309,3 +507,9 @@
     Returns: GCD of all numbers
     """
     return reduce(gcd, list)
+    
+###############################################################################
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/temporal_relationships.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_relationships.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/temporal_relationships.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -632,4 +632,11 @@
     if where == "()":
         where = None
 
-    return where
\ No newline at end of file
+    return where
+    
+
+###############################################################################
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()

Modified: grass/trunk/lib/python/temporal/unit_tests.py
===================================================================
--- grass/trunk/lib/python/temporal/unit_tests.py	2013-04-17 16:55:58 UTC (rev 55856)
+++ grass/trunk/lib/python/temporal/unit_tests.py	2013-04-17 21:19:53 UTC (rev 55857)
@@ -563,112 +563,13 @@
     if delta != 0:
         core.fatal("Compute datetime delta is wrong %s" % (delta))
 
-###############################################################################
-
-def test_compute_relative_time_granularity():
-
-    # First we test intervals
-    print "Test 1"
-    maps = []
-    fact = 5
-    start = 1
-    end = start * fact
-    for i in range(6):
-        end = start * fact
-        map = RasterDataset(None)
-        map.set_relative_time(start, end, "years")
-        maps.append(map)
-        start = end
-
-    fact = fact - 1
-    gran = round(compute_relative_time_granularity(maps))
-    if fact - gran != 0:
-        core.fatal("Wrong granularity reference %i != gran %i" % (fact, gran))
-
-    print "Test 2"
-    maps = []
-    fact = 3
-    start = 1.0 / 86400
-    end = start * fact
-    for i in range(10):
-        end = start * fact
-        map = RasterDataset(None)
-        map.set_relative_time(start, end, "years")
-        maps.append(map)
-        start = end
-
-    fact = fact - 1
-    gran = round(compute_relative_time_granularity(maps) * 86400)
-    if fact - gran != 0:
-        core.fatal("Wrong granularity reference %i != gran %i" % (fact, gran))
-
-    print "Test 3 with gaps"
-    maps = []
-    fact = 3
-    start = 1
-    end = start + fact
-    for i in range(10):
-        shift = i * 2 * fact
-        start = shift
-        end = start + fact
-
-        map = RasterDataset(None)
-        map.set_relative_time(start, end, "years")
-        maps.append(map)
-
-    gran = round(compute_relative_time_granularity(maps))
-    if fact - gran != 0:
-        core.fatal("Wrong granularity reference %i != gran %i" % (fact, gran))
-
-    # Second we test intervals and points mixed
-
-    print "Test 4 intervals and points"
-    maps = []
-    fact = 5
-    start = 1
-    end = start * fact
-    count = 0
-    for i in range(6):
-        end = start * fact
-        map = RasterDataset(None)
-        if count % 2 == 0:
-            map.set_relative_time(start, end, "years")
-        else:
-            map.set_relative_time(start, None)
-        maps.append(map)
-        start = end
-        count += 1
-
-    fact = fact - 1
-    gran = round(compute_relative_time_granularity(maps))
-    if fact - gran != 0:
-        core.fatal("Wrong granularity reference %i != gran %i" % (fact, gran))
-
-    # Second we test points only
-
-    print "Test 5 points only"
-    maps = []
-    fact = 3
-    start = 1.0 / 86400
-    for i in range(10):
-        point = (i + 1) * fact * start
-        map = RasterDataset(None)
-        map.set_relative_time(point, None, years)
-        maps.append(map)
-
-    gran = round(compute_relative_time_granularity(maps) * 86400)
-    if fact - gran != 0:
-        core.fatal("Wrong granularity reference %i != gran %i" % (fact, gran))
-
-###############################################################################
-
 def test_compute_absolute_time_granularity():
 
     # First we test intervals
     print "Test 1"
     maps = []
     a = datetime(2001, 1, 1)
-    increment = "1 years"
+    increment = "1 year"
     for i in range(10):
         start = increment_datetime_by_string(a, increment, i)
         end = increment_datetime_by_string(a, increment, i + 1)
@@ -700,7 +601,7 @@
     print "Test 3"
     maps = []
     a = datetime(2001, 5, 1)
-    increment = "1 months"
+    increment = "1 month"
     for i in range(20):
         start = increment_datetime_by_string(a, increment, i)
         end = increment_datetime_by_string(a, increment, i + 1)
@@ -732,7 +633,7 @@
     print "Test 3"
     maps = []
     a = datetime(2001, 1, 1)
-    increment = "1 days"
+    increment = "1 day"
     for i in range(6):
         start = increment_datetime_by_string(a, increment, i)
         end = increment_datetime_by_string(a, increment, i + 1)
@@ -764,7 +665,7 @@
     print "Test 5"
     maps = []
     a = datetime(2001, 3, 1)
-    increment = "1 months, 4 days"
+    increment = "1 month, 4 days"
     for i in range(20):
         start = increment_datetime_by_string(a, increment, i)
         end = increment_datetime_by_string(a, increment, i + 1)
@@ -772,7 +673,7 @@
         map.set_absolute_time(start, end)
         maps.append(map)
 
-    increment = "1 days"
+    increment = "1 day"
     gran = compute_absolute_time_granularity(maps)
     if increment != gran:
         core.fatal("Wrong granularity reference %s != gran %s" % (
@@ -885,6 +786,8 @@
     for i in range(24):
         start = increment_datetime_by_string(a, increment, i)
         end = increment_datetime_by_string(a, increment, i + 1)
+        print start
+        print end
         map = RasterDataset(None)
         map.set_absolute_time(start, end)
         maps.append(map)
@@ -1489,13 +1392,13 @@
     _map.set_absolute_time(datetime(2001, 05, 01), datetime(2001, 06, 01))
     map_listA.append(copy.copy(_map))
 
-    tb = temporal_topology_builder()
+    tb = TemporalTopologyBuilder()
     tb.build(map_listA)
 
     count = 0
     for _map in tb:
         print "[%s]" % (_map.get_name())
-        _map.print_temporal_topology_info()
+        _map.print_topology_info()
         if _map.get_id() != map_listA[count].get_id():
             core.fatal("Error building temporal topology <%s> != <%s>" %
                 (_map.get_id(), map_listA[count].get_id()))
@@ -1519,36 +1422,36 @@
     _map.set_absolute_time(datetime(2001, 05, 01), datetime(2001, 05, 14))
     map_listB.append(copy.copy(_map))
 
-    tb = temporal_topology_builder()
+    tb = TemporalTopologyBuilder()
     tb.build(map_listB)
 
     # Probing some relations
-    if map_listB[0].get_temporal_overlapped()[0] != map_listB[1]:
+    if map_listB[0].get_overlapped()[0] != map_listB[1]:
         core.fatal("Error building temporal topology")
-    if map_listB[0].get_temporal_overlapped()[1] != map_listB[2]:
+    if map_listB[0].get_overlapped()[1] != map_listB[2]:
         core.fatal("Error building temporal topology")
-    if map_listB[2].get_temporal_contains()[0] != map_listB[3]:
+    if map_listB[2].get_contains()[0] != map_listB[3]:
         core.fatal("Error building temporal topology")
-    if map_listB[3].get_temporal_during()[0] != map_listB[2]:
+    if map_listB[3].get_during()[0] != map_listB[2]:
         core.fatal("Error building temporal topology")
 
     count = 0
     for _map in tb:
         print "[%s]" % (_map.get_map_id
         ())
-        _map.print_temporal_topology_shell_info()
+        _map.print_topology_shell_info()
         if _map.get_id() != map_listB[count].get_id():
             core.fatal("Error building temporal topology <%s> != <%s>" %
                 (_map.get_id(), map_listB[count].get_id()))
         count += 1
 
-    tb = temporal_topology_builder()
-    tb.build2(map_listA, map_listB)
+    tb = TemporalTopologyBuilder()
+    tb.build(map_listA, map_listB)
 
     count = 0
     for _map in tb:
         print "[%s]" % (_map.get_map_id())
-        _map.print_temporal_topology_shell_info()
+        _map.print_topology_shell_info()
         if _map.get_id() != map_listA[count].get_id():
             core.fatal("Error building temporal topology <%s> != <%s>" %
                 (_map.get_id(), map_listA[count].get_id()))
@@ -1557,21 +1460,21 @@
     count = 0
     for _map in map_listB:
         print "[%s]" % (_map.get_map_id())
-        _map.print_temporal_topology_shell_info()
+        _map.print_topology_shell_info()
 
     # Probing some relations
-    if map_listA[3].get_temporal_follows()[0] != map_listB[1]:
+    if map_listA[3].get_follows()[0] != map_listB[1]:
         core.fatal("Error building temporal topology")
-    if map_listA[3].get_temporal_precedes()[0] != map_listB[4]:
+    if map_listA[3].get_precedes()[0] != map_listB[4]:
         core.fatal("Error building temporal topology")
-    if map_listA[3].get_temporal_overlaps()[0] != map_listB[2]:
+    if map_listA[3].get_overlaps()[0] != map_listB[2]:
         core.fatal("Error building temporal topology")
-    if map_listA[3].get_temporal_contains()[0] != map_listB[3]:
+    if map_listA[3].get_contains()[0] != map_listB[3]:
         core.fatal("Error building temporal topology")
 
-    if map_listA[2].get_temporal_during()[0] != map_listB[1]:
+    if map_listA[2].get_during()[0] != map_listB[1]:
         core.fatal("Error building temporal topology")
-    if map_listA[2].get_temporal_during()[1] != map_listB[2]:
+    if map_listA[2].get_during()[1] != map_listB[2]:
         core.fatal("Error building temporal topology")
 
 ###############################################################################
@@ -1751,10 +1654,10 @@
 ###############################################################################
 
 if __name__ == "__main__":
+    init()
     test_increment_datetime_by_string()
     test_adjust_datetime_to_granularity()
     test_spatial_extent_intersection()
-    test_compute_relative_time_granularity()
     test_compute_absolute_time_granularity()
     test_compute_datetime_delta()
     test_spatial_extent_intersection()



More information about the grass-commit mailing list