# ============================================================================== #
    # settings  .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 -*-


""" this file includs global variables shared between tab """

import numpy as np
import pandas as pd
import datetime


# --- Save study ---
acoustic_data = []               # Acoustic data number                                                     # List of integer
dirname_save_as = ""             # Path to save a study                                                     # String
filename_save_as = ""            # File name to save a study                                                # String

# --- load raw data ---
dirname_open = ""                # Path to open a study                                                     # String
filename_open = ""               # File name to open a study                                                # String

# =========================================================
#                 --- ACOUSTIC DATA TAB ---
# =========================================================

# Variables names               # Description                                                               # Type

ABS_name = []                   # Acoustic Backscatter System name : ["Aquascat 1000R", "UB-SediFlow"]      # List of strings
temperature = 0.00                 # Temperature of measurements (One temperature for all measurements)        # Float
water_velocity = 0.00              # Speed of sound in water (One speed of sound for all measurements)         # Float

# --- Acoustic raw data ---
path_BS_raw_data = []           # Paths of the acoustic data files                                          # List of strings
filename_BS_raw_data = []       # Files names of the acoustic data files                                    # List of strings

BS_raw_data = []                # Acoustic raw data measurements : 3D arrays (freq x depth x time)          # List of arrays
BS_raw_data_reshape = []        # Acoustic raw data measurements : 2D arrays (freq x (depth x time))        # List of arrays

depth = []                      # Distance from transducer : 2D array (freq x depth)                        # List of arrays
depth_reshape = []              # Distance from transducer : 2D array (freq x (depth x time))               # List of arrays
depth_2D = []                   # Distance from transducer : 2D array (freq x depth)                        # List of arrays

time = []                       # Time of measurements : 2D array (freq x time)                             # List of arrays
time_reshape = []               # Time of measurements : 2D array (freq x (depth x time))                   # List of arrays

# --- Measurement information ---
date = []                       # Date of measurements                                                      # List of dates
hour = []                       # Time of measurements                                                      # List of time

distance_from_ABS_to_free_surface = []   # Set distance from ABS to free surface                            # List of floats

freq = []                       # Frequency of measurements : 1D array                                      # List of arrays
freq_text = []                  # Frequency of measurements : list of string                                # List of lists

kt_read = []                    # Constant of calibration kt of the ABS read from acoustic file             # List of list
                                    # for each frequency
kt_corrected = []               # Constant of calibration kt of the ABS corrected                           # list of float
                                    # Sometimes, the read values of kt are wrong. Then, we define
                                    # default values for all frequency of the ABS.

water_attenuation = []          # Sound attenuation in water for each frequency and for one temperature     # List of lists
nb_profiles = []                # Total number of profiles for each frequency = time length                 # List of lists
nb_profiles_per_sec = []        # Profile rate (Hz) for each frequency                                      # List of lists
nb_cells = []                   # Number of cells in profiles for each frequency                            # List of lists
cell_size = []                  # Cell size for each frequency (m)                                          # List of lists
pulse_length = []               # Pulse length (m)                                                          # List of lists
nb_pings_per_sec = []           # Number of pings per seconds (Hz)                                          # List of lists
nb_pings_averaged_per_profile = [] # Profiles per average                                                   # List of lists
gain_rx = []                    # Rx gain                                                                   # List of lists
gain_tx = []                    # Tx gain                                                                   # List of lists

DataFrame_acoustic = pd.DataFrame()

# --- Modify raw data limits ---
tmin = []                       # Minimum boundary of time for each recording : (index, value)              # List of tuples
tmax = []                       # Maximum boundary of time for each recording : (index, value)              # List of tuples
rmin = []                       # Minimum boundary of depth for each recording : (index, value)             # List of tuples
rmax = []                       # Maximum boundary of depth for each recording : (index, value)             # List of tuples

BS_cross_section = []           # BS data limited with tmin and tmax values                                 # List of arrays
depth_cross_section = []        # depth limited with rmin and rmax values                                   # List of arrays
time_cross_section = []         # time limited with rmin and rmax values                                    # List of arrays

# --- Detect bottom ---
BS_stream_bed = []              # BS data (raw or cross_section) with detected bottom :                     # List of arrays
                                    # 3D array : (freq x depth x time)
depth_bottom = []               # Depth value of th bottom : 1D array                                       # List of arrays
val_bottom = []                 # Level of the BS signal on the bottom : 1D array                           # List of arrays
ind_bottom = []                 # Index of bottom in depth array : list of int                              # List of lists
freq_bottom_detection = []      # Frequency use to detect the bottom : (index, string)                      # List of tuple

