r.colors.stddev.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 os
  32. import atexit
  33. import grass.script as gscript
  34. def z(n):
  35. return mean + n * stddev
  36. def cleanup():
  37. if tmpmap:
  38. gscript.run_command('g.remove', flags='f', type='raster',
  39. name=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 = gscript.read_command('r.univar', flags='g', map=map)
  48. kv = gscript.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(["0% blue",
  55. "%f blue" % z(-2),
  56. "%f white" % mean,
  57. "%f red" % z(+2),
  58. "100% red"])
  59. else:
  60. # banded free floating black/red/yellow/green/yellow/red/black
  61. # reclass with labels only works for category (integer) based maps
  62. # r.reclass input="$GIS_OPT_MAP" output="${GIS_OPT_MAP}.stdevs" <<
  63. # EOF
  64. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  65. rules = '\n'.join(["0% black",
  66. "%f black" % z(-3),
  67. "%f red" % z(-3),
  68. "%f red" % z(-2),
  69. "%f yellow" % z(-2),
  70. "%f yellow" % z(-1),
  71. "%f green" % z(-1),
  72. "%f green" % z(+1),
  73. "%f yellow" % z(+1),
  74. "%f yellow" % z(+2),
  75. "%f red" % z(+2),
  76. "%f red" % z(+3),
  77. "%f black" % z(+3),
  78. "100% black"])
  79. else:
  80. tmpmap = "r_col_stdev_abs_%d" % os.getpid()
  81. gscript.mapcalc("$tmp = abs($map)", tmp=tmpmap, map=map)
  82. # data centered on 0 (e.g. map of deviations)
  83. info = gscript.raster_info(tmpmap)
  84. maxv = info['max']
  85. # current r.univar truncates percentage to the base integer
  86. s = gscript.read_command('r.univar', flags='eg', map=map,
  87. percentile=[95.45,
  88. 68.2689,
  89. 99.7300])
  90. kv = gscript.parse_key_val(s)
  91. stddev1 = float(kv['percentile_68_2689'])
  92. stddev2 = float(kv['percentile_95_45'])
  93. stddev3 = float(kv['percentile_99_73'])
  94. if not bands:
  95. # zero centered smooth blue/white/red
  96. rules = '\n'.join(["%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(["%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. gscript.write_command('r.colors', map=map, rules='-', stdin=rules)
  119. if __name__ == "__main__":
  120. options, flags = gscript.parser()
  121. atexit.register(cleanup)
  122. main()