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
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
# Author: Cunren Liang
# Copyright 2021
import os
import copy
import glob
import shutil
import argparse
import numpy as np
import isce
import isceobj
import s1a_isce_utils as ut
from isceobj.Sensor.TOPS import createTOPSSwathSLCProduct
from isceobj.TopsProc.runIon import renameFile
def createParser():
parser = argparse.ArgumentParser(description='check overlap among all acquisitons')
parser.add_argument('-r', '--reference', dest='reference', type=str, required=True,
help='directory with reference acquistion')
parser.add_argument('-s', '--secondarys', dest='secondarys', type=str, required=True,
help='directory with secondarys acquistions')
parser.add_argument('-g', '--geom_reference', dest='geom_reference', type=str, default=None,
help='directory with geometry of reference')
return parser
def cmdLineParse(iargs = None):
parser = createParser()
return parser.parse_args(args=iargs)
def main(iargs=None):
'''
check overlap among all acquistions, only keep the bursts that in the common overlap,
and then renumber the bursts.
'''
inps = cmdLineParse(iargs)
referenceDir = inps.reference
secondaryDir = sorted(glob.glob(os.path.join(inps.secondarys, '*')))
acquistionDir = [referenceDir] + secondaryDir
invalidSwath = []
for i in [1, 2, 3]:
for x in acquistionDir:
if not (os.path.isdir(os.path.join(x, 'IW{}'.format(i))) and os.path.isfile(os.path.join(x, 'IW{}.xml'.format(i)))):
invalidSwath.append(i)
break
if invalidSwath == [1, 2, 3]:
raise Exception('there are no common swaths among the acquistions')
else:
validSwath = [i for i in [1, 2, 3] if i not in invalidSwath]
print('valid swath from scanning acquistion directory: {}'.format(validSwath))
invalidSwath2 = []
for swath in validSwath:
referenceSwath = ut.loadProduct(os.path.join(referenceDir, 'IW{0}.xml'.format(swath)))
burstoffsetAll = []
minBurstAll = []
maxBurstAll = []
secondarySwathAll = []
for secondaryDirX in secondaryDir:
secondarySwath = ut.loadProduct(os.path.join(secondaryDirX, 'IW{0}.xml'.format(swath)))
secondarySwathAll.append(secondarySwath)
burstoffset, minBurst, maxBurst = referenceSwath.getCommonBurstLimits(secondarySwath)
burstoffsetAll.append(burstoffset)
minBurstAll.append(minBurst)
maxBurstAll.append(maxBurst)
minBurst = max(minBurstAll)
maxBurst = min(maxBurstAll)
numBurst = maxBurst - minBurst
if minBurst >= maxBurst:
invalidSwath2.append(swath)
else:
#add reference
swathAll = [referenceSwath] + secondarySwathAll
burstoffsetAll = [0] + burstoffsetAll
for dirx, swathx, burstoffsetx in zip(acquistionDir, swathAll, burstoffsetAll):
swathTmp = createTOPSSwathSLCProduct()
swathTmp.configure()
#change reserved burst properties and remove non-overlap bursts
for jj in range(len(swathx.bursts)):
ii = jj - burstoffsetx
#burstFileName = os.path.join(os.path.abspath(dirx), 'IW{}'.format(swath), os.path.basename(swathx.bursts[jj].image.filename))
burstFileName = os.path.join(os.path.abspath(dirx), 'IW{}'.format(swath), 'burst_%02d'%(jj+1) + '.slc')
if minBurst <= ii < maxBurst:
kk = ii - minBurst
#change burst properties
swathx.bursts[jj].burstNumber = kk + 1
swathx.bursts[jj].image.filename = os.path.join(os.path.dirname(swathx.bursts[jj].image.filename), 'burst_%02d'%(kk+1) + '.slc')
swathTmp.bursts.append(swathx.bursts[jj])
else:
#remove non-overlap bursts
#os.remove(burstFileName)
os.remove(burstFileName+'.vrt')
os.remove(burstFileName+'.xml')
#remove geometry files accordingly if provided
if dirx == referenceDir:
if inps.geom_reference is not None:
for fileType in ['hgt', 'incLocal', 'lat', 'lon', 'los', 'shadowMask']:
geomFileName = os.path.join(os.path.abspath(inps.geom_reference), 'IW{}'.format(swath), fileType + '_%02d'%(jj+1) + '.rdr')
os.remove(geomFileName)
os.remove(geomFileName+'.vrt')
os.remove(geomFileName+'.xml')
#change reserved burst file names
for jj in range(len(swathx.bursts)):
ii = jj - burstoffsetx
#burstFileName = os.path.join(os.path.abspath(dirx), 'IW{}'.format(swath), os.path.basename(swathx.bursts[jj].image.filename))
burstFileName = os.path.join(os.path.abspath(dirx), 'IW{}'.format(swath), 'burst_%02d'%(jj+1) + '.slc')
if minBurst <= ii < maxBurst:
kk = ii - minBurst
burstFileNameNew = os.path.join(os.path.abspath(dirx), 'IW{}'.format(swath), 'burst_%02d'%(kk+1) + '.slc')
if burstFileName != burstFileNameNew:
img = isceobj.createImage()
img.load(burstFileName + '.xml')
img.setFilename(burstFileNameNew)
#img.extraFilename = burstFileNameNew+'.vrt'
img.renderHdr()
#still use original vrt
os.remove(burstFileName+'.xml')
os.remove(burstFileNameNew+'.vrt')
os.rename(burstFileName+'.vrt', burstFileNameNew+'.vrt')
#change geometry file names accordingly if provided
if dirx == referenceDir:
if inps.geom_reference is not None:
for fileType in ['hgt', 'incLocal', 'lat', 'lon', 'los', 'shadowMask']:
geomFileName = os.path.join(os.path.abspath(inps.geom_reference), 'IW{}'.format(swath), fileType + '_%02d'%(jj+1) + '.rdr')
geomFileNameNew = os.path.join(os.path.abspath(inps.geom_reference), 'IW{}'.format(swath), fileType + '_%02d'%(kk+1) + '.rdr')
if geomFileName != geomFileNameNew:
renameFile(geomFileName, geomFileNameNew)
#change swath properties
swathx.bursts = swathTmp.bursts
swathx.numberOfBursts = numBurst
#remove original and write new
os.remove( os.path.join(dirx, 'IW{}.xml'.format(swath)) )
ut.saveProduct(swathx, os.path.join(dirx, 'IW{}.xml'.format(swath)))
#remove invalid swaths
invalidSwath3 = list(sorted(set(invalidSwath+invalidSwath2)))
for swath in invalidSwath3:
for dirx in acquistionDir:
iwdir = os.path.join(dirx, 'IW{}'.format(swath))
iwxml = os.path.join(dirx, 'IW{}.xml'.format(swath))
if os.path.isdir(iwdir):
shutil.rmtree(iwdir)
if os.path.isfile(iwxml):
os.remove(iwxml)
if __name__ == '__main__':
'''
Main driver.
'''
# Main Driver
main()