r.in.aster.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 platform
  62. import grass.script as grass
  63. bands = {
  64. 'L1A': {
  65. '1': "VNIR_Band1:ImageData",
  66. '2': "VNIR_Band2:ImageData",
  67. '3n': "VNIR_Band3N:ImageData",
  68. '3b': "VNIR_Band3B:ImageData",
  69. '4': "SWIR_Band4:ImageData",
  70. '5': "SWIR_Band5:ImageData",
  71. '6': "SWIR_Band6:ImageData",
  72. '7': "SWIR_Band7:ImageData",
  73. '8': "SWIR_Band8:ImageData",
  74. '9': "SWIR_Band9:ImageData",
  75. '10': "TIR_Band10:ImageData",
  76. '11': "TIR_Band11:ImageData",
  77. '12': "TIR_Band12:ImageData",
  78. '13': "TIR_Band13:ImageData",
  79. '14': "TIR_Band14:ImageData"
  80. },
  81. 'L1B': {
  82. '1': "VNIR_Swath:ImageData1",
  83. '2': "VNIR_Swath:ImageData2",
  84. '3n': "VNIR_Swath:ImageData3N",
  85. '3b': "VNIR_Swath:ImageData3B",
  86. '4': "SWIR_Swath:ImageData4",
  87. '5': "SWIR_Swath:ImageData5",
  88. '6': "SWIR_Swath:ImageData6",
  89. '7': "SWIR_Swath:ImageData7",
  90. '8': "SWIR_Swath:ImageData8",
  91. '9': "SWIR_Swath:ImageData9",
  92. '10': "TIR_Swath:ImageData10",
  93. '11': "TIR_Swath:ImageData11",
  94. '12': "TIR_Swath:ImageData12",
  95. '13': "TIR_Swath:ImageData13",
  96. '14': "TIR_Swath:ImageData14"
  97. }
  98. }
  99. def main():
  100. input = options['input']
  101. proctype = options['proctype']
  102. output = options['output']
  103. band = options['band']
  104. #check whether gdalwarp is in path and executable
  105. if not grass.find_program('gdalwarp', ['--version']):
  106. grass.fatal("gdalwarp is not in the path and executable")
  107. #create temporary file to hold gdalwarp output before importing to GRASS
  108. tempfile = grass.read_command("g.tempfile", pid = os.getpid()).strip() + '.tif'
  109. #get projection information for current GRASS location
  110. proj = grass.read_command('g.proj', flags = 'jf').strip()
  111. #currently only runs in projected location
  112. if "XY location" in proj:
  113. grass.fatal("This module needs to be run in a projected location (found: %s)" % proj)
  114. #process list of bands
  115. allbands = ['1','2','3n','3b','4','5','6','7','8','9','10','11','12','13','14']
  116. if band == 'all':
  117. bandlist = allbands
  118. else:
  119. bandlist = band.split(',')
  120. #initialize datasets for L1A and L1B
  121. if proctype in ["L1A", "L1B"]:
  122. for band in bandlist:
  123. if band in allbands:
  124. dataset = bands[proctype][band]
  125. srcfile = "HDF4_EOS:EOS_SWATH:%s:%s" % (input, dataset)
  126. import_aster(proj, srcfile, tempfile, band)
  127. else:
  128. grass.fatal('band %s is not an available Terra/ASTER band' % band)
  129. elif proctype == "DEM":
  130. srcfile = input
  131. import_aster(proj, srcfile, tempfile, "DEM")
  132. #cleanup
  133. grass.message("Cleaning up ...")
  134. grass.try_remove(tempfile)
  135. grass.message("Done.")
  136. return
  137. def import_aster(proj, srcfile, tempfile, band):
  138. #run gdalwarp with selected options (must be in $PATH)
  139. #to translate aster image to geotiff
  140. grass.message("Georeferencing aster image ...")
  141. grass.debug("gdalwarp -t_srs %s %s %s" % (proj, srcfile, tempfile))
  142. if platform.system() == "Darwin":
  143. cmd = ["arch", "-i386", "gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  144. else:
  145. cmd = ["gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  146. p = grass.call(cmd)
  147. if p != 0:
  148. #check to see if gdalwarp executed properly
  149. return
  150. #import geotiff to GRASS
  151. grass.message("Importing into GRASS ...")
  152. outfile = "%s.%s" % (output, band)
  153. grass.run_command("r.in.gdal", overwrite = flags['o'], input = tempfile, output = outfile)
  154. # write cmd history
  155. grass.raster_history(outfile)
  156. if __name__ == "__main__":
  157. options, flags = grass.parser()
  158. main()