Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
import numpy as np
from osgeo import gdal
import os
import argparse
import pyproj
from geocodeGdal import write_xml
def createParser():
'''
Create command line parser.
'''
parser = argparse.ArgumentParser(description='Convert geocoded files to Antarctica grid')
parser.add_argument('-i', '--input', dest='infile', type=str, required=True,
help='Input file to geocode')
parser.add_argument('-r', '--resamp', dest='resampmethod', type=str, default='near',
help='Resampling method')
parser.add_argument('-f', '--format', dest='outformat', type=str, default='GTiff',
help='Output GDAL format. If ENVI, ISCE XML is also written.')
return parser
def cmdLineParse(iargs=None):
'''
Command line parser.
'''
parser = createParser()
inps = parser.parse_args(args=iargs)
inps.infile = inps.infile.split()
if inps.outformat not in ['ENVI', 'GTiff']:
raise Exception('Format can be ENVI or GTiff')
return inps
def getGridLimits(geofile=None, latfile=None, lonfile=None):
'''
Get limits corresponding to Alex's grid.
'''
xmin = -2678407.5
xmax = 2816632.5
Nx = 22896
ymin = -2154232.5
ymax = 2259847.5
Ny = 18392
delx = 240.
dely = 240.
spolar = pyproj.Proj("+init=EPSG:3031")
minyy = np.inf
minxx = np.inf
maxxx = -np.inf
maxyy = -np.inf
samples = 20
if geofile is None:
latds = gdal.Open(latfile, gdal.GA_ReadOnly)
londs = gdal.Open(lonfile, gdal.GA_ReadOnly)
width = latds.RasterXSize
lgth = latds.RasterYSize
xs = np.linspace(0, width-1, num=samples).astype(int)
ys = np.linspace(0, lgth-1, num=samples).astype(int)
for line in range(samples):
lats = latds.GetRasterBand(1).ReadAsArray(0, ys[line], width, 1)
lons = londs.GetRasterBand(1).ReadAsArray(0, ys[line], width, 1)
llat = lats[xs]
llon = lats[ys]
xx, yy = spolar(llon, llat)
minxx = min(minxx, xx.min())
maxxx = max(maxxx, xx.max())
minyy = min(minyy, yy.min())
maxyy = max(maxyy, yy.max())
latds = None
londs = None
elif (latfile is None) and (lonfile is None):
ds = gdal.Open(geofile, gdal.GA_ReadOnly)
trans = ds.GetGeoTransform()
width = ds.RasterXSize
lgth = ds.RasterYSize
ds = None
xs = np.linspace(0, width-1, num=samples)
ys = np.linspace(0, lgth-1, num=samples)
lons = trans[0] + xs * trans[1]
for line in range(samples):
lats = (trans[3] + ys[line] * trans[5]) * np.ones(samples)
xx, yy = spolar(lons, lats)
minxx = min(minxx, xx.min())
maxxx = max(maxxx, xx.max())
minyy = min(minyy, yy.min())
maxyy = max(maxyy, yy.max())
else:
raise Exception('Either geofile is provided (or) latfile and lonfile. All 3 inputs cannot be provided')
ii0 = max(int((ymax - maxyy - dely/2.0) / dely ), 0)
ii1 = min(int((ymax - minyy + dely/2.0) / dely ) + 1, Ny)
jj0 = max(int((minxx - xmin - delx/2.0)/delx), 0)
jj1 = min(int((maxxx - xmin + delx/2.0)/delx) + 1, Nx)
ylim = ymax - np.array([ii1,ii0]) * dely
xlim = xmin + np.array([jj0,jj1]) * delx
return ylim, xlim
def runGeo(inps, ylim, xlim, method='near', fmt='GTiff'):
WSEN = str(xlim[0]) + ' ' + str(ylim[0]) + ' ' + str(xlim[1]) + ' ' + str(ylim[1])
if fmt == 'ENVI':
ext = '.ant'
else:
ext = '.tif'
for infile in inps:
infile = os.path.abspath(infile)
print('geocoding: ' + infile)
cmd = 'gdalwarp -of ' + fmt + ' -t_srs EPSG:3031 -te ' + WSEN + ' -tr 240.0 240.0 -srcnodata 0 -dstnodata 0 -r ' + method + ' ' + infile + ' ' + infile + ext
status = os.system(cmd)
if status:
raise Exception('Command {0} Failed'.format(cmd))
def main(iargs=None):
'''
Main driver.
'''
inps = cmdLineParse(iargs)
ylim, xlim = getGridLimits(geofile=inps.infile[0])
print('YLim: ', ylim, (ylim[1]-ylim[0])/240. + 1)
print('XLim: ', xlim, (xlim[1]-xlim[0])/240. + 1)
runGeo(inps.infile, ylim, xlim,
method=inps.resampmethod, fmt=inps.outformat)
if __name__ == '__main__':
main()