File size: 5,227 Bytes
787163a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | """
Author: Jason Jiang
Date: 2024.01.30
用于处理单个文件夹内的加速度数据,然后保存为HDF5文件,后续再合并所有文件夹的HDF5文件
"""
import h5py
import numpy as np
import os
import re # 引入正则表达式库
import sys
def delete_file(file_path:str) -> None:
"""
先删除本来的 h5 文件
"""
if os.path.exists(file_path):
os.remove(file_path)
print(f"Delete file: {file_path}")
def extract_floor_response(folder_path:str,
start_gms_number:int = 0,
number_of_gm:int=250) -> np.ndarray:
"""
读取所有 results 文件中包含 Acc_Roof 的文件,并存为一个大数组。
"""
# 初始化一个大型的 NumPy 数组,假设每个地震动数据有3000个时间点
Acc_Floor_Response = np.empty((57, number_of_gm, 3000))
for file_name in os.listdir(folder_path):
if all(keyword in file_name for keyword in ['Blg', 'RoofAcc']) and file_name.endswith('.txt'):
# 使用正则表达式从文件名中提取数字
numbers = re.findall(r'\d+', file_name)
# print(f"number: {numbers}")
blg_number = int(numbers[0]) -1 # 第一个数字是楼宇号
gms_number = int(numbers[1]) - start_gms_number -1 # 第二个数字是地震动号
# 读取文件
file_path = os.path.join(folder_path, file_name)
data = np.loadtxt(file_path, skiprows=1, usecols=1) # 只读取第二列
# 如果数据长度小于3000,补充0到长度为3000
if data.shape[0] < 3000:
data = np.pad(data, (0, 3000 - data.shape[0]), 'constant')
Acc_Floor_Response[blg_number, gms_number, :] = data
Acc_Floor_Response = np.transpose(Acc_Floor_Response, (1, 0, 2))
Acc_Floor_Response = Acc_Floor_Response.reshape(57*number_of_gm, 3000)
print(f"Acc_Blg_GMs.shape = {Acc_Floor_Response.shape}")
return Acc_Floor_Response # (14250, 3000)
def extract_damage_state(folder_path:str,
start_gms_number:int = 0,
number_of_gm:int=250) -> np.ndarray:
"""
读取所有 results 文件中损伤等级的数据,并存为一个大数组。
"""
# 初始化numpy数组
damage_states = np.zeros((57, number_of_gm, 1))
file_path = os.path.join(folder_path, 'DamageState.txt')
# 读取txt文件并处理数据
with open(file_path, 'r') as file:
next(file) # 跳过标题行
for line in file:
data = line.strip().split()
building_index = int(data[0]) - 1
earthquake_index = int(data[1].split('_')[1]) - start_gms_number - 1
damage_state = max(map(int, data[2:8]))
damage_states[building_index, earthquake_index, 0] = damage_state
damage_states = np.transpose(damage_states, (1, 0, 2))
damage_states = damage_states.reshape(57*number_of_gm, 1)
print(f"damage_states.shape = {damage_states.shape}")
return damage_states # (14250, 1)
def save_2_hdf5(file_name:str,
dataset_name:str,
array:np.ndarray) -> None:
"""存放数据到 HDF5 文件中。
Args:
file_name: 文件名
dataset_name: 数据集名称
array: 数据集
"""
# 创建一个新的 HDF5 文件
with h5py.File(file_name, 'a') as f:
# 将数据存储为数据集
f.create_dataset(dataset_name, data=array)
def save_h5():
# 确定当前工作目录是脚本所在的目录
current_folder = os.path.dirname(os.path.abspath(__file__))
# current_folder = os.getcwd()
os.chdir(current_folder)
print(f"current_folder = {current_folder}")
# 处理当前文件夹中的数据
folder_name = os.path.basename(current_folder)
print(f"folder_name = {folder_name}")
# 首先删除本来的 h5 文件
delete_file(os.path.join(f"{folder_name}.h5"))
match = re.match(r'^(\d+)-(\d+)$', folder_name)
if not match:
sys.exit(f"Folder name '{folder_name}' does not match the expected format.")
start_num = int(match.group(1))
end_num = int(match.group(2))
start_gms_number = start_num - 1
number_of_gm = end_num - start_num + 1
results_folder_path = os.path.join(current_folder, 'Results')
if not os.path.exists(results_folder_path):
sys.exit(f"The results folder does not exist: {results_folder_path}")
# 提取数据
print(f"Processing folder: {folder_name}")
Acc_Floor_Response = extract_floor_response(results_folder_path, start_gms_number, number_of_gm)
DS_Blg = extract_damage_state(results_folder_path, start_gms_number, number_of_gm)
# 存储数据到 HDF5 文件中
hdf5_filename = os.path.join(current_folder, f"{folder_name}.h5")
save_2_hdf5(hdf5_filename, 'Acc_Floor_Response', Acc_Floor_Response)
save_2_hdf5(hdf5_filename, 'DS_Blg', DS_Blg)
print(f"Data saved to {hdf5_filename}")
if __name__ == "__main__":
save_h5() |