python_批量计算指定目录下的数据的固定率和时长

2023-12-18 17:24:26

小工具:在指定的目录下,批量计算gga文件或者pos文件的固定率和数据时长。

import sys
import os
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QRadioButton
from PyQt5.QtCore import Qt


class InputDialog(QDialog):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle('批量计算固定率')
        self.setGeometry(600, 600, 600, 200)
        self.setWindowFlags(Qt.Dialog | Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint)

        layout = QVBoxLayout()

        self.radio_button1 = QRadioButton('gga', self)
        self.radio_button2 = QRadioButton('pos', self)
        self.radio_button1.setChecked(True)
        layout.addWidget(self.radio_button1)
        layout.addWidget(self.radio_button2)

        label1 = QLabel('日志文件路径:')
        layout.addWidget(label1)

        self.line_edit1 = QLineEdit()
        layout.addWidget(self.line_edit1)

        label2 = QLabel('计算结果存放路径:')
        layout.addWidget(label2)

        self.line_edit2 = QLineEdit()
        layout.addWidget(self.line_edit2)

        button = QPushButton('计算')
        button.clicked.connect(self.submit)
        layout.addWidget(button)

        self.setLayout(layout)

    def submit(self):
        input1 = self.line_edit1.text()
        input2 = self.line_edit2.text()
        if self.radio_button1.isChecked():
            option = 'gga'
        else:
            option = 'pos'

        self.function(input1, input2, option)

    def function(self, source_dir, target_dir, option):
        for filename in os.listdir(source_dir):
            # 构造文件的完整路径
            file_path = os.path.join(source_dir, filename)

            # 如果是文件,则计算
            if os.path.isfile(file_path):  # and "gga" in file_path:
                num_sum, num_all, per = 0, 0, 0
                times = 0

                if option == 'gga' and "gga" in file_path:
                    with open(file_path, 'r') as fr:
                        for lines in fr:
                            temp = lines.split(",")

                            if temp[0] == "$GPGGA":
                                # GPGGA总行数
                                num_all += 1
                                if temp[6] == '4':
                                    # 固定解=4行数
                                    num_sum += 1


                elif option == 'pos' and "pos" in file_path:
                    with open(file_path, 'r') as fr:

                        for lines in fr:
                            temp = lines.split(" ")

                            if "POS:" in temp[0]:
                                # POS总行数
                                num_all += 1
                                if "POS:1" in temp[0]:
                                    # POS:1固定解行数
                                    num_sum += 1

                if num_all >= 1:
                    # 固定率
                    per = round(num_sum / num_all * 100, 2)

                    # 数据时长/小时
                    times = round(num_all / 3600, 2)

                    sitename = os.path.basename(file_path)[:4]

                    result = sitename + ",数据时长/小时," + str(times) + ",固定率," + str(per) + "%"
                    print(result)

                    # 构造目标路径
                    t = datetime.now().strftime("%m%d%H%M")
                    target_path = os.path.join(target_dir, '固定率结果' + t + '.csv')

                    with open(target_path, 'a+') as resultfile:
                        resultfile.writelines(result + '\n')

            # 如果是目录,则递归处理子目录下的文件
            elif os.path.isdir(file_path):
                self.function(file_path, target_dir, option)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = InputDialog()
    dialog.show()
    sys.exit(app.exec_())

文章来源:https://blog.csdn.net/ramsey17/article/details/135065060
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。