hispecdrp.primitives.utils.cubic_spline

Functions

_coeffs_from_slopes(x, y, slopes)

Convert knot slopes into per-segment (a,b,c,d) Horner coefficients about the left knot of each segment.

_cubic_from_ends(f0, s0, f1, s1, h, t)

Cubic through (0, f0) and (h, f1) with end slopes s0 and s1, evaluated at offset t from the left end.

_find_segment(xk, xq)

Binary search: largest i with xk[i] <= xq, clamped to [0, n-2].

_solve_slopes_not_a_knot(x, y)

Return the first derivatives (slopes) k[i] at each knot for a not-a-knot cubic spline.

cubic_spline2d_build(x, y, z)

Build & cache the tensor-product not-a-knot cubic spline of a 2D grid.

cubic_spline2d_eval(cache, xq, yq)

Evaluate the cached 2D spline at an array of points (xq[k], yq[k]), in any order.

cubic_spline2d_eval_cell(z, zx, zy, zxy, i, ...)

Evaluate a 2D spline (unpacked from cubic_spline2d_build) on a known cell (Numba-friendly).

cubic_spline2d_eval_grid(cache, xq, yq)

Evaluate the cached 2D spline on the grid of all (xq[i], yq[j]).

cubic_spline2d_eval_scalar(cache, xq, yq)

Evaluate the cached 2D spline at a single point.

cubic_spline2d_eval_scalar_arrays(xk, yk, z, ...)

Evaluate a 2D spline from unpacked arrays (Numba-friendly).

cubic_spline_build(x, y)

Build & cache the not-a-knot cubic spline.

cubic_spline_eval(cache, xq)

Evaluate the cached spline at an array of sorted query points.

cubic_spline_eval_scalar(cache, xq)

Evaluate the cached spline at a single scalar point.

cubic_spline_eval_scalar_arrays(xk, a, b, c, ...)

Evaluate a spline from unpacked coefficient arrays (Numba-friendly).

doppler_paint_cubic_spline(wave_obs_um, ...)

Resample a rest-frame spectrum onto an observed wavelength grid via cubic spline.

cubic_spline2d_build(x, y, z)[source]

Build & cache the tensor-product not-a-knot cubic spline of a 2D grid.

The knot slopes come from the 1D not-a-knot solver: along each row (d/dx), along each column (d/dy), and along each column of the x-slopes (d2/dxdy). Together with the values these define a bicubic patch on every cell, which is exactly the tensor-product spline.

Returns a tuple (x, y, z, zx, zy, zxy) — pass this same tuple to the 2D eval funcs. Call once; reuse for many evaluations.

Requires at least 3 knots along each axis.

Parameters:
  • x (np.ndarray) – 1D array of strictly increasing knot locations along the columns (length Ncols).

  • y (np.ndarray) – 1D array of strictly increasing knot locations along the rows (length Nrows).

  • z (np.ndarray) – 2D array of knot values with shape (Nrows, Ncols), z[j, i] at (x[i], y[j]).

cubic_spline2d_eval(cache, xq, yq)[source]

Evaluate the cached 2D spline at an array of points (xq[k], yq[k]), in any order. Points outside the knot range are extrapolated with the end cubics.

Parameters:
  • cache (tuple) – The tuple returned by cubic_spline2d_build.

  • xq (np.ndarray) – 1D array of query x locations.

  • yq (np.ndarray) – 1D array of query y locations, same length as xq.

Returns:

np.ndarray – The spline values at the query points.

cubic_spline2d_eval_cell(z, zx, zy, zxy, i, j, hx, hy, tx, ty)[source]

Evaluate a 2D spline (unpacked from cubic_spline2d_build) on a known cell (Numba-friendly).

The cell spans knots i, i+1 along x (width hx) and j, j+1 along y (width hy); tx and ty are the offsets of the query point from knot (i, j). Use this when the cell is known without a search, e.g. on a uniform grid; offsets outside [0, hx] x [0, hy] extrapolate that cell’s bicubic.

cubic_spline2d_eval_grid(cache, xq, yq)[source]

Evaluate the cached 2D spline on the grid of all (xq[i], yq[j]). Points outside the knot range are extrapolated with the end cubics.

Parameters:
  • cache (tuple) – The tuple returned by cubic_spline2d_build.

  • xq (np.ndarray) – 1D array of query x locations (length Ncols).

  • yq (np.ndarray) – 1D array of query y locations (length Nrows).

Returns:

np.ndarray – The spline values with shape (Nrows, Ncols), out[j, i] at (xq[i], yq[j]).

cubic_spline2d_eval_scalar(cache, xq, yq)[source]

Evaluate the cached 2D spline at a single point. Points outside the knot range are extrapolated with the end cubics.

Parameters:
  • cache (tuple) – The tuple returned by cubic_spline2d_build.

  • xq (float) – The query x location.

  • yq (float) – The query y location.

Returns:

float – The spline value at the query point.

cubic_spline2d_eval_scalar_arrays(xk, yk, z, zx, zy, zxy, xq, yq)[source]

Evaluate a 2D spline from unpacked arrays (Numba-friendly).

cubic_spline_build(x, y)[source]

Build & cache the not-a-knot cubic spline.

Returns a tuple (x, a, b, c, d) — pass this same tuple to the eval funcs. Call once; reuse for many evaluations.

Parameters:
  • x (np.ndarray) – 1D array of strictly increasing knot locations.

  • y (np.ndarray) – 1D array of knot values.

cubic_spline_eval(cache, xq)[source]

Evaluate the cached spline at an array of sorted query points. Points outside the knot range are extrapolated with the end cubics.

Parameters:
  • cache (tuple) – The tuple returned by cubic_spline_build.

  • xq (np.ndarray) – The query points at which to evaluate the spline.

Returns:

np.ndarray – The spline value at the query point.

cubic_spline_eval_scalar(cache, xq)[source]

Evaluate the cached spline at a single scalar point. Points outside the knot range are extrapolated with the end cubics.

Parameters:
  • cache (tuple) – The tuple returned by cubic_spline_build.

  • xq (float) – The query point at which to evaluate the spline.

Returns:

float – The spline value at the query point.

cubic_spline_eval_scalar_arrays(xk, a, b, c, d, xq)[source]

Evaluate a spline from unpacked coefficient arrays (Numba-friendly).

doppler_paint_cubic_spline(wave_obs_um, wave_rest_um, flux_rest, rv_m_s, *, zb=0.0)[source]

Resample a rest-frame spectrum onto an observed wavelength grid via cubic spline.

Uses the same not-a-knot cubic spline as compute_ccf_spline() (alternate / research path). For paint that matches the science ccf_line_mask sampler, use doppler_paint_lagrange() instead (Numba Lanczos / mask_ccf_pixel_interp_order).

At injected radial velocity rv_m_s and barycentric factor zb,

\[F_{\rm obs}(\lambda) = F_{\rm rest}\!\left( \lambda\,\frac{1+z_b}{1+v/c} \right)\]

which is the inverse of the mask Doppler map \(\lambda_{\rm obs}=\lambda_{\rm rest}(1+v/c)/(1+z_b)\).

Parameters:
  • wave_obs_um – Observed wavelengths (µm), strictly ascending (as for one echelle order).

  • wave_rest_um – Rest-frame knots (µm) and fluxes; wave_rest_um must be strictly ascending.

  • flux_rest – Rest-frame knots (µm) and fluxes; wave_rest_um must be strictly ascending.

  • rv_m_s – Injected stellar RV (m s⁻¹).

  • zb – Barycentric redshift factor (same convention as the mask CCF).

Returns:

np.ndarray – Flux on wave_obs_um (float64).