Skip to content

POES

el_paso.recipes.poes.process_poes_meped.process_poes_meped_electron

process_poes_meped_electron

Process POES/MetOp MEPED electron flux data into magnetic-field-resolved data products.

Downloads and extracts the SEM-2 "fluxes-2sec" CDF files for the given POES/MetOp satellite, bins the integral electron flux, energy channels, local pitch angles, and ephemeris onto the given time cadence. The two local pitch angles (0 and 90 degrees relative to the spacecraft) are stacked into a single pitch-angle variable, and the geodetic position is converted to GEO coordinates. T89 magnetic field quantities (B_Calc, B_Eq, MLT_Eq, R_Eq, Alpha_Eq, L_star, L_m) are computed and the resulting variables are saved using a daily LEO saving strategy.

Parameters:

Name Type Description Default
satellite_str poes_satellite_literal

The POES/MetOp satellite to process.

required
raw_data_path str | Path

Directory where the raw downloaded data files are stored.

required
processed_data_path str | Path

Directory where the processed output files are saved.

required
start_time datetime

Start of the time interval to process.

required
end_time datetime

End of the time interval to process.

required
num_cores int

Number of CPU cores used for the magnetic field computations. Defaults to 32.

32
bin_cadence timedelta

Time cadence used to bin the extracted variables. Defaults to timedelta(minutes=5).

