68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
class WindDataHandler:
|
||
def __init__(self, dll_path='./LAMPWindData.dll'):
|
||
self.dll_path = dll_path
|
||
self.wind_lib = ctypes.CDLL(dll_path)
|
||
self._setup_prototypes()
|
||
self.current_file_info = None
|
||
|
||
def _setup_prototypes(self):
|
||
"""设置 DLL 函数的参数和返回类型"""
|
||
# ... 函数原型设置代码见上一节,此处省略 ...
|
||
|
||
def get_data_file_info(self, filepath):
|
||
"""获取风场数据文件信息"""
|
||
# 将Python字符串转换为字节字符串
|
||
filepath_bytes = filepath.encode('utf-8')
|
||
info = self.wind_lib.getDataFileInfo(filepath_bytes)
|
||
self.current_file_info = info
|
||
return info
|
||
|
||
def get_wind_data_file_time_arr(self, filepath, info):
|
||
"""获取时间数组"""
|
||
filepath_bytes = filepath.encode('utf-8')
|
||
if info.Num <= 0:
|
||
return []
|
||
|
||
# 创建足够大的数组来存储时间戳
|
||
time_arr = (ctypes.c_int64 * info.Num)()
|
||
result = self.wind_lib.get_WindDataFileTimeArr(filepath_bytes, info, time_arr)
|
||
|
||
if result == 0: # 假设返回0表示成功
|
||
return list(time_arr)
|
||
else:
|
||
print(f"Error getting time array: {result}")
|
||
return []
|
||
|
||
def read_wind_data_file(self, filepath, info, time_id):
|
||
"""读取指定时间ID的风场数据 (U/V分量)"""
|
||
filepath_bytes = filepath.encode('utf-8')
|
||
layer_size = info.Height * info.Width
|
||
|
||
# 创建数组来存储U和V分量数据
|
||
u_arr = (ctypes.c_double * layer_size)()
|
||
v_arr = (ctypes.c_double * layer_size)()
|
||
|
||
result = self.wind_lib.Read_WindDataFile(filepath_bytes, info, time_id, u_arr, v_arr)
|
||
|
||
if result == 0:
|
||
# 将ctypes数组转换为Python列表或numpy数组(如果可用)
|
||
return list(u_arr), list(v_arr)
|
||
else:
|
||
print(f"Error reading wind data: {result}")
|
||
return None, None
|
||
|
||
def write_wind_data_file(self, filepath, info, time_id, u_arr, v_arr):
|
||
"""将风场数据写入文件"""
|
||
filepath_bytes = filepath.encode('utf-8')
|
||
layer_size = info.Height * info.Width
|
||
|
||
# 确保输入数据长度正确
|
||
if len(u_arr) != layer_size or len(v_arr) != layer_size:
|
||
raise ValueError(f"UArr and VArr must have length {layer_size}")
|
||
|
||
# 将Python列表转换为ctypes数组
|
||
u_arr_ctypes = (ctypes.c_double * layer_size)(*u_arr)
|
||
v_arr_ctypes = (ctypes.c_double * layer_size)(*v_arr)
|
||
|
||
result = self.wind_lib.Write_WindDataFile(filepath_bytes, info, time_id, u_arr_ctypes, v_arr_ctypes)
|
||
return result |