r.colors.stddev.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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: Sets color rules based on stddev from a raster map's mean value.
  18. #% keywords: raster
  19. #% keywords: color table
  20. #%end
  21. #% option G_OPT_R_MAP
  22. #%end
  23. #%flag
  24. #% key: b
  25. #% description: Color using standard deviation bands
  26. #%end
  27. #%flag
  28. #% key: z
  29. #% description: Force center at zero
  30. #%end
  31. import sys
  32. import os
  33. import atexit
  34. import grass.script as grass
  35. def z(n):
  36. return mean + n * stddev
  37. def cleanup():
  38. if tmpmap:
  39. grass.run_command('g.remove', rast = tmpmap, quiet = True)
  40. def main():
  41. global tmpmap
  42. tmpmap = None
  43. map = options['map']
  44. zero = flags['z']
  45. bands = flags['b']
  46. if not zero:
  47. s = grass.read_command('r.univar', flags = 'g', map = map)
  48. kv = grass.parse_key_val(s)
  49. global mean, stddev
  50. mean = float(kv['mean'])
  51. stddev = float(kv['stddev'])
  52. if not bands:
  53. # smooth free floating blue/white/red
  54. rules = '\n'.join([
  55. "0% blue",
  56. "%f blue" % z(-2),
  57. "%f white" % mean,
  58. "%f red" % z(+2),
  59. "100% red"])
  60. else:
  61. # banded free floating black/red/yellow/green/yellow/red/black
  62. # reclass with labels only works for category (integer) based maps
  63. #r.reclass input="$GIS_OPT_MAP" output="${GIS_OPT_MAP}.stdevs" << EOF
  64. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  65. rules = '\n'.join([
  66. "0% black",
  67. "%f black" % z(-3),
  68. "%f red" % z(-3),
  69. "%f red" % z(-2),
  70. "%f yellow" % z(-2),
  71. "%f yellow" % z(-1),
  72. "%f green" % z(-1),
  73. "%f green" % z(+1),
  74. "%f yellow" % z(+1),
  75. "%f yellow" % z(+2),
  76. "%f red" % z(+2),
  77. "%f red" % z(+3),
  78. "%f black" % z(+3),
  79. "100% black"])
  80. else:
  81. tmpmap = "r_col_stdev_abs_%d" % os.getpid()
  82. grass.mapcalc("$tmp = abs($map)", tmp = tmpmap, map = map)
  83. # data centered on 0 (e.g. map of deviations)
  84. info = grass.raster_info(tmpmap)
  85. maxv = info['max']
  86. # current r.univar truncates percentage to the base integer
  87. s = grass.read_command('r.univar', flags = 'eg', map = map, percentile = [95.45,68.2689,99.7300])
  88. kv = grass.parse_key_val(s)
  89. stddev1 = float(kv['percentile_68'])
  90. stddev2 = float(kv['percentile_95'])
  91. stddev3 = float(kv['percentile_99'])
  92. if not bands:
  93. # zero centered smooth blue/white/red
  94. rules = '\n'.join([
  95. "%f blue" % -maxv,
  96. "%f blue" % -stddev2,
  97. "0 white",
  98. "%f red" % stddev2,
  99. "%f red" % maxv])
  100. else:
  101. # zero centered banded black/red/yellow/green/yellow/red/black
  102. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  103. rules = '\n'.join([
  104. "%f black" % -maxv,
  105. "%f black" % -stddev3,
  106. "%f red" % -stddev3,
  107. "%f red" % -stddev2,
  108. "%f yellow" % -stddev2,
  109. "%f yellow" % -stddev1,
  110. "%f green" % -stddev1,
  111. "%f green" % stddev1,
  112. "%f yellow" % stddev1,
  113. "%f yellow" % stddev2,
  114. "%f red" % stddev2,
  115. "%f red" % stddev3,
  116. "%f black" % stddev3,
  117. "%f black" % maxv,
  118. ])
  119. grass.write_command('r.colors', map = map, rules = '-', stdin = rules)
  120. if __name__ == "__main__":
  121. options, flags = grass.parser()
  122. atexit.register(cleanup)
  123. main()