~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to scripts/r.in.aster/r.in.aster.py

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

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