OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
timestamp.py
Go to the documentation of this file.
1 import numpy as np
2 import datetime
3 
4 TAI58_OFFSET = datetime.datetime(1970, 1, 1) - datetime.datetime(1958, 1, 1)
5 LEAPSEC = 37 # TODO: look up by year
6 
7 def decode_timestamp(seconds,subseconds):
8  return float(seconds) + float(subseconds) / pow(2,8*subseconds.itemsize)
9 
10 CCSDS_timestamp = np.dtype([
11  ('seconds', '>u4'), # Time since start of epoch in seconds (TAI58)
12  ('subsecs', '>u2'), # fractional seconds
13 ]) # total length = 6 bytes
14 
15 def parse_CCSDS_timestamp(timestr):
16  return decode_timestamp(timestr['seconds'], timestr['subsecs'])
17 
18 def readTimestamp(data):
19  tmp = np.frombuffer(data, dtype=CCSDS_timestamp, count=1)
20  ts = parse_CCSDS_timestamp(tmp[0])
21  return ts
22 
23 def tai58_as_datetime(tai58):
24  # convert tai58 (seconds since 1958, including leapsecs) to datetime object
25  dt = datetime.datetime.utcfromtimestamp(tai58 - LEAPSEC) - TAI58_OFFSET
26  return dt # seconds since Jan 1, 1970
27 
28 def seconds_since(tai58, basetime=None):
29  # translate TAI58 to seconds since a baseline time (defaults to start of day)
30  dt = tai58_as_datetime(tai58)
31  if not basetime:
32  basetime = dt.replace(hour=0, minute=0, second=0, microsecond=0)
33  return (dt - basetime).total_seconds()
34 
35 def datetime_repr(dt):
36  return dt.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
def seconds_since(tai58, basetime=None)
Definition: timestamp.py:28
def decode_timestamp(seconds, subseconds)
Definition: timestamp.py:7
def parse_CCSDS_timestamp(timestr)
Definition: timestamp.py:15
def readTimestamp(data)
Definition: timestamp.py:18
def tai58_as_datetime(tai58)
Definition: timestamp.py:23
def datetime_repr(dt)
Definition: timestamp.py:35