t.rast.series.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.rast.series
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Perform different aggregation algorithms from r.series on all or a
  9. # selected subset of raster maps in a space time raster dataset
  10. # COPYRIGHT: (C) 2011-2017 by the GRASS Development Team
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. #############################################################################
  23. #%module
  24. #% description: Performs different aggregation algorithms from r.series on all or a subset of raster maps in a space time raster dataset.
  25. #% keyword: temporal
  26. #% keyword: aggregation
  27. #% keyword: series
  28. #% keyword: raster
  29. #% keyword: time
  30. #%end
  31. #%option G_OPT_STRDS_INPUT
  32. #%end
  33. #%option
  34. #% key: method
  35. #% type: string
  36. #% description: Aggregate operation to be performed on the raster maps
  37. #% required: yes
  38. #% multiple: no
  39. #% 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
  40. #% answer: average
  41. #%end
  42. #%option
  43. #% key: quantile
  44. #% type: double
  45. #% description: Quantile to calculate for method=quantile
  46. #% required: no
  47. #% multiple: no
  48. #% options: 0.0-1.0
  49. #%end
  50. #%option
  51. #% key: order
  52. #% type: string
  53. #% description: Sort the maps by category
  54. #% required: no
  55. #% multiple: yes
  56. #% options: id, name, creator, mapset, creation_time, modification_time, start_time, end_time, north, south, west, east, min, max
  57. #% answer: start_time
  58. #%end
  59. #%option G_OPT_T_WHERE
  60. #%end
  61. #%option G_OPT_R_OUTPUT
  62. #%end
  63. #%flag
  64. #% key: t
  65. #% description: Do not assign the space time raster dataset start and end time to the output map
  66. #%end
  67. #%flag
  68. #% key: n
  69. #% description: Propagate NULLs
  70. #%end
  71. import grass.script as grass
  72. from grass.exceptions import CalledModuleError
  73. ############################################################################
  74. def main():
  75. # lazy imports
  76. import grass.temporal as tgis
  77. # Get the options
  78. input = options["input"]
  79. output = options["output"]
  80. method = options["method"]
  81. quantile = options["quantile"]
  82. order = options["order"]
  83. where = options["where"]
  84. add_time = flags["t"]
  85. nulls = flags["n"]
  86. # Make sure the temporal database exists
  87. tgis.init()
  88. sp = tgis.open_old_stds(input, "strds")
  89. rows = sp.get_registered_maps("id", where, order, None)
  90. if rows:
  91. # Create the r.series input file
  92. filename = grass.tempfile(True)
  93. file = open(filename, 'w')
  94. for row in rows:
  95. string = "%s\n" % (row["id"])
  96. file.write(string)
  97. file.close()
  98. flag = ""
  99. if len(rows) > 1000:
  100. grass.warning(_("Processing over 1000 maps: activating -z flag of r.series which slows down processing"))
  101. flag += "z"
  102. if nulls:
  103. flag += "n"
  104. try:
  105. grass.run_command("r.series", flags=flag, file=filename,
  106. output=output, overwrite=grass.overwrite(),
  107. method=method, quantile=quantile)
  108. except CalledModuleError:
  109. grass.fatal(_("%s failed. Check above error messages.") % 'r.series')
  110. if not add_time:
  111. # Create the time range for the output map
  112. if output.find("@") >= 0:
  113. id = output
  114. else:
  115. mapset = grass.encode(grass.gisenv()["MAPSET"])
  116. id = output + "@" + mapset
  117. map = sp.get_new_map_instance(id)
  118. map.load()
  119. # We need to set the temporal extent from the subset of selected maps
  120. maps = sp.get_registered_maps_as_objects(where=where, order=order, dbif=None)
  121. first_map = maps[0]
  122. last_map = maps[-1]
  123. start_a, end_a = first_map.get_temporal_extent_as_tuple()
  124. start_b, end_b = last_map.get_temporal_extent_as_tuple()
  125. if end_b is None:
  126. end_b = start_b
  127. if first_map.is_time_absolute():
  128. extent = tgis.AbsoluteTemporalExtent(start_time=start_a, end_time=end_b)
  129. else:
  130. extent = tgis.RelativeTemporalExtent(start_time=start_a, end_time=end_b,
  131. unit=first_map.get_relative_time_unit())
  132. map.set_temporal_extent(extent=extent)
  133. # Register the map in the temporal database
  134. if map.is_in_db():
  135. map.update_all()
  136. else:
  137. map.insert()
  138. if __name__ == "__main__":
  139. options, flags = grass.parser()
  140. main()