i.tasscap.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 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
  45. #% key: satellite
  46. #% type: string
  47. #% description: Satellite sensor
  48. #% required: yes
  49. #% multiple: no
  50. #% options: landsat4_tm,landsat5_tm,landsat7_etm
  51. #% 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
  52. #%end
  53. #%option G_OPT_R_INPUTS
  54. #%end
  55. #%option G_OPT_R_BASENAME_OUTPUT
  56. #% label: Name of input raster map(s)
  57. #% description: For Landsat 4-7, the raster maps should be the bands 1, 2, 3, 4, 5, and 7.
  58. #%end
  59. import sys
  60. import os
  61. import grass.script as grass
  62. parms = [[( 0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863),
  63. (-0.2848,-0.2435,-0.5435, 0.7243, 0.0840,-0.1800),
  64. ( 0.1509, 0.1973, 0.3279, 0.3406,-0.7112,-0.4572)],
  65. [( 0.2909, 0.2493, 0.4806, 0.5568, 0.4438, 0.1706, 10.3695),
  66. (-0.2728,-0.2174,-0.5508, 0.7221, 0.0733,-0.1648, -0.7310),
  67. ( 0.1446, 0.1761, 0.3322, 0.3396,-0.6210,-0.4186, -3.3828),
  68. ( 0.8461,-0.0731,-0.4640,-0.0032,-0.0492,-0.0119, 0.7879)],
  69. [( 0.3561, 0.3972, 0.3904, 0.6966, 0.2286, 0.1596),
  70. (-0.3344,-0.3544,-0.4556, 0.6966,-0.0242,-0.2630),
  71. ( 0.2626, 0.2141, 0.0926, 0.0656,-0.7629,-0.5388),
  72. ( 0.0805,-0.0498, 0.1950,-0.1327, 0.5752,-0.7775)]]
  73. ordinals = ["first", "second", "third", "fourth"]
  74. names = ["Brightness", "Greenness", "Wetness", "Haze"]
  75. def calc1(out, bands, k1, k2, k3, k4, k5, k7, k0 = 0):
  76. grass.mapcalc(
  77. "$out = $k1 * $band1 + $k2 * $band2 + $k3 * $band3 + $k4 * $band4 + $k5 * $band5 + $k7 * $band7 + $k0",
  78. out = out, k1 = k1, k2 = k2, k3 = k3, k4 = k4, k5 = k5, k7 = k7, k0 = k0, **bands)
  79. grass.run_command('r.colors', map = out, color = 'grey')
  80. def calcN(outpre, bands, i, n):
  81. grass.message(_("LANDSAT-%d...") % n)
  82. for j, p in enumerate(parms[i]):
  83. out = "%s.%d" % (outpre, j + 1)
  84. ord = ordinals[j]
  85. if n == 4:
  86. name = ''
  87. else:
  88. name = " (%s)" % names[j]
  89. grass.message(_("Calculating %s TC component %s%s ...") % (ord, out, name))
  90. calc1(out, bands, *p)
  91. def main():
  92. options, flags = grass.parser()
  93. satellite = options['satellite']
  94. output_basename = options['basename']
  95. inputs = options['input'].split(',')
  96. num_of_bands = 6
  97. if len(inputs) != num_of_bands:
  98. grass.fatal(_("The number of input raster maps (bands) should be %s.") % num_of_bands)
  99. # this is here just for the compatibility with r.mapcalc expression
  100. # remove this if not really needed in new implementation
  101. bands = {}
  102. for i, band in enumerate(inputs):
  103. band_num = i + 1
  104. if band_num == 6:
  105. band_num = 7
  106. bands['band' + str(band_num)] = band
  107. print bands
  108. if satellite == 'landsat4_tm':
  109. calcN(output_basename, bands, 0, 4)
  110. elif satellite == 'landsat5_tm':
  111. calcN(output_basename, bands, 1, 5)
  112. elif satellite == 'landsat7_etm':
  113. calcN(output_basename, bands, 2, 7)
  114. else:
  115. raise RuntimeError("Invalid satellite: " + satellite)
  116. grass.message(_("Tasseled Cap components calculated."))
  117. if __name__ == "__main__":
  118. main()