# ============================================================================== #
    # mainwindow.py - AcouSed                                   #
    # Copyright (C) 2024  INRAE                                              #
    #                                                                        #
    # This program is free software: you can redistribute it and/or modify   #
    # it under the terms of the GNU General Public License as published by   #
    # the Free Software Foundation, either version 3 of the License, or      #
    # (at your option) any later version.                                    #
    #                                                                        #
    # This program is distributed in the hope that it will be useful,        #
    # but WITHOUT ANY WARRANTY; without even the implied warranty of         #
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
    # GNU General Public License for more details.                           #
    #                                                                        #
    # You should have received a copy of the GNU General Public License      #
    # along with this program.  If not, see <https://www.gnu.org/licenses/>. #

#                                                      by Brahim MOUDJED #
# ============================================================================== #

# -*- coding: utf-8 -*-

from cProfile import label

from PyQt5.QtWidgets import (
    QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QGroupBox,
    QLabel, QSpinBox,  QComboBox, QLineEdit, QSlider,
    QGridLayout, QMessageBox, QScrollArea, QFileDialog,
    QSpacerItem, QSizePolicy
)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import (
    Qt, QCoreApplication, QEvent, pyqtSignal, QPropertyAnimation,
    QSize
)

import numpy as np
from copy import deepcopy
from astropy.convolution import convolve

import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolBar
from matplotlib.colors import LogNorm, BoundaryNorm
from os import path

from View.show_popup_combobox import ComboBoxShowPopUpWindow
from View.plot_noise_window import PlotNoiseWindow

import Translation.constant_string as cs

from Model.acoustic_data_loader import AcousticDataLoader

import settings as stg

_translate = QCoreApplication.translate


