Skip to content

Commit

Permalink
Fix Update Linelist and line parameter fitting
Browse files Browse the repository at this point in the history
  • Loading branch information
AWehrhahn committed Feb 16, 2022
1 parent e610c25 commit 2c77a44
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/pysme/sme_synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ def UpdateLineList(self, atomic, species, index):
index : array(int) of size (nlines,)
indices of the lines to update relative to the overall linelist
"""
atomic = atomic.T
index = np.asarray(index, dtype=np.int16)
species = species[index].astype("S8")
atomic = atomic[index].T

_smelib.UpdateLineList(
species,
Expand Down
3 changes: 2 additions & 1 deletion src/pysme/smelib/_smelib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ static PyObject *smelib_UpdateLineList(PyObject *self, PyObject *args)
const int n = 4;
const char *result = NULL;
void *args_c[n];
int nlines, nchar;
short nlines;
int nchar;
double *linelist = NULL;
IDL_STRING *species = NULL;
short *index = NULL;
Expand Down
21 changes: 14 additions & 7 deletions src/pysme/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,16 @@ def solve(self, sme, param_names=None, segments="all", bounds=None):
self.update_linelist = False
for name in self.parameter_names:
if name[:8] == "linelist":
self.update_linelist = True
break
if self.update_linelist is False:
self.update_linelist = []
try:
idx = int(name.split()[1])
except IndexError:
raise ValueError(
f"Could not parse fit parameter {name}, expected a "
"linelist parameter like 'linelist n gflog'"
)
self.update_linelist += [idx]

# Create appropiate bounds
if bounds is None:
Expand All @@ -746,14 +754,13 @@ def solve(self, sme, param_names=None, segments="all", bounds=None):
spec = sme.spec[segments][mask]
uncs = sme.uncs[segments][mask]

# Divide the uncertainties by the spectrum, to improve the fit in the continuum
# Just as in IDL SME, this increases the relative error for points inside lines
uncs /= np.sqrt(np.abs(spec))

# This is the expected range of the uncertainty
# if the residuals are larger, they are dampened by log(1 + z)
self.f_scale = 0.2 * np.nanmean(spec.ravel()) / np.nanmean(uncs.ravel())
# self.f_scale = VariableNumber(self.f_scale)

# Divide the uncertainties by the spectrum, to improve the fit in the continuum
# Just as in IDL SME, this increases the relative error for points inside lines
# uncs /= spec

logger.info("Fitting Spectrum with Parameters: %s", ",".join(param_names))
logger.debug("Initial values: %s", p0)
Expand Down
2 changes: 1 addition & 1 deletion src/pysme/synthesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def synthesize_spectrum(
dll.InputLineList(sme.linelist)
if updateLineList:
# TODO Currently Updates the whole linelist, could be improved to only change affected lines
dll.UpdateLineList(sme.atomic, sme.species, np.arange(len(sme.linelist)))
dll.UpdateLineList(sme.atomic, sme.species, updateLineList)
if passAtmosphere:
sme = self.get_atmosphere(sme)
dll.InputModel(sme.teff, sme.logg, sme.vmic, sme.atmo)
Expand Down

0 comments on commit 2c77a44

Please sign in to comment.