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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 4 15:26:26 PST 2016


Author: huhabla
Date: 2016-01-04 15:26:26 -0800 (Mon, 04 Jan 2016)
New Revision: 67484

Modified:
   grass/trunk/lib/python/temporal/temporal_granularity.py
Log:
temporal framework: Better common granularity computation that checks the start times of space time datasets


Modified: grass/trunk/lib/python/temporal/temporal_granularity.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_granularity.py	2016-01-04 21:17:04 UTC (rev 67483)
+++ grass/trunk/lib/python/temporal/temporal_granularity.py	2016-01-04 23:26:26 UTC (rev 67484)
@@ -516,16 +516,225 @@
 
 ###############################################################################
 
-def compute_common_absolute_time_granularity(gran_list):
-    """Compute the greatest common granule from a list of absolute time granules
+def compute_common_absolute_time_granularity(gran_list,
+                                             start_date_list = None):
+    """ Compute the greatest common granule from a list of absolute time granules,
+        considering the start times of the related space time datasets in the
+        common granularity computation.
 
+        :param gran_list: List of granularities
+        :param start_date_list: List of the start times of related space time datasets
+        :return: The common granularity
+
         .. code-block:: python
 
+            from datetime import datetime
             >>> import grass.temporal as tgis
             >>> tgis.init()
