Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions imap_processing/ialirt/l0/process_swe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging

import numpy as np
import pandas as pd
import xarray as xr
from numpy.typing import NDArray

Expand Down Expand Up @@ -155,26 +154,24 @@ def get_ialirt_energies() -> list:
return energy


def normalize_counts(counts: NDArray, latest_cal: pd.Series) -> NDArray:
def normalize_counts(counts: NDArray, interp_cal: NDArray) -> NDArray:
"""
Normalize the counts using the latest calibration factor.

Parameters
----------
counts : np.ndarray
Array of counts.
latest_cal : pd.Series
Array of latest calibration factors.
interp_cal : np.ndarray
Array of calibration factors.

Returns
-------
norm_counts : np.ndarray
Array of normalized counts.
"""
latest_cal = latest_cal.to_numpy()

# Norm counts where counts are non-negative
norm_counts = counts * (latest_cal / GEOMETRIC_FACTORS)[:, np.newaxis]
norm_counts = counts * (interp_cal / GEOMETRIC_FACTORS)[:, np.newaxis]
norm_counts[norm_counts < 0] = 0

return norm_counts
Expand Down Expand Up @@ -520,10 +517,56 @@ def process_swe(accumulated_data: xr.Dataset, in_flight_cal_files: list) -> list

# Grab the latest calibration factor
in_flight_cal_df = read_in_flight_cal_data(in_flight_cal_files)
latest_cal = in_flight_cal_df.sort_values("met_time").iloc[-1][1::]
cal_cols = [f"cem{i}" for i in range(1, 8)]
cal_met = in_flight_cal_df["met_time"].to_numpy()

group_time_first_half = (
grouped["time_seconds"].where(grouped["swe_seq"] < 30, drop=True).values
)
group_time_first_half_mid = (
group_time_first_half[0] + group_time_first_half[-1]
) // 2

interp_cal_first_half = np.array(
[
float(
np.interp(
int(group_time_first_half_mid),
cal_met,
in_flight_cal_df[cem].to_numpy(),
)
)
for cem in cal_cols
],
dtype=np.float64,
)
group_time_second_half = (
grouped["time_seconds"].where(grouped["swe_seq"] >= 30, drop=True).values
)
group_time_second_half_mid = (
group_time_second_half[0] + group_time_second_half[-1]
) // 2

interp_cal_second_half = np.array(
[
float(
np.interp(
int(group_time_second_half_mid),
cal_met,
in_flight_cal_df[cem].to_numpy(),
)
)
for cem in cal_cols
],
dtype=np.float64,
)

normalized_first_half = normalize_counts(corrected_first_half, latest_cal)
normalized_second_half = normalize_counts(corrected_second_half, latest_cal)
normalized_first_half = normalize_counts(
corrected_first_half, interp_cal_first_half
)
normalized_second_half = normalize_counts(
corrected_second_half, interp_cal_second_half
)

# Sum over the 7 detectors
summed_first_half_cem = np.sum(normalized_first_half, axis=1)
Expand Down Expand Up @@ -562,6 +605,7 @@ def process_swe(accumulated_data: xr.Dataset, in_flight_cal_files: list) -> list
"met_in_utc": met_to_utc(met_first_half).split(".")[0],
"ttj2000ns": int(met_to_ttj2000ns(met_first_half)),
"instrument": "swe",
"swe_epoch": int(met_to_ttj2000ns(group_time_first_half_mid)),
"swe_normalized_counts": [int(val) for val in summed_first],
"swe_counterstreaming_electrons": bde_first_half,
},
Expand All @@ -573,6 +617,7 @@ def process_swe(accumulated_data: xr.Dataset, in_flight_cal_files: list) -> list
"met_in_utc": met_to_utc(met_second_half).split(".")[0],
"ttj2000ns": int(met_to_ttj2000ns(met_second_half)),
"instrument": "swe",
"swe_epoch": int(met_to_ttj2000ns(group_time_second_half_mid)),
"swe_normalized_counts": [int(val) for val in summed_second],
"swe_counterstreaming_electrons": bde_second_half,
},
Expand Down
16 changes: 2 additions & 14 deletions imap_processing/tests/ialirt/unit/test_process_swe.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_process_spacecraft_packet(

swe_product = process_swe(sc_xarray_data, [in_flight_cal_file])

assert len(swe_product[0].keys()) == 7
assert len(swe_product[0].keys()) == 8


def test_get_energy():
Expand Down Expand Up @@ -324,19 +324,7 @@ def test_normalize_counts():
dtype=np.uint8,
)

latest_cal = pd.Series(
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
index=[
"cem1",
"cem2",
"cem3",
"cem4",
"cem5",
"cem6",
"cem7",
], # Simulating real data structure
dtype=np.float64,
)
latest_cal = np.array([2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0])
expected = np.zeros((2, 7, 3), dtype=np.float64)

for i in range(2):
Expand Down