| """
|
| 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 的文件,并存为一个大数组。
|
|
|
| """
|
|
|
| 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)
|
|
|
| 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)
|
|
|
|
|
| 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
|
|
|
| def extract_damage_state(folder_path:str,
|
| start_gms_number:int = 0,
|
| number_of_gm:int=250) -> np.ndarray:
|
| """
|
| 读取所有 results 文件中损伤等级的数据,并存为一个大数组。
|
|
|
| """
|
|
|
| damage_states = np.zeros((57, number_of_gm, 1))
|
| file_path = os.path.join(folder_path, 'DamageState.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
|
|
|
| def save_2_hdf5(file_name:str,
|
| dataset_name:str,
|
| array:np.ndarray) -> None:
|
| """存放数据到 HDF5 文件中。
|
|
|
| Args:
|
| file_name: 文件名
|
| dataset_name: 数据集名称
|
| array: 数据集
|
|
|
| """
|
|
|
| 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__))
|
|
|
| os.chdir(current_folder)
|
| print(f"current_folder = {current_folder}")
|
|
|
| folder_name = os.path.basename(current_folder)
|
| print(f"folder_name = {folder_name}")
|
|
|
| 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_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() |