From 6484abba2702bfefb98c024902a8413db5c5370c Mon Sep 17 00:00:00 2001
From: Lan Dam <ldam@passcal.nmt.edu>
Date: Mon, 14 Aug 2023 15:10:01 -0600
Subject: [PATCH] handle no known data detected

---
 documentation/08 _ Select Waveforms.help.md   | 12 +--
 sohstationviewer/controller/processing.py     |  5 +-
 sohstationviewer/view/main_window.py          | 89 +++++++++++--------
 .../plotting/gps_plot/extract_gps_data.py     | 17 +++-
 tests/test_controller/test_processing.py      |  5 +-
 5 files changed, 80 insertions(+), 48 deletions(-)

diff --git a/documentation/08 _ Select Waveforms.help.md b/documentation/08 _ Select Waveforms.help.md
index 56a38d2bb..7f4a3a8d8 100644
--- a/documentation/08 _ Select Waveforms.help.md	
+++ b/documentation/08 _ Select Waveforms.help.md	
@@ -44,10 +44,12 @@ checked,  a warning will be created, "Checked data streams will be ignored
 for RT130 data type."
 
 ## Displaying waveform channels
-If one of TPS or RAW checkboxes aren't checked which means no data need to be
-displayed,  all the waveform selected will be ignored.
-
-To display waveform channels,  user need to check:
+TPS needs to be checked to display Time-Power-Squared of waveform.
+RAW needs to be checked to display actual signal of waveform.
 + <img alt="TPS" src="images/select_waveform/select_TPS.png" height="30" />: to diplay Time-Power-Squared of the selected waveform data 
 + <img alt="RAW" src="images/select_waveform/select_RAW.png" height="30" />: and check RAW to display the actual selected waveform data.
-<br />
\ No newline at end of file
+<br />
+
+If any of waveform is checked but no TPS or RAW is checked,
++ For RT130, the program will read event of the selected data stream.
++ For MSeed, the program will pop up message request user to clear waveform selection or select either TPS or RAW.
\ No newline at end of file
diff --git a/sohstationviewer/controller/processing.py b/sohstationviewer/controller/processing.py
index 13715a429..4c34572b7 100644
--- a/sohstationviewer/controller/processing.py
+++ b/sohstationviewer/controller/processing.py
@@ -186,8 +186,9 @@ def detect_data_type(list_of_dir: List[str]) -> Optional[str]:
         raise Exception(msg)
 
     elif data_type_list == {'Unknown'}:
-        msg = ("There are no known data detected.\n"
-               "Please select different folder(s).")
+        msg = ("There are no known data detected.\n\n"
+               "Do you want to cancel to select different folder(s)\n"
+               "Or continue to read any available mseed file?")
         raise Exception(msg)
 
     return list(dir_data_type_dict.values())[0][0]
