From 45f714116642c591ef9288fde1d58ac8c4ad9bac Mon Sep 17 00:00:00 2001 From: Laura Sandoval Date: Tue, 27 Aug 2024 13:16:36 -0600 Subject: [PATCH 1/4] added back position --- .../ultra/unit/test_ultra_l1b_extended.py | 14 ++++++ .../ultra/l1b/ultra_l1b_extended.py | 44 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py b/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py index 84b027666..fdae38096 100644 --- a/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py +++ b/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py @@ -10,6 +10,7 @@ get_front_y_position, get_path_length, get_ph_tof_and_back_positions, + get_ssd_back_position, ) @@ -85,3 +86,16 @@ def test_get_ph_tof_and_back_positions( np.testing.assert_array_equal(ph_xb, selected_rows["Xb"].astype("float")) np.testing.assert_array_equal(ph_yb, selected_rows["Yb"].astype("float")) + + +def test_get_ssd_back_position( + de_dataset, + events_fsw_comparison_theta_0, +): + """Tests get_ssd_back_position function.""" + ssd_yb = get_ssd_back_position(de_dataset) + + df = pd.read_csv(events_fsw_comparison_theta_0) + df_filt = df[(df["StartType"] != -1) & (df["StopType"] >= 8)] + + np.testing.assert_array_equal(ssd_yb, df_filt["Yb"].astype("float")) diff --git a/imap_processing/ultra/l1b/ultra_l1b_extended.py b/imap_processing/ultra/l1b/ultra_l1b_extended.py index 47c25b653..319077b5e 100644 --- a/imap_processing/ultra/l1b/ultra_l1b_extended.py +++ b/imap_processing/ultra/l1b/ultra_l1b_extended.py @@ -1,10 +1,12 @@ """Calculates Extended Raw Events for ULTRA L1b.""" from enum import Enum +from typing import ClassVar import numpy as np import xarray from numpy import ndarray +from numpy.typing import NDArray from imap_processing.ultra.l1b.lookup_utils import ( get_back_position, @@ -24,10 +26,11 @@ class StopType(Enum): - """Stop Type: 1=Top, 2=Bottom.""" + """Stop Type: 1=Top, 2=Bottom, SSD: 8-15.""" Top = 1 Bottom = 2 + SSD: ClassVar[list[int]] = [8, 9, 10, 11, 12, 13, 14, 15] def get_front_x_position(start_type: ndarray, start_position_tdc: ndarray) -> ndarray: @@ -258,3 +261,42 @@ def get_path_length(front_position: tuple, back_position: tuple, d: float) -> fl ) return r + + +def get_ssd_back_position( + de_dataset: xarray.Dataset, +) -> NDArray[np.float64]: + """ + Calculate the Y SSD positions (yb) for the specified dataset. + + This function calculates the back positions in the Y direction (yb) + for SSDs, expressed in hundredths of a millimeter. + + Parameters + ---------- + de_dataset : xarray.Dataset + The input dataset containing STOP_TYPE and SSD_FLAG data. + + Returns + ------- + yb : np.ndarray + A NumPy array containing the calculated Y SSD positions + in hundredths of a millimeter for each relevant epoch + in the dataset. The length of this array matches the number + of epochs where the STOP_TYPE is SSD. + + Notes + ----- + The X back position (xb) is assumed to be 0 for SSD. + """ + indices = np.nonzero(np.isin(de_dataset["STOP_TYPE"], StopType.SSD.value))[0] + yb = np.zeros(len(indices), dtype=np.float64) + de_filtered = de_dataset.isel(epoch=indices) + + for i in range(8): + # Multiply ybs times 100 to convert to hundredths of a millimeter. + yb[de_filtered[f"SSD_FLAG_{i}"].data == 1] = ( + get_image_params(f"YBKSSD{i}") * 100 + ) + + return np.asarray(yb, dtype=np.float64) From 9a7946160fd5d239baab23430a2ba4f0e1688381 Mon Sep 17 00:00:00 2001 From: Laura Sandoval Date: Tue, 27 Aug 2024 13:17:26 -0600 Subject: [PATCH 2/4] added back position --- imap_processing/ultra/l1b/ultra_l1b_extended.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/imap_processing/ultra/l1b/ultra_l1b_extended.py b/imap_processing/ultra/l1b/ultra_l1b_extended.py index 319077b5e..79fb07985 100644 --- a/imap_processing/ultra/l1b/ultra_l1b_extended.py +++ b/imap_processing/ultra/l1b/ultra_l1b_extended.py @@ -282,8 +282,7 @@ def get_ssd_back_position( yb : np.ndarray A NumPy array containing the calculated Y SSD positions in hundredths of a millimeter for each relevant epoch - in the dataset. The length of this array matches the number - of epochs where the STOP_TYPE is SSD. + in the dataset. Notes ----- From 5fe29b03afba5f7cf6037d9fb8e6f02ef5311ce3 Mon Sep 17 00:00:00 2001 From: Laura Sandoval Date: Tue, 27 Aug 2024 14:36:17 -0600 Subject: [PATCH 3/4] added tof offset --- .../ultra/unit/test_ultra_l1b_extended.py | 28 ++++++++++-- .../ultra/l1b/ultra_l1b_extended.py | 44 +++++++++++++------ 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py b/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py index fdae38096..a0cd7930a 100644 --- a/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py +++ b/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py @@ -5,12 +5,13 @@ import pytest from imap_processing.ultra.l1b.ultra_l1b_extended import ( + StartType, StopType, get_front_x_position, get_front_y_position, get_path_length, get_ph_tof_and_back_positions, - get_ssd_back_position, + get_ssd_back_position_and_tof_offset, ) @@ -88,14 +89,33 @@ def test_get_ph_tof_and_back_positions( np.testing.assert_array_equal(ph_yb, selected_rows["Yb"].astype("float")) -def test_get_ssd_back_position( +def test_get_ssd_back_position_and_tof_offset( de_dataset, events_fsw_comparison_theta_0, ): """Tests get_ssd_back_position function.""" - ssd_yb = get_ssd_back_position(de_dataset) + yb, tof_offset, ssd_number = get_ssd_back_position_and_tof_offset(de_dataset) df = pd.read_csv(events_fsw_comparison_theta_0) df_filt = df[(df["StartType"] != -1) & (df["StopType"] >= 8)] - np.testing.assert_array_equal(ssd_yb, df_filt["Yb"].astype("float")) + np.testing.assert_array_equal(yb, df_filt["Yb"].astype("float")) + + tof_offset_lt = tof_offset[df_filt["StartType"] == StartType.Left.value] + tof_offset_rt = tof_offset[df_filt["StartType"] == StartType.Right.value] + + ssd_number_lt = ssd_number[df_filt["StartType"] == StartType.Left.value] + ssd_number_rt = ssd_number[df_filt["StartType"] == StartType.Right.value] + + np.testing.assert_array_equal( + tof_offset_lt[ssd_number_lt == 3], + np.full(len(tof_offset_lt[ssd_number_lt == 3]), -4.2), + ) + np.testing.assert_array_equal( + tof_offset_rt[ssd_number_rt == 7], + np.full(len(tof_offset_rt[ssd_number_rt == 7]), -6), + ) + np.testing.assert_array_equal( + tof_offset_rt[ssd_number_rt == 4], + np.full(len(tof_offset_rt[ssd_number_rt == 4]), -4), + ) diff --git a/imap_processing/ultra/l1b/ultra_l1b_extended.py b/imap_processing/ultra/l1b/ultra_l1b_extended.py index 79fb07985..35fa6e87a 100644 --- a/imap_processing/ultra/l1b/ultra_l1b_extended.py +++ b/imap_processing/ultra/l1b/ultra_l1b_extended.py @@ -6,7 +6,6 @@ import numpy as np import xarray from numpy import ndarray -from numpy.typing import NDArray from imap_processing.ultra.l1b.lookup_utils import ( get_back_position, @@ -25,6 +24,13 @@ # TODO: make lookup tables into config files. +class StartType(Enum): + """Start Type: 1=Left, 2=Right.""" + + Left = 1 + Right = 2 + + class StopType(Enum): """Stop Type: 1=Top, 2=Bottom, SSD: 8-15.""" @@ -263,14 +269,11 @@ def get_path_length(front_position: tuple, back_position: tuple, d: float) -> fl return r -def get_ssd_back_position( +def get_ssd_back_position_and_tof_offset( de_dataset: xarray.Dataset, -) -> NDArray[np.float64]: +) -> tuple[np.ndarray, np.ndarray, np.ndarray]: """ - Calculate the Y SSD positions (yb) for the specified dataset. - - This function calculates the back positions in the Y direction (yb) - for SSDs, expressed in hundredths of a millimeter. + Lookup the Y SSD positions (yb), TOF Offset, and SSD number. Parameters ---------- @@ -280,22 +283,37 @@ def get_ssd_back_position( Returns ------- yb : np.ndarray - A NumPy array containing the calculated Y SSD positions - in hundredths of a millimeter for each relevant epoch - in the dataset. + Y SSD positions in hundredths of a millimeter. + tof_offset : np.ndarray + TOF offset. + ssd_number : np.ndarray + SSD number. Notes ----- The X back position (xb) is assumed to be 0 for SSD. """ indices = np.nonzero(np.isin(de_dataset["STOP_TYPE"], StopType.SSD.value))[0] - yb = np.zeros(len(indices), dtype=np.float64) de_filtered = de_dataset.isel(epoch=indices) + yb = np.zeros(len(indices), dtype=np.float64) + ssd_number = np.zeros(len(indices), dtype=int) + tof_offset = np.zeros(len(indices), dtype=np.float64) + for i in range(8): # Multiply ybs times 100 to convert to hundredths of a millimeter. yb[de_filtered[f"SSD_FLAG_{i}"].data == 1] = ( get_image_params(f"YBKSSD{i}") * 100 ) - - return np.asarray(yb, dtype=np.float64) + ssd_number[de_filtered[f"SSD_FLAG_{i}"].data == 1] = i + + tof_offset[ + (de_filtered["START_TYPE"] == StartType.Left.value) + & (de_filtered[f"SSD_FLAG_{i}"].data == 1) + ] = get_image_params(f"TOFSSDLTOFF{i}") + tof_offset[ + (de_filtered["START_TYPE"] == StartType.Right.value) + & (de_filtered[f"SSD_FLAG_{i}"].data == 1) + ] = get_image_params(f"TOFSSDRTOFF{i}") + + return yb, tof_offset, ssd_number From b8d9a4e5df11d636022dc7f1b07986aa52593f5b Mon Sep 17 00:00:00 2001 From: Laura Sandoval Date: Tue, 3 Sep 2024 09:24:35 -0600 Subject: [PATCH 4/4] response to pr --- .../tests/ultra/unit/test_ultra_l1b_extended.py | 8 ++++++++ imap_processing/ultra/l1b/ultra_l1b_extended.py | 14 ++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py b/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py index a0cd7930a..2f9ed9826 100644 --- a/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py +++ b/imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py @@ -119,3 +119,11 @@ def test_get_ssd_back_position_and_tof_offset( tof_offset_rt[ssd_number_rt == 4], np.full(len(tof_offset_rt[ssd_number_rt == 4]), -4), ) + + assert np.all(ssd_number_lt >= 0), "Values in ssd_number_lt out of range." + + assert np.all(ssd_number_lt <= 7), "Values in ssd_number_lt out of range." + + assert np.all(ssd_number_rt >= 0), "Values in ssd_number_rt out of range." + + assert np.all(ssd_number_rt <= 7), "Values in ssd_number_rt out of range." diff --git a/imap_processing/ultra/l1b/ultra_l1b_extended.py b/imap_processing/ultra/l1b/ultra_l1b_extended.py index 35fa6e87a..a70e5e032 100644 --- a/imap_processing/ultra/l1b/ultra_l1b_extended.py +++ b/imap_processing/ultra/l1b/ultra_l1b_extended.py @@ -301,19 +301,17 @@ def get_ssd_back_position_and_tof_offset( tof_offset = np.zeros(len(indices), dtype=np.float64) for i in range(8): + ssd_flag_mask = de_filtered[f"SSD_FLAG_{i}"].data == 1 + # Multiply ybs times 100 to convert to hundredths of a millimeter. - yb[de_filtered[f"SSD_FLAG_{i}"].data == 1] = ( - get_image_params(f"YBKSSD{i}") * 100 - ) - ssd_number[de_filtered[f"SSD_FLAG_{i}"].data == 1] = i + yb[ssd_flag_mask] = get_image_params(f"YBKSSD{i}") * 100 + ssd_number[ssd_flag_mask] = i tof_offset[ - (de_filtered["START_TYPE"] == StartType.Left.value) - & (de_filtered[f"SSD_FLAG_{i}"].data == 1) + (de_filtered["START_TYPE"] == StartType.Left.value) & ssd_flag_mask ] = get_image_params(f"TOFSSDLTOFF{i}") tof_offset[ - (de_filtered["START_TYPE"] == StartType.Right.value) - & (de_filtered[f"SSD_FLAG_{i}"].data == 1) + (de_filtered["START_TYPE"] == StartType.Right.value) & ssd_flag_mask ] = get_image_params(f"TOFSSDRTOFF{i}") return yb, tof_offset, ssd_number