-
Notifications
You must be signed in to change notification settings - Fork 22
/
Main.py
828 lines (706 loc) · 31 KB
/
Main.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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
"""
HyperInSPACE Community Processor (HyperCP) is the successor to the
Hyperspectral In situ Support for PACE (HyperInSPACE) project. It is
designed to process above-water, hyperspectral radiometry from
Satlantic/Sea-Bird HyperSAS instruments and TriOS RAMSES instruments.
See README.md for installation instructions and processor guide.
See Changelog.md for software updates.
See HyperCP_Project_guidelines.md and HyperCP_Project_guidelines_APPENDIX.md
for collaboration guidelines and contact information.
See the Discussions tab on GitHub for software support and development
forum.
"""
import argparse
import os
import sys
import time
from PyQt5 import QtCore, QtGui, QtWidgets
# import requests
# from tqdm import tqdm
from Source import PACKAGE_DIR as CODE_HOME
from Source.MainConfig import MainConfig
from Source.Controller import Controller
from Source.ConfigFile import ConfigFile
from Source.ConfigWindow import ConfigWindow
from Source.GetAnc import GetAnc
from Source.SeaBASSHeader import SeaBASSHeader
from Source.SeaBASSHeaderWindow import SeaBASSHeaderWindow
from Source.Utilities import Utilities
VERSION = "1.2.8"
class Window(QtWidgets.QWidget):
"""Window is the main GUI container"""
def __init__(self, parent=None):
self.inputDirectory = ""
self.outputDirectory = ""
self.ancFileDirectory = ""
super().__init__(parent)
# self.setStyleSheet("background-color: #e3e6e1;")
# Create - if inexistent - directories Plots, Config and Logs
hypercpDirs = ["Plots", "Config", "Logs"]
for directory in hypercpDirs:
dirPath = os.path.join(CODE_HOME, directory)
if not os.path.exists(dirPath):
os.makedirs(dirPath)
# Confirm that core data files are in place. Download if necessary.
fpfZhang = os.path.join(CODE_HOME, "Data", "Zhang_rho_db.mat")
if not os.path.exists(fpfZhang):
Utilities.downloadZhangDB(fpfZhang)
self.initUI()
def initUI(self):
"""Initialize the user interface"""
# Main window configuration restore
MainConfig.loadConfig(
MainConfig.fileName, VERSION
) # VERSION in case it has to make new
MainConfig.settings["version"] = VERSION # VERSION to update if necessary
# Banner
banner = QtWidgets.QLabel(self)
# pixmap = QtGui.QPixmap('./Data/banner.jpg')
# pixmap = QtGui.QPixmap('./Data/Img/with_background_530x223.png')
pixmap = QtGui.QPixmap(
os.path.join(CODE_HOME, "Data", "Img", "banner_530x151.png")
)
banner.setPixmap(pixmap)
banner.setAlignment(QtCore.Qt.AlignCenter)
# Configuration File section
configLabel = QtWidgets.QLabel("Select/Create Configuration File", self)
configLabel_font = configLabel.font()
configLabel_font.setPointSize(10)
configLabel_font.setBold(True)
configLabel.setFont(configLabel_font)
self.fsm = QtWidgets.QFileSystemModel()
self.fsm.setNameFilters(["*.cfg"])
self.fsm.setNameFilterDisables(False) # This activates the Filter (on Win10)
self.fsm.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.configComboBox = QtWidgets.QComboBox(self)
self.configComboBox.setModel(self.fsm)
self.configComboBox.setRootModelIndex(
self.fsm.setRootPath(os.path.join(CODE_HOME, "Config"))
)
self.fsm.directoryLoaded.connect(self.on_directoryLoaded)
self.configComboBox.currentTextChanged.connect(self.comboBox1Changed)
self.configNewButton = QtWidgets.QPushButton("New", self)
self.configEditButton = QtWidgets.QPushButton("Edit", self)
self.configDeleteButton = QtWidgets.QPushButton("Delete", self)
self.configNewButton.clicked.connect(self.configNewButtonPressed)
self.configEditButton.clicked.connect(self.configEditButtonPressed)
self.configDeleteButton.clicked.connect(self.configDeleteButtonPressed)
self.inDirLabel = QtWidgets.QLabel("Input Data Parent Directory", self)
self.inputDirectory = MainConfig.settings["inDir"]
self.inDirButton = QtWidgets.QPushButton(self.inputDirectory, self)
self.inDirButton.clicked.connect(self.inDirButtonPressed)
self.outDirLabel = QtWidgets.QLabel("Output Directory", self)
self.outputDirectory = MainConfig.settings["outDir"]
self.outDirButton = QtWidgets.QPushButton(self.outputDirectory, self)
self.outDirButton.clicked.connect(self.outDirButtonPressed)
# self.outDirButton.setGeometry(200, 150, 100, 40)
self.outDirButton.resize(50, 100)
self.outInDirButton = QtWidgets.QPushButton("^^^ Mimic Input Dir. vvv", self)
self.outInDirButton.clicked.connect(self.outInDirButtonPressed)
self.ancFileLabel = QtWidgets.QLabel(
"Ancillary Data File (SeaBASS format; MUST USE UTC)"
)
self.ancFileLineEdit = QtWidgets.QLineEdit()
self.ancFileLineEdit.setText(str(MainConfig.settings["ancFile"]))
self.ancFileDirectory = MainConfig.settings["ancFileDir"]
self.ancAddButton = QtWidgets.QPushButton("Add", self)
self.ancRemoveButton = QtWidgets.QPushButton("Remove", self)
self.ancAddButton.clicked.connect(self.ancAddButtonPressed)
self.ancRemoveButton.clicked.connect(self.ancRemoveButtonPressed)
singleLevelLabel = QtWidgets.QLabel("Single-Level Processing", self)
singleLevelLabel_font = singleLevelLabel.font()
singleLevelLabel_font.setPointSize(14)
singleLevelLabel_font.setBold(True)
singleLevelLabel.setFont(singleLevelLabel_font)
self.singleL1aButton = QtWidgets.QPushButton(
"Level 0 (Raw) --> Level 1A (HDF5)", self
)
self.singleL1aqcButton = QtWidgets.QPushButton("L1A --> L1AQC", self)
self.singleL1bButton = QtWidgets.QPushButton("L1AQC --> L1B", self)
self.singleL1bqcButton = QtWidgets.QPushButton("L1B --> L1BQC", self)
self.singleL2Button = QtWidgets.QPushButton("L1BQC --> L2", self)
self.singleL1aButton.clicked.connect(self.singleL1aClicked)
self.singleL1aqcButton.clicked.connect(self.singleL1aqcClicked)
self.singleL1bButton.clicked.connect(self.singleL1bClicked)
self.singleL1bqcButton.clicked.connect(self.singleL1bqcClicked)
self.singleL2Button.clicked.connect(self.singleL2Clicked)
multiLevelLabel = QtWidgets.QLabel("Multi-Level Processing", self)
multiLevelLabel_font = multiLevelLabel.font()
multiLevelLabel_font.setPointSize(14)
multiLevelLabel_font.setBold(True)
multiLevelLabel.setFont(multiLevelLabel_font)
self.multi2Button = QtWidgets.QPushButton("Raw (BIN) ----->> L2 (HDF5)", self)
self.multi2Button.clicked.connect(self.multi2Clicked)
popQueryLabel = QtWidgets.QLabel(
"Suppress pop-up window on processing fail?", self
)
self.popQueryCheckBox = QtWidgets.QCheckBox("", self)
if int(MainConfig.settings["popQuery"]) == 1:
self.popQueryCheckBox.setChecked(True)
self.popQueryCheckBoxUpdate()
self.popQueryCheckBox.clicked.connect(self.popQueryCheckBoxUpdate)
########################################################################################
# Add QtWidgets to the Window
########################################################################################
# vBox = vertical box layout
vBox = QtWidgets.QVBoxLayout()
vBox.addWidget(banner)
vBox.addWidget(configLabel)
vBox.addWidget(self.configComboBox)
# Horizontal Box; New Edit Delete Buttons
configHBox = QtWidgets.QHBoxLayout()
configHBox.addWidget(self.configNewButton)
configHBox.addWidget(self.configEditButton)
configHBox.addWidget(self.configDeleteButton)
vBox.addLayout(configHBox)
# vBox.addStretch(1) # allows vBox to stretch open here when resized
inDataHBox = QtWidgets.QHBoxLayout()
inDataHBox.addWidget(self.inDirLabel)
inDataHBox.addWidget(self.inDirButton)
vBox.addLayout(inDataHBox)
vBox.addWidget(self.outInDirButton)
outHBox = QtWidgets.QHBoxLayout()
outHBox.addWidget(self.outDirLabel)
outHBox.addWidget(self.outDirButton)
vBox.addLayout(outHBox)
vBox.addWidget(self.ancFileLabel)
vBox.addWidget(self.ancFileLineEdit)
# vBox.addStretch(1)
ancHBox = QtWidgets.QHBoxLayout()
ancHBox.addWidget(self.ancAddButton)
ancHBox.addWidget(self.ancRemoveButton)
vBox.addLayout(ancHBox)
singleHBox = QtWidgets.QHBoxLayout()
singleHBox.addWidget(singleLevelLabel)
singleVBox = QtWidgets.QVBoxLayout()
singleVBox.addWidget(self.singleL1aButton)
singleVBox.addWidget(self.singleL1aqcButton)
singleVBox.addWidget(self.singleL1bButton)
singleVBox.addWidget(self.singleL1bqcButton)
singleVBox.addWidget(self.singleL2Button)
singleHBox.addLayout(singleVBox)
vBox.addLayout(singleHBox)
multiHBox = QtWidgets.QHBoxLayout()
multiHBox.addWidget(multiLevelLabel)
multiHBox.addWidget(self.multi2Button)
vBox.addLayout(multiHBox)
popQueryBox = QtWidgets.QHBoxLayout()
popQueryBox.addWidget(popQueryLabel)
popQueryBox.addWidget(self.popQueryCheckBox)
vBox.addLayout(popQueryBox)
# vBox.setContentsMargins(0, 0, 0, 0)
# vBox.addStretch(1)
self.setLayout(vBox)
# self.setGeometry(300, 300, 290, 600)
self.setWindowTitle(f"HyperCP Main v{MainConfig.settings['version']}")
# self.setFixedSize(self.sizeHint())
self.show()
########################################################################################
# Build functionality methods
def on_directoryLoaded(self):
index = self.configComboBox.findText(MainConfig.settings["cfgFile"])
self.configComboBox.setCurrentIndex(index)
# MainConfig.saveConfig(MainConfig.fileName)
def comboBox1Changed(self, value):
MainConfig.settings["cfgFile"] = value
index = self.configComboBox.findText(MainConfig.settings["cfgFile"])
self.configComboBox.setCurrentIndex(index)
print("MainConfig: Configuration file changed to: ", value)
# MainConfig.saveConfig(MainConfig.fileName)
def configNewButtonPressed(self):
print("New Config Dialogue")
fileName, ok = QtWidgets.QInputDialog.getText(
self, "New Config File", "Enter File Name"
)
if ok:
if not fileName.endswith(".cfg"):
fileName = fileName + ".cfg"
fp = os.path.join(CODE_HOME, "Config", fileName)
if os.path.exists(fp):
ret = QtWidgets.QMessageBox.question(
self,
"File Exists",
"Overwrite Config File?",
QtWidgets.QMessageBox.Yes
| QtWidgets.QMessageBox.No
| QtWidgets.QMessageBox.Cancel,
QtWidgets.QMessageBox.Cancel,
)
if ret == QtWidgets.QMessageBox.Yes:
print("Overwriting Config File:", fileName)
ConfigFile.createDefaultConfig(fileName, 1)
MainConfig.settings["cfgFile"] = ConfigFile.filename
seaBASSHeaderFileName = ConfigFile.settings["seaBASSHeaderFileName"]
print("Overwriting SeaBASSHeader File: ", seaBASSHeaderFileName)
SeaBASSHeader.createDefaultSeaBASSHeader(seaBASSHeaderFileName)
else:
print("Create New Config File: ", fileName)
ConfigFile.createDefaultConfig(fileName, 1)
MainConfig.settings["cfgFile"] = ConfigFile.filename
seaBASSHeaderFileName = ConfigFile.settings["seaBASSHeaderFileName"]
print("Creating New SeaBASSHeader File: ", seaBASSHeaderFileName)
SeaBASSHeader.createDefaultSeaBASSHeader(seaBASSHeaderFileName)
MainConfig.saveConfig(MainConfig.fileName)
def configEditButtonPressed(self):
print("Edit Config Dialogue")
MainConfig.saveConfig(MainConfig.fileName)
configFileName = self.configComboBox.currentText()
inputDir = self.inputDirectory
configPath = os.path.join(CODE_HOME, "Config", configFileName)
if os.path.isfile(configPath):
ConfigFile.loadConfig(configFileName)
configDialog = ConfigWindow(configFileName, inputDir, self)
configDialog.show()
else:
message = "Not a Config File: " + configFileName
QtWidgets.QMessageBox.critical(self, "Error", message)
def configDeleteButtonPressed(self):
print("Delete Config Dialogue")
configFileName = self.configComboBox.currentText()
configPath = os.path.join(CODE_HOME, "Config", configFileName)
if os.path.isfile(configPath):
configDeleteMessage = "Delete " + configFileName + "?"
reply = QtWidgets.QMessageBox.question(
self,
"Message",
configDeleteMessage,
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No,
)
if reply == QtWidgets.QMessageBox.Yes:
ConfigFile.deleteConfig(configFileName)
else:
message = "Not a Config File: " + configFileName
QtWidgets.QMessageBox.critical(self, "Error", message)
MainConfig.saveConfig(MainConfig.fileName)
def inDirButtonPressed(self):
temp = self.inputDirectory
self.inputDirectory = QtWidgets.QFileDialog.getExistingDirectory(
self, "Choose Directory.", self.inputDirectory
)
if self.inputDirectory == "":
self.inputDirectory = temp
if self.inputDirectory == "":
self.inputDirectory = os.path.join(CODE_HOME, "/Data")
print("Data input directory changed: ", self.inputDirectory)
self.inDirButton.setText(self.inputDirectory)
MainConfig.settings["inDir"] = self.inputDirectory
MainConfig.saveConfig(MainConfig.fileName)
return self.inputDirectory
def outDirButtonPressed(self):
temp = self.outputDirectory
self.outputDirectory = QtWidgets.QFileDialog.getExistingDirectory(
self,
"SUBDIRECTORIES FOR DATA LEVELS WILL BE CREATED HERE AUTOMATICALLY.",
self.outputDirectory,
)
if self.outputDirectory == "":
self.outputDirectory = temp
if self.outputDirectory == "":
self.outputDirectory = os.path.join(CODE_HOME, "Data", "Sample_Data")
print("Data output directory changed: ", self.outputDirectory)
print("NOTE: Subdirectories for data levels will be created here")
print(" automatically, unless they already exist.")
self.outDirButton.setText(self.outputDirectory)
MainConfig.settings["outDir"] = self.outputDirectory
MainConfig.saveConfig(MainConfig.fileName)
return self.outputDirectory
def outInDirButtonPressed(self):
self.outputDirectory = self.inputDirectory
print("Data output directory changed: ", self.outputDirectory)
print("NOTE: Subdirectories for data levels will be created here")
print(" automatically, unless they already exist.")
self.outDirButton.setText(self.outputDirectory)
MainConfig.settings["outDir"] = self.outputDirectory
MainConfig.saveConfig(MainConfig.fileName)
return self.outputDirectory
def ancAddButtonPressed(self):
print("Ancillary File Add Dialogue")
if self.ancFileDirectory == "":
self.ancFileDirectory = (
self.inputDirectory
) # Reverts to Input directory first
if self.ancFileDirectory == "":
self.ancFileDirectory = os.path.join(
CODE_HOME, "Data", "Sample_Data"
) # Falls back to ./Data/Sample_Data
fnames = QtWidgets.QFileDialog.getOpenFileNames(
self, "Select Ancillary Data File", self.ancFileDirectory
)
if any(fnames):
print(fnames)
if len(fnames[0]) == 1:
self.ancFileLineEdit.setText(fnames[0][0])
MainConfig.settings["ancFile"] = fnames[0][0] # unclear why this sometimes does not "take" the first time
MainConfig.saveConfig(MainConfig.fileName)
def ancRemoveButtonPressed(self):
print("Wind File Remove Dialogue")
self.ancFileLineEdit.setText("")
MainConfig.settings["ancFile"] = ""
self.ancFileDirectory = ""
MainConfig.saveConfig(MainConfig.fileName)
def processSingle(self, lvl):
print("Process Single-Level")
t0Single = time.time()
# Load Config file
configFileName = self.configComboBox.currentText()
MainConfig.settings["cfgPath"] = os.path.join(
CODE_HOME, "Config", configFileName)
if not os.path.isfile(MainConfig.settings["cfgPath"]):
message = "Not valid Config File: " + configFileName
QtWidgets.QMessageBox.critical(self, "Error", message)
return
MainConfig.saveConfig(MainConfig.fileName)
ConfigFile.loadConfig(configFileName)
seaBASSHeaderFileName = ConfigFile.settings["seaBASSHeaderFileName"]
SeaBASSHeader.loadSeaBASSHeader(seaBASSHeaderFileName)
InstrumentType = ConfigFile.settings["SensorType"]
# To check instrument type
if InstrumentType.lower() == "trios":
flag_Trios = 1
elif InstrumentType.lower() == "seabird":
flag_Trios = 0
else:
print("Error in configuration file: Sensor type not specified")
sys.exit()
# Select data files
# if not self.inputDirectory[0]:
# print("Bad input parent directory.")
# return
if lvl == "L1A":
inLevel = "raw"
elif lvl == "L1AQC":
inLevel = "L1A"
elif lvl == "L1B":
inLevel = "L1AQC"
elif lvl == "L1BQC":
inLevel = "L1B"
elif lvl == "L2":
inLevel = "L1BQC"
else:
inLevel = None
# Check for subdirectory associated with level chosen
subInputDir = os.path.join(self.inputDirectory, inLevel)
if os.path.exists(subInputDir):
openFileNames = QtWidgets.QFileDialog.getOpenFileNames(
self, "Open File", subInputDir
)
fileNames = openFileNames[0] # The first element is the whole list
else:
openFileNames = QtWidgets.QFileDialog.getOpenFileNames(
self, "Open File", self.inputDirectory
)
fileNames = openFileNames[0] # The first element is the whole list
print("Files:", openFileNames)
if not fileNames:
return
print("Process Calibration Files")
calFiles = ConfigFile.settings["CalibrationFiles"]
if flag_Trios == 0:
calibrationMap = Controller.processCalibrationConfig(
configFileName, calFiles
)
else:
calibrationMap = Controller.processCalibrationConfigTrios(calFiles)
# calibrationMap = 0
if not calibrationMap.keys():
print(
"No calibration files found. "
"Check Config directory for your instrument files."
)
return
print("Output Directory:", os.path.abspath(self.outputDirectory))
if not self.outputDirectory[0]:
print("Bad output directory.")
return
Controller.processFilesSingleLevel(
self.outputDirectory, fileNames, calibrationMap, lvl, flag_Trios
)
t1Single = time.time()
print(f"Time elapsed: {str(round((t1Single-t0Single)/60))} minutes")
def closeEvent(self, event):
# reply = QtWidgets.QMessageBox.question(self, 'Window Close', 'Are you sure you want to close the window?',
# QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
# if reply == QtWidgets.QMessageBox.Yes:
configFileName = self.configComboBox.currentText()
MainConfig.settings["cfgPath"] = os.path.join(
CODE_HOME, "Config", configFileName)
MainConfig.saveConfig(MainConfig.fileName)
event.accept()
# else:
# event.ignore()
def singleL1aClicked(self):
self.processSingle("L1A")
def singleL1aqcClicked(self):
self.processSingle("L1AQC")
def singleL1bClicked(self):
self.processSingle("L1B")
def singleL1bqcClicked(self):
self.processSingle("L1BQC")
def singleL2Clicked(self):
self.processSingle("L2")
def processMulti(self):
print("Process Multi-Level")
MainConfig.saveConfig(MainConfig.fileName)
t0Multi = time.time()
# Load Config file
configFileName = self.configComboBox.currentText()
configPath = os.path.join(CODE_HOME, "Config", configFileName)
MainConfig.settings["cfgPath"] = configPath
if not os.path.isfile(configPath):
message = "Not valid Config File: " + configFileName
QtWidgets.QMessageBox.critical(self, "Error", message)
return
ConfigFile.loadConfig(configFileName)
# Select data files
if not self.inputDirectory[0]:
print("Bad input directory.")
return
openFileNames = QtWidgets.QFileDialog.getOpenFileNames(
self, "Open File", self.inputDirectory
)
print("Files:", openFileNames)
if not openFileNames[0]:
return
fileNames = openFileNames[0]
print("Output Directory:", self.outputDirectory)
if not self.outputDirectory:
return
InstrumentType = ConfigFile.settings["SensorType"]
calFiles = ConfigFile.settings["CalibrationFiles"]
# To check instrument type
if InstrumentType.lower() == "trios":
flag_Trios = 1
calibrationMap = Controller.processCalibrationConfigTrios(calFiles)
elif InstrumentType.lower() == "seabird":
flag_Trios = 0
print("Process Calibration Files")
filename = ConfigFile.filename
calibrationMap = Controller.processCalibrationConfig(filename, calFiles)
else:
print("Error in configuration file: Sensor type not specified")
sys.exit()
Controller.processFilesMultiLevel(
self.outputDirectory, fileNames, calibrationMap, flag_Trios
)
t1Multi = time.time()
print(f"Time elapsed: {str(round((t1Multi-t0Multi)/60))} Minutes")
def multi2Clicked(self):
"""Sneaky work around until I can pass signals btw. windows"""
self.processMulti()
def popQueryCheckBoxUpdate(self):
print("Main - popQueryCheckBoxUpdate")
MainConfig.settings["popQuery"] = int(self.popQueryCheckBox.isChecked())
MainConfig.saveConfig(MainConfig.fileName)
# def saveButtonClicked(self):
# print("Main - saveButtonClicked")
# MainConfig.saveConfig(MainConfig.fileName)
###################################################################################
class Command:
"""Class for batching without using the GUI. Scripted calls preferred, but
direct call possible."""
def __init__(
self,
configFP,
from_level,
iFile,
dataDirectory,
to_level,
anc='',
processMultiLevel=False,
):
self.configFilename = configFP
self.inputFile = iFile
self.inputDirectory = os.path.join(dataDirectory, from_level)
self.outputDirectory = dataDirectory
self.level = to_level
self.ancFile = anc
super().__init__()
# Confirm that core data files are in place. Download if necessary.
fpfZhang = os.path.join(CODE_HOME, "Data", "Zhang_rho_db.mat")
if not os.path.exists(fpfZhang):
Utilities.downloadZhangDB(fpfZhang, force=True)
# Create a default main config to be filled with cmd argument
# to avoid reading the one generated with the GUI
MainConfig.createDefaultConfig("cmdline_main.config", VERSION)
MainConfig.fileName = "cmdline_main.config"
# Update main configuration path with cmd line input
MainConfig.settings["cfgFile"] = os.path.split(configFP)[-1]
MainConfig.settings["cfgPath"] = configFP
MainConfig.settings["version"] = VERSION
MainConfig.settings["ancFile"] = self.ancFile
MainConfig.settings["outDir"] = self.outputDirectory
if isinstance(iFile,list):
# Process the entire directory of the first file in the list
MainConfig.settings["inDir"] = os.path.dirname(iFile[0])
else:
# Single file
MainConfig.settings["inDir"] = os.path.dirname(iFile)
# Now make it a list as it is expected to be
# inputFile = [inputFile]
# No GUI used: error message are display in prompt and not in graphical window
MainConfig.settings["popQuery"] = 1 # 1 suppresses popup
MainConfig.saveConfig(MainConfig.fileName)
print("MainConfig - Config updated with cmd line arguments")
ConfigFile.loadConfig(self.configFilename)
InstrumentType = ConfigFile.settings["SensorType"]
calFiles = ConfigFile.settings["CalibrationFiles"]
if InstrumentType.lower() == "trios":
flag_Trios = 1
calibrationMap = Controller.processCalibrationConfigTrios(calFiles)
elif InstrumentType.lower() == "seabird":
flag_Trios = 0
print("Process Calibration Files")
filename = ConfigFile.filename
calibrationMap = Controller.processCalibrationConfig(filename, calFiles)
else:
print("Error in configuration file: Sensor type not specified")
sys.exit()
# Update the SeaBASS .hdr file in case changes were made to the configuration without using the GUI
SeaBASSHeader.loadSeaBASSHeader(ConfigFile.settings['seaBASSHeaderFileName'])
SeaBASSHeaderWindow.configUpdateButtonPressed(self, 'config1')
SeaBASSHeader.saveSeaBASSHeader(ConfigFile.settings['seaBASSHeaderFileName'])
if processMultiLevel:
if InstrumentType.lower() == "trios" and to_level == "L1A":
Controller.processFilesMultiLevel(
self.outputDirectory, iFile, calibrationMap, flag_Trios
)
else:
Controller.processFilesMultiLevel(
self.outputDirectory, [iFile], calibrationMap, flag_Trios
)
else:
# processSingleLevel is only prepared for a singleton file at a time
Controller.processSingleLevel(
self.outputDirectory, iFile, calibrationMap, to_level, flag_Trios
)
if __name__ == "__main__":
# Arguments declaration
######## This section is not up to date. Scripted calls to Command are preferred. ##########
parser = argparse.ArgumentParser(description="Arguments description")
# Mandatory arguments
required = parser.add_argument_group("Required arguments")
required.add_argument(
"-cmd",
action="store_true",
dest="cmd",
help="To use for commandline mode. If not given, the GUI mode is run",
default=None,
)
required.add_argument(
"-c",
action="store",
dest="configFilePath",
help="Path of the configuration file",
default=None,
type=str,
)
required.add_argument(
"-i",
action="store",
dest="inputFile",
help="Path of the input file",
default=None,
type=str,
)
required.add_argument(
"-o",
action="store",
dest="outputDirectory",
help="Path of the output folder",
default=None,
type=str,
)
required.add_argument(
"-l",
action="store",
dest="level",
help="Level of the generated file. e.g.: Computing RAW to L1A means -l L1A",
choices=["L1A", "L1AQC", "L1B", "L1BQC", "L2"],
default=None,
type=str,
)
required.add_argument(
"-m",
action="store",
dest="multiLevel",
help="Single or multilevel processing (L0-L2): -m False",
choices=["True", "False"],
default="False",
type=str,
)
required.add_argument(
"-a",
action="store",
dest="ancFile",
help="Path of the ancillary file",
default=None,
type=str,
)
required.add_argument(
"-u",
action="store",
dest="username",
help="Username of the account on https://oceancolor.gsfc.nasa.gov/",
default=None,
type=str,
)
required.add_argument(
"-p",
action="store",
dest="password",
help="Password of the account on https://oceancolor.gsfc.nasa.gov/",
default=None,
type=str,
)
args = parser.parse_args()
# If the commandline option is given, check if all needed information are given
if args.cmd and (
args.configFilePath is None
or args.inputFile is None
or args.multiLevel is None
or args.outputDirectory is None
or args.level is None
):
parser.error(
"-cmd requires -c config -i inputFile -m multiLevel -o outputDirectory -l processingLevel"
)
# If the commandline option is given, check if all needed information are given
if (
args.cmd
and args.level == "L1BQC"
and (args.username is None or args.password is None)
):
parser.error(
"L1BQC processing requires username and password for https://oceancolor.gsfc.nasa.gov/"
)
# We store all arguments in variables
cmd = args.cmd
configFilePath = args.configFilePath
inputFile = args.inputFile
outputDirectory = args.outputDirectory
level = args.level
ancFile = args.ancFile
username = args.username
password = args.password
multiLevel = args.multiLevel
# Close splashscreen
try:
import platform
if platform.system() in ["Windows", "Linux"]:
import pyi_splash
pyi_splash.close()
except ImportError:
pass
# If the cmd argument is given, run the Command class without the GUI
if cmd:
os.environ["HYPERINSPACE_CMD"] = "TRUE" # Must be a string
if not (args.username is None or args.password is None):
# Only for L2 processing set credentials
GetAnc.userCreds(username, password)
Command(configFilePath, inputFile, multiLevel, outputDirectory, level, ancFile)
else:
os.environ["HYPERINSPACE_CMD"] = "FALSE" # Must be a string
app = QtWidgets.QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())