From 02b2e29519ec5b4b5b0555f61c0a735ec3d2f957 Mon Sep 17 00:00:00 2001
From: Kien Le <kienle@passcal.nmt.edu>
Date: Fri, 10 Feb 2023 11:15:06 -0700
Subject: [PATCH] Fix program breaking when plotting waveform or TPS with no
 channel chosen

---
 .../plotting/time_power_squared_dialog.py     | 43 +++++++++++--------
 .../view/plotting/waveform_dialog.py          | 39 +++++++++++------
 2 files changed, 51 insertions(+), 31 deletions(-)

diff --git a/sohstationviewer/view/plotting/time_power_squared_dialog.py b/sohstationviewer/view/plotting/time_power_squared_dialog.py
index 4ba8ae4fa..98570b999 100755
--- a/sohstationviewer/view/plotting/time_power_squared_dialog.py
+++ b/sohstationviewer/view/plotting/time_power_squared_dialog.py
@@ -97,9 +97,9 @@ class TimePowerSquaredWidget(plotting_widget.PlottingWidget):
         self.min_x = max(data_time[0], start_tm)
         self.max_x = min(data_time[1], end_tm)
 
-        if self.axes:
-            self.plotting_axes.fig.clear()
-            self.draw()
+        self.plotting_axes.fig.clear()
+        self.draw()
+
         self.date_mode = self.parent.date_format.upper()
         if waveform_data == {}:
             title = "NO WAVEFORM DATA TO DISPLAY."
@@ -112,17 +112,12 @@ class TimePowerSquaredWidget(plotting_widget.PlottingWidget):
 
         if waveform_data == {}:
             self.draw()
+            self.clean_up('NO DATA')
             return
         self.plotting_data1 = waveform_data
         self.min_x = max(data_time[0], start_tm)
         self.max_x = min(data_time[1], end_tm)
-        self.plot_total = len(waveform_data)
-        self.plotting_bot = const.BOTTOM
-        self.plotting_bot_pixel = const.BOTTOM_PX
-        self.axes = []
-        self.rulers = []
-        self.zoom_marker1s = []
-        self.zoom_marker2s = []
+
         self.each_day5_min_list = get_each_day_5_min_list(self.min_x,
                                                           self.max_x)
         for chan_id in self.plotting_data1:
@@ -168,16 +163,28 @@ class TimePowerSquaredWidget(plotting_widget.PlottingWidget):
             self.axes.append(ax)
         self.processed_channels.append(chan_id)
         if len(self.processed_channels) == len(self.channels):
-            if chan_id == '':
-                stopped_msg = 'TPS plot stopped.'
-                display_tracking_info(self.tracking_box, stopped_msg)
-            else:
-                finished_msg = 'TPS plot finished.'
-                display_tracking_info(self.tracking_box, finished_msg)
-                self.done()
-            self.stopped.emit()
+            self.clean_up(chan_id)
         self.finished_lock.unlock()
 
+    def clean_up(self, chan_id):
+        """
+        Clean up after all available waveform channels have been stopped or
+        plotted. The cleanup steps are as follows.
+            Display a finished message
+            Add finishing touches to the plot
+            Emit the stopped signal of the widget
+        """
+
+        if chan_id == '':
+            msg = 'TPS plot stopped.'
+        else:
+            msg = 'TPS plot finished.'
+            if chan_id != 'NO DATA':
+                self.done()
+
+        display_tracking_info(self.tracking_box, msg)
+        self.stopped.emit()
+
     def done(self):
         """Add finishing touches to the plot and display it on the screen."""
         self.set_legend()
diff --git a/sohstationviewer/view/plotting/waveform_dialog.py b/sohstationviewer/view/plotting/waveform_dialog.py
index dff29749e..83d7ba6e9 100755
--- a/sohstationviewer/view/plotting/waveform_dialog.py
+++ b/sohstationviewer/view/plotting/waveform_dialog.py
@@ -8,6 +8,7 @@ from PySide2 import QtCore, QtWidgets
 
 from sohstationviewer.view.plotting.waveform_processor import \
     WaveformChannelProcessor
+from sohstationviewer.view.util.enums import LogType
 from sohstationviewer.view.util.plot_func_names import plot_functions
 from sohstationviewer.view.plotting.plotting_widget import plotting_widget
 
@@ -130,6 +131,8 @@ class WaveformWidget(plotting_widget.PlottingWidget):
                                  waveform_data, mass_pos_data)
             if not ret:
                 self.draw()
+                self.clean_up()
+                self.finished.emit()
                 return
             self.create_waveform_channel_processors()
             self.process_channel()
@@ -228,23 +231,33 @@ class WaveformWidget(plotting_widget.PlottingWidget):
             channel_processor = self.data_processors[0]
             self.thread_pool.start(channel_processor)
         except IndexError:
-            self.plot_mass_pos_channels()
-            if self.is_first_plotting:
-                self.done()
-                finished_msg = 'Waveform plot finished'
-            else:
-                super().set_lim()
-                self.draw()
-                finished_msg = 'Zooming in finished'
-
-            display_tracking_info(self.tracking_box, finished_msg, 'info')
-
-            self.is_working = False
-            self.is_first_plotting = False
+            self.clean_up()
 
         except Exception as e:
             print(e)
 
+    def clean_up(self):
+        """
+        Clean up after all available waveform channels have been plotted. The
+        cleanup steps are as follows.
+            Plot the mass position channels
+            Display a finish message
+            Reset all internal flags
+        """
+        self.plot_mass_pos_channels()
+        if self.is_first_plotting:
+            self.done()
+            finished_msg = 'Waveform plot finished'
+        else:
+            super().set_lim()
+            self.draw()
+            finished_msg = 'Zooming in finished'
+
+        display_tracking_info(self.tracking_box, finished_msg, LogType.INFO)
+
+        self.is_working = False
+        self.is_first_plotting = False
+
     def plot_single_channel(self, c_data, chan_id):
         """
         Plot the channel chan_id.
-- 
GitLab