Skip to content

Commit

Permalink
Fix trigger scaling for QL bkg and spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
samaloney committed Dec 18, 2023
1 parent e5258f7 commit b31c64a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
12 changes: 5 additions & 7 deletions stixcore/products/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,18 @@ def unscale_triggers(scaled_triggers, *, integration, detector_masks, ssid, fact
active_trigger_groups = active_detectors_per_trigger_group >= 1

# QL are summed to total trigger (1 trigger value)
if ssid in {30, 31, 32}:
# BSD SPEC data are summed to total trigger (1 trigger value)
if ssid in {30, 31, 32, 24}:
n_group = active_trigger_groups.astype(int).sum()
n_int = integration.as_float().to_value(u.ds).reshape(-1, 1) # units of 0.1s
# BSD pixel/vis data not summed (16 trigger values)
elif ssid in {21, 22, 23}:
n_group = active_trigger_groups.astype(int)
# BSD SPEC data are summed to total trigger (1 trigger value)
elif ssid == 24:
n_group = active_trigger_groups.astype(int).sum()
n_int = integration.as_float().to_value(u.ds) # units of 0.1s
else:
raise ValueError(f'Unscaling not support for SSID {ssid}')

n_int = integration.as_float().to_value(u.ds) # units of 0.1s

# Scaled to ints onboard, so bins have scaled width of 1, so error is 0.5 times the total factor
# Scaled to ints onboard, bins have scaled width of 1, so error is 0.5 times the total factor
scaling_error = np.full_like(scaled_triggers, 0.5, dtype=float) * n_group.T * n_int * factor
# The FSW essential floors the value so add 0.5 so trigger is the centre of range +/- error
unscaled_triggers = (scaled_triggers * n_group.T * n_int * factor) + scaling_error
Expand Down
22 changes: 15 additions & 7 deletions stixcore/products/level0/quicklookL0.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,14 @@ def from_levelb(cls, levelb, parent=''):

triggers = packets.get_value('NIX00274').T
triggers_var = packets.get_value('NIX00274', attr="error").T

# As fixed not sent in TM so hard code here BKC detector is index 9
dmask = np.zeros(shape=(1, 32), dtype=int)
dmask[0, 9] = 1
if control['compression_scheme_triggers_skm'].tolist() == [[0, 0, 7]]:
logger.debug('Unscaling trigger ')
triggers, triggers_var = unscale_triggers(
triggers, integration=duration, detector_masks=control['detector_mask'],
triggers, integration=duration, detector_masks=dmask,
ssid=levelb.ssid)

data = Data()
Expand Down Expand Up @@ -330,17 +334,21 @@ def from_levelb(cls, levelb, parent=''):
counts_var = np.vstack(counts_var).T
counts = np.pad(counts, ((pad_before, pad_after), (0, 0)), constant_values=0)
counts_var = np.pad(counts_var, ((pad_before, pad_after), (0, 0)), constant_values=0)
triggers = packets.get_value('NIX00484').T.reshape(-1)
triggers_var = packets.get_value('NIX00484', attr='error').T.reshape(-1)
triggers = packets.get_value('NIX00484').T
triggers_var = packets.get_value('NIX00484', attr='error').T

# These are per detector spectra so n_acc is 1 by design and not in TM so hard code here
dmask = np.zeros(shape=(1, 32), dtype=int)
dmask[0, 9] = 1

if control['compression_scheme_triggers_skm'].tolist == [[0, 0, 7]]:
if control['compression_scheme_triggers_skm'].tolist() == [[0, 0, 7]]:
logger.debug('Unscaling trigger ')
triggers, triggers_var = unscale_triggers(
triggers, integration=duration, detector_masks=control['detector_mask'],
triggers, integration=duration, detector_masks=dmask,
ssid=levelb.ssid)

triggers = np.pad(triggers, (pad_before, pad_after), mode='edge')
triggers_var = np.pad(triggers_var, (pad_before, pad_after), mode='edge')
triggers = np.pad(triggers, ((pad_before, pad_after), (0, 0)), mode='edge')
triggers_var = np.pad(triggers_var, ((pad_before, pad_after), (0, 0)), mode='edge')

detector_index = np.pad(np.array(did, np.int16), (pad_before, pad_after), mode='edge')
num_integrations = np.pad(np.array(packets.get_value('NIX00485'), np.uint16),
Expand Down
4 changes: 2 additions & 2 deletions stixcore/products/tests/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def test_unscale(factor, n_int, ssid):

norm = n_groups * n_int * factor

triggers_in = np.tile(np.arange(255*n_int*factor).reshape(-1, 1), 16, )
triggers_in = np.repeat(np.arange(255*n_int*factor).reshape(-1, 1), 16, axis=1)
if ssid == 24:
triggers_in = triggers_in.sum(axis=1)
triggers_in = triggers_in.sum(axis=1, keepdims=True)

triggers_scaled = np.floor(triggers_in / norm)
trigger_unscaled_var = 0.5 * norm
Expand Down

0 comments on commit b31c64a

Please sign in to comment.