From c5b654bc3b697e0020811776494ef22f670a0fe6 Mon Sep 17 00:00:00 2001
From: ldam <ldam@passcal.nmt.edu>
Date: Tue, 22 Aug 2023 16:03:58 -0600
Subject: [PATCH] detect_data_type: check is_multiplex for different folders

---
 sohstationviewer/controller/processing.py | 13 ++++++++++---
 tests/controller/test_processing.py       | 16 ++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/sohstationviewer/controller/processing.py b/sohstationviewer/controller/processing.py
index 34da617fe..cbdb5f442 100644
--- a/sohstationviewer/controller/processing.py
+++ b/sohstationviewer/controller/processing.py
@@ -169,9 +169,10 @@ def detect_data_type(list_of_dir: List[str]) -> Optional[str]:
     sign_chan_data_type_dict = get_signature_channels()
 
     dir_data_type_dict = {}
-    is_multiplex = None
+    is_multiplex_dict = {}
     for d in list_of_dir:
         data_type = "Unknown"
+        is_multiplex = None
         for path, subdirs, files in os.walk(d):
             for file_name in files:
                 path2file = Path(path).joinpath(file_name)
@@ -191,10 +192,12 @@ def detect_data_type(list_of_dir: List[str]) -> Optional[str]:
         if is_multiplex is None:
             raise Exception("No channel found for the data set")
 
+        is_multiplex_dict[d] = is_multiplex
         if data_type == "Unknown":
             dir_data_type_dict[d] = "Unknown"
         else:
             dir_data_type_dict[d] = data_type
+    is_multiplex_list = list(set(is_multiplex_dict.values()))
     data_type_list = list(set(dir_data_type_dict.values()))
     if len(data_type_list) > 1:
         dir_data_type_str = json.dumps(dir_data_type_dict)
@@ -204,13 +207,17 @@ def detect_data_type(list_of_dir: List[str]) -> Optional[str]:
                f"{dir_data_type_str}\n\n"
                f"Please have only data that related to each other.")
         raise Exception(msg)
-
+    elif len(is_multiplex_list) > 1:
+        msg = ("There are both multiplexed and non-multiplexed data "
+               "detected.\n\nPlease have only data that related to"
+               " each other.")
+        raise Exception(msg)
     elif data_type_list == ['Unknown']:
         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 data_type_list[0], is_multiplex
+    return data_type_list[0], is_multiplex_list[0]
 
 
 def get_data_type_from_file(
diff --git a/tests/controller/test_processing.py b/tests/controller/test_processing.py
index fb377d015..b6e411b82 100644
--- a/tests/controller/test_processing.py
+++ b/tests/controller/test_processing.py
@@ -256,6 +256,22 @@ class TestDetectDataType(TestCase):
             f"{self.dir2.name}: Q330\n\n"
             f"Please have only data that related to each other.")
 
+    def test_same_data_types_different_multiplex(self):
+        """
+        Test basic functionality of detect_data_type - the given directories
+        contain same data types but different multiplex.
+        """
+        returned_data_types = [('Q330', True), ('Q330', False)]
+        self.mock_get_data_type_from_file.side_effect = returned_data_types
+
+        with self.assertRaises(Exception) as context:
+            detect_data_type([self.dir1.name, self.dir2.name])
+        self.assertEqual(
+            str(context.exception),
+            "There are both multiplexed and non-multiplexed data "
+            "detected.\n\nPlease have only data that related to"
+            " each other.")
+
     def test_unknown_data_type(self):
         """
         Test basic functionality of detect_data_type - can't detect any data
-- 
GitLab