-
Notifications
You must be signed in to change notification settings - Fork 11
/
reprojectGDAL.py
207 lines (168 loc) · 5.68 KB
/
reprojectGDAL.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
import os
import subprocess as sp
import numpy as np
from netCDF4 import Dataset
verbose=True
# set to the path where gdal binaries are located
# ex: gdalPath='/usr/loca/bin'
# None means they are in the PATH environment variable
gdalPath='/usr/local/bin'
gdalEnv={}
# from WPS/geogrid/src/misc_definitions_module.F
wrfProjectionsCode={
0:'PROJ_LATLONG',
1:'PROJ_LC',
2:'PROJ_PS',
3:'PROJ_MERC',
4:'PROJ_GAUSS',
5:'PROJ_CYL',
6:'PROJ_CASSINI',
102:'PROJ_PS_WGS84',
105:'PROJ_ALBERS_NAD83',
203:'PROJ_ROTLL'
}
_lambert='EPSG:9802'
_polars='EPSG:9810'
_mercator='EPSG:9805'
knownProjectionsProj4={
'PROJ_LATLONG':None,
'PROJ_LC':'+proj=lcc +lat_1=%(truelat1)f +lat_2=%(truelat2)f +lat_0=%(truelat1)f +lon_0=%(std_lon)f +R=6370000',
'PROJ_PS':None, #need to check encoding... '+proj=stere +lat_ts=%(truelat1)f +lat_0=%(truelat1)f +lon_0=%(std_lon)f -k_0=1.0 +x_0=0. +y_0=0.'
'PROJ_MERC':None #proj.4 doesn't support lat of nat. origin/wrf only gives truelat...
#'+proj=merc +lat_ts=%(truelat1)f +lon_0=%(std_lon)f +x_0=0. +y_0=0.'
}
_unproj='+proj=longlat +datum=WGS84 +nodefs'
_gdal_translate='gdal_translate'
_gdalwarp='gdalwarp'
_vrtFMT='''<VRTDataset rasterXSize="%(xsize)i" rasterYSize="%(ysize)i">
<VRTRasterBand dataType="%(dtype)s" band="1" subClass="VRTRawRasterBand">
<SourceFilename relativetoVRT="1">%(fname)s</SourceFilename>
<PixelOffset>%(dsize)i</PixelOffset>
<LineOffset>%(lsize)i</LineOffset>
<ByteOrder>%(byteorder)s</ByteOrder>
</VRTRasterBand>
</VRTDataset>
'''
def message(s):
if verbose:
print s
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
class ProjectionError(Exception):
pass
class UnknownProjection(ProjectionError):
pass
class UnsupportedProjection(ProjectionError):
pass
class NoGDALSupport(ProjectionError):
pass
def getGDALProg(prog):
if gdalPath is not None:
prog=os.path.join(gdalPath,prog)
prog=which(prog)
if prog is None:
raise NoGDALSupport("Could not find %s."%prog)
return prog
def getEPSGProjectionDef(map_proj,cen_lat,cen_lon,truelat1,truelat2,
std_lon,pole_lat,pole_lon):
proj=wrfProjectionsCode.get(map_proj,None)
if proj is None:
raise UnknownProjection
elif proj == 'PROJ_LATLONG':
return None
proj=knownProjectionsProj4.get(proj,None)
if proj is None:
raise UnsupportedProjection
d={'cen_lat':cen_lat,'cen_lon':cen_lon,'truelat1':truelat1,'truelat2':truelat2,\
'std_lon':std_lon,'pole_lat':pole_lat,'pole_lon':pole_lon}
return proj % d
def georeferenceImage(img,proj,gcps,out):
gt=getGDALProg(_gdal_translate)
args=[gt,'-of','VRT','-a_srs',proj]
for g in gcps:
args.append('-gcp')
args.append('%(ix)i' % g)
args.append('%(iy)i' % g)
args.append('%(lon)f' % g)
args.append('%(lat)f' % g)
args.append(img)
args.append(out)
message('running: %s' % (' '.join(args)))
p=sp.Popen(args,shell=False,env=gdalEnv,stdout=sp.PIPE)
p.communicate()
if p.returncode != 0:
print p.returncode
raise NoGDALSupport('Failed to execute %s' % _gdal_translate)
def warpImage(img,out):
if os.path.exists(out):
os.remove(out)
gw=getGDALProg(_gdalwarp)
args=[gw,'-of','netCDF','-t_srs',_unproj,img,out]
message('running: %s' % (' '.join(args)))
p=sp.Popen(args,shell=False,env=gdalEnv,stdout=sp.PIPE)
p.communicate()
if p.returncode != 0:
raise NoGDALSupport('Failed to execute %s' % _gdalwarp)
def createGCP(ix,iy,lon,lat):
return {'ix':ix,'iy':iy,'lon':lon,'lat':lat}
def unProjectImage(img,proj,gcps):
vrt=img+'.vrt'
tmp='tmp_'+vrt
georeferenceImage(img,proj,gcps,vrt)
warpImage(vrt,tmp)
def vrtFromArray(fname,a):
assert a.ndim == 2
a=np.ascontiguousarray(a)
bin=fname+'.bin'
b=a.astype('<f4')
b.tofile(bin)
ny,nx=a.shape
s=_vrtFMT % {'xsize':nx,'ysize':ny,'dtype':'Float32',
'fname':bin,'dsize':4,'lsize':4*nx,'byteorder':'LSB'}
open(fname,'w').write(s)
def readNC(fname):
f=Dataset(fname,'r')
bds={'west':f.variables['lon'][0],
'east':f.variables['lon'][-1],
'south':f.variables['lat'][0],
'north':f.variables['lat'][-1]}
return np.flipud(f.variables['Band1'][:]),bds
# find gdal programs or raise error on import
getGDALProg(_gdal_translate)
getGDALProg(_gdalwarp)
if __name__ == '__main__':
from netCDF4 import Dataset
import sys
(nc,img)=sys.argv[1:]
f=Dataset(nc,'r')
srx,sry=10,10
arr=f.variables['NFUEL_CAT'][-1:,:-sry,:-srx].squeeze()
lat=f.variables['FXLAT'][0,:-sry,:-srx]
lon=f.variables['FXLONG'][0,:-sry,:-srx]
ny,nx=arr.shape
ny=ny-1
nx=nx-1
gcp=[createGCP(0,0,lon[0,0],lat[0,0]),
createGCP(0,ny,lon[ny,0],lat[ny,0]),
createGCP(nx,0,lon[0,nx],lat[0,nx]),
createGCP(nx,ny,lon[ny,nx],lat[ny,nx])]
proj=getEPSGProjectionDef(f.MAP_PROJ,f.CEN_LAT,f.CEN_LON,
f.TRUELAT1,f.TRUELAT2,f.STAND_LON,
f.POLE_LAT,f.POLE_LON)
vrtFromArray('test.vrt',np.flipud(arr))
georeferenceImage('test.vrt',proj,gcp,'tmp_test.vrt')
warpImage('tmp_test.vrt','warp_test.nc')
a=readNC('warp_test.nc')