r.in.aster.py 5.5 KB

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