# depth_bottom_detection_min = []  # Min value to detect bottom on the first vertical                         # List of float
# depth_bottom_detection_max = [] # Max value to detect bottom on the first vertical                          # List of float
# depth_bottom_detection_1st_int_area = [] # interval for searching area                                      # List of float

# ----------------------------------------------------------------------------------------------------------------------
# =========================================================
#              --- SIGNAL PREPROCESSING TAB ---
# =========================================================

# Variables names               # Description                                                               # Type

path_BS_noise_data = []         # Paths of the acoustic noise data files                                    # List of strings
filename_BS_noise_data = []     # Files names of the acoustic noise data files                              # List of strings

noise_method = []               # = 0 if noise is computed from a file                                      # List of integer
                                # = 1 if noise is computed from profile tail (value)

noise_value = []                # Constant value get from profile tail                                      # List of float

BS_noise_raw_data = []          # Acoustic noise raw data measurements : 3D arrays                          # List of arrays
                                # If a noise file is downloaded, shape is (freq x depth_noise x time_noise)
                                # If noise is computed with value from profile tail, shape is the same than BS_raw_data or BS_cross_section or BS_stream_bed
time_noise = []                 # Time of noise data read from the noise file
depth_noise = []                # Depth of noise data read from the noise file
BS_noise_averaged_data = []     # BS_noise_raw_data is averaged and fit the shape of                        # List of arrays
                                #   BS_raw_data or BS_cross_section or BS_stream_bed

BS_mean = []                    # Average BS_raw_data or BS_cross_section or BS_stream_bed                  # List of arrays
                                #   to plot profile tail  : 1D arrays

SNR_filter_value = []           # Signal to Noise Ratio value use to delete point on BS signal              # List of float
Nb_cells_to_average_BS_signal = [] # Nb of cells used to average the BS signal                              # List of float

data_preprocessed = []          # New name of preprocessed data                                             # List of string

SNR_raw_data = []               # SNR from BS_raw_data and BS_noise_averaged_data : 3D array                # List of arrays
SNR_cross_section = []          # SNR from BS_cross_section and BS_noise_averaged_data : 3D array           # List of arrays
SNR_stream_bed = []             # SNR from BS_stream_bed and BS_noise_averaged_data : 3D array              # List of arrays

# --- Processed data in Signal Processing Tab ---
BS_raw_data_pre_process_SNR = [] # BS_raw_data preprocessed with SNR_filter_value                           # List of arrays
                                 #  (remove point if SNR < SNR_filter_value) : 3D array
BS_raw_data_pre_process_average = [] # BS_raw_data average with Nb_cells_to_average_BS_signal : 3D arrays   # List of arrays

BS_cross_section_pre_process_SNR = [] # BS_cross_section preprocessed with SNR_filter_value                 # List of arrays
                                    #  (remove point if SNR < SNR_filter_value) : 3D array
BS_cross_section_pre_process_average = [] # BS_cross_section averaged with Nb_cells_to_average_BS_signal : 3D arrays   # List of arrays

BS_stream_bed_pre_process_SNR = []  # BS_cross_section preprocessed with SNR_filter_value                   # List of arrays
                                    #  (remove point if SNR < SNR_filter_value) : 3D array
BS_stream_bed_pre_process_average = []  # BS_cross_section averaged with Nb_cells_to_average_BS_signal : 3D arrays   # List of arrays

# ----------------------------------------------------------------------------------------------------------------------
# =========================================================
#                  --- SAMPLE DATA TAB ---
# =========================================================

# Variables names               # Description                                                               # Type

# --- Fine sediment Data ---

path_fine = ""                  # Path of the fine sediments data files                                   # string
filename_fine = ""              # File name of the fine sediments data                                    # string
columns_fine = []               # Column names of the fine sediments data                                 # Index list

sample_fine = []                # Fine sediments sample [('Fn', n-1)]                                     # List of tuples

distance_from_bank_fine = []    # Distance from bank (m) of the fine sediments                            # List of float
depth_fine = []                 # Depth (m) of the fine sediments                                         # List of float
time_fine = []                  # Collection time (sec) of the fine sediments                             # List of float

