r.colors.stddev.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.colors.stddev
  5. # AUTHOR: M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
  6. # New Zealand
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: Set color rules based on stddev from a map's mean value.
  9. #
  10. # COPYRIGHT: (c) 2007,2009-2010 Hamish Bowman, and the GRASS Development Team
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. #%Module
  17. #% description: Set color rules based on stddev from a raster map's mean value.
  18. #% keywords: raster
  19. #% keywords: color table
  20. #%End
  21. #% option
  22. #% key: map
  23. #% type: string
  24. #% gisprompt: old,cell,raster
  25. #% key_desc: name
  26. #% description: Name of raster map
  27. #% required: yes
  28. #%end
  29. #%flag
  30. #% key: b
  31. #% description: Color using standard deviation bands
  32. #%end
  33. #%flag
  34. #% key: z
  35. #% description: Force center at zero
  36. #%end
  37. import sys
  38. import os
  39. import atexit
  40. import grass.script as grass
  41. def z(n):
  42. return mean + n * stddev
  43. def cleanup():
  44. if tmpmap:
  45. grass.run_command('g.remove', rast = tmpmap, quiet = True)
  46. def main():
  47. global tmpmap
  48. tmpmap = None
  49. map = options['map']
  50. zero = flags['z']
  51. bands = flags['b']
  52. if not zero:
  53. s = grass.read_command('r.univar', flags = 'g', map = map)
  54. kv = grass.parse_key_val(s)
  55. global mean, stddev
  56. mean = float(kv['mean'])
  57. stddev = float(kv['stddev'])
  58. if not bands:
  59. # smooth free floating blue/white/red
  60. rules = '\n'.join([
  61. "0% blue",
  62. "%f blue" % z(-2),
  63. "%f white" % mean,
  64. "%f red" % z(+2),
  65. "100% red"])
  66. else:
  67. # banded free floating black/red/yellow/green/yellow/red/black
  68. # reclass with labels only works for category (integer) based maps
  69. #r.reclass map="$GIS_OPT_MAP" output="${GIS_OPT_MAP}.stdevs" << EOF
  70. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  71. rules = '\n'.join([
  72. "0% black",
  73. "%f black" % z(-3),
  74. "%f red" % z(-3),
  75. "%f red" % z(-2),
  76. "%f yellow" % z(-2),
  77. "%f yellow" % z(-1),
  78. "%f green" % z(-1),
  79. "%f green" % z(+1),
  80. "%f yellow" % z(+1),
  81. "%f yellow" % z(+2),
  82. "%f red" % z(+2),
  83. "%f red" % z(+3),
  84. "%f black" % z(+3),
  85. "100% black"])
  86. else:
  87. tmpmap = "r_col_stdev_abs_%d" % os.getpid()
  88. grass.mapcalc("$tmp = abs($map)", tmp = tmpmap, map = map)
  89. # data centered on 0 (e.g. map of deviations)
  90. info = grass.raster_info(tmpmap)
  91. maxv = info['max']
  92. # current r.univar truncates percentage to the base integer
  93. s = grass.read_command('r.univar', flags = 'eg', map = map, percentile = [95.45,68.2689,99.7300])
  94. kv = grass.parse_key_val(s)
  95. stddev1 = float(kv['percentile_68'])
  96. stddev2 = float(kv['percentile_95'])
  97. stddev3 = float(kv['percentile_99'])
  98. if not bands:
  99. # zero centered smooth blue/white/red
  100. rules = '\n'.join([
  101. "%f blue" % -maxv,
  102. "%f blue" % -stddev2,
  103. "0 white",
  104. "%f red" % stddev2,
  105. "%f red" % maxv])
  106. else:
  107. # zero centered banded black/red/yellow/green/yellow/red/black
  108. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  109. rules = '\n'.join([
  110. "%f black" % -maxv,
  111. "%f black" % -stddev3,
  112. "%f red" % -stddev3,
  113. "%f red" % -stddev2,
  114. "%f yellow" % -stddev2,
  115. "%f yellow" % -stddev1,
  116. "%f green" % -stddev1,
  117. "%f green" % stddev1,
  118. "%f yellow" % stddev1,
  119. "%f yellow" % stddev2,
  120. "%f red" % stddev2,
  121. "%f red" % stddev3,
  122. "%f black" % stddev3,
  123. "%f black" % maxv,
  124. ])
  125. grass.write_command('r.colors', map = map, rules = '-', stdin = rules)
  126. if __name__ == "__main__":
  127. options, flags = grass.parser()
  128. atexit.register(cleanup)
  129. main()