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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Aug 5 04:07:37 PDT 2013


Author: huhabla
Date: 2013-08-05 04:07:37 -0700 (Mon, 05 Aug 2013)
New Revision: 57415

Modified:
   grass/trunk/lib/python/temporal/temporal_granularity.py
Log:
Fixed granularity check


Modified: grass/trunk/lib/python/temporal/temporal_granularity.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_granularity.py	2013-08-05 01:39:31 UTC (rev 57414)
+++ grass/trunk/lib/python/temporal/temporal_granularity.py	2013-08-05 11:07:37 UTC (rev 57415)
@@ -24,16 +24,16 @@
 from datetime_math import *
 
 ###############################################################################
-        
+
 def check_granularity_string(granularity, temporal_type):
     """!Check if the granularity string is valid
-    
+
         @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")
@@ -62,25 +62,30 @@
         False
         >>> check_granularity_string("bla second", "absolute")
         False
+        >>> check_granularity_string("bla", "absolute")
+        False
         >>> check_granularity_string(1, "relative")
         True
         >>> check_granularity_string("bla", "relative")
         False
-        
+
         @endcode
     """
     temporal_type
-    
+
     if granularity is None:
         return False
 
     if temporal_type == "absolute":
-        num, unit = granularity.split(" ")
-        if unit not in ["second", "seconds", "minute", "minutes", "hour", 
+        try:
+            num, unit = granularity.split(" ")
+        except:
+            return False
+        if unit not in ["second", "seconds", "minute", "minutes", "hour",
                         "hours", "day", "days", "week", "weeks", "month",
                         "months", "year", "years"]:
             return False
-            
+
         try:
             integer = int(num)
         except:
@@ -92,7 +97,7 @@
             return False
     else:
         return False
-        
+
     return True
 
 ###############################################################################
@@ -100,16 +105,16 @@
 def compute_relative_time_granularity(maps):
     """!Compute the relative time granularity
 
-        Attention: The computation of the granularity 
-        is only correct in case of not overlapping intervals. 
+        Attention: The computation of the granularity
+        is only correct in case of not overlapping intervals.
         Hence a correct temporal topology is required for computation.
 
         @param maps a ordered by start_time list of map objects
         @return An integer
-        
-        
+
+
         @code
-        
+
         >>> import grass.temporal as tgis
         >>> tgis.init()
         >>> maps = []
@@ -120,7 +125,7 @@
         ...     maps.append(map)
         >>> tgis.compute_relative_time_granularity(maps)
         1
-        
+
         >>> maps = []
         >>> count = 0
         >>> timelist = ((0,3), (3,6), (6,9))
@@ -132,7 +137,7 @@
         ...   count += 1
         >>> tgis.compute_relative_time_granularity(maps)
         3
-        
+
         >>> maps = []
         >>> count = 0
         >>> timelist = ((0,3), (4,6), (8,11))
@@ -144,7 +149,7 @@
         ...   count += 1
         >>> tgis.compute_relative_time_granularity(maps)
         1
-        
+
         >>> maps = []
         >>> count = 0
         >>> timelist = ((0,8), (2,6), (5,9))
@@ -156,7 +161,7 @@
         ...   count += 1
         >>> tgis.compute_relative_time_granularity(maps)
         4
-        
+
         >>> maps = []
         >>> count = 0
         >>> timelist = ((0,8), (8,12), (12,18))
@@ -168,7 +173,7 @@
         ...   count += 1
         >>> tgis.compute_relative_time_granularity(maps)
         2
-        
+
         >>> maps = []
         >>> count = 0
         >>> timelist = ((0,None), (8,None), (12,None), (24,None))
@@ -180,7 +185,7 @@
         ...   count += 1
         >>> tgis.compute_relative_time_granularity(maps)
         4
-        
+
         >>> maps = []
         >>> count = 0
         >>> timelist = ((0,None), (8,14), (18,None), (24,None))
@@ -192,7 +197,7 @@
         ...   count += 1
         >>> tgis.compute_relative_time_granularity(maps)
         2
-        
+
         @endcode
     """
 
@@ -214,7 +219,7 @@
             if relation == "after":
                 start1, end1 = maps[i].get_temporal_extent_as_tuple()
                 start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
-                # Gaps are between intervals, intervals and 
+                # Gaps are between intervals, intervals and
                 # points, points and points
                 if end1 and start2:
                     t = abs(end1 - start2)
@@ -241,16 +246,16 @@
 def compute_absolute_time_granularity(maps):
     """!Compute the absolute time granularity
 
-        Attention: The computation of the granularity 
-        is only correct in case of not overlapping intervals. 
+        Attention: The computation of the granularity
+        is only correct in case of not overlapping intervals.
         Hence a correct temporal topology is required for computation.
-        
+
         The computed granularity is returned as number of seconds or minutes or hours
-        or days or months or years. 
+        or days or months or years.
 
         @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
@@ -268,7 +273,7 @@
         ...   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))
@@ -280,7 +285,7 @@
         ...   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))
@@ -292,7 +297,7 @@
         ...   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))
@@ -308,7 +313,7 @@
         >>> 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:  
+        >>> for t in timelist:
         ...   map = tgis.RasterDataset("a%i at P"%count)
         ...   check = map.set_absolute_time(t[0],t[1])
         ...   if check:
@@ -316,7 +321,7 @@
         ...   count += 1
         >>> tgis.compute_absolute_time_granularity(maps)
         '6 hours'
-        
+
         @endcode
     """
 
@@ -350,7 +355,7 @@
             if relation == "after":
                 start1, end1 = maps[i].get_temporal_extent_as_tuple()
                 start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
-                # Gaps are between intervals, intervals and 
+                # Gaps are between intervals, intervals and
                 # points, points and points
                 if end1 and start2:
                     delta.append(end1 - start2)
@@ -507,7 +512,7 @@
     Returns: GCD of all numbers
     """
     return reduce(gcd, list)
-    
+
 ###############################################################################
 
 if __name__ == "__main__":



More information about the grass-commit mailing list