以NASA在1976年发布的B747气动数据报告中提取的数据库为例,讲解数据库提取、创建界面、可视化、平滑处理及插值等操作,具体代码如下
import sqlite3 #引入sqlite3库,对数据库进行提取
import csv #引入csv库,将数据库中提取的各气动系数的数据写入csv文件中
import tkinter as tk #引入tk库,创建可视化界面
import matplotlib.pyplot as plt #可视化
from scipy.interpolate import CubicSpline #将曲线和曲面平滑处理
import numpy as np #使用数组
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg #在界面中显示图片
def linear_interpolation_3d(x, y, z, x_values, y_values, z_values, x_new, y_new):#三维插值函数
idx_x = np.searchsorted(x_values, x) - 1
idx_y = np.searchsorted(y_values, y) - 1
if idx_x < 0:
idx_x = 0
if idx_x >= len(x_values) - 1:
idx_x = len(x_values) - 2
if idx_y < 0:
idx_y = 0
if idx_y >= len(y_values) - 1:
idx_y = len(y_values) - 2
z_new = z_values[idx_y, idx_x] + (z - z_values[idx_y, idx_x]) * (z_values[idx_y, idx_x + 1] - z_values[idx_y, idx_x]) / (x_values[idx_x + 1] - x_values[idx_x])
return z_new
conn=sqlite3.connect("B747data0.db")#读取数据库
cursor=conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
table_name = []
for table_info in tables: #遍历数据库
table_name.append(table_info[0])
mysel=cursor.execute(f"SELECT * FROM {table_info[0]};")
with open(table_info[0]+'.csv','w+',newline='') as file:#写入csv文件
write=csv.writer(file)
for row in mysel:
a = list(row)
write.writerow(a)
def draw_2d(): #二维可视化,在按钮中调用
category = category_var.get()
operation = operation_var.get()
if category == "CL":
if operation == "CL_Basic":
table_name0 = "clb_2_0_7"
elif category == "CD":
if operation == "CD_Basic":
table_name0 = "cdaab_3_0_5"
elif operation == "CD_M_flap_up":
table_name0 = "cdmal_3_0_8"
elif operation == "CD_M_flap_down":
table_name0 = "cdmah_3_0_9"
elif category == "CY":
if operation == "CY_beta":
table_name0 = "cybeta_7_0_5_1"
elif category == &