temporal_granularity.py 17 KB

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