Skip to content

Commit

Permalink
Fix duplicated hk time bin bug
Browse files Browse the repository at this point in the history
  • Loading branch information
samaloney committed Jan 30, 2024
1 parent e539c8b commit 50ca833
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions stixcore/products/level0/housekeepingL0.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(self, *, service_type, service_subtype, ssid, control, data,

@classmethod
def from_levelb(cls, levelb, parent=''):
packets, idb_versions, control = HKProduct.from_levelb(levelb, parent=parent)
packets, idb_versions, control = super().from_levelb(levelb, parent=parent)

# Create array of times as dt from date_obs
times = SCETime(control['scet_coarse'], control['scet_fine'])
Expand Down Expand Up @@ -141,7 +141,7 @@ def __init__(self, *, service_type, service_subtype, ssid, control, data,

@classmethod
def from_levelb(cls, levelb, parent=''):
packets, idb_versions, control = HKProduct.from_levelb(levelb, parent=parent)
packets, idb_versions, control = super().from_levelb(levelb, parent=parent)

# Create array of times as dt from date_obs
times = SCETime(control['scet_coarse'], control['scet_fine'])
Expand Down
13 changes: 6 additions & 7 deletions stixcore/products/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,21 +580,20 @@ def __add__(self, other):
other_data['old_index'] = [f"o{i}" for i in other_data['control_index']]
self_data['old_index'] = [f"s{i}" for i in self_data['control_index']]

control = vstack((self_control, other_control))
if (self.service_type, self.service_subtype) == (3, 25):
self_data['time'] = SCETime(self_control['scet_coarse'], self_control['scet_fine'])
other_data['time'] = SCETime(other_control['scet_coarse'], other_control['scet_fine'])

logger.debug('len self: %d, len other %d', len(self_data), len(other_data))

data = (vstack((self_data, other_data))
if self.scet_timerange.start <= other.scet_timerange.start
else vstack((other_data, self_data)))

control = vstack((self_control, other_control))
data = vstack((self_data, other_data))
logger.debug('len stacked %d', len(data))

# Fits write we do np.around(time - start_time).as_float().to(u.cs)).astype("uint32"))
# So need to do something similar here to avoid comparing un-rounded value to rounded values
data['time_float'] = np.around((data['time'] - data['time'].min()).as_float().to('cs'))

# remove dublicate data based on time bin and sort the data
# remove duplicate data based on time bin and sort the data
data = unique(data, keys=['time_float'])
data.sort(['time_float'])

Expand Down
19 changes: 17 additions & 2 deletions stixcore/time/datetime.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""
Array like time objects
"""

import logging
import operator
from datetime import datetime

import numpy as np
from sunpy.time.timerange import TimeRange
Expand All @@ -12,6 +13,7 @@
from astropy.utils import ShapedLikeNDArray
from astropy.utils.data_info import MixinInfo

from stixcore import get_logger
from stixcore.ephemeris.manager import Spice

__all__ = ['SCETBase', 'SCETime', 'SCETimeDelta', 'SCETimeRange', 'SEC_IN_DAY']
Expand All @@ -22,6 +24,10 @@
SEC_IN_DAY = 24 * 60 * 60


logger = get_logger(__name__)
logger.setLevel(logging.INFO)


class TimeInfo(MixinInfo):
attr_names = MixinInfo.attr_names
_supports_indexing = True
Expand Down Expand Up @@ -431,7 +437,7 @@ def from_string(cls, scet_str, sep=':'):
coarse, fine = zip(*[list(map(int, ts.split(sep))) for ts in scet_str])
return SCETime(coarse=coarse, fine=fine)

def to_datetime(self):
def to_datetime(self, raise_error=False):
"""
Return a python datetime object.
Expand All @@ -446,6 +452,15 @@ def to_datetime(self):
except TypeError:
utc = Spice.instance.scet_to_datetime(self.to_string())

kernel_name = Spice.instance.meta_kernel_path[1].name.split('_')[4]
kernel_date = datetime.strptime(kernel_name, '%Y%m%d')
bad = [t.replace(tzinfo=None) > kernel_date
for t in (utc if isinstance(utc, list) else [utc])]
if any(bad):
if raise_error is True:
raise ValueError(f'Converting OBT to UTC after kernel issue date: {kernel_date}.')
logger.warning(f'Converting OBT to UTC after kernel issue date: {kernel_date}.')

return utc

def to_time(self):
Expand Down

0 comments on commit 50ca833

Please sign in to comment.