Skip to content
Snippets Groups Projects
Commit f5753592 authored by Bruce Flynn's avatar Bruce Flynn
Browse files

changes to timecode helpers

parent a2660b1a
No related branches found
No related tags found
No related merge requests found
......@@ -13,13 +13,10 @@ See:
"""
__copyright__ = "Copyright (C) 2015 University of Wisconsin SSEC. All rights reserved."
import ctypes as c
from datetime import datetime, timedelta
from edosl0util.timecode import cds_stamp, cds_to_timestamp
from edosl0util.timecode import cds_to_dt
GROUP_FIRST = 0b01
GROUP_LAST = 0b10
......@@ -63,12 +60,10 @@ class Timecode(BaseStruct):
"""
Secondary header timecode baseclass.
"""
def __repr__(self):
return str(self.asdatetime())
def astimestamp(self):
raise NotImplementedError()
def asdatetime(self):
raise NotImplementedError()
......@@ -98,20 +93,16 @@ class AquaCucTimecode(Timecode):
EPOCH_SECS = (EPOCH - datetime(1970, 1, 1)).total_seconds()
SUB_SECOND_UNITS = 15.2
def astimestamp(self):
tc = self.timecode
return cds_to_timestamp(tc.days, tc.milliseconds, tc.microseconds, self.EPOCH_SECS)
def asdatetime(self):
"""
Return converted to UTC where leap seconds are as defined in `leap_seconds`.
FIXME: Verify this conversion is correct, specfically the use of
SUB_SECOND_UNIT.
"""
seconds = self.seconds + self.leap_seconds
micros = self.SUB_SECOND_UNITS * self.sub_seconds
return self.EPOCH + timedelta(seconds=seconds, microseconds=micros)
seconds = self.seconds + self.leap_seconds
return cds_to_dt(
(seconds - self.leap_seconds) // 86400,
micros // 1e3,
micros % 1e3)
class DaySegmentedTimecode(Timecode):
......@@ -125,14 +116,8 @@ class DaySegmentedTimecode(Timecode):
('microseconds', c.c_uint16)
]
EPOCH = datetime(1958, 1, 1)
EPOCH_SECS = (EPOCH - datetime(1970, 1, 1)).total_seconds()
def astimestamp(self):
return cds_to_timestamp(self.days, self.milliseconds, self.microseconds, self.EPOCH_SECS)
def asdatetime(self):
return cds_stamp(self.days, self.milliseconds, self.microseconds)
return cds_to_dt(self.days, self.milliseconds, self.microseconds)
class AquaGirdSecondaryHeader(BaseStruct):
......@@ -178,12 +163,22 @@ class JpssFirstSecondaryHeader(BaseStruct):
]
class ViirsPacketId(BaseStruct):
_pack_ = 1
_fields_ = [
('scan_number', c.c_uint32),
('packet_time', DaySegmentedTimecode),
]
_jpss_headers = {
GROUP_FIRST: JpssFirstSecondaryHeader,
GROUP_CONTINUING: None,
GROUP_LAST: None,
GROUP_STANDALONE: JpssSecondaryHeader,
}
def jpss_header_lookup(primary_header):
return _jpss_headers.get(primary_header.sequence_grouping)
......@@ -269,4 +264,6 @@ def aqua_header_lookup(primary_header):
apid = primary_header.apid
grouping = primary_header.sequence_grouping
return _aqua_headers.get((apid, grouping))
_aqua_headers = aqua_headers()
# encoding: utf-8
__copyright__ = "Copyright (C) 2015 University of Wisconsin SSEC. All rights reserved."
from datetime import datetime, timedelta
from astropy.time import Time, TimeDelta
JPSS_EPOCH = datetime(1958, 1, 1)
UNIX_EPOCH = datetime(1970, 1, 1)
IET_EPOCH = Time('1958-01-01', scale='tai')
UNIX_EPOCH = Time('1970-01-01', scale='utc')
def unixtime(dt):
"""
Datetime to Unix timestamp.
"""
if dt > UNIX_EPOCH:
return (dt - UNIX_EPOCH).total_seconds()
return (UNIX_EPOCH - dt).total_seconds()
return (dt - UNIX_EPOCH).total_seconds()
def cds_to_timestamp(days, millis, microseconds, epoch):
def cds_to_dt(days, millis, microseconds):
"""
CDS to unix timestamp seconds.
CCSDS Day Segmented timecode to UTC datetime.
"""
return epoch + (86400 * days) + (millis / 1000.) + (microseconds / 1000000.)
iet = cds_to_iet(days, millis, microseconds)
return (IET_EPOCH + TimeDelta(iet, format='sec')).utc.datetime
def cds_stamp(days, millis, microseconds, epoch=JPSS_EPOCH):
def cds_to_iet(days, ms, us):
"""
CCSDS Day Segmented timecode to UTC datetime.
CCSDS Day Segmented timecode parts to IET
"""
return epoch + timedelta(days=days,
seconds=millis / 1000.0 + (microseconds / 1000000.))
ccsds_epoch = Time('1958-01-01', scale='tai')
day = Time(ccsds_epoch.jd + days, scale='utc', format='jd') - IET_EPOCH
return int(day.sec * 1e6 + ms * 1e3 + us)
......@@ -14,6 +14,7 @@ setup(
],
install_requires=[
'h5py',
'astropy',
],
extras_require={
'testing': [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment