t.rast.aggregate.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #% keywords: temporal
  19. #% keywords: aggregation
  20. #% keywords: raster
  21. #%end
  22. #%option G_OPT_STRDS_INPUT
  23. #%end
  24. #%option G_OPT_STRDS_OUTPUT
  25. #%end
  26. #%option
  27. #% key: basename
  28. #% type: string
  29. #% label: Basename of the new generated output maps
  30. #% description: Either a numerical suffix or the start time (s-flag) separated by an underscore will be attached to create a unique identifier
  31. #% required: yes
  32. #% multiple: no
  33. #% gisprompt:
  34. #%end
  35. #%option
  36. #% key: granularity
  37. #% type: string
  38. #% 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
  39. #% required: yes
  40. #% multiple: no
  41. #%end
  42. #%option
  43. #% key: method
  44. #% type: string
  45. #% description: Aggregate operation to be performed on the raster maps
  46. #% required: yes
  47. #% multiple: no
  48. #% 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
  49. #% answer: average
  50. #%end
  51. #%option
  52. #% key: offset
  53. #% type: integer
  54. #% description: Offset that is used to create the output map ids, output map id is generated as: basename_ (count + offset)
  55. #% required: no
  56. #% multiple: no
  57. #% answer: 0
  58. #%end
  59. #%option
  60. #% key: nprocs
  61. #% type: integer
  62. #% description: Number of r.mapcalc processes to run in parallel
  63. #% required: no
  64. #% multiple: no
  65. #% answer: 1
  66. #%end
  67. #%option G_OPT_T_SAMPLE
  68. #% options: equal,overlaps,overlapped,starts,started,finishes,finished,during,contains
  69. #% answer: contains
  70. #%end
  71. #%option G_OPT_T_WHERE
  72. #%end
  73. #%flag
  74. #% key: n
  75. #% description: Register Null maps
  76. #%end
  77. #%flag
  78. #% key: s
  79. #% description: Use start time - truncated accoring to granularity - as suffix. This flag overrides the offset option.
  80. #%end
  81. import grass.script as gcore
  82. import grass.temporal as tgis
  83. ############################################################################
  84. def main():
  85. # Get the options
  86. input = options["input"]
  87. output = options["output"]
  88. where = options["where"]
  89. gran = options["granularity"]
  90. base = options["basename"]
  91. register_null = flags["n"]
  92. method = options["method"]
  93. sampling = options["sampling"]
  94. offset = options["offset"]
  95. nprocs = options["nprocs"]
  96. time_suffix = flags["s"]
  97. topo_list = sampling.split(",")
  98. tgis.init()
  99. dbif = tgis.SQLDatabaseInterfaceConnection()
  100. dbif.connect()
  101. sp = tgis.open_old_stds(input, "strds", dbif)
  102. map_list = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif)
  103. if not map_list:
  104. dbif.close()
  105. gcore.fatal(_("Space time raster dataset <%s> is empty") % input)
  106. # We will create the strds later, but need to check here
  107. tgis.check_new_stds(output, "strds", dbif, gcore.overwrite())
  108. start_time = map_list[0].temporal_extent.get_start_time()
  109. if sp.is_time_absolute():
  110. start_time = tgis.adjust_datetime_to_granularity(start_time, gran)
  111. # We use the end time first
  112. end_time = map_list[-1].temporal_extent.get_end_time()
  113. has_end_time = True
  114. # In case no end time is available, then we use the start time of the last map layer
  115. if end_time is None:
  116. end_time = map_list[- 1].temporal_extent.get_start_time()
  117. has_end_time = False
  118. granularity_list = []
  119. # Build the granularity list
  120. while True:
  121. if has_end_time is True:
  122. if start_time >= end_time:
  123. break
  124. else:
  125. if start_time > end_time:
  126. break
  127. granule = tgis.RasterDataset(None)
  128. start = start_time
  129. if sp.is_time_absolute():
  130. end = tgis.increment_datetime_by_string(start_time, gran)
  131. granule.set_absolute_time(start, end)
  132. else:
  133. end = start_time + int(gran)
  134. granule.set_relative_time(start, end, sp.get_relative_time_unit())
  135. start_time = end
  136. granularity_list.append(granule)
  137. output_list = tgis.aggregate_by_topology(granularity_list=granularity_list, granularity=gran,
  138. map_list=map_list,
  139. topo_list=topo_list, basename=base, time_suffix=time_suffix,
  140. offset=offset, method=method, nprocs=nprocs, spatial=None,
  141. overwrite=gcore.overwrite())
  142. if output_list:
  143. temporal_type, semantic_type, title, description = sp.get_initial_values()
  144. output_strds = tgis.open_new_stds(output, "strds", temporal_type,
  145. title, description, semantic_type,
  146. dbif, gcore.overwrite())
  147. if register_null:
  148. register_null=False
  149. else:
  150. register_null=True
  151. tgis.register_map_object_list("rast", output_list, output_strds, register_null,
  152. sp.get_relative_time_unit(), dbif)
  153. # Update the raster metadata table entries with aggregation type
  154. output_strds.set_aggregation_type(method)
  155. output_strds.metadata.update(dbif)
  156. dbif.close()
  157. if __name__ == "__main__":
  158. options, flags = gcore.parser()
  159. main()