temporal_granularity.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. """
  2. Functions to compute the temporal granularity of a map list
  3. Usage:
  4. .. code-block:: python
  5. import grass.temporal as tgis
  6. tgis.compute_relative_time_granularity(maps)
  7. (C) 2012-2013 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. :authors: Soeren Gebbert
  12. """
  13. from abstract_dataset import *
  14. from datetime_math import *
  15. ###############################################################################
  16. def check_granularity_string(granularity, temporal_type):
  17. """Check if the granularity string is valid
  18. :param granularity: The granularity string
  19. :param temporal_type: The temporal type of the granularity relative or absolute
  20. :return: True if valid, False if invalid
  21. .. code-block:: python
  22. >>> check_granularity_string("1 year", "absolute")
  23. True
  24. >>> check_granularity_string("1 month", "absolute")
  25. True
  26. >>> check_granularity_string("1 day", "absolute")
  27. True
  28. >>> check_granularity_string("1 minute", "absolute")
  29. True
  30. >>> check_granularity_string("1 hour", "absolute")
  31. True
  32. >>> check_granularity_string("1 second", "absolute")
  33. True
  34. >>> check_granularity_string("5 months", "absolute")
  35. True
  36. >>> check_granularity_string("5 days", "absolute")
  37. True
  38. >>> check_granularity_string("5 minutes", "absolute")
  39. True
  40. >>> check_granularity_string("5 years", "absolute")
  41. True
  42. >>> check_granularity_string("5 hours", "absolute")
  43. True
  44. >>> check_granularity_string("2 seconds", "absolute")
  45. True
  46. >>> check_granularity_string("1 secondo", "absolute")
  47. False
  48. >>> check_granularity_string("bla second", "absolute")
  49. False
  50. >>> check_granularity_string("bla", "absolute")
  51. False
  52. >>> check_granularity_string(1, "relative")
  53. True
  54. >>> check_granularity_string("bla", "relative")
  55. False
  56. """
  57. temporal_type
  58. if granularity is None:
  59. return False
  60. if temporal_type == "absolute":
  61. try:
  62. num, unit = granularity.split(" ")
  63. except:
  64. return False
  65. if unit not in ["second", "seconds", "minute", "minutes", "hour",
  66. "hours", "day", "days", "week", "weeks", "month",
  67. "months", "year", "years"]:
  68. return False
  69. try:
  70. integer = int(num)
  71. except:
  72. return False
  73. elif temporal_type == "relative":
  74. try:
  75. integer = int(granularity)
  76. except:
  77. return False
  78. else:
  79. return False
  80. return True
  81. ###############################################################################
  82. def compute_relative_time_granularity(maps):
  83. """Compute the relative time granularity
  84. Attention: The computation of the granularity
  85. is only correct in case of not overlapping intervals.
  86. Hence a correct temporal topology is required for computation.
  87. :param maps: a ordered by start_time list of map objects
  88. :return: An integer
  89. .. code-block:: python
  90. >>> import grass.temporal as tgis
  91. >>> tgis.init()
  92. >>> maps = []
  93. >>> for i in range(5):
  94. ... map = tgis.RasterDataset("a%i@P"%i)
  95. ... check = map.set_relative_time(i,i + 1,"seconds")
  96. ... if check:
  97. ... maps.append(map)
  98. >>> tgis.compute_relative_time_granularity(maps)
  99. 1
  100. >>> maps = []
  101. >>> count = 0
  102. >>> timelist = ((0,3), (3,6), (6,9))
  103. >>> for t in timelist:
  104. ... map = tgis.RasterDataset("a%i@P"%count)
  105. ... check = map.set_relative_time(t[0],t[1],"years")
  106. ... if check:
  107. ... maps.append(map)
  108. ... count += 1
  109. >>> tgis.compute_relative_time_granularity(maps)
  110. 3
  111. >>> maps = []
  112. >>> count = 0
  113. >>> timelist = ((0,3), (4,6), (8,11))
  114. >>> for t in timelist:
  115. ... map = tgis.RasterDataset("a%i@P"%count)
  116. ... check = map.set_relative_time(t[0],t[1],"years")
  117. ... if check:
  118. ... maps.append(map)
  119. ... count += 1
  120. >>> tgis.compute_relative_time_granularity(maps)
  121. 1
  122. >>> maps = []
  123. >>> count = 0
  124. >>> timelist = ((0,8), (2,6), (5,9))
  125. >>> for t in timelist:
  126. ... map = tgis.RasterDataset("a%i@P"%count)
  127. ... check = map.set_relative_time(t[0],t[1],"months")
  128. ... if check:
  129. ... maps.append(map)
  130. ... count += 1
  131. >>> tgis.compute_relative_time_granularity(maps)
  132. 4
  133. >>> maps = []
  134. >>> count = 0
  135. >>> timelist = ((0,8), (8,12), (12,18))
  136. >>> for t in timelist:
  137. ... map = tgis.RasterDataset("a%i@P"%count)
  138. ... check = map.set_relative_time(t[0],t[1],"days")
  139. ... if check:
  140. ... maps.append(map)
  141. ... count += 1
  142. >>> tgis.compute_relative_time_granularity(maps)
  143. 2
  144. >>> maps = []
  145. >>> count = 0
  146. >>> timelist = ((0,None), (8,None), (12,None), (24,None))
  147. >>> for t in timelist:
  148. ... map = tgis.RasterDataset("a%i@P"%count)
  149. ... check = map.set_relative_time(t[0],t[1],"minutes")
  150. ... if check:
  151. ... maps.append(map)
  152. ... count += 1
  153. >>> tgis.compute_relative_time_granularity(maps)
  154. 4
  155. >>> maps = []
  156. >>> count = 0
  157. >>> timelist = ((0,None), (8,14), (18,None), (24,None))
  158. >>> for t in timelist:
  159. ... map = tgis.RasterDataset("a%i@P"%count)
  160. ... check = map.set_relative_time(t[0],t[1],"hours")
  161. ... if check:
  162. ... maps.append(map)
  163. ... count += 1
  164. >>> tgis.compute_relative_time_granularity(maps)
  165. 2
  166. """
  167. # The interval time must be scaled to days resolution
  168. granularity = None
  169. delta = []
  170. # First we compute the timedelta of the intervals
  171. for map in maps:
  172. start, end = map.get_temporal_extent_as_tuple()
  173. if start and end:
  174. t = abs(end - start)
  175. delta.append(int(t))
  176. # Compute the timedelta of the gaps
  177. for i in range(len(maps)):
  178. if i < len(maps) - 1:
  179. relation = maps[i + 1].temporal_relation(maps[i])
  180. if relation == "after":
  181. start1, end1 = maps[i].get_temporal_extent_as_tuple()
  182. start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
  183. # Gaps are between intervals, intervals and
  184. # points, points and points
  185. if end1 and start2:
  186. t = abs(end1 - start2)
  187. delta.append(int(t))
  188. if not end1 and start2:
  189. t = abs(start1 - start2)
  190. delta.append(int(t))
  191. delta.sort()
  192. ulist = list(set(delta))
  193. if len(ulist) > 1:
  194. # Find greatest common divisor
  195. granularity = gcd_list(ulist)
  196. elif len(ulist) == 1:
  197. granularity = ulist[0]
  198. else:
  199. granularity = 0
  200. return granularity
  201. ###############################################################################
  202. def compute_absolute_time_granularity(maps):
  203. """Compute the absolute time granularity
  204. Attention: The computation of the granularity
  205. is only correct in case of not overlapping intervals.
  206. Hence a correct temporal topology is required for computation.
  207. The computed granularity is returned as number of seconds or minutes or hours
  208. or days or months or years.
  209. :param maps: a ordered by start_time list of map objects
  210. :return: The temporal topology as string "integer unit"
  211. .. code-block:: python
  212. >>> import grass.temporal as tgis
  213. >>> import datetime
  214. >>> dt = datetime.datetime
  215. >>> tgis.init()
  216. >>> maps = []
  217. >>> count = 0
  218. >>> timelist = ((dt(2000,01,01),None), (dt(2000,02,01),None))
  219. >>> for t in timelist:
  220. ... map = tgis.RasterDataset("a%i@P"%count)
  221. ... check = map.set_absolute_time(t[0],t[1])
  222. ... if check:
  223. ... maps.append(map)
  224. ... count += 1
  225. >>> tgis.compute_absolute_time_granularity(maps)
  226. '1 month'
  227. >>> maps = []
  228. >>> count = 0
  229. >>> timelist = ((dt(2000,01,01),None), (dt(2000,01,02),None), (dt(2000,01,03),None))
  230. >>> for t in timelist:
  231. ... map = tgis.RasterDataset("a%i@P"%count)
  232. ... check = map.set_absolute_time(t[0],t[1])
  233. ... if check:
  234. ... maps.append(map)
  235. ... count += 1
  236. >>> tgis.compute_absolute_time_granularity(maps)
  237. '1 day'
  238. >>> maps = []
  239. >>> count = 0
  240. >>> timelist = ((dt(2000,01,01),None), (dt(2000,01,02),None), (dt(2000,05,04,0,5,30),None))
  241. >>> for t in timelist:
  242. ... map = tgis.RasterDataset("a%i@P"%count)
  243. ... check = map.set_absolute_time(t[0],t[1])
  244. ... if check:
  245. ... maps.append(map)
  246. ... count += 1
  247. >>> tgis.compute_absolute_time_granularity(maps)
  248. '30 seconds'
  249. >>> maps = []
  250. >>> count = 0
  251. >>> timelist = ((dt(2000,01,01),dt(2000,05,02)), (dt(2000,05,04,2),None))
  252. >>> for t in timelist:
  253. ... map = tgis.RasterDataset("a%i@P"%count)
  254. ... check = map.set_absolute_time(t[0],t[1])
  255. ... if check:
  256. ... maps.append(map)
  257. ... count += 1
  258. >>> tgis.compute_absolute_time_granularity(maps)
  259. '2 hours'
  260. >>> maps = []
  261. >>> count = 0
  262. >>> timelist = ((dt(2000,01,01),dt(2000,02,01)), (dt(2005,05,04,12),dt(2007,05,20,6)))
  263. >>> for t in timelist:
  264. ... map = tgis.RasterDataset("a%i@P"%count)
  265. ... check = map.set_absolute_time(t[0],t[1])
  266. ... if check:
  267. ... maps.append(map)
  268. ... count += 1
  269. >>> tgis.compute_absolute_time_granularity(maps)
  270. '6 hours'
  271. """
  272. has_seconds = False
  273. has_minutes = False
  274. has_hours = False
  275. has_days = False
  276. has_months = False
  277. has_years = False
  278. use_seconds = False
  279. use_minutes = False
  280. use_hours = False
  281. use_days = False
  282. use_months = False
  283. use_years = False
  284. delta = []
  285. datetime_delta = []
  286. # First we compute the timedelta of the intervals
  287. for map in maps:
  288. start, end = map.get_temporal_extent_as_tuple()
  289. if start and end:
  290. delta.append(end - start)
  291. datetime_delta.append(compute_datetime_delta(start, end))
  292. # Compute the timedelta of the gaps
  293. for i in range(len(maps)):
  294. if i < len(maps) - 1:
  295. relation = maps[i + 1].temporal_relation(maps[i])
  296. if relation == "after":
  297. start1, end1 = maps[i].get_temporal_extent_as_tuple()
  298. start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
  299. # Gaps are between intervals, intervals and
  300. # points, points and points
  301. if end1 and start2:
  302. delta.append(end1 - start2)
  303. datetime_delta.append(compute_datetime_delta(end1, start2))
  304. if not end1 and start2:
  305. delta.append(start2 - start1)
  306. datetime_delta.append(compute_datetime_delta(
  307. start1, start2))
  308. # Check what changed
  309. dlist = []
  310. for d in datetime_delta:
  311. if "second" in d and d["second"] > 0:
  312. has_seconds = True
  313. #print "has second"
  314. if "minute" in d and d["minute"] > 0:
  315. has_minutes = True
  316. #print "has minute"
  317. if "hour" in d and d["hour"] > 0:
  318. has_hours = True
  319. #print "has hour"
  320. if "day" in d and d["day"] > 0:
  321. has_days = True
  322. #print "has day"
  323. if "month" in d and d["month"] > 0:
  324. has_months = True
  325. #print "has month"
  326. if "year" in d and d["year"] > 0:
  327. has_years = True
  328. #print "has year"
  329. # Create a list with a single time unit only
  330. if has_seconds:
  331. for d in datetime_delta:
  332. if "second" in d and d["second"] > 0:
  333. dlist.append(d["second"])
  334. elif "minute" in d and d["minute"] > 0:
  335. dlist.append(d["minute"] * 60)
  336. elif "hour" in d and d["hour"] > 0:
  337. dlist.append(d["hour"] * 3600)
  338. elif "day" in d and d["day"] > 0:
  339. dlist.append(d["day"] * 24 * 3600)
  340. else:
  341. dlist.append(d["max_days"] * 24 * 3600)
  342. use_seconds = True
  343. elif has_minutes:
  344. for d in datetime_delta:
  345. if "minute" in d and d["minute"] > 0:
  346. dlist.append(d["minute"])
  347. elif "hour" in d and d["hour"] > 0:
  348. dlist.append(d["hour"] * 60)
  349. elif "day" in d:
  350. dlist.append(d["day"] * 24 * 60)
  351. else:
  352. dlist.append(d["max_days"] * 24 * 60)
  353. use_minutes = True
  354. elif has_hours:
  355. for d in datetime_delta:
  356. if "hour" in d and d["hour"] > 0:
  357. dlist.append(d["hour"])
  358. elif "day" in d and d["day"] > 0:
  359. dlist.append(d["day"] * 24)
  360. else:
  361. dlist.append(d["max_days"] * 24)
  362. use_hours = True
  363. elif has_days:
  364. for d in datetime_delta:
  365. if "day" in d and d["day"] > 0:
  366. dlist.append(d["day"])
  367. else:
  368. dlist.append(d["max_days"])
  369. use_days = True
  370. elif has_months:
  371. for d in datetime_delta:
  372. if "month" in d and d["month"] > 0:
  373. dlist.append(d["month"])
  374. elif "year" in d and d["year"] > 0:
  375. dlist.append(d["year"] * 12)
  376. use_months = True
  377. elif has_years:
  378. for d in datetime_delta:
  379. if "year" in d:
  380. dlist.append(d["year"])
  381. use_years = True
  382. dlist.sort()
  383. ulist = list(set(dlist))
  384. if len(ulist) == 0:
  385. return None
  386. if len(ulist) > 1:
  387. # Find greatest common divisor
  388. granularity = gcd_list(ulist)
  389. else:
  390. granularity = ulist[0]
  391. if use_seconds:
  392. if granularity == 1:
  393. return "%i second" % granularity
  394. else:
  395. return "%i seconds" % granularity
  396. elif use_minutes:
  397. if granularity == 1:
  398. return "%i minute" % granularity
  399. else:
  400. return "%i minutes" % granularity
  401. elif use_hours:
  402. if granularity == 1:
  403. return "%i hour" % granularity
  404. else:
  405. return "%i hours" % granularity
  406. elif use_days:
  407. if granularity == 1:
  408. return "%i day" % granularity
  409. else:
  410. return "%i days" % granularity
  411. elif use_months:
  412. if granularity == 1:
  413. return "%i month" % granularity
  414. else:
  415. return "%i months" % granularity
  416. elif use_years:
  417. if granularity == 1:
  418. return "%i year" % granularity
  419. else:
  420. return "%i years" % granularity
  421. return None
  422. ###############################################################################
  423. # http://akiscode.com/articles/gcd_of_a_list.shtml
  424. # Copyright (c) 2010 Stephen Akiki
  425. # MIT License (Means you can do whatever you want with this)
  426. # See http://www.opensource.org/licenses/mit-license.php
  427. # Error Codes:
  428. # None
  429. def gcd(a, b):
  430. """The Euclidean Algorithm """
  431. a = abs(a)
  432. b = abs(b)
  433. while a:
  434. a, b = b % a, a
  435. return b
  436. ###############################################################################
  437. def gcd_list(list):
  438. """Finds the GCD of numbers in a list.
  439. Input: List of numbers you want to find the GCD of
  440. E.g. [8, 24, 12]
  441. Returns: GCD of all numbers
  442. """
  443. return reduce(gcd, list)
  444. ###############################################################################
  445. if __name__ == "__main__":
  446. import doctest
  447. doctest.testmod()