82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
from datetime import datetime, timedelta
|
||
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_float = struct.unpack("<d", date_bytes)[0]
|
||
date_str = str(int(date_float))
|
||
year = int(date_str[:4])
|
||
month = int(date_str[4:6])
|
||
day = int(date_str[6:8])
|
||
# start_date = datetime(year, month, day)
|
||
# print(f"Date: {datetime(year, month, day)}")
|
||
|
||
# 第一个时间戳所有数据
|
||
# time_bytes = file.read(8)
|
||
# time = struct.unpack("<d", time_bytes)[0]
|
||
# print(f"Time: {time}")
|
||
# hours = int(time / 60)
|
||
# first_date = start_date + timedelta(hours=hours)
|
||
|
||
# print(f"Minute: {minute}")
|
||
# data_size = column_count_int * 8 # 每个数据点8个字节
|
||
# file.seek(data_size, 1)
|
||
# time1_bytes = file.read(8)
|
||
# time1 = struct.unpack("<d", time1_bytes)[0]
|
||
# print(f"Time: {time1}")
|
||
# file.seek(data_size, 1)
|
||
# time2_bytes = file.read(8)
|
||
# time2 = struct.unpack("<d", time2_bytes)[0]
|
||
# print(f"Time: {time2}")
|
||
# for i in range(column_count_int):
|
||
# print(i)
|
||
# if i > 10:
|
||
# break
|
||
# data_bytes = file.read(8)
|
||
# data = struct.unpack("<d", data_bytes)[0]
|
||
# print(f"Data: {data}")
|
||
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
|
||
print("index", index)
|
||
return df
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 使用函数读取文件
|
||
file_path = "lz.out/lz.rivqdown.dat"
|
||
df = read_binary_file(file_path)
|
||
print(df.shape)
|
||
print(df.head())
|