计算文件中相邻两行的时间差,统计数据丢失的情况,并写入文件中
# 分析outpos的gga数据丢失情况
import os
import matplotlib.pyplot as plt
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog
plt.rcParams["font.sans-serif"] = ["SimHei"] # 设置显示中文字体
plt.rcParams["axes.unicode_minus"] = False # 设置正常显示符号
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"] = "1"
class MyDialog(QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.setWindowTitle('outpos数据丢失')
self.setGeometry(100, 100, 200, 200)
# 创建垂直布局管理器
layout = QVBoxLayout()
# 创建文件选择框
label3 = QLabel('请点击浏览按钮,选择outpos的pos文件;\n再点击执行按钮,输出定位结果的数据丢失情况。', self)
layout.addWidget(label3)
self.file_input1 = QPushButton('浏览...', self)
layout.addWidget(self.file_input1)
self.file_input1.clicked.connect(self.open_file_dialog1)
# 添加确认执行按钮
self.ok_button = QPushButton('执行', self)
self.ok_button.clicked.connect(self.execute)
layout.addWidget(self.ok_button)
# 将布局设置为主窗口的布局
self.setLayout(layout)
def open_file_dialog1(self):
file_dialog = QFileDialog(self)
file_dialog.setFileMode(QFileDialog.ExistingFiles)
if file_dialog.exec_():
file_paths = file_dialog.selectedFiles()
merged_filename = ""
if len(file_paths) > 1:
# 获取第一个文件的目录和文件名,用于创建合并后的文件名
directory = os.path.dirname(file_paths[0])
filename = os.path.splitext(os.path.basename(file_paths[0]))[0]
merged_filename = os.path.join(directory, f'{filename}_merged').replace('\\', '/')
with open(merged_filename, 'w') as merged_file:
for path in file_paths:
with open(path, 'r') as file:
merged_file.write(file.read())
merged_file.write('\n') # 在每个文件之间添加换行符
elif len(file_paths) == 1:
merged_filename = file_paths
else:
print('Please select files.')
if merged_filename:
self.file_input1.setText(''.join(merged_filename))
def execute(self):
# 执行操作,可以在这里处理输入框和文件选择框的内容
file_path_s = self.file_input1.text()
# 结果存放文件
file_dir = os.path.dirname(file_path_s)
file_name = os.path.basename(file_path_s).split('.')[0]
t = datetime.now().strftime("%m%d%H%M")
file_path_d = os.path.join(file_dir, file_name + '_' + t + '.txt')
with open(file_path_s, 'r', encoding="utf-8", errors="ignore") as file, open(file_path_d, 'w') as result_file:
# 跳过GPST文件头,从第三行开始
lines = file.readlines()[2:]
for index, line in enumerate(lines):
# 跳过最后一行的比较,避免出现IndexError: list index out of range
# enumerate索引的起始位置默认为0,所以需要减1
if index < len(lines) - 1:
# 分别切割相邻的两行数据
co_st = line.strip().split(' ')
co_en = lines[index + 1].strip().split(' ')
# 确保有至少两列,组成年月日时分秒格式,并拼接成新的字符串
if len(co_st) >= 2 and len(co_en) >= 2:
st = co_st[0] + ' ' + co_st[1]
et = co_en[0] + ' ' + co_en[1]
# 时间格式化
start_time = datetime.strptime(st, '%Y/%m/%d %H:%M:%S.%f')
end_time = datetime.strptime(et, '%Y/%m/%d %H:%M:%S.%f')
dif = end_time - start_time
# hour, min, sec = str(dif).split(':')
sec = dif.total_seconds()
# 差值不等于1,说明历元丢失
if sec != 1:
result = st + ' > ' + et + ' 之间丢失 ' + str(int(sec - 1)) + ' 个数据'
# print(f"{st} > {et} 之间丢失 {int(sec - 1)} 个历元")
print(result)
result_file.writelines(result + "\n")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
dialog = MyDialog()
# 解决子窗口不能操作的问题
dialog.show()
dialog.exec_()
加入开始、结束时间等;
# 分析outpos的gga数据丢失情况
import os
import matplotlib.pyplot as plt
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QPushButton, QFileDialog
plt.rcParams["font.sans-serif"] = ["SimHei"] # 设置显示中文字体
plt.rcParams["axes.unicode_minus"] = False # 设置正常显示符号
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"] = "1"
class MyDialog(QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.setWindowTitle('outpos数据丢失')
self.setGeometry(100, 100, 200, 200)
# 创建垂直布局管理器
layout = QVBoxLayout()
# 创建文件选择框
label3 = QLabel('请点击浏览按钮,选择outpos的pos文件;\n再点击执行按钮,输出定位结果的数据丢失情况。', self)
layout.addWidget(label3)
self.file_input1 = QPushButton('浏览...', self)
layout.addWidget(self.file_input1)
self.file_input1.clicked.connect(self.open_file_dialog1)
# 添加确认执行按钮
self.ok_button = QPushButton('执行', self)
self.ok_button.clicked.connect(self.execute)
layout.addWidget(self.ok_button)
# 将布局设置为主窗口的布局
self.setLayout(layout)
def open_file_dialog1(self):
file_dialog = QFileDialog(self)
file_dialog.setFileMode(QFileDialog.ExistingFiles)
if file_dialog.exec_():
file_paths = file_dialog.selectedFiles()
merged_filename = ""
if len(file_paths) > 1:
# 获取第一个文件的目录和文件名,用于创建合并后的文件名
directory = os.path.dirname(file_paths[0])
filename = os.path.splitext(os.path.basename(file_paths[0]))[0]
merged_filename = os.path.join(directory, f'{filename}_merged').replace('\\', '/')
with open(merged_filename, 'w') as merged_file:
for path in file_paths:
with open(path, 'r') as file:
merged_file.write(file.read())
merged_file.write('\n') # 在每个文件之间添加换行符
elif len(file_paths) == 1:
merged_filename = file_paths
else:
print('Please select files.')
if merged_filename:
self.file_input1.setText(''.join(merged_filename))
def execute(self):
# 执行操作,可以在这里处理输入框和文件选择框的内容
file_path_s = self.file_input1.text()
# 结果存放文件
file_dir = os.path.dirname(file_path_s)
file_name = os.path.basename(file_path_s).split('.')[0]
t = datetime.now().strftime("%m%d%H%M")
file_path_d = os.path.join(file_dir, file_name + '_' + t + '.txt')
with open(file_path_s, 'r', encoding="utf-8", errors="ignore") as file, open(file_path_d, 'w') as result_file:
# 跳过GPST文件头,从第三行开始
lines = file.readlines()[2:]
ll = len(lines)
for index, line in enumerate(lines):
# 跳过最后一行的比较,避免出现IndexError: list index out of range
# enumerate索引的起始位置默认为0,所以需要减1
if index < ll - 1:
# 分别切割相邻的两行数据
co_st = line.strip().split(' ')
co_en = lines[index + 1].strip().split(' ')
# 确保有至少两列,组成年月日时分秒格式,并拼接成新的字符串
if len(co_st) >= 2 and len(co_en) >= 2:
st = co_st[0] + ' ' + co_st[1]
et = co_en[0] + ' ' + co_en[1]
# 时间格式化
start_time = datetime.strptime(st, '%Y/%m/%d %H:%M:%S.%f')
end_time = datetime.strptime(et, '%Y/%m/%d %H:%M:%S.%f')
# 起始时间
if index == 0:
result_s = '起始时间:' + st
first = start_time
# 计算时间差
dif = end_time - start_time
# hour, min, sec = str(dif).split(':')
sec = dif.total_seconds()
# 差值不等于1,说明数据丢失
if sec != 1:
result = st + ' > ' + et + ' 之间丢失 ' + str(int(sec - 1)) + ' 个数据'
# print(f"{st} > {et} 之间丢失 {int(sec - 1)} 个历元")
print(result)
result_file.writelines(result + "\n")
# 结束时间
if index == ll - 2:
# 计算整体的时间差
all_dif = end_time - first
all_sec = int(all_dif.total_seconds()) + 1
all_rate = round((all_sec - ll) / all_sec * 100, 2)
result_e = '结束时间:' + et + '\n' + '理论数量:' + str(all_sec) + '\n' + '丢失数量:' + str(
all_sec - ll) + '\n' + '丢失率:' + str(all_rate) + '%'
print(result_s + '\n' + result_e)
result_file.writelines(result_s + "\n")
result_file.writelines(result_e + "\n")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
dialog = MyDialog()
# 解决子窗口不能操作的问题
dialog.show()
dialog.exec_()