python-gps时间转换成utc时间的经纬度文件

gps时间的经纬度(单位为度)csv文件转换成utc时间的经纬度(单位为度分)GGA文件。

gps文件如下:

0.039,0.006,0.000,0.039,28,28,42,1,2*76
$GPCHC,2286,374130.36,255.68,1.37,-0.47,0.01,0.00,-0.01,0.0087,0.0261,1.0027,39.74307198,116.45563454,23.14,-0.038,0.006,0.001,0.039,28,28,42,1,2*75
$GPCHC,2286,374130.38,255.68,1.37,-0.47,-0.01,0.05,0.03,0.0086,0.0258,1.0025,39.74307198,116.45563453,23.14,-0.039,0.007,0.001,0.040,28,28,42,1,2*7C
$GPCHC,2286,374130.40,255.68,1.37,-0.47,-0.01,0.03,0.01,0.0089,0.0263,1.0023,39.74307198,116.45563452,23.14,-0.039,0.006,0.001,0.040,28,28,42,1,2*76
$GPCHC,2286,374130.42,255.68,1.37,-0.47,-0.01,0.03,0.03,1.0020,39,42,1,2*7F
$GPCHC,2286,374130.44,255.68,1.37,-0.47,0.01,0.05,0.02,0.0085,0.0255,1.0020,39.74307198,116.45563459,23.14,-0.036,0.007,0.001,0.037,28,28,42,0,2*54
$GPCHC,2286,374130.46,255.68,

import csv
import os

from datetime import datetime, timedelta

# 闰秒
LEAP_SECONDS = 18


# 输入:GPS周、GPS周内秒、闰秒(可选,gps时间不同,闰秒值也不同,由Leap_Second.dat文件决定)
# 输出:UTC时间(格林尼治时间)
# 输入示例: gps_week_seconds_to_utc(2119, 214365.000)
# 输出示例: '2020-08-18 11:32:27.000000'
def gps_week_seconds_to_utc(gpsweek, gpsseconds, leapseconds=LEAP_SECONDS):
    datetimeformat = "%Y-%m-%d %H:%M:%S.%f"
    datetimeformatD = "%H%M%S.%f"
    datetimeformatY = "%d,%m,%Y"
    epoch = datetime.strptime("1980-01-06 00:00:00.000", datetimeformat)
    # timedelta函数会处理seconds为负数的情况
    elapsed = timedelta(days=(gpsweek * 7), seconds=(gpsseconds - leapseconds))
    y = datetime.strftime(epoch + elapsed, datetimeformatY)
    # 6位的毫秒切掉4位,只保留2位
    d = datetime.strftime(epoch + elapsed, datetimeformatD)[:-4]
    yd = d + ',' + y
    return d, yd


def exchange_csvfile(ori_file, dst_file):
    num = 0
    with open(ori_file, 'r') as tmp, open(dst_file, 'w') as dst:
        reader = csv.reader(tmp)
        for row in reader:
            # 剔除缺失数据的行,规避异常IndexError: list index out of range
            if len(row) > 23:
                if '$GPCHC' in row[0] and row[12] and row[13]:
                    # gpst转成utc时间
                    gpsweek = int(row[1])
                    gpsseconds = float(row[2])
                    d, y = gps_week_seconds_to_utc(gpsweek, gpsseconds)

                    # 经纬的度转成度分
                    lon = int(row[12][:2]) * 100 + float(row[12][2:]) * 60
                    lat = int(row[13][:3]) * 100 + float(row[13][3:]) * 60

                    dst.writelines(['$GPZDA' + ',' + str(y) + ',' + '' + ',' + '*68' + '\n'])
                    dst.writelines(['$GPGGA' + ',' + str(d) + ',' + str(lon) + ',' + 'N' + ',' + str(
                        lat) + ',' + 'E' + ',' + '4' + ',' + '30' + ',' + '0.7' + ',' + '15.878' + ',' + 'M' + ',' + '0.00' + ',' + 'M' + ',' + '01' + ',' + '0770*5' + ',' + 'A' + '\n'])
                    num += 1

    print("完成%s条数据写入。" % num)


if __name__ == '__main__':
    ori_file = 'E:/python/pythonProject1/threading_test/2023_11_3-1/tmp20231103-01.csv'
    # 获取文件名(包括后缀),如tmp20231103-01.csv
    file_name = os.path.basename(ori_file)
    # 对文件名切割,添加_new
    dst_file_name = file_name.split(".")[0] + "_new." + file_name.split(".")[1]
    # 将文件完整目录切割成文件夹和文件名的形式
    file_path = os.path.split(ori_file)
    # 拼接新文件的路径
    dst_file = os.path.join(file_path[0], dst_file_name)

    exchange_csvfile(ori_file, dst_file)

你可能感兴趣的:(python)