Skip to content

Commit 64f579d

Browse files
committed
get rid of reshaping
1 parent eebe81b commit 64f579d

File tree

2 files changed

+75
-69
lines changed

2 files changed

+75
-69
lines changed

imap_processing/codice/codice_l2.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -934,68 +934,60 @@ def process_hi_direct_events(dependencies: ProcessingInputCollection) -> xr.Data
934934
l2_dataset = l1a_dataset
935935
# Load energy table and tof table needed for conversions
936936
energy_table, tof_table = get_hi_de_luts(dependencies)
937-
937+
# Initialize nan array for calculations
938+
nan_array = np.full(l2_dataset["ssd_id"].shape, np.nan)
938939
# Convert from position to elevation angle in degrees relative to the spacecraft
939940
# axis
940-
elevation_angle_shape = l2_dataset["ssd_id"].shape
941-
ssd_id_flat = l2_dataset["ssd_id"].values.ravel()
942-
elevation_angle = np.array(
943-
[SSD_ID_TO_ELEVATION.get(id, np.nan) for id in ssd_id_flat]
944-
).reshape(elevation_angle_shape)
945-
946-
l2_dataset["elevation_angle"] = (
947-
l2_dataset["ssd_id"].dims,
948-
elevation_angle.astype(np.float32),
941+
ssd_id = l2_dataset["ssd_id"].values
942+
valid_ssd = (ssd_id <= 15) & (ssd_id >= 0)
943+
944+
elevation = nan_array.copy()
945+
elevation[valid_ssd] = SSD_ID_TO_ELEVATION[ssd_id[valid_ssd]]
946+
l2_dataset["elevation_angle"] = xr.DataArray(
947+
data=elevation.astype(np.float32), dims=l2_dataset["ssd_id"].dims
949948
)
950949
# Calculate ssd energy in meV
951-
gain_flat = l2_dataset["gain"].values.ravel()
952-
ssd_energy_flat = l2_dataset["ssd_energy"].values.ravel()
953-
# Initialize ssd_energy with nans
954-
ssd_energy = np.full(ssd_energy_flat.shape, np.nan)
950+
gain = l2_dataset["gain"].values
951+
ssd_energy = l2_dataset["ssd_energy"].values
955952
valid_mask = (
956-
(np.isin(gain_flat, list(GAIN_ID_TO_STR.keys())))
957-
& (ssd_id_flat <= 15)
958-
& (ssd_energy_flat != len(energy_table))
953+
(np.isin(gain, list(GAIN_ID_TO_STR.keys())))
954+
& valid_ssd
955+
& (ssd_energy != len(energy_table))
959956
)
960957
# The columns are organized in order of id and gains
961958
# E.g. ssd 0 - LG, ssd 0 - MG, ssd 0 - HG, ssd 1 - LG, ssd 1 - MG, ssd 1 - HG, ...
962-
cols = ssd_id_flat * 3 + (gain_flat - 1)
963-
ssd_energy[valid_mask] = energy_table[ssd_energy_flat[valid_mask], cols[valid_mask]]
964-
l2_dataset["ssd_energy"].data = ssd_energy.reshape(
965-
l2_dataset["ssd_energy"].shape
966-
).astype(np.float32)
959+
cols = ssd_id * 3 + (gain - 1)
960+
ssd_energy_converted = nan_array.copy()
961+
ssd_energy_converted[valid_mask] = energy_table[
962+
ssd_energy[valid_mask], cols[valid_mask]
963+
]
964+
l2_dataset["ssd_energy"].data = ssd_energy_converted.astype(np.float32)
967965

968966
# Convert spin_sector to spin_angle in degrees
969-
theta_angles = np.array(
970-
[SSD_ID_TO_SPIN_ANGLE.get(ssd_id, np.nan) for ssd_id in ssd_id_flat]
971-
)
972-
spin_angles = (
973-
theta_angles + 15.0 * l2_dataset["spin_sector"].values.ravel()
974-
) % 360.0
975-
976-
l2_dataset["spin_angle"] = xr.DataArray(
977-
data=spin_angles.reshape(l2_dataset["spin_sector"].shape),
978-
dims=l2_dataset["spin_sector"].dims,
967+
theta_angles = nan_array.copy()
968+
theta_angles[valid_ssd] = SSD_ID_TO_SPIN_ANGLE[ssd_id[valid_ssd]]
969+
l2_dataset["spin_angle"] = (
970+
(theta_angles + 15.0 * l2_dataset["spin_sector"]) % 360.0
979971
).astype(np.float32)
980972

981973
# Calculate TOF in ns
982-
tof_flat = l2_dataset["tof"].values.ravel()
974+
tof = l2_dataset["tof"].values
983975
# Get valid TOF indices for lookup
984-
valid_tof_mask = tof_flat < tof_table.shape[0]
985-
tof_ns = np.full(tof_flat.shape, np.nan)
976+
valid_tof_mask = tof < tof_table.shape[0]
977+
tof_ns = nan_array.copy()
986978
# Get tof values in ns from first column of tof_table
987-
tof_ns[valid_tof_mask] = tof_table[tof_flat[valid_tof_mask], 0]
979+
tof_ns[valid_tof_mask] = tof_table[tof[valid_tof_mask], 0]
988980
l2_dataset["tof"] = xr.DataArray(
989-
data=tof_ns.reshape(l2_dataset["tof"].shape),
981+
data=tof_ns,
990982
dims=l2_dataset["tof"].dims,
991983
).astype(np.float32)
992984

993985
# Calculate energy per nuc
994-
energy_nuc = np.full(tof_flat.shape, np.nan)
986+
energy_nuc = nan_array.copy()
995987
# Get value from second column of tof_table (E/n (MeV/n))
996-
energy_nuc[valid_tof_mask] = tof_table[tof_flat[valid_tof_mask], 1]
988+
energy_nuc[valid_tof_mask] = tof_table[tof[valid_tof_mask], 1]
997989
l2_dataset["energy_per_nuc"] = xr.DataArray(
998-
data=energy_nuc.reshape(l2_dataset["tof"].shape),
990+
data=energy_nuc,
999991
dims=l2_dataset["tof"].dims,
1000992
).astype(np.float32)
1001993
# Drop unused variables

imap_processing/codice/constants.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,36 +1142,50 @@
11421142
}
11431143

11441144
# SSD ID to Elevation Angle
1145-
SSD_ID_TO_ELEVATION = {
1146-
0: 150.0,
1147-
1: 138.6,
1148-
3: 115.7,
1149-
4: 90.0,
1150-
5: 64.3,
1151-
7: 41.4,
1152-
8: 30.0,
1153-
9: 41.4,
1154-
11: 64.3,
1155-
12: 90.0,
1156-
13: 115.7,
1157-
15: 138.6,
1158-
}
1145+
# The index corresponds to the SSD ID. Missing SSD IDs are represented with np.nan.
1146+
SSD_ID_TO_ELEVATION = np.array(
1147+
[
1148+
150.0,
1149+
138.6,
1150+
np.nan,
1151+
115.7,
1152+
90.0,
1153+
64.3,
1154+
np.nan,
1155+
41.4,
1156+
30.0,
1157+
41.4,
1158+
np.nan,
1159+
64.3,
1160+
90.0,
1161+
115.7,
1162+
np.nan,
1163+
138.6,
1164+
]
1165+
)
11591166

11601167
# gain lookup table
11611168
GAIN_ID_TO_STR = {1: "LG", 2: "MG", 3: "HG"}
11621169

1163-
# SSD ID to Spin Angle
1164-
SSD_ID_TO_SPIN_ANGLE = {
1165-
0: 277.50,
1166-
1: 236.61,
1167-
3: 221.19,
1168-
4: 217.5,
1169-
5: 221.19,
1170-
7: 236.61,
1171-
8: 277.50,
1172-
9: 318.39,
1173-
11: 333.81,
1174-
12: 337.50,
1175-
13: 333.81,
1176-
15: 318.39,
1177-
}
1170+
# SSD ID to Spin Angle (degrees)
1171+
# The index corresponds to the SSD ID. Missing SSD IDs are represented with np.nan.
1172+
SSD_ID_TO_SPIN_ANGLE = np.array(
1173+
[
1174+
277.50,
1175+
236.61,
1176+
np.nan,
1177+
221.19,
1178+
217.5,
1179+
221.19,
1180+
np.nan,
1181+
236.61,
1182+
277.50,
1183+
318.39,
1184+
np.nan,
1185+
333.81,
1186+
337.50,
1187+
333.81,
1188+
np.nan,
1189+
318.39,
1190+
]
1191+
)

0 commit comments

Comments
 (0)