Skip to content

Commit d969a1d

Browse files
authored
Fix Hi L2 naming of bg variables (IMAP-Science-Operations-Center#2677)
1 parent 7d82163 commit d969a1d

4 files changed

Lines changed: 30 additions & 32 deletions

File tree

imap_processing/ena_maps/ena_maps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,8 @@ class HiPointingSet(LoHiBasePointingSet):
657657
# renamed to match L2 variables
658658
l1c_to_l2_var_mapping: ClassVar[dict[str, str]] = {
659659
"exposure_times": "exposure_factor",
660-
"background_rates": "bg_rates",
661-
"background_rates_uncertainty": "bg_rates_unc",
660+
"background_rates": "bg_rate",
661+
"background_rates_uncertainty": "bg_rate_sys_err",
662662
}
663663

664664
def __init__(self, dataset: xr.Dataset | str | Path):

imap_processing/hi/hi_l2.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
SC_FRAME_VARS_TO_PROJECT = {
2929
"counts",
3030
"exposure_factor",
31-
"bg_rates",
32-
"bg_rates_unc",
31+
"bg_rate",
32+
"bg_rate_sys_err",
3333
"obs_date",
3434
}
3535
HELIO_FRAME_VARS_TO_PROJECT = SC_FRAME_VARS_TO_PROJECT | {"energy_sc"}
3636
# TODO: is an exposure time weighted average for obs_date appropriate?
37-
FULL_EXPOSURE_TIME_AVERAGE_SET = {"bg_rates", "bg_rates_unc", "obs_date", "energy_sc"}
37+
FULL_EXPOSURE_TIME_AVERAGE_SET = {"bg_rate", "bg_rate_sys_err", "obs_date", "energy_sc"}
3838

3939

4040
# =============================================================================
@@ -297,7 +297,7 @@ def calculate_all_rates_and_intensities(
297297
Parameters
298298
----------
299299
map_ds : xarray.Dataset
300-
Map dataset with projected PSET data (counts, exposure_factor, bg_rates,
300+
Map dataset with projected PSET data (counts, exposure_factor, bg_rate,
301301
energy_delta_minus, energy_delta_plus, etc.) and an `energy` coordinate
302302
containing the ESA nominal central energies in keV.
303303
l2_ancillary_path_dict : dict[str, pathlib.Path]
@@ -362,7 +362,7 @@ def calculate_ena_signal_rates(map_ds: xr.Dataset) -> xr.Dataset:
362362
Parameters
363363
----------
364364
map_ds : xarray.Dataset
365-
Map dataset that has counts, exposure_factor, and bg_rates calculated.
365+
Map dataset that has counts, exposure_factor, and bg_rate calculated.
366366
367367
Returns
368368
-------
@@ -373,7 +373,7 @@ def calculate_ena_signal_rates(map_ds: xr.Dataset) -> xr.Dataset:
373373
with np.errstate(divide="ignore"):
374374
# Calculate the ENA Signal Rate
375375
map_ds["ena_signal_rates"] = (
376-
map_ds["counts"] / map_ds["exposure_factor"] - map_ds["bg_rates"]
376+
map_ds["counts"] / map_ds["exposure_factor"] - map_ds["bg_rate"]
377377
)
378378
# Calculate the ENA Signal Rate Uncertainties
379379
# The minimum count uncertainty is 1 for any pixel that has non-zero
@@ -440,7 +440,7 @@ def calculate_ena_intensity(
440440
map_ds["ena_signal_rate_stat_unc"] / flux_conversion_divisor
441441
)
442442
map_ds["ena_intensity_sys_err"] = (
443-
np.sqrt(map_ds["bg_rates"] * map_ds["exposure_factor"])
443+
np.sqrt(map_ds["bg_rate"] * map_ds["exposure_factor"])
444444
/ map_ds["exposure_factor"]
445445
/ flux_conversion_divisor
446446
)
@@ -562,7 +562,7 @@ def _calculate_improved_stat_variance(
562562

563563
logger.debug("Computing geometric factor normalized signal rates")
564564

565-
# signal_rates = counts / exposure_factor - bg_rates
565+
# signal_rates = counts / exposure_factor - bg_rate
566566
# signal_rates shape is: (n_epoch, n_energy, n_cal_prod, n_spatial_pixels)
567567
signal_rates = map_ds["ena_signal_rates"]
568568

@@ -581,7 +581,7 @@ def _calculate_improved_stat_variance(
581581
logger.debug("Including background rates in uncertainty calculation")
582582
# Convert averaged signal rates back to flux uncertainties
583583
# Total count rates for Poisson uncertainty calculation
584-
total_count_rates_for_uncertainty = map_ds["bg_rates"] + averaged_signal_rates
584+
total_count_rates_for_uncertainty = map_ds["bg_rate"] + averaged_signal_rates
585585

586586
logger.debug("Computing improved flux uncertainties")
587587
# Statistical variance:

imap_processing/tests/ena_maps/test_ena_maps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_init(self, hi_pset_cdf_path):
154154
assert hi_pset.num_points == 3600
155155
np.testing.assert_array_equal(hi_pset.az_el_points.shape, (3600, 2))
156156

157-
for var_name in ["exposure_factor", "bg_rates", "bg_rates_unc"]:
157+
for var_name in ["exposure_factor", "bg_rate", "bg_rate_sys_err"]:
158158
assert var_name in hi_pset.data
159159

160160
def test_from_cdf(self, hi_pset_cdf_path):

imap_processing/tests/hi/test_hi_l2.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ def sample_map_dataset():
128128
"ena_intensity_sys_err": xr.DataArray(
129129
np.random.rand(*shape) * 5 + 1, dims=list(coords.keys())
130130
),
131-
"bg_rates": xr.DataArray(
131+
"bg_rate": xr.DataArray(
132132
np.random.rand(*shape) * 20 + 5, dims=list(coords.keys())
133133
),
134-
"bg_rates_unc": xr.DataArray(
134+
"bg_rate_sys_err": xr.DataArray(
135135
np.random.rand(*shape) * 2 + 1, dims=list(coords.keys())
136136
),
137137
"exposure_factor": xr.DataArray(
@@ -301,10 +301,10 @@ def test_calculate_ena_signal_rates(empty_rectangular_map_dataset):
301301
name="exposure_factor",
302302
dims=list(exposure_sizes.keys()),
303303
),
304-
"bg_rates": xr.DataArray(
304+
"bg_rate": xr.DataArray(
305305
np.arange(np.prod(tuple(map_ds.sizes.values()))).reshape(counts_shape)
306306
% 2,
307-
name="bg_rates",
307+
name="bg_rate",
308308
dims=list(map_ds.sizes.keys()),
309309
),
310310
}
@@ -316,7 +316,7 @@ def test_calculate_ena_signal_rates(empty_rectangular_map_dataset):
316316
assert var_name in result_ds
317317
assert result_ds[var_name].shape == counts_shape
318318
# Verify that there are no negative signal rates. The synthetic data combination
319-
# where counts = 0, exposure_factor = 1, and bg_rates = 1 would result in
319+
# where counts = 0, exposure_factor = 1, and bg_rate = 1 would result in
320320
# an ena_signal_rate of (0 / 1) - 1 = -1
321321
assert np.nanmin(result_ds["ena_signal_rates"].values) >= 0
322322
# Verify that the minimum finite uncertainty is sqrt(1) / exposure_factor.
@@ -345,9 +345,9 @@ def ena_intensity_map_ds(empty_rectangular_map_dataset):
345345
name="ena_signal_rate_stat_unc",
346346
dims=list(map_ds.sizes.keys()),
347347
),
348-
"bg_rates_unc": xr.DataArray(
348+
"bg_rate_sys_err": xr.DataArray(
349349
np.arange(np.prod(tuple(map_ds.sizes.values()))).reshape(var_shape) % 3,
350-
name="bg_rates_unc",
350+
name="bg_rate_sys_err",
351351
dims=list(map_ds.sizes.keys()),
352352
),
353353
}
@@ -359,7 +359,7 @@ def ena_intensity_map_ds(empty_rectangular_map_dataset):
359359
)
360360
map_ds.update(
361361
{
362-
"bg_rates": xr.DataArray(
362+
"bg_rate": xr.DataArray(
363363
np.ones(bg_shape) * 5.0,
364364
dims=[d for d in map_ds.sizes.keys() if d != "calibration_prod"],
365365
),
@@ -566,7 +566,7 @@ def test_weighted_average_mathematical_correctness():
566566
np.array([100.0, 400.0]).reshape(1, 1, 2, 1, 1),
567567
dims=list(coords.keys()),
568568
),
569-
"bg_rates": xr.DataArray(
569+
"bg_rate": xr.DataArray(
570570
np.array([5.0]).reshape(1, 1, 1, 1),
571571
dims=[d for d in coords.keys() if d != "calibration_prod"],
572572
),
@@ -624,7 +624,7 @@ def test_statistical_uncertainty_combination_correctness():
624624
sys_err_values, dims=list(coords.keys())
625625
),
626626
"ena_signal_rates": xr.DataArray(flux_values, dims=list(coords.keys())),
627-
"bg_rates": xr.DataArray(
627+
"bg_rate": xr.DataArray(
628628
np.array([1.0, 2.0]).reshape(1, 1, 2, 1, 1), dims=list(coords.keys())
629629
),
630630
"exposure_factor": xr.DataArray(
@@ -783,8 +783,8 @@ def test_process_single_pset_renames_variables(
783783

784784
# Check that variables were renamed
785785
assert "exposure_factor" in result
786-
assert "bg_rates" in result
787-
assert "bg_rates_unc" in result
786+
assert "bg_rate" in result
787+
assert "bg_rate_sys_err" in result
788788
# Original names should not exist
789789
assert "exposure_times" not in result
790790
assert "background_rates" not in result
@@ -834,16 +834,16 @@ def test_process_single_pset_exposure_time_weighting(
834834
descriptor = MapDescriptor.from_string("h90-ena-h-sf-nsp-full-gcs-6deg-3mo")
835835
energy_kev = xr.DataArray([0.5, 0.75, 1.1], dims=["esa_energy_step"])
836836

837-
# bg_rates should be multiplied by exposure_factor
837+
# bg_rate should be multiplied by exposure_factor
838838
result = process_single_pset(
839839
mock_pset_dataset,
840840
energy_kev,
841841
descriptor,
842-
vars_to_exposure_time_average={"bg_rates"},
842+
vars_to_exposure_time_average={"bg_rate"},
843843
)
844844

845-
# bg_rates was 5.0, exposure_factor is 100.0, so result should be 500.0
846-
assert np.allclose(result["bg_rates"].values, 500.0)
845+
# bg_rate was 5.0, exposure_factor is 100.0, so result should be 500.0
846+
assert np.allclose(result["bg_rate"].values, 500.0)
847847

848848

849849
@mock.patch("imap_processing.hi.hi_l2.calculate_ram_mask")
@@ -973,10 +973,8 @@ def mock_map_dataset_for_rates():
973973
np.ones(exposure_shape) * 10.0,
974974
dims=["epoch", "esa_energy_step", "longitude", "latitude"],
975975
),
976-
"bg_rates": xr.DataArray(
977-
np.ones(shape) * 2.0, dims=list(coords.keys())[:5]
978-
),
979-
"bg_rates_unc": xr.DataArray(
976+
"bg_rate": xr.DataArray(np.ones(shape) * 2.0, dims=list(coords.keys())[:5]),
977+
"bg_rate_sys_err": xr.DataArray(
980978
np.ones(shape) * 0.5, dims=list(coords.keys())[:5]
981979
),
982980
"obs_date": xr.DataArray(

0 commit comments

Comments
 (0)