r.in.aster.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: r_in_aster.py
  6. # AUTHOR(S): Michael Barton (michael.barton@asu.edu) and
  7. # Glynn Clements (glynn@gclements.plus.com)
  8. # Based on r.in.aster bash script for GRASS
  9. # by Michael Barton and Paul Kelly
  10. # PURPOSE: Rectifies, georeferences, & imports Terra-ASTER imagery
  11. # using gdalwarp
  12. # COPYRIGHT: (C) 2008 by the GRASS Development Team
  13. #
  14. # This program is free software under the GNU General Public
  15. # License (>=v2). Read the file COPYING that comes with GRASS
  16. # for details.
  17. #
  18. #############################################################################
  19. #
  20. # Requires:
  21. # gdalwarp
  22. # gdal compiled with HDF4 support
  23. #%Module
  24. #% description: Georeference, rectify, and import Terra-ASTER imagery and relative DEMs using gdalwarp.
  25. #% keyword: raster
  26. #% keyword: import
  27. #% keyword: imagery
  28. #% keyword: Terra-ASTER
  29. #%End
  30. #%option G_OPT_F_INPUT
  31. #% description: Name of input ASTER image
  32. #%end
  33. #%option
  34. #% key: proctype
  35. #% type: string
  36. #% description: ASTER imagery processing type (Level 1A, Level 1B, or relative DEM)
  37. #% options: L1A,L1B,DEM
  38. #% answer: L1B
  39. #% required: yes
  40. #%end
  41. #%option
  42. #% key: band
  43. #% type: string
  44. #% description: List L1A or L1B band to translate (1,2,3n,...), or enter 'all' to translate all bands
  45. #% answer: all
  46. #% required: yes
  47. #%end
  48. #%option G_OPT_R_OUTPUT
  49. #% description: Base name for output raster map (band number will be appended to base name)
  50. #%end
  51. import sys
  52. import os
  53. import platform
  54. import grass.script as grass
  55. bands = {
  56. 'L1A': {
  57. '1': "VNIR_Band1:ImageData",
  58. '2': "VNIR_Band2:ImageData",
  59. '3n': "VNIR_Band3N:ImageData",
  60. '3b': "VNIR_Band3B:ImageData",
  61. '4': "SWIR_Band4:ImageData",
  62. '5': "SWIR_Band5:ImageData",
  63. '6': "SWIR_Band6:ImageData",
  64. '7': "SWIR_Band7:ImageData",
  65. '8': "SWIR_Band8:ImageData",
  66. '9': "SWIR_Band9:ImageData",
  67. '10': "TIR_Band10:ImageData",
  68. '11': "TIR_Band11:ImageData",
  69. '12': "TIR_Band12:ImageData",
  70. '13': "TIR_Band13:ImageData",
  71. '14': "TIR_Band14:ImageData"
  72. },
  73. 'L1B': {
  74. '1': "VNIR_Swath:ImageData1",
  75. '2': "VNIR_Swath:ImageData2",
  76. '3n': "VNIR_Swath:ImageData3N",
  77. '3b': "VNIR_Swath:ImageData3B",
  78. '4': "SWIR_Swath:ImageData4",
  79. '5': "SWIR_Swath:ImageData5",
  80. '6': "SWIR_Swath:ImageData6",
  81. '7': "SWIR_Swath:ImageData7",
  82. '8': "SWIR_Swath:ImageData8",
  83. '9': "SWIR_Swath:ImageData9",
  84. '10': "TIR_Swath:ImageData10",
  85. '11': "TIR_Swath:ImageData11",
  86. '12': "TIR_Swath:ImageData12",
  87. '13': "TIR_Swath:ImageData13",
  88. '14': "TIR_Swath:ImageData14"
  89. }
  90. }
  91. def main():
  92. input = options['input']
  93. proctype = options['proctype']
  94. output = options['output']
  95. band = options['band']
  96. #check whether gdalwarp is in path and executable
  97. if not grass.find_program('gdalwarp', '--help'):
  98. grass.fatal(_("gdalwarp is not in the path and executable"))
  99. #create temporary file to hold gdalwarp output before importing to GRASS
  100. tempfile = grass.read_command("g.tempfile", pid = os.getpid()).strip() + '.tif'
  101. #get projection information for current GRASS location
  102. proj = grass.read_command('g.proj', flags = 'jf').strip()
  103. #currently only runs in projected location
  104. if "XY location" in proj:
  105. grass.fatal(_("This module needs to be run in a projected location (found: %s)") % proj)
  106. #process list of bands
  107. allbands = ['1','2','3n','3b','4','5','6','7','8','9','10','11','12','13','14']
  108. if band == 'all':
  109. bandlist = allbands
  110. else:
  111. bandlist = band.split(',')
  112. #initialize datasets for L1A and L1B
  113. if proctype in ["L1A", "L1B"]:
  114. for band in bandlist:
  115. if band in allbands:
  116. dataset = bands[proctype][band]
  117. srcfile = "HDF4_EOS:EOS_SWATH:%s:%s" % (input, dataset)
  118. import_aster(proj, srcfile, tempfile, output, band)
  119. else:
  120. grass.fatal(_('band %s is not an available Terra/ASTER band') % band)
  121. elif proctype == "DEM":
  122. srcfile = input
  123. import_aster(proj, srcfile, tempfile, output, "DEM")
  124. #cleanup
  125. grass.message(_("Cleaning up ..."))
  126. grass.try_remove(tempfile)
  127. grass.message(_("Done."))
  128. return
  129. def import_aster(proj, srcfile, tempfile, output, band):
  130. #run gdalwarp with selected options (must be in $PATH)
  131. #to translate aster image to geotiff
  132. grass.message(_("Georeferencing aster image ..."))
  133. grass.debug("gdalwarp -t_srs %s %s %s" % (proj, srcfile, tempfile))
  134. if platform.system() == "Darwin":
  135. cmd = ["arch", "-i386", "gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  136. else:
  137. cmd = ["gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  138. p = grass.call(cmd)
  139. if p != 0:
  140. #check to see if gdalwarp executed properly
  141. return
  142. #import geotiff to GRASS
  143. grass.message(_("Importing into GRASS ..."))
  144. outfile = "%s.%s" % (output, band)
  145. grass.run_command("r.in.gdal", input = tempfile, output = outfile)
  146. # write cmd history
  147. grass.raster_history(outfile)
  148. if __name__ == "__main__":
  149. options, flags = grass.parser()
  150. main()