t.rast.aggregate.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.rast.aggregate
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Temporally aggregates the maps of a space time raster dataset by a user defined granularity.
  9. # COPYRIGHT: (C) 2011-2014 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (version 2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. #%module
  17. #% description: Aggregates temporally the maps of a space time raster dataset by a user defined granularity.
  18. #% keyword: temporal
  19. #% keyword: aggregation
  20. #% keyword: raster
  21. #% keyword: time
  22. #%end
  23. #%option G_OPT_STRDS_INPUT
  24. #%end
  25. #%option G_OPT_STRDS_OUTPUT
  26. #%end
  27. #%option
  28. #% key: basename
  29. #% type: string
  30. #% label: Basename of the new generated output maps
  31. #% description: Either a numerical suffix or the start time (s-flag) separated by an underscore will be attached to create a unique identifier
  32. #% required: yes
  33. #% multiple: no
  34. #% gisprompt:
  35. #%end
  36. #%option
  37. #% key: suffix
  38. #% type: string
  39. #% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %05)
  40. #% answer: gran
  41. #% required: no
  42. #% multiple: no
  43. #%end
  44. #%option
  45. #% key: granularity
  46. #% type: string
  47. #% description: Aggregation granularity, format absolute time "x years, x months, x weeks, x days, x hours, x minutes, x seconds" or an integer value for relative time
  48. #% required: yes
  49. #% multiple: no
  50. #%end
  51. #%option
  52. #% key: method
  53. #% type: string
  54. #% description: Aggregate operation to be performed on the raster maps
  55. #% required: yes
  56. #% multiple: no
  57. #% options: average,count,median,mode,minimum,min_raster,maximum,max_raster,stddev,range,sum,variance,diversity,slope,offset,detcoeff,quart1,quart3,perc90,quantile,skewness,kurtosis
  58. #% answer: average
  59. #%end
  60. #%option
  61. #% key: offset
  62. #% type: integer
  63. #% description: Offset that is used to create the output map ids, output map id is generated as: basename_ (count + offset)
  64. #% required: no
  65. #% multiple: no
  66. #% answer: 0
  67. #%end
  68. #%option
  69. #% key: nprocs
  70. #% type: integer
  71. #% description: Number of r.series processes to run in parallel
  72. #% required: no
  73. #% multiple: no
  74. #% answer: 1
  75. #%end
  76. #%option
  77. #% key: file_limit
  78. #% type: integer
  79. #% description: The maximum number of open files allowed for each r.series process
  80. #% required: no
  81. #% multiple: no
  82. #% answer: 1000
  83. #%end
  84. #%option G_OPT_T_SAMPLE
  85. #% options: equal,overlaps,overlapped,starts,started,finishes,finished,during,contains
  86. #% answer: contains
  87. #%end
  88. #%option G_OPT_T_WHERE
  89. #%end
  90. #%flag
  91. #% key: n
  92. #% description: Register Null maps
  93. #%end
  94. import grass.script as gcore
  95. import grass.temporal as tgis
  96. ############################################################################
  97. def main():
  98. # Get the options
  99. input = options["input"]
  100. output = options["output"]
  101. where = options["where"]
  102. gran = options["granularity"]
  103. base = options["basename"]
  104. register_null = flags["n"]
  105. method = options["method"]
  106. sampling = options["sampling"]
  107. offset = options["offset"]
  108. nprocs = options["nprocs"]
  109. file_limit = options["file_limit"]
  110. time_suffix = options["suffix"]
  111. topo_list = sampling.split(",")
  112. tgis.init()
  113. dbif = tgis.SQLDatabaseInterfaceConnection()
  114. dbif.connect()
  115. sp = tgis.open_old_stds(input, "strds", dbif)
  116. map_list = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif)
  117. if not map_list:
  118. dbif.close()
  119. gcore.fatal(_("Space time raster dataset <%s> is empty") % input)
  120. # We will create the strds later, but need to check here
  121. tgis.check_new_stds(output, "strds", dbif, gcore.overwrite())
  122. start_time = map_list[0].temporal_extent.get_start_time()
  123. if sp.is_time_absolute():
  124. start_time = tgis.adjust_datetime_to_granularity(start_time, gran)
  125. # We use the end time first
  126. end_time = map_list[-1].temporal_extent.get_end_time()
  127. has_end_time = True
  128. # In case no end time is available, then we use the start time of the last map layer
  129. if end_time is None:
  130. end_time = map_list[- 1].temporal_extent.get_start_time()
  131. has_end_time = False
  132. granularity_list = []
  133. # Build the granularity list
  134. while True:
  135. if has_end_time is True:
  136. if start_time >= end_time:
  137. break
  138. else:
  139. if start_time > end_time:
  140. break
  141. granule = tgis.RasterDataset(None)
  142. start = start_time
  143. if sp.is_time_absolute():
  144. end = tgis.increment_datetime_by_string(start_time, gran)
  145. granule.set_absolute_time(start, end)
  146. else:
  147. end = start_time + int(gran)
  148. granule.set_relative_time(start, end, sp.get_relative_time_unit())
  149. start_time = end
  150. granularity_list.append(granule)
  151. output_list = tgis.aggregate_by_topology(granularity_list=granularity_list, granularity=gran,
  152. map_list=map_list,
  153. topo_list=topo_list, basename=base, time_suffix=time_suffix,
  154. offset=offset, method=method, nprocs=nprocs, spatial=None,
  155. overwrite=gcore.overwrite(), file_limit=file_limit)
  156. if output_list:
  157. temporal_type, semantic_type, title, description = sp.get_initial_values()
  158. output_strds = tgis.open_new_stds(output, "strds", temporal_type,
  159. title, description, semantic_type,
  160. dbif, gcore.overwrite())
  161. if register_null:
  162. register_null=False
  163. else:
  164. register_null=True
  165. tgis.register_map_object_list("rast", output_list, output_strds, register_null,
  166. sp.get_relative_time_unit(), dbif)
  167. # Update the raster metadata table entries with aggregation type
  168. output_strds.set_aggregation_type(method)
  169. output_strds.metadata.update(dbif)
  170. dbif.close()
  171. if __name__ == "__main__":
  172. options, flags = gcore.parser()
  173. main()