diff --git a/sohstationviewer/view/main_window.py b/sohstationviewer/view/main_window.py
index 688d18460..dce8b4657 100755
--- a/sohstationviewer/view/main_window.py
+++ b/sohstationviewer/view/main_window.py
@@ -398,31 +398,41 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
         :rtype: List[str, int]
         """
         req_wf_chans = []
-
-        if self.all_wf_chans_check_box.isChecked():
-            req_mseed_wildcards = ['*']
-            req_dss = ['*']      # all data stream
-        else:
-            req_dss = []
-            req_mseed_wildcards = []
-            for idx, ds_checkbox in enumerate(self.ds_check_boxes):
-                if ds_checkbox.isChecked():
-                    req_dss.append(idx + 1)
-            if self.mseed_wildcard_edit.text().strip() != "":
-                req_mseed_wildcards = self.mseed_wildcard_edit.text(
-                    ).split(",")
-
-        if self.data_type == 'RT130':
-            req_wf_chans = req_dss
-            if req_dss != ['*'] and req_mseed_wildcards != []:
-                msg = 'MSeed Wildcards will be ignored for RT130.'
-                self.processing_log.append((msg, LogType.WARNING))
-        else:
-            req_wf_chans = req_mseed_wildcards
-            if req_mseed_wildcards != ['*'] and req_dss != []:
-                msg = ('Checked data streams will be ignored for '
-                       'none-RT130 data type.')
-                self.processing_log.append((msg, LogType.WARNING))
+        if (self.data_type != 'RT130' and
+                (self.all_wf_chans_check_box.isChecked()
+                 or self.mseed_wildcard_edit.text().strip() != "")
+                and not self.tps_check_box.isChecked()
+                and not self.raw_check_box.isChecked()):
+            raise Exception(
+                "Waveform channels have been selected but there are none of "
+                "TPS or RAW checkboxes checked.\nPlease clear the "
+                "selection of waveform if you don't want to display the data.")
+
+        if self.tps_check_box.isChecked() or self.raw_check_box.isChecked():
+            if self.all_wf_chans_check_box.isChecked():
+                req_mseed_wildcards = ['*']
+                req_dss = ['*']      # all data stream
+            else:
+                req_dss = []
+                req_mseed_wildcards = []
+                for idx, ds_checkbox in enumerate(self.ds_check_boxes):
+                    if ds_checkbox.isChecked():
+                        req_dss.append(idx + 1)
+                if self.mseed_wildcard_edit.text().strip() != "":
+                    req_mseed_wildcards = self.mseed_wildcard_edit.text(
+                        ).split(",")
+
+            if self.data_type == 'RT130':
+                req_wf_chans = req_dss
+                if req_dss != ['*'] and req_mseed_wildcards != []:
+                    msg = 'MSeed Wildcards will be ignored for RT130.'
+                    self.processing_log.append((msg, LogType.WARNING))
+            else:
+                req_wf_chans = req_mseed_wildcards
+                if req_mseed_wildcards != ['*'] and req_dss != []:
+                    msg = ('Checked data streams will be ignored for '
+                           'none-RT130 data type.')
+                    self.processing_log.append((msg, LogType.WARNING))
         return req_wf_chans
 
     def get_requested_soh_chan(self):
@@ -551,13 +561,6 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
         else:
             self.min_gap = None
 
-        # if waveform channels are selected, Event DS will be read from EH/ET
-        # header
-        # rt130_waveform_data_req is to read data for wave form data
-        rt130_waveform_data_req = False
-        if self.raw_check_box.isChecked() or self.tps_check_box.isChecked():
-            rt130_waveform_data_req = True
-
         if self.mseed_wildcard_edit.text().strip() != '':
             try:
                 check_chan_wildcards_format(self.mseed_wildcard_edit.text())
@@ -581,9 +584,21 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
         try:
             self.read_from_file_list()
         except Exception as e:
-            QtWidgets.QMessageBox.warning(self, "Select directory", str(e))
-            self.cancel_loading()
-            return
+            if 'no known data detected' in str(e):
+                msgbox = QtWidgets.QMessageBox()
+                msgbox.setWindowTitle('Do you want to continue?')
+                msgbox.setText(str(e))
+                msgbox.addButton(QtWidgets.QMessageBox.Cancel)
+                msgbox.addButton('Continue', QtWidgets.QMessageBox.YesRole)
+                result = msgbox.exec_()
+                if result == QtWidgets.QMessageBox.Cancel:
+                    self.cancel_loading()
+                    return
+                self.data_type == 'Unknown'
+            else:
+                QtWidgets.QMessageBox.warning(self, "Select directory", str(e))
+                self.cancel_loading()
+                return
 
         dir_size = sum(get_dir_size(str(dir))[0] for dir in self.dir_names)
         if dir_size > constants.BIG_FILE_SIZE:
@@ -630,8 +645,7 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
             read_start=self.start_tm,
             read_end=self.end_tm,
             include_mp123=self.mass_pos_123zne_check_box.isChecked(),
-            include_mp456=self.mass_pos_456uvw_check_box.isChecked(),
-            rt130_waveform_data_req=rt130_waveform_data_req
+            include_mp456=self.mass_pos_456uvw_check_box.isChecked()
         )
 
         self.data_loader.worker.finished.connect(self.data_loaded)
@@ -743,7 +757,6 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
         """
         if self.has_problem:
             return
-        self.clear_plots()
         self.is_plotting_soh = True
         self.plotting_widget.set_colors(self.color_mode)
         self.waveform_dlg.plotting_widget.set_colors(self.color_mode)
diff --git a/sohstationviewer/view/plotting/gps_plot/extract_gps_data.py b/sohstationviewer/view/plotting/gps_plot/extract_gps_data.py
index 35f04bcd3..8950c1c39 100644
--- a/sohstationviewer/view/plotting/gps_plot/extract_gps_data.py
+++ b/sohstationviewer/view/plotting/gps_plot/extract_gps_data.py
@@ -434,8 +434,23 @@ def gps_data_rt130(data_obj: RT130) -> List[GPSPoint]:
 
 @extract_gps_data.register(MSeed)
 def gps_data_mseed(data_obj: MSeed) -> List[GPSPoint]:
-    data_type = detect_data_type([data_obj.dir])
+    try:
+        data_type = detect_data_type([data_obj.dir])
+    except Exception:
+        data_type = 'Unknown'
+
     if data_type == 'Q330':
         return extract_gps_data_q330(data_obj)
     elif data_type == 'Centaur' or data_type == 'Pegasus':
         return extract_gps_data_pegasus_centaur(data_obj, data_type)
+    else:
+        # data_type = "Unknown"
+        try:
+            gps_data = extract_gps_data_q330(data_obj)
+        except KeyError:
+            try:
+                gps_data = extract_gps_data_pegasus_centaur(
+                    data_obj, data_type)
+            except AttributeError:
+                return []
+        return gps_data
diff --git a/tests/test_controller/test_processing.py b/tests/test_controller/test_processing.py
index 0fa881ff8..379d50d17 100644
--- a/tests/test_controller/test_processing.py
+++ b/tests/test_controller/test_processing.py
@@ -370,8 +370,9 @@ class TestDetectDataType(TestCase):
             detect_data_type([self.dir1.name])
         self.assertEqual(
             str(context.exception),
-            "There are no known data detected.\n"
-            "Please select different folder(s).")
+            "There are no known data detected.\n\n"
+            "Do you want to cancel to select different folder(s)\n"
+            "Or continue to read any available mseed file?")
 
 
 class TestGetDataTypeFromFile(TestCase):
-- 
GitLab