JasonXF's picture
Add files using upload-large-folder tool
787163a verified
Raw
History Blame Contribute Delete
2.39 kB
import os
import re
import h5py
import numpy as np
def read_h5_data(h5_path, dataset_name):
with h5py.File(h5_path, 'r') as h5_file:
return h5_file[dataset_name][()]
def main(file_name:str):
target_directory = os.getcwd()
folder_pattern = re.compile(r'^(\d+)-(\d+)$') # 修改正则表达式以匹配文件夹
file_pattern = re.compile(r'^(\d+)-(\d+)\.h5$') # 正则表达式匹配.h5文件
print(f"target_directory = {target_directory}")
# 获取所有匹配文件夹,并根据数字范围对它们进行排序
folders = [f for f in os.listdir(target_directory) if folder_pattern.match(f)]
folders.sort(key=lambda x: int(folder_pattern.match(x).group(1)))
print(f"Found {len(folders)} folders: {folders}")
# 初始化列表以存储拼接后的数组
Acc_Floor_Responses = []
DS_Blgs = []
# 循环读取每个文件夹中的HDF5文件,并拼接数据
for folder in folders:
h5_file_name = f"{folder}.h5"
h5_path = os.path.join(target_directory, folder, h5_file_name) # 文件路径包括文件夹名称
if os.path.exists(h5_path) and file_pattern.match(h5_file_name):
print(f"Processing {h5_path}...")
Acc_Floor_Response = read_h5_data(h5_path, 'Acc_Floor_Response')
DS_Blg = read_h5_data(h5_path, 'DS_Blg')
# 将数据添加到列表中
Acc_Floor_Responses.append(Acc_Floor_Response)
DS_Blgs.append(DS_Blg)
else:
print(f"Expected file {h5_file_name} not found in {folder}")
# 使用numpy.concatenate进行数组拼接
Acc_Floor_Responses = np.concatenate(Acc_Floor_Responses, axis=0)
DS_Blgs = np.concatenate(DS_Blgs, axis=0)
# 打印出拼接后的数组形状以检查
print(f"Acc_Floor_Responses shape: {Acc_Floor_Responses.shape}")
print(f"DS_Blgs shape: {DS_Blgs.shape}")
# 可以选择保存拼接后的大数组
h5_combined_path = os.path.join(target_directory, file_name)
with h5py.File(h5_combined_path, 'w') as h5_combined:
h5_combined.create_dataset('Acc_Floor_Response', data=Acc_Floor_Responses)
h5_combined.create_dataset('Blg_Damage_State', data=DS_Blgs)
print(f"Combined data saved to {h5_combined_path}")
if __name__ == "__main__":
main('Blg_F2_6m_IM7_SCD2.h5')