r.colors.stddev.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: r.colors.stddev
  5. # AUTHOR: M. Hamish Bowman, Dept. Marine Science, Otago University,
  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. from grass.script.utils import decode
  35. def z(n):
  36. return mean + n * stddev
  37. def cleanup():
  38. if tmpmap:
  39. gscript.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 = gscript.read_command('r.univar', flags='g', map=map)
  49. kv = gscript.parse_key_val(decode(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(["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" <<
  64. # EOF
  65. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  66. rules = '\n'.join(["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. gscript.mapcalc("$tmp = abs($map)", tmp=tmpmap, map=map)
  83. # data centered on 0 (e.g. map of deviations)
  84. info = gscript.raster_info(tmpmap)
  85. maxv = info['max']
  86. # current r.univar truncates percentage to the base integer
  87. s = gscript.read_command('r.univar', flags='eg', map=map,
  88. percentile=[95.45,
  89. 68.2689,
  90. 99.7300])
  91. kv = gscript.parse_key_val(decode(s))
  92. stddev1 = float(kv['percentile_68_2689'])
  93. stddev2 = float(kv['percentile_95_45'])
  94. stddev3 = float(kv['percentile_99_73'])
  95. if not bands:
  96. # zero centered smooth blue/white/red
  97. rules = '\n'.join(["%f blue" % -maxv,
  98. "%f blue" % -stddev2,
  99. "0 white",
  100. "%f red" % stddev2,
  101. "%f red" % maxv])
  102. else:
  103. # zero centered banded black/red/yellow/green/yellow/red/black
  104. # >3 S.D. outliers colored black so they show up in d.histogram w/ white background
  105. rules = '\n'.join(["%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. gscript.write_command('r.colors', map=map, rules='-', stdin=rules)
  120. if __name__ == "__main__":
  121. options, flags = gscript.parser()
  122. atexit.register(cleanup)
  123. main()