mapcalc.py 26 KB

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