r_in_aster.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/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 _message(msg, *args):
  101. subprocess.call(["g.message", "message=%s" % msg] + list(args))
  102. def debug(msg):
  103. _message(msg, '-d')
  104. def message(msg):
  105. _message(msg)
  106. def error(msg):
  107. _message(msg, '-e')
  108. sys.exit(1)
  109. def main():
  110. #check whether gdalwarp is in path and executable
  111. p = None
  112. try:
  113. p = subprocess.call(['gdalwarp', '--version'])
  114. except:
  115. pass
  116. if p == None or p != 0:
  117. error("gdalwarp is not in the path and executable")
  118. #initialize variables
  119. dataset = ''
  120. srcfile = ''
  121. proj = ''
  122. band = ''
  123. outfile = ''
  124. bandlist = []
  125. #create temporary file to hold gdalwarp output before importing to GRASS
  126. tempfile = grass.read_command("g.tempfile", pid = os.getpid()).strip() + '.tif'
  127. #get projection information for current GRASS location
  128. proj = grass.read_command('g.proj', flags = 'jf').strip()
  129. #currently only runs in projected location
  130. if "XY location" in proj:
  131. error ("This module needs to be run in a projected location (found: %s)" % proj)
  132. #process list of bands
  133. allbands = ['1','2','3n','3b','4','5','6','7','8','9','10','11','12','13','14']
  134. if options['band'].strip() == 'all':
  135. bandlist = allbands
  136. else:
  137. bandlist = options['band'].split(',')
  138. #initialize datasets for L1A and L1B
  139. if options['proctype'] in ["L1A", "L1B"]:
  140. for band in bandlist:
  141. if band in allbands:
  142. dataset = bands[options['proctype']][band]
  143. srcfile = "HDF4_EOS:EOS_SWATH:%s:%s" % (options['input'], dataset)
  144. import_aster(proj, srcfile, tempfile, band)
  145. else:
  146. error('band %s is not an available Terra/ASTER band' % band)
  147. elif options['proctype'] == "DEM":
  148. srcfile=options['input']
  149. import_aster(proj, srcfile, tempfile, "DEM")
  150. # write cmd history: Not sure how to replicate this in Python yet
  151. #r.support "$GIS_OPT_OUTPUT" history="${CMDLINE}"
  152. #cleanup
  153. message("Cleaning up ...")
  154. os.remove(tempfile)
  155. message("Done.")
  156. return
  157. def import_aster(proj, srcfile, tempfile, band):
  158. #run gdalwarp with selected options (must be in $PATH)
  159. #to translate aster image to geotiff
  160. message("Georeferencing aster image ...")
  161. debug("gdalwarp -t_srs %s %s %s" % (proj, srcfile, tempfile))
  162. if platform.system() == "Darwin":
  163. cmd = ["arch", "-i386", "gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  164. else:
  165. cmd = ["gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  166. p = subprocess.call(cmd)
  167. if p != 0:
  168. #check to see if gdalwarp executed properly
  169. return
  170. #p = subprocess.call(["gdal_translate", srcfile, tempfile])
  171. #import geotiff to GRASS
  172. message("Importing into GRASS ...")
  173. outfile = options['output'].strip()+'.'+band
  174. grass.run_command("r.in.gdal", overwrite = flags['o'], input = tempfile, output = outfile)
  175. return
  176. if __name__ == "__main__":
  177. options, flags = grass.parser()
  178. main()