r_in_aster.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/python
  2. ############################################################################
  3. #
  4. # MODULE: r_in_aster.py
  5. # AUTHOR(S): Michael Barton (michael.barton@asu.edu)
  6. # Based on r.in.aster bash script for GRASS
  7. # by Michael Barton and Paul Kelly
  8. # PURPOSE: Rectifies, georeferences, & imports Terra-ASTER imagery
  9. # using gdalwarp
  10. # COPYRIGHT: (C) 2008 by the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. #
  18. # Requires:
  19. # gdalwarp
  20. #%Module
  21. #% description: Georeference, rectify, and import Terra-ASTER imagery and relative DEM's using gdalwarp.
  22. #% keywords: raster, imagery, import
  23. #%End
  24. #%option
  25. #% key: input
  26. #% type: string
  27. #% gisprompt: old_file,file,input
  28. #% description: Input ASTER image to be georeferenced & rectified
  29. #% required: yes
  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
  47. #% key: output
  48. #% type: string
  49. #% description: Base name for output raster map (band number will be appended to base name)
  50. #% gisprompt: old,cell,raster
  51. #% required: yes
  52. #%end
  53. #%flag
  54. #% key: o
  55. #% description: Overwrite existing file
  56. #%END
  57. import sys
  58. import os
  59. import subprocess
  60. import platform
  61. def main():
  62. #if ( os.getenv("GIS_FLAG_OVERWRITE") == "1" ):
  63. # print "Flag -f set"
  64. #else:
  65. # print "Flag -f not set"
  66. #check whether gdalwarp is in path and executable
  67. p = None
  68. try:
  69. p = subprocess.call(['gdalwarp', '--version'])
  70. except:
  71. pass
  72. if p == None or p != 0:
  73. subprocess.call(["g.message", "-e", "gdalwarp is not in the path and executable"])
  74. return
  75. #initialize variables
  76. dataset = ''
  77. srcfile = ''
  78. proj = ''
  79. band = ''
  80. outfile = ''
  81. bandlist = []
  82. #create temporary file to hold gdalwarp output before importing to GRASS
  83. tempfileCmd = subprocess.Popen(["g.tempfile","pid=%d" % os.getpid()],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  84. tempfile = tempfileCmd.stdout.read().strip()
  85. tempfile=tempfile + '.tif'
  86. #get projection information for current GRASS location
  87. p = subprocess.Popen(['g.proj', '-jf'], stdout=subprocess.PIPE,
  88. stderr=subprocess.PIPE)
  89. proj = p.stdout.read()
  90. #currently only runs in projected location
  91. if "XY location" in proj:
  92. subprocess.call(["g.message", "-e", "This module needs to be run in a projected location (found: %s)" % proj])
  93. return
  94. #process list of bands
  95. if os.getenv("GIS_OPT_BAND").strip() == 'all':
  96. bandlist = ['1','2','3n','3b','4','5','6','7','8','9','10','11','12','13','14']
  97. else:
  98. bandlist = os.getenv("GIS_OPT_BAND").split(',')
  99. print 'bandlist =',bandlist
  100. #initialize datasets for L1A and L1B
  101. #
  102. if os.getenv("GIS_OPT_PROCTYPE") == "L1A":
  103. for band in bandlist:
  104. if band == '1':
  105. dataset="VNIR_Band1:ImageData"
  106. elif band == '2':
  107. dataset="VNIR_Band2:ImageData"
  108. elif band == '3n':
  109. dataset="VNIR_Band3N:ImageData"
  110. elif band == '3b':
  111. dataset="VNIR_Band3B:ImageData"
  112. elif band == '4':
  113. dataset="SWIR_Band4:ImageData"
  114. elif band == '5':
  115. dataset="SWIR_Band5:ImageData"
  116. elif band == '6':
  117. dataset="SWIR_Band6:ImageData"
  118. elif band == '7':
  119. dataset="SWIR_Band7:ImageData"
  120. elif band == '8':
  121. dataset="SWIR_Band8:ImageData"
  122. elif band == '9':
  123. dataset="SWIR_Band9:ImageData"
  124. elif band == '10':
  125. dataset="TIR_Band10:ImageData"
  126. elif band == '11':
  127. dataset="TIR_Band11:ImageData"
  128. elif band == '12':
  129. dataset="TIR_Band12:ImageData"
  130. elif band == '13':
  131. dataset="TIR_Band13:ImageData"
  132. elif band == '14':
  133. dataset="TIR_Band14:ImageData"
  134. srcfile = "HDF4_EOS:EOS_SWATH:%s:%s" % (os.getenv("GIS_OPT_INPUT"), dataset)
  135. import_aster(proj, srcfile, tempfile, band)
  136. elif os.getenv("GIS_OPT_PROCTYPE") == "L1B":
  137. for band in bandlist:
  138. if band == '1':
  139. dataset="VNIR_Swath:ImageData1"
  140. elif band == '2':
  141. dataset="VNIR_Swath:ImageData2"
  142. elif band == '3n':
  143. dataset="VNIR_Swath:ImageData3N"
  144. elif band == '3b':
  145. dataset="VNIR_Swath:ImageData3B"
  146. elif band == '4':
  147. dataset="SWIR_Swath:ImageData4"
  148. elif band == '5':
  149. dataset="SWIR_Swath:ImageData5"
  150. elif band == '6':
  151. dataset="SWIR_Swath:ImageData6"
  152. elif band == '7':
  153. dataset="SWIR_Swath:ImageData7"
  154. elif band == '8':
  155. dataset="SWIR_Swath:ImageData8"
  156. elif band == '9':
  157. dataset="SWIR_Swath:ImageData9"
  158. elif band == '10':
  159. dataset="TIR_Swath:ImageData10"
  160. elif band == '11':
  161. dataset="TIR_Swath:ImageData11"
  162. elif band == '12':
  163. dataset="TIR_Swath:ImageData12"
  164. elif band == '13':
  165. dataset="TIR_Swath:ImageData13"
  166. elif band == '14':
  167. dataset="TIR_Swath:ImageData14"
  168. srcfile = "HDF4_EOS:EOS_SWATH:%s:%s" % (os.getenv("GIS_OPT_INPUT"), dataset)
  169. import_aster(proj, srcfile, tempfile, band)
  170. elif os.getenv("GIS_OPT_PROCTYPE") == "DEM":
  171. srcfile=os.getenv("GIS_OPT_INPUT")
  172. import_aster(proj, srcfile, tempfile, "DEM")
  173. #for debugging
  174. #print 'source file=',srcfile
  175. #print 'tempfile=',tempfile
  176. #print 'proj =',proj
  177. # write cmd history: Not sure how to replicate this in Python yet
  178. #r.support "$GIS_OPT_OUTPUT" history="${CMDLINE}"
  179. #cleanup
  180. subprocess.call(["g.message", "Cleaning up ..."])
  181. os.remove(tempfile)
  182. subprocess.call(["g.message", "Done."])
  183. return
  184. def import_aster(proj, srcfile, tempfile, band):
  185. #run gdalwarp with selected options (must be in $PATH)
  186. #to translate aster image to geotiff
  187. subprocess.call(["g.message", "Georeferencing aster image ..."])
  188. subprocess.call(["g.message", "-d", "message=gdalwarp -t_srs %s %s %s" % (proj, srcfile, tempfile)])
  189. if platform.system() == "Darwin":
  190. cmd = ["arch", "-i386", "gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  191. else:
  192. cmd = ["gdalwarp", "-t_srs", proj, srcfile, tempfile ]
  193. p = subprocess.call(cmd)
  194. if p != 0:
  195. #check to see if gdalwarp executed properly
  196. return
  197. #p = subprocess.call(["gdal_translate", srcfile, tempfile])
  198. #import geotiff to GRASS
  199. subprocess.call(["g.message", "Importing into GRASS ..."])
  200. outfile = os.getenv("GIS_OPT_OUTPUT").strip()+'.'+band
  201. cmd = ["r.in.gdal", "input=%s" % tempfile, "output=%s" % outfile]
  202. if ( os.getenv("GIS_FLAG_O") == "1" ):
  203. cmd.append("--o")
  204. #msgcmd = ['g.message']
  205. #msgcmd = msgcmd.extend(cmd)
  206. #subprocess.call(msgcmd)
  207. subprocess.call(cmd)
  208. return
  209. if __name__ == "__main__":
  210. if ( len(sys.argv) <= 1 or sys.argv[1] != "@ARGS_PARSED@" ):
  211. os.execvp("g.parser", [sys.argv[0]] + sys.argv)
  212. else:
  213. main();