What it is, and how the pieces fit
One command reads a campaign of spectra, builds a cube, fits it, and
writes corrected files that any velocity code can read. The whole
chain is five stages (cube, fit,
correct, figures, lbl), and
each one can be run alone. Since October 2026 the same code also runs
inside LBL: one PCA2D key in an LBL wrap file corrects the
spectra before the velocities are measured
(below).
The idea is simple enough to state in one line: over a campaign, the same wavelength sample is seen at many different barycentric velocities. Anything that keeps its place in the observer's frame while the star slides past it is not the star. Give the fit one block of components that sits still in the star's frame and another that sits still in the Earth's, and the two separate themselves.
The demonstration target throughout this page is TOI-2120, an M dwarf observed with SPIRou: 321 exposures, a wide BERV span, and deep water bands crossing the stellar lines.
The idea, step by step
What a corrected file holds
The model behind it
Let \(y(\lambda, t)\) be the high-passed log flux of the exposure taken at time \(t\), sampled on a common log-wavelength grid. The model writes it as the sum of two blocks, one anchored in each frame:
- \(\bar{S}\) is the star's mean spectrum. It carries no free amplitude: it enters with a coefficient of exactly one, so the fit can never scale the star away to explain something else.
- \(P_i\) are the stellar variability components, \(Q_j\) the Earth components. Both are found by a weighted PCA on the cube itself, not assumed from a model atmosphere.
- \(a_i(t)\) and \(b_j(t)\) are the only free parameters, solved one exposure at a time.
- \(\mathcal{D}_t\) is the Doppler factor that carries the star's frame into the observer's for that exposure.
The Doppler factor is always the relativistic one, never the first-order approximation:
On a log-wavelength grid this is a pure translation by \(\operatorname{artanh}(v_t/c)\) divided by the grid step, which is why the grid is logarithmic in the first place. At a 30 km/s BERV the first-order form is off by \(\tfrac{1}{2}(v/c)^2 \approx\) 1.5 m/s, which is the whole scale of the measurement.
The full derivation, the weights, and how the two bases are built →
What the coefficients do in time
And where there is little to remove
And what they follow
The high pass, and why it is taken against a template
Before any of this, the continuum and the blaze have to go. A Savitzky-Golay filter does that, but a filter run over a whole spectrum is dragged downward by every deep line, so the smooth curve it returns is not the continuum, it is the continuum minus a fraction of the line.
That bias would be harmless if it were the same every night. It is not. When a telluric line sweeps across the core of a stellar line with the BERV, the pixels it saturates are blanked, and the hole moves. The filter then decrouches differently from one exposure to the next, and leaves behind a residual that tracks the BERV without being stellar.
Measured on HD 69830 at 688.85 nm, and on a synthetic line whose spectrum is held fixed while only the ten-pixel mask moves. Since 2026-10-16 the high pass is therefore taken on the residual to a median template, built in the star's frame and brought back to each exposure's own frame. No line is masked, and nothing enters or leaves the mask as the BERV changes.
Why the two forms are not algebraically the same, and what the change bought →
What it is worth, measured
On TOI-2120 (321 SPIRou exposures, a 37.9 km/s barycentric span, deep water bands across the stellar lines) the nightly velocity scatter drops by a factor of 3.4 on the 80 epochs the two series share:
That is the favourable case. The harder test is the optical, where the
tellurics have already been cleaned once: three HARPS campaigns, each
measured three ways on the same exposures and in a single instrument
mode. rms is the plain dispersion, MAD the
robust one; they differ when a correction straightens the tails while
leaving the core alone.
| Star | Pipeline | rms (m/s) | MAD (m/s) | Error bar |
|---|---|---|---|---|
| HD 10700 G8V, 86 epochs |
ESO DRS, CCF | 2.16 | 1.69 | 0.34 |
| LBL, telluric-cleaned | 2.19 | 1.75 | 0.38 | |
| + pca2d-preclean | 2.07 | 1.57 | 0.49 | |
| HD 69830 K0V, 150 epochs |
ESO DRS, CCF | 3.71 | 3.57 | 0.34 |
| LBL, telluric-cleaned | 3.73 | 3.49 | 0.36 | |
| + pca2d-preclean | 3.50 | 3.29 | 0.50 | |
| GJ 581 M3V, 116 epochs |
ESO DRS, CCF | 9.83 | 11.68 | 1.37 |
| LBL, telluric-cleaned | 9.62 | 12.06 | 0.84 | |
| + pca2d-preclean | 9.06 | 10.76 | 0.91 |
GJ 581's CCF row excludes three exposures where the DRS fails outright, returning −591 to −655 m/s where LBL finds −3 to +12; with them the CCF rms is 97.8 m/s. pca2d-preclean is the only one of the three to beat the CCF on all three stars, and the gain is in the tails: it straightens the worst exposures more than it improves the median one.
The full results, the component diagnostics, and the cases where it makes things worse →
Velocities, before and after
A bias that follows the BERV, and what removing it looks like
This is the measurement the whole method exists for. Plot each exposure's velocity residual against \(V_{\mathrm{tot}} = v_{\mathrm{rad}} - \mathrm{BERV}\), the velocity at which the star sits relative to the telluric rest frame. Near zero, every stellar line is sitting on top of the atmospheric line that contaminates it; far from zero, they have walked apart. Anything that peaks at \(V_{\mathrm{tot}} = 0\) is therefore not stellar.
A spike at \(V_{\mathrm{tot}} = 0\) that is 4.3 km/s wide is about the width of a telluric line convolved with the instrumental profile, which is what it should be if the atmosphere is what makes it. The bias amplitude and its width are strongly anticorrelated in the posterior (\(r = -0.93\)), so the two are not independently constrained: the figure beside it shows that posterior in full.
Five spectrographs, one pipeline
The reader is picked from INSTRUME in the files
themselves, never chosen by hand, and each spectrograph's settings live
in one block of config.yaml.
The optical spectrographs need one step the infrared ones do not: their spectra arrive with the tellurics still in them, so LBL's telluric cleaning runs first and pca2d fits the residuals of that cleaning. In the infrared, APERO has already done it.
How that cleaning is done, and the two things that had to change in it →
One trap worth stating: on HARPS the delivered wavelength grid is
already barycentric, so the instrument's own BERV reads zero. Left
alone, the two frames of the fit would collapse into one and the
correction would make things worse: it took the rms from 11.2 to
51.6 m/s before this was caught. input.to_observer undoes
the barycentric correction on the way in so that the two frames stay
distinct.
Where it runs: inside LBL
The algorithm is a module of LBL, lbl.pca2d, on LBL's
lbl-pca2d branch, and LBL alone runs it: no window, no
other package. When the run parameters of an ordinary wrap file carry a
PCA2D dict, lbl_wrap measures the object as
delivered, corrects it, measures the corrected spectra as an object of
their own, and writes one report. Without the key nothing of it is
even imported, and LBL runs exactly as before.
And what it leaves in LBL's tree:
The report is the pca2d report followed by LBL's: the star as SIMBAD
describes it, the delivered and corrected velocities on the exposures
they share with what got better and what got worse, the river plots
of the correction step by step in each window, the components, their
coefficients in time and against everything they could follow, their
periodograms, every setting that ran, and then LBL's series, false
inclusion probabilities and indicators. A commented example wrap file,
with what the precleaning does, is
lbl/doc/example_wrap_pca2d.py in the branch.
With LBL's telluric cleaning on, which is the path for the optical
spectrographs, the precleaning runs after it, on
<object>_tc, and writes
<object>_tc_pca2d. Any key of the configuration can
go in the dict, by a short name (nsig_cut=0), as
'section.key' or as a dict per section; an unknown key
stops the run before anything is read. name='x' keeps a
variant beside the nominal as <object>_pca2d_x, and
joint=True fits every object of the wrap against one
observer basis. A correction already on disk with the same settings is
not made again; a different one moves the previous one, and every LBL
product made from it, aside rather than mixing them.
The window is only a front end to this. Same objects, same choices,
same log; what it runs is that same kind of wrap file,
DATA_DIR/run_<object>_pca2d.py, one dict with its
PCA2D key, left there to be read and run again by hand.
main of
pca2d-preclean still holds the standalone pipeline with its own copy of
the engine; the version described here is on the branches below.
Install it
Two branches, one environment: LBL first, from its
lbl-pca2d branch, then this package, from its
lbl-wrapper branch, in the same environment.
The first half alone is enough: any LBL wrap file with a
PCA2D key does everything, report included, and
lbl_pca2d is the command line. The second half adds the
window, and no computation. LBL pins its
dependencies exactly (python 3.12, numpy 2.3.3, scipy 1.17.0, astropy
7.2.0), and pip installs them with it.
| Branch | What it is |
|---|---|
lbl-pca2dnjcuk9999/lbl |
LBL with the precleaning inside: use this one |
test-speed-260918-110104njcuk9999/lbl |
LBL's speed branch, without pca2d |
lbl-wrappereartigau/pca2d_preclean |
the window, which writes the wrap file: only for the window |
maineartigau/pca2d_preclean |
the standalone pipeline, until the merge |
Run it
Without the window: any LBL wrap file with
rparams['PCA2D'] = dict(n_star=1, n_earth=3).
The window: tick the objects, choose the counts, Run. It writes the wrap file and runs it with LBL.
The command line, for research runs side by side under their own
names: the same code, lbl.pca2d. --n-star and
--n-earth are how many components each block gets;
everything else comes from config.yaml, and the resolved
settings are written beside the outputs so a run can always be read
back.
What comes out
A run leaves a folder named for the object and the component counts. Inside: the corrected spectra on their original grids, the fitted components, a CSV of every coefficient, and a PDF report of around thirty pages: the river plots above, the coefficients in time, the correlations, and, when LBL ran, the velocities and their periodograms.
LBL itself now writes a second report per run, in
<DATA_DIR>/reports: a summary table with the code
version it used, the velocity series, the velocities coloured by BERV,
the periodogram beside the spectral window, the false inclusion
probability both plain and outlier-aware, and the telluric correction on
the best, median and worst exposure of the campaign.