acoused/Model/create_table_for_save_as.py

329 lines
16 KiB
Python

import numpy as np
from PyQt5.QtWidgets import QFileDialog, QApplication
import sqlite3
import settings as stg
from os import chdir
import time
class CreateTableForSaveAs():
def __init__(self):
self.create_AcousticFile = """CREATE TABLE File(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
acoustic_file STRING)"""
self.create_Measure = """ CREATE TABLE Measure(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
frequency FLOAT,
kt FLOAT,
NbProfiles INTEGER,
NbProfilesPerSeconds FLOAT,
NbCells INTEGER,
CellSize FLOAT,
PulseLength FLOAT,
NbPingsPerSeconds FLOAT,
NbPingsAveragedPerProfile FLOAT,
GainRx FLOAT,
GainTx FLOAT)
"""
# for i in stg.acoustic_data:
# exec("self.create_BSRawData_" + str(i) + " = '''CREATE TABLE BSRawData_" + str(i) +
# "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
# "acoustic_data INTEGER, " +
# " ".join(["time_" + str(int(f)) + " FLOAT," for f in stg.freq[i][:-1]]) +
# ["time_" + str(int(stg.freq[i][-1]))][0] + " FLOAT" + ")''' ")
# for i in stg.acoustic_data:
# exec("self.create_BSRawData_" + str(i) + " = '''CREATE TABLE BSRawData_" + str(i) +
# "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
# "acoustic_data INTEGER, " +
# " ".join(["time_" + str(int(f)) + " FLOAT," for f in stg.freq[i]]) +
# " ".join(["depth_" + str(int(f)) + " FLOAT," for f in stg.freq[i]]) +
# " ".join(["BS_raw_data_" + str(int(f)) + " FLOAT," for f in stg.freq[i][:-1]]) +
# ["BS_raw_data_" + str(int(stg.freq[i][-1]))][0] + " FLOAT" + ")''' ")
self.create_BSRawData = '''CREATE TABLE BSRawData(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
time BLOB,
depth BLOB,
BS_raw_data BLOB)'''
self.create_Settings = '''CREATE TABLE Settings(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
temperature FLOAT,
time_min_ind INTEGER,
time_min_val FLOAT,
time_max_ind INTEGER,
time_max_val FLOAT,
depth_min_ind INTEGER,
depth_min_val FLOAT,
depth_max_ind INTEGER,
depth_max_val FLOAT)'''
self.open_file_dialog()
start = time.time()
self.create_table()
print(f"end : {time.time() - start} sec")
def open_file_dialog(self):
options = QFileDialog.Options()
name = QFileDialog.getSaveFileName(
caption="Save As", directory="", filter="AcouSed Files (*.acd)", options=options)
if name:
stg.dirname_save_as = "/".join(name[0].split("/")[:-1]) + "/"
stg.filename_save_as = name[0].split("/")[-1]
chdir(stg.dirname_save_as)
# file = open(stg.filename_save_as, 'w')
# file.write(self.create_Measure)
# file.close()
def create_table(self):
# Create a new database and open a database connection to allow sqlite3 to work with it.
cnx = sqlite3.connect(stg.filename_save_as + '.acd')
# Create database cursor to execute SQL statements and fetch results from SQL queries.
cur = cnx.cursor()
# --------------------------------------------------------------------------------------------------------------
# --- Table File ---
start_table_File = time.time()
cur.execute("DROP TABLE if exists File")
cur.execute(self.create_AcousticFile)
for i in stg.acoustic_data:
query0 = (f"INSERT into File(acoustic_data, acoustic_file)"
f"VALUES({stg.acoustic_data[i]}, {str(stg.filename_BS_raw_data[i]).split('.')[0]})")
cur.execute(query0)
cnx.commit()
print(f"table File : {time.time() - start_table_File} sec")
# --------------------------------------------------------------------------------------------------------------
# --- Table Measure ---
start_table_Measure = time.time()
# Drop Table if exists
cur.execute("DROP TABLE if exists Measure")
# Execute the CREATE TABLE statement
cur.execute(self.create_Measure)
# Fill the table Measure
for i in stg.acoustic_data:
for f, k, r, t in zip(stg.freq[i], stg.kt[i], stg.gain_rx[i], stg.gain_tx[i]):
query = (f"INSERT into Measure(acoustic_data, frequency, kt, NbProfiles, NbProfilesPerSeconds, "
f"NbCells, CellSize, PulseLength, NbPingsPerSeconds, "
f"NbPingsAveragedPerProfile, GainRx, GainTx) "
f"VALUES({i}, {f}, {k}, {stg.nb_profiles[i]}, {stg.nb_profiles_per_sec[i]}, "
f"{stg.nb_cells[i]}, {stg.cell_size[i]}, {stg.pulse_length[i]}, {stg.nb_pings_per_sec[i]}, "
f"{stg.nb_pings_averaged_per_profile[i]}, {r}, {t})")
cur.execute(query)
# Commit the transaction after executing INSERT.
cnx.commit()
print(f"table Measure : {time.time() - start_table_Measure} sec")
# --------------------------------------------------------------------------------------------------------------
# --- Table BSRawData_i ---
start_table_BSRawData = time.time()
cur.execute('DROP TABLE if exists BSRawData')
# # Drop Table BSRawData_i if exists
# for i in stg.acoustic_data:
# exec("cur.execute('DROP TABLE if exists BSRawData_" + str(i) + "')")
#
# # Execute the CREATE TABLE BSRawData_i statement
# for i in stg.acoustic_data:
# exec("cur.execute(self.create_BSRawData_" + str(i) + ")")
# Fill the table BSRawData_i
# # --- Fill table BSRawData_i with acoustic data integer ---
# for i in stg.acoustic_data:
# for j in range(stg.time_reshape[i].shape[0]):
# exec(f"query1 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data)" +
# f" VALUES({i}) ''') ")
#
# exec("cur.execute(query1)")
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# for i in stg.acoustic_data:
# , depth, BS_raw_data
cur.execute(self.create_BSRawData)
# homer = np.array([1, 2, 3, 4, 5])
# bart = np.array([6, 7, 8, 9, 10])
# lisa = np.array([11, 12, 13, 14, 15])
# marge = homer.tobytes()
# print("to string : ", homer.tostring())
# print("to bytes : ", homer.tobytes())
# query2 = f''' INSERT into BSRawData(acoustic_data, time) VALUES({0}, {marge}) '''
# cur.execute('''CREATE TABLE IF NOT EXISTS BSRawData(time BLOB)''')
for i in stg.acoustic_data:
# cur.execute(''' INSERT into BSRawData(acoustic_data, time, depth, BS_raw_data) VALUES(?, ?, ?, ?)''',
# (stg.acoustic_data[i], homer.tobytes(),
# bart.tobytes(), lisa.tobytes()))
cur.execute(''' INSERT into BSRawData(acoustic_data, time, depth, BS_raw_data, BS_cross_section)
VALUES(?, ?, ?, ?)''',
(stg.acoustic_data[i], stg.time[i].tobytes(),
stg.depth[i].tobytes(), stg.BS_raw_data[i].tobytes()))
# cur.execute(''' INSERT into BSRawData(time) VALUES(?)''', (marge,))
# query2 = f''' INSERT into BSRawData(acoustic_data, time) VALUES({0}, {stg.time[0][0, :].tobytes()}) ''' # , {stg.depth[i].tobytes()}, {stg.BS_raw_data[i].tobytes()}
# for j in range(stg.time_reshape[i].shape[0]):
#
# # print("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# # ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ", " +
# # ", ".join(["depth_" + str(int(f)) for f in stg.freq[i]]) + ")" +
# # f" VALUES" + str(eval("0, " +
# # ", ".join(["stg.time_reshape[" + str(i) + "][" + str(
# # j) + ", " + str(k) + "]" for k, _ in
# # enumerate(stg.freq[i])]) + ", " +
# # ", ".join(["stg.depth_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]" for k, _ in enumerate(stg.freq[i])]))) + " ''' )")
#
# exec("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ", " +
# ", ".join(["depth_" + str(int(f)) for f in stg.freq[i]]) + ", " +
# ", ".join(["BS_raw_data_" + str(int(f)) for f in stg.freq[i]]) + ")"
# f" VALUES" + str(eval(str(i) + ", " +
# ", ".join(["stg.time_reshape[" + str(i) + "][" + str(
# j) + ", " + str(k) + "]" for k, _ in
# enumerate(stg.freq[i])]) + ", " +
# ", ".join(["stg.depth_reshape[" + str(i) + "][" + str(
# j) + ", " + str(k) + "]" for k, _ in
# enumerate(stg.freq[i])]) + ", " +
# ", ".join(["stg.BS_raw_data_reshape[" + str(i) + "][" + str(
# j) + ", " + str(k) + "]" for k, _ in
# enumerate(stg.freq[i])]))) + " ''' )")
# cur.execute(query2)
# Commit the transaction after executing INSERT.
cnx.commit()
print(f"table BSRawData : {time.time() - start_table_BSRawData} sec")
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# # --- Fill table BSRawData_i with data stg.time_reshape ---
# for i in stg.acoustic_data:
# for j in range(stg.time_reshape[i].shape[0]):
#
# # print("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# # ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ")" +
# # f" VALUES" + str(eval("0, " + ", ".join(["stg.time_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]" for k, _ in enumerate(stg.freq[i])]))) + " ''' )")
#
# exec("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ")" +
# f" VALUES" + str(eval("0, " + ", ".join(["stg.time_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]" for k, _ in enumerate(stg.freq[i])]))) + " ''' )")
#
# exec("cur.execute(query2)")
#
# # Commit the transaction after executing INSERT.
# cnx.commit()
#
# # --- Fill table BSRawData_i with data stg.depth_reshape ---
# for i in stg.acoustic_data:
# for f in stg.freq[i]:
# exec("query3 = ('''ALTER TABLE BSRawData_" + str(i) +
# " ADD " + ["depth_" + str(int(f)) + " FLOAT"][0] + " ''')")
#
# exec("cur.execute(query3)")
#
# # Commit the transaction after executing INSERT.
# cnx.commit()
#
# for i in stg.acoustic_data:
# for k, f in enumerate(stg.freq[i]):
# for j in range(stg.depth_reshape[i].shape[0]):
# exec("query4 = '''UPDATE BSRawData_" + str(i) +
# " SET " + ["depth_" + str(int(f))][0] + " = " +
# str(eval("stg.depth_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]")) +
# " WHERE ID = " + str(j + 1) + " ''' ")
#
# exec("cur.execute(query4)")
# # --- Fill table BSRawData_i with data stg.BS_raw_data_reshape ---
# for i in stg.acoustic_data:
# for f in stg.freq[i]:
#
# exec("query5 = ('''ALTER TABLE BSRawData_" + str(i) +
# " ADD " + ["BS_raw_data_" + str(int(f)) + " FLOAT"][0] + " ''')")
#
# exec("cur.execute(query5)")
#
# # Commit the transaction after executing INSERT.
# cnx.commit()
#
# for i in stg.acoustic_data:
# for k, f in enumerate(stg.freq[i]):
# for j in range(stg.BS_raw_data_reshape[i].shape[0]):
#
# exec("query6 = '''UPDATE BSRawData_" + str(i) +
# " SET " + ["BS_raw_data_" + str(int(f))][0] + " = " +
# str(eval("stg.BS_raw_data_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]")) +
# " WHERE ID = " + str(j+1) + " ''' ")
#
# exec("cur.execute(query6)")
#
# cnx.commit()
# --------------------------------------------------------------------------------------------------------------
# --- Table Settings ---
start_table_Settings = time.time()
cur.execute("DROP TABLE if exists Settings")
cur.execute(self.create_Settings)
print(stg.acoustic_data, stg.temperature, stg.rmin, stg.rmax, stg.tmin, stg.tmax)
for i in stg.acoustic_data:
query7 = (f'''INSERT into Settings(acoustic_data, temperature,
time_min_ind, time_min_val, time_max_ind, time_max_val,
depth_min_ind, depth_min_val, depth_max_ind, depth_max_val)
VALUES({stg.acoustic_data[i]},
{stg.temperature[i]},
{stg.tmin[i][0]},
{stg.tmin[i][1]},
{stg.tmax[i][0]},
{stg.tmax[i][1]},
{stg.rmin[i][0]},
{stg.rmin[i][1]},
{stg.rmax[i][0]},
{stg.rmax[i][1]})''')
cur.execute(query7)
cnx.commit()
print(f"table Settings : {time.time() - start_table_Settings} sec")
# Close database cursor
cur.close()
# Close database connection
cnx.close()