r.in.aster.py 5.2 KB

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