-
Notifications
You must be signed in to change notification settings - Fork 0
/
SARImage.py
359 lines (309 loc) · 10.5 KB
/
SARImage.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
# Generate line-of-sight interferograms
#
# input: disclocOutput
# output: image
#
# usage:
# python SARImage.py (testing with default data set and parameters)
# python SARIMage.py dislocOutput imageURL
# python SARImage.py dislocOutput elevation(degree) azimuth(degree) radarFrequency(in GHz) imageURL
#
# output:
# [dislocOutput].png
# [dislocOutput].kml
"""
#=====================================================
# History:
# 2010/09/07: fix nan problem
# 2010/09/20: fix white stripes
# 2011/05/09: add QuakeSim logo
# 2011/05/09: record parameters in kml files
# 2011/05/10: temporary fix for QuakeSim logo on small image
#=====================================================
import csv, math, sys, os, math, string, zipfile
try:
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
#from matplotlib.backends.backend_agg import FigureCanvasAgg
import matplotlib.image as mpimg
except ImportError:
sys.exit("Import matplotlib failed ")
def dxy2lonlat(xy, ref):
"""
convert dx, dy to lon, lat
parameters: dx, dy, reflonlat
"""
flattening = 1.0/298.247
yfactor = 111.32
lon1,lat1 = ref
#xfactor = equard*Pi/180*Cos[lat1 Degree]*(1.0 - (flattening*Sin[lat1 Degree])^2)
#eqrad = 6378.139
#equard*Pi/180 = 111.32
xfactor = 111.32*math.cos(math.radians(lat1))*(1.0 - flattening*(math.sin(math.radians(lat1))**2))
lon2 = xy[0]/xfactor + lon1
lat2 = xy[1]/yfactor + lat1
return [lon2,lat2]
def generateKML(extent, outputname, imageurl, params):
"""
generate KML
"""
kml = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Folder>
<name>Disloc Interferograms</name>
<description>%s</description>
<GroundOverlay>
<name>Synthetic Interferograms</name>
<Icon>
<href>%s</href>
</Icon>
<altitude>0</altitude>
<altitudeMode>relativeToGround</altitudeMode>
<LatLonBox>
<north>%s</north>
<south>%s</south>
<east>%s</east>
<west>%s</west>
</LatLonBox>
</GroundOverlay>
<ScreenOverlay>
<name>UAVSAR Legend</name>
<Icon>
<href>2pi.png</href>
</Icon>
<overlayXY x="0" y="0" xunits="fraction" yunits="fraction"/>
<screenXY x="0" y="40" xunits="pixels" yunits="pixels"/>
<rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
<size x="0" y="0" xunits="fraction" yunits="fraction"/>
</ScreenOverlay>
</Folder>
</kml>
"""
west = str(extent[0])
east = str(extent[1])
south = str(extent[2])
north = str(extent[3])
href = imageurl + "/" + outputname + ".png"
if imageurl == "":
href = outputname + ".png"
dislocname = os.path.basename(str(params[0]))
description = "<![CDATA[%s]]>"
desc = '<br>'.join(["Disloc :", dislocname,
"Elevation (degree): ", str(params[1]),
"Azimuth (degree): ", str(params[2]),
"Radar Wavelength (cm): ", "%.3f" % params[3]])
#print kml % (north,south,east,west)
description = description % (desc)
kml = kml % (description, href, north,south,east,west)
kmlf = open(outputname + ".kml",'w')
kmlf.write(kml)
kmlf.close()
# generate kmz
kmz = outputname + ".kmz"
legendfile = os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "2pi.png"
with zipfile.ZipFile(kmz,'w',zipfile.ZIP_DEFLATED) as myzip:
myzip.write(outputname + ".kml")
myzip.write(href)
myzip.write(legendfile,"2pi.png")
myzip.close
def color_wheel(fcw):
""" make goldstein color wheel """
#fcw=1 flips order of colors
wheel = [[None]*16 for i in range(3)]
for i in range(16):
if i==0:
ib=255
ir=ib
ig=ib
elif i>=1 and i<=5:
ib = 255
ir = (i-1)*51
ig = 255 - 51*(i-1)
elif i>=6 and i<=10:
ir = 255
ig = (i-6)*51
ib = 255 - (i-6)*51
elif i>=11 and i<=15:
ig = 255
ib = (i-11)*51
ir = 255 -(i-11)*51
else:
print("something wrong!")
wheel[0][i]=math.floor(ir/255.0*225)+30
wheel[1][i]=math.floor(ig/255.0*225)+30
wheel[2][i]=math.floor(ib/255.0*225)+30
if fcw == 1:
#flip the color wheel
for i in range(3):
junk = wheel[i][1:]
junk.reverse()
wheel[i][1:]=junk
colormatrx = []
for j in range(256):
mag = math.floor(j/16)
i = j % 16
red = math.floor(mag/16.0*wheel[0][i]*16/15)
green = math.floor(mag/16.0*wheel[1][i]*16/15)
blue = math.floor(mag/16.0*wheel[2][i]*16/15)
colormatrx.append([red/255.0,green/255.0,blue/255.0])
return colormatrx
def drawimage(datatable,lonlatgrid, outputname, imageurl, params,colortable=True):
"""
produece image
"""
data=[]
lat_xy = []
lon_xy = []
for row in datatable:
lon_xy.append(row[0])
lat_xy.append(row[1])
data.append(row[2])
# get the right extent box
xy0= [min(lon_xy),min(lat_xy)]
xy1= [max(lon_xy),max(lat_xy)]
#print(xy0)
#print(xy1)
## z = np.array(data)
## z = z.reshape(lonlatgrid[1],lonlatgrid[0])
##
## fig = plt.figure()
## fig.subplots_adjust(left=0.0,bottom=0.0,top=1.0,right=1.0)
##
## im = plt.imshow(z,cmap=cm.jet,
## origin='lower', alpha=0.875,aspect="auto",interpolation=None, extent=[xy0[0],xy1[0],xy0[1],xy1[1]])
##
## plt.axis("off")
## plt.savefig(outputname + ".png", format="PNG",transparent=True)
## #print xy0, xy1
## generateKML([xy0[0],xy1[0],xy0[1],xy1[1]],outputname, imageurl)
# new color wheel
z = np.array(data)
# drawing with negative fringe
z = -1 *z
minz, maxz = min(z), max(z)
# disloc discrete color wheel
if not colortable:
#scale it to [1~15]
z = np.fix((z-minz)/(maxz-minz)*14+ 0.001) + 1
z = z.astype("int")
#handle nan number nan is colored by colm[0]
nan = np.where(z<0)
z[nan]=0
colormatrx = color_wheel(0)
# the brightness of colorwheel
p=14
colm = np.array(colormatrx[(p - 1)*16:p*16])
newimg = colm[z]
# uavsar color table
if colortable:
#load color table
colortable_csv = os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "colortable.csv"
with open(colortable_csv,"r") as csvfile:
rawtable = csv.reader(csvfile)
colortable=[]
numinx=[]
for row in rawtable:
rowfloat=list(map(float,row))
colortable.append(rowfloat[1:4])
numinx.append(int(rowfloat[0]))
csvfile.close
z = np.fix((z-minz)/(maxz-minz)*200) - 100
#print min(z),max(z)
newimg = []
for item in z:
inx=int((item))
# tweak for 175
#if inx > 70:
# inx = -inx
#if inx < - 74:
# inx = inx - 2* (inx + 87)
pos=numinx.index(inx)
newimg.append(colortable[pos])
newimg = np.array(newimg)
# reshape array as image
newimg = newimg.reshape(lonlatgrid[1],lonlatgrid[0],3)
# figsize
fig = plt.figure(figsize=(lonlatgrid[0]/12.0,lonlatgrid[1]/12.0))
fig.subplots_adjust(left=0.0,bottom=0.0,top=1.0,right=1.0)
fig.patch.set_alpha(0.85)
im = plt.imshow(newimg,interpolation="bilinear",origin='lower',alpha=0.9)
plt.axis("off")
# add QuakeSim logo
#if (lonlatgrid[1] < 100) or (lonlatgrid[0] < 100):
# logo = mpimg.imread('QuakeSimLogoGrayEmbossSmall.png')
# fig.figimage(logo, xo=fig.bbox.xmax, yo=2,zorder=1)
#else:
# logo = mpimg.imread('QuakeSimLogoGrayEmboss.png')
# fig.figimage(logo, xo=fig.bbox.xmax, yo=2,zorder=1)
plt.savefig(outputname + ".png", format="PNG",transparent=True,dpi=(96))
generateKML([xy0[0],xy1[0],xy0[1],xy1[1]],outputname, imageurl, params)
# close fig to release memory
plt.close(fig)
# return extent
return [xy0[0],xy1[0],xy0[1],xy1[1]]
def lineofsight (ele,azi,radarWL,disO,url):
"""
caculate line of sight
parameters:elevation,azimuth,radarWaveLength,disclocOutput
"""
params = [disO, ele, azi,radarWL]
outputReader = csv.reader(open(disO), delimiter = ' ')
rawdata=[]
header = 0
for row in outputReader:
# remove ''
line = [i for i in row if i !='']
if not ('x' in line):
rawdata.append(list(map(float,line)))
else:
# locate the position of x y ux uy uz exx exy eyy
header = len(rawdata)
del outputReader
# 30 30 32.237000 -115.083000
gridsize = list(map(int,rawdata[0][:2]))
reflonlat = [rawdata[0][3], rawdata[0][2]]
data = rawdata[header:]
#print reflonlat
#print len(data)
#print gridsize
# unit vector
# g = {Sin[Azimuth] Cos[Elevation], Cos[Azimuth] Cos[Elevation], Sin[Elevation]}
azimuth, elevation = math.radians(azi),math.radians(ele)
g = [math.sin(azimuth)*math.cos(elevation),math.cos(azimuth)*math.cos(elevation),math.sin(elevation)]
#print g
datatable = []
for entry in data:
# conver x,y to lon,lat
lonlat = dxy2lonlat(entry[:2],reflonlat)
ux,uy,uv = entry[2:5] # Disloc displacement values are typically in mm
# line of sight displacement
losd = (g[0]*ux + g[1]*uy + g[2]*uv)/5.0 # Convert from mm to cm to be consistent with radarWL
# fringe
# fringe = abs(math.modf(2*losd / radarWL)[0])
#fringe = 2*losd / radarWL - math.floor(2*losd / radarWL)
# mapping is changed to ~2pi to 2pi to match UAVSAR
# shall be -12 ~ 12
fringe = 2*losd / radarWL - math.floor(2*losd / radarWL)
#print fringe
datatable.append([lonlat[0],lonlat[1],fringe])
# output for test
#writer = csv.writer(open(disO+"_table", "wb"))
#writer.writerows(datatable)
#del writer
outputname = os.path.basename(disO) + ".insar"
imageextent = drawimage(datatable,gridsize, outputname, url, params,colortable = True)
return imageextent
def main():
#test case 1
elevation = 60
azimuth = -5
radarFrequency = 1.26*10**9
radarWaveLength = 299792458.0/radarFrequency * 100.0 # Convert to cm
disclocOutput = "test/4fault.csv"
imageURL = ""
lineofsight(elevation, azimuth,radarWaveLength,disclocOutput, imageURL)
if __name__ == "__main__":
main()