timedelta(minutes=5)
Source code in el_paso/recipes/poes/process_poes_meped.py
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def process_poes_meped_electron(
    satellite_str: poes_satellite_literal,
    raw_data_path: str | Path,
    processed_data_path: str | Path,
    start_time: datetime,
    end_time: datetime,
    num_cores: int = 32,
    bin_cadence: timedelta = timedelta(minutes=5),
) -> None:
    """Process POES/MetOp MEPED electron flux data into magnetic-field-resolved data products.

    Downloads and extracts the SEM-2 "fluxes-2sec" CDF files for the given POES/MetOp satellite,
    bins the integral electron flux, energy channels, local pitch angles, and ephemeris onto the
    given time cadence. The two local pitch angles (0 and 90 degrees relative to the spacecraft)
    are stacked into a single pitch-angle variable, and the geodetic position is converted to GEO
    coordinates. T89 magnetic field quantities (B_Calc, B_Eq, MLT_Eq, R_Eq, Alpha_Eq, L_star, L_m)
    are computed and the resulting variables are saved using a daily LEO saving strategy.

    Args:
        satellite_str (poes_satellite_literal): The POES/MetOp satellite to process.
        raw_data_path (str | Path): Directory where the raw downloaded data files are stored.
        processed_data_path (str | Path): Directory where the processed output files are saved.
        start_time (datetime): Start of the time interval to process.
        end_time (datetime): End of the time interval to process.
        num_cores (int, optional): Number of CPU cores used for the magnetic field computations.
            Defaults to 32.
        bin_cadence (timedelta, optional): Time cadence used to bin the extracted variables.
            Defaults to timedelta(minutes=5).
    """
    data_path_stem = f"{raw_data_path}/POES/{satellite_str}/YYYY/MM/"
    url = f"https://spdf.gsfc.nasa.gov/pub/data/noaa/{satellite_str}/sem2_fluxes-2sec/YYYY/"
    file_name_stem = satellite_str + "_poes-sem2_fluxes-2sec_YYYYMMDD_.{3}.cdf"

    ep.download(
        start_time,
        end_time,
        save_path=data_path_stem,
        file_cadence="daily",
        download_url=url,
        file_name_stem=file_name_stem,
    )

    extraction_infos = [
        ep.ExtractionInfo(
            result_key="Epoch",
            name_or_column="Epoch",
            unit=ep.units.tt2000,
        ),
        ep.ExtractionInfo(
            result_key="Energy",
            name_or_column="mep_ele_int_energies",
            unit=u.keV,
            is_time_dependent=False,
        ),
        ep.ExtractionInfo(
            result_key="FEIU",
            name_or_column="mep_ele_flux",
            unit=(u.cm**2 * u.s * u.sr) ** (-1),
        ),
        ep.ExtractionInfo(
            result_key="PA_local_t0",
            name_or_column="meped_alpha_0_sat",
            unit=u.deg,
        ),
        ep.ExtractionInfo(
            result_key="PA_local_t90",
            name_or_column="meped_alpha_90_sat",
            unit=u.deg,
        ),
        ep.ExtractionInfo(
            result_key="alt",
            name_or_column="alt",
            unit=u.km,
        ),
        ep.ExtractionInfo(
            result_key="lon",
            name_or_column="lon",
            unit=u.deg,
        ),
        ep.ExtractionInfo(
            result_key="lat",
            name_or_column="lat",
            unit=u.deg,
        ),
    ]

    variables = ep.extract_variables_from_files(
        start_time,
        end_time,
        file_cadence="daily",
        data_path=data_path_stem,
        file_name_stem=file_name_stem,
        extraction_infos=extraction_infos,
    )

    time_bin_methods = {
        "Energy": ep.TimeBinMethod.Repeat,
        "alt": ep.TimeBinMethod.NanMean,
        "lat": ep.TimeBinMethod.NanMean,
        "lon": ep.TimeBinMethod.NanMean,
        "PA_local_t0": ep.TimeBinMethod.NanMean,
        "PA_local_t90": ep.TimeBinMethod.NanMean,
        "FEIU": ep.TimeBinMethod.NanMedian,
    }

    binned_time_var = ep.processing.bin_by_time(
        variables["Epoch"], variables, time_bin_methods, bin_cadence, start_time=start_time, end_time=end_time
    )

    variables["FEIU"].apply_thresholds_on_data(lower_threshold=0)
    variables["FEIU"].transpose_data((0, 2, 1))

    # stack pitch angles
    pa_arr = np.stack((variables["PA_local_t0"].get_data(u.deg), variables["PA_local_t90"].get_data(u.deg))).T.astype(
        np.float64
    )
    variables["PA_local"] = ep.Variable(data=pa_arr, original_unit=u.deg)

    del variables["PA_local_t0"]
    del variables["PA_local_t90"]

    xGDZ_arr = np.stack(
        (variables["alt"].get_data(), variables["lat"].get_data(), variables["lon"].get_data())
    ).T.astype(np.float64)
    model_coord = ep.processing.magnetic_field_utils.Coords()

    # convert time_array to datetimes for transform function
    time_var_datetime = [
        datetime.fromtimestamp(t, tz=timezone.utc) for t in binned_time_var.get_data(ep.units.posixtime)
    ]
    xgeo_arr = model_coord.transform(time_var_datetime, xGDZ_arr, ep.IRBEM_SYSAXIS_GDZ, ep.IRBEM_SYSAXIS_GEO)
    variables["xGEO"] = ep.Variable(data=xgeo_arr, original_unit=ep.units.RE)

    del variables["alt"]
    del variables["lon"]
    del variables["lat"]

    variables_to_compute: ep.processing.VariableRequest = [
        ("B_Calc", "T89"),
        ("B_Eq", "T89"),
        ("MLT_Eq", "T89"),
        ("B_Eq", "T89"),
        ("R_Eq", "T89"),
        ("Alpha_Eq", "T89"),
        ("L_star", "T89"),
        ("L_m", "T89"),
    ]

    magnetic_field_variables = ep.processing.compute_magnetic_field_variables(
        time_var=binned_time_var,
        xgeo_var=variables["xGEO"],
        energy_var=variables["Energy"],
        pa_local_var=variables["PA_local"],
        particle_species="electron",
        variables_to_compute=variables_to_compute,
        irbem_options=ep.processing.magnetic_field_utils.IrbemOptions(),
        num_cores=num_cores,
    )

    variables |= magnetic_field_variables

    variables_to_save = {
        "Epoch": binned_time_var,
        "FEIU": variables["FEIU"],
        "Energy_FEIU": variables["Energy"],
        "Alpha": variables["PA_local"],
        "Alpha_Eq": magnetic_field_variables["Alpha_Eq_T89"],
        "R_Eq": magnetic_field_variables["R_Eq_T89"],
        "MLT": magnetic_field_variables["MLT_Eq_T89"],
        "B_Calc": magnetic_field_variables["B_Calc_T89"],
        "B_Eq": magnetic_field_variables["B_Eq_T89"],
        "Position": variables["xGEO"],
    }

    saving_strategy = ep.saving_strategies.DailyLEORBStrategy(
        base_data_path=Path(processed_data_path),
        mission="POES",
        satellite=satellite_str,
        instrument="MEPED",
        mag_field="T89",
        data_standard=ep.data_standards.GFZStandard(),
    )

    ep.save(variables_to_save, saving_strategy, start_time, end_time, time_var=binned_time_var)  # ty:ignore[invalid-argument-type]

