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
19 changes: 14 additions & 5 deletions el_paso/processing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
#
# SPDX-License-Identifier: Apache-2.0

from el_paso.processing import magnetic_field_utils
from el_paso.processing.bin_by_time import TimeBinMethod, bin_by_time
from el_paso.processing.compute_equatorial_plasmaspheric_density import compute_equatorial_plasmaspheric_density
from el_paso.processing.compute_equatorial_plasmaspheric_density import (
compute_equatorial_plasmaspheric_density,
)
from el_paso.processing.compute_invariant_K import compute_invariant_K
from el_paso.processing.compute_invariant_mu import compute_invariant_mu
from el_paso.processing.compute_magnetic_field_variables import VariableRequest, compute_magnetic_field_variables
from el_paso.processing.compute_magnetic_field_variables import (
VariableRequest,
compute_magnetic_field_variables,
)
from el_paso.processing.compute_phase_space_density import compute_phase_space_density
from el_paso.processing.construct_pitch_angle_distribution import construct_pitch_angle_distribution
from el_paso.processing.construct_pitch_angle_distribution import (
construct_pitch_angle_distribution,
)
from el_paso.processing.convert_string_to_datetime import convert_string_to_datetime
from el_paso.processing.create_quality_flag_from_magnetometer import (
magnetometer_quality_flags,
)
from el_paso.processing.fold_pitch_angles_and_flux import fold_pitch_angles_and_flux
from el_paso.processing.get_real_time_tipsod import get_real_time_tipsod
from el_paso.processing.magnetic_field_utils import MagFieldVarTypes
Expand All @@ -32,5 +41,5 @@
"convert_string_to_datetime",
"fold_pitch_angles_and_flux",
"get_real_time_tipsod",
"magnetic_field_utils",
"magnetometer_quality_flags",
]
44 changes: 44 additions & 0 deletions el_paso/processing/create_quality_flag_from_magnetometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2026 GFZ Helmholtz Centre for Geosciences
# SPDX-FileContributor: Alwin Roy
#
# SPDX-License-Identifier: Apache-2.0

import numpy as np
from astropy import units as u

import el_paso as ep


def magnetometer_quality_flags(variables: dict, threshold: int = 500) -> np.ndarray:
"""Compute a quality mask for magnetometer data using Bt.

The mask flags samples that are considered valid based on two criteria:
1. Magnetic field magnitude (Bt) must be positive.
2. Consecutive differences |ΔBt| must be below the spike threshold.

Args:
variables (dict): Dictionary containing variable objects. Must include
the key "Bt". Each variable must implement ``get_data()``.
threshold(int, optional): Spike detection threshold in nT.
Defaults to 500.

Returns:
np.ndarray: Boolean array where ``True`` indicates valid data and
``False`` indicates flagged samples.
"""
bt_var = variables["Bt"]
bt = bt_var.get_data(u.nT)

mask_positive = bt > 0

delta_b = np.diff(bt, prepend=bt[0])
mask_spike = np.abs(delta_b) < threshold

mask = mask_positive & mask_spike

mask = ep.Variable(
data=mask_positive & mask_spike,
original_unit=u.dimensionless_unscaled,
)

return mask
3 changes: 3 additions & 0 deletions scripts/inspect_cdf_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def inspect_cdf_file(file_path: str) -> None:
for var in variable_names:
var_attrs_full = cdf_file.varattsget(var)
vdr_info = cdf_file.varinq(var)
if vdr_info.Last_Rec < 0:
print(f"{var}: no records")
continue
var_data = cdf_file.varget(var)