class SignalProcessingTab(QWidget):

    ''' This class generates the Signal Processing Tab '''

    FillCombobox = pyqtSignal()

    def __init__(self, widget_tab):
        super().__init__()

        self.path_icon = "./icons/"
        self.icon_folder = QIcon(self.path_icon + "folder.png")
        self.icon_triangle_left = QIcon(self.path_icon + "triangle_left.png")
        self.icon_triangle_right = QIcon(self.path_icon + "triangle_right.png")
        self.icon_triangle_left_to_begin = QIcon(self.path_icon + "triangle_left_to_begin.png")
        self.icon_triangle_right_to_end = QIcon(self.path_icon + "triangle_right_to_end.png")
        self.icon_update = QIcon(self.path_icon + "update.png")
        self.icon_clear = QIcon(self.path_icon + "clear.png")
        self.icon_apply = QIcon(self.path_icon + "circle_green_arrow_right.png")

        ### --- General layout of widgets ---

        #   |-----------------|     |------------|      |-----------------|
        #   |   Data to be    |     |  Plot SNR  |      |  Pre-processing |
        #   |  pre-processed  |     |------------|      |     options     |
        #   |-----------------|                         |-----------------|
        #   |------------------------------------------------------------------------|
        #   |-----------------------|  |-----------------------|  |------------------|
        #   |   Plot pre-processed  |  |   Plot pre-processed  |  |   Pre-processed  |
        #   |      data 2D field    |  |      data profile     |  |     data list    |
        #   |-----------------------|  |-----------------------|  |------------------|
        #   |------------------------------------------------------------------------|

        self.verticalLayoutMain = QVBoxLayout(widget_tab)

        self.horizontalLayout_Top = QHBoxLayout()
        self.verticalLayoutMain.addLayout(self.horizontalLayout_Top, 5)

        self.horizontalLayout_Bottom = QHBoxLayout()
        self.verticalLayoutMain.addLayout(self.horizontalLayout_Bottom, 5)

        # --------------------------------------------------------------------------------------------------------------
        ### --- Layout of groupbox in the top layout box

        self.groupbox_study_data = QGroupBox()
        self.horizontalLayout_Top.addWidget(self.groupbox_study_data, 3)

        self.groupbox_plot_SNR = QGroupBox()
        self.horizontalLayout_Top.addWidget(self.groupbox_plot_SNR, 5)

        self.groupbox_pre_processing_option = QGroupBox()
        self.horizontalLayout_Top.addWidget(self.groupbox_pre_processing_option, 2)

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        # +++ --- Download data to be processed + Download noise file + compute noise data from profile tail +  --- +++
        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        self.groupbox_study_data.setTitle("Study data")

        ### --- Groupbox download data to be processed ---
        self.verticalLayout_groupbox_study_data = QVBoxLayout(self.groupbox_study_data)

        self.horizontalLayout_groupbox_data_to_be_processed_Pushbutton_plot_clear_noise = QHBoxLayout()
        self.verticalLayout_groupbox_study_data.addLayout(self.horizontalLayout_groupbox_data_to_be_processed_Pushbutton_plot_clear_noise)

        self.groupbox_data_to_be_processed = QGroupBox()
        self.gridLayout_groupbox_data_to_be_processed = QGridLayout(self.groupbox_data_to_be_processed)
        self.groupbox_data_to_be_processed.setTitle("Data to be processed")

        self.combobox_acoustic_data_choice = QComboBox()
        self.combobox_acoustic_data_choice.setMinimumWidth(200)
        self.gridLayout_groupbox_data_to_be_processed.addWidget(self.combobox_acoustic_data_choice, 0, 0, 1, 1, Qt.AlignCenter)

        self.pushbutton_update = QPushButton()
        self.pushbutton_update.setIcon(self.icon_update)
        self.gridLayout_groupbox_data_to_be_processed.addWidget(self.pushbutton_update, 0, 1, 1, 1, Qt.AlignCenter)

        self.horizontalLayout_groupbox_data_to_be_processed_Pushbutton_plot_clear_noise.addWidget(
            self.groupbox_data_to_be_processed)

        self.pushbutton_plot_noise_data = QPushButton()
        self.pushbutton_plot_noise_data.setText("Plot noise field")
        self.horizontalLayout_groupbox_data_to_be_processed_Pushbutton_plot_clear_noise.addWidget(
            self.pushbutton_plot_noise_data)

        self.pushbutton_clear_noise_data = QPushButton()
        self.pushbutton_clear_noise_data.setIcon(self.icon_clear)
        self.horizontalLayout_groupbox_data_to_be_processed_Pushbutton_plot_clear_noise.addWidget(
            self.pushbutton_clear_noise_data)

        ### --- Groupbox download noise file ---

        self.groupbox_download_noise_file = QGroupBox()
        self.groupbox_download_noise_file.setTitle("Noise file")
        self.groupbox_download_noise_file.setCheckable(True)
        self.groupbox_download_noise_file.setChecked(True)
        self.gridLayout_groupbox_noise_file = QGridLayout(self.groupbox_download_noise_file)
        self.verticalLayout_groupbox_study_data.addWidget(self.groupbox_download_noise_file)

        self.pushbutton_noise_file = QPushButton()
        self.pushbutton_noise_file.setIcon(self.icon_folder)
        self.gridLayout_groupbox_noise_file.addWidget(self.pushbutton_noise_file, 0, 0, 1, 1, Qt.AlignCenter)
        self.lineEdit_noise_file = QLineEdit()
        self.gridLayout_groupbox_noise_file.addWidget(self.lineEdit_noise_file, 0, 1, 1, 2, Qt.AlignLeft)

        ### --- Groupbox compute noise from profile tail ---

        self.groupbox_compute_noise_from_profile_tail = QGroupBox()
        self.groupbox_compute_noise_from_profile_tail.setTitle("Profile tail")
        self.groupbox_compute_noise_from_profile_tail.setCheckable(True)
        self.groupbox_compute_noise_from_profile_tail.setChecked(False)

        self.verticalLayout_groupbox_study_data.addWidget(self.groupbox_compute_noise_from_profile_tail)

        self.horizontalLayout_groupbox_compute_noise_from_profile_tail = QHBoxLayout(self.groupbox_compute_noise_from_profile_tail)

        self.groupbox_plot_profile_tail = QGroupBox()
        self.horizontalLayout_groupbox_compute_noise_from_profile_tail.addWidget(self.groupbox_plot_profile_tail)

        self.verticalLayout_groupbox_plot_profile_tail = QVBoxLayout(self.groupbox_plot_profile_tail)
        self.canvas_profile_tail = FigureCanvas()
        self.verticalLayout_groupbox_plot_profile_tail.addWidget(self.canvas_profile_tail)
        self.horizontalLayout_groupbox_compute_noise_from_profile_tail.addWidget(self.groupbox_plot_profile_tail)

        self.groupbox_option_profile_tail = QGroupBox()

        self.horizontalLayout_groupbox_compute_noise_from_profile_tail.addWidget(self.groupbox_option_profile_tail)

        self.gridLayout_groupbox_option_profile_tail = QGridLayout(self.groupbox_option_profile_tail)

        self.combobox_freq_noise_from_profile_tail = QComboBox()
        self.gridLayout_groupbox_option_profile_tail.addWidget(self.combobox_freq_noise_from_profile_tail, 0, 0, 1, 2, Qt.AlignCenter)

        self.lineEdit_val1 = QLineEdit()
        self.lineEdit_val1.setText("0.00")
        self.gridLayout_groupbox_option_profile_tail.addWidget(self.lineEdit_val1, 1, 0, 1, 1, Qt.AlignCenter)

        self.lineEdit_val2 = QLineEdit()
        self.lineEdit_val2.setText("0.00")
        self.gridLayout_groupbox_option_profile_tail.addWidget(self.lineEdit_val2, 1, 1, 1, 1, Qt.AlignCenter)

        self.pushbutton_plot_profile_tail = QPushButton()
        self.pushbutton_plot_profile_tail.setIcon(self.icon_apply)
        self.gridLayout_groupbox_option_profile_tail.addWidget(self.pushbutton_plot_profile_tail, 1, 2, 1, 1, Qt.AlignCenter)

        self.lineEdit_profile_tail_value = QLineEdit()
        self.lineEdit_profile_tail_value.setText("0.0000")
        self.gridLayout_groupbox_option_profile_tail.addWidget(self.lineEdit_profile_tail_value, 2, 0, 1, 1, Qt.AlignCenter)

        self.label_Volts = QLabel()
        self.label_Volts.setText("Volts")
        self.gridLayout_groupbox_option_profile_tail.addWidget(self.label_Volts, 2, 1, 1, 1, Qt.AlignLeft)

        self.pushbutton_apply_noise_from_profile_tail = QPushButton()
        self.pushbutton_apply_noise_from_profile_tail.setIcon(self.icon_apply)
        self.gridLayout_groupbox_option_profile_tail.addWidget(self.pushbutton_apply_noise_from_profile_tail, 2, 2, 1, 1, Qt.AlignCenter)

        # ++++++++++++++++++++++++
        # +++ --- Plot SNR --- +++
        # ++++++++++++++++++++++++

        self.groupbox_plot_SNR.setTitle("Plot SNR")
        self.verticalLayout_groupbox_plot_SNR = QVBoxLayout(self.groupbox_plot_SNR)

        self.canvas_SNR = FigureCanvas()
        self.toolbar_SNR = NavigationToolBar(self.canvas_SNR, self)
        self.verticalLayout_groupbox_plot_SNR.addWidget(self.toolbar_SNR)

        self.scroll_SNR = QScrollArea()
        self.scroll_SNR.setWidget(self.canvas_SNR)
        self.scroll_SNR.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scroll_SNR.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scroll_SNR.setAlignment(Qt.AlignCenter)
        self.verticalLayout_groupbox_plot_SNR.addWidget(self.scroll_SNR)

        # +++++++++++++++++++++++++++++++++++++
        # +++ --- Pre-processing option --- +++
        # +++++++++++++++++++++++++++++++++++++

        self.groupbox_pre_processing_option.setTitle("Pre-processing options")
        self.verticalLayout_groupbox_pre_processing_option = QVBoxLayout(self.groupbox_pre_processing_option)

        ### --- Groupbox SNR criterion ---
        self.groupbox_SNR_criterion = QGroupBox()

        self.groupbox_SNR_criterion.setTitle("SNR filter")
        self.gridLayout_SNR_criterion = QGridLayout(self.groupbox_SNR_criterion)

        self.label_SNR_criterion = QLabel("SNR < ")
        self.gridLayout_SNR_criterion.addWidget(self.label_SNR_criterion, 0, 0, 1, 1, Qt.AlignCenter)
        self.lineEdit_SNR_criterion = QLineEdit()
        self.lineEdit_SNR_criterion.setMaximumWidth(60)
        self.gridLayout_SNR_criterion.addWidget(self.lineEdit_SNR_criterion, 0, 1, 1, 1, Qt.AlignCenter)
        self.pushbutton_Apply_SNR_filter = QPushButton()
        self.pushbutton_Apply_SNR_filter.setIcon(self.icon_apply)
        self.gridLayout_SNR_criterion.addWidget(self.pushbutton_Apply_SNR_filter, 0, 2, 1, 1, Qt.AlignCenter)

        self.verticalLayout_groupbox_pre_processing_option.addWidget(self.groupbox_SNR_criterion)

        # --- Rayleigh criterion groupbox ---
        self.groupbox_Rayleigh_criterion = QGroupBox()

        self.groupbox_Rayleigh_criterion.setTitle("Despiking the signal")
        self.groupbox_Rayleigh_criterion.setDisabled(True)
        self.gridLayout_Rayleigh_criterion = QGridLayout(self.groupbox_Rayleigh_criterion)

        self.label_Rayleigh_criterion = QLabel()
        self.label_Rayleigh_criterion.setText("<V²>/<V>² <=")
        self.gridLayout_Rayleigh_criterion.addWidget(self.label_Rayleigh_criterion, 0, 0, 1, 1)

        self.spinbox_Rayleigh_criterion = QSpinBox()
        self.spinbox_Rayleigh_criterion.setRange(0, 9999)
        self.spinbox_Rayleigh_criterion.setValue(10)
        self.gridLayout_Rayleigh_criterion.addWidget(self.spinbox_Rayleigh_criterion, 0, 1, 1, 1)

        self.label_4pi = QLabel()
        self.label_4pi.setText("% x 4/pi")
        self.gridLayout_Rayleigh_criterion.addWidget(self.label_4pi, 0, 2, 1, 1)

        self.pushbutton_despiking_signal = QPushButton()
        self.pushbutton_despiking_signal.setText("Despiking the signal")
        self.gridLayout_Rayleigh_criterion.addWidget(self.pushbutton_despiking_signal, 0, 3, 1, 1)

        self.verticalLayout_groupbox_pre_processing_option.addWidget(self.groupbox_Rayleigh_criterion)

        # --- Average signal groupbox ---
        self.groupbox_window_size = QGroupBox()

        self.groupbox_window_size.setTitle("Horizontal averaging")
        self.gridLayout_groupbox_window_size = QGridLayout(self.groupbox_window_size)

        self.label_signal_averaging_horizontal = QLabel()
        self.label_signal_averaging_horizontal.setText("Horizontal +/- ")
        self.gridLayout_groupbox_window_size.addWidget(self.label_signal_averaging_horizontal, 0, 0, 1, 1, Qt.AlignCenter)

        self.lineEdit_horizontal_average = QLineEdit()
        self.lineEdit_horizontal_average.setText("0.00")
        self.lineEdit_horizontal_average.setMaximumWidth(60)
        self.gridLayout_groupbox_window_size.addWidget(self.lineEdit_horizontal_average, 0, 1, 1, 1, Qt.AlignCenter)

        self.label_cells_horizontal = QLabel()
        self.label_cells_horizontal.setText("cells = +/- ? sec")
        self.gridLayout_groupbox_window_size.addWidget(self.label_cells_horizontal, 0, 2, 1, 1, Qt.AlignCenter)

        self.pushbutton_average = QPushButton()
        self.pushbutton_average.setIcon(self.icon_apply)
        self.gridLayout_groupbox_window_size.addWidget(self.pushbutton_average, 0, 3, 1, 1, Qt.AlignCenter)

        self.verticalLayout_groupbox_pre_processing_option.addWidget(self.groupbox_window_size)

        # # ++++++++++++++++++++++++++++++++++++++++++++++
        # # +++ --- Plot BS data filtered with SNR --- +++
        # # ++++++++++++++++++++++++++++++++++++++++++++++

        # --------------------------------------------------------------------------------------------------------------
        ### --- Layout of groupbox in the bottom layout box

        self.groupbox_plot_pre_processed_data_2D_field = QGroupBox()
        self.horizontalLayout_Bottom.addWidget(self.groupbox_plot_pre_processed_data_2D_field, 5)

        self.groupbox_plot_pre_processed_data_profile = QGroupBox()
        self.horizontalLayout_Bottom.addWidget(self.groupbox_plot_pre_processed_data_profile, 3)

        self.groupbox_list_pre_processed_data = QGroupBox()
        self.horizontalLayout_Bottom.addWidget(self.groupbox_list_pre_processed_data, 2)

        # ++++++++++++++++++++++++++++++++++++++++++++++++
        # +++ --- Plot pre-processed data 2D field --- +++
        # ++++++++++++++++++++++++++++++++++++++++++++++++

        self.groupbox_plot_pre_processed_data_2D_field.setTitle("Display pre-processed data 2D fields")

        self.verticalLayout_groupbox_plot_pre_processed_data_2D_field = QVBoxLayout(
            self.groupbox_plot_pre_processed_data_2D_field)

        self.canvas_BS = FigureCanvas()
        self.toolbar_BS = NavigationToolBar(self.canvas_BS, self)

        self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.toolbar_BS)

        self.scroll_BS = QScrollArea()
        self.scroll_BS.setWidget(self.canvas_BS)
        self.scroll_BS.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scroll_BS.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scroll_BS.setAlignment(Qt.AlignCenter)
        self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.scroll_BS)

        # +++++++++++++++++++++++++++++++++++++++++++++++
        # +++ --- Plot pre-processed data profile --- +++
        # +++++++++++++++++++++++++++++++++++++++++++++++

        self.groupbox_plot_pre_processed_data_profile.setTitle("Display pre-processed data profile")

        self.verticalLayout_groupbox_plot_pre_processed_data_2D_profile = QVBoxLayout(
            self.groupbox_plot_pre_processed_data_profile)

        self.horizontalLayout_profile_frequency = QHBoxLayout()
        self.verticalLayout_groupbox_plot_pre_processed_data_2D_profile.addLayout(
            self.horizontalLayout_profile_frequency)

        self.spacerItem_frequency_profile = QSpacerItem(50, 10, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.horizontalLayout_profile_frequency.addSpacerItem(self.spacerItem_frequency_profile)
        self.combobox_frequency_profile = QComboBox()
        self.horizontalLayout_profile_frequency.addWidget(self.combobox_frequency_profile)

        self.groupbox_plot_profile = QGroupBox()
        self.verticalLayout_groupbox_plot_profile = QVBoxLayout(self.groupbox_plot_profile)

        self.canvas_profile = FigureCanvas()
        self.toolbar_profile = NavigationToolBar(self.canvas_profile, self)

        self.verticalLayout_groupbox_plot_profile.addWidget(self.toolbar_profile)
        self.verticalLayout_groupbox_plot_profile.addWidget(self.canvas_profile)

        self.verticalLayout_groupbox_plot_pre_processed_data_2D_profile.addWidget(self.groupbox_plot_profile)

        # --- Slider for moving the profile ---
        self.horizontalLayout_slider = QHBoxLayout()
        self.verticalLayout_groupbox_plot_pre_processed_data_2D_profile.addLayout(self.horizontalLayout_slider)

        self.pushbutton_slider_left_to_begin = QPushButton()
        self.pushbutton_slider_left_to_begin.setIcon(self.icon_triangle_left_to_begin)
        self.horizontalLayout_slider.addWidget(self.pushbutton_slider_left_to_begin)

        self.pushbutton_slider_left = QPushButton()
        self.pushbutton_slider_left.setIcon(self.icon_triangle_left)
        self.horizontalLayout_slider.addWidget(self.pushbutton_slider_left)

        self.lineEdit_slider = QLineEdit()
        self.lineEdit_slider.setText("1")
        self.lineEdit_slider.setFixedWidth(50)
        self.horizontalLayout_slider.addWidget(self.lineEdit_slider)

        self.pushbutton_slider_right = QPushButton()
        self.pushbutton_slider_right.setIcon(self.icon_triangle_right)
        self.horizontalLayout_slider.addWidget(self.pushbutton_slider_right)

        self.pushbutton_slider_right_to_end = QPushButton()
        self.pushbutton_slider_right_to_end.setIcon(self.icon_triangle_right_to_end)
        self.horizontalLayout_slider.addWidget(self.pushbutton_slider_right_to_end)

        self.slider = QSlider()
        self.horizontalLayout_slider.addWidget(self.slider, 9)

        self.slider.setOrientation(Qt.Horizontal)
        self.slider.setCursor(Qt.OpenHandCursor)
        self.slider.setMinimum(1)
        self.slider.setMaximum(10)
        self.slider.setTickInterval(1)
        self.slider.setValue(1)

        # ++++++++++++++++++++++++++++++++++++++++++
        # +++ --- List of pre-processed data --- +++
        # ++++++++++++++++++++++++++++++++++++++++++

        self.groupbox_list_pre_processed_data.setTitle("List pre-processed data: user label")

        self.verticalLayout_groupbox_list_pre_processed_data = QVBoxLayout(self.groupbox_list_pre_processed_data)

        # Create a scroll area
        self.scrollArea_list_preprocessed_data = QScrollArea()
        self.verticalLayout_groupbox_list_pre_processed_data.addWidget(self.scrollArea_list_preprocessed_data)

        # Set scrollbar policies
        self.scrollArea_list_preprocessed_data.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scrollArea_list_preprocessed_data.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scroll_BS.setAlignment(Qt.AlignLeft)

        # Create a widget to be scrolled
        self.widget_scrollArea_list_preprocessed_data = QWidget()
        self.verticalLayout_scrollArea_list_pre_processed_data = QVBoxLayout()
        self.widget_scrollArea_list_preprocessed_data.setLayout(self.verticalLayout_scrollArea_list_pre_processed_data)

        # Add content to the widget (labels in this example)
        for i in range(20):
            exec("self.lineEdit_list_pre_processed_data_" + str(i) + "= QLineEdit()")
            eval("self.verticalLayout_scrollArea_list_pre_processed_data.addWidget("
                 "self.lineEdit_list_pre_processed_data_" + str(i) + ")")
            eval("self.lineEdit_list_pre_processed_data_" + str(i) + ".setDisabled(True)")


        # Set the widget as the scroll area's widget
        self.scrollArea_list_preprocessed_data.setWidget(self.widget_scrollArea_list_preprocessed_data)
        self.scrollArea_list_preprocessed_data.setWidgetResizable(True)

        # --------------------------------------------------------------------------------------------------------------
        # --------------------------------------------------------------------------------------------------------------
        #                              +++++++++ CONNECT SIGNAL OF WIDGET TO SLOT FUNCTION +++++++++
        # --------------------------------------------------------------------------------------------------------------
        # --------------------------------------------------------------------------------------------------------------

        self.pushbutton_update.clicked.connect(self.update_SignalPreprocessingTab)
        self.pushbutton_update.clicked.connect(self.compute_average_profile_tail)
        self.pushbutton_update.clicked.connect(self.plot_averaged_profile_tail)

        self.combobox_acoustic_data_choice.currentIndexChanged.connect(self.combobox_acoustic_data_choice_change_index)

        self.groupbox_download_noise_file.toggled.connect(self.groupbox_download_noise_file_toggle)

        self.groupbox_compute_noise_from_profile_tail.toggled.connect(self.groupbox_option_profile_tail_toggle)

        self.pushbutton_noise_file.clicked.connect(self.open_dialog_box)

        self.pushbutton_plot_noise_data.clicked.connect(self.open_plot_noise_window)

        self.pushbutton_clear_noise_data.clicked.connect(self.clear_noise_data)

        self.pushbutton_plot_profile_tail.clicked.connect(self.compute_average_profile_tail)
        self.pushbutton_plot_profile_tail.clicked.connect(self.plot_averaged_profile_tail)

        self.pushbutton_apply_noise_from_profile_tail.clicked.connect(self.plot_averaged_profile_tail)
        self.pushbutton_apply_noise_from_profile_tail.clicked.connect(self.compute_noise_from_profile_tail_value)

        self.combobox_freq_noise_from_profile_tail.currentIndexChanged.connect(self.compute_average_profile_tail)
        self.combobox_freq_noise_from_profile_tail.currentIndexChanged.connect(self.plot_averaged_profile_tail)

        self.pushbutton_Apply_SNR_filter.clicked.connect(self.remove_point_with_snr_filter)

        self.lineEdit_horizontal_average.returnPressed.connect(self.update_label_cells_sec)
        self.pushbutton_average.clicked.connect(self.compute_averaged_BS_data)

        self.pushbutton_slider_left_to_begin.clicked.connect(self.slide_profile_number_to_begin)

        self.pushbutton_slider_left.clicked.connect(self.slide_profile_number_to_left)

        self.lineEdit_slider.returnPressed.connect(self.profile_number_on_lineEdit)

        self.pushbutton_slider_right.clicked.connect(self.slide_profile_number_to_right)

        self.pushbutton_slider_right_to_end.clicked.connect(self.slide_profile_number_to_end)

        self.slider.valueChanged.connect(self.update_lineEdit_by_moving_slider)
        self.slider.valueChanged.connect(self.update_plot_pre_processed_profile)

    # --------------------------------------------------------------------------------------------------------------
    # --------------------------------------------------------------------------------------------------------------
    #                              +++++++++ FUNCTION +++++++++
    # --------------------------------------------------------------------------------------------------------------
    # --------------------------------------------------------------------------------------------------------------

    def update_SignalPreprocessingTab(self):

        """ The tab is updated in two cases :
                - the user remove a file (in the list widget) in the first tab (Acoustic data), so that the combobox
                    of data to be processed is updated,
                - the user change the limits of one or all the records in the first tab (Acoustic data) """

        self.combobox_acoustic_data_choice.clear()
        self.combobox_acoustic_data_choice.addItems(stg.filename_BS_raw_data)

        if stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] == 0:

            self.groupbox_download_noise_file.setChecked(True)
            self.groupbox_compute_noise_from_profile_tail.setChecked(False)
            self.groupbox_download_noise_file_toggle()

        elif stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] == 1:

            self.groupbox_download_noise_file.setChecked(False)
            self.groupbox_compute_noise_from_profile_tail.setChecked(True)
            self.groupbox_option_profile_tail_toggle()

        self.combobox_freq_noise_from_profile_tail.clear()
        self.combobox_freq_noise_from_profile_tail.addItems(stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()])

        self.combobox_acoustic_data_choice.currentIndexChanged.connect(self.combobox_acoustic_data_choice_change_index)

    def activate_list_of_pre_processed_data(self):
        for i in range(self.combobox_acoustic_data_choice.count()):
            eval("self.lineEdit_list_pre_processed_data_" + str(i) + ".setDisabled(True)")
        eval("self.lineEdit_list_pre_processed_data_" + str(
            self.combobox_acoustic_data_choice.currentIndex()) + ".setEnabled(True)")
        eval("self.lineEdit_list_pre_processed_data_" + str(
            self.combobox_acoustic_data_choice.currentIndex()) + ".returnPressed.connect(self.rename_preprocessed_data)")

        eval("self.lineEdit_list_pre_processed_data_" + str(self.combobox_acoustic_data_choice.currentIndex()) +
             ".setText(stg.filename_BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()])")

    def rename_preprocessed_data(self):
        exec("stg.data_preprocessed[self.combobox_acoustic_data_choice.currentIndex()] = "
             "self.lineEdit_list_pre_processed_data_" + str(self.combobox_acoustic_data_choice.currentIndex()) + ".text()")

        # ------------------------------------------------------

    def groupbox_download_noise_file_toggle(self):
        if self.groupbox_download_noise_file.isChecked() == True:
            self.groupbox_compute_noise_from_profile_tail.setChecked(False)
        elif self.groupbox_download_noise_file.isChecked() == False:
            self.groupbox_compute_noise_from_profile_tail.setChecked(True)

    def groupbox_download_noise_file_size_change(self):
        duration = 500
        self.animation_groupbox_download_noise_file = QPropertyAnimation(self.groupbox_download_noise_file, b"size")
        self.animation_groupbox_download_noise_file.setDuration(duration)

        self.animation_groupbox_download_noise_file.setStartValue(QSize(self.groupbox_download_noise_file.width(),
                                                              self.groupbox_download_noise_file.height()))

        if self.groupbox_download_noise_file.isChecked():
            self.animation_groupbox_download_noise_file.setEndValue(
                QSize(self.groupbox_download_noise_file.width(),
                      self.groupbox_download_noise_file.sizeHint().height()))
        else:
            self.animation_groupbox_download_noise_file.setEndValue(QSize(self.groupbox_download_noise_file.width(), 25))

        self.animation_groupbox_download_noise_file.start()

    def groupbox_option_profile_tail_toggle(self):
        if self.groupbox_compute_noise_from_profile_tail.isChecked() == True:
            self.groupbox_download_noise_file.setChecked(False)
        elif self.groupbox_compute_noise_from_profile_tail.isChecked() == False:
            self.groupbox_download_noise_file.setChecked(True)

    def groupbox_option_profile_tail_size_change(self):
        duration = 500
        self.animation_groupbox_option_profile_tail = QPropertyAnimation(self.groupbox_option_profile_tail, b"size")
        self.animation_groupbox_option_profile_tail.setDuration(duration)

        self.animation_groupbox_option_profile_tail.setStartValue(QSize(self.groupbox_option_profile_tail.width(),
                                                             self.groupbox_option_profile_tail.height()))

        if self.groupbox_option_profile_tail.isChecked():
            self.animation_groupbox_option_profile_tail.setEndValue(
                QSize(self.groupbox_option_profile_tail.width(),
                      self.groupbox_option_profile_tail.sizeHint().height()))
        else:
            self.animation_groupbox_option_profile_tail.setEndValue(QSize(self.groupbox_option_profile_tail.width(), 25))

        self.animation_groupbox_option_profile_tail.start()

    # ------------------------------------------------------
    def compute_average_profile_tail(self):

        if (float(self.lineEdit_val1.text()) == 0) and (float(self.lineEdit_val2.text()) == 0):
            if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):
                self.lineEdit_val1.setText(str(
                    '%.3f' % np.nanmin(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                                           self.combobox_freq_noise_from_profile_tail.currentIndex()])))
                self.lineEdit_val2.setText(str(
                    '%.3f' % np.nanmax(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                                           self.combobox_freq_noise_from_profile_tail.currentIndex()])))

            elif stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):
                self.lineEdit_val1.setText(str(
                    '%.3f' % np.nanmin(stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                                           self.combobox_freq_noise_from_profile_tail.currentIndex()])))
                self.lineEdit_val2.setText(str(
                    '%.3f' % np.nanmax(stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                                           self.combobox_freq_noise_from_profile_tail.currentIndex()])))

        # --- Find index of line edit value ---
        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            val1 = np.where(np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                                       self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                   float(self.lineEdit_val1.text().replace(",", ".")))
                            ==
                            np.nanmin(np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                                                 self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                             float(self.lineEdit_val1.text().replace(",", "."))))
                            )[0][0]
            val2 = np.where(np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                                       self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                   float(self.lineEdit_val2.text().replace(",", ".")))
                            ==
                            np.nanmin(np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                                                 self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                             float(self.lineEdit_val2.text().replace(",", "."))))
                            )[0][0]

        elif stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            val1 = np.where(np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                                       self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                   float(self.lineEdit_val1.text().replace(",", ".")))
                            ==
                            np.nanmin(np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                                                 self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                             float(self.lineEdit_val1.text().replace(",", "."))))
                            )[0][0]
            val2 = np.where(np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                                       self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                   float(self.lineEdit_val2.text().replace(",", ".")))
                            ==
                            np.nanmin(np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                                                 self.combobox_freq_noise_from_profile_tail.currentIndex()] -
                                             float(self.lineEdit_val2.text().replace(",", "."))))
                            )[0][0]

        stg.BS_mean[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.nanmean(stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][:, :, val1:val2], axis=2))

    def plot_averaged_profile_tail(self):

        # --- Plot averaged signal ---

        if stg.BS_mean[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            self.verticalLayout_groupbox_plot_profile_tail.removeWidget(self.canvas_profile_tail)

            self.fig_profile_tail, self.axis_profile_tail = plt.subplots(nrows=1, ncols=1, layout='constrained')
            self.canvas_profile_tail = FigureCanvas(self.fig_profile_tail)

            self.verticalLayout_groupbox_plot_profile_tail.addWidget(self.canvas_profile_tail)

            self.axis_profile_tail.plot(
                    -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][self.combobox_freq_noise_from_profile_tail.currentIndex()],
                    stg.BS_mean[self.combobox_acoustic_data_choice.currentIndex()][self.combobox_freq_noise_from_profile_tail.currentIndex()],
                    color="blue", linewidth=1)
            self.axis_profile_tail.plot(
                    -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_freq_noise_from_profile_tail.currentIndex()],
                    float(self.lineEdit_profile_tail_value.text().replace(",", ".")) *
                        np.ones(stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                            self.combobox_freq_noise_from_profile_tail.currentIndex()].shape[0]),
                    linestyle='dashed', linewidth=2, color='red')

            self.axis_profile_tail.set_yscale('log')
            self.axis_profile_tail.tick_params(axis='both', labelsize=8)
            self.axis_profile_tail.text(.98, .03, "Depth (m)",
                                        fontsize=8, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.9,
                                        horizontalalignment='right', verticalalignment='bottom', rotation='horizontal',
                                        transform=self.axis_profile_tail.transAxes)
            self.axis_profile_tail.text(.1, .45, "BS signal (v)",
                                        fontsize=8, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.9,
                                        horizontalalignment='right', verticalalignment='bottom', rotation='vertical',
                                        transform=self.axis_profile_tail.transAxes)
            self.axis_profile_tail.text(.98, .85,
                                        stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()][
                                            self.combobox_freq_noise_from_profile_tail.currentIndex()],
                                        fontsize=10, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5,
                                        horizontalalignment='right', verticalalignment='bottom',
                                        transform=self.axis_profile_tail.transAxes)

            self.fig_profile_tail.canvas.draw_idle()

    # ------------------------------------------------------

    def combobox_acoustic_data_choice_change_index(self):

        self.compute_average_profile_tail()
        self.plot_averaged_profile_tail()

        self.lineEdit_SNR_criterion.setText(str(stg.SNR_filter_value[self.combobox_acoustic_data_choice.currentIndex()]))

        self.plot_transect_with_SNR_data()
        self.plot_pre_processed_BS_signal()
        self.lineEdit_horizontal_average.setText(str(stg.Nb_cells_to_average_BS_signal[self.combobox_acoustic_data_choice.currentIndex()]))

        self.combobox_frequency_profile.clear()
        self.combobox_frequency_profile.addItems(
            [f for f in stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()]])
        self.plot_pre_processed_profile()

        if self.combobox_acoustic_data_choice.count() > 0:

            for i in range(self.combobox_acoustic_data_choice.count()):
                eval("self.lineEdit_list_pre_processed_data_" + str(i) + ".setDisabled(True)")
            #
            eval("self.lineEdit_list_pre_processed_data_" + str(
                self.combobox_acoustic_data_choice.currentIndex()) + ".setEnabled(True)")

    def clear_noise_data(self):

        stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.SNR_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.time_noise[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] = 0
        stg.SNR_filter_value[self.combobox_acoustic_data_choice.currentIndex()] = 0

        stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])

        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])

        stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])
        stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = np.array([])

        if stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] == 0:
            self.lineEdit_noise_file.clear()

        elif stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] == 1:
            self.lineEdit_val1.clear()
            self.lineEdit_val1.setText("0.00")

            self.lineEdit_val2.clear()
            self.lineEdit_val2.setText("0.00")

            self.lineEdit_profile_tail_value.clear()
            self.lineEdit_profile_tail_value.setText("0.0000")

            self.verticalLayout_groupbox_plot_profile_tail.removeWidget(self.canvas_profile_tail)
            self.canvas_profile_tail = FigureCanvas()
            self.verticalLayout_groupbox_plot_profile_tail.addWidget(self.canvas_profile_tail)

        self.lineEdit_SNR_criterion.setText("0.00")
        self.lineEdit_horizontal_average.setText("0.00")

        # --- Clear SNR plot ---
        self.verticalLayout_groupbox_plot_SNR.removeWidget(self.toolbar_SNR)
        self.verticalLayout_groupbox_plot_SNR.removeWidget(self.scroll_SNR)

        self.canvas_SNR = FigureCanvas()
        self.toolbar_SNR = NavigationToolBar(self.canvas_SNR, self)
        self.scroll_SNR.setWidget(self.canvas_SNR)

        self.verticalLayout_groupbox_plot_SNR.addWidget(self.toolbar_SNR)
        self.verticalLayout_groupbox_plot_SNR.addWidget(self.scroll_SNR)

        # --- Clear BS plot ---
        self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.removeWidget(self.toolbar_BS)
        self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.removeWidget(self.scroll_BS)

        self.canvas_BS = FigureCanvas()
        self.toolbar_BS = NavigationToolBar(self.canvas_BS, self)
        self.scroll_BS.setWidget(self.canvas_BS)

        self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.toolbar_BS)
        self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.scroll_BS)

        self.combobox_frequency_profile.clear()

        self.verticalLayout_groupbox_plot_profile.removeWidget(self.toolbar_profile)
        self.verticalLayout_groupbox_plot_profile.removeWidget(self.canvas_profile)

        self.canvas_profile = FigureCanvas()
        self.toolbar_profile = NavigationToolBar(self.canvas_profile, self)

        self.verticalLayout_groupbox_plot_profile.addWidget(self.toolbar_profile)
        self.verticalLayout_groupbox_plot_profile.addWidget(self.canvas_profile)

        self.slider.setValue(1)
        self.slider.setMaximum(10)

        self.slider.setValue(0)
        self.slider.setMaximum(10)

    def open_dialog_box(self):

        if self.combobox_acoustic_data_choice.count() > 0:
            filename = QFileDialog.getOpenFileNames(self, "AQUAscat Noise file",
                                                   [stg.path_BS_raw_data[-1] if self.combobox_acoustic_data_choice.count() > 0 else ""][0],
                                                   "Aquascat file (*.aqa)",
                                                    options=QFileDialog.DontUseNativeDialog)
            if filename[0]:
                dir_name = path.dirname(filename[0][0])
                name = path.basename(filename[0][0])
                stg.path_BS_noise_data[self.combobox_acoustic_data_choice.currentIndex()] = dir_name
                stg.filename_BS_noise_data[self.combobox_acoustic_data_choice.currentIndex()] = name

            try:

                self.load_noise_data_and_compute_SNR()

            except ValueError as e:
                msgBox = QMessageBox()
                msgBox.setWindowTitle("Download Error")
                msgBox.setIcon(QMessageBox.Warning)
                msgBox.setText("Please select a file")
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec()

            else:
                self.lineEdit_noise_file.setText(stg.filename_BS_noise_data[self.combobox_acoustic_data_choice.currentIndex()])
                self.lineEdit_noise_file.setToolTip(stg.path_BS_noise_data[self.combobox_acoustic_data_choice.currentIndex()])
                self.plot_transect_with_SNR_data()

                self.combobox_frequency_profile.clear()
                self.combobox_frequency_profile.addItems([f for f in stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()]])
                self.combobox_frequency_profile.currentIndexChanged.connect(self.plot_pre_processed_BS_signal)
                self.combobox_frequency_profile.currentIndexChanged.connect(self.update_plot_pre_processed_profile)

                if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.slider.setMaximum(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape[1])

                else:

                    self.slider.setMaximum(stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape[1])

                self.plot_pre_processed_BS_signal()
                self.plot_pre_processed_profile()

                stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] = 0

                self.activate_list_of_pre_processed_data()

        else:

            msgBox = QMessageBox()
            msgBox.setWindowTitle("Download Error")
            msgBox.setIcon(QMessageBox.Warning)
            msgBox.setText("Please upload acoustic data")
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec()


    def load_noise_data_and_compute_SNR(self):

        stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] = 0

        noise_data = AcousticDataLoader(stg.path_BS_noise_data[self.combobox_acoustic_data_choice.currentIndex()] +
                                        "/" +
                                        stg.filename_BS_noise_data[self.combobox_acoustic_data_choice.currentIndex()])
        stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = noise_data._BS_raw_data

        stg.time_noise[self.combobox_acoustic_data_choice.currentIndex()] = noise_data._time
        stg.depth_noise[self.combobox_acoustic_data_choice.currentIndex()] = noise_data._r

        if stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            noise = np.zeros(stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape)
            for f, _ in enumerate(noise_data._freq):
                noise[f, :, :] = np.mean(
                    stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :, :], axis=(0, 1))
            stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] = noise
            stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.divide((stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()] -
                           stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()]) ** 2,
                          stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] ** 2))

        elif stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            noise = np.zeros(stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape)
            for f, _ in enumerate(noise_data._freq):
                noise[f, :, :] = np.mean(
                    stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :, :], axis=(0, 1))
            stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] = noise
            stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.divide((stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()] -
                           stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()]) ** 2,
                          stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] ** 2))
            # stg.SNR_reshape = np.reshape(stg.SNR_cross_section, (stg.r.shape[1] * stg.t.shape[1], stg.freq.shape[0]), order="F")

        else:

            noise = np.zeros(stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape)
            for f, _ in enumerate(noise_data._freq):
                noise[f, :, :] = np.mean(
                    stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :, :], axis=(0, 1))
            stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] = noise
            stg.SNR_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.divide((stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()] -
                           stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()]) ** 2,
                          stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] ** 2))

    def open_plot_noise_window(self):
        pnw = PlotNoiseWindow()
        pnw.exec()

    def compute_noise_from_profile_tail_value(self):

        stg.noise_method[self.combobox_acoustic_data_choice.currentIndex()] = 1

        stg.noise_value[self.combobox_acoustic_data_choice.currentIndex()] = (
            float(self.lineEdit_profile_tail_value.text().replace(",", ".")))

        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):
            stg.time_noise[self.combobox_acoustic_data_choice.currentIndex()] = (
                stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()])
        else:
            stg.time_noise[self.combobox_acoustic_data_choice.currentIndex()] = (
                stg.time[self.combobox_acoustic_data_choice.currentIndex()])
        if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):
            stg.depth_noise[self.combobox_acoustic_data_choice.currentIndex()] = (
                stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()])
        else:
            stg.depth_noise[self.combobox_acoustic_data_choice.currentIndex()] = (
                stg.depth[self.combobox_acoustic_data_choice.currentIndex()])

        # --- Compute noise from value and compute SNR ---

        if stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.full(stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape,
                        float(self.lineEdit_profile_tail_value.text().replace(",", "."))))

            stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()][:, :,
                :stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape[2]])
            stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.divide((stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()]
                           - stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()]) ** 2,
                          stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] ** 2))

        elif stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.full(stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape,
                        float(self.lineEdit_profile_tail_value.text().replace(",", "."))))

            stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()][:, :,
                :stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape[2]])
            stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.divide((stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()]
                           - stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()]) ** 2,
                          stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] ** 2))

        else:

            stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.full(stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape,
                        float(self.lineEdit_profile_tail_value.text().replace(",", "."))))

            stg.BS_noise_averaged_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()])
            stg.SNR_raw_data[self.combobox_acoustic_data_choice.currentIndex()] = (
                np.divide((stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()]
                           - stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()]) ** 2,
                          stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()] ** 2))

        self.combobox_frequency_profile.clear()
        self.combobox_frequency_profile.addItems(
            [f for f in stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()]])

        # --- Trigger graphic widgets ---

        if stg.SNR_filter_value[self.combobox_acoustic_data_choice.currentIndex()] == 0:
            self.lineEdit_SNR_criterion.setText("0.00")
        else:
            self.lineEdit_SNR_criterion.setText(str(stg.SNR_filter_value[self.combobox_acoustic_data_choice.currentIndex()]))

        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):
            self.slider.setMaximum(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape[1])
        else:
            self.slider.setMaximum(stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape[1])

        self.plot_transect_with_SNR_data()
        self.plot_pre_processed_BS_signal()
        self.plot_pre_processed_profile()

        self.activate_list_of_pre_processed_data()

    def plot_noise(self):
        self.horizontalLayout_groupbox_plot_noise_data.removeWidget(self.canvas_noise)

        self.fig_noise, self.axis_noise = plt.subplots(nrows=1, ncols=1, layout="constrained")
        self.canvas_noise = FigureCanvas(self.fig_noise)

        self.horizontalLayout_groupbox_plot_noise_data.addWidget(self.canvas_noise)

        val_min = np.nanmin(stg.BS_noise_raw_data[self.combobox_freq_noise.currentIndex(), :, :])
        val_max = np.nanmax(stg.BS_noise_raw_data[self.combobox_freq_noise.currentIndex(), :, :])


        if val_min == val_max:

            pcm = self.axis_noise.pcolormesh(stg.time_snr[0, :],
                                             -stg.depth[self.combobox_freq_noise.currentIndex(), :],
                                             stg.BS_noise_raw_data[self.combobox_freq_noise.currentIndex(), :, :],
                                             cmap='viridis')

        else:

            if val_min == 0:
                val_min = 1e-5

            pcm = self.axis_noise.pcolormesh(stg.time_snr[0, :],
                                             -stg.r[self.combobox_freq_noise.currentIndex(), :],
                                             stg.BS_noise_raw_data[self.combobox_freq_noise.currentIndex(), :, :],
                                             cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

        self.axis_noise.tick_params(axis='both', which='minor', labelsize=10)

    def plot_transect_with_SNR_data(self):
        # --- Condition if table is not filled ---
        # if not self.lineEdit_noise_file.text():
        if len(stg.BS_noise_raw_data) == 0:
            msgBox = QMessageBox()
            msgBox.setWindowTitle("Plot transect Error")
            msgBox.setIcon(QMessageBox.Warning)
            msgBox.setText("Load data before plot transect 2D field")
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec()

        # elif self.canvas_SNR == None:
        else:

            if ((self.combobox_acoustic_data_choice.currentIndex() != -1)
                    and (stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))):

                self.verticalLayout_groupbox_plot_SNR.removeWidget(self.toolbar_SNR)
                self.verticalLayout_groupbox_plot_SNR.removeWidget(self.scroll_SNR)

                self.fig_SNR, self.axis_SNR = plt.subplots(nrows=stg.freq[self.combobox_acoustic_data_choice.currentIndex()].shape[0], ncols=1, sharex=True, sharey=False, layout='constrained')
                self.canvas_SNR = FigureCanvas(self.fig_SNR)
                self.toolbar_SNR = NavigationToolBar(self.canvas_SNR, self)

                self.scroll_SNR.setWidget(self.canvas_SNR)

                self.verticalLayout_groupbox_plot_SNR.addWidget(self.toolbar_SNR)
                self.verticalLayout_groupbox_plot_SNR.addWidget(self.scroll_SNR)

                for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):

                    if stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                            if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                            elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                        else:

                            if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                            elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                        val_min = np.nanmin(stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                        val_max = np.nanmax(stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                        if val_min == val_max:
                            levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6])
                            bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1.2]
                            norm = BoundaryNorm(boundaries=bounds, ncolors=300)
                        else:
                            if val_min == 0:
                                val_min = 1e-5
                            if val_max > 1000:
                                levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6])
                                bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1.2]
                                norm = BoundaryNorm(boundaries=bounds, ncolors=300)
                            else:
                                levels = np.array([00.1, 1, 2, 10, 100, 1000, val_max * 1000 + 1])
                                bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1000 + 1]
                                norm = BoundaryNorm(boundaries=bounds, ncolors=300)

                        cf = (self.axis_SNR[f].contourf(x, -y,
                                                        stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                        levels, cmap='gist_rainbow',
                                                        norm=norm))

                    elif stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                            if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                            elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                        else:

                            if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                            elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                                x, y = np.meshgrid(
                                    stg.time[self.combobox_acoustic_data_choice.currentIndex()][f, :],
                                    stg.depth[self.combobox_acoustic_data_choice.currentIndex()][f, :])

                        val_min = np.nanmin(stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                        val_max = np.nanmax(stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                        if val_min == val_max:
                            levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6])
                            bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1.2]
                            norm = BoundaryNorm(boundaries=bounds, ncolors=300)
                        else:
                            if val_min == 0:
                                val_min = 1e-5
                            if val_max > 1000:
                                levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6])
                                bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1.2]
                                norm = BoundaryNorm(boundaries=bounds, ncolors=300)

                            else:
                                levels = np.array([00.1, 1, 2, 10, 100, 1000, val_max * 1000 + 1])
                                bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1000 + 1]
                                norm = BoundaryNorm(boundaries=bounds, ncolors=300)

                        cf = (self.axis_SNR[f].contourf(x, -y,
                                                        stg.SNR_cross_section[
                                                            self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                        levels, cmap='gist_rainbow', norm=norm))

                    else:

                        x, y = np.meshgrid(stg.time[self.combobox_acoustic_data_choice.currentIndex()][0, :],
                                           stg.depth[self.combobox_acoustic_data_choice.currentIndex()][0, :])

                        val_min = np.nanmin(stg.SNR_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                        val_max = np.nanmax(stg.SNR_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                        if val_min == val_max:
                            levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6])
                            bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1.2]
                            norm = BoundaryNorm(boundaries=bounds, ncolors=300)
                        else:
                            if val_min == 0:
                                val_min = 1e-5
                            if val_max > 1000:
                                levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6])
                                bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1.2]
                                norm = BoundaryNorm(boundaries=bounds, ncolors=300)
                            else:
                                levels = np.array([00.1, 1, 2, 10, 100, 1000, val_max*1000 + 1])
                                bounds = [00.1, 1, 2, 10, 100, 1000, val_max * 1000 + 1]
                                norm = BoundaryNorm(boundaries=bounds, ncolors=300)

                        cf = (self.axis_SNR[f].contourf(x, -y,
                                                        stg.SNR_raw_data[
                                                            self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                        levels, cmap='gist_rainbow', norm=norm))


                    self.axis_SNR[f].text(1, .70, stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()][f],
                                            fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5,
                                            horizontalalignment='right', verticalalignment='bottom',
                                            transform=self.axis_SNR[f].transAxes)

                self.fig_SNR.supxlabel('Time (sec)', fontsize=10)
                self.fig_SNR.supylabel('Depth (m)', fontsize=10)
                cbar = self.fig_SNR.colorbar(cf, ax=self.axis_SNR[:], shrink=1, location='right')
                cbar.set_label(label='Signal to Noise Ratio', rotation=270, labelpad=10)
                cbar.set_ticklabels(['0', '1', '2', '10', '100', r'10$^3$', r'10$^6$'])
                self.fig_SNR.canvas.draw_idle()

            else:

                self.verticalLayout_groupbox_plot_SNR.removeWidget(self.toolbar_SNR)
                self.verticalLayout_groupbox_plot_SNR.removeWidget(self.scroll_SNR)

                self.canvas_SNR = FigureCanvas()
                self.toolbar_SNR = NavigationToolBar(self.canvas_SNR, self)
                self.scroll_SNR.setWidget(self.canvas_SNR)

                self.verticalLayout_groupbox_plot_SNR.addWidget(self.toolbar_SNR)
                self.verticalLayout_groupbox_plot_SNR.addWidget(self.scroll_SNR)

    def remove_point_with_snr_filter(self):

        if len(stg.BS_noise_raw_data) == 0:
            msgBox = QMessageBox()
            msgBox.setWindowTitle("SNR filter Error")
            msgBox.setIcon(QMessageBox.Warning)
            msgBox.setText("Load Noise data from acoustic data tab before using SNR filter")
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec()

        else:

            stg.SNR_filter_value[self.combobox_acoustic_data_choice.currentIndex()] = (
                float(self.lineEdit_SNR_criterion.text().replace(",", ".")))

            if stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                stg.BS_stream_bed_pre_process_SNR = deepcopy(stg.BS_stream_bed)

                for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                    stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        f,
                        np.where(stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, :, :] <
                                 float(self.lineEdit_SNR_criterion.text().replace(",", ".")))[0],
                        np.where(stg.SNR_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, :, :] <
                                 float(self.lineEdit_SNR_criterion.text().replace(",", ".")))[1]] \
                        = np.nan

            elif stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                stg.BS_cross_section_pre_process_SNR = deepcopy(stg.BS_cross_section)

                for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                    stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        f,
                        np.where(stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :,
                                 :] < float(self.lineEdit_SNR_criterion.text().replace(",", ".")))[0],
                        np.where(stg.SNR_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :,
                                 :] < float(self.lineEdit_SNR_criterion.text().replace(",", ".")))[1]] \
                        = np.nan

            else:

                stg.BS_raw_data_pre_process_SNR = deepcopy(stg.BS_raw_data)

                for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                    stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        f,
                        np.where(stg.SNR_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :,
                                 :] < float(self.lineEdit_SNR_criterion.text().replace(",", ".")))[0],
                        np.where(stg.SNR_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :,
                                 :] < float(self.lineEdit_SNR_criterion.text().replace(",", ".")))[1]] \
                        = np.nan

            self.compute_averaged_BS_data()
            self.update_plot_pre_processed_profile()

    def plot_pre_processed_BS_signal(self):

        self.lineEdit_horizontal_average.setText(str(stg.Nb_cells_to_average_BS_signal[self.combobox_acoustic_data_choice.currentIndex()]))

        if ((self.combobox_acoustic_data_choice.currentIndex() != -1)
                and (stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))):

            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.removeWidget(self.toolbar_BS)
            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.removeWidget(self.scroll_BS)

            self.fig_BS, self.axis_BS = plt.subplots(nrows=stg.freq[self.combobox_acoustic_data_choice.currentIndex()].shape[0], ncols=1, sharex=True, sharey=False, layout="constrained")
            self.canvas_BS = FigureCanvas(self.fig_BS)
            self.toolbar_BS = NavigationToolBar(self.canvas_BS, self)

            self.scroll_BS.setWidget(self.canvas_BS)

            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.toolbar_BS)
            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.scroll_BS)

            if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    x_time = stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()]
                    y_depth = stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()]

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    x_time = stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()]
                    y_depth = stg.depth[self.combobox_acoustic_data_choice.currentIndex()]

            else:

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    x_time = stg.time[self.combobox_acoustic_data_choice.currentIndex()]
                    y_depth = stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()]

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    x_time = stg.time[self.combobox_acoustic_data_choice.currentIndex()]
                    y_depth = stg.depth[self.combobox_acoustic_data_choice.currentIndex()]


            for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):

                if stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(x_time[f, :], -y_depth[f, :],
                                                     stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                  cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                    self.axis_BS[f].plot(x_time[f, :], -stg.depth_bottom[self.combobox_acoustic_data_choice.currentIndex()],
                                         color='black', linewidth=1, linestyle="solid")

                elif stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(
                        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(
                        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(
                        x_time[f, :], -y_depth[f, :],
                        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                        cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                elif stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(
                        stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(
                        stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(x_time[f, :], -y_depth[f, :],
                                                     stg.BS_raw_data_pre_process_average[
                                                         self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                     cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                elif stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(x_time[f, :], -y_depth[f, :],
                                                     stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                  cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                    self.axis_BS[f].plot(x_time[f, :],
                                         -stg.depth_bottom[self.combobox_acoustic_data_choice.currentIndex()],
                                         color='black', linewidth=1, linestyle="solid")

                elif stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(
                        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(
                        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(
                        x_time[f, :], -y_depth[f, :],
                        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                        cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                elif stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(
                        stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(
                        stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(x_time[f, :], -y_depth[f, :],
                                                     stg.BS_raw_data_pre_process_SNR[
                                                         self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                     cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                elif stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(
                        stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(
                        stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(x_time[f, :], -y_depth[f, :],
                                                     stg.BS_stream_bed[
                                                         self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                     cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                elif stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(
                        stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(
                        stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(x_time[f, :], -y_depth[f, :],
                                                     stg.BS_cross_section[
                                                         self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                     cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                elif stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    val_min = np.nanmin(
                        stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])
                    val_max = np.nanmax(
                        stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, :, :])

                    if val_min == 0:
                        val_min = 1e-5

                    pcm = self.axis_BS[f].pcolormesh(x_time[f, :], -y_depth[f, :],
                                                     stg.BS_raw_data[
                                                         self.combobox_acoustic_data_choice.currentIndex()][f, :, :],
                                                     cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max))

                self.axis_BS[f].text(1, .70, stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()][f],
                                     fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5,
                                     horizontalalignment='right', verticalalignment='bottom',
                                     transform=self.axis_BS[f].transAxes)

                # --- Display red line on acoustic recording for profile position ---

                self.red_line_return, = self.axis_BS[self.combobox_frequency_profile.currentIndex()].plot(
                    x_time[self.combobox_frequency_profile.currentIndex(), self.slider.value() - 1] *
                    np.ones(y_depth.shape[1]),
                    -y_depth[self.combobox_frequency_profile.currentIndex(), :],
                    linestyle="solid", linewidth=2, color="red")

            self.fig_BS.supxlabel('Time (sec)', fontsize=10)
            self.fig_BS.supylabel('Depth (m)', fontsize=10)
            cbar = self.fig_BS.colorbar(pcm, ax=self.axis_BS[:], shrink=1, location='right')
            cbar.set_label(label='Acoustic backscatter signal (V)', rotation=270, labelpad=10)
            self.fig_BS.canvas.draw_idle()

        else:

            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.removeWidget(self.toolbar_BS)
            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.removeWidget(self.scroll_BS)

            self.canvas_BS = FigureCanvas()
            self.toolbar_BS = NavigationToolBar(self.canvas_BS, self)
            self.scroll_BS.setWidget(self.canvas_BS)

            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.toolbar_BS)
            self.verticalLayout_groupbox_plot_pre_processed_data_2D_field.addWidget(self.scroll_BS)

    def update_label_cells_sec(self):

        print("Je change la valeur du moyennage")
        print(stg.nb_profiles_per_sec)
        print(stg.nb_profiles_per_sec[self.combobox_acoustic_data_choice.currentIndex()][0])
        print(self.combobox_acoustic_data_choice.currentIndex())
        self.label_cells_horizontal.clear()
        self.label_cells_horizontal.setText(
            "cells = +/- " +
            str((float(self.lineEdit_horizontal_average.text().replace(",", ".")) // 2) *
                (1 / stg.nb_profiles_per_sec[self.combobox_acoustic_data_choice.currentIndex()][0])) +
            " sec")

    def compute_averaged_BS_data(self):

        kernel_avg = np.ones(2 * int(float(self.lineEdit_horizontal_average.text().replace(",", "."))) + 1)
        print(kernel_avg)

        stg.Nb_cells_to_average_BS_signal[self.combobox_acoustic_data_choice.currentIndex()] = (
            float(self.lineEdit_horizontal_average.text().replace(",", ".")))

        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                x_time = stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()]
                y_depth = stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()]

            elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                x_time = stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()]
                y_depth = stg.depth[self.combobox_acoustic_data_choice.currentIndex()]

        else:

            if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                x_time = stg.time[self.combobox_acoustic_data_choice.currentIndex()]
                y_depth = stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()]

            elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                x_time = stg.time[self.combobox_acoustic_data_choice.currentIndex()]
                y_depth = stg.depth[self.combobox_acoustic_data_choice.currentIndex()]

        if stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = (deepcopy(
                stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()]))

            for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                for i in range(y_depth.shape[1]):

                    stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, i, :] = (
                        convolve(stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, i, :],
                                 kernel_avg))

        elif stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = (deepcopy(
                stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()]))

            for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                for i in range(y_depth.shape[1]):

                    stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, i, :] = (
                        convolve(stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, i, :],
                                 kernel_avg))

        elif stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = (deepcopy(
                stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()]))

            for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                for i in range(y_depth.shape[1]):

                    stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, i, :] = (
                        convolve(stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][f, i, :],
                                 kernel_avg))

        elif stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = (deepcopy(
                stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()]))

            for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                for i in range(y_depth.shape[1]):
                    stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, i, :] = (
                        convolve(stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][f, i, :], kernel_avg))

        elif stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = (deepcopy(
                stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()]))

            for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                for i in range(y_depth.shape[1]):
                    stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, i, :] = (
                        convolve(stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()][f, i, :],
                                 kernel_avg))

        elif stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

            stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()] = (deepcopy(
                stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()]))

            for f, _ in enumerate(stg.freq[self.combobox_acoustic_data_choice.currentIndex()]):
                for i in range(y_depth.shape[1]):
                    stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][f, i, :] = (
                        convolve(stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][f, i, :], kernel_avg))

        self.plot_pre_processed_BS_signal()
        self.update_plot_pre_processed_profile()

    def plot_pre_processed_profile(self):

        if ((self.combobox_acoustic_data_choice.currentIndex() != -1) and
                (stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))):

            self.verticalLayout_groupbox_plot_profile.removeWidget(self.toolbar_profile)
            self.verticalLayout_groupbox_plot_profile.removeWidget(self.canvas_profile)

            self.figure_profile, self.axis_profile = plt.subplots(nrows=1, ncols=1, layout="constrained")
            self.canvas_profile = FigureCanvas(self.figure_profile)
            self.toolbar_profile = NavigationToolBar(self.canvas_profile, self)

            self.verticalLayout_groupbox_plot_profile.addWidget(self.toolbar_profile)
            self.verticalLayout_groupbox_plot_profile.addWidget(self.canvas_profile)

            # --- PLot profile ---

            if stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                    linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_cross_section_pre_process_average[self.combobox_frequency_profile.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_cross_section_pre_process_SNR[self.combobox_frequency_profile.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            self.axis_profile.text(
                .95, .05, stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()][
                    self.combobox_frequency_profile.currentIndex()],
                fontsize=10, fontweight='bold', fontname="Ubuntu",
                fontstyle="normal", c="black", alpha=0.2,
                horizontalalignment='right', verticalalignment='bottom',
                transform=self.axis_profile.transAxes)

            self.axis_profile.set_xlabel("Acoustic Backscatter Signal (V)")
            self.axis_profile.set_ylabel("Depth (m)")

            self.figure_profile.canvas.draw_idle()

        else:

            self.combobox_frequency_profile.clear()

            self.verticalLayout_groupbox_plot_profile.removeWidget(self.toolbar_profile)
            self.verticalLayout_groupbox_plot_profile.removeWidget(self.canvas_profile)

            self.canvas_profile = FigureCanvas()
            self.toolbar_profile = NavigationToolBar(self.canvas_profile, self)

            self.verticalLayout_groupbox_plot_profile.addWidget(self.toolbar_profile)
            self.verticalLayout_groupbox_plot_profile.addWidget(self.canvas_profile)

            self.slider.setValue(1)
            self.slider.setMaximum(10)

    def update_plot_pre_processed_profile(self):

        if ((self.combobox_acoustic_data_choice.currentIndex() != -1) and
                (stg.BS_noise_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))):

            self.axis_profile.cla()

            # --- PLot profile ---

            if stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                    linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_average[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data_pre_process_SNR[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_stream_bed[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            elif stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

                elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.axis_profile.plot(
                        stg.BS_raw_data[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :, self.slider.value() - 1],
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                         self.combobox_frequency_profile.currentIndex(), :],
                        linestyle='solid', color='k', linewidth=1)

            self.axis_profile.text(
                .95, .05, stg.freq_text[self.combobox_acoustic_data_choice.currentIndex()][
                    self.combobox_frequency_profile.currentIndex()],
                fontsize=10, fontweight='bold', fontname="Ubuntu",
                fontstyle="normal", c="black", alpha=0.2,
                horizontalalignment='right', verticalalignment='bottom',
                transform=self.axis_profile.transAxes)

            self.axis_profile.set_xlabel("Acoustic Backscatter Signal (V)")
            self.axis_profile.set_ylabel("Depth (m)")

            self.figure_profile.canvas.draw_idle()

            # --- Update red line position on transect ---
            if stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.red_line_return.set_data(
                        stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                            self.combobox_frequency_profile.currentIndex(), self.slider.value()-1] *
                            np.ones(stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape[1]),
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                            self.combobox_frequency_profile.currentIndex(), :])

                elif stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.red_line_return.set_data(
                        stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                            self.combobox_frequency_profile.currentIndex(), self.slider.value() - 1] *
                        np.ones(stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape[1]),
                        -stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :])

            elif stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.red_line_return.set_data(
                        stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                            self.combobox_frequency_profile.currentIndex(), self.slider.value() - 1] *
                        np.ones(stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape[1]),
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :])

                elif stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):

                    self.red_line_return.set_data(
                        stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                            self.combobox_frequency_profile.currentIndex(), self.slider.value() - 1] *
                        np.ones(stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape[1]),
                        -stg.depth[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), :])

            self.fig_BS.canvas.draw_idle()

    # ------------------------------------- Connect Push Button Load Data + Slider -------------------------------------

    def slide_profile_number_to_begin(self):
        self.slider.setValue(int(self.slider.minimum()))
        self.update_lineEdit_by_moving_slider()

    def slide_profile_number_to_right(self):
        self.slider.setValue(int(self.slider.value()) + 1)
        self.update_lineEdit_by_moving_slider()

    def slide_profile_number_to_left(self):
        self.slider.setValue(int(self.slider.value()) - 1)
        self.update_lineEdit_by_moving_slider()

    def slide_profile_number_to_end(self):
        self.slider.setValue(int(self.slider.maximum()))
        self.update_lineEdit_by_moving_slider()

    def profile_number_on_lineEdit(self):
        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):
            self.slider.setValue(
                int(np.where(
                    np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][self.combobox_frequency_profile.currentIndex()] -
                           float(self.lineEdit_slider.text().replace(",", "."))) ==
                    np.nanmin(
                        np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][self.combobox_frequency_profile.currentIndex()] -
                               float(self.lineEdit_slider.text().replace(",", ".")))))[0][0]))
        else:
            self.slider.setValue(
                int(np.where(
                    np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][self.combobox_frequency_profile.currentIndex()] -
                           float(self.lineEdit_slider.text().replace(",", "."))) ==
                    np.nanmin(
                        np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][self.combobox_frequency_profile.currentIndex()] -
                               float(self.lineEdit_slider.text().replace(",", ".")))))[0][0]))

    def update_lineEdit_by_moving_slider(self):
        if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,):
            self.lineEdit_slider.setText(
                str(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), self.slider.value()-1]))
        else:
            self.lineEdit_slider.setText(
                str(stg.time[self.combobox_acoustic_data_choice.currentIndex()][
                        self.combobox_frequency_profile.currentIndex(), self.slider.value()-1]))