+            >>> grans = ["20 second", "10 minutes", "2 hours"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '20 seconds'
+
+            >>> grans = ["20 second", "10 minutes", "2 hours"]
+            >>> dates = [datetime(2001,1,1,0,0,20),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 second'
+
+            >>> grans = ["7200 second", "240 minutes", "1 year"]
+            >>> dates = [datetime(2001,1,1,0,0,10),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 second'
+
+            >>> grans = ["7200 second", "89 minutes", "1 year"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '60 seconds'
+
+            >>> grans = ["120 minutes", "2 hours"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '60 minutes'
+
+            >>> grans = ["120 minutes", "2 hours"]
+            >>> dates = [datetime(2001,1,1,0,30,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 minute'
+
+            >>> grans = ["360 minutes", "3 hours"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '60 minutes'
+
+            >>> grans = ["2 hours", "4 hours", "8 hours"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '2 hours'
+
+            >>> grans = ["2 hours", "4 hours", "8 hours"]
+            >>> dates = [datetime(2001,1,1,2,0,0),
+            ...          datetime(2001,1,1,4,0,0),
+            ...          datetime(2001,1,1,8,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 hour'
+
+            >>> grans = ["8 hours", "2 days"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '8 hours'
+
+            >>> grans = ["8 hours", "2 days"]
+            >>> dates = [datetime(2001,1,1,10,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 hour'
+
+            >>> grans = ["120 months", "360 months", "4 years"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '12 months'
+
+            >>> grans = ["30 days", "10 days", "5 days"]
+            >>> dates = [datetime(2001,2,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '5 days'
+
+            >>> grans = ["30 days", "10 days", "5 days"]
+            >>> dates = [datetime(2001,2,2,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 day'
+
+            >>> grans = ["2 days", "360 months", "4 years"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '2 days'
+
+            >>> grans = ["2 days", "360 months", "4 years"]
+            >>> dates = [datetime(2001,1,2,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 day'
+
+            >>> grans = ["120 months", "360 months", "4 years"]
+            >>> dates = [datetime(2001,2,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 month'
+
+            >>> grans = ["120 months", "361 months", "4 years"]
+            >>> dates = [datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),
+            ...          datetime(2001,1,1,0,0,0),]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '1 month'
+
+            >>> grans = ["120 months", "360 months", "4 years"]
+            >>> tgis.compute_common_absolute_time_granularity(grans, dates)
+            '12 months'
+
+        ..
+
+    """
+
+    common_granule = compute_common_absolute_time_granularity_simple(gran_list)
+
+    if start_date_list is None:
+        return common_granule
+
+    num, granule = common_granule.split()
+
+    if granule in ["seconds",  "second"]:
+        # If the start seconds are different between the start dates
+        # set the granularity to one second
+        for start_time in start_date_list:
+            if start_time.second != start_date_list[0].second:
+                return "1 second"
+        # Make sure the granule does not exceed the hierarchy limit
+        if int(num) > 60:
+            return "60 seconds"
+
+    if granule in ["minutes",  "minute"]:
+        # If the start minutes are different between the start dates
+        # set the granularity to one minute
+        for start_time in start_date_list:
+            if start_time.minute != start_date_list[0].minute:
+                return "1 minute"
+        # Make sure the granule does not exceed the hierarchy limit
+        if int(num) > 60:
+            return "60 minutes"
+
+    if granule in ["hours",  "hour"]:
+        # If the start hours are different between the start dates
+        # set the granularity to one hour
+        for start_time in start_date_list:
+            if start_time.hour != start_date_list[0].hour:
+                return "1 hour"
+        # Make sure the granule does not exceed the hierarchy limit
+        if int(num) > 24:
+            return "24 hours"
+
+    if granule in ["days",  "day"]:
+        # If the start days are different between the start dates
+        # set the granularity to one day
+        for start_time in start_date_list:
+            if start_time.day != start_date_list[0].day:
+                return "1 day"
+        # Make sure the granule does not exceed the hierarchy limit
+        if int(num) > 365:
+            return "365 days"
+
+    if granule in ["months",  "month"]:
+        # If the start months are different between the start dates
+        # set the granularity to one month
+        for start_time in start_date_list:
+            if start_time.month != start_date_list[0].month:
+                return "1 month"
+        # Make sure the granule does not exceed the hierarchy limit
+        if int(num) > 12:
+            return "12 months"
+
+    return common_granule
+
+###############################################################################
+
+def compute_common_absolute_time_granularity_simple(gran_list):
+    """ Compute the greatest common granule from a list of absolute time granules
+
+        :param gran_list: List of granularities
+        :param start_date_list:
+        :return: The common granularity
+
+        .. code-block:: python
+
+            >>> import grass.temporal as tgis
+            >>> tgis.init()
             >>> grans = ["1 second", "2 seconds", "30 seconds"]
             >>> tgis.compute_common_absolute_time_granularity(grans)
-            '1 seconds'
+            '1 second'
             
             >>> grans = ["3 second", "6 seconds", "30 seconds"]
             >>> tgis.compute_common_absolute_time_granularity(grans)
@@ -585,11 +794,11 @@
 
             >>> grans = ["120 months", "361 months", "4 years"]
             >>> tgis.compute_common_absolute_time_granularity(grans)
-            '1 months'
+            '1 month'
 
             >>> grans = ["2 years", "3 years", "4 years"]
             >>> tgis.compute_common_absolute_time_granularity(grans)
-            '1 years'
+            '1 year'
     """
     
     has_seconds = False # 0
@@ -691,7 +900,10 @@
             seconds.append(years[0]*60*60*24*366)
 
         num = gcd_list(seconds)
-        return "%i %s"%(num,  "seconds")
+        gran = "second"
+        if num > 1:
+            gran += "s"
+        return "%i %s"%(num,  gran)
         
     elif has_minutes:
         if has_hours:
@@ -711,7 +923,10 @@
             minutes.append(years[0]*60*24*365)
             minutes.append(years[0]*60*24*366)
         num = gcd_list(minutes)
-        return "%i %s"%(num,  "minutes")
+        gran = "minute"
+        if num > 1:
+            gran += "s"
+        return "%i %s"%(num,  gran)
         
     elif has_hours:
         if has_days:
@@ -728,7 +943,10 @@
             hours.append(years[0]*24*365)
             hours.append(years[0]*24*366)
         num = gcd_list(hours)
-        return "%i %s"%(num,  "hours")
+        gran = "hour"
+        if num > 1:
+            gran += "s"
+        return "%i %s"%(num,  gran)
 
     elif has_days:
         if has_months:
@@ -742,18 +960,27 @@
             days.append(years[0]*365)
             days.append(years[0]*366)
         num = gcd_list(days)
-        return "%i %s"%(num,  "days")
+        gran = "day"
+        if num > 1:
+            gran += "s"
+        return "%i %s"%(num,  gran)
 
     elif has_months:
         if has_years:
             years.sort()
             months.append(years[0]*12)
         num = gcd_list(months)
-        return "%i %s"%(num,  "months")
+        gran = "month"
+        if num > 1:
+            gran += "s"
+        return "%i %s"%(num,  gran)
         
     elif has_years:
         num = gcd_list(years)
-        return "%i %s"%(num,  "years")
+        gran = "year"
+        if num > 1:
+            gran += "s"
+        return "%i %s"%(num,  gran)
         
 
 ###############################################################################



More information about the grass-commit mailing list