B
    d                 @   s  d Z ddlmZ dZddlmZ ejZejZ	ddl
Z
ddlZddlZede dZdZd	Zd
d Zdd Zdd Zdd Zdd Zd*ddZd+ddZd,ddZd-ddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Z e!d)kre  e  e  e  e   dS ).a  
A Python implementation of GPS related time conversions.

Copyright 2002 by Bud P. Bruegger, Sistema, Italy
mailto:bud@sistema.it
http://www.sistema.it

Modifications for GPS seconds by Duncan Brown

PyUTCFromGpsSeconds added by Ben Johnson

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.

GPS Time Utility functions

This file contains a Python implementation of GPS related time conversions.

The two main functions convert between UTC and GPS time (GPS-week, time of
week in seconds, GPS-day, time of day in seconds).  The other functions are
convenience wrappers around these base functions.

A good reference for GPS time issues is:
http://www.oc.nps.navy.mil/~jclynch/timsys.html

Note that python time types are represented in seconds since (a platform
dependent Python) Epoch.  This makes implementation quite straight forward
as compared to some algorigthms found in the literature and on the web.
    )print_functionz*Duncan Brown <duncan@gravity.phys.uwm.edu>)git_versionNzglue.gpstime is no longer maintained and will be removed in a future release of lscsoft-glue, please migrate your workflow to use a different GPS conversion modulei:	 iQ )i        r   r   r   c             C   s>   d}t | |||dddddf	}t |d }|d d }|S )z,returns day of week: 0=Sun, 1=Mon, .., 6=Sat   r   g        r   r      )timemktime	localtime)yearmonthdayhrtZpyDowZgpsDow r   Y/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/glue/gpstime.py	dayOfWeek=   s
    r   c             C   s   d}t | |||ddd S )z.returns (full) gpsWeek for given date (in UTC)r   r   g        )
gpsFromUTC)r   r   r   r   r   r   r   gpsWeekE   s    r   c             C   s2   d}t | |||dddddf	}t |d }|S )z*returns julian day=day since Jan 1 of yearr   r   g        r   r   )r	   r
   r   )r   r   r   r   r   ZjulDayr   r   r   	julianDayK   s    r   c             C   s.   | |||||gdddg }t |t j }|S )z&similar to python's mktime but for utcr   )r	   r
   timezone)r   r   r   hourminsecspecutcr   r   r   mkUTCR   s    r   c             C   s   t | }|dd S )z-returns tuple from a python time value in UTCN)r	   gmtime)pyUTCZ	ymdhmsXXXr   r   r   ymdhmsFromPyUTCX   s    
r!      c             C   s"   t | }t||f  }|dd S )zdconvenience function:
         allows to use python UTC times and
         returns only week and towr      )r!   r   )r    leapSecsymdhmsZwSowDSoDr   r   r   wtFromUTCpy]   s    r&   c             C   s   |d }t d }t|}	t| |||||dddf	}
|
| }
|
|	 }|t | }tt|t }tt|t }|t }||||fS )a`  converts UTC to: gpsWeek, secsOfWeek, gpsDay, secsOfDay

    a good reference is:  http://www.oc.nps.navy.mil/~jclynch/timsys.html

    This is based on the following facts (see reference above):

    GPS time is basically measured in (atomic) seconds since
    January 6, 1980, 00:00:00.0  (the GPS Epoch)

    The GPS week starts on Saturday midnight (Sunday morning), and runs
    for 604800 seconds.

    Currently, GPS time is 13 seconds ahead of UTC (see above reference).
    While GPS SVs transmit this difference and the date when another leap
    second takes effect, the use of leap seconds cannot be predicted.  This
    routine is precise until the next leap second is introduced and has to be
    updated after that.

    SOW = Seconds of Week
    SOD = Seconds of Day

    Note:  Python represents time in integer seconds, fractions are lost!!!
    r   )r   r   r   r   r   )gpsEpochr	   r
   
secsInWeekintmathfloor	secsInDay)r   r   r   r   r   r   r$   secFract
epochTuplet0r   tdiffZgpsSOWr   ZgpsDayZgpsSODr   r   r   r   e   s    
r   c          	   C   sh   |d }t d }t|tj }| t | | }|| }t|\	}}	}
}}}}}}||	|
|||| fS )zconverts gps week and seconds to UTC

    see comments of inverse function!

    SOW = seconds of week
    gpsWeek is the full number (not modulo 1024)
    r   )r   r   r   )r'   r	   r
   r   r(   r   )r   ZSOWr$   r-   r.   r/   r0   r   r   r   r   hhmmssr   r   Zdaylightsavingr   r   r   
UTCFromGps   s    r4   c             C   s4   t t|   }}t|d d d d d |d  S )z^converts the python epoch to gps seconds

    pyEpoch = the python epoch from time.time()
    r   <      r   r   )r   r!   r)   )r    r$   r   r   r   r   GpsSecondsFromPyUTC   s    r7   c             C   s   t  dS )zconverts gps seconds to the
    python epoch. That is, the time
    that would be returned from time.time()
    at gpsseconds.
    N)r    )Z
gpssecondsr   r   r   PyUTCFromGpsSeconds   s    r8   c           	   C   s  t d t   t d tdddddddd\} }}}t d| |||f  t d	 t d
t| |dd  t d tdddddd\} }}}t d| |||f  t d	 t d
t| |dd  t d tdddddd\} }}}t d| |||f  t d	 t d
t| |  d S )Nz--------------------zEThe GPS Epoch when everything began (1980, 1, 6, 0, 0, 0, leapSecs=0)i  r   r   r   )r$   z(**** week: %s, sow: %s, day: %s, sod: %sz     and hopefully back:z**** %s, %s, %s, %s, %s, %s
z@The time of first Rollover of GPS week (1999, 8, 21, 23, 59, 47)i           ;   /   r"   zJToday is GPS week 1186, day 3, seems to run ok (2002, 10, 2, 12, 6, 13.56)i  
   r#   r   gQ+@)printr   r4   )wZsowdZsodr   r   r   testTimeStuff   s"    rB   c               C   s   t dtddd d S )Nz2002, 10, 11 -> 284  ==??== i  r>      )r?   r   r   r   r   r   testJulD   s    rD   c               C   s   t dtddd d S )Nz2002, 10, 11 -> 1187  ==??== i  r>   rC   )r?   r   r   r   r   r   testGpsWeek   s    rE   c               C   s(   t dtddd t dtddd d S )Nz2002, 10, 12 -> 6  ==??== i  r>   r   z2002, 10, 6  -> 0  ==??== r   )r?   r   r   r   r   r   testDayOfWeek   s    rF   c              C   sF   d} t d|  t|  }t|}t d| t|\}}t d||f d S )N)i  r>   r   r9   "   g(@ztesting for: zyields     : zweek and time: )r?   r   r!   r&   )r%   ZpyUtcbackr@   r   r   r   r   testPyUtilties   s    

rI   __main__)r"   )r"   )r"   )r"   )"__doc__
__future__r   
__author__Zgluer   date__date__id__version__r	   r*   warningswarnDeprecationWarningr(   r,   r'   r   r   r   r   r!   r&   r   r4   r7   r8   rB   rD   rE   rF   rI   __name__r   r   r   r   <module>'   sD   

)