el_paso.recipes.poes.process_poes_ted.process_poes_ted_electron

process_poes_ted_electron

Process POES/MetOp TED electron flux data into magnetic-field-resolved data products.

Downloads and extracts the SEM-2 "fluxes-2sec" CDF files for the given POES/MetOp satellite, bins the differential electron flux, energy channels, local pitch angles, and ephemeris onto the given time cadence. The two local pitch angles (0 and 30 degrees relative to the spacecraft) are stacked into a single pitch-angle variable and folded into the 0-90 degree range, and the geodetic position is converted to GEO and spherical GEO coordinates. T89 magnetic field quantities (B_Calc, MLT, B_Eq, R_Eq, Alpha_Eq, Alpha_LC, Alpha_LC_Eq, and optionally L_star and L_m) are computed and the resulting variables are saved using a daily LEO saving strategy.

Parameters:

Name Type Description Default
satellite_str poes_satellite_literal

The POES/MetOp satellite to process.

required
raw_data_path str | Path

Directory where the raw downloaded data files are stored.

required
processed_data_path str | Path

Directory where the processed output files are saved.

required
start_time datetime

Start of the time interval to process.

required
end_time datetime

End of the time interval to process.

required
num_cores int

Number of CPU cores used for the magnetic field computations. Defaults to 32.

32
bin_cadence timedelta

Time cadence used to bin the extracted variables. Defaults to timedelta(minutes=5).

timedelta(minutes=5)
calculate_Lm_Lstar bool

If True, additionally compute and save the L_m and L_star magnetic field quantities. Defaults to False.

