r.in.aster.py 5.3 KB

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