r.in.aster.py 5.3 KB

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