False
Source code in el_paso/recipes/poes/process_poes_ted.py
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def process_poes_ted_electron(
    satellite_str: poes_satellite_literal,
    raw_data_path: str | Path,
    processed_data_path: str | Path,
    start_time: datetime,
    end_time: datetime,
    num_cores: int = 32,
    bin_cadence: timedelta = timedelta(minutes=5),
    *,
    calculate_Lm_Lstar: bool = False,
) -> None:
    """Process POES/MetOp TED electron flux data into magnetic-field-resolved data products.

    Downloads and extracts the SEM-2 "fluxes-2sec" CDF files for the given POES/MetOp satellite,
    bins the differential electron flux, energy channels, local pitch angles, and ephemeris onto
    the given time cadence. The two local pitch angles (0 and 30 degrees relative to the
    spacecraft) are stacked into a single pitch-angle variable and folded into the 0-90 degree
    range, and the geodetic position is converted to GEO and spherical GEO coordinates. T89
    magnetic field quantities (B_Calc, MLT, B_Eq, R_Eq, Alpha_Eq, Alpha_LC, Alpha_LC_Eq, and
    optionally L_star and L_m) are computed and the resulting variables are saved using a daily
    LEO saving strategy.

    Args:
        satellite_str (poes_satellite_literal): The POES/MetOp satellite to process.
        raw_data_path (str | Path): Directory where the raw downloaded data files are stored.
        processed_data_path (str | Path): Directory where the processed output files are saved.
        start_time (datetime): Start of the time interval to process.
        end_time (datetime): End of the time interval to process.
        num_cores (int, optional): Number of CPU cores used for the magnetic field computations.
            Defaults to 32.
        bin_cadence (timedelta, optional): Time cadence used to bin the extracted variables.
            Defaults to timedelta(minutes=5).
        calculate_Lm_Lstar (bool, optional): If True, additionally compute and save the L_m and
            L_star magnetic field quantities. Defaults to False.
    """
    data_path_stem = f"{raw_data_path}/POES/{satellite_str}/YYYY/MM/"
    url = f"https://spdf.gsfc.nasa.gov/pub/data/noaa/{satellite_str}/sem2_fluxes-2sec/YYYY/"
    file_name_stem = satellite_str + "_poes-sem2_fluxes-2sec_YYYYMMDD_.{3}.cdf"

    ep.download(
        start_time,
        end_time,
        save_path=data_path_stem,
        file_cadence="daily",
        download_url=url,
        file_name_stem=file_name_stem,
    )

    extraction_infos = [
        ep.ExtractionInfo(
            result_key="Epoch",
            name_or_column="Epoch",
            unit=ep.units.tt2000,
        ),
        ep.ExtractionInfo(
            result_key="Energy",
            name_or_column="ted_ele_diff_energies",
            unit=u.eV,
            is_time_dependent=False,
        ),
        ep.ExtractionInfo(
            result_key="FEDU",
            name_or_column="ted_ele_flux",
            unit=(u.cm**2 * u.s * u.sr * u.eV) ** (-1),
        ),
        ep.ExtractionInfo(
            result_key="PA_local_t0",
            name_or_column="ted_alpha_0_sat",
            unit=u.deg,
        ),
        ep.ExtractionInfo(
            result_key="PA_local_t30",
            name_or_column="ted_alpha_30_sat",
            unit=u.deg,
        ),
        ep.ExtractionInfo(
            result_key="alt",
            name_or_column="alt",
            unit=u.km,
        ),
        ep.ExtractionInfo(
            result_key="lon",
            name_or_column="lon",
            unit=u.deg,
        ),
        ep.ExtractionInfo(
            result_key="lat",
            name_or_column="lat",
            unit=u.deg,
        ),
    ]

    variables = ep.extract_variables_from_files(
        start_time,
        end_time,
        file_cadence="daily",
        data_path=data_path_stem,
        file_name_stem=file_name_stem,
        extraction_infos=extraction_infos,
    )

    variables["FEDU"].apply_thresholds_on_data(lower_threshold=0)

    time_bin_methods = {
        "Energy": ep.TimeBinMethod.Repeat,
        "alt": ep.TimeBinMethod.NanMean,
        "lat": ep.TimeBinMethod.NanMean,
        "lon": ep.TimeBinMethod.NanMean,
        "PA_local_t0": ep.TimeBinMethod.NanMean,
        "PA_local_t30": ep.TimeBinMethod.NanMean,
        "FEDU": ep.TimeBinMethod.NanMean,
    }

    binned_time_var = ep.processing.bin_by_time(
        variables["Epoch"], variables, time_bin_methods, bin_cadence, start_time=start_time, end_time=end_time
    )

    variables["FEDU"].transpose_data((0, 2, 1))
    # stack pitch angles
    pa_arr = np.stack((variables["PA_local_t0"].get_data(u.deg), variables["PA_local_t30"].get_data(u.deg))).T.astype(
        np.float64
    )
    pa_arr = np.where(pa_arr > 90, 180 - pa_arr, pa_arr)

    variables["PA_local"] = ep.Variable(data=pa_arr, original_unit=u.deg)

    del variables["PA_local_t0"], variables["PA_local_t30"]

    xGDZ_arr = np.stack(
        (variables["alt"].get_data(), variables["lat"].get_data(), variables["lon"].get_data())
    ).T.astype(np.float64)
    model_coord = ep.processing.magnetic_field_utils.Coords()

    # convert time_array to datetimes for transform function
    time_var_datetime = [
        datetime.fromtimestamp(t, tz=timezone.utc) for t in binned_time_var.get_data(ep.units.posixtime)
    ]
    xgeo_arr = model_coord.transform(time_var_datetime, xGDZ_arr, ep.IRBEM_SYSAXIS_GDZ, ep.IRBEM_SYSAXIS_GEO)
    variables["xGEO"] = ep.Variable(data=xgeo_arr, original_unit=ep.units.RE)

    geo_sph_arr = model_coord.transform(time_var_datetime, xGDZ_arr, ep.IRBEM_SYSAXIS_GDZ, ep.IRBEM_SYSAXIS_SPH)
    variables["geo_alt"] = ep.Variable(data=geo_sph_arr[:, 0], original_unit=ep.units.RE)
    variables["geo_lat"] = ep.Variable(data=geo_sph_arr[:, 1], original_unit=u.deg)
    variables["geo_lon"] = ep.Variable(data=geo_sph_arr[:, 2], original_unit=u.deg)

    variables_to_compute: ep.processing.VariableRequest = [
        ("B_Calc", "T89"),
        ("MLT", "T89"),
        ("B_Eq", "T89"),
        ("R_Eq", "T89"),
        ("Alpha_Eq", "T89"),
        ("Alpha_LC", "T89"),
        ("Alpha_LC_Eq", "T89"),
    ]

    if calculate_Lm_Lstar:
        variables_to_compute.extend([("L_star", "T89"), ("L_m", "T89")])  # ty:ignore[invalid-argument-type]

    magnetic_field_variables = ep.processing.compute_magnetic_field_variables(
        time_var=binned_time_var,
        xgeo_var=variables["xGEO"],
        energy_var=variables["Energy"],
        pa_local_var=variables["PA_local"],
        particle_species="electron",
        variables_to_compute=variables_to_compute,
        irbem_options=ep.processing.magnetic_field_utils.IrbemOptions(),
        num_cores=num_cores,
    )

    variables |= magnetic_field_variables

    variables_to_save = {
        "Epoch": binned_time_var,
        "FEDU": variables["FEDU"],
        "Energy_FEDU": variables["Energy"],
        "Alpha": variables["PA_local"],
        "Alpha_Eq": magnetic_field_variables["Alpha_Eq_T89"],
        "R_Eq": magnetic_field_variables["R_Eq_T89"],
        "MLT": magnetic_field_variables["MLT_T89"],
        "B_Calc": magnetic_field_variables["B_Calc_T89"],
        "B_Eq": magnetic_field_variables["B_Eq_T89"],
        "Position": variables["xGEO"],
        "Position_geo_alt": variables["geo_alt"],
        "Position_geo_lat": variables["geo_lat"],
        "Position_geo_lon": variables["geo_lon"],
        "Alpha_LC": magnetic_field_variables["Alpha_LC_T89"],
        "Alpha_LC_Eq": magnetic_field_variables["Alpha_LC_Eq_T89"],
    }

    if calculate_Lm_Lstar:
        variables_to_save |= {
            "L_m": magnetic_field_variables["L_m_T89"],
            "L_star": magnetic_field_variables["L_star_T89"],
        }

    saving_strategy = ep.saving_strategies.DailyLEORBStrategy(
        base_data_path=Path(processed_data_path),
        mission="POES",
        satellite=satellite_str,
        instrument="TED",
        mag_field="T89",
        data_standard=ep.data_standards.GFZStandard(),
    )

    ep.save(variables_to_save, saving_strategy, start_time, end_time, time_var=binned_time_var)  # ty:ignore[invalid-argument-type]