mapcalc.py 26 KB

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