river-sim/read_dat.py
2024-11-07 17:53:11 +08:00

51 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import struct
import pandas as pd
def read_binary_file(file_path):
df = pd.DataFrame()
with open(file_path, "rb") as file:
index = 0
# 读取前8个字节表示数据列数
column_bytes = file.read(8)
if not column_bytes:
return # 文件结束
column_count = struct.unpack("<d", column_bytes)[0]
column_count_int = int(column_count)
print(f"Column Count: {column_count_int}")
# 读取接下来的8个字节表示日期
date_bytes = file.read(8)
date = struct.unpack("<d", date_bytes)[0]
print(f"Date: {date}")
while True:
# print("index", index)
if index > 335:
break
time_bytes = file.read(8)
time = struct.unpack("<d", time_bytes)[0]
# print(f"time: {time}")
# 读取数据矩阵
data_size = column_count_int * 8 # 每个数据点8个字节
data_bytes = file.read(data_size)
data_matrix = struct.unpack(f"<{column_count_int}d", data_bytes)
data_matrix = [data_matrix]
new_df = pd.DataFrame(data_matrix)
df = pd.concat([df, new_df], ignore_index=True)
# print(f"Data Matrix: {data_matrix}")
index = index + 1
return df
# 使用函数读取文件
file_path = "lz.out/lz.rivqdown.dat"
df = read_binary_file(file_path)
print(df.shape)
print(df.head())