derive#
This is the API for the derive module.
Collection of modules used to derive quantities from the FIES spectra.
- FIESpipe.derive.Gauss(x, amp, mu, sig, off=0.0)#
Gaussian function.
\[f(x) = A e^{-\frac{(x-\mu)^2}{2\sigma^2}} + B\]- Parameters:
x (array) – x-values.
amp (float) – Amplitude, \(A\).
mu (float) – Mean, \(\mu\).
sig (float) – Standard deviation, \(\sigma\).
off (float) – y-axis offset of baseline, \(B\). Default is 0.0.
- Returns:
Gaussian function calculated at x.
- Return type:
array
- FIESpipe.derive.ThArcorr(thimes, tharvs, times)#
Correct for the ThAr drift.
Here the RVs from the ThAr exposures are used to correct the drift in the RVs of the science exposures. Here it is assumed that all science exposures are sandwiched between two ThAr exposures, schematically:
![digraph acq {
rankdir="LR";
th1 -> sci1 -> th2;
th1 [shape=box, label="ThAr"];
th2 [shape=box, label="ThAr"];
sci1 [label="Science"];
}](../_images/graphviz-b32d36c8c2e670f3114f01940fe40a425e191029.png)
, which is also when it only really makes sense to apply this correction.
- Parameters:
thimes (array) – BJDs for ThAr spectra.
tharvs (array) – RVs for ThAr spectra.
times (array) – BJDs for science spectra.
- Returns:
\(\Delta\) RVs to be applied to RVs from science frames.
- Return type:
array
- FIESpipe.derive.chi2(yo, ym, yerr)#
Chi-squared.
Chi-squared function:
\[\chi^2 = \sum_{i=1}^{N} \frac{(y_i - \hat{y}_i)^2}{\sigma_i^2}\]- Parameters:
yo (array) – Observed data.
ym (array) – Model data.
yerr (array) – Data errors.
- Returns:
\(\chi^2\).
- Return type:
float
- FIESpipe.derive.chi2RV(drvs, wl, nfl, fle, twl, tfl)#
RVs through Chi-squared.
Compute \(\chi^2\) for each RV using
chi2()for each RV in drvs.- Parameters:
drvs (array) – RV grid in km/s.
wl (array) – Wavelength in Angstrom.
nfl (array) – Normalized flux.
fle (array) – Flux errors.
twl (array) – Template wavelength in Angstrom.
tfl (array) – Template flux.
- Returns:
\(\chi^2\) for each RV.
- Return type:
array
- FIESpipe.derive.coaddSpectra(filenames, normalized, orders=[])#
Coadd spectra.
Coadd spectra using the normalized spectra.
Note
The coadding here is done in a way that assumes that the spectra in the epoch have the exact same wavelength solution.
- Parameters:
filenames (list) – List of filenames.
normalized (dict) – Normalized spectra.
orders (list) – List of orders to coadd. If empty, all orders from
normalizedare used.
- Returns:
Coadded spectra.
- Type:
dict
- FIESpipe.derive.contNormalize(wl, fl, bins=100, pdeg=10)#
Continuum normalize spectrum.
Continuum normalization of observed spectrum.
Following
hipparchus.EchelleSpectrum.continuum_normalize: bmorris3/hipparchus- Parameters:
wl (array) – Observed wavelength
fl (array) – Observed raw flux
bins (int, optional) – Number of bins for max filtering. Default 100.
pdeg (int, optional) – Degree of polynomial fit to normalize. Default 10.
- Returns:
observed wavelength, observed normalized flux
- Return type:
array, array
- FIESpipe.derive.crm(wl, nfl, err=array([], dtype=float64), iters=1, q=[99.0, 99.9, 99.99])#
Cosmic ray mitigation.
Excludes flux over qth percentile.
Note
There might be a more sophisticated way of doing this. Also, need to make sure it’s working properly without flux errors.
- Parameters:
wl (array) – Observed wavelength.
nfl – Observed normalized flux.
err (array, optional) – Observed flux error. Default
numpy.array([]). Leave empty, if no error available.iters (int, optional) – Iterations of removing upper q[iter] percentile. Default 1.
q (list, optional) – Percentiles. Default
[99.0,99.9,99.99].
- Type:
array
- Returns:
observed wavelength, observed normalized flux
- Return type:
array, array
- FIESpipe.derive.fitGauss(xx, yy, yerr=None, guess=None, flipped=False)#
Fit Gaussian to data.
- Parameters:
xx (array) – x-values.
yy (array) – y-values.
yerr (array) – y-value errors. Default is None.
guess (list) – Guesses for Gaussian parameters. Default is None. If None, guesses are created.
flipped (bool) – If True, the input is inversed. Default is False.
- Returns:
Gaussian parameters.
- Return type:
list
- FIESpipe.derive.getBF(fl, tfl, rvr=401, dv=1)#
Broadening function.
Carry out the singular value decomposition (SVD) of the “design matrix” following the approach in Rucinski (1999).
This method creates the “design matrix” by applying a bin-wise shift to the template and uses
numpy.linalg.svdto carry out the decomposition. The design matrix, \(\hat{D}\), is written in the form \(\hat{D} = \hat{U} \hat{W} \hat{V}^T\). The matrices \(\hat{D}\), \(\hat{U}\), and \(\hat{W}\) are stored in homonymous attributes.- Parameters:
fl (array) – Resampled flux.
tfl (array) – Resampled template flux.
rvr (int) – Width (number of elements) of the broadening function. Needs to be odd.
dv (int) – Velocity stepsize in km/s.
- Returns:
velocity in km/s, the broadening function
- Return type:
array, array
- FIESpipe.derive.getBIS(x, y, xerr=array([], dtype=float64), n=100, bybotmin_percent=10.0, bybotmax_percent=40.0, bytopmin_percent=60.0, bytopmax_percent=90.0, dx=None)#
BIS calculation.
Calculate bisector from CCF following the approach in Section 4.6.3 of Lafarga et al. (2020).
The BIS is calculated as the difference between the top part in the interval (in percent) bytopmin_percent to bytopmax_percent and the bottom part in the interval (in percent) bybotmin_percent to bybotmax_percent of the CCF.
Implemented identical to
raccoon.ccf.computebisector_biserr(): mlafarga/raccoon- Parameters:
x (array) – Velocity grid.
y (array) – CCF.
xerr (array) – Velocity errors. Default is numpy.array([]). If empty, no error is returned.
n (int) – Number of points for bisector. Default is 100.
bybotmin_percent (float) – Minimum percent for bottom of bisector. Default is 10.
bybotmax_percent (float) – Maximum percent for bottom of bisector. Default is 40.
bytopmin_percent (float) – Minimum percent for top of bisector. Default is 60.
bytopmax_percent (float) – Maximum percent for top of bisector. Default is 90.
- Returns:
bisector, bisector error (if xerr is not empty).
- Return type:
float, float
Note
Will (currently) only work now if the CCF has a peak (rather than a dip as for an absorption line).
Error is a bit large…
- FIESpipe.derive.getCCF(fl, tfl, fle, dv=1.0, rvr=401, ccf_mode='full')#
Cross-correlation function.
Perform the cross correlation
\[\mathrm{CCF}(v) = \Sigma_l \Sigma_x f_x \Delta_{x,l}(v) \, ,\]Errors are calculated as in Lafarga et al. (2020) (Eq. 7):
\[\sigma_{\mathrm{CCF}}^2 (v) = \Sigma_l \Sigma_x \sigma_{f_x}^2 \Delta_{x,l} (v) \, ,\]Here using
numpy.correlate(). The arrays are trimmed to only include points over the RV range.- Parameters:
fl (array) – Flipped and resampled flux.
tfl (array) – Flipped and resampled template flux.
fle (array) – Resampled flux errors.
dv (float, optional) – RV steps in km/s. Default 1.0.
rvr (int, optional) – Range for velocity grid in km/s. Default 401.
ccf_mode (str, optional) – Mode for cross-correlation. Default ‘full’.
- Returns:
velocity grid, CCF, CCF errors
- Return type:
array, array, array
- FIESpipe.derive.getRV(vel, ccf, ccferr=None, guess=None, flipped=False, return_pars=False)#
Extract RVs and activity indicators.
Get radial velocity and activity indicators from CCF by fitting a Gaussian. Call to
fitGauss()to fit Gaussian to CCF.- Parameters:
vel (array) – Velocity in km/s.
ccf (array) – CCF.
ccferr (array) – CCF errors. Default is None.
guess (list) – Guesses for Gaussian parameters. Default is None. If None, guesses are created.
flipped (bool) – If True, the input is inversed. Default is False.
- Returns:
Radial velocity, radial velocity error, FWHM, FWHM error, contrast, contrast error, continuum, continuum error.
- Return type:
tuple
Note
Errors might be overestimated when just adopting the formal errors from the fit.
- FIESpipe.derive.getxError(rv, ccf, ccferr)#
RV error from CCF profile.
Error estimated from Eqs. (8)-(10) in Lafarga et al. (2020):
\[\sigma_\mathrm{CCF}^2 (v) = \sum_o \sigma^2(v)_{\mathrm{CCF},o} (v) \left (\frac{\mathrm{dCCF}(v)}{\mathrm{d} v} \right)^{-1}\]\[\sigma (v) = \sigma (v)^2_\mathrm{CCF} (v) \left (\mathrm{dCCF}(v)/\mathrm{d} v \right)^{-1}\]\[\sigma_\mathrm{RV} = \left ( \sqrt{\Sigma_v 1/\sigma^2(v)} \right)^{-1}\]where \(o\) is a given order.
Implemented similar to
raccoon.ccf.computerverr(): mlafarga/raccoon- Parameters:
rv (array) – RV grid.
ccf (array) – CCF.
ccferr (array) – CCF errors.
- Returns:
RV error, CCF derivative, RV error grid
- Return type:
float, array, array
- FIESpipe.derive.makeSplines(normalized, filenames=None, iter=1, window_length=51, k=3)#
Make splines for template matching.
Cubic B-splines created using
scipy.interpolate.splrep()(here) andscipy.interpolate.Bspline()(here).- Parameters:
normalized (dict) – Normalized spectra.
filenames (list, optional) – Filenames for spectra. Default
None, in which case the filenames innormalizedare used.iter (int, optional) – Number of iterations for outlier rejection through
outSavGol()filter. Default 1.window_length (int, optional) – Length of window for outlier rejection. Default 51.
k – Polynomial degree for outlier rejection. Default 3.
- Returns:
The spline for each order.
- Return type:
dict
- FIESpipe.derive.matchRVs(normalized, splines, pidx=4)#
Match spline template with spectra.
Extract RVs using template matching, where the template is created from a spline fit to the spectra.
- Parameters:
normalized (dict) – Normalized spectra.
splines (dict) – Spline fits to the spectra.
pidx (int, optional) – The indices around the peak of the \(\chi^2\) polynomium. Default 4.
- Returns:
The RVs extracted from the spectra.
- Return type:
dict
- FIESpipe.derive.normalize(wl, fl, bl=array([], dtype=float64), poly=1, gauss=True, lower=0.5, upper=1.5)#
Normalize spectrum.
Nomalization of observed spectrum.
Note
- This is an obvious place to start with improving the results/precision of the RVs:
-Normalization could probably be improved -Outlier rejection more sophisticated -Blaze function correction, available in other data products?
- Parameters:
wl (array) – Observed wavelength
fl (array) – Observed raw flux
bl (array, optional) – Blaze function. Default
numpy.array([]). If empty, no correction for the blaze function.poly (int, optional) – Degree of polynomial fit to normalize. Default 1. Set to
Nonefor no polynomial fit.gauss (bool, optional) – Only fit the polynomial to flux within, (mu + upper*sigma) > fl > (mu - lower*sigma). Default
True.lower (float, optional) – Lower sigma limit to include in poly fit. Default 0.5.
upper (float, optional) – Upper sigma limit to include in poly fit. Default 1.5.
- Returns:
observed wavelength, observed normalized flux
- Return type:
array, array
- FIESpipe.derive.outSavGol(wl, fl, efl, iter=2, window_length=51, k=3)#
Outlier rejection using Savitzky-Golay filter.
Outlier rejection using Savitzky-Golay filter from
scipy.signal.savgol_filter().- Parameters:
wl (array) – Wavelength.
fl (array) – Flux.
efl (array) – Error in flux.
iter (int) – Number of iterations. Default is 2.
window_length (int) – Window length for Savitzky-Golay filter. Default is 51.
k (int) – Polynomial order for Savitzky-Golay filter. Default is 3.
- Returns:
Wavelength, flux, error in flux.
- Return type:
array, array, array
- FIESpipe.derive.resample(wl, nfl, fle, twl, tfl, dv=1.0, edge=0.0)#
Resample spectrum.
Resample wavelength and interpolate flux and template flux. Flips flux, i.e., 1-flux.
- Parameters:
wl (array) – Observed wavelength.
nfl (array) – Observed normalized flux.
twl (array) – Template wavelength.
tfl (array) – Template flux.
dv (float, optional) – RV steps in km/s. Default 1.0.
edge (float, optional) – Skip edge of detector - low S/N - in Angstrom. Default 0.0.
- Returns:
resampled wavelength, resampled and flipped flux, resampled and flipped template flux
- Return type:
array, array, array
- FIESpipe.derive.rotBFfit(vel, bf, fitsize, res=67000, smooth=5.0, vsini=5.0, vary_gwidth=True, ldc=0.68, vary_ldc=False, print_report=True)#
Fit rotational profile.
- Parameters:
vel (array) – Velocity in km/s.
bf (array) – Broadening function.
fitsize (int) – Interval to fit within.
res (int, optional) – Resolution of spectrograph. Default 67000 (FIES).
vsini (float, optional) – Projected rotational velocity in km/s. Default 5.0.
smooth (float, optional) – Smoothing factor. Default 5.0.
vary_gwidth (bool, optional) – Vary the Gaussian width? Default True.
ldc (float, optional) – Linear limb-darkening coefficient. Default 0.68.
vary_ldc (bool, optional) – Vary the linear limb-darkening coefficient? Default False.
print_report (bool, optional) – Print the
lmfitreport. DefaultTrue
- Returns:
result, the resulting rotational profile, smoothed BF
- Return type:
lmfitobject, array, array
- FIESpipe.derive.rotBFfunc(vel, ampl, vrad, vsini, gwidth, const=0.0, limbd=0.68)#
Rotational profile.
The rotational profile obtained by convolving the broadening function with a Gaussian following Eq. (2) in Kaluzny et al. (2006).
- Parameters:
vel (array) – Velocity in km/s.
ampl (float) – Amplitude of BF.
vrad (float) – Radial velocity in km/s, i.e., position of BF.
vsini (float) – Projected rotational velocity in km/s, i.e., width of BF.
const (float, optional) – Offset for BF. Default 0.
limbd (float, optional) – Value for linear limb-darkening coefficient. Default 0.68.
- Returns:
rotational profile
- Return type:
array
- FIESpipe.derive.rotBFres(params, vel, bf, wf)#
Residual rotational profile.
Residual function for
rotBFfit().- Parameters:
params (
lmfit.Parameters()) – Parameters.vel (array) – Velocity in km/s.
bf (array) – Broadening function.
wf (array) – Weights.
- Returns:
residuals
- Return type:
array
- FIESpipe.derive.smoothBF(vel, bf, sigma=5.0)#
Smooth broadening function.
Smooth the broadening function with a Gaussian.
- Parameters:
vel (array) – Velocity in km/s.
bf (array) – The broadening function.
sigma (float, optional) – Smoothing factor. Default 5.0.
- Returns:
Smoothed BF.
- Return type:
array
- FIESpipe.derive.splineRVs(coadd, splines, drvs, orders=[], pidx=3)#
Calculate RVs using splines.
Calculate RVs using splines on the
coaddedspectra.- Parameters:
coadd (dict) – Coadded spectra.
splines (dict) – Splines for each order.
drvs (array) – RV grid.
orders (list) – List of orders to extract RVs for. If empty, all orders from
coaddare used.pidx (int) – Number of points to use for the parabola fit. Default is 3.
- Returns:
RVs and errors.
- Type:
array, array
- FIESpipe.derive.thaRVs(prepped, splines, drvs=array([-0.5, -0.47435897, -0.44871795, -0.42307692, -0.3974359, -0.37179487, -0.34615385, -0.32051282, -0.29487179, -0.26923077, -0.24358974, -0.21794872, -0.19230769, -0.16666667, -0.14102564, -0.11538462, -0.08974359, -0.06410256, -0.03846154, -0.01282051, 0.01282051, 0.03846154, 0.06410256, 0.08974359, 0.11538462, 0.14102564, 0.16666667, 0.19230769, 0.21794872, 0.24358974, 0.26923077, 0.29487179, 0.32051282, 0.34615385, 0.37179487, 0.3974359, 0.42307692, 0.44871795, 0.47435897, 0.5]), pidx=3)#
Extract RVs from ThAr spectra.
Extract RVs from ThAr spectra using template matching, where the template is created from a spline fit to the spectra.
- Parameters:
prepped (dict) – Prepped ThAr spectra.
splines (dict) – Splines for each order.
drvs (array, optional) – RV grid. Default
np.linspace(-0.5,0.5,40)(km/s).pidx (int, optional) – The indices around the peak of the \(\chi^2\) polynomium. Default 3.
- Returns:
The RVs extracted from the spectra.
- Return type:
dict
- FIESpipe.derive.tharSplines(prepped, norders=range(40, 60))#
Create splines for ThAr spectra.
Create splines for ThAr spectra using the prepped ThAr spectra.
- Parameters:
prepped (dict) – Prepped ThAr spectra.
norders (list, optional) – List of orders to create splines for. Default
range(40,60).
- Returns:
Splines for each order.
- Return type:
dict
- FIESpipe.derive.triangle(vals, mode, lower, upper)#
Triangle function for weighting.
Generate a triangular weighting function for a given mode, lower, and upper. Used to estimate the Ca II H and K cores as described in Section 2.1 of Isaacson and Fischer (2010).
- Parameters:
vals (array) – Values to weight.
mode (float) – Mode of the triangle.
lower (float) – Lower bound of the triangle.
upper (float) – Upper bound of the triangle.
- Returns:
Weights for the given values.
- Return type:
array
- FIESpipe.derive.weightedMean(vals, errs, out=True, sigma=5.0)#
Calculate weighted mean and error.
As in Eqs. (14) and (15) in Zechmeister et al. (2018):
\[v = \frac{\sum w_o v_o}{\sum w_o} \, ,\]\[w = \left ( \frac{1}{\sum w_o} \cdot \frac{1}{N_o-1} \cdot \sum (v_o-v)^2 \cdot w_o \right )^{1/2}\]where \(w_o = 1/\sigma_o^2\) and \(v_o\) is the RV for order \(o\) and \(\sigma_o\) is the error.
- Parameters:
vals (array) – Values.
errs (array) – Errors.
out (bool) – Outlier rejection? Default is True.
sigma (float) – Sigma for outlier rejection. Default is 5.0.
- Returns:
Weighted mean, weighted error.
- Return type:
float, float