forked from BiodivBONDS/S1-pre-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess
662 lines (604 loc) · 28.1 KB
/
preprocess
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import glob
import os
import snappy
import gc
import shutil
from snappy import ProductIO, GPF
# from rsgislib.segmentation import segutils
# Get snappy Operators
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
# HashMap Key-Value pairs
HashMap = snappy.jpy.get_type('java.util.HashMap')
gc.enable()
# Working directory
input_dir = "/mnt/DD/Bonds_Project/Aurea_Project/Input/test/"
images_list = []
s1list = []
class Image:
VVcalibFile = ''
coregistrationlist = []
def __init__(self, path):
# Basic attributes
self.path = path
# self.imgpath = ProductIO.readProduct(path)
self.timestamp = path.split('_')[6:7]
# Orbit file
self.orbFile = ''
self.orb = False
# Calibrated
self.VVcalibFile = ''
self.VHcalibFile = ''
self.calib = False
# Terrain correction
self.VVterrainFlatFile = ''
self.VHterrainFlatFile = ''
self.terr = False
self.VVorthoCorrFile = ''
self.VHorthoCorrFile = ''
self.orth = False
self.VHorthFile = ''
self.VVorthFile = ''
# Speckle filter
self.VVspeckFile = ''
self.VHspeckFile = ''
self.speck = False
# Decibel
self.VVdBFile = ''
self.VHdBFile = ''
self.dB = False
# meanstack
self.stack = False
self.VVmeanFile = ''
self.VHmeanFile = ''
# Polarisation
# if 'VV' in self.path:
# self.polarisation = 'VV'
# elif 'VH' in self.path:
# self.polarisation = 'VH'
# else:
# self.polarisation = ''
def getPath(self):
return self.path
def setPath(self, path):
self.path = path
def silentDelete(self, correction):
try:
toRemove = getattr(self, 'correction')
os.remove(toRemove)
os.rmdir(correction)
except OSError:
print('could not delete directory doesnt exists')
pass
except AttributeError:
print('could not find attribute')
pass
def deleteOnceDone(self):
try:
print('deleting stuff')
for things in os.listdir(input_dir):
if ''.join(self.timestamp) in things and things.endswith('.data') and 'orth' not in things:
print(things + ' is going to the recycle bin')
shutil.rmtree(things)
except OSError:
print('error deleting data folder')
pass
def getTimestamp(self):
return self.timestamp
def setTimestamp(self, path):
self.timestamp = path.split('_')[6:7]
return self.timestamp
def determineTimeStamp(self):
self.timestamp = self.path.split('_')[6:7]
def customTimeStamp(self, usertimestamp):
# we want to set a timestamp with the given timestamp argument
self.timestamp = usertimestamp
def datafolder(self, cor):
# on veut creer une fonction qui renvoie une chaine de caractere path pour un object image
# de type /inputdir/string_timestamp.data/
data = ''
try:
dim = getattr(self, cor)
if dim:
data = dim.split('.')[:-1] + 'data/'
except AttributeError:
print('Invalid Attribute')
pass
return data
def openImg(self):
# Open image with Snap reader
res = ProductIO.readProduct(self.path)
return res
def applyOrbit(self):
# first step to apply orbitfile
"""
Applying orbit on S1 GRD Object Image File
"""
if self.orb:
print('file already exists')
elif "orb_" + ''.join(self.getTimestamp()) + '.dim' in os.listdir(input_dir):
self.orbFile = input_dir + "orb_" + ''.join(self.getTimestamp())
print('file has already been proccessed')
pass
else:
# ---------initialisation des parametres
parameters = HashMap()
# ---------Input
orbit_param = snappy.GPF.createProduct("Apply-Orbit-File", parameters, ProductIO.readProduct(self.path))
#
# --------Output
self.orbFile = input_dir + "orb_" + ''.join(self.getTimestamp())
ProductIO.writeProduct(orbit_param, self.orbFile,
'BEAM-DIMAP') # on cree un objet qu'on rempli avec le tableau image
print(str(self.getTimestamp()) + " is done ")
# ---------Return values
self.orb = True
return self.orbFile, self.orb
def applyCalibration(self):
"""
Input: Amplitude_VH, Intesity_VH, Amplitude_VV, Intensity_VV : an orbite file corrected
output: beta0_VH, beta0_VV : a calibrated image file
"""
print("Now applying calibration")
if self.calib:
print('file already exists')
pass
elif 'VVcalib_' + ''.join(self.getTimestamp()) + '.dim' in os.listdir(input_dir):
self.VVcalibFile = input_dir + 'VVcalib_' + ''.join(self.getTimestamp())
self.VHcalibFile = input_dir + 'VHcalib_' + ''.join(self.getTimestamp())
print(' file has already been calibrated')
pass
else:
# initialisation des parametres
parameters = HashMap()
parameters.put('outputSigmaBand', True)
parameters.put('outputBetaBand', False)
parameters.put('outputGammaBand', False)
parameters.put("outputImageScaleInDb", False)
parametersVH = parameters
parameters.put('Polarisations', 'VV')
parametersVH.put('Polarisations', 'VH')
self.VVcalibFile = input_dir + "VVcalib_" + ''.join(self.getTimestamp())
self.VHcalibFile = input_dir + 'VHcalib_' + ''.join(self.getTimestamp())
calib_param = snappy.GPF.createProduct("Calibration", parameters,
ProductIO.readProduct(self.orbFile + '.dim'))
calib_paramVH = snappy.GPF.createProduct("Calibration", parametersVH,
ProductIO.readProduct(self.orbFile + '.dim'))
# ----Writing
# ---VV
ProductIO.writeProduct(calib_param, self.VVcalibFile, 'BEAM-DIMAP')
print(str(self.VVcalibFile) + " is done")
# ----VH
ProductIO.writeProduct(calib_paramVH, self.VHcalibFile, 'BEAM-DIMAP')
print(str(self.VHcalibFile) + " is done")
self.calib = True
# Deleting old stuff
# print('trying to free disk space')
# if self.orb:
# try:
# for things in input_dir + 'orb_' + ''.join(self.getTimestamp()) + '.data/':
# os.remove(things)
# os.removedirs(input_dir + 'orb_' + ''.join(self.getTimestamp()) + '.data/')
# print('orbfile has been removed')
# except OSError:
# print('nothing to delete')
# pass
return self.VVcalibFile, self.VHcalibFile, self.calib
def applyTerrainCorrection(self):
# Apply Terrain Flattening on a Calibrated file
VVcalibFile = ''
print "Now applying terrain correction"
# LEt us check if it exists
if self.terr:
print('file already exists')
elif 'VVcalib_' + ''.join(self.getTimestamp()) + '.dim' in os.listdir(input_dir):
print('file has already been calibrated adding to desc')
self.VVterrainFlatFile = input_dir + 'VVterr_' + ''.join(self.getTimestamp())
self.VHterrainFlatFile = input_dir + 'VHterr_' + ''.join(self.getTimestamp())
else:
# Output
print('applying terrain flattening operator')
self.VVterrainFlatFile = input_dir + "VVterr_" + ''.join(self.getTimestamp())
self.VHterrainFlatFile = input_dir + 'VHterr_' + ''.join(self.getTimestamp())
# ---------Parameters-------
parameters = HashMap()
# parameters.put('demName', 'SRTM 3Sec')
# dempath = '/mnt/DD/Bonds_Project/Hardi_Process/dem/srtm_26_13.zip'
# parameters.put('mapProjection', proj)
# parameters.put('externalDEMFile', 'dempath')
# parameters.put('pixelSpacingInMeter', '10.0')
parameters.put('demName', 'SRTM 1sec HGT')
parameters.put('demResamplingMethod', 'BILINEAR_INTERPOLATION')
parameters.put('additionalOverlap', 0.1)
parameters.put('OversamplingMultiple', 1.5)
parametersVH = parameters
parameters.put('sourceBands', 'Sigma0_VV')
parametersVH.put('sourceBands', 'Sigma0_VH')
# ----------Reading --------
# ----Creating the product
# -- VV --
# we fetch the input , the input shall be a calibrated sar file so we are checking if it exists
try:
VVcalibFile = getattr(self, "VVcalibFile")
print('doing VV, VV calib File is : ' + VVcalibFile)
except AttributeError:
print('No VVcalibFile')
pass
if VVcalibFile: # We assume VH exists if VV exists , thus reading
# ------------Reading
VVterr_param = snappy.GPF.createProduct("Terrain-Flattening", parameters,
ProductIO.readProduct(self.VVcalibFile + '.dim'))
# -- VH --
VHterr_param = snappy.GPF.createProduct("Terrain-Flattening", parametersVH,
ProductIO.readProduct(self.VHcalibFile + '.dim'))
# ----- Writing----
ProductIO.writeProduct(VVterr_param, self.VVterrainFlatFile, 'BEAM-DIMAP')
print str(self.VVterrainFlatFile) + " is done "
ProductIO.writeProduct(VHterr_param, self.VHterrainFlatFile, 'BEAM-DIMAP')
print str(self.VHterrainFlatFile) + " is done "
# ------------Deleting useless stuff and returning values -------
self.terr = True
return self.VVterrainFlatFile, self.VHterrainFlatFile, self.terr
else: # Condition if VV doesnt exist( '' chain )
print('Image not calibrated')
pass
def applyOrthorectification(self):
VVcalibFile = ''
# Correction of geometry of object images , Input = Gamma0 terrain flattened file only
print("Now applying Orthorectification")
if self.orth:
print("file already exists")
elif 'VVorth_' + ''.join(self.getTimestamp()) + '.dim' in os.listdir(input_dir):
print('file has already been geo corrected adding to desc')
self.VVorthoCorrFile = input_dir + "VVorth_" + ''.join(self.getTimestamp())
self.VHorthoCorrFile = input_dir + 'VHorth_' + ''.join(self.getTimestamp())
else:
# Validity check
try:
VVcalibFile = getattr(self, "VVcalibFile")
print('doing VV, VV calib File is : ' + VVcalibFile)
except AttributeError:
print('No VVcalibFile')
pass
if VVcalibFile:
# -------initialisation des parametres
parameters = HashMap()
parameters.put('demResamplingMethod', 'BICUBIC_INTERPOLATION')
parameters.put('demName', 'SRTM 1Sec HGT')
parametersVH = parameters
parametersVH.put('sourceBands', 'Sigma0_VH')
parameters.put('sourceBands', 'Sigma0_VV')
# ------------Output----
self.VVorthoCorrFile = input_dir + "VVorth_" + ''.join(self.getTimestamp())
self.VHorthoCorrFile = input_dir + 'VHorth_' + ''.join(self.getTimestamp())
# --------Reading---------
# VH
VHorth_param = snappy.GPF.createProduct("Terrain-Correction", parameters,
ProductIO.readProduct(self.VVcalibFile + '.dim'))
# VV
VVorth_param = snappy.GPF.createProduct("Terrain-Correction", parametersVH,
ProductIO.readProduct(self.VHcalibFile + '.dim'))
# --------writing-------
# VV
ProductIO.writeProduct(VHorth_param, self.VHorthoCorrFile, 'BEAM-DIMAP')
print(str(self.VHorthoCorrFile + ''.join(self.getTimestamp()) + " is done "))
# VH
ProductIO.writeProduct(VVorth_param, self.VVorthoCorrFile, 'BEAM-DIMAP')
print(str(self.VVorthoCorrFile + ''.join(self.getTimestamp()) + 'is done '))
# ------- Return values and deleting useless stuff
self.orth = True
return self.VVorthoCorrFile, self.VHorthoCorrFile, self.orth
else:
print('file has not been terrain corrected')
pass
def applySpeckleFilter(self, corvv='', corvh=''):
VVInput = ''
VHInput = ''
# fonction qui permet de retirer le speckle d'une image radar, Gamma 0 en input
print("Now applying speckle filtering")
# check if file has already been speckle filtered
if self.speck:
print("file already exists")
elif "VVspeck_" + ''.join(self.getTimestamp()) + '.dim' in os.listdir(input_dir):
self.VVspeckFile = input_dir + "VVspeck_" + ''.join(self.getTimestamp())
self.VHspeckFile = input_dir + "VHspeck_" + ''.join(self.getTimestamp())
print('file has already been proccessed')
pass
else:
# Validity Check
try:
VVInput = getattr(self, corvv)
VHInput = getattr(self, corvh)
except AttributeError:
print (' correction does not exist')
pass
if VVInput or VHInput:
# -----------------initialisation des parametres--------
parameters = HashMap()
# parameters.put('sourceBands', 'Sigma0_VV','Sigma0_VH')
# parameters.put('numberofLooks', 4)
parameters.put('filter', 'Lee Sigma')
parameters.put('windowSize', "5x5")
parameters.put('sigma', 0.9)
parameters.put('targetWindowSize', "5")
parametersVH = parameters
parameters.put('sourceBands', 'Sigma0_VV')
# ---------- Output----------
self.VVspeckFile = input_dir + "VVspeck_" + ''.join(self.getTimestamp())
self.VHspeckFile = input_dir + "VHspeck_" + ''.join(self.getTimestamp())
# ----------Reading-------
# VV
VVspeck_param = snappy.GPF.createProduct("Speckle-Filter", parameters,
ProductIO.readProduct(VVInput + '.dim'))
# VH
parametersVH.put('sourceBands', 'Sigma0_VH')
VHspeck_param = snappy.GPF.createProduct("Speckle-Filter", parametersVH,
ProductIO.readProduct(VHInput + '.dim'))
# ------- Writing--------
# VV
print(" Using speckefiltering on " + str(self.VVspeckFile))
ProductIO.writeProduct(VVspeck_param, self.VVspeckFile, 'BEAM-DIMAP')
print(str(self.VVspeckFile) + "is done")
# VH
print(" Using speckefiltering on " + str(self.VVspeckFile))
ProductIO.writeProduct(VHspeck_param, self.VHspeckFile, 'BEAM-DIMAP')
print(str(self.VHspeckFile) + "is done")
# -------- Return values and deleting---------
self.speck = True
return self.VVspeckFile, self.VHspeckFile, self.speck
def convertToDecibel(self):
# imgFileVV = ''
# imgFileVH = ''
# Convert an img to decibel, cor is actually the type of correction one may set to dB
if self.dB:
# check if file has already been speckle filtered
print("file already exists")
elif "VVdB_" + ''.join(self.getTimestamp()) + '.dim' in os.listdir(input_dir):
self.VVdBFile = input_dir + "VVdB_" + ''.join(self.getTimestamp())
self.VHdBFile = input_dir + "VHdB_" + ''.join(self.getTimestamp())
print('file has already been proccessed')
pass
else:
# There are 2 possible inputs, one is a terrain corrected image (orth)
# The other is a mean raster which wont have a true stack attribute
# both will have either VV or VH ,
# corvv = {VVmeanFile, VVorthoCorrFile } , corvh = { VHmeanFile, VHorthoCorrFile}
try:
print("Now converting to dB")
print ("converting Linear to decibel for " + ''.join(self.getTimestamp()))
# -------Output
# VV , we can insert a try statement here to safe proof the run in case of an unlikely error
self.VVdBFile = input_dir + "VVdB_" + ''.join(self.getTimestamp())
# VH
self.VHdBFile = input_dir + "VHdB_" + ''.join(self.getTimestamp())
# initialisation des parametres
parameters = HashMap()
parametersVH = parameters
parameters.put('sourceBands', 'Sigma0_VV')
parametersVH.put('sourceBands', 'Sigma0_VH')
# Reading
VVdb_param = snappy.GPF.createProduct("LinearToFromdB", parameters,
ProductIO.readProduct(self.VVorthoCorrFile + '.dim'))
VHdb_param = snappy.GPF.createProduct("LinearToFromdB", parametersVH,
ProductIO.readProduct(self.VHorthoCorrFile + '.dim'))
# Writing , Nique sa grand mere la pute geotiff de mes couilles
ProductIO.writeProduct(VVdb_param, self.VVdBFile, 'GeoTIFF-BigTIFF')
print str(self.VVdBFile) + "is done"
ProductIO.writeProduct(VHdb_param, self.VHdBFile, 'GeoTIFF-BigTIFF')
print str(self.VHdBFile) + "is done"
# Return values
self.dB = True
return self.VVdBFile, self.VHdBFile, self.dB
except AttributeError:
print('correction doesnt exists')
pass
def converToDecibel(self):
# We want a function wich take a object , read it into an image and the nperform convert to DB OP
try:
print('Converting to DB')
# Output
# polarisation = ''
if 'VV' in self.path:
polarisation = 'VV_'
elif 'VH' in self.path:
polarisation = 'VH_'
else:
polarisation = ''
output = input_dir + '/dB_' + polarisation + ''.join(self.getTimestamp())
# Reading
im = ProductIO.readProduct(self.path)
parameters = HashMap()
dbparam = snappy.GPF.createProduct("LinearToFromdB", parameters, im)
# Writing
ProductIO.writeProduct(dbparam, output, 'GeoTIFF')
except OSError:
print('Invalid')
def stackRegistration(self):
stacklist = []
ProductIO.readProduct(self).append(stacklist)
self.stack = True
return stacklist, self.stack
def addtoCoRegistration(self, coregistrationlist=None):
coregistrationlist.append(ProductIO.readProduct(self.path))
return coregistrationlist
# def coRegistration(cmd):
# stacklistVV = []
# print('doing dem coregistration')
# # on cree l'objet product permettant de faire la coregistration
# # initialisation des parametres
# parameters = HashMap()
# parameters.put('demName', 'GETASSE30')
# parameters.put('demResamplingMethod', 'BICUBIC_INTERPOLATION')
# parameters.put('ResamplingType', 'BICUBIC_INTERPOLATION')
# # on cree la sortie
# coRegisterVV = input_dir + '/coregistrationVV' + '.dim'
# coRegisterVH = input_dir + '/coregistrationVH' + '.dim'
# print "destination folder is " + str(input_dir)
# param = snappy.GPF.createProduct("DEM-Assisted-Coregistration", parameters, stacklist)
# param2 = snappy.GPF.createProduct("DEM-Assisted-Coregistration", parameters, stack2)
# ProductIO.writeProduct(param, outfile, 'BEAM-DIMAP')
# ProductIO.writeProduct(param2, outfile2, 'BEAM-DIMAP')
def imageSearch(folder, extension):
for images in os.listdir(folder):
if images.endswith(extension):
images_list.append(folder + images)
print("listes d'images: " + str(images_list))
return images_list
def createstack(polarisation, stack):
if os.path.isfile(input_dir + "/+" + polarisation + 'stack*'):
print "file already exists"
else:
print "creating stack"
# initialisation des parametres
parameters = HashMap() # initialise le dico pour les parametres
parameters.put('extent', 'Master')
parameters.put('initialOffsetMethod', 'Product Geolocation')
parameters.put('ResamplingType', 'None')
parameters.put('FindOptimalMaster', 'True')
# recherche des images pour le stack
create_stack = GPF.createProduct("CreateStack", parameters, stack)
output = input_dir + '/stackVV'
ProductIO.writeProduct(create_stack, output, 'BEAM-DIMAP')
def createS1Imglist(inputfolder='', extension='zip'):
if inputfolder:
folder = inputfolder
else:
folder = "/mnt/DD/Bonds_Project/Aurea_Project/Input/2017/"
imageSearch(folder, extension) # The goal here is to fetch a list of path and give it to our variable object
for path in images_list:
# a list named s1list will be created and will get line of path for each image into the path attribute
s1list.append(Image(path))
return s1list, images_list
# def checkDone(self,targetdirectory):
# # We want to create a function which search a folder in order to determine if some process has been done
# # so first we want using the timestamp (which differentiate one image from another ) to search the folder
# correctionlist = ['calibVH_','calibVV_','orb_','speckVH_','speckVV_','VHterr_','VVterr_']
# for time in glob.glob(targetdirectory):
# if ''.join(self.getTimestamp()) in time:
# for correction in correctionlist:
# if time.startswith(correction):
# self.
def demCoregistration():
print('doing dem coregistration')
nom_image_list = []
nom_image_list2 = []
stack = []
stack2 = []
for img in glob.glob(input_dir + '/VVorth*'):
print(str(img))
if '.dim' in img:
nom_image_list.append(img)
for imgVH in glob.glob(input_dir + '/VHorth*'):
print(str(imgVH))
if '.dim' in imgVH:
nom_image_list2.append(imgVH)
print(str(nom_image_list))
print(str(nom_image_list2))
print str(stack)
# on cree l'objet product permettant de faire la coregistration
for f in nom_image_list:
stack.append(ProductIO.readProduct(f))
for g in nom_image_list2:
stack2.append(ProductIO.readProduct(g))
# initialisation des parametres
parameters = HashMap()
parameters.put('demName', 'SRTM 1Sec HGT')
parameters.put('demResamplingMethod', 'BICUBIC_INTERPOLATION')
parameters.put('ResamplingType', 'BICUBIC_INTERPOLATION')
parametersVH = parameters
parameters.put('sourceBands', 'Sigma0_VV')
parametersVH.put('sourceBands', 'Sigma0_VH')
# on cree la sortie
outfile = input_dir + '/coregistrationVV'
outfile2 = input_dir + '/coregistrationVH'
print "destination folder is " + str(outfile)
param = snappy.GPF.createProduct("DEM-Assisted-Coregistration", parameters, stack)
ProductIO.writeProduct(param, outfile, 'BEAM-DIMAP')
print('doing VH ')
param2 = snappy.GPF.createProduct("DEM-Assisted-Coregistration", parametersVH, stack2)
ProductIO.writeProduct(param2, outfile2, 'BEAM-DIMAP')
def stackaveraging(s=input_dir + '/coregistrationVV.dim', s2=input_dir + '/coregistrationVH.dim'):
# On cree une fonction permettant de calculer la moyenne de l'ensemble des bandes d'un stack par pixels
print 'stack averaging'
# initialisation des parametres
parameters = HashMap()
parameters.put('Statistic', 'Mean Average')
parametersVH = parameters
parameters.put('sourceBands', 'Sigma0_VV')
parametersVH.put('sourceBands', 'Sigma0_VH')
stack = ProductIO.readProduct(s)
stack2 = ProductIO.readProduct(s2)
# creation of the output
output = input_dir + '/meanraster/meanraster.dim'
output2 = input_dir + '/meanraster/meanrasterVH.dim'
createmeanraster = snappy.GPF.createProduct("Stack-Averaging", parameters, stack)
andVH = snappy.GPF.createProduct("Stack-Averaging", parametersVH, stack2)
print('first VV')
ProductIO.writeProduct(createmeanraster, output, 'BEAM-DIMAP')
print('Then VH ')
ProductIO.writeProduct(andVH, output2, 'BEAM-DIMAP')
# Converting ASAP to DB
print 'converting to dB'
parameters2 = HashMap()
parameters2.put('sourceBands', 'Sigma0_VV')
parameters2VH = HashMap()
parameters2VH.put('sourceBands','Sigma0_VH') # VV ou VH
db_param = snappy.GPF.createProduct("LinearToFromdB", parameters2,
ProductIO.readProduct(output))
ProductIO.writeProduct(db_param, input_dir + '/meanraster/db_meanrasterVV.tif', 'GeoTIFF-BigTIFF')
print('dbmeanraster VV is done ')
print('now doing VH ')
db_paramVH = snappy.GPF.createProduct("LinearToFromdB", parameters2, ProductIO.readProduct(output2))
ProductIO.writeProduct(db_paramVH, input_dir + '/meanraster/db_meanrasterVH.tif', 'GeoTIFF-BigTIFF')
def run(inputfolder=''):
if inputfolder:
os.chdir(inputfolder)
currentDir = os.getcwd()
print(currentDir)
createS1Imglist(inputfolder)
if not s1list:
print('list is empty, filling list')
for s1 in images_list:
s1list.append(Image(s1))
else:
inputfolder = input_dir
os.chdir(input_dir)
createS1Imglist(inputfolder)
print('Step 1 , Preprocessing Image0s ')
for s1img in s1list:
if isinstance(s1img, Image):
# Check if it is an object image
#s1img.applyOrbit()
#s1img.applyCalibration()
#s1img.applySpeckleFilter('VVcalibFile', 'VHcalibFile')
#s1img.applyTerrainCorrection()
#s1img.applyOrthorectification()
s1img.convertToDecibel()
#s1img.deleteOnceDone()
print('preprocess is done ')
print('starting Step 2 , CoRegistration')
stackVV = []
stackVH = []
# We are looking for image to stack, and we need those which are not stacked
#for s1img in s1list:
# if not s1img.stack:
# stackVV.append(ProductIO.readProduct(getattr(s1img, 'VVorthoCorrFile')))
# stackVH.append(ProductIO.readProduct(getattr(s1img, 'VHorthoCorrFile')))
# s1img.stack = True
#demCoregistration()
# createstack('VV', stackVV)
# createstack('VH', stackVH)
# def main():
# # loop to fetch all zip in folder for preprocessing
# for folder in glob.glob(input_dir + "/*.zip"):
# if folder.endswith(".zip"):
# timestamp = folder.split("_")[5]
# sentinel_image = ProductIO.readProduct(folder)
# print(str(timestamp) + " is being processed")
# applyOrbit(timestamp, sentinel_image, HashMap)
# applyCalibration(timestamp, sentinel_image, HashMap)
# # applyMultilook(timestamp, HashMap)
# applyTerrainCorrection(timestamp, HashMap)
# applyOrthorectification(timestamp, HashMap)
# applySpeckleFilter(timestamp, HashMap)
# # shepherdSeg()
# main()