Skip to content
Merged
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
2 changes: 2 additions & 0 deletions docs/swapi/figure_src/figure_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
REPO_ROOT = Path(__file__).resolve().parents[3]
sys.path.insert(0, str(REPO_ROOT))

# ruff: disable[E402]
from imap_l3_processing.constants import (
METERS_PER_KILOMETER,
PROTON_CHARGE_COULOMBS,
PROTON_MASS_KG,
)
from imap_l3_processing.swapi.constants import SWAPI_K_FACTOR
from imap_l3_processing.swapi.response.swapi_response import SwapiResponse
# ruff: enable[E402]

FIGURES_DIR = REPO_ROOT / "docs" / "swapi" / "figures"

Expand Down
2 changes: 2 additions & 0 deletions docs/swapi/figure_src/plot_pui_cutoff_staircase.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
REPO_ROOT = Path(__file__).resolve().parents[3]
sys.path.insert(0, str(REPO_ROOT))

# ruff: disable[E402]
import matplotlib

matplotlib.use("Agg")
Expand Down Expand Up @@ -59,6 +60,7 @@
from imap_l3_processing.swapi.l3a.utils import (
velocity_components_to_angles_in_instrument_frame,
)
# ruff: enable[E402]

_SW_SPEED_KMS = 450.0
_SW_AZIMUTH_DEG = 0.0
Expand Down
2 changes: 2 additions & 0 deletions docs/swapi/figure_src/plot_pui_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
REPO_ROOT = Path(__file__).resolve().parents[3]
sys.path.insert(0, str(REPO_ROOT))

# ruff: disable[E402]
import matplotlib

matplotlib.use("Agg")
Expand All @@ -38,6 +39,7 @@
FittingParameters,
VasyliunasSiscoeDistribution,
)
# ruff: enable[E402]

_CUTOFF_SPEED_KMS = 450.0
_SW_SPEED_INERTIAL_KMS = 450.0
Expand Down
2 changes: 2 additions & 0 deletions docs/swapi/figure_src/plot_pui_reference_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
REPO_ROOT = Path(__file__).resolve().parents[3]
sys.path.insert(0, str(REPO_ROOT))

# ruff: disable[E402]
import matplotlib

matplotlib.use("Agg")
Expand All @@ -30,6 +31,7 @@

from figure_utils import FIGURES_DIR
from imap_l3_processing.constants import HE_PUI_PARTICLE_MASS_PER_CHARGE_M_P_PER_E
# ruff: enable[E402]

_REFERENCE_CSV_PATH = (
REPO_ROOT / "tests" / "test_data" / "swapi" / "pui_count_rate_reference.csv"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def deserialize(self, reader):

# additional padding when odd number of bins?
if self.data["number_of_bins_per_histogram"] % 2 == 1:
aux = reader.read_uint8()
_ = reader.read_uint8()

return self.data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ def _save_data_to_json_file(self):
data = copy.deepcopy(self.data)

for key in fmt["arrays"].keys():
if (
type(data[key]) == np.ndarray
if isinstance(
data[key], np.ndarray
): # check if we have more than one direct event
data[key] = (
"["
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _check_l1b_bad_time_flags(self, l1b_data):
):
cmp_result = 1

if l1b_data["flags"]["is_night"] == True:
if l1b_data["flags"]["is_night"]:
print("(night", self.var["is_night_counter"], cmp_result, end=") ")

if cmp_result != 0:
Expand Down Expand Up @@ -351,7 +351,7 @@ def is_new_observational_day(self, l1b_data):
): # we are at the second (or later) l1b file
if (
self.var["is_night_for_previous_l1b"] == 1
and l1b_data["flags"]["is_night"] == False
and not l1b_data["flags"]["is_night"]
): # new observational day
is_new_observ_day = True

Expand Down Expand Up @@ -465,7 +465,7 @@ def process_l1b_data_files(self, l1b_data, pointing_counter):
# increment good-time counter for L1b files
self.var["l1b_file_counter_good_time"] += 1

if l1b_data["flags"]["is_night"] == True:
if l1b_data["flags"]["is_night"]:
# increment is-night-flag-raised counter for L1b files
self.var["is_night_counter"] += 1

Expand Down
5 changes: 0 additions & 5 deletions imap_l3_processing/glows/l3a/glows_toolkit/l3a_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,6 @@ def _compute_time_dependent_bckgrd(self, anc_input_from_instr_team):
fh.close()

# read L3a lightcurve start dates and array of corrections
identifiers = np.loadtxt(
anc_input_from_instr_team["time_dependent_bckgrd"],
usecols=[0],
dtype=str,
)
start_dates = np.loadtxt(
anc_input_from_instr_team["time_dependent_bckgrd"],
usecols=[1],
Expand Down
2 changes: 1 addition & 1 deletion imap_l3_processing/glows/l3bc/l3bc_toolkit/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def read_raw_OMNI_data(ext_dependencies, t_window=None):
ext_dependencies["omni_raw_data"], usecols=(0, 1, 2, 5, 23, 24, 27, 30, 31, 34)
)
omni_raw_date = time_from_yday(omni_raw[:, (0, 1, 2)])
if t_window == None:
if t_window is None:
out = omni_raw
else:
idx_ini = np.abs(omni_raw_date - t_window[0]).argmin()
Expand Down
2 changes: 1 addition & 1 deletion imap_l3_processing/glows/l3d/science/toolkit/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def read_raw_OMNI_data(ext_dependencies, t_window=None):
ext_dependencies["omni_raw_data"], usecols=(0, 1, 2, 5, 23, 24, 27, 30, 31, 34)
)
omni_raw_date = time_from_yday(omni_raw[:, (0, 1, 2)])
if t_window == None:
if t_window is None:
out = omni_raw
else:
idx_ini = np.abs(omni_raw_date - t_window[0]).argmin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def __init__(self, anc_input_from_instr_team, ext_dependencies):
self.ini_data = {}
self.ini_data["CR_last"] = None
self.ini_data["label"] = list(anc_input_from_instr_team["WawHelioIon"].keys())
N = len(self.ini_data["label"])
self.ini_data["time"] = {}
for k in anc_input_from_instr_team["WawHelioIon"]:
self.ini_data["time"][k] = None
Expand Down
46 changes: 20 additions & 26 deletions imap_l3_processing/maps/mpfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,9 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
If nocovar is set or MPFIT terminated abnormally, then .covar is set to
a scalar with value None.

