-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.py
758 lines (568 loc) · 21.3 KB
/
batch.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
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#-------------------------------------------------------------------------------
# Name: SIAM batch creator
# Purpose: This script uses a Tkinter GUI to create a SIAM
# batch file based on SIAM compatible files located in a target
# directory.
#
# Author: h.Augustin
#
# Created: 21.12.2016
#
#-------------------------------------------------------------------------------
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
import os
import sys
import Tkinter
import fnmatch
import subprocess
import tkMessageBox
import gdal
# Set GDAL environment variable for translating grib to tiff.
# os.environ['GDAL_DATA'] = r'C:\Program Files\GDAL\gdal-data'
def param_check():
if var00.get() == '':
info = 'Please enter SIAM executable location!'
tkMessageBox.showerror('Missing Parameter', info)
elif s2_Folder.get() == '':
info = 'Please enter root folder!'
tkMessageBox.showerror('Missing Parameter', info)
elif var06.get() == 'Select one.':
info = 'Please select an image type!\n\nSentinel 2 is LANDSAT_LIKE.'
tkMessageBox.showerror('Missing Parameter', info)
else:
master.quit()
def show_params():
if var06.get() == 'Select one.':
info = 'Please select an image type!\n\nSentinel 2 is LANDSAT_LIKE.'
tkMessageBox.showerror('Missing Parameter', info)
else:
test = ('S2 path:\n {}\n\n'
'Image type: {} ({})\n'
'Sensor type: {} ({})\n'
'Processing with a binary mask: {}\n'
'Crisp classification: {}\n'
'Smoke-plume mask: {}\n'
'Cloud mask: {}\n'
'Burnt area mask: {}\n'
'Vegetation binary mask: {}\n'
'Vegetation trinary mask: {}\n'
'Baresoil built-up trinary mask: {}\n'
'Cloud trinary mask: {}\n'
'Water trinary mask: {}\n'
'Shadow trinary mask: {}\n'
'Urban area binary mask: {}\n'
'Shape: {}').format(
s2_Folder.get(), var06.get(), image_type[var06.get()],
sensor_type.get(), data_type[sensor_type.get()],
var03.get(), var07.get(), var09.get(), var10.get(), var11.get(),
var12.get(), var13.get(), var14.get(), var15.get(), var16.get(),
var17.get(), var18.get(), var19.get())
tkMessageBox.showinfo('Test Parameters', test)
#------------------------------------------------------------------------------#
# GUI #
#------------------------------------------------------------------------------#
#
# Create GUI interface frames.
#
master = Tkinter.Tk()
title = Tkinter.Frame(master)
title.pack(side='top')
siam_input = Tkinter.Frame(master)
siam_input.pack()
s2_input = Tkinter.Frame(master)
s2_input.pack()
img_options = Tkinter.Frame(master)
img_options.pack()
sensor_options = Tkinter.Frame(master)
sensor_options.pack()
binary = Tkinter.Frame(master)
binary.pack()
buttons = Tkinter.Frame(master)
buttons.config(pady=15)
buttons.pack(side='bottom')
#
# Title
#
label01 = Tkinter.Label(title, text="SIAM Batch Creator")
label01.config(width=30, font=("Courier", 44))
label01.pack(pady=15)
#
# Define SIAM .exe location text entry box.
#
label02 = Tkinter.Label(siam_input, text="Insert SIAM .exe path: ")
label02.pack(pady=15, side='left')
var00 = Tkinter.StringVar()
var00.set(
r'C:\SIAM\SIAM_r88v7\SIAM_License_Executables\SIAM_r88v7_Windows.exe')
var00x = Tkinter.Entry(siam_input, textvariable=var00, justify='left')
var00x.config(width=61)
var00x.pack(pady=15, side='right')
#
# Define S2 root folder text entry box.
#
label03 = Tkinter.Label(s2_input, text='Path to root folder where images '
'including .dat files for SIAM are saved: ')
label03.pack(pady=15, side='left')
cwd = os.getcwd()
root_folder = os.path.join(cwd, 'tempS2')
s2_Folder = Tkinter.StringVar()
s2_Folder.set(root_folder)
s2_Folderx = Tkinter.Entry(s2_input, textvariable=s2_Folder, justify='left')
s2_Folderx.config(width=40)
s2_Folderx.pack(pady=15, side='right')
#
# Create image-type dropdown menu.
#
label04 = Tkinter.Label(img_options, text="Select an image type for SIAM: ")
label04.pack(side='left')
var06 = Tkinter.StringVar()
image_type = {"LANDSAT_LIKE": 1, "SPOT_LIKE": 2, "AVHRR_LIKE": 3, "VHR_LIKE": 4}
var06.set('Select one.')
var06x = Tkinter.OptionMenu(img_options, var06, *image_type.keys())
var06x.config(width=15, anchor='w')
var06x.pack(pady=10, side='right')
# image_type[var06.get()] returns the key value
label05 = Tkinter.Label(sensor_options, text="Select the actual sensor: ")
label05.pack(side='left')
sensor_type = Tkinter.StringVar()
data_type = {"Sentinel-2": 1, "Landsat 8": 2, "clip_mosaic_calrefbyt": 3}
sensor_type.set('Select one.')
sensor_typex = Tkinter.OptionMenu(sensor_options, sensor_type, *data_type.keys())
sensor_typex.config(width=15, anchor='w')
sensor_typex.pack(pady=10, side='right')
#
# Create checkboxes for all binary variables.
#
var03 = Tkinter.IntVar()
var03x = Tkinter.Checkbutton(binary, text="Use a binary mask for processing",
variable=var03, anchor='w', height=1, width=50)
var03x.pack()
var07 = Tkinter.IntVar()
var07.set(1)
var07x = Tkinter.Checkbutton(binary,
text="Use crisp classification (default=true)",
variable=var07, anchor='w', height=1, width=50)
var07x.pack()
var09 = Tkinter.IntVar()
var09x = Tkinter.Checkbutton(binary, text="Smoke-plume mask",
variable=var09, anchor='w', height=1, width=50)
var09x.pack()
var10 = Tkinter.IntVar()
var10x = Tkinter.Checkbutton(binary, text="Cloud mask",
variable=var10, anchor='w', height=1, width=50)
var10x.pack()
var11 = Tkinter.IntVar()
var11x = Tkinter.Checkbutton(binary, text="Burnt area mask",
variable=var11, anchor='w', height=1, width=50)
var11x.pack()
var12 = Tkinter.IntVar()
var12x = Tkinter.Checkbutton(binary, text="Vegetation binary mask",
variable=var12, anchor='w', height=1, width=50)
var12x.pack()
var13 = Tkinter.IntVar()
var13x = Tkinter.Checkbutton(binary, text="Vegetation trinary mask",
variable=var13, anchor='w', height=1, width=50)
var13x.pack()
var14 = Tkinter.IntVar()
var14x = Tkinter.Checkbutton(binary, text="Baresoil builtup trinary mask",
variable=var14, anchor='w', height=1, width=50)
var14x.pack()
var15 = Tkinter.IntVar()
var15x = Tkinter.Checkbutton(binary, text="Cloud trinary mask",
variable=var15, anchor='w', height=1, width=50)
var15x.pack()
var16 = Tkinter.IntVar()
var16x = Tkinter.Checkbutton(binary, text="Water trinary mask",
variable=var16, anchor='w', height=1, width=50)
var16x.pack()
var17 = Tkinter.IntVar()
var17x = Tkinter.Checkbutton(binary, text="Shadow trinary mask",
variable=var17, anchor='w', height=1, width=50)
var17x.pack()
var18 = Tkinter.IntVar()
var18x = Tkinter.Checkbutton(binary, text="Urban area binary mask",
variable=var18, anchor='w', height=1, width=50)
var18x.pack()
var19 = Tkinter.IntVar()
var19x = Tkinter.Checkbutton(binary, text="Shape.",
variable=var19, anchor='w', height=1, width=50)
var19x.pack()
#
# Ability to close GUI and print current state of variables using bottons.
#
button01 = Tkinter.Button(buttons, text='Continue', command=param_check)
button01.pack()
button02 = Tkinter.Button(buttons, text='Test', command=show_params)
button02.pack()
#
# Creates GUI box where widgets go.
#
Tkinter.mainloop()
#------------------------------------------------------------------------------#
# Search for number of tiles and ask whether to continue #
#------------------------------------------------------------------------------#
#
# Sentinel-2
#
if data_type[sensor_type.get()] == 1:
procFolders = []
for dirpath, dirnames, filenames in os.walk(s2_Folder.get(), topdown=True):
for dirname in dirnames:
if dirname == 'PROC_DATA':
procFolders.append(os.path.join(dirpath, dirname))
#
# Hide the main window.
#
Tkinter.Tk().withdraw()
question = ('Would you like to process {} tiles in SIAM?').format(
str(len(procFolders)))
#
# Create the content of the window.
#
messagebox = tkMessageBox.askyesno('SIAM batch creator',
question)
#------------------------------------------------------------------------------#
# Create batch file and launch SIAM or quit #
#------------------------------------------------------------------------------#
if messagebox:
#
# Register all of the GDAL drivers.
#
gdal.AllRegister()
#
# Create empty batch file for SIAM.
#
var06_text = var06.get()
if len(procFolders) > 1:
batFilename = 'SIAM_multiple_batch_{}.bat'.format(var06_text[:-5])
else:
batFilename = 'SIAM_batch_{}.bat'.format(var06_text[:-5])
batch_path = os.path.join(s2_Folder.get(), batFilename)
#
# Convert binary variables and image type identifier from GUI to strings.
#
var00 = var00.get()
var03 = str(var03.get())
var06 = str(image_type[var06.get()])
var07 = str(var07.get())
var09 = str(var09.get())
var10 = str(var10.get())
var11 = str(var11.get())
var12 = str(var12.get())
var13 = str(var13.get())
var14 = str(var14.get())
var15 = str(var15.get())
var16 = str(var16.get())
var17 = str(var17.get())
var18 = str(var18.get())
var19 = str(var19.get())
for procFolder in procFolders:
var01 = procFolder
#
# Create the folder for SIAM output data if it doesn't exist.
#
siam_output = os.path.join(procFolder, 'siamoutput')
if not(os.path.exists(siam_output)):
os.mkdir(siam_output)
var08 = siam_output
#
# Fill var02 variable with the calrefbyt filename.
#
for filename in os.listdir(procFolder):
if (filename.endswith('.dat')
and fnmatch.fnmatch(filename, '*_calrefbyt_*')):
var02 = filename
#
# Go to next folder if calibrated, stacked raster not yet craeted.
#
if not var02:
print '*_calrefbyt_* not found in {}'.format(procFolder)
print 'Tile not processed.'
continue
#
# Path to calrefbyt.
#
calrefbyt = os.path.join(procFolder, var02)
#
# Open image to calculate rows and columns.
#
img = gdal.Open(calrefbyt, gdal.GA_ReadOnly)
if img is None:
print '\nCould not open *calrefbyt*.dat'
print 'Folder not processed: {}\n'.format(procFolder)
continue
var04 = str(img.RasterYSize) # Rows.
var05 = str(img.RasterXSize) # Columns.
#
# Create string to write to batch file.
#
batch_entry = ' '.join((var00, var01, var02, var03, var04, var05,
var06, var07, var08, var09, var10, var11, var12, var13, var14,
var15, var16, var17, var18, var19))
with open(batch_path, 'a') as f:
f.write(batch_entry + '\n')
## print batch_entry
#
# Clean up.
#
img = None
#
# Location.
#
print '\n\n{} created.\nSaved to: {}\n\n'.format(batFilename, batch_path)
else:
print '\nNo SIAM batch file created.\n'
sys.exit()
#
# Landsat-8
#
elif data_type[sensor_type.get()] == 2:
#
# Create list for landsat-8 folders folder paths.
#
landsat8Folders = []
for dirpath, dirnames, filenames in os.walk(s2_Folder.get(), topdown=True):
for dirname in dirnames:
if dirname.startswith('LC08_L1TP_') and dirname.endswith('_T1'):
landsat8Folders.append(os.path.join(dirpath, dirname))
#
# Hide the main window.
#
Tkinter.Tk().withdraw()
question = ('Wold you like to process {} landsat8 images in SIAM?').format(
len(landsat8Folders))
#
# Create the content of the window.
#
procFolders = landsat8Folders
messagebox = tkMessageBox.askyesno('SIAM batch creator',
question)
#------------------------------------------------------------------------------#
# Create batch file and launch SIAM or quit #
#------------------------------------------------------------------------------#
if messagebox:
#
# Register all of the GDAL drivers.
#
gdal.AllRegister()
#
# Create empty batch file for SIAM.
#
var06_text = var06.get()
if len(procFolders) > 1:
batFilename = 'SIAM_multiple_batch_{}.bat'.format(var06_text[:-5])
else:
batFilename = 'SIAM_batch_{}.bat'.format(var06_text[:-5])
batch_path = os.path.join(s2_Folder.get(), batFilename)
#
# Convert binary variables and image type identifier from GUI to strings.
#
var00 = var00.get()
var03 = str(var03.get())
var06 = str(image_type[var06.get()])
var07 = str(var07.get())
var09 = str(var09.get())
var10 = str(var10.get())
var11 = str(var11.get())
var12 = str(var12.get())
var13 = str(var13.get())
var14 = str(var14.get())
var15 = str(var15.get())
var16 = str(var16.get())
var17 = str(var17.get())
var18 = str(var18.get())
var19 = str(var19.get())
for landsat8Folder in landsat8Folders:
var01 = landsat8Folder
#
# Create the folder for SIAM output data if it doesn't exist.
#
siam_output = os.path.join(landsat8Folder, 'siamoutput')
if not(os.path.exists(siam_output)):
os.mkdir(siam_output)
var08 = siam_output
#
# Fill var02 variable with the calrefbyt filename.
#
for filename in os.listdir(landsat8Folder):
if (filename.endswith('.dat')
and fnmatch.fnmatch(filename, '*_calrefbyt_*')):
var02 = filename
#
# Go to next folder if calibrated, stacked raster not yet craeted.
#
if not var02:
print '*_calrefbyt_* not found in {}'.format(landsat8Folder)
print 'Tile not processed.'
continue
#
# Path to calrefbyt.
#
calrefbyt = os.path.join(landsat8Folder, var02)
#
# Open image to calculate rows and columns.
#
img = gdal.Open(calrefbyt, gdal.GA_ReadOnly)
if img is None:
print '\nCould not open *calrefbyt*.dat'
print 'Folder not processed: {}\n'.format(landsat8Folder)
continue
var04 = str(img.RasterYSize) # Rows.
var05 = str(img.RasterXSize) # Columns.
#
# Create string to write to batch file.
#
batch_entry = ' '.join((var00, var01, var02, var03, var04, var05,
var06, var07, var08, var09, var10, var11, var12, var13, var14,
var15, var16, var17, var18, var19))
with open(batch_path, 'a') as f:
f.write(batch_entry + '\n')
## print batch_entry
#
# Clean up.
#
img = None
#
# Location.
#
print '\n\n{} created.\nSaved to: {}\n\n'.format(batFilename, batch_path)
else:
print '\nNo SIAM batch file created.\n'
sys.exit()
#
# Landsat-8 or Sentinel-2 clipped mosaic.
#
elif data_type[sensor_type.get()] == 3:
#
# Create list for landsat-8 or sentinel-2 clipped mosaic folder paths.
#
mosaicFiles = []
for dirpath, dirnames, filenames in os.walk(s2_Folder.get(), topdown=True):
for filename in filenames:
if filename.endswith('_clip_mosaic_calrefbyt_lndstlk.dat'):
mosaicFiles.append(os.path.join(dirpath, filename))
print mosaicFiles
#
# Hide the main window.
#
Tkinter.Tk().withdraw()
question = ('Wold you like to process {} mosaics in SIAM?').format(
len(mosaicFiles))
#
# Create the content of the window.
#
messagebox = tkMessageBox.askyesno('SIAM batch creator',
question)
#------------------------------------------------------------------------------#
# Create batch file and launch SIAM or quit #
#------------------------------------------------------------------------------#
if messagebox:
#
# Register all of the GDAL drivers.
#
gdal.AllRegister()
#
# Create empty batch file for SIAM.
#
var06_text = var06.get()
if len(mosaicFiles) > 1:
batFilename = 'SIAM_multiple_batch_{}.bat'.format(var06_text[:-5])
else:
batFilename = 'SIAM_batch_{}.bat'.format(var06_text[:-5])
batch_path = os.path.join(s2_Folder.get(), batFilename)
#
# Convert binary variables and image type identifier from GUI to strings.
#
var00 = var00.get()
var03 = str(var03.get())
var06 = str(image_type[var06.get()])
var07 = str(var07.get())
var09 = str(var09.get())
var10 = str(var10.get())
var11 = str(var11.get())
var12 = str(var12.get())
var13 = str(var13.get())
var14 = str(var14.get())
var15 = str(var15.get())
var16 = str(var16.get())
var17 = str(var17.get())
var18 = str(var18.get())
var19 = str(var19.get())
for mosaicFile in mosaicFiles:
var01 = os.path.dirname(mosaicFile)
#
# Create the folder for SIAM output data if it doesn't exist.
#
siam_output = os.path.join(var01, 'siamoutput')
if not(os.path.exists(siam_output)):
os.mkdir(siam_output)
var08 = siam_output
#
# Fill var02 variable with the calrefbyt filename.
#
var02 = os.path.basename(mosaicFile)
#
# Path to calrefbyt.
#
calrefbyt = mosaicFile
#
# Open image to calculate rows and columns.
#
img = gdal.Open(calrefbyt, gdal.GA_ReadOnly)
if img is None:
print '\nCould not open *calrefbyt*.dat'
print 'Folder not processed: {}\n'.format(os.path.dirname(mosaicFile))
continue
var04 = str(img.RasterYSize) # Rows.
var05 = str(img.RasterXSize) # Columns.
#
# Create string to write to batch file.
#
batch_entry = ' '.join((var00, var01, var02, var03, var04, var05,
var06, var07, var08, var09, var10, var11, var12, var13, var14,
var15, var16, var17, var18, var19))
with open(batch_path, 'a') as f:
f.write(batch_entry + '\n')
## print batch_entry
#
# Clean up.
#
img = None
#
# Location.
#
print '\n\n{} created.\nSaved to: {}\n\n'.format(batFilename, batch_path)
else:
print '\nNo SIAM batch file created.\n'
sys.exit()
#------------------------------------------------------------------------------#
# Launch batch file. #
#------------------------------------------------------------------------------#
## May need to add threading to avoid GUI freezing upon launch.
# Excerpt from Tiede's ArcGIS Python Toolbox to launch SIAM
# myprocess = subprocess.Popen(batch_path)
# myprocess.wait() # We wait for process to finish
# print myprocess.returncode # then we get its returncode.
#------------------------------------------------------------------------------#
# Parameters from Example batch file explained. #
#------------------------------------------------------------------------------#
# (0) C:\SIAM\SIAM_r88v7\SIAM_License_Executables\SIAM_r88v7_Windows.exe [SIAM executable]
# (1) E:\Temp_ha\S2A_OPER_MSI_L1C_TL_MTI__20150813T201603_A000734_T33TUN_N01.03 [location of calibrated files]
# (2) S2A_OPER_MSI_L1C_TL_MTI__20150813T201603_A000734_T33TUN_calrefbyt_lndstlk.dat [calibrated file]
# (3) 0 [Use a binary mask for processing]
# (4) 10980 [rows]
# (5) 10980 [columns]
# (6) 1 [image type: ("LANDSAT_LIKE","1")("SPOT_LIKE","2")("AVHRR_LIKE","3")("VHR_LIKE","4")]
# (7) 1 [Select crisp or fuzzy classification]
# (8) E:\Temp_ha\S2A_OPER_MSI_L1C_TL_MTI__20150813T201603_A000734_T33TUN_N01.03\siamoutput [output folder]
# (9) 0 [Smoke-Plume mask]
# (10) 0 [Cloud Mask]
# (11) 0 [Burnt area mask]
# (12) 1 [Vegetation Binary mask]
# (13) 0 [Vegetation Trinary mask]
# (14) 0 [Baresoil Builtup Trinary mask]
# (15) 0 [Cloud Trinary mask]
# (16) 0 [Water Trinary mask]
# (17) 0 [Shadow Trinary mask]
# (18) 1 [Urban area binary mask]