-
Notifications
You must be signed in to change notification settings - Fork 3
/
procSentinelRTC-devel.py
executable file
·393 lines (339 loc) · 12.6 KB
/
procSentinelRTC-devel.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/python
import sys,os,re
import numpy as np
import datetime
import time
import glob
import csv
# You will need to update these paths to your own if you want to use this
# script
global baseSNAP
baseSNAP = '/home/sarko/snap/bin/gpt -q 16 -c 24576M '
global extDEM
extDEM=''
noOrb = False
cleanTemp = False
procRTC=False
subset = False
if len(sys.argv) == 1:
print '******************************'
print ' Usage: procSentinelTC.py <input file> [-t tempdir] [-dortc] [-noorbit] [-dem demfile] [-subset ullon ullat lrlon lrlat]'
print ' Note: All files and tempdir must be absolutely pathed'
print ' Input file can either be an S-1 zip file or an asf-datapool*.csv file'
print '******************************'
infiles = []
tdir = './'
if len(sys.argv)>2:
for i in range(1,len(sys.argv)):
if sys.argv[i] == '-t':
tdir = sys.argv[i+1]
i+=1
elif sys.argv[i] == '-dem':
extDEM = sys.argv[i+1]
i+=1
elif sys.argv[i]=='-noorbit':
noOrb = True
elif sys.argv[i]=='-dortc':
procRTC = True
elif sys.argv[i] == '-subset':
ullon = float(sys.argv[i+1])
ullat = float(sys.argv[i+2])
lrlon = float(sys.argv[i+3])
lrlat = float(sys.argv[i+4])
subset = True
i = i+4
else:
infiles.append(sys.argv[i])
i+=1
# Create temp and geotiff directory if they do not exist
if tdir == './':
tdir = './temp'
if not os.path.exists(tdir):
os.system('mkdir %s' % tdir)
if not os.path.exists('./geotiffs'):
os.system('mkdir ./geotiffs')
def timestamp(date):
return time.mktime(date.timetuple())
def tempDir(tdir,ifile):
td2 = '%s/%s' % (tdir,ifile)
if not os.path.exists(tdir):
os.system('mkdir %s' % tdir)
if not os.path.exists(td2):
os.system('mkdir %s' % td2)
return td2
def applyCal(inData,td2):
calFlag = 'Calibration -PoutputBetaBand=true -PoutputSigmaBand=false '
out = '-t %s/%s ' % (td2,inData.replace('.dim','_CAL'))
inD = '-Ssource=%s/%s' % (td2,inData)
cmd = baseSNAP + calFlag + out + inD
print 'Applying Calibration'
os.system(cmd)
return '%s' % inData.replace('.dim','_CAL.dim')
def removeThermalNoise(inData,td2):
calFlag = 'ThermalNoiseRemoval '
out = '-t %s/%s ' % (td2,inData.replace('.dim','_NR'))
inD = '-SsourceProduct=%s/%s' % (td2,inData)
cmd = baseSNAP + calFlag + out + inD
print 'Applying Thermal Noise Removal'
os.system(cmd)
return '%s' % inData.replace('.dim','_NR.dim')
def topsarDeburst(inData,td2):
calFlag = 'TOPSAR-Deburst '
out = '-t %s/%s ' % (td2,inData.replace('.dim','_DB'))
inD = '-Ssource=%s/%s' % (td2,inData)
cmd = baseSNAP + calFlag + out + inD
print 'Debursting TOPSAR Data'
print cmd
os.system(cmd)
return '%s' % inData.replace('.dim','_DB.dim')
def topsarDeburst1(granule,td2,baseGran):
calFlag = 'TOPSAR-Deburst '
#out = '-t %s/%s ' % (td2,inData.replace('.dim','_DB'))
out = '-t %s/%s ' % (td2,baseGran+'_DB')
inD = '-Ssource=%s' % granule
cmd = baseSNAP + calFlag + out + inD
print 'Debursting TOPSAR Data'
print cmd
os.system(cmd)
return '%s' % baseGran+'_DB.dim'
#return '%s' % inData.replace('.dim','_DB.dim')
def applySpeckleFilter(inData,td2):
calFlag = 'Speckle-Filter '
out = '-t %s/%s ' % (td2,inData.replace('.dim','_SF'))
inD = '-Ssource=%s/%s -PwindowSize=\'5x5\'' % (td2,inData)
cmd = baseSNAP + calFlag + out + inD
print 'Applying Speckle Filter'
os.system(cmd)
return '%s' % inData.replace('.dim','_SF.dim')
def applyML(inData,td2):
calFlag = 'Multilook -PnRgLooks=4 -PnAzLooks=1 '
out = '-t %s/%s ' % (td2,inData.replace('.dim','_ML'))
inD = '-Ssource=%s/%s' % (td2,inData)
cmd = baseSNAP + calFlag + out + inD
print 'Applying Multilook'
print cmd
os.system(cmd)
return '%s' % inData.replace('.dim','_ML.dim')
def applyOrbit(granule,td2,baseGran):
aoFlag = 'Apply-Orbit-File '
#oType = '-PcontinueOnFail=\"true\" -PorbitType=\'Sentinel Restituted (Auto Download)\' '
oType = '-PcontinueOnFail=\"true\" -PorbitType=\'Sentinel Precise (Auto Download)\' -PpolyDegree=3 '
out = '-t %s/%s ' % (td2,baseGran+'_OB')
cmd = baseSNAP + aoFlag + out + oType + granule
print 'Applying Precise Orbit file'
print cmd
os.system(cmd)
return '%s' % baseGran+'_OB.dim'
def applyOrbit1(inData,td2):
aoFlag = 'Apply-Orbit-File '
#oType = '-PcontinueOnFail=\"true\" -PorbitType=\'Sentinel Restituted (Auto Download)\' '
oType = '-PcontinueOnFail=\"true\" -PorbitType=\'Sentinel Precise (Auto Download)\' '
#out = '-t %s/%s ' % (td2,baseGran+'_OB')
out = '-t %s/%s ' % (td2,inData.replace('.dim','_OB'))
inD = '-Ssource=%s/%s' % (td2,inData)
cmd = baseSNAP + aoFlag + out + oType + inD
print 'Applying Precise Orbit file'
print cmd
os.system(cmd)
return '%s' % inData.replace('.dim','_OB.dim')
#return '%s' % baseGran+'_OB.dim'
def applyTF(inData,td2,subsetFlag=0):
calFlag = 'Terrain-Flattening '
out = '-t %s/%s ' % (td2,inData.replace('.dim','_TF'))
inD = '-Ssource=%s/%s' % (td2,inData)
if extDEM != '':
inD = inD + ' -PdemName=\"External DEM\" -PexternalDEMFile=%s -PexternalDEMNoDataValue=0 -PreGridMethod=false ' % extDEM
else:
#inD = inD + ' -PdemName=\"SRTM 3Sec\" '
inD = inD + ' -PdemName=\"SRTM 1Sec HGT\" -PreGridMethod=false '
cmd = baseSNAP + calFlag + out + inD
print 'Applying Terrain Flattening -- This will take some time'
print cmd
os.system(cmd)
if subsetFlag==0:
return '%s' % inData.replace('.dim','_TF.dim')
else:
return '%s' % inData.replace('.dim','_TF.dim')
def applySubset(inData,subpoly,td2):
calFlag = 'Subset -PgeoRegion=\"%s\" -PcopyMetadata=true ' % subpoly
out = '-t %s/%s ' % (td2,inData.replace('.dim','_SU'))
inD = '-Ssource=%s/%s' % (td2,inData)
cmd = baseSNAP + calFlag + out + inD
print 'Subsetting'
print cmd
os.system(cmd)
return '%s' % inData.replace('.dim','_SU.dim')
def applyTC(inData,td2):
calFlag = 'Terrain-Correction -PalignToStandardGrid=true '
out = '-t %s/%s ' % (td2,inData.replace('.dim','_TC'))
#out = '-t %s/%s -f HDF5 ' % (td2,inData.replace('.dim','_TC'))
inD = '-Ssource=%s/%s' % (td2,inData)
if extDEM != '':
inD = inD + ' -PdemName=\"External DEM\" -PexternalDEMFile=%s -PexternalDEMNoDataValue=0 ' % extDEM
else:
#inD = inD + ' -PdemName=\"SRTM 3Sec\" '
#inD = inD + ' -PdemName=\"SRTM 1Sec HGT\" -PimgResamplingMethod=\"NEAREST_NEIGHBOUR\" '
inD = inD + ' -PdemName=\"SRTM 1Sec HGT\" '
cmd = baseSNAP + calFlag + out + inD
print 'Applying Terrain Correction -- This will take some time'
print cmd
os.system(cmd)
return '%s' % inData.replace('.dim','_TC.dim')
def parseCSV(csvfile):
data = open(csvfile,'r').readlines()[1:]
infiles = []
t = list(csv.reader(data))
print len(t)
for line in t:
for item in line:
if 'datapool' in item:
infiles.append(item)
return infiles
global infile
infile = sys.argv[1]
if '.zip' in sys.argv[1]:
#infiles = [sys.argv[1]]
pass
elif '.csv' in sys.argv[1]:
infiles = parseCSV(sys.argv[1])
else:
sys.exit('Did not understand input file')
for infile in infiles:
fin = infile
infile = re.split('/',infile)[-1]
print 'Infile is: %s' % infile
# Need to figure out where input file is
baseSen = os.getcwd()
print fin
print '%s/%s' % (baseSen,infile)
if os.path.exists('%s/%s' % (baseSen,infile)):
pass
else:
cmd = 'wget -c %s' % fin
print cmd
os.system(cmd)
#if 'S1A' in infile:
#baseSen = '/hugeslice/datapool/prod/data/GRD/SA/'
#else:
#baseSen = '/hugeslice/datapool/prod/data/GRD/SB/'
#if 'SLC' in infile:
#infile = re.split('/',infile.replace('SLC_','GRDH'))[-1]
#inf = infile.split('_')
#inf[4] = '*'
#inf[5] = '*'
#inf[8] = '*'
#infile = '_'.join(inf)
#orbit = int(re.split('_',infile)[6])
#orbit = str(orbit - orbit%1000)
#inf = baseSen + orbit + '/' + infile
#infile = glob.glob(inf)[0]
#print infile
if '.zip' in infile:
#orbit = int(re.split('_',infile)[6])
#orbit = str(orbit - orbit%1000)
infile = baseSen + '/' + infile
else:
orbit = int(re.split('_',infile)[6])
orbit = str(orbit - orbit%1000)
infile = baseSen + orbit + '/' + infile
print 'Fully pathed infile is: %s' % infile
if os.path.exists(infile):
print infile
baseGran = re.split('/',infile.replace('.zip',''))[-1]
start = datetime.datetime.now()
td2 = tempDir(tdir,baseGran)
print td2
#if 'SLC' in infile:
#print 'Debursting SLC product'
#calOut = topsarDeburst1(infile,td2,baseGran)
if noOrb == False:
obOut = applyOrbit(infile,td2,baseGran)
#obOut = applyOrbit1(calOut,td2)
print obOut
print 'Time to fix orbit: ',
lasttime = datetime.datetime.now()
print(timestamp(datetime.datetime.now())-timestamp(start))
nrOut = removeThermalNoise(obOut,td2)
calOut = applyCal(nrOut,td2)
else:
calOut = applyCal(infile,td2)
print calOut
print 'Time to calibrate: ',
print(timestamp(datetime.datetime.now())-timestamp(lasttime))
lasttime = datetime.datetime.now()
if 'SLC' in infile:
print 'Debursting SLC product'
calOut = topsarDeburst(calOut,td2)
if subset == True:
subs = 'POLYGON((%3.2f %3.2f,%3.2f %3.2f,%3.2f %3.2f,%3.2f %3.2f, %3.2f %3.2f))' % (ullon,ullat,lrlon,ullat,lrlon,lrlat,ullon,lrlat,ullon,ullat)
print subs
subOut = applySubset(calOut,subs,td2)
else:
subOut = calOut
# This is really speckle filtering even though it looks like calibration
lasttime = datetime.datetime.now()
print 'Speckle filtering'
#subOut = applySpeckleFilter(subOut,td2)
print 'Time to Speckle Filter ',
print(timestamp(datetime.datetime.now())-timestamp(lasttime))
lasttime = datetime.datetime.now()
if 'SLC' in infile:
print 'Multilooking SLC product'
subOut = applyML(subOut,td2)
print 'Time to multilook ',
print(timestamp(datetime.datetime.now())-timestamp(lasttime))
lasttime = datetime.datetime.now()
if procRTC == True:
if subset == True:
tfOut = applyTF(subOut,td2,1)
else:
tfOut = applyTF(subOut,td2)
print tfOut
print 'Time to terrain flatten: ',
print(timestamp(datetime.datetime.now())-timestamp(lasttime))
lasttime = datetime.datetime.now()
tcOut = applyTC(tfOut,td2)
else:
tcOut = applyTC(subOut,td2)
print tcOut
print 'Time to terrain correct: ',
print(timestamp(datetime.datetime.now())-timestamp(lasttime))
print 'Total processing time: ',
print(timestamp(datetime.datetime.now())-timestamp(start))
if cleanTemp == True:
cmd = 'cd %s; rm -r *OB.* *CAL.* *TF.*' % td2
print cmd
os.system(cmd)
if procRTC == True and 'IW' in infile:
ifile = 'Gamma0_VV.img'
ifile2 = 'Gamma0_VH.img'
bgran2 = baseGran.replace('1SDV','1SDH')
elif procRTC == True and '1SDH' in infile:
ifile = 'Gamma0_VV.img'
ifile2 = 'Gamma0_VH.img'
bgran2 = baseGran.replace('1SDV','1SDH')
else:
ifile = 'Beta0_VV.img'
ifile2 = 'Beta0_VH.img'
#ifile = 'Sigma0_VV.img'
#ifile2 = 'Sigma0_VH.img'
bgran2 = baseGran.replace('1SDV','1SDH')
# This line is for Cleveland Volcano
#cmd = 'cd %s/%s*TC.data;gdal_translate -projwin -170.2 52.94 -169.8 52.7 Gamma0_VV.img %s.tif' % (td2,baseGran,baseGran)
# This line is for Calbuco
if subset==True:
cmd = 'cd %s/%s*TC.data;gdal_translate -co COMPRESS=PACKBITS -projwin %f %f %f %f %s %s.tif' % (td2,baseGran,ullon,ullat,lrlon,lrlat,ifile,baseGran)
cmd2 = 'cd %s/%s*TC.data;gdal_translate -co COMPRESS=PACKBITS -projwin %f %f %f %f %s %s.tif' % (td2,baseGran,ullon,ullat,lrlon,lrlat,ifile2,bgran2)
else:
cmd = 'cd %s/%s*TC.data;gdal_translate -co COMPRESS=PACKBITS %s %s.tif' % (td2,baseGran,ifile,baseGran)
cmd2 = 'cd %s/%s*TC.data;gdal_translate -co COMPRESS=PACKBITS %s %s.tif' % (td2,baseGran,ifile2,bgran2)
print cmd
os.system(cmd)
print cmd2
os.system(cmd2)
cmd = 'mv %s/*/*.tif geotiffs; rm -r %s' % (td2,td2)
#cmd = 'mv %s/*/*.tif geotiffs' % td2
print cmd
os.system(cmd)
else:
print('Input file could not be found')