Skip to content

Commit 1f162e4

Browse files
authored
FIX: Apply flux correction to background intensities for Lo (#2384)
Lo carries their background intensities separately, so we need to correct those as well.
1 parent ccca558 commit 1f162e4

2 files changed

Lines changed: 35 additions & 21 deletions

File tree

imap_processing/lo/l2/lo_l2.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,27 +1166,32 @@ def calculate_flux_corrections(dataset: xr.Dataset, flux_factors: Path) -> xr.Da
11661166

11671167
# Flux correction
11681168
corrector = PowerLawFluxCorrector(flux_factors)
1169-
# FluxCorrector works on (energy, :) arrays, so we need to flatten the map
1170-
# spatial dimensions for the correction and then reshape back after.
1171-
input_shape = dataset["ena_intensity"].shape[1:] # Exclude epoch dimension
1172-
intensity = dataset["ena_intensity"].values[0].reshape(len(dataset["energy"]), -1)
1173-
stat_uncert = (
1174-
dataset["ena_intensity_stat_uncert"]
1175-
.values[0]
1176-
.reshape(len(dataset["energy"]), -1)
1177-
)
1178-
corrected_intensity, corrected_stat_unc = corrector.apply_flux_correction(
1179-
intensity,
1180-
stat_uncert,
1181-
dataset["energy"].data,
1182-
)
1183-
# Add the size 1 epoch dimension back in to the corrected fluxes.
1184-
dataset["ena_intensity"].data = corrected_intensity.reshape(input_shape)[
1185-
np.newaxis, ...
1186-
]
1187-
dataset["ena_intensity_stat_uncert"].data = corrected_stat_unc.reshape(input_shape)[
1188-
np.newaxis, ...
1189-
]
1169+
1170+
# NOTE: We need to apply this to both total flux and background flux
1171+
for var in ["ena", "bg"]:
1172+
# FluxCorrector works on (energy, :) arrays, so we need to flatten the map
1173+
# spatial dimensions for the correction and then reshape back after.
1174+
input_shape = dataset[f"{var}_intensity"].shape[1:] # Exclude epoch dimension
1175+
intensity = (
1176+
dataset[f"{var}_intensity"].values[0].reshape(len(dataset["energy"]), -1)
1177+
)
1178+
stat_uncert = (
1179+
dataset[f"{var}_intensity_stat_uncert"]
1180+
.values[0]
1181+
.reshape(len(dataset["energy"]), -1)
1182+
)
1183+
corrected_intensity, corrected_stat_unc = corrector.apply_flux_correction(
1184+
intensity,
1185+
stat_uncert,
1186+
dataset["energy"].data,
1187+
)
1188+
# Add the size 1 epoch dimension back in to the corrected fluxes.
1189+
dataset[f"{var}_intensity"].data = corrected_intensity.reshape(input_shape)[
1190+
np.newaxis, ...
1191+
]
1192+
dataset[f"{var}_intensity_stat_uncert"].data = corrected_stat_unc.reshape(
1193+
input_shape
1194+
)[np.newaxis, ...]
11901195

11911196
return dataset
11921197

imap_processing/tests/lo/test_lo_l2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ def sample_dataset_with_intensities():
566566
intensity_values * 0.1,
567567
)
568568

569+
dataset["bg_intensity"] = dataset["ena_intensity"] * 0.5 # 50% background
570+
571+
# Add statistical uncertainties (10% of intensity)
572+
dataset["bg_intensity_stat_uncert"] = dataset["bg_intensity"] * 0.1
573+
569574
return dataset
570575

571576

@@ -1142,6 +1147,10 @@ def test_calculate_flux_corrections_energy_dimension_handling(
11421147
("epoch", "energy", "x", "y"),
11431148
uncert_values.copy(),
11441149
)
1150+
original_dataset["bg_intensity"] = original_dataset["ena_intensity"] * 0.5
1151+
original_dataset["bg_intensity_stat_uncert"] = (
1152+
original_dataset["bg_intensity"] * 0.1
1153+
)
11451154

11461155
# Run flux correction on a copy
11471156
dataset_copy = original_dataset.copy(deep=True)

0 commit comments

Comments
 (0)