i.tasscap.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: i.tasscap
  6. # AUTHOR(S): Markus Neteler. neteler itc.it
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: At-satellite reflectance based tasseled cap transformation.
  9. # COPYRIGHT: (C) 1997-2004,2008, 2014 by the GRASS Development Team
  10. #
  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. # TODO: Check if MODIS Tasseled Cap makes sense to be added
  16. # http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=1025776
  17. # Add other, e.g from here: http://www.sjsu.edu/faculty/watkins/tassel.htm
  18. #############################################################################
  19. # References:
  20. # LANDSAT-4/LANDSAT-5:
  21. # script based on i.tasscap.tm4 from Dr. Agustin Lobo - alobo@ija.csic.es
  22. # TC-factor changed to CRIST et al. 1986, p.1467 (Markus Neteler 1/99)
  23. # Proc. IGARSS 1986
  24. #
  25. # LANDSAT-7:
  26. # TASSCAP factors cited from:
  27. # DERIVATION OF A TASSELED CAP TRANSFORMATION BASED ON LANDSAT 7 AT-SATELLITE REFLECTANCE
  28. # Chengquan Huang, Bruce Wylie, Limin Yang, Collin Homer and Gregory Zylstra Raytheon ITSS,
  29. # USGS EROS Data Center Sioux Falls, SD 57198, USA
  30. # http://landcover.usgs.gov/pdf/tasseled.pdf
  31. #
  32. # This is published as well in INT. J. OF RS, 2002, VOL 23, NO. 8, 1741-1748.
  33. # Compare discussion:
  34. # http://adis.cesnet.cz/cgi-bin/lwgate/IMAGRS-L/archives/imagrs-l.log0211/date/article-14.html
  35. #############################################################################
  36. #
  37. #%Module
  38. #% description: Performs Tasseled Cap (Kauth Thomas) transformation.
  39. #% keywords: imagery
  40. #% keywords: transformation
  41. #% keywords: Landsat
  42. #% keywords: Tasseled Cap transformation
  43. #%end
  44. #%option G_OPT_R_INPUTS
  45. #% description: For Landsat4-7: bands 1, 2, 3, 4, 5, and 7
  46. #%end
  47. #%option G_OPT_R_BASENAME_OUTPUT
  48. #% label: Name for output basename raster map(s)
  49. #%end
  50. #%option
  51. #% key: sensor
  52. #% type: string
  53. #% description: Satellite sensor
  54. #% required: yes
  55. #% multiple: no
  56. #% options: landsat4_tm,landsat5_tm,landsat7_etm
  57. #% descriptions: landsat4_tm;Use transformation rules for Landsat 4 TM;landsat5_tm;Use transformation rules for Landsat 5 TM;landsat7_etm;Use transformation rules for Landsat 7 ETM
  58. #%end
  59. import grass.script as grass
  60. # weights for 6 Landsat bands: TM4, TM5, TM7
  61. parms = [[( 0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863), # Landsat TM4
  62. (-0.2848,-0.2435,-0.5435, 0.7243, 0.0840,-0.1800),
  63. ( 0.1509, 0.1973, 0.3279, 0.3406,-0.7112,-0.4572)],
  64. [( 0.2909, 0.2493, 0.4806, 0.5568, 0.4438, 0.1706, 10.3695), # Landsat TM5
  65. (-0.2728,-0.2174,-0.5508, 0.7221, 0.0733,-0.1648, -0.7310),
  66. ( 0.1446, 0.1761, 0.3322, 0.3396,-0.6210,-0.4186, -3.3828),
  67. ( 0.8461,-0.0731,-0.4640,-0.0032,-0.0492,-0.0119, 0.7879)],
  68. [( 0.3561, 0.3972, 0.3904, 0.6966, 0.2286, 0.1596), # Landsat TM7
  69. (-0.3344,-0.3544,-0.4556, 0.6966,-0.0242,-0.2630),
  70. ( 0.2626, 0.2141, 0.0926, 0.0656,-0.7629,-0.5388),
  71. ( 0.0805,-0.0498, 0.1950,-0.1327, 0.5752,-0.7775)]]
  72. ordinals = ["first", "second", "third", "fourth"]
  73. names = ["Brightness", "Greenness", "Wetness", "Haze"]
  74. def calc1(out, bands, k1, k2, k3, k4, k5, k7, k0 = 0):
  75. grass.mapcalc(
  76. "$out = $k1 * $band1 + $k2 * $band2 + $k3 * $band3 + $k4 * $band4 + $k5 * $band5 + $k7 * $band7 + $k0",
  77. out = out, k1 = k1, k2 = k2, k3 = k3, k4 = k4, k5 = k5, k7 = k7, k0 = k0, **bands)
  78. grass.run_command('r.colors', map = out, color = 'grey')
  79. def calcN(outpre, bands, i, n):
  80. grass.message(_("Satellite band-%d...") % n)
  81. for j, p in enumerate(parms[i]):
  82. out = "%s.%d" % (outpre, j + 1)
  83. ord = ordinals[j]
  84. if n == 4:
  85. name = ''
  86. else:
  87. name = " (%s)" % names[j]
  88. grass.message(_("Calculating %s TC component %s%s ...") % (ord, out, name))
  89. calc1(out, bands, *p)
  90. def main():
  91. options, flags = grass.parser()
  92. satellite = options['sensor']
  93. output_basename = options['output']
  94. inputs = options['input'].split(',')
  95. num_of_bands = 6
  96. if len(inputs) != num_of_bands:
  97. grass.fatal(_("The number of input raster maps (bands) should be %s") % num_of_bands)
  98. # this is here just for the compatibility with r.mapcalc expression
  99. # remove this if not really needed in new implementation
  100. bands = {}
  101. for i, band in enumerate(inputs):
  102. band_num = i + 1
  103. if band_num == 6:
  104. band_num = 7
  105. bands['band' + str(band_num)] = band
  106. grass.debug(1, bands)
  107. if satellite == 'landsat4_tm':
  108. calcN(output_basename, bands, 0, 4)
  109. elif satellite == 'landsat5_tm':
  110. calcN(output_basename, bands, 1, 5)
  111. elif satellite == 'landsat7_etm':
  112. calcN(output_basename, bands, 2, 7)
  113. else:
  114. raise RuntimeError("Invalid satellite: " + satellite)
  115. grass.run_command('r.support', map = "%s.%d" % (output_basename, 1), description = "Tasseled Cap 1: brightness")
  116. grass.run_command('r.support', map = "%s.%d" % (output_basename, 2), description = "Tasseled Cap 2: greenness")
  117. grass.run_command('r.support', map = "%s.%d" % (output_basename, 3), description = "Tasseled Cap 3: wetness")
  118. grass.run_command('r.support', map = "%s.%d" % (output_basename, 4), description = "Tasseled Cap 4: atmospheric haze")
  119. grass.message(_("Tasseled Cap components calculated"))
  120. if __name__ == "__main__":
  121. main()