from Model.AquascatDataLoader import RawAquascatData import numpy as np import pandas as pd import datetime import matplotlib.pyplot as plt from matplotlib.colors import LogNorm from Model.udt_extract.raw_extract import raw_extract # raw_20210519_102332.udt raw_20210520_135452.udt raw_20210525_092759.udt raw_20210525_080454.udt path_BS_raw_data0 = ("/home/bmoudjed/Documents/3 SSC acoustic meas project/Graphical interface project/Data/APAVER_2021/" "Rhone_20210519/Rhone_20210519/record/") filename0 = "raw_20210526_153310.udt" class AcousticDataLoaderUBSediFlow(): def __init__(self, path_BS_raw_data: str): path_BS_raw_data = path_BS_raw_data0 + filename0 self.path_BS_raw_data = path_BS_raw_data # --- Extract Backscatter acoustic raw data with class --- # Extraction function: device_name, time_begin, time_end, param_us_dicts, data_us_dicts, data_dicts, settings_dict \ = raw_extract(self.path_BS_raw_data) # --- Date and Hour of measurements read on udt data file --- filename = self.path_BS_raw_data[-23:] date_and_time = datetime.datetime(year=int(filename[4:8]), month=int(filename[8:10]), day=int(filename[10:12]), hour=int(filename[13:15]), minute=int(filename[15:17]), second=int(filename[17:19])) self._date = date_and_time.date() print(f"date : {self._date}") self._hour = date_and_time.time() print(f"time : {self._hour}") self._freq = np.array([[]]) self._r = np.array([[]]) self._time = np.array([[]]) self._BS_raw_data = np.array([[[]]]) time_len = [] for config in param_us_dicts.keys(): # print("-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x") # print(f"config : {config} \n") for channel in param_us_dicts[config].keys(): print("-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x") print(f"channel : {channel} \n") print(data_us_dicts[config][channel].keys()) # print(data_us_dicts[config][channel]['echo_avg_profile']) # --- Frequencies --- self._freq = np.append(self._freq, param_us_dicts[config][channel]['f0']) # --- Depth for each frequencies --- depth = [param_us_dicts[config][channel]['r_cell1'] * i for i in list(range(param_us_dicts[config][channel]['n_cell']))] if self._r.shape[1] == 0: self._r = np.array([depth]) else: self._r = np.append(self._r, [depth], axis=0) # --- Time for each frequencies --- time = [[(t - data_us_dicts[config][channel]['echo_avg_profile']['time'][0]).total_seconds() for t in data_us_dicts[config][channel]['echo_avg_profile']['time']]] time_len = np.append(time_len, len(time[0])) if len(time_len) == 1: # print(f"1 time length : {len(time[0])}") self._time = np.array(time) # print(f"self._time.shape {self._time.shape}") elif self._time.shape[1] == len(time[0]): # print(f"2 time length : {len(time[0])}") self._time = np.append(self._time, time, axis=0) # print(f"self._time.shape {self._time.shape}") elif self._time.shape[1] > len(time[0]): # print(f"3 time length : {len(time[0])}") # print(f"self._time.shape {self._time.shape}") # print([int(np.min(time_len)) + int(i) - 1 for i in range(1, int(np.max(time_len))-int(np.min(time_len))+1)]) self._time = np.delete(self._time, [int(np.min(time_len)) + int(i) - 1 for i in range(1, int(np.max(time_len))-int(np.min(time_len))+1)], axis=1) self._time = np.append(self._time, time, axis=0) # print(f"self._time.shape {self._time.shape}") elif self._time.shape[1] < len(time[0]): # print(f"4 time length : {len(time[0])}") time = time[:int(np.max(time_len)) - (int(np.max(time_len)) - int(np.min(time_len)))] self._time = np.append(self._time, time, axis=0) # print(f"self._time.shape {self._time.shape}") # --- US Backscatter raw signal --- for f, freq in enumerate(self._freq): if f == 0: # print(data_us_dicts[config][channel]['echo_avg_profile']['data']) self._BS_raw_data = np.array([np.reshape(data_us_dicts[config][channel]['echo_avg_profile']['data'], (self._time.shape[1], self._r.shape[1])).transpose()]) # print(self._BS_raw_data.shape) else: self._BS_raw_data = np.append(self._BS_raw_data, np.array([np.reshape(np.array( data_us_dicts[config][channel]['echo_avg_profile']['data']), (self._time.shape[1], self._r.shape[1])).transpose()]), axis=0) # print(self._BS_raw_data.shape) # print(len(self._BS_raw_data)) # print(self._BS_raw_data) print("self._time.shape ", self._time.shape) print("self._r.shape ", self._r.shape) self._freq_text = np.array([str(f) + " MHz" for f in [np.round(f*1e-6, 2) for f in self._freq]]) print("self._freq_text ", self._freq_text) print("self._freq_text ", self._freq) # self._BS_raw_data = np.array(np.reshape(self._BS_raw_data, (len(self._freq), self._r.shape[1], self._time.shape[1]))) print("self._BS_raw_data.shape ", self._BS_raw_data.shape) # print("device_name ", device_name, "\n") # print("time_begin ", time_begin, "\n") # print("time_end ", time_end, "\n") # print(f"param_dicts keys {param_us_dicts.keys()} \n") # print(param_us_dicts, "\n") # for i in range(len(list(param_us_dicts.keys()))): # print(f"param_us_dicts {i} : {list(param_us_dicts.items())[i]} \n") # # print("settings_dict ", settings_dict, "\n") # print(f"keys in data_us_dicts {data_us_dicts[1][1].keys()} \n") # # les clés du dictionnaire data_us_dicts : # # dict_keys(['echo_avg_profile', 'saturation_avg_profile', 'velocity_avg_profile', 'snr_doppler_avg_profile', # # 'velocity_std_profile', 'a1_param', 'a0_param', 'noise_g_high', 'noise_g_low']) # print(f"data_us_dicts keys in echo avg profile {data_us_dicts[1][1]['echo_avg_profile'].keys()} \n") # print(f"number of profiles {len(data_us_dicts[1][1]['echo_avg_profile']['data'])} \n") # print(f"number of cells {data_us_dicts[1][1]['echo_avg_profile']['data'][0].shape} \n") # self._data_BS = RawAquascatData(self.path_BS_raw_data) # self._nb_profiles = self._data_BS.NumProfiles # self._nb_profiles_per_sec = self._data_BS.ProfileRate # self._nb_cells = self._data_BS.NumCells # self._cell_size = self._data_BS.cellSize # self._pulse_length = self._data_BS.TxPulseLength # self._nb_pings_per_sec = self._data_BS.PingRate # self._nb_pings_averaged_per_profile = self._data_BS.Average # self._kt = self._data_BS.Kt # self._gain_rx = self._data_BS.RxGain # self._gain_tx = self._data_BS.TxGain # self._snr = np.array([]) # self._snr_reshape = np.array([]) # self._time_snr = np.array([]) # print(type(self._gain_tx)) # print(["BS - " + f for f in self._freq_text]) # print(self._time.shape[0]*self._r.shape[0]*4) # print(self._time[np.where(np.floor(self._time) == 175)]) # print(np.where((self._time) == 155)[0][0]) # fig, ax = plt.subplots(nrows=len(self._freq), ncols=1) # for f, freq in enumerate(self._freq): # # print(f"{f} : {freq} \n") # pcm = ax[f].pcolormesh(self._time[f, :], self._r[f, :], (self._BS_raw_data[f, :, :self._time.shape[1]]), # cmap='viridis', # norm=LogNorm(vmin=np.min(self._BS_raw_data[f, :, :]), vmax=np.max(self._BS_raw_data[f, :, :])), shading='gouraud') # ) # # ax.pcolormesh(range(self._BS_raw_data.shape[2]), range(self._BS_raw_data.shape[0]), self._BS_raw_data[:, 1, :], cmap='viridis', # # norm=LogNorm(vmin=1e-5, vmax=np.max(self._BS_raw_data[:, 0, :]))) # , shading='gouraud') # fig.colorbar(pcm, ax=ax[:], shrink=1, location='right') # plt.show() # fig, ax = plt.subplots(nrows=1, ncols=1) # ax.plot(list(range(self._time.shape[1])), self._time[0, :]) # # ax.set_ylim(2, 20) # plt.show() # print(self.reshape_BS_raw_cross_section()) # self.reshape_BS_raw_cross_section() # self.reshape_r() # self.reshape_t() def reshape_BS_raw_cross_section(self): BS_raw_cross_section = np.reshape(self._BS_raw_data, (self._r.shape[1]*self._time.shape[1], len(self._freq)), order="F") # print(BS_raw_cross_section.shape) return BS_raw_cross_section def reshape_r(self): r = np.zeros((self._r.shape[1]*self._time.shape[1], len(self._freq))) for i, _ in enumerate(self._freq): r[:, i] = np.repeat(self._r[i, :], self._time.shape[1]) # print(r.shape) return r # def compute_r_2D(self): # r2D = np.repeat(self._r, self._time.size, axis=1) # return r2D def reshape_t(self): t = np.zeros((self._r.shape[1]*self._time.shape[1], len(self._freq))) for i, _ in enumerate(self._freq): t[:, i] = np.repeat(self._time[i, :], self._r.shape[1]) # print(t.shape) return t # def concatenate_data(self): # self.reshape_BS_raw_cross_section() # # print(self.reshape_t().shape) # # print(se.lf.reshape_BS_raw_cross_section().shape) # df = pd.DataFrame(np.concatenate((self.reshape_t(), self.reshape_BS_raw_cross_section()), axis=1), # columns=["time"] + self._freq_text) # return df if __name__ == "__main__": AcousticDataLoaderUBSediFlow(path_BS_raw_data0 + filename0)