aggregation.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. """
  2. Aggregation methods for space time raster datasets
  3. Usage:
  4. .. code-block:: python
  5. import grass.temporal as tgis
  6. tgis.aggregate_raster_maps(dataset, mapset, inputs, base, start, end, count, method, register_null, dbif)
  7. (C) 2012-2013 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. :author: Soeren Gebbert
  12. """
  13. import grass.script as gscript
  14. from grass.exceptions import CalledModuleError
  15. from .space_time_datasets import RasterDataset
  16. from .datetime_math import create_suffix_from_datetime
  17. from .datetime_math import create_time_suffix
  18. from .datetime_math import create_numeric_suffix
  19. from .core import get_current_mapset, get_tgis_message_interface, init_dbif
  20. from .spatio_temporal_relationships import SpatioTemporalTopologyBuilder, \
  21. create_temporal_relation_sql_where_statement
  22. ###############################################################################
  23. def collect_map_names(sp, dbif, start, end, sampling):
  24. """Gather all maps from dataset using a specific sample method
  25. :param sp: The space time raster dataset to select aps from
  26. :param dbif: The temporal database interface to use
  27. :param start: The start time of the sample interval, may be relative or
  28. absolute
  29. :param end: The end time of the sample interval, may be relative or
  30. absolute
  31. :param sampling: The sampling methods to use
  32. """
  33. use_start = False
  34. use_during = False
  35. use_overlap = False
  36. use_contain = False
  37. use_equal = False
  38. use_follows = False
  39. use_precedes = False
  40. # Initialize the methods
  41. if sampling:
  42. for name in sampling.split(","):
  43. if name == "start":
  44. use_start = True
  45. if name == "during":
  46. use_during = True
  47. if name == "overlap":
  48. use_overlap = True
  49. if name == "contain":
  50. use_contain = True
  51. if name == "equal":
  52. use_equal = True
  53. if name == "follows":
  54. use_follows = True
  55. if name == "precedes":
  56. use_precedes = True
  57. else:
  58. use_start = True
  59. if sp.get_map_time() != "interval":
  60. use_start = True
  61. use_during = False
  62. use_overlap = False
  63. use_contain = False
  64. use_equal = False
  65. use_follows = False
  66. use_precedes = False
  67. where = create_temporal_relation_sql_where_statement(start, end,
  68. use_start,
  69. use_during,
  70. use_overlap,
  71. use_contain,
  72. use_equal,
  73. use_follows,
  74. use_precedes)
  75. rows = sp.get_registered_maps("id", where, "start_time", dbif)
  76. if not rows:
  77. return None
  78. names = []
  79. for row in rows:
  80. names.append(row["id"])
  81. return names
  82. ###############################################################################
  83. def aggregate_raster_maps(inputs, base, start, end, count, method,
  84. register_null, dbif, offset=0):
  85. """Aggregate a list of raster input maps with r.series
  86. :param inputs: The names of the raster maps to be aggregated
  87. :param base: The basename of the new created raster maps
  88. :param start: The start time of the sample interval, may be relative or
  89. absolute
  90. :param end: The end time of the sample interval, may be relative or
  91. absolute
  92. :param count: The number to be attached to the basename of the new
  93. created raster map
  94. :param method: The aggreation method to be used by r.series
  95. :param register_null: If true null maps will be registered in the space
  96. time raster dataset, if false not
  97. :param dbif: The temporal database interface to use
  98. :param offset: Offset to be added to the map counter to create the map ids
  99. """
  100. msgr = get_tgis_message_interface()
  101. msgr.verbose(_("Aggregating %s raster maps") % (len(inputs)))
  102. output = "%s_%i" % (base, int(offset) + count)
  103. mapset = get_current_mapset()
  104. map_id = output + "@" + mapset
  105. new_map = RasterDataset(map_id)
  106. # Check if new map is in the temporal database
  107. if new_map.is_in_db(dbif):
  108. if gscript.overwrite() is True:
  109. # Remove the existing temporal database entry
  110. new_map.delete(dbif)
  111. new_map = RasterDataset(map_id)
  112. else:
  113. msgr.error(_("Raster map <%(name)s> is already in temporal "
  114. "database, use overwrite flag to overwrite" %
  115. ({"name": new_map.get_name()})))
  116. return
  117. msgr.verbose(_("Computing aggregation of maps between %(st)s - %(end)s" % {
  118. 'st': str(start), 'end': str(end)}))
  119. # Create the r.series input file
  120. filename = gscript.tempfile(True)
  121. file = open(filename, 'w')
  122. for name in inputs:
  123. string = "%s\n" % (name)
  124. file.write(string)
  125. file.close()
  126. # Run r.series
  127. try:
  128. if len(inputs) > 1000:
  129. gscript.run_command("r.series", flags="z", file=filename,
  130. output=output, overwrite=gscript.overwrite(),
  131. method=method)
  132. else:
  133. gscript.run_command("r.series", file=filename,
  134. output=output, overwrite=gscript.overwrite(),
  135. method=method)
  136. except CalledModuleError:
  137. dbif.close()
  138. msgr.fatal(_("Error occurred in r.series computation"))
  139. # Read the raster map data
  140. new_map.load()
  141. # In case of a null map continue, do not register null maps
  142. if new_map.metadata.get_min() is None and \
  143. new_map.metadata.get_max() is None:
  144. if not register_null:
  145. gscript.run_command("g.remove", flags='f', type='raster',
  146. name=output)
  147. return None
  148. return new_map
  149. ##############################################################################
  150. def aggregate_by_topology(granularity_list, granularity, map_list, topo_list,
  151. basename, time_suffix, offset=0, method="average",
  152. nprocs=1, spatial=None, dbif=None, overwrite=False,
  153. file_limit=1000):
  154. """Aggregate a list of raster input maps with r.series
  155. :param granularity_list: A list of AbstractMapDataset objects.
  156. The temporal extents of the objects are used
  157. to build the spatio-temporal topology with the
  158. map list objects
  159. :param granularity: The granularity of the granularity list
  160. :param map_list: A list of RasterDataset objects that contain the raster
  161. maps that should be aggregated
  162. :param topo_list: A list of strings of topological relations that are
  163. used to select the raster maps for aggregation
  164. :param basename: The basename of the new generated raster maps
  165. :param time_suffix: Use the granularity truncated start time of the
  166. actual granule to create the suffix for the basename
  167. :param offset: Use a numerical offset for suffix generation
  168. (overwritten by time_suffix)
  169. :param method: The aggregation method of r.series (average,min,max, ...)
  170. :param nprocs: The number of processes used for parallel computation
  171. :param spatial: This indicates if the spatial topology is created as
  172. well: spatial can be None (no spatial topology), "2D"
  173. using west, east, south, north or "3D" using west,
  174. east, south, north, bottom, top
  175. :param dbif: The database interface to be used
  176. :param overwrite: Overwrite existing raster maps
  177. :param file_limit: The maximum number of raster map layers that
  178. should be opened at once by r.series
  179. :return: A list of RasterDataset objects that contain the new map names
  180. and the temporal extent for map registration
  181. """
  182. import grass.pygrass.modules as pymod
  183. import copy
  184. msgr = get_tgis_message_interface()
  185. dbif, connected = init_dbif(dbif)
  186. topo_builder = SpatioTemporalTopologyBuilder()
  187. topo_builder.build(mapsA=granularity_list, mapsB=map_list, spatial=spatial)
  188. # The module queue for parallel execution
  189. process_queue = pymod.ParallelModuleQueue(int(nprocs))
  190. # Dummy process object that will be deep copied
  191. # and be put into the process queue
  192. r_series = pymod.Module("r.series", output="spam", method=[method],
  193. overwrite=overwrite, quiet=True, run_=False,
  194. finish_=False)
  195. g_copy = pymod.Module("g.copy", raster=['spam', 'spamspam'],
  196. quiet=True, run_=False, finish_=False)
  197. output_list = []
  198. count = 0
  199. for granule in granularity_list:
  200. msgr.percent(count, len(granularity_list), 1)
  201. count += 1
  202. aggregation_list = []
  203. if "equal" in topo_list and granule.equal:
  204. for map_layer in granule.equal:
  205. aggregation_list.append(map_layer.get_name())
  206. if "contains" in topo_list and granule.contains:
  207. for map_layer in granule.contains:
  208. aggregation_list.append(map_layer.get_name())
  209. if "during" in topo_list and granule.during:
  210. for map_layer in granule.during:
  211. aggregation_list.append(map_layer.get_name())
  212. if "starts" in topo_list and granule.starts:
  213. for map_layer in granule.starts:
  214. aggregation_list.append(map_layer.get_name())
  215. if "started" in topo_list and granule.started:
  216. for map_layer in granule.started:
  217. aggregation_list.append(map_layer.get_name())
  218. if "finishes" in topo_list and granule.finishes:
  219. for map_layer in granule.finishes:
  220. aggregation_list.append(map_layer.get_name())
  221. if "finished" in topo_list and granule.finished:
  222. for map_layer in granule.finished:
  223. aggregation_list.append(map_layer.get_name())
  224. if "overlaps" in topo_list and granule.overlaps:
  225. for map_layer in granule.overlaps:
  226. aggregation_list.append(map_layer.get_name())
  227. if "overlapped" in topo_list and granule.overlapped:
  228. for map_layer in granule.overlapped:
  229. aggregation_list.append(map_layer.get_name())
  230. if aggregation_list:
  231. msgr.verbose(_("Aggregating %(len)i raster maps from %(start)s to"
  232. " %(end)s") % ({"len": len(aggregation_list),
  233. "start": str(granule.temporal_extent.get_start_time()),
  234. "end": str(granule.temporal_extent.get_end_time())}))
  235. if granule.is_time_absolute() is True and time_suffix == 'gran':
  236. suffix = create_suffix_from_datetime(granule.temporal_extent.get_start_time(),
  237. granularity)
  238. output_name = "{ba}_{su}".format(ba=basename, su=suffix)
  239. elif granule.is_time_absolute() is True and time_suffix == 'time':
  240. suffix = create_time_suffix(granule)
  241. output_name = "{ba}_{su}".format(ba=basename, su=suffix)
  242. else:
  243. output_name = create_numeric_suffix(basename, count + int(offset),
  244. time_suffix)
  245. map_layer = RasterDataset("%s@%s" % (output_name,
  246. get_current_mapset()))
  247. map_layer.set_temporal_extent(granule.get_temporal_extent())
  248. if map_layer.map_exists() is True and overwrite is False:
  249. msgr.fatal(_("Unable to perform aggregation. Output raster "
  250. "map <%(name)s> exists and overwrite flag was "
  251. "not set" % ({"name": output_name})))
  252. output_list.append(map_layer)
  253. if len(aggregation_list) > 1:
  254. # Create the r.series input file
  255. filename = gscript.tempfile(True)
  256. file = open(filename, 'w')
  257. for name in aggregation_list:
  258. string = "%s\n" % (name)
  259. file.write(string)
  260. file.close()
  261. mod = copy.deepcopy(r_series)
  262. mod(file=filename, output=output_name)
  263. if len(aggregation_list) > int(file_limit):
  264. msgr.warning(_("The limit of open files (%i) was "
  265. "reached (%i). The module r.series will "
  266. "be run with flag z, to avoid open "
  267. "files limit exceeding." % (int(file_limit),
  268. len(aggregation_list))))
  269. mod(flags="z")
  270. process_queue.put(mod)
  271. else:
  272. mod = copy.deepcopy(g_copy)
  273. mod(raster=[aggregation_list[0], output_name])
  274. process_queue.put(mod)
  275. process_queue.wait()
  276. if connected:
  277. dbif.close()
  278. msgr.percent(1, 1, 1)
  279. return output_list