acoused/Model/read_table_for_open.py

158 lines
6.8 KiB
Python

import numpy as np
from PyQt5.QtWidgets import QFileDialog, QApplication
import sqlite3
from os import path, chdir
import settings as stg
class ReadTableForOpen:
def __init__(self):
self.open_file_dialog()
chdir(stg.dirname_open)
self.sql_file_to_open = open(stg.filename_open)
self.read_table()
# self.reshape_variables()
def open_file_dialog(self):
name = QFileDialog.getOpenFileName(caption="Open Acoused file", directory="", filter="Acoused file (*.acd)")
if name:
stg.dirname_open = path.dirname(name[0])
stg.filename_open = path.basename(name[0])
def read_table(self):
# connexion to File db
cnx = sqlite3.connect(stg.filename_open)
# Create database cursor to execute SQL statements and fetch results from SQL queries.
cur = cnx.cursor()
query = '''SELECT acoustic_data, acoustic_file FROM File'''
data = cur.execute(query).fetchall()
stg.acoustic_data, stg.filename_BS_raw_data = [x[0] for x in data], [y[1] for y in data]
for i in stg.acoustic_data:
query1 = f'''SELECT frequency, kt, NbProfiles, NbProfilesPerSeconds, "
f"NbCells, CellSize, PulseLength, NbPingsPerSeconds, "
f"NbPingsAveragedPerProfile, GainRx, GainTx
FROM Measure WHERE (acoustic_data = {i})'''
data1 = cur.execute(query1).fetchall()
# print(data1)
# print([x[0] for x in data1])
stg.freq.append([x[0] for x in data1])
stg.kt.append([x[1] for x in data1])
stg.nb_profiles.append([x[2] for x in data1])
stg.nb_profiles_per_sec.append([x[3] for x in data1])
stg.nb_cells.append([x[4] for x in data1])
stg.cell_size.append([x[5] for x in data1])
stg.pulse_length.append([x[6] for x in data1])
stg.nb_pings_per_sec.append([x[7] for x in data1])
stg.nb_pings_averaged_per_profile.append([x[8] for x in data1])
stg.gain_rx.append([x[9] for x in data1])
stg.gain_tx.append([x[10] for x in data1])
# for f_ind, f_val in enumerate(stg.freq[i]):
#
# exec("query2 = '''SELECT time_" + str(int(f_val)) + ", depth_" + str(int(f_val)) + ", BS_raw_data_" + str(int(f_val)) +
# " FROM BSRawData_" + str(int(i)) + " WHERE (acoustic_data = " + str(i) + ") ''' ")
# exec("data2 = cur.execute(query2).fetchall()")
# # print(eval("data2"))
#
# if f_ind == 0:
# time_reshape_temp = np.array([[x[0] for x in eval("data2")]])
# # print("1 time_reshape_temp.shape ", time_reshape_temp.shape)
# depth_reshape_temp = np.array([[x[1] for x in eval("data2")]])
# BS_raw_data_reshape_temp = np.array([[x[2] for x in eval("data2")]])
# else:
# time_reshape_temp = np.insert(time_reshape_temp, f_ind, [x[0] for x in eval("data2")], axis=0)
# # print("2 time_reshape_temp.shape ", time_reshape_temp.shape)
# depth_reshape_temp = np.insert(depth_reshape_temp, f_ind, [x[1] for x in eval("data2")], axis=0)
# BS_raw_data_reshape_temp = np.insert(BS_raw_data_reshape_temp, f_ind, [x[1] for x in eval("data2")], axis=0)
#
# stg.time_reshape.append(time_reshape_temp.transpose())
# # print(len(stg.time_reshape))
# # print(stg.time_reshape[i].shape)
# stg.depth_reshape.append(depth_reshape_temp.transpose())
# stg.BS_raw_data_reshape.append(BS_raw_data_reshape_temp.transpose())
query2 = f'''SELECT acoustic_data, time, depth, BS_raw_data FROM BSRawData WHERE (acoustic_data = {i})'''
data2 = cur.execute(query2).fetchall()
print("len(data2) ", len(data2[0]))
# print("data2 : ", data2)
# data2_retreived = np.frombuffer(data2[0][1], dtype=np.float64)
# print("", data2_retreived)
stg.time.append(np.frombuffer(data2[0][1], dtype=np.float64).reshape((len(stg.freq[i]), -1)))
print(stg.time[0].shape)
print(stg.time)
stg.depth.append(np.frombuffer(data2[0][2], dtype=np.float64).reshape(len(stg.freq[i]), -1))
print(stg.depth[0].shape)
print(stg.depth)
stg.BS_raw_data.append(np.frombuffer(data2[0][3], dtype=np.float64))
# time_reshape_temp = np.insert(time_reshape_temp, time_reshape_temp.shape[0], [x[0] for x in eval("data2")], axis=0)
# stg.depth_reshape.append([x[2] for x in data2])
# stg.BS_raw_data_reshape.append([x[3] for x in data2])
# query = '''SELECT acoustic_file FROM File'''
# cur.execute(query)
# stg.acoustic_data = cur.fetchall()
# Close database cursor
cur.close()
# Close database connection
cnx.close()
def reshape_variables(self):
for i in stg.acoustic_data:
for f, _ in enumerate(stg.freq[i]):
if f == 0:
depth_temp = np.array([
stg.depth_reshape[i][np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][0]:
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][1], f]
])
time_temp = np.array([
stg.time_reshape[i][
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0], f]
])
else:
# print(np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f]))
depth_temp = np.insert(depth_temp,
depth_temp.shape[0],
stg.depth_reshape[i][np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][0]:
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][1], f],
axis=0)
time_temp = np.insert(time_temp,
time_temp.shape[0],
stg.time_reshape[i][
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0], f],
axis=0)
stg.depth.append(depth_temp)
stg.time.append(time_temp)
stg.BS_raw_data.append(np.reshape(stg.BS_raw_data_reshape[i],
(len(stg.freq[i]), stg.depth[i].shape[1], stg.time[i].shape[1])))