使用背景
由于在GNSS卫星定位中,GNSS接收设备通过4G传输的RTCM数据、星历数据以及GGA数据存在丢包情况,但是在数据传输频率为1HZ,数据量太过庞大的时候,难以靠人工查找丢包位置,因此开发该脚本,通过导入GGA数据、RTCM数据以及星历数据来自动查找丢包位置。
调用库
1 2 3
| import re import tkinter as tk from tkinter import filedialog
|
主函数(文件打开与保存)
1 2 3 4 5 6
| root = tk.Tk() root.withdraw() File_path = filedialog.askopenfilename() File_path_root = File_path.split('.')[0] File_path_root = File_path_root + '.txt' time_array = []
|
秒数转日期时间
1 2 3 4 5 6
| def second2time(seconds): day = seconds // (3600 * 24) hours = (seconds - day * 24 * 3600) // 3600 minutes = ((seconds - day * 24 * 3600) % 3600) // 60 seconds = (seconds - day * 24 * 3600) % 60 return f"{day:02d}日 {hours:02d}:{minutes:02d}:{seconds:02d}"
|
星历数据丢包判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| def ephemeris_judge(): total_number_loss = 0 print("请输入星历间隔:") interval = int(input()) with open(File_path, 'r', encoding='utf-8') as file: for line in file: if 'RTCM 1074' in line: match = re.search(r'(\d{4}/\d{2}/\d{2}) (\d{2}):(\d{2}):(\d{2})', line) if match and 14 <= match.group(2) <= 24: total_number_loss += 1 year_month_day = match.group(1) hour = match.group(2) minute = match.group(3) second = match.group(4) seconds = int(hour) * 3600 + int(minute) * 60 + int(second) time_array.append(seconds) for index in range(1, len(time_array)): if time_array[index] - time_array[index - 1] > interval: print(f"{second2time(time_array[index - 1])}秒与前一个时间戳相差超过{interval}秒")
|
RTCM数据丢包判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| def rtcm_judge(): total_number = 0 number_loss = 0 print("请输入采样间隔:") interval = int(input()) with open(File_path, 'r') as file: for line in file: match = re.search(r"(\d{4} \d{2})( \d{2}) (\d{2}) (\d{2}) (\d{2}\.\d{7})", line) if match: total_number += 1 day = match.group(2) hour = match.group(3) minute = match.group(4) second = match.group(5) seconds = int(day) * 24 * 3600 + int(hour) * 3600 + int(minute) * 60 + int(float(second)) time_array.append(seconds) with open(File_path_root, 'w') as f: for index in range(1, len(time_array)): if time_array[index] - time_array[index - 1] > interval: number_loss += 1 print(f"{second2time(time_array[index - 1])}秒与前一个时间戳相差超过{interval}秒") f.write(second2time(time_array[index - 1]) + '\n')
print(f"总包数:{total_number}") print(f"丢包数:{number_loss}")
|
GGA数据丢包判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| def gga_judge(): total_number = 0 number_loss = 0 print("请输入采样间隔:") interval = int(input()) with open(File_path, 'r', encoding='latin1') as file: for line in file: match = re.search(r"\$GBGGA,([0-9]+\.[0-9]+)", line) if match: total_number += 1 all_time = float(match.group(1)) hour = all_time // 10000 minute = (all_time - hour * 10000) // 100 second = all_time - hour * 10000 - minute * 100 seconds = hour * 3600 + minute * 60 + second time_array.append(seconds) with open(File_path_root, 'w') as f: for index in range(1, len(time_array)): if time_array[index] - time_array[index - 1] > interval and abs( time_array[index - 1] + interval - 86400 - time_array[index]) != 0: number_loss += 1 print(f"{iindex+1}行与前一个时间戳相差超过{interval}秒") f.write(str(index+1) + '\n')
|
主函数(调用函数判断)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| functions = { "eph": ephemeris_judge, "rtcm": rtcm_judge, "gga": gga_judge }
choice = input("请输入要调用的函数名(eph, rtcm, gga):").strip()
if choice in functions: functions[choice]() else: print("无效的函数名,请输入 eph、rtcm 或 gga")
|