Skip to content

Commit dc85ad3

Browse files
authored
SWAPI: fill ESA step 0 with nan value. Update acquisition time (IMAP-Science-Operations-Center#2102)
1 parent eb4188c commit dc85ad3

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

imap_processing/ialirt/l0/process_swapi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from imap_processing.ialirt.utils.time import calculate_time
1616
from imap_processing.spice.time import met_to_ttj2000ns, met_to_utc
1717
from imap_processing.swapi.l1.swapi_l1 import process_sweep_data
18-
from imap_processing.swapi.l2.swapi_l2 import TIME_PER_BIN
18+
from imap_processing.swapi.l2.swapi_l2 import SWAPI_LIVETIME
1919

2020
logger = logging.getLogger(__name__)
2121

@@ -190,8 +190,8 @@ def process_swapi_ialirt(unpacked_data: xr.Dataset) -> list[dict]:
190190
continue
191191

192192
raw_coin_count = process_sweep_data(grouped_dataset, "swapi_coin_cnt")
193-
raw_coin_rate = raw_coin_count / TIME_PER_BIN
194-
count_rate_error = np.sqrt(raw_coin_count) / TIME_PER_BIN
193+
raw_coin_rate = raw_coin_count / SWAPI_LIVETIME
194+
count_rate_error = np.sqrt(raw_coin_count) / SWAPI_LIVETIME
195195

196196
solution = optimize_pseudo_parameters(raw_coin_rate, count_rate_error)
197197

imap_processing/swapi/l1/swapi_l1.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def decompress_count(
138138

139139
# SWAPI suggested using big value to indicate overflow.
140140
new_count[compressed_indices & (count_data == 0xFFFF)] = np.iinfo(np.int32).max
141-
return new_count
141+
return (new_count).astype(np.float32)
142142

143143

144144
def find_sweep_starts(packets: xr.Dataset) -> npt.NDArray:
@@ -474,6 +474,12 @@ def process_swapi_science(
474474
swp_scem_counts = decompress_count(raw_scem_count, scem_compression_flags)
475475
swp_coin_counts = decompress_count(raw_coin_count, coin_compression_flags)
476476

477+
# Fill first index of 72 steps with nan value per
478+
# SWAPI team's instruction. nan helps with plotting.
479+
swp_pcem_counts[:, 0] = np.nan
480+
swp_scem_counts[:, 0] = np.nan
481+
swp_coin_counts[:, 0] = np.nan
482+
477483
# ====================================================
478484
# Load the CDF attributes
479485
# ====================================================
@@ -600,17 +606,17 @@ def process_swapi_science(
600606
)
601607

602608
dataset["swp_pcem_counts"] = xr.DataArray(
603-
np.array(swp_pcem_counts, dtype=np.uint16),
609+
swp_pcem_counts,
604610
dims=["epoch", "esa_step"],
605611
attrs=cdf_manager.get_variable_attributes("pcem_counts"),
606612
)
607613
dataset["swp_scem_counts"] = xr.DataArray(
608-
np.array(swp_scem_counts, dtype=np.uint16),
614+
swp_scem_counts,
609615
dims=["epoch", "esa_step"],
610616
attrs=cdf_manager.get_variable_attributes("scem_counts"),
611617
)
612618
dataset["swp_coin_counts"] = xr.DataArray(
613-
np.array(swp_coin_counts, dtype=np.uint16),
619+
swp_coin_counts,
614620
dims=["epoch", "esa_step"],
615621
attrs=cdf_manager.get_variable_attributes("coin_counts"),
616622
)

imap_processing/swapi/l2/swapi_l2.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
logger = logging.getLogger(__name__)
1313

1414

15-
TIME_PER_BIN = 0.167 # seconds
15+
SWAPI_LIVETIME = 0.145 # seconds
1616

1717

1818
def solve_full_sweep_energy(
@@ -159,14 +159,12 @@ def swapi_l2(
159159
160160
To process science data to L2, we need to:
161161
- convert counts to rates. This is done by dividing the counts by the
162-
TIME_PER_BIN time. TIME_PER_BIN is the exposure time per energy bin which is
163-
obtained by dividing the time for one complete sweep
164-
(12 s, coarse + fine sweep) by the total energy steps (72),
165-
i.e., TIME_PER_BIN = 12/72 = 0.167 s. This will be constant.
162+
SWAPI_LIVETIME time. LIVETIME is data acquisition time. It will
163+
be constant, SWAPI_LIVETIME = 0.145 s.
166164
167165
- update uncertainty. Calculate new uncertainty value using
168-
SWP_PCEM_ERR data from level one and divide by TIME_PER_BIN. Eg.
169-
SWP_PCEM_UNC = SWP_PCEM_ERR / TIME_PER_BIN
166+
SWP_PCEM_ERR data from level one and divide by SWAPI_LIVETIME. Eg.
167+
SWP_PCEM_UNC = SWP_PCEM_ERR / SWAPI_LIVETIME
170168
Do the same for SCEM and COIN data.
171169
172170
Parameters
@@ -233,32 +231,32 @@ def swapi_l2(
233231
]
234232

235233
# convert counts to rate
236-
l2_dataset["swp_pcem_rate"] = l1_dataset["swp_pcem_counts"] / TIME_PER_BIN
237-
l2_dataset["swp_scem_rate"] = l1_dataset["swp_scem_counts"] / TIME_PER_BIN
238-
l2_dataset["swp_coin_rate"] = l1_dataset["swp_coin_counts"] / TIME_PER_BIN
234+
l2_dataset["swp_pcem_rate"] = l1_dataset["swp_pcem_counts"] / SWAPI_LIVETIME
235+
l2_dataset["swp_scem_rate"] = l1_dataset["swp_scem_counts"] / SWAPI_LIVETIME
236+
l2_dataset["swp_coin_rate"] = l1_dataset["swp_coin_counts"] / SWAPI_LIVETIME
239237
# update attrs
240238
l2_dataset["swp_pcem_rate"].attrs = cdf_manager.get_variable_attributes("pcem_rate")
241239
l2_dataset["swp_scem_rate"].attrs = cdf_manager.get_variable_attributes("scem_rate")
242240
l2_dataset["swp_coin_rate"].attrs = cdf_manager.get_variable_attributes("coin_rate")
243241

244242
# update uncertainty
245243
l2_dataset["swp_pcem_rate_stat_uncert_plus"] = (
246-
l1_dataset["swp_pcem_counts_stat_uncert_plus"] / TIME_PER_BIN
244+
l1_dataset["swp_pcem_counts_stat_uncert_plus"] / SWAPI_LIVETIME
247245
)
248246
l2_dataset["swp_pcem_rate_stat_uncert_minus"] = (
249-
l1_dataset["swp_pcem_counts_stat_uncert_minus"] / TIME_PER_BIN
247+
l1_dataset["swp_pcem_counts_stat_uncert_minus"] / SWAPI_LIVETIME
250248
)
251249
l2_dataset["swp_scem_rate_stat_uncert_plus"] = (
252-
l1_dataset["swp_scem_counts_stat_uncert_plus"] / TIME_PER_BIN
250+
l1_dataset["swp_scem_counts_stat_uncert_plus"] / SWAPI_LIVETIME
253251
)
254252
l2_dataset["swp_scem_rate_stat_uncert_minus"] = (
255-
l1_dataset["swp_scem_counts_stat_uncert_minus"] / TIME_PER_BIN
253+
l1_dataset["swp_scem_counts_stat_uncert_minus"] / SWAPI_LIVETIME
256254
)
257255
l2_dataset["swp_coin_rate_stat_uncert_plus"] = (
258-
l1_dataset["swp_coin_counts_stat_uncert_plus"] / TIME_PER_BIN
256+
l1_dataset["swp_coin_counts_stat_uncert_plus"] / SWAPI_LIVETIME
259257
)
260258
l2_dataset["swp_coin_rate_stat_uncert_minus"] = (
261-
l1_dataset["swp_coin_counts_stat_uncert_minus"] / TIME_PER_BIN
259+
l1_dataset["swp_coin_counts_stat_uncert_minus"] / SWAPI_LIVETIME
262260
)
263261
# update attrs
264262
l2_dataset[

imap_processing/tests/swapi/test_swapi_l1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_decompress_count():
8484
# compressed + no-overflow, compressed + overflow, no compression
8585
raw_values = np.array([[12, 0xFFFF, 12]])
8686
compression_flag = np.array([[1, 1, 0]])
87-
expected = np.array([[12 * 16, np.iinfo(np.int32).max, 12]], dtype=np.int32)
87+
expected = np.array([[12 * 16, np.iinfo(np.int32).max, 12]], dtype=np.float32)
8888
returned_value = decompress_count(raw_values, compression_flag)
8989
np.testing.assert_array_equal(returned_value, expected)
9090

imap_processing/tests/swapi/test_swapi_l2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from imap_processing.cdf.utils import load_cdf, write_cdf
1111
from imap_processing.swapi.l1.swapi_l1 import swapi_l1
1212
from imap_processing.swapi.l2.swapi_l2 import (
13-
TIME_PER_BIN,
13+
SWAPI_LIVETIME,
1414
solve_full_sweep_energy,
1515
swapi_l2,
1616
)
@@ -123,7 +123,7 @@ def second_get_file_paths_side_effect(descriptor):
123123
# Test uncertainty variables are as expected
124124
np.testing.assert_array_equal(
125125
l2_dataset["swp_pcem_rate_stat_uncert_plus"],
126-
l1_dataset["swp_pcem_counts_stat_uncert_plus"] / TIME_PER_BIN,
126+
l1_dataset["swp_pcem_counts_stat_uncert_plus"] / SWAPI_LIVETIME,
127127
)
128128
# Since L2 data's date is before any date in ESA unit conversion table,
129129
# check that it returns nan in first 63 energy steps

0 commit comments

Comments
 (0)