r.colors.stddev.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #% keyword: raster
  19. #% keyword: 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', flags = 'f', type = 'raster',
  40. name = tmpmap, quiet = True)
  41. def main():
  42. global tmpmap
  43. tmpmap = None
  44. map = options['map']
  45. zero = flags['z']
  46. bands = flags['b']
  47. if not zero:
  48. s = grass.read_command('r.univar', flags = 'g', map = map)
  49. kv = grass.parse_key_val(s)
  50. global mean, stddev
  51. mean = float(kv['mean'])
  52. stddev = float(kv['stddev'])
  53. if not bands:
  54. # smooth free floating blue/white/red
  55. rules = '\n'.join([
  56. "0% blue",
  57. "%f blue" % z(-2),
  58. "%f white" % mean,
  59. "%f red" % z(+2),
  60. "100% red"])
  61. else:
  62. # banded free floating black/red/yellow/green/yellow/red/black
  63. # reclass with labels only works for category (integer) based maps
  64. #r.reclass input="$GIS_OPT_MAP" output="${GIS_OPT_MAP}.stdevs" << EOF
  65. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  66. rules = '\n'.join([
  67. "0% black",
  68. "%f black" % z(-3),
  69. "%f red" % z(-3),
  70. "%f red" % z(-2),
  71. "%f yellow" % z(-2),
  72. "%f yellow" % z(-1),
  73. "%f green" % z(-1),
  74. "%f green" % z(+1),
  75. "%f yellow" % z(+1),
  76. "%f yellow" % z(+2),
  77. "%f red" % z(+2),
  78. "%f red" % z(+3),
  79. "%f black" % z(+3),
  80. "100% black"])
  81. else:
  82. tmpmap = "r_col_stdev_abs_%d" % os.getpid()
  83. grass.mapcalc("$tmp = abs($map)", tmp = tmpmap, map = map)
  84. # data centered on 0 (e.g. map of deviations)
  85. info = grass.raster_info(tmpmap)
  86. maxv = info['max']
  87. # current r.univar truncates percentage to the base integer
  88. s = grass.read_command('r.univar', flags = 'eg', map = map, percentile = [95.45,68.2689,99.7300])
  89. kv = grass.parse_key_val(s)
  90. stddev1 = float(kv['percentile_68_2689'])
  91. stddev2 = float(kv['percentile_95_45'])
  92. stddev3 = float(kv['percentile_99_73'])
  93. if not bands:
  94. # zero centered smooth blue/white/red
  95. rules = '\n'.join([
  96. "%f blue" % -maxv,
  97. "%f blue" % -stddev2,
  98. "0 white",
  99. "%f red" % stddev2,
  100. "%f red" % maxv])
  101. else:
  102. # zero centered banded black/red/yellow/green/yellow/red/black
  103. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  104. rules = '\n'.join([
  105. "%f black" % -maxv,
  106. "%f black" % -stddev3,
  107. "%f red" % -stddev3,
  108. "%f red" % -stddev2,
  109. "%f yellow" % -stddev2,
  110. "%f yellow" % -stddev1,
  111. "%f green" % -stddev1,
  112. "%f green" % stddev1,
  113. "%f yellow" % stddev1,
  114. "%f yellow" % stddev2,
  115. "%f red" % stddev2,
  116. "%f red" % stddev3,
  117. "%f black" % stddev3,
  118. "%f black" % maxv,
  119. ])
  120. grass.write_command('r.colors', map = map, rules = '-', stdin = rules)
  121. if __name__ == "__main__":
  122. options, flags = grass.parser()
  123. atexit.register(cleanup)
  124. main()