var_shape = var_data.shape # type: ignore[reportAttributeAccessIssue]
Expand Down
83 changes: 14 additions & 69 deletions tutorials/2_variable_class.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,10 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "941c7d1f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[91mLogger not instantiated.\u001b[0m\n",
"\u001b[91mBasic logger can be instantiated using `logging.basicConfig(level=logging.INFO)`\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'Epoch': Variable holding (1000,) data points with metadata: VariableMetadata(unit=Unit(dimensionless), original_cadence_seconds=0, source_files=['example_orbit.csv'], description='', processing_notes='', standard_name=''),\n",
" 'alt': Variable holding (1000,) data points with metadata: VariableMetadata(unit=Unit(\"km\"), original_cadence_seconds=0, source_files=['example_orbit.csv'], description='', processing_notes='', standard_name=''),\n",
" 'lon': Variable holding (1000,) data points with metadata: VariableMetadata(unit=Unit(\"km\"), original_cadence_seconds=0, source_files=['example_orbit.csv'], description='', processing_notes='', standard_name=''),\n",
" 'lat': Variable holding (1000,) data points with metadata: VariableMetadata(unit=Unit(\"km\"), original_cadence_seconds=0, source_files=['example_orbit.csv'], description='', processing_notes='', standard_name='')}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"from datetime import datetime, timezone\n",
"\n",
Expand Down Expand Up @@ -97,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "5ba56b19",
"metadata": {},
"outputs": [],
Expand All @@ -115,7 +93,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "fdef5a25",
"metadata": {},
"outputs": [],
Expand All @@ -136,7 +114,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "a762b8f5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -166,7 +144,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "929bab50",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -222,18 +200,10 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "fb2b1708",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[datetime.datetime(2017, 7, 30, 0, 0, 11, 507000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 0, 34, 203000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 0, 56, 899000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 1, 19, 596000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 1, 30, 945000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 1, 42, 293000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 2, 4, 990000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 2, 27, 685000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 2, 39, 34000, tzinfo=datetime.timezone.utc), datetime.datetime(2017, 7, 30, 0, 2, 50, 382000, tzinfo=datetime.timezone.utc)]\n"
]
}
],
"outputs": [],
"source": [
"variables[\"Epoch\"].convert_to_unit(ep.units.posixtime)\n",
"times = [datetime.fromtimestamp(timestamp, timezone.utc) for timestamp in variables[\"Epoch\"].get_data()]\n",
Expand All @@ -250,23 +220,10 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"id": "25916994",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Energies in eV: [14.98455 16.81365 18.8538 21.17535 23.707949 26.592299 29.828398\n",
" 33.486603 37.566902 42.13965 ]\n",
"Energies in MeV using get_data: [1.4984550e-05 1.6813650e-05 1.8853800e-05 2.1175350e-05 2.3707949e-05\n",
" 2.6592299e-05 2.9828398e-05 3.3486602e-05 3.7566901e-05 4.2139647e-05]\n",
"Energies in MeV after converting: [1.4984550e-05 1.6813650e-05 1.8853800e-05 2.1175350e-05 2.3707949e-05\n",
" 2.6592299e-05 2.9828398e-05 3.3486602e-05 3.7566901e-05 4.2139647e-05]\n"
]
}
],
"outputs": [],
"source": [
"print(\"Energies in eV:\", variables[\"Energy_FEDU\"].get_data()[0, 0:10])\n",
"print(\"Energies in MeV using get_data:\", variables[\"Energy_FEDU\"].get_data(\"MeV\")[0, 0:10])\n",
Expand All @@ -285,22 +242,10 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "1ec55e23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2.99691001e-05 3.36273006e-05 3.77075994e-05 4.23507008e-05\n",
" 4.74158987e-05 5.31845981e-05 5.96567952e-05 6.69732035e-05\n",
" 7.51338011e-05 8.42792942e-05]\n",
"[0.01498455 0.01681365 0.0188538 0.02117535 0.02370795 0.0265923\n",
" 0.0298284 0.0334866 0.0375669 0.04213965]\n"
]
}
],
"outputs": [],
"source": [
"old_data = variables[\"Energy_FEDU\"].get_data().astype(np.float64)\n",
"\n",
Expand All @@ -316,7 +261,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "verb4d",
"display_name": "venv",
"language": "python",
"name": "python3"
},
Expand All @@ -330,7 +275,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.13"
"version": "3.12.10"
}
},
"nbformat": 4,
Expand Down
Loading
Loading