mapcalc.py 26 KB

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