i.tasscap.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #############################################################################
  20. # References:
  21. # LANDSAT-4/LANDSAT-5:
  22. # script based on i.tasscap.tm4 from Dr. Agustin Lobo - alobo@ija.csic.es
  23. # TC-factor changed to CRIST et al. 1986, p.1467 (Markus Neteler 1/99)
  24. # Proc. IGARSS 1986
  25. #
  26. # LANDSAT-7:
  27. # TASSCAP factors cited from:
  28. # DERIVATION OF A TASSELED CAP TRANSFORMATION BASED ON LANDSAT 7 AT-SATELLITE REFLECTANCE
  29. # Chengquan Huang, Bruce Wylie, Limin Yang, Collin Homer and Gregory Zylstra Raytheon ITSS,
  30. # USGS EROS Data Center Sioux Falls, SD 57198, USA
  31. # http://landcover.usgs.gov/pdf/tasseled.pdf
  32. #
  33. # This is published as well in INT. J. OF RS, 2002, VOL 23, NO. 8, 1741-1748.
  34. # Compare discussion:
  35. # http://adis.cesnet.cz/cgi-bin/lwgate/IMAGRS-L/archives/imagrs-l.log0211/date/article-14.html
  36. #
  37. # Landsat8: Baig, M.H.A., Zhang, L., Shuai, T., Tong, Q., 2014. Derivation of a tasselled cap transformation
  38. # based on Landsat 8 at-satellite reflectance. Remote Sensing Letters 5, 423-431.
  39. # doi:10.1080/2150704X.2014.915434
  40. #
  41. #############################################################################
  42. #
  43. #%Module
  44. #% description: Performs Tasseled Cap (Kauth Thomas) transformation.
  45. #% keywords: imagery
  46. #% keywords: transformation
  47. #% keywords: Landsat
  48. #% keywords: Tasseled Cap transformation
  49. #%end
  50. #%option G_OPT_R_INPUTS
  51. #% description: For Landsat4-7: bands 1, 2, 3, 4, 5, and 7; for Landsat8: bands 2, 3, 4, 5, 6, and 7
  52. #%end
  53. #%option G_OPT_R_BASENAME_OUTPUT
  54. #% label: Name for output basename raster map(s)
  55. #%end
  56. #%option
  57. #% key: sensor
  58. #% type: string
  59. #% description: Satellite sensor
  60. #% required: yes
  61. #% multiple: no
  62. #% options: landsat4_tm,landsat5_tm,landsat7_etm,landsat8
  63. #% 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;landsat8;Use transformation rules for Landsat 8
  64. #%end
  65. import grass.script as grass
  66. # weights for 6 Landsat bands: TM4, TM5, TM7, OLI
  67. parms = [[( 0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863), # Landsat TM4
  68. (-0.2848,-0.2435,-0.5435, 0.7243, 0.0840,-0.1800),
  69. ( 0.1509, 0.1973, 0.3279, 0.3406,-0.7112,-0.4572)],
  70. [( 0.2909, 0.2493, 0.4806, 0.5568, 0.4438, 0.1706, 10.3695), # Landsat TM5
  71. (-0.2728,-0.2174,-0.5508, 0.7221, 0.0733,-0.1648, -0.7310),
  72. ( 0.1446, 0.1761, 0.3322, 0.3396,-0.6210,-0.4186, -3.3828),
  73. ( 0.8461,-0.0731,-0.4640,-0.0032,-0.0492,-0.0119, 0.7879)],
  74. [( 0.3561, 0.3972, 0.3904, 0.6966, 0.2286, 0.1596), # Landsat TM7
  75. (-0.3344,-0.3544,-0.4556, 0.6966,-0.0242,-0.2630),
  76. ( 0.2626, 0.2141, 0.0926, 0.0656,-0.7629,-0.5388),
  77. ( 0.0805,-0.0498, 0.1950,-0.1327, 0.5752,-0.7775)],
  78. [( 0.3029, 0.2786, 0.4733, 0.5599, 0.5080, 0.1872), # Landsat TM8
  79. (-0.2941,-0.2430,-0.5424, 0.7276, 0.0713,-0.1608),
  80. ( 0.1511, 0.1973, 0.3283, 0.3407,-0.7117,-0.4559),
  81. (-0.8239, 0.0849, 0.4396, -0.058, 0.2013,-0.2773)]]
  82. ordinals = ["first", "second", "third", "fourth"]
  83. names = ["Brightness", "Greenness", "Wetness", "Haze"]
  84. def calc1(out, bands, k1, k2, k3, k4, k5, k7, k0 = 0):
  85. grass.mapcalc(
  86. "$out = $k1 * $band1 + $k2 * $band2 + $k3 * $band3 + $k4 * $band4 + $k5 * $band5 + $k7 * $band7 + $k0",
  87. out = out, k1 = k1, k2 = k2, k3 = k3, k4 = k4, k5 = k5, k7 = k7, k0 = k0, **bands)
  88. grass.run_command('r.colors', map = out, color = 'grey')
  89. def calcN(outpre, bands, i, n):
  90. grass.message(_("Satellite band-%d...") % n)
  91. for j, p in enumerate(parms[i]):
  92. out = "%s.%d" % (outpre, j + 1)
  93. ord = ordinals[j]
  94. if n == 4:
  95. name = ''
  96. else:
  97. name = " (%s)" % names[j]
  98. grass.message(_("Calculating %s TC component %s%s ...") % (ord, out, name))
  99. calc1(out, bands, *p)
  100. def main():
  101. options, flags = grass.parser()
  102. satellite = options['sensor']
  103. output_basename = options['output']
  104. inputs = options['input'].split(',')
  105. num_of_bands = 6
  106. if len(inputs) != num_of_bands:
  107. grass.fatal(_("The number of input raster maps (bands) should be %s") % num_of_bands)
  108. # this is here just for the compatibility with r.mapcalc expression
  109. # remove this if not really needed in new implementation
  110. bands = {}
  111. for i, band in enumerate(inputs):
  112. band_num = i + 1
  113. if band_num == 6:
  114. band_num = 7
  115. bands['band' + str(band_num)] = band
  116. grass.debug(1, bands)
  117. if satellite == 'landsat4_tm':
  118. calcN(output_basename, bands, 0, 4)
  119. elif satellite == 'landsat5_tm':
  120. calcN(output_basename, bands, 1, 5)
  121. elif satellite == 'landsat7_etm':
  122. calcN(output_basename, bands, 2, 7)
  123. elif satellite == 'landsat8':
  124. calcN(output_basename, bands, 2, 7)
  125. else:
  126. raise RuntimeError("Invalid satellite: " + satellite)
  127. grass.run_command('r.support', map = "%s.%d" % (output_basename, 1), description = "Tasseled Cap 1: brightness")
  128. grass.run_command('r.support', map = "%s.%d" % (output_basename, 2), description = "Tasseled Cap 2: greenness")
  129. grass.run_command('r.support', map = "%s.%d" % (output_basename, 3), description = "Tasseled Cap 3: wetness")
  130. grass.run_command('r.support', map = "%s.%d" % (output_basename, 4), description = "Tasseled Cap 4: atmospheric haze")
  131. grass.message(_("Tasseled Cap components calculated"))
  132. if __name__ == "__main__":
  133. main()