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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: python