temporal_granularity.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. def compute_common_absolute_time_granularity(gran_list):
  425. """Compute the greatest common granule from a list of absolute time granules
  426. .. code-block:: python
  427. >>> import grass.temporal as tgis
  428. >>> tgis.init()
  429. >>> grans = ["1 second", "2 seconds", "30 seconds"]
  430. >>> tgis.compute_common_absolute_time_granularity(grans)
  431. '1 seconds'
  432. >>> grans = ["3 second", "6 seconds", "30 seconds"]
  433. >>> tgis.compute_common_absolute_time_granularity(grans)
  434. '3 seconds'
  435. >>> grans = ["12 second", "18 seconds", "30 seconds", "10 minutes"]
  436. >>> tgis.compute_common_absolute_time_granularity(grans)
  437. '6 seconds'
  438. >>> grans = ["20 second", "10 minutes", "2 hours"]
  439. >>> tgis.compute_common_absolute_time_granularity(grans)
  440. '20 seconds'
  441. >>> grans = ["7200 second", "240 minutes", "1 year"]
  442. >>> tgis.compute_common_absolute_time_granularity(grans)
  443. '7200 seconds'
  444. >>> grans = ["7200 second", "89 minutes", "1 year"]
  445. >>> tgis.compute_common_absolute_time_granularity(grans)
  446. '60 seconds'
  447. >>> grans = ["10 minutes", "20 minutes", "30 minutes", "40 minutes", "2 hours"]
  448. >>> tgis.compute_common_absolute_time_granularity(grans)
  449. '10 minutes'
  450. >>> grans = ["120 minutes", "2 hours"]
  451. >>> tgis.compute_common_absolute_time_granularity(grans)
  452. '120 minutes'
  453. >>> grans = ["360 minutes", "3 hours"]
  454. >>> tgis.compute_common_absolute_time_granularity(grans)
  455. '180 minutes'
  456. >>> grans = ["2 hours", "4 hours", "8 hours"]
  457. >>> tgis.compute_common_absolute_time_granularity(grans)
  458. '2 hours'
  459. >>> grans = ["8 hours", "2 days"]
  460. >>> tgis.compute_common_absolute_time_granularity(grans)
  461. '8 hours'
  462. >>> grans = ["48 hours", "1 month"]
  463. >>> tgis.compute_common_absolute_time_granularity(grans)
  464. '24 hours'
  465. >>> grans = ["48 hours", "1 year"]
  466. >>> tgis.compute_common_absolute_time_granularity(grans)
  467. '24 hours'
  468. >>> grans = ["2 months", "4 months", "1 year"]
  469. >>> tgis.compute_common_absolute_time_granularity(grans)
  470. '2 months'
  471. >>> grans = ["120 months", "360 months", "4 years"]
  472. >>> tgis.compute_common_absolute_time_granularity(grans)
  473. '24 months'
  474. >>> grans = ["120 months", "361 months", "4 years"]
  475. >>> tgis.compute_common_absolute_time_granularity(grans)
  476. '1 months'
  477. >>> grans = ["2 years", "3 years", "4 years"]
  478. >>> tgis.compute_common_absolute_time_granularity(grans)
  479. '1 years'
  480. """
  481. has_seconds = False # 0
  482. has_minutes = False # 1
  483. has_hours = False # 2
  484. has_days = False # 3
  485. has_months = False # 4
  486. has_years = False # 5
  487. seconds = []
  488. minutes = []
  489. hours = []
  490. days = []
  491. months = []
  492. years = []
  493. min_gran = 6
  494. max_gran = -1
  495. for entry in gran_list:
  496. if not check_granularity_string(entry, "absolute"):
  497. return False
  498. num, gran = entry.split()
  499. if gran in ["seconds", "second"]:
  500. has_seconds = True
  501. if min_gran > 0:
  502. min_gran = 0
  503. if max_gran < 0:
  504. max_gran = 0
  505. seconds.append(int(num))
  506. if gran in ["minutes", "minute"]:
  507. has_minutes = True
  508. if min_gran > 1:
  509. min_gran = 1
  510. if max_gran < 1:
  511. max_gran = 1
  512. minutes.append(int(num))
  513. if gran in ["hours", "hour"]:
  514. has_hours = True
  515. if min_gran > 2:
  516. min_gran = 2
  517. if max_gran < 2:
  518. max_gran = 2
  519. hours.append(int(num))
  520. if gran in ["days", "day"]:
  521. has_days = True
  522. if min_gran > 3:
  523. min_gran = 3
  524. if max_gran < 3:
  525. max_gran = 3
  526. days.append(int(num))
  527. if gran in ["months", "month"]:
  528. has_months = True
  529. if min_gran > 4:
  530. min_gran = 4
  531. if max_gran < 4:
  532. max_gran = 4
  533. months.append(int(num))
  534. if gran in ["years", "year"]:
  535. has_years = True
  536. if min_gran > 5:
  537. min_gran = 5
  538. if max_gran < 5:
  539. max_gran = 5
  540. years.append(int(num))
  541. if has_seconds:
  542. if has_minutes:
  543. minutes.sort()
  544. seconds.append(minutes[0]*60)
  545. if has_hours:
  546. hours.sort()
  547. seconds.append(hours[0]*60*60)
  548. if has_days:
  549. days.sort()
  550. seconds.append(days[0]*60*60*24)
  551. if has_months:
  552. months.sort()
  553. seconds.append(months[0]*60*60*24*28)
  554. seconds.append(months[0]*60*60*24*29)
  555. seconds.append(months[0]*60*60*24*30)
  556. seconds.append(months[0]*60*60*24*31)
  557. if has_years:
  558. years.sort()
  559. seconds.append(years[0]*60*60*24*365)
  560. seconds.append(years[0]*60*60*24*366)
  561. num = gcd_list(seconds)
  562. return "%i %s"%(num, "seconds")
  563. elif has_minutes:
  564. if has_hours:
  565. hours.sort()
  566. minutes.append(hours[0]*60)
  567. if has_days:
  568. days.sort()
  569. minutes.append(days[0]*60*24)
  570. if has_months:
  571. months.sort()
  572. minutes.append(months[0]*60*24*28)
  573. minutes.append(months[0]*60*24*29)
  574. minutes.append(months[0]*60*24*30)
  575. minutes.append(months[0]*60*24*31)
  576. if has_years:
  577. years.sort()
  578. minutes.append(years[0]*60*24*365)
  579. minutes.append(years[0]*60*24*366)
  580. num = gcd_list(minutes)
  581. return "%i %s"%(num, "minutes")
  582. elif has_hours:
  583. if has_days:
  584. days.sort()
  585. hours.append(days[0]*24)
  586. if has_months:
  587. months.sort()
  588. hours.append(months[0]*24*28)
  589. hours.append(months[0]*24*29)
  590. hours.append(months[0]*24*30)
  591. hours.append(months[0]*24*31)
  592. if has_years:
  593. years.sort()
  594. hours.append(years[0]*24*365)
  595. hours.append(years[0]*24*366)
  596. num = gcd_list(hours)
  597. return "%i %s"%(num, "hours")
  598. elif has_days:
  599. if has_months:
  600. months.sort()
  601. days.append(months[0]*28)
  602. days.append(months[0]*29)
  603. days.append(months[0]*30)
  604. days.append(months[0]*31)
  605. if has_years:
  606. years.sort()
  607. days.append(years[0]*365)
  608. days.append(years[0]*366)
  609. num = gcd_list(days)
  610. return "%i %s"%(num, "days")
  611. elif has_months:
  612. if has_years:
  613. years.sort()
  614. months.append(years[0]*12)
  615. num = gcd_list(months)
  616. return "%i %s"%(num, "months")
  617. elif has_years:
  618. num = gcd_list(years)
  619. return "%i %s"%(num, "years")
  620. ###############################################################################
  621. # http://akiscode.com/articles/gcd_of_a_list.shtml
  622. # Copyright (c) 2010 Stephen Akiki
  623. # MIT License (Means you can do whatever you want with this)
  624. # See http://www.opensource.org/licenses/mit-license.php
  625. # Error Codes:
  626. # None
  627. def gcd(a, b):
  628. """The Euclidean Algorithm """
  629. a = abs(a)
  630. b = abs(b)
  631. while a:
  632. a, b = b % a, a
  633. return b
  634. ###############################################################################
  635. def gcd_list(list):
  636. """Finds the GCD of numbers in a list.
  637. :param list: List of numbers you want to find the GCD of
  638. E.g. [8, 24, 12]
  639. :return: GCD of all numbers
  640. """
  641. return reduce(gcd, list)
  642. ###############################################################################
  643. if __name__ == "__main__":
  644. import doctest
  645. doctest.testmod()