mapcalc.py 26 KB

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