radius_grain_fine = []          # Grain radius (m) of the fine sediments                                  # 1D Array
Ctot_fine = []                  # Total concentration (g/L) of the fine sediments                         # List of float
Ctot_fine_per_cent = []         # Total concentration (%) of the fine sediments                           # List of float
D50_fine = []                   # D50 diameter (um) of the fine sediments                                 # List of float
frac_vol_fine = []              # Volume fraction of the fine sediments                                   # 2D Array
frac_vol_fine_cumul = []        # Cumulated volume fraction (%) of the fine sediments                     # 2D Array

# --- Sand sediment Data ---

path_sand = ""                  # Path of the sand sediments data files                                   # string
filename_sand = ""              # File name of the sand sediments data                                    # string
columns_sand = []               # Column names of the sand sediments data                                 # Index list

sample_sand = []                # Sand sediments sample [('Sn', n-1)]                                     # List of tuples

distance_from_bank_sand = []    # Distance from bank (m) of the sand sediments                            # List of float
depth_sand = []                 # Depth (m) of the sand sediments                                         # List of float
time_sand = []                  # Collection time (sec) of the sand sediments                             # List of float

radius_grain_sand = []          # Grain radius (m) of the sand sediments                                  # 1D Array
Ctot_sand = []                  # Total concentration (g/L) of the sand sediments                         # List of float
Ctot_sand_per_cent = []         # Total concentration (%) of the sand sediments                           # List of float
D50_sand = []                   # D50 diameter (um) of the sand sediments                                 # List of float
frac_vol_sand = []              # Volume fraction of the sand sediments                                   # 2D Array
frac_vol_sand_cumul = []        # Cumulated volume fraction (%) of the sand sediments                     # 2D Array



# ----------------------------------------------------------------------------------------------------------------------
# =========================================================
#              --- SEDIMENT CALIBRATION TAB ---
# =========================================================

# Variables names               # Description                                                               # Type

# --- Parameters choice for calibration ---

frequencies_for_calibration = []  # Frequencies chosen for calibration [(f1_val, f1_ind), (f2_val, f2_ind)]  # List of 2 tuples
frequency_for_inversion = tuple() # Frequency chosen for inversion (finv_val, finv_ind)                     # Tuple

fine_sample_profile = []        # Fine samples choose for the profile in calibration [('Fn', n-1)]          # List of tuples

sand_sample_target = []         # Sand sample target for calibration [('Sn', n-1)]                          # List of 1 tuple
sand_sample_target_indice = []  # position index (time, depth) of the Sand sample target for                # List of 2 tuples
                                #   each calibration frequency
                                #   [(time_index_freq1, depth_index_freq1), (time_index_freq2, depth_index_freq2)]

# --- Calibration computation ---

range_lin_interp = np.array([]) # Point of the depth use to interpolate the fine sediment profile           # 1D array
M_profile_fine = np.array([])   # Interpolate concentration of fine sediments                               # 1D array

path_calibration_file = ""      # Path of the calibration file                                              # string
filename_calibration_file = ""  # File name of the calibration file                                         # string

ks = []                         # List [ks_freq1, ks_freq2]                                                # List of floats
sv = []                         # List [sv_freq1, sv_freq2]                                                # List of floats
X_exponent = []                 # List of one value [X_exponent]                                           # List of one float
alpha_s = []                    # List [alphas_freq1, alphas_freq2]                                        # List of floats
zeta = []                       # List [zeta_freq1, zeta_freq2]                                            # List of floats

kt2D = []                       # List of kt2D, computed from kt_corrected or kt_read                      # List of 2D arrays
kt3D = []                       # List of kt3D, computed from kt2D                                         # List of 3D arrays
J_cross_section = []            # List of J_cross_section compute for all acoustic data recording          # List of lists of 3D arrays
                                # [[J_cross_section_freq1, J_cross_section_freq2], [], []]
FCB = []                        # 3D array
depth_real = []                 # 3D array
lin_reg = []                    # (lin_reg_compute.slope, lin_reg_compute.intercept)                       # Tuple

# ----------------------------------------------------------------------------------------------------------------------
# =========================================================
#               --- ACOUSTIC INVERSION TAB ---
# =========================================================

# Variables names               # Description                                                               # Type

VBI_cross_section = []          # Volume Backscattering Index                                               # List of 2D array
fine_sample_position = []       # Fine samples indexes position for time and depth [(time_index, depth_index)]  # List of tuples
sand_sample_position = []       # Sand samples indexes position for time and depth [(time_index, depth_index)]  # List of tuples
SSC_fine = []                   # Suspended Sediment Concentration of the fine sediments                    # List of one 3D array
SSC_sand = []                   # Suspended Sediment Concentration of the sand sediments                    # List of one 3D array




