mapcalc.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. (C) 2012-2013 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. @author Soeren Gebbert
  9. """
  10. from space_time_datasets import *
  11. from open_stds import *
  12. from multiprocessing import Process
  13. import grass.script as gscript
  14. ############################################################################
  15. def dataset_mapcalculator(inputs, output, type, expression, base, method,
  16. nprocs=1, register_null=False, spatial=False):
  17. """!Perform map-calculations of maps from different space time
  18. raster/raster3d datasets, using a specific sampling method
  19. to select temporal related maps.
  20. A mapcalc expression must be provided to process the temporal
  21. selected maps. Temporal operators are available in addition to
  22. the r.mapcalc operators:
  23. Supported operators for relative and absolute time are:
  24. - td() - the time delta of the current interval in days
  25. and fractions of days or the unit in case of relative time
  26. - start_time() - The start time of the interval from the begin of
  27. the time series in days and fractions of days or the
  28. unit in case of relative time
  29. - end_time() - The end time of the current interval from the begin of
  30. the time series in days and fractions of days or the
  31. unit in case of relative time
  32. Supported operators for absolute time:
  33. - start_doy() - Day of year (doy) from the start time [1 - 366]
  34. - start_dow() - Day of week (dow) from the start time [1 - 7],
  35. the start of the week is monday == 1
  36. - start_year() - The year of the start time [0 - 9999]
  37. - start_month() - The month of the start time [1 - 12]
  38. - start_week() - Week of year of the start time [1 - 54]
  39. - start_day() - Day of month from the start time [1 - 31]
  40. - start_hour() - The hour of the start time [0 - 23]
  41. - start_minute() - The minute of the start time [0 - 59]
  42. - start_second() - The second of the start time [0 - 59]
  43. - end_doy() - Day of year (doy) from the end time [1 - 366]
  44. - end_dow() - Day of week (dow) from the end time [1 - 7],
  45. the start of the week is monday == 1
  46. - end_year() - The year of the end time [0 - 9999]
  47. - end_month() - The month of the end time [1 - 12]
  48. - end_week() - Week of year of the end time [1 - 54]
  49. - end_day() - Day of month from the end time [1 - 31]
  50. - end_hour() - The hour of the end time [0 - 23]
  51. - end_minute() - The minute of the end time [0 - 59]
  52. - end_second() - The minute of the end time [0 - 59]
  53. @param inputs The names of the input space time raster/raster3d datasets
  54. @param output The name of the extracted new space time raster(3d) dataset
  55. @param type The type of the dataset: "raster" or "raster3d"
  56. @param expression The r(3).mapcalc expression
  57. @param base The base name of the new created maps in case a
  58. mapclac expression is provided
  59. @param method The method to be used for temporal sampling
  60. @param nprocs The number of parallel processes to be used for
  61. mapcalc processing
  62. @param register_null Set this number True to register empty maps
  63. @param spatial Check spatial overlap
  64. """
  65. # We need a database interface for fast computation
  66. dbif = SQLDatabaseInterfaceConnection()
  67. dbif.connect()
  68. mapset = get_current_mapset()
  69. msgr = get_tgis_message_interface()
  70. input_name_list = inputs.split(",")
  71. first_input = open_old_space_time_dataset(input_name_list[0], type, dbif)
  72. # All additional inputs in reverse sorted order to avoid
  73. # wrong name substitution
  74. input_name_list = input_name_list[1:]
  75. input_name_list.sort()
  76. input_name_list.reverse()
  77. input_list = []
  78. for input in input_name_list:
  79. sp = open_old_space_time_dataset(input, type, dbif)
  80. input_list.append(copy.copy(sp))
  81. new_sp = check_new_space_time_dataset(output, type, dbif,
  82. gscript.overwrite())
  83. # Sample all inputs by the first input and create a sample matrix
  84. if spatial:
  85. msgr.message(_("Starting spatio-temporal sampling..."))
  86. else:
  87. msgr.message(_("Starting temporal sampling..."))
  88. map_matrix = []
  89. id_list = []
  90. sample_map_list = []
  91. # First entry is the first dataset id
  92. id_list.append(first_input.get_name())
  93. if len(input_list) > 0:
  94. has_samples = False
  95. for dataset in input_list:
  96. list = dataset.sample_by_dataset(stds=first_input,
  97. method=method, spatial=spatial,
  98. dbif=dbif)
  99. # In case samples are not found
  100. if not list and len(list) == 0:
  101. dbif.close()
  102. msgr.message(_("No samples found for map calculation"))
  103. return 0
  104. # The fist entries are the samples
  105. map_name_list = []
  106. if not has_samples:
  107. for entry in list:
  108. granule = entry["granule"]
  109. # Do not consider gaps
  110. if granule.get_id() is None:
  111. continue
  112. sample_map_list.append(granule)
  113. map_name_list.append(granule.get_name())
  114. # Attach the map names
  115. map_matrix.append(copy.copy(map_name_list))
  116. has_samples = True
  117. map_name_list = []
  118. for entry in list:
  119. maplist = entry["samples"]
  120. granule = entry["granule"]
  121. # Do not consider gaps in the sampler
  122. if granule.get_id() is None:
  123. continue
  124. if len(maplist) > 1:
  125. msgr.warning(_("Found more than a single map in a sample "
  126. "granule. Only the first map is used for "
  127. "computation. Use t.rast.aggregate.ds to "
  128. "create synchronous raster datasets."))
  129. # Store all maps! This includes non existent maps,
  130. # identified by id == None
  131. map_name_list.append(maplist[0].get_name())
  132. # Attach the map names
  133. map_matrix.append(copy.copy(map_name_list))
  134. id_list.append(dataset.get_name())
  135. else:
  136. list = first_input.get_registered_maps_as_objects(dbif=dbif)
  137. if list is None:
  138. dbif.close()
  139. msgr.message(_("No maps registered in input dataset"))
  140. return 0
  141. map_name_list = []
  142. for map in list:
  143. map_name_list.append(map.get_name())
  144. sample_map_list.append(map)
  145. # Attach the map names
  146. map_matrix.append(copy.copy(map_name_list))
  147. # Needed for map registration
  148. map_list = []
  149. if len(map_matrix) > 0:
  150. msgr.message(_("Starting mapcalc computation..."))
  151. count = 0
  152. # Get the number of samples
  153. num = len(map_matrix[0])
  154. # Parallel processing
  155. proc_list = []
  156. proc_count = 0
  157. # For all samples
  158. for i in range(num):
  159. count += 1
  160. if count % 10 == 0:
  161. msgr.percent(count, num, 1)
  162. # Create the r.mapcalc statement for the current time step
  163. map_name = "{base}_{suffix}".format(base=base,
  164. suffix=gscript.get_num_suffix(count, num))
  165. # Remove spaces and new lines
  166. expr = expression.replace(" ", "")
  167. # Check that all maps are in the sample
  168. valid_maps = True
  169. # Replace all dataset names with their map names of the
  170. # current time step
  171. for j in range(len(map_matrix)):
  172. if map_matrix[j][i] is None:
  173. valid_maps = False
  174. break
  175. # Substitute the dataset name with the map name
  176. expr = expr.replace(id_list[j], map_matrix[j][i])
  177. # Proceed with the next sample
  178. if not valid_maps:
  179. continue
  180. # Create the new map id and check if the map is already
  181. # in the database
  182. map_id = map_name + "@" + mapset
  183. new_map = first_input.get_new_map_instance(map_id)
  184. # Check if new map is in the temporal database
  185. if new_map.is_in_db(dbif):
  186. if gscript.overwrite():
  187. # Remove the existing temporal database entry
  188. new_map.delete(dbif)
  189. new_map = first_input.get_new_map_instance(map_id)
  190. else:
  191. msgr.error(_("Map <%s> is already in temporal database, "
  192. "use overwrite flag to overwrite"))
  193. continue
  194. # Set the time stamp
  195. if sample_map_list[i].is_time_absolute():
  196. start, end = sample_map_list[i].get_absolute_time()
  197. new_map.set_absolute_time(start, end)
  198. else:
  199. start, end, unit = sample_map_list[i].get_relative_time()
  200. new_map.set_relative_time(start, end, unit)
  201. # Parse the temporal expressions
  202. expr = _operator_parser(expr, sample_map_list[0],
  203. sample_map_list[i])
  204. # Add the output map name
  205. expr = "%s=%s" % (map_name, expr)
  206. map_list.append(new_map)
  207. msgr.verbose(_("Apply mapcalc expression: \"%s\"") % expr)
  208. # Start the parallel r.mapcalc computation
  209. if type == "raster":
  210. proc_list.append(Process(target=_run_mapcalc2d, args=(expr,)))
  211. else:
  212. proc_list.append(Process(target=_run_mapcalc3d, args=(expr,)))
  213. proc_list[proc_count].start()
  214. proc_count += 1
  215. if proc_count == nprocs or proc_count == num or count == num:
  216. proc_count = 0
  217. exitcodes = 0
  218. for proc in proc_list:
  219. proc.join()
  220. exitcodes += proc.exitcode
  221. if exitcodes != 0:
  222. dbif.close()
  223. msgr.fatal(_("Error while mapcalc computation"))
  224. # Empty process list
  225. proc_list = []
  226. # Register the new maps in the output space time dataset
  227. msgr.message(_("Starting map registration in temporal database..."))
  228. temporal_type, semantic_type, title, description = first_input.get_initial_values()
  229. new_sp = open_new_space_time_dataset(output, type,
  230. temporal_type, title, description,
  231. semantic_type, dbif,
  232. gscript.overwrite())
  233. count = 0
  234. # collect empty maps to remove them
  235. empty_maps = []
  236. # Insert maps in the temporal database and in the new space time
  237. # dataset
  238. for new_map in map_list:
  239. count += 1
  240. if count % 10 == 0:
  241. msgr.percent(count, num, 1)
  242. # Read the map data
  243. new_map.load()
  244. # In case of a null map continue, do not register null maps
  245. if new_map.metadata.get_min() is None and \
  246. new_map.metadata.get_max() is None:
  247. if not register_null:
  248. empty_maps.append(new_map)
  249. continue
  250. # Insert map in temporal database
  251. new_map.insert(dbif)
  252. new_sp.register_map(new_map, dbif)
  253. # Update the spatio-temporal extent and the metadata table entries
  254. new_sp.update_from_registered_maps(dbif)
  255. msgr.percent(1, 1, 1)
  256. # Remove empty maps
  257. if len(empty_maps) > 0:
  258. names = ""
  259. count = 0
  260. for map in empty_maps:
  261. if count == 0:
  262. names += "%s" % (map.get_name())
  263. else:
  264. names += ",%s" % (map.get_name())
  265. count += 1
  266. if type == "raster":
  267. gscript.run_command("g.remove", type='rast', name=names, quiet=True, flags='f')
  268. elif type == "raster3d":
  269. gscript.run_command("g.remove", type='rast3d', name=names, quiet=True, flags='f')
  270. dbif.close()
  271. ###############################################################################
  272. def _run_mapcalc2d(expr):
  273. """Helper function to run r.mapcalc in parallel"""
  274. exit(gscript.run_command("r.mapcalc", expression=expr,
  275. overwrite=gscript.overwrite(), quiet=True))
  276. ###############################################################################
  277. def _run_mapcalc3d(expr):
  278. """Helper function to run r3.mapcalc in parallel"""
  279. exit(gscript.run_command("r3.mapcalc", expression=expr,
  280. overwrite=gscript.overwrite(), quiet=True))
  281. ###############################################################################
  282. def _operator_parser(expr, first, current):
  283. """This method parses the expression string and substitutes
  284. the temporal operators with numerical values.
  285. Supported operators for relative and absolute time are:
  286. * td() - the time delta of the current interval in days
  287. and fractions of days or the unit in case of relative time
  288. * start_time() - The start time of the interval from the begin of the
  289. time series in days and fractions of days or the unit
  290. in case of relative time
  291. * end_time() - The end time of the current interval from the begin of
  292. the time series in days and fractions of days or the
  293. unit in case of relative time
  294. Supported operators for absolute time:
  295. * start_doy() - Day of year (doy) from the start time [1 - 366]
  296. * start_dow() - Day of week (dow) from the start time [1 - 7],
  297. the start of the week is monday == 1
  298. * start_year() - The year of the start time [0 - 9999]
  299. * start_month() - The month of the start time [1 - 12]
  300. * start_week() - Week of year of the start time [1 - 54]
  301. * start_day() - Day of month from the start time [1 - 31]
  302. * start_hour() - The hour of the start time [0 - 23]
  303. * start_minute() - The minute of the start time [0 - 59]
  304. * start_second() - The second of the start time [0 - 59]
  305. * end_doy() - Day of year (doy) from the end time [1 - 366]
  306. * end_dow() - Day of week (dow) from the end time [1 - 7],
  307. the start of the week is monday == 1
  308. * end_year() - The year of the end time [0 - 9999]
  309. * end_month() - The month of the end time [1 - 12]
  310. * end_week() - Week of year of the end time [1 - 54]
  311. * end_day() - Day of month from the end time [1 - 31]
  312. * end_hour() - The hour of the end time [0 - 23]
  313. * end_minute() - The minute of the end time [0 - 59]
  314. * end_second() - The minute of the end time [0 - 59]
  315. The modified expression is returned.
  316. """
  317. is_time_absolute = first.is_time_absolute()
  318. expr = _parse_td_operator(expr, is_time_absolute, first, current)
  319. expr = _parse_start_time_operator(expr, is_time_absolute, first, current)
  320. expr = _parse_end_time_operator(expr, is_time_absolute, first, current)
  321. expr = _parse_start_operators(expr, is_time_absolute, current)
  322. expr = _parse_end_operators(expr, is_time_absolute, current)
  323. return expr
  324. ###############################################################################
  325. def _parse_start_operators(expr, is_time_absolute, current):
  326. """
  327. Supported operators for absolute time:
  328. * start_doy() - Day of year (doy) from the start time [1 - 366]
  329. * start_dow() - Day of week (dow) from the start time [1 - 7],
  330. the start of the week is monday == 1
  331. * start_year() - The year of the start time [0 - 9999]
  332. * start_month() - The month of the start time [1 - 12]
  333. * start_week() - Week of year of the start time [1 - 54]
  334. * start_day() - Day of month from the start time [1 - 31]
  335. * start_hour() - The hour of the start time [0 - 23]
  336. * start_minute() - The minute of the start time [0 - 59]
  337. * start_second() - The second of the start time [0 - 59]
  338. """
  339. start, end = current.get_absolute_time()
  340. msgr = get_tgis_message_interface()
  341. if expr.find("start_year()") >= 0:
  342. if not is_time_absolute:
  343. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  344. "time." % ("start_*")))
  345. expr = expr.replace("start_year()", str(start.year))
  346. if expr.find("start_month()") >= 0:
  347. if not is_time_absolute:
  348. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  349. "time." % ("start_*")))
  350. expr = expr.replace("start_month()", str(start.month))
  351. if expr.find("start_week()") >= 0:
  352. if not is_time_absolute:
  353. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  354. "time." % ("start_*")))
  355. expr = expr.replace("start_week()", str(start.isocalendar()[1]))
  356. if expr.find("start_day()") >= 0:
  357. if not is_time_absolute:
  358. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  359. "time." % ("start_*")))
  360. expr = expr.replace("start_day()", str(start.day))
  361. if expr.find("start_hour()") >= 0:
  362. if not is_time_absolute:
  363. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  364. "time." % ("start_*")))
  365. expr = expr.replace("start_hour()", str(start.hour))
  366. if expr.find("start_minute()") >= 0:
  367. if not is_time_absolute:
  368. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  369. "time." % ("start_*")))
  370. expr = expr.replace("start_minute()", str(start.minute))
  371. if expr.find("start_second()") >= 0:
  372. if not is_time_absolute:
  373. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  374. "time." % ("start_*")))
  375. expr = expr.replace("start_second()", str(start.second))
  376. if expr.find("start_dow()") >= 0:
  377. if not is_time_absolute:
  378. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  379. "time." % ("start_*")))
  380. expr = expr.replace("start_dow()", str(start.isoweekday()))
  381. if expr.find("start_doy()") >= 0:
  382. if not is_time_absolute:
  383. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  384. "time." % ("start_*")))
  385. year = datetime(start.year, 1, 1)
  386. delta = start - year
  387. expr = expr.replace("start_doy()", str(delta.days + 1))
  388. return expr
  389. ###############################################################################
  390. def _parse_end_operators(expr, is_time_absolute, current):
  391. """
  392. Supported operators for absolute time:
  393. - end_doy() - Day of year (doy) from the end time [1 - 366]
  394. - end_dow() - Day of week (dow) from the end time [1 - 7],
  395. the start of the week is monday == 1
  396. - end_year() - The year of the end time [0 - 9999]
  397. - end_month() - The month of the end time [1 - 12]
  398. - end_week() - Week of year of the end time [1 - 54]
  399. - end_day() - Day of month from the end time [1 - 31]
  400. - end_hour() - The hour of the end time [0 - 23]
  401. - end_minute() - The minute of the end time [0 - 59]
  402. - end_second() - The minute of the end time [0 - 59]
  403. In case of time instances the end_* expression will be replaced by
  404. null()
  405. """
  406. start, end = current.get_absolute_time()
  407. msgr = get_tgis_message_interface()
  408. if expr.find("end_year()") >= 0:
  409. if not is_time_absolute:
  410. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  411. "time." % ("end_*")))
  412. if not end:
  413. expr = expr.replace("end_year()", "null()")
  414. else:
  415. expr = expr.replace("end_year()", str(end.year))
  416. if expr.find("end_month()") >= 0:
  417. if not is_time_absolute:
  418. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  419. "time." % ("end_*")))
  420. if not end:
  421. expr = expr.replace("end_month()", "null()")
  422. else:
  423. expr = expr.replace("end_month()", str(end.month))
  424. if expr.find("end_week()") >= 0:
  425. if not is_time_absolute:
  426. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  427. "time." % ("end_*")))
  428. if not end:
  429. expr = expr.replace("end_week()", "null()")
  430. else:
  431. expr = expr.replace("end_week()", str(end.isocalendar()[1]))
  432. if expr.find("end_day()") >= 0:
  433. if not is_time_absolute:
  434. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  435. "time." % ("end_*")))
  436. if not end:
  437. expr = expr.replace("end_day()", "null()")
  438. else:
  439. expr = expr.replace("end_day()", str(end.day))
  440. if expr.find("end_hour()") >= 0:
  441. if not is_time_absolute:
  442. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  443. "time." % ("end_*")))
  444. if not end:
  445. expr = expr.replace("end_hour()", "null()")
  446. else:
  447. expr = expr.replace("end_hour()", str(end.hour))
  448. if expr.find("end_minute()") >= 0:
  449. if not is_time_absolute:
  450. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  451. "time." % ("end_*")))
  452. if not end:
  453. expr = expr.replace("end_minute()", "null()")
  454. else:
  455. expr = expr.replace("end_minute()", str(end.minute))
  456. if expr.find("end_second()") >= 0:
  457. if not is_time_absolute:
  458. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  459. "time." % ("end_*")))
  460. if not end:
  461. expr = expr.replace("end_second()", "null()")
  462. else:
  463. expr = expr.replace("end_second()", str(end.second))
  464. if expr.find("end_dow()") >= 0:
  465. if not is_time_absolute:
  466. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  467. "time." % ("end_*")))
  468. if not end:
  469. expr = expr.replace("end_dow()", "null()")
  470. else:
  471. expr = expr.replace("end_dow()", str(end.isoweekday()))
  472. if expr.find("end_doy()") >= 0:
  473. if not is_time_absolute:
  474. msgr.fatal(_("The temporal operators <%s> support only absolute "\
  475. "time." % ("end_*")))
  476. if not end:
  477. expr = expr.replace("end_doy()", "null()")
  478. else:
  479. year = datetime(end.year, 1, 1)
  480. delta = end - year
  481. expr = expr.replace("end_doy()", str(delta.days + 1))
  482. return expr
  483. ###############################################################################
  484. def _parse_td_operator(expr, is_time_absolute, first, current):
  485. """Parse the time delta operator td(). This operator
  486. represents the size of the current sample time interval
  487. in days and fraction of days for absolute time,
  488. and in relative units in case of relative time.
  489. In case of time instances, the td() operator will be of type null().
  490. """
  491. if expr.find("td()") >= 0:
  492. td = "null()"
  493. if is_time_absolute:
  494. start, end = current.get_absolute_time()
  495. if end != None:
  496. td = time_delta_to_relative_time(end - start)
  497. else:
  498. start, end, unit = current.get_relative_time()
  499. if end != None:
  500. td = end - start
  501. expr = expr.replace("td()", str(td))
  502. return expr
  503. ###############################################################################
  504. def _parse_start_time_operator(expr, is_time_absolute, first, current):
  505. """Parse the start_time() operator. This operator represent
  506. the time difference between the start time of the sample space time
  507. raster dataset and the start time of the current sample interval or
  508. instance. The time is measured in days and fraction of days for absolute
  509. time, and in relative units in case of relative time."""
  510. if expr.find("start_time()") >= 0:
  511. if is_time_absolute:
  512. start1, end = first.get_absolute_time()
  513. start, end = current.get_absolute_time()
  514. x = time_delta_to_relative_time(start - start1)
  515. else:
  516. start1, end, unit = first.get_relative_time()
  517. start, end, unit = current.get_relative_time()
  518. x = start - start1
  519. expr = expr.replace("start_time()", str(x))
  520. return expr
  521. ###############################################################################
  522. def _parse_end_time_operator(expr, is_time_absolute, first, current):
  523. """Parse the end_time() operator. This operator represent
  524. the time difference between the start time of the sample space time
  525. raster dataset and the end time of the current sample interval. The time
  526. is measured in days and fraction of days for absolute time,
  527. and in relative units in case of relative time.
  528. The end_time() will be represented by null() in case of a time instance.
  529. """
  530. if expr.find("end_time()") >= 0:
  531. x = "null()"
  532. if is_time_absolute:
  533. start1, end = first.get_absolute_time()
  534. start, end = current.get_absolute_time()
  535. if end:
  536. x = time_delta_to_relative_time(end - start1)
  537. else:
  538. start1, end, unit = first.get_relative_time()
  539. start, end, unit = current.get_relative_time()
  540. if end:
  541. x = end - start1
  542. expr = expr.replace("end_time()", str(x))
  543. return expr