From 642ba4b489460719e3cd64a1346539a3639d587d Mon Sep 17 00:00:00 2001
From: Bruce Flynn <brucef@ssec.wisc.edu>
Date: Tue, 2 Jan 2018 13:25:27 -0600
Subject: [PATCH] fix issues related to new timecode code

---
 edosl0util/crgen.py    | 7 ++++---
 edosl0util/merge.py    | 5 ++++-
 edosl0util/stream.py   | 2 +-
 edosl0util/timecode.py | 8 ++++++++
 4 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/edosl0util/crgen.py b/edosl0util/crgen.py
index c6ccb47..ccb606e 100644
--- a/edosl0util/crgen.py
+++ b/edosl0util/crgen.py
@@ -11,6 +11,7 @@ import pprint
 import edosl0util.crio as crio
 from edosl0util.headers import DaySegmentedTimecode
 from edosl0util.stream import jpss_packet_stream
+from edosl0util.timecode import dt_to_cds
 
 
 def diff_crs(real_file, generated_file):
@@ -115,7 +116,7 @@ def pds_id_from_path(pds_file):
 def get_pds_creation_time(pds_file_or_id):
     """Parse 11-char creation time out of a PDS ID or file name; return a DaySegmentedTimecode"""
     pds_file_or_id = os.path.basename(pds_file_or_id)
-    return create_timecode(datetime.strptime(pds_file_or_id[22:33], '%y%j%H%M%S'))
+    return create_timecode(dt_to_cds(datetime.strptime(pds_file_or_id[22:33], '%y%j%H%M%S')))
 
 
 def build_apid_info(scan_apid_info):
@@ -189,7 +190,7 @@ def scan_packets(pds_file, prev_pds_file=None):
             if pkt.cds_timecode:
                 if not first_pkt_time:
                     first_pkt_time = pkt.cds_timecode
-                last_pkt_time = pkt.cds_timemcode
+                last_pkt_time = pkt.cds_timecode
         return {'first_packet_time': create_timecode(first_pkt_time),
                 'last_packet_time': create_timecode(last_pkt_time),
                 'apid_info': [apid_map[k] for k in sorted(apid_map)]}
@@ -241,7 +242,7 @@ def create_timecode(tc):
 
     Handles input of None by returning epoch value of 1958-01-01.
     """
-    return DaySegmentedTimecode(tc.days, tc.millis, tc.micros) if tc else DaySegmentedTimecode()
+    return DaySegmentedTimecode(*tc) if tc else DaySegmentedTimecode()
 
 
 idps_epoch = datetime(1958, 1, 1)
diff --git a/edosl0util/merge.py b/edosl0util/merge.py
index 64c951d..d73b9e1 100644
--- a/edosl0util/merge.py
+++ b/edosl0util/merge.py
@@ -95,11 +95,14 @@ def read_packet_index(stream):
 
 
 def _sort_by_time_apid(index, order=None):
+    """
+    Sort pointers by time and apid in-place.
+    """
     if order:
         index = sorted(index, key=lambda p: order.index(p.apid) if p.apid in order else -1)
     else:
         index = sorted(index, key=lambda p: p.apid)
-    return sorted(index, key=lambda p: p.cds_timecode)
+    return sorted(index, key=lambda p: p.timecode)
 
 
 def _filter_duplicates_by_size(index):
diff --git a/edosl0util/stream.py b/edosl0util/stream.py
index b6edc98..6111beb 100644
--- a/edosl0util/stream.py
+++ b/edosl0util/stream.py
@@ -124,7 +124,7 @@ class Packet(object):
         return (
             self.secondary_header and
             hasattr(self.secondary_header, 'timecode') and
-            self.secondary_header.day_segmented_timecode() or None)
+            self.secondary_header.timecode.day_segmented_timecode() or None)
 
     @property
     def stamp(self):
diff --git a/edosl0util/timecode.py b/edosl0util/timecode.py
index cf1c357..37f2a75 100644
--- a/edosl0util/timecode.py
+++ b/edosl0util/timecode.py
@@ -48,4 +48,12 @@ def cds_to_dt(days, ms, us):
     return timecode_parts_to_dt(days, ms, us, CDS_EPOCH)
 
 
+def dt_to_cds(dt):
+    """
+    UTC datetime to (day, millis, micros)
+    """
+    d = (dt - CDS_EPOCH)
+    return (d.days, int(d.seconds * 1e3), d.microseconds)
+
+
 iet_to_dt = _grain.iet2utc
-- 
GitLab