GPS时间转utc时间/unix时间

一些雕虫小技

# coding:utf-8
# !/usr/bin/python
""" Convierte la hora UTC en Unix """
import datetime
import calendar
from datetime import datetime, timedelta
LEAP_SECONDS = 18

class Hora():
    """ Convierte la hora UTC en Unix """
    @classmethod
    def __init__(cls):
        """ Constructor """

    @classmethod
    def convert_utc_to_unix(cls, date_time):
        """ Convierte fecha y hora utc a unix """
        return calendar.timegm(date_time.utctimetuple())

    @classmethod
    def convert_str_to_utc(cls, date_time):
        """ Convierte string a utc """
        return datetime.strptime(date_time, '%Y-%m-%d-%H-%M-%S')

    @classmethod
    def main(cls, date_time):
        """ Orquestador """
        cls.obj = Hora()
        unix_code = cls.obj.convert_utc_to_unix(cls.obj.convert_str_to_utc(date_time))
        return(unix_code)

# if __name__ == '__main__':
    #Manual
    
    #Automatico
    #Hora().main(datetime.datetime.utcnow())
import numpy as np
def gps_week_seconds_to_utc(gpsweek, gpsseconds, leapseconds=LEAP_SECONDS):
    datetimeformat = "%Y-%m-%d %H:%M:%S.%f"
    epoch = datetime.strptime("1980-01-06 00:00:00.000", datetimeformat)
    # timedelta函数会处理seconds为负数的情况
    elapsed = timedelta(days=(gpsweek*7), seconds=(gpsseconds-leapseconds))
    return datetime.strftime(epoch+elapsed, datetimeformat)



def degree2rad(degree):
    return degree * np.pi / 180


def BLH2xyz(L, B, H, rad=True):
    if not rad:
        L = degree2rad(L)
        B = degree2rad(B)
    a = 6378137.0000
    b = 6356752.3141
    e2 = 1 - (b / a)**2
    N = a/np.sqrt(1-e2*np.sin(B)**2)
    x = (N + H) * np.cos(B) * np.cos(L)
    y = (N + H) * np.cos(B) * np.sin(L)
    z = (N * (1 - e2) + H) * np.sin(B)
    return x, y, z

import os, sys,re

name="20211223"
# basedir="/media/car/T7/Apaper_two/dataset/myrecord/mti/"+name+'.txt'
# txtdir="/media/car/T7/Apaper_two/dataset/myrecord/"+name+'.txt'
basedir="/media/car/T7/htpaper/datasets/IVECO/"+name+'.txt'
txtdir="//media/car/T7/htpaper/datasets/IVECO/tum_"+name+'.txt'
f1=open(basedir,'r')
f2=open(txtdir,"w")
#f2.write('#timestamp [ns],w_RS_S_x [rad s^-1],w_RS_S_y [rad s^-1],w_RS_S_z [rad s^-1],a_RS_S_x [m s^-2],a_RS_S_y [m s^-2],a_RS_S_z [m s^-2]\n')
line=f1.readline()

cnt=1

while line:
    #print(len(re.findall(r"\-?\d+\.?\d*" ,line)))
    if len(re.findall(r"\-?\d+\.?\d*" ,line))==18:
        #print('yes')
        gpssecond=float(re.findall(r"\-?\d+\.?\d*" ,line)[1])
        utctime=gps_week_seconds_to_utc(2189,gpssecond)
        year=int(utctime[0:4])
        month=int(utctime[5:7])
        day=int(utctime[8:10])
        hour=int(utctime[11:13])
        minute=int(utctime[14:16])
        second=int(utctime[17:19])
        nsecond=int(utctime[20:26])


        if hour>=24:
            hour=hour-24
            day=day+1

        q0=0
        q1=0
        q2=0
        q3=0
        x=re.findall(r"\-?\d+\.?\d*" ,line)[2]
        y=re.findall(r"\-?\d+\.?\d*" ,line)[3]
        z=re.findall(r"\-?\d+\.?\d*" ,line)[4]
        timestring=str(year)+'-'+str(month)+'-'+str(day)+'-'+str(hour)+'-'+str(minute)+'-'+str(second)
        #print(timestring)

        #timestamps=str(Hora().main("2019-07-25-12-13-53"))+'.'+str(nanosecond)
        #timestamps=str(round(Hora().main(timestring)+nanosecond*0.000000001,9))
        timestamps=str("%0.9f"%(Hora().main(timestring)+nsecond*0.000001))
        f2.write(timestamps+' ')
        f2.write(x+' '+y+' '+z+' ')
        f2.write(str(q1)+' '+str(q2)+' '+str(q3)+' '+str(q0))

        f2.write('\n')
        #print(timestamps+'\n'+str(cnt))
        cnt+=1
    line=f1.readline()
    # if len(re.findall(r"\-?\d+\.?\d*" ,line)==16:
    #     print()

你可能感兴趣的:(unix,gps,python)