r.in.aster.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 DEMs using gdalwarp.
  24. #% keywords: raster
  25. #% keywords: imagery
  26. #% keywords: import
  27. #%End
  28. #%option
  29. #% key: input
  30. #% type: string
  31. #% gisprompt: old_file,file,input
  32. #% description: Input ASTER image to be georeferenced & rectified
  33. #% required: yes
  34. #%end
  35. #%option
  36. #% key: proctype
  37. #% type: string
  38. #% description: ASTER imagery processing type (Level 1A, Level 1B, or relative DEM)
  39. #% options: L1A,L1B,DEM
  40. #% answer: L1B
  41. #% required: yes
  42. #%end
  43. #%option
  44. #% key: band
  45. #% type: string
  46. #% description: List L1A or L1B band to translate (1,2,3n,...), or enter 'all' to translate all bands
  47. #% answer: all
  48. #% required: yes
  49. #%end
  50. #%option
  51. #% key: output
  52. #% type: string
  53. #% description: Base name for output raster map (band number will be appended to base name)
  54. #% gisprompt: old,cell,raster
  55. #% required: yes
  56. #%end
  57. #%flag
  58. #% key: o
  59. #% description: Overwrite existing file
  60. #%END
  61. import sys
  62. import os
  63. import platform
  64. import grass.script as grass
  65. bands = {
  66. 'L1A': {
  67. '1': "VNIR_Band1:ImageData",
  68. '2': "VNIR_Band2:ImageData",
  69. '3n': "VNIR_Band3N:ImageData",
  70. '3b': "VNIR_Band3B:ImageData",
  71. '4': "SWIR_Band4:ImageData",
  72. '5': "SWIR_Band5:ImageData",
  73. '6': "SWIR_Band6:ImageData",
  74. '7': "SWIR_Band7:ImageData",
  75. '8': "SWIR_Band8:ImageData",
  76. '9': "SWIR_Band9:ImageData",
  77. '10': "TIR_Band10:ImageData",
  78. '11': "TIR_Band11:ImageData",
  79. '12': "TIR_Band12:ImageData",
  80. '13': "TIR_Band13:ImageData",
  81. '14': "TIR_Band14:ImageData"
  82. },
  83. 'L1B': {
  84. '1': "VNIR_Swath:ImageData1",
  85. '2': "VNIR_Swath:ImageData2",
  86. '3n': "VNIR_Swath:ImageData3N",
  87. '3b': "VNIR_Swath:ImageData3B",
  88. '4': "SWIR_Swath:ImageData4",
  89. '5': "SWIR_Swath:ImageData5",
  90. '6': "SWIR_Swath:ImageData6",
  91. '7': "SWIR_Swath:ImageData7",
  92. '8': "SWIR_Swath:ImageData8",
  93. '9': "SWIR_Swath:ImageData9",
  94. '10': "TIR_Swath:ImageData10",
  95. '11': "TIR_Swath:ImageData11",
  96. '12': "TIR_Swath:ImageData12",
  97. '13': "TIR_Swath:ImageData13",
  98. '14': "TIR_Swath:ImageData14"
  99. }
  100. }
  101. def main():
  102. input = options['input']
  103. proctype = options['proctype']
  104. output = options['output']
  105. band = options['band']
  106. #check whether gdalwarp is in path and executable
  107. if not grass.find_program('gdalwarp', ['--version']):
  108. grass.fatal(_("gdalwarp is not in the path and executable"))
  109. #create temporary file to hold gdalwarp output before importing to GRASS
  110. tempfile = grass.read_command("g.tempfile", pid = os.getpid()).strip() + '.tif'
  111. #get projection information for current GRASS location
  112. proj = grass.read_command('g.proj', flags = 'jf').strip()
  113. #currently only runs in projected location
  114. if "XY location" in proj:
  115. grass.fatal(_("This module needs to be run in a projected location (found: %s)") % proj)
  116. #process list of bands
  117. allbands = ['1','2','3n','3b','4','5','6','7','8','9','10','11','12','13','14']
  118. if band == 'all':
  119. bandlist = allbands
  120. else:
  121. bandlist = band.split(',')
  122. #initialize datasets for L1A and L1B
  123. if proctype in ["L1A", "L1B"]:
  124. for band in bandlist:
  125. if band in allbands:
  126. dataset = bands[proctype][band]
  127. srcfile = "HDF4_EOS:EOS_SWATH:%s:%s" % (input, dataset)
  128. import_aster(proj, srcfile, tempfile, band)
  129. else:
  130. grass.fatal(_('band %s is not an available Terra/ASTER band') % band)
  131. elif proctype == "DEM":
  132. srcfile = input
  133. import_aster(proj, srcfile, tempfile, "DEM")
  134. #cleanup
  135. grass.message(_("Cleaning up ..."))
  136. grass.try_remove(tempfile)
  137. grass.message(_("Done."))
  138. return
  139. def import_aster(proj, srcfile, tempfile, band):
  140. #run gdalwarp with selected options (must be in $PATH)
  141. #to translate aster image to geotiff
  142. grass.message(_("Georeferencing aster image ..."))
  143. grass.debug("gdalwarp -t_srs %s %s %s" % (proj, srcfile, tempfile))
  144. if platform.system() == "Darwin":
  145. cmd = ["arch", "-i386", "gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  146. else:
  147. cmd = ["gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  148. p = grass.call(cmd)
  149. if p != 0:
  150. #check to see if gdalwarp executed properly
  151. return
  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()