Skip to content

Commit

Permalink
add degap but set it false by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashuwu89 committed Sep 10, 2023
1 parent 31e59bc commit 3f45bbb
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
46 changes: 42 additions & 4 deletions pyspedas/elfin/epd/calibration_l2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from pytplot import get_data, store_data, options
from pytplot.tplot_math import degap
import numpy as np
import bisect

Expand Down Expand Up @@ -95,6 +96,7 @@ def epd_l2_Espectra(
LC_tvar,
LCfatol=None,
LCfptol=None,
nodegap=True,
):

"""
Expand All @@ -114,6 +116,9 @@ def epd_l2_Espectra(
Tolerance angle for perp flux. A negative value means a wider angle for
perp flux.
Default is -11 deg.
Nodegap: bool, optional
Flag for degap. If set, skip degap.
Default is False.
Return
----------
Expand All @@ -139,8 +144,8 @@ def epd_l2_Espectra(
FOVo2 = 11. # Field of View divided by 2 (deg)
dphsect = 360./nspinsectors
SectWidtho2 = dphsect/2.
LCfatol = FOVo2 + SectWidtho2 # tolerance of pitch angle in field aligned direction, default 22.25 deg
LCfptol = -FOVo2 # tolerance of pitch angel in perpendicular direction, default -11. deg
LCfatol = FOVo2 + SectWidtho2 if LCfatol is None else LCfatol # tolerance of pitch angle in field aligned direction, default 22.25 deg
LCfptol = -FOVo2 if LCfptol is None else LCfptol # tolerance of pitch angel in perpendicular direction, default -11. deg
# TODO: make sure these default values are correct

# calculate domega in PA
Expand Down Expand Up @@ -219,6 +224,23 @@ def epd_l2_Espectra(
store_data(perp_var, data={'x': data.times, 'y': Espectra_perp, 'v': energy}, attr_dict=get_data(flux_tvar, metadata=True))
epd_l2_Espectra_option(perp_var)

#===========================
# DEGAP
#===========================
if nodegap is False:
nspinsinsum = get_data(f"{flux_tvar[0:3]}_pef_nspinsinsum")
spinper = get_data(f"{flux_tvar[0:3]}_pef_tspin")
if np.average(nspinsinsum.y) > 1 :
mydt = np.max(nspinsinsum.y)*np.median(spinper.y)
else:
mydt = np.median(spinper.y)
breakpoint()
degap(omni_var, dt=mydt, margin=0.5*mydt/2)
degap(para_var, dt=mydt, margin=0.5*mydt/2)
degap(anti_var, dt=mydt, margin=0.5*mydt/2)
degap(perp_var, dt=mydt, margin=0.5*mydt/2)


return [omni_var, para_var, anti_var, perp_var]


Expand Down Expand Up @@ -272,6 +294,7 @@ def epd_l2_PAspectra(
flux_tvar,
energybins = None,
energies = None,
nodegap=True,
):
"""
This function processes a 3D energy-time spectra from ELF EPD L2 data
Expand Down Expand Up @@ -315,7 +338,9 @@ def epd_l2_PAspectra(
13 3350-4150 3728.6
14 4150-5800 4906.1
15 5800+ 6500.0
Nodegap: bool, optional
Flag for degap. If set, skip degap.
Default is False.
Return
----------
Expand Down Expand Up @@ -383,6 +408,19 @@ def epd_l2_PAspectra(

store_data(PA_var, data={'x': data.times, 'y': PAspectra_single, 'v': pas2plot})
epd_l2_PAspectra_option(PA_var)
PA_tvars.append(PA_var)

#===========================
# DEGAP
#===========================
if nodegap is False:
nspinsinsum = get_data(f"{flux_tvar[0:3]}_pef_nspinsinsum")
spinper = get_data(f"{flux_tvar[0:3]}_pef_tspin")
if np.average(nspinsinsum.y) > 1 :
mydt = np.max(nspinsinsum.y)*np.median(spinper.y)
else:
mydt = np.median(spinper.y)
degap(PA_var, dt=mydt, margin=0.5*mydt/2)
PA_tvars.append(PA_var)


return PA_tvars
28 changes: 25 additions & 3 deletions pyspedas/elfin/epd/epd.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def elfin_load_epd(trange=['2020-11-01', '2020-11-02'],
fullspin=False,
PAspec_energies=None,
PAspec_energybins=None,

Espec_LCfatol=None,
Espec_LCfptol=None,
):
"""
This function loads data from the Energetic Particle Detector (EPD)
Expand Down Expand Up @@ -115,6 +116,16 @@ def elfin_load_epd(trange=['2020-11-01', '2020-11-02'],
14 4150-5800 4906.1
15 5800+ 6500.0
Espec_LCfatol: float, optional
Tolerance angle for para and anti flux. A positive value makes the loss
cone/antiloss cone smaller by this amount.
Default is 22.25 deg.
Espec_LCfptol: float, optional
Tolerance angle for perp flux. A negative value means a wider angle for
perp flux.
Default is -11 deg.
Returns
----------
List of tplot variables created.
Expand All @@ -139,8 +150,10 @@ def elfin_load_epd(trange=['2020-11-01', '2020-11-02'],
type_ = "nflux"

if level == "l1":
return epd_l1_postprocessing(tvars, trange=trange, type_=type_, nspinsinsum=nspinsinsum,
tvars = epd_l1_postprocessing(tvars, trange=trange, type_=type_, nspinsinsum=nspinsinsum,
unit=CALIBRATED_TYPE_UNITS[type_])
return tvars

elif level == "l2":
logging.info("ELFIN EPD L2: START PROCESSING.")
# check whether input type is allowed
Expand All @@ -149,7 +162,16 @@ def elfin_load_epd(trange=['2020-11-01', '2020-11-02'],
type_ = "nflux"

res = 'hs' if fullspin is False else 'fs'
return epd_l2_postprocessing(tvars, fluxtype=type_, res=res, PAspec_energies=PAspec_energies, PAspec_energybins=PAspec_energybins)
tvars = epd_l2_postprocessing(
tvars,
fluxtype=type_,
res=res,
PAspec_energies=PAspec_energies,
PAspec_energybins=PAspec_energybins,
Espec_LCfatol=Espec_LCfatol,
Espec_LCfptol=Espec_LCfptol,)

return tvars
else:
raise ValueError(f"Unknown level: {level}")

Expand Down
14 changes: 13 additions & 1 deletion pyspedas/elfin/epd/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def epd_l2_postprocessing(
datatype='e',
PAspec_energies = None,
PAspec_energybins = None,
Espec_LCfatol = None,
Espec_LCfptol = None,
):
"""
Process ELF EPD L2 data and generate omni, para, anti, perp flux spectra.
Expand Down Expand Up @@ -156,6 +158,16 @@ def epd_l2_postprocessing(
14 4150-5800 4906.1
15 5800+ 6500.0
Espec_LCfatol: float, optional
Tolerance angle for para and anti flux. A positive value makes the loss
cone/antiloss cone smaller by this amount.
Default is 22.25 deg.
Espec_LCfptol: float, optional
Tolerance angle for perp flux. A negative value means a wider angle for
perp flux.
Default is -11 deg.
Returns
----------
List of tplot variables created.
Expand All @@ -176,7 +188,7 @@ def epd_l2_postprocessing(

logging.info("ELFIN EPD L2: START ENERGY SPECTOGRAM.")
# get energy spectra in four directions
tvars = epd_l2_Espectra(flux_tname[0], LC_tname[0])
tvars = epd_l2_Espectra(flux_tname[0], LC_tname[0], LCfatol=Espec_LCfatol, LCfptol=Espec_LCfptol)

logging.info("ELFIN EPD L2: START PITCH ANGLE SPECTOGRAM.")
# get pitch angle spectra
Expand Down
24 changes: 17 additions & 7 deletions pyspedas/elfin/tests/test_epd_l2_spectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def setUpClass(cls):
#cls.t = ['2022-08-03/08:30:00','2022-08-03/09:00:00']
#cls.probe = 'a'
#cls.t = ['2022-04-12/19:00:00','2022-04-12/19:15:00']
cls.t = ['2022-04-13/01:28:00','2022-04-13/01:35:00']
#cls.t = ['2022-04-13/01:28:00','2022-04-13/01:35:00']
#cls.t = ['2021-10-10/09:50:00','2021-10-10/10:10:00'] # elb with gap
cls.t = ['2021-10-12/23:00:00','2021-10-12/23:10:00'] # elb with gap
#cls.t = ['2022-04-01/09:45:00','2022-04-01/10:10:00'] # elb with inner belt
cls.probe = 'b'
#cls.t = ['2022-08-28/15:54','2022-08-28/16:15']
#cls.probe = 'a'
Expand Down Expand Up @@ -150,7 +153,7 @@ def test_epd_l2_hs_nflux(self):
elf_pef_Et_nflux = pytplot.get_data(f"el{self.probe}_pef_Et_nflux")
elf_pef_pa = pytplot.get_data(f"el{self.probe}_pef_pa")

assert_allclose(elf_pef_hs_Epat_nflux.v1, self.elf_pef_hs_Epat_nflux_ch1.v[0:251,:], rtol=1e-02)
assert_allclose(elf_pef_hs_Epat_nflux.v1, self.elf_pef_hs_Epat_nflux_ch1.v, rtol=0.1)
assert_allclose(elf_pef_hs_Epat_nflux.y[:,:,0], self.elf_pef_hs_Epat_nflux_ch0.y, rtol=1e-02)
assert_allclose(elf_pef_hs_Epat_nflux.y[:,:,1], self.elf_pef_hs_Epat_nflux_ch1.y, rtol=1e-02)
assert_allclose(elf_pef_hs_LCdeg.y, self.elf_pef_hs_LCdeg.y, rtol=1e-02)
Expand Down Expand Up @@ -179,7 +182,14 @@ def test_epd_l2_hs_nflux(self):

def test_epd_l2_hs_eflux(self):
"""Validate epd l2 halfspin eflux spectogram"""
pyspedas.elfin.epd(trange=self.t, probe=self.probe, level='l2',no_update=True, type_='eflux')
pyspedas.elfin.epd(
trange=self.t,
probe=self.probe,
level='l2',
no_update=True,
type_='eflux',
Espec_LCfatol=40,
Espec_LCfptol=5,)
elf_pef_hs_eflux_ch0 = pytplot.get_data(f"el{self.probe}_pef_hs_eflux_ch0")
elf_pef_hs_eflux_ch1 = pytplot.get_data(f"el{self.probe}_pef_hs_eflux_ch1")
elf_pef_hs_eflux_ch2 = pytplot.get_data(f"el{self.probe}_pef_hs_eflux_ch2")
Expand All @@ -191,7 +201,7 @@ def test_epd_l2_hs_eflux(self):
elf_pef_hs_Epat_eflux = pytplot.get_data(f"el{self.probe}_pef_hs_Epat_eflux")
elf_pef_Et_eflux = pytplot.get_data(f"el{self.probe}_pef_Et_eflux")

assert_allclose(elf_pef_hs_Epat_eflux.v1, self.elf_pef_hs_Epat_eflux_ch1.v, rtol=1e-02)
assert_allclose(elf_pef_hs_Epat_eflux.v1, self.elf_pef_hs_Epat_eflux_ch1.v, rtol=0.1)
assert_allclose(elf_pef_hs_Epat_eflux.y[:,:,0], self.elf_pef_hs_Epat_eflux_ch0.y, rtol=1e-02)
assert_allclose(elf_pef_hs_Epat_eflux.y[:,:,1], self.elf_pef_hs_Epat_eflux_ch1.y, rtol=1e-02)
assert_allclose(elf_pef_Et_eflux.y, self.elf_pef_Et_eflux.y, rtol=1e-02)
Expand Down Expand Up @@ -235,7 +245,7 @@ def test_epd_l2_fs_nflux(self):
elf_pef_fs_LCdeg = pytplot.get_data(f"el{self.probe}_pef_fs_LCdeg")
elf_pef_fs_Epat_nflux = pytplot.get_data(f"el{self.probe}_pef_fs_Epat_nflux")

assert_allclose(elf_pef_fs_Epat_nflux.v1, self.elf_pef_fs_Epat_nflux_ch1.v, rtol=1e-02)
assert_allclose(elf_pef_fs_Epat_nflux.v1, self.elf_pef_fs_Epat_nflux_ch1.v, rtol=0.1)
assert_allclose(elf_pef_fs_Epat_nflux.y[:,:,0], self.elf_pef_fs_Epat_nflux_ch0.y, rtol=1e-02)
assert_allclose(elf_pef_fs_Epat_nflux.y[:,:,1], self.elf_pef_fs_Epat_nflux_ch1.y, rtol=1e-02)
assert_allclose(elf_pef_fs_LCdeg.y, self.elf_pef_fs_LCdeg.y, rtol=1e-02)
Expand Down Expand Up @@ -273,7 +283,7 @@ def test_epd_l2_fs_eflux(self):
elf_pef_fs_eflux_omni = pytplot.get_data(f"el{self.probe}_pef_fs_eflux_omni")
elf_pef_fs_Epat_eflux = pytplot.get_data(f"el{self.probe}_pef_fs_Epat_eflux")

assert_allclose(elf_pef_fs_Epat_eflux.v1, self.elf_pef_fs_Epat_eflux_ch1.v, rtol=1e-02)
assert_allclose(elf_pef_fs_Epat_eflux.v1, self.elf_pef_fs_Epat_eflux_ch1.v, rtol=0.1)
assert_allclose(elf_pef_fs_Epat_eflux.y[:,:,0], self.elf_pef_fs_Epat_eflux_ch0.y, rtol=1e-02)
assert_allclose(elf_pef_fs_Epat_eflux.y[:,:,1], self.elf_pef_fs_Epat_eflux_ch1.y, rtol=1e-02)
assert_allclose(elf_pef_fs_eflux_omni.y, self.elf_pef_fs_eflux_omni.y, rtol=1e-02)
Expand All @@ -288,7 +298,7 @@ def test_epd_l2_fs_eflux(self):
assert_allclose(elf_pef_fs_eflux_ch1.y, spec2plot, rtol=1e-02)

logging.info("FULLSPIN EFLUX DATA TEST FINISHED.")


if __name__ == '__main__':
unittest.main()

0 comments on commit 3f45bbb

Please sign in to comment.