-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathISCE2StaMPS_network_draw.py
253 lines (195 loc) · 10.8 KB
/
ISCE2StaMPS_network_draw.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
#!/usr/bin/env python3
'''#!/usr/bin/env python'''
'''This is a script used to draw InSAR network in temp and perp bsln relative to 1st scene.
Parameters:
Add something here
Returns:
'''
import numpy as np
import os, sys
import datetime
import argparse
from datetime import date
import matplotlib.pyplot as plt
from os.path import abspath as abspath
from os.path import join as join
from os.path import dirname as dirname
from os.path import abspath as abspath
appdir = dirname(dirname(abspath(__file__)))
sys.path.insert(0, appdir)
#from modules.geotools import DecimalYearComp as decyrcomp
'''import subprocess
import glob
from modules.basictools import LoggingGen'''
def cmdLineParse():
'''
Command Line Parser.
'''
parser = argparse.ArgumentParser(description='Generate InSAR network to choose master date.')
parser.add_argument('-fs', '--fontsize', type=float, required=False, help='Font size of text of image in the map', dest='ftsz')
parser.add_argument('-ms', '--markersize', type=float, required=False, help='Marker size of scatters of images in the map', dest='mksz')
parser.add_argument('-ma', '--masterdate', type=str, required=False, help='Master date in string format as YYYYMMDD', dest='msdt')
parser.add_argument('-o', '--outfile', type=str, required=False, help='Name of output file', dest='ofle')
inputs = parser.parse_args()
#if (not inputs.input):
# print('Error!!! No input list is provided.')
# sys.exit(0)
return inputs
def decyrcomp(year, month, day): # This tool is used to calc. decimal year from year, month, date inputed
fn_noofdays = (date(year, month, day) - date(year, 1, 1)).days
fn_daysofyear = (date(year + 1, month, day) - date(year, month, day)).days
fn_decimal_year = year + float(fn_noofdays)/float(fn_daysofyear) # This "float" fn. is used so that
# the result of fn_noofdays/fn_daysofyear will be in
# double (float) format instead of integer
return fn_decimal_year
def PSIFGnetGen(tims, rbperp, imgname=None, masteridx=None, plttit=None, xunit=None, yunit=None, imshow=True, fname=None, figdpi = 1000, ftsz=None, mksz=None):
plt.clf()
if masteridx != None:
for imgidx in range(len(tims)):
if imgidx != msid:
plt.plot([tims[imgidx], tims[masteridx]], [rbperp[imgidx], rbperp[masteridx]], '-', color = 'gray')
'''if mksz != None:
plt.scatter(tims, rbperp, marker = 'o', c = 'red', s = mksz)
else:
plt.scatter(tims, rbperp, marker = 'o', c = 'red')'''
plt.plot(tims, rbperp, 'ro')
if imgname != None:
if masteridx == None:
for idx in range(len(imgname)):
if ftsz != None:
plt.text(tims[idx], rbperp[idx], imgname[idx], horizontalalignment='center', verticalalignment='bottom', fontsize=ftsz)
else:
plt.text(tims[idx], rbperp[idx], imgname[idx], horizontalalignment='center', verticalalignment='bottom')
if ftsz != None:
plt.text(tims[masteridx], rbperp[masteridx], imgname[masteridx], horizontalalignment='center', verticalalignment='bottom', fontsize=ftsz)
'''else:
plt.text(tims[masteridx], rbperp[masteridx], imgname[masteridx], horizontalalignment='center', verticalalignment='bottom', color='blue')'''
trange = max(tims) - min(tims)
rrange = max(rbperp) - min(rbperp)
plt.axis([min(tims) - trange/10, max(tims) + trange/10, min(rbperp) - rrange/10, max(rbperp) + rrange/10])
if plttit == None:
plt.title('InSAR network')
else:
plt.title(plttit)
if xunit == None:
plt.xlabel('Acquisition Time [year]', fontsize = 14, color = 'k')
#plt.xlabel('Time [year]')
#plt.xlabel('Acquisition Time [year]', fontsize = 14, color = 'r')
else:
plt.xlabel('Acquisition Time [' + xunit + ']', fontsize = 14, color = 'k')
#plt.xlabel('Time [' + xunit + ']', fontsize = 14, color = 'r')
#plt.xlabel('Acquisition Time [' + xunit + ']', fontsize = 14, color = 'r')
if yunit == None:
plt.ylabel('Perpendicular Baseline [m]', fontsize = 14, color = 'k')
#plt.ylabel('Perpendicular Baseline [m]', fontsize = 14, color = 'b')
else:
plt.ylabel('Perpendicular Baseline [' + yunit + ']', fontsize = 14, color = 'k')
#plt.ylabel('Perpendicular Baseline [' + yunit + ']', fontsize = 14, color = 'b')
plt.grid()
plt.draw()
#IFGnetplotfile1 = 'step_2_IFG_Net_1.png'
'''if verbose == 'True':
print('\r\n\tMake an IFG network plot 1:')
print('\t\tIFG network plot 1 will be save to file: ' + repr(IFGnetplotfile1))'''
'''logger.info('\tMake an IFG network plot 1:')
logger.info('\t\tIFG network plot 1 will be save to file: ' + repr(IFGnetplotfile1))'''
if fname !=None:
#plt.savefig(IFGnetplotfile1, dpi = 100)
plt.savefig(fname, dpi = figdpi)
if imshow == True:
plt.show()
def tempbperpread(ifile):
mimg = (os.path.split(ifile)[1]).split('_')[0] # master image in yyyymmdd format
simg = ((os.path.split(ifile)[1]).split('_')[1]).split('.')[0] # slave image in yyyymmdd format
mdate = date(int(mimg[:4]), int(mimg[4:6]), int(mimg[6:8])) # master date in datetime format
sdate = date(int(simg[:4]), int(simg[4:6]), int(simg[6:8])) # slave date in datetime format
rfile = open(ifile, "r")
cnt = rfile.readlines()
rfile.close()
bperp = []
bpar = []
for line in cnt:
if line.split()[0] == 'Bperp':
bperp.append(float(line.split()[-1]))
if line.split()[0] == 'Bpar':
bpar.append(float(line.split()[-1]))
return mimg, simg, mdate, sdate, bperp, bpar
if __name__ == '__main__':
sttm = datetime.datetime.now() # Luyen cmt: start time running for all prog.
print
print ('##########################################################################################################################')
print ('# #')
print ('# This is the app. used to run draw InSAR network in temp and perp bsln relative to 1st scene #')
print ('# (Script used: ISCE2StaMPS_bsln_draw.py) #')
print ('# #')
print ('# This is carried out after running steps by ISCE2StaMPS_run_steps #')
print ('# #')
print ('##########################################################################################################################')
'''#######################
##### CREATE A LOG FILE
logfile = 'ISCE2StaMPS_run_steps_log.txt'
if os.path.isfile(logfile):
print ('\r\nThe logging file: ' + logfile + ' is already existent. Please delete or change its name.\r\n')
sys.exit()
#logger = LoggingGen(logfile)'''
'''logger.info ('##########################################################################################################################')
logger.info ('# #')
logger.info ('# This is the app. used to run steps listed in directory "run_files" in order to adapt ISCE to StaMPS #')
logger.info ('# (Script used: ISCE2StaMPS_run_steps.py) #')
logger.info ('# #')
logger.info ('##########################################################################################################################')'''
inputs = cmdLineParse()
ftsz = inputs.ftsz
mksz = inputs.mksz
msdt = inputs.msdt
if inputs.ofle != None:
imgfile = inputs.ofle
else:
imgfile = 'IFGNet.png'
crdir = os.getcwd() # Current dir
bsdir = 'baselines' # The dir of which "run_files" dir is included
print ('\r\nTemporal and perpendicular baselines will be identified from subdirectories located in: ' + repr(join(crdir, bsdir)))
#logger.info ('Steps being run is listed in the directory ' + repr(rfdir))
subdirlst = [item for item in os.listdir(join(crdir, bsdir)) if os.path.isdir(join(crdir, bsdir, item))] # List of subdirs included in 'baselines' directory
imgs = []
rbtemp = []
rbperp = []
for count, idir in enumerate(subdirlst):
print ('\r\nSubdirectory #: ' + str(count + 1).zfill(len(str(len(subdirlst)))) + ' / ' + str(len(subdirlst)) + ': ' + abspath(join(crdir, bsdir, idir)))
print ('\tIdentify temporal and perpendicular baselines from file: ' + abspath(join(crdir, bsdir, idir, idir + '.txt')))
mimg, simg, mdate, sdate, bperp, _ = tempbperpread(abspath(join(crdir, bsdir, idir, idir + '.txt')))
print ('\r\n\tMaster image in yyyymmdd format: ', mimg)
print ( '\tSlave image in yyyymmdd format: ', simg)
print ('\r\n\tMaster image in datetime format: ', mdate)
print ( '\tSlave image in datetime format: ', sdate)
if count == 0:
imgs.append(mimg)
rbtemp.append(0)
rbperp.append(0)
imgs.append(simg)
rbperp.append(np.mean(bperp))
rbtemp.append((sdate - mdate).days)
print ('\r\n\tMaster-Slave temp baseline [days]: ', rbtemp[-1])
print ( '\tMaster-Slave perp baseline [m]: ', rbperp[-1])
if msdt != None and msdt in imgs:
msid = imgs.index(msdt)
rbperp = [elm - rbperp[msid] for elm in rbperp]
rbtemp = [decyrcomp(int(elm[0:4]), int(elm[4:6]), int(elm[6:8])) for elm in imgs]
elif msdt != None and msdt not in imgs:
print ('Input master date is nonexistent. Check it !')
sys.exit()
if msdt != None:
PSIFGnetGen(rbtemp, rbperp, imgname=imgs, masteridx=msid, plttit=None, xunit='year', yunit='m', imshow=True, fname=imgfile, figdpi = 1000, ftsz = ftsz, mksz = mksz)
else:
PSIFGnetGen(rbtemp, rbperp, imgs, masteridx=None, plttit=None, xunit='day', yunit='meter', imshow=True, fname=imgfile, figdpi = 1000, ftsz = ftsz, mksz = mksz)
#IFGnetGen(tims, rbperp, imgname=None, plttit=None, xunit=None, yunit=None, imshow=True, fname=None, figdpi = 1000):
print ('\r\nInSAR network has been generated and saved in: ' + abspath(imgfile))
fntm = datetime.datetime.now() # Luyen cmt: finish time running for all prog.
print ('\r\n==========================================================================================================================')
print ('--------------------------------------------------------------------------------------------------------------------------')
print ('Prog. started at : ' + str(sttm))
print ('Prog. finished at : ' + str(fntm))
print ('total running time: ' + str(fntm - sttm))
print ('Program finished !')
print ('--------------------------------------------------------------------------------------------------------------------------')
print ('==========================================================================================================================')