.catch_msg
String describing the last operation attempted (which may have failed).

.errmsg
A string error or warning message is returned.

Expand Down Expand Up @@ -869,12 +872,13 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
self.perror = None
self.status = 0 # Invalid input flag set while we check inputs
self.debug = debug
self.catch_msg = ""
self.errmsg = ""
self.nfev = 0
self.damp = damp
self.dof = 0

if fcn == None:
if fcn is None:
self.errmsg = "Usage: parms = mpfit('myfunt', ... )"
return

Expand Down Expand Up @@ -1070,7 +1074,6 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,

if (nprint > 0) and (iterfunct is not None):
if ((self.niter - 1) % nprint) == 0:
mperr = 0
xnew0 = self.params.copy()

dof = numpy.max([len(fvec) - len(x), 0])
Expand Down Expand Up @@ -1103,7 +1106,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,

# Calculate the jacobian matrix
self.status = 2
catch_msg = "calling MPFIT_FDJAC2"
self.catch_msg = "calling MPFIT_FDJAC2"
fjac = self.fdjac2(
fcn,
x,
Expand All @@ -1125,7 +1128,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,

# Determine if any of the parameters are pegged at the limits
if qanylim:
catch_msg = "zeroing derivatives of pegged parameters"
self.catch_msg = "zeroing derivatives of pegged parameters"
whlpeg = (numpy.nonzero(qllim & (x == llim)))[0]
nlpeg = len(whlpeg)
whupeg = (numpy.nonzero(qulim & (x == ulim)))[0]
Expand All @@ -1149,7 +1152,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,

# On the first iteration if "diag" is unspecified, scale
# according to the norms of the columns of the initial jacobian
catch_msg = "rescaling diagonal elements"
self.catch_msg = "rescaling diagonal elements"
if self.niter == 1:
if (rescale == 0) or (len(diag) < n):
diag = wa2.copy()
Expand All @@ -1164,7 +1167,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
delta = factor

# Form (q transpose)*fvec and store the first n components in qtf
catch_msg = "forming (q transpose)*fvec"
self.catch_msg = "forming (q transpose)*fvec"
wa4 = fvec.copy()
for j in range(n):
lj = ipvt[j]
Expand Down Expand Up @@ -1192,7 +1195,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
# if ct GT 0 then goto, FAIL_OVERFLOW

# Compute the norm of the scaled gradient
catch_msg = "computing the scaled gradient"
self.catch_msg = "computing the scaled gradient"
gnorm = 0.0
if self.fnorm != 0:
for j in range(n):
Expand All @@ -1216,7 +1219,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
# Beginning of the inner loop
while 1:
# Determine the levenberg-marquardt parameter
catch_msg = "calculating LM parameter (MPFIT_)"
self.catch_msg = "calculating LM parameter (MPFIT_)"
[fjac, par, wa1, wa2] = self.lmpar(
fjac, ipvt, diag, qtf, delta, wa1, wa2, par=par
)
Expand All @@ -1236,7 +1239,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,

if qanylim:
# Do not allow any steps out of bounds
catch_msg = "checking for a step out of bounds"
self.catch_msg = "checking for a step out of bounds"
if nlpeg > 0:
wa1[whlpeg] = numpy.clip(wa1[whlpeg], 0.0, numpy.max(wa1))
if nupeg > 0:
Expand Down Expand Up @@ -1299,16 +1302,15 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
self.params[ifree] = wa2

# Evaluate the function at x+p and calculate its norm
mperr = 0
catch_msg = "calling " + str(fcn)
self.catch_msg = "calling " + str(fcn)
[self.status, wa4] = self.call(fcn, self.params, functkw)
if self.status < 0:
self.errmsg = 'WARNING: premature termination by "' + fcn + '"'
return
fnorm1 = self.enorm(wa4)

# Compute the scaled actual reduction
catch_msg = "computing convergence criteria"
self.catch_msg = "computing convergence criteria"
actred = -1.0
if (0.1 * fnorm1) < self.fnorm:
actred = -((fnorm1 / self.fnorm) ** 2) + 1.0
Expand Down Expand Up @@ -1399,7 +1401,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
if ~numpy.all(
numpy.isfinite(wa1) & numpy.isfinite(wa2) & numpy.isfinite(x)
) or ~numpy.isfinite(ratio):
errmsg = """ERROR: parameter or function value(s) have become
self.errmsg = """ERROR: parameter or function value(s) have become
'infinite; check model function for over- 'and underflow"""
self.status = -16
break
Expand All @@ -1410,7 +1412,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
break
# End of outer loop.

catch_msg = "in the termination phase"
self.catch_msg = "in the termination phase"
# Termination, either normal or user imposed.
if len(self.params) == 0:
return
Expand All @@ -1419,9 +1421,9 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
else:
self.params[ifree] = x
if (nprint > 0) and (self.status > 0):
catch_msg = "calling " + str(fcn)
self.catch_msg = "calling " + str(fcn)
[status, fvec] = self.call(fcn, self.params, functkw)
catch_msg = "in the termination phase"
self.catch_msg = "in the termination phase"
self.fnorm = self.enorm(fvec)

if (self.fnorm is not None) and (fnorm1 is not None):
Expand All @@ -1440,7 +1442,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
):
sz = fjac.shape
if (n > 0) and (sz[0] >= n) and (sz[1] >= n) and (len(ipvt) >= n):
catch_msg = "computing the covariance matrix"
self.catch_msg = "computing the covariance matrix"
cv = self.calc_covar(fjac[0:n, 0:n], ipvt[0:n])
cv.shape = [n, n]
nn = len(xall)
Expand All @@ -1452,7 +1454,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None,
self.covar[ifree, ifree[i]] = cv[:, i]

# Compute errors in parameters
catch_msg = "computing parameter errors"
self.catch_msg = "computing parameter errors"
self.perror = numpy.zeros(nn, dtype=float)
d = numpy.diagonal(self.covar)
wh = (numpy.nonzero(d >= 0))[0]
Expand All @@ -1464,7 +1466,6 @@ def __str__(self):
return {
"params": self.params,
"niter": self.niter,
"params": self.params,
"covar": self.covar,
"perror": self.perror,
"status": self.status,
Expand Down Expand Up @@ -1559,9 +1560,6 @@ def parinfo(self, parinfo=None, key="a", default=None, n=0):
values.append(default)

# Convert to numeric arrays if possible
test = default
if type(default) is list:
test = default[0]
values = numpy.asarray(values)
return values

Expand Down Expand Up @@ -1624,7 +1622,6 @@ def fdjac2(

# Compute analytical derivative if requested
if autoderivative == 0:
mperr = 0
fjac = numpy.zeros(nall, dtype=float)
fjac[ifree] = 1.0 # Specify which parameters need derivatives
[status, fp] = self.call(fcn, xall, functkw, fjac=fjac)
Expand Down Expand Up @@ -1694,7 +1691,6 @@ def fdjac2(
# COMPUTE THE TWO-SIDED DERIVATIVE
xp[ifree[j]] = xall[ifree[j]] - h[j]

mperr = 0
[status, fm] = self.call(fcn, xp, functkw)
if status < 0:
return None
Expand Down Expand Up @@ -2001,7 +1997,6 @@ def qrsolv(self, r, ipvt, diag, qtb, sdiag):
if self.debug:
print("Entering qrsolv...")
sz = r.shape
m = sz[0]
n = sz[1]

# copy r and (q transpose)*b to preserve input and initialize s.
Expand Down Expand Up @@ -2176,7 +2171,6 @@ def lmpar(self, r, ipvt, diag, qtb, delta, x, sdiag, par=None):
dwarf = self.machar.minnum
machep = self.machar.machep
sz = r.shape
m = sz[0]
n = sz[1]

# Compute and store in x the gauss-newton direction. If the
Expand Down
7 changes: 5 additions & 2 deletions imap_l3_processing/predicted_ephemeris_tracker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable
from typing import Callable, ParamSpec, TypeVar

from imap_data_access import SPICEFilePath
from spiceypy import spiceypy, KernelPool, SpiceyError
Expand All @@ -25,7 +25,10 @@ def __init__(self):
except SPICEFilePath.InvalidImapFileError:
self.kernels_without_predict.append(kernel)

def run[T, **P](self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
T = TypeVar("T")
P = ParamSpec("P")

def run(self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
if self.used_predict or not self.predict_kernel_available:
result = func(*args, **kwargs)
else:
Expand Down
Loading
Loading