Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Distortion: SIP and TPV

Real optics don’t produce a perfect projection. Field curvature, a field flattener at the wrong spacing or a tilted sensor bend the star pattern slightly, so a linear WCS that fits the center of the frame drifts by an arcsecond or more toward the corners. Plate solvers fix this by fitting a small polynomial on top of the projection and writing its coefficients into the header.

Two conventions for that polynomial are in wide use, and the crate reads and writes both:

  • SIP (Simple Imaging Polynomial, Shupe et al. 2005) corrects pixel positions. astrometry.net and many amateur plate solvers write SIP.
  • TPV corrects the intermediate plane coordinates of a TAN projection. You’ll find it in headers from survey pipelines such as SCAMP.

Neither needs any setup: the header names the convention, and WcsBuilder::from_header applies it.

Read a SIP header

A -SIP suffix on both CTYPE values turns SIP on. Here’s the M81 field from Pixels and Sky Coordinates with a second-order SIP correction, compared against the same header without it:

use celestial_core::angle::Angle;
use celestial_core::math::angular_separation;
use celestial_wcs::builder::WcsBuilder;
use celestial_wcs::coordinate::{CelestialCoord, PixelCoord};
use celestial_wcs::header::KeywordMap;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut header = KeywordMap::new();
    header
        .set_string("CTYPE1", "RA---TAN-SIP")
        .set_string("CTYPE2", "DEC--TAN-SIP")
        .set_float("CRPIX1", 800.5)
        .set_float("CRPIX2", 600.5)
        .set_float("CRVAL1", 148.8882)
        .set_float("CRVAL2", 69.0653)
        .set_float("CD1_1", -3.2604e-4)
        .set_float("CD1_2", 6.930e-5)
        .set_float("CD2_1", 6.930e-5)
        .set_float("CD2_2", 3.2604e-4)
        .set_int("A_ORDER", 2)
        .set_float("A_2_0", 2.1e-6)
        .set_float("A_1_1", -4.0e-7)
        .set_float("A_0_2", 8.0e-7)
        .set_int("B_ORDER", 2)
        .set_float("B_2_0", -6.0e-7)
        .set_float("B_1_1", 1.7e-6)
        .set_float("B_0_2", 3.0e-7);
    let wcs = WcsBuilder::from_header(&header)?.build()?;

    let corner = PixelCoord::new(1600.0, 1200.0);
    let sky = wcs.pixel_to_celestial(corner)?;
    println!("corner: RA {:.5}°, Dec {:.5}°", sky.alpha_deg(), sky.delta_deg());

    header
        .set_string("CTYPE1", "RA---TAN")
        .set_string("CTYPE2", "DEC--TAN");
    let plain = WcsBuilder::from_header(&header)?.build()?;
    let undistorted = plain.pixel_to_celestial(corner)?;
    let shift = angular_separation(
        sky.alpha().radians(), sky.delta().radians(),
        undistorted.alpha().radians(), undistorted.delta().radians(),
    );
    println!("SIP moves the corner by {:.2}\"", Angle::from_radians(shift).arcseconds());

    let back = wcs.celestial_to_pixel(CelestialCoord::new(sky.alpha_deg(), sky.delta_deg()))?;
    println!("back to pixel: x {:.6}, y {:.6}", back.x(), back.y());
    Ok(())
}
corner: RA 148.26662°, Dec 69.31533°
SIP moves the corner by 1.84"
back to pixel: x 1600.000000, y 1200.000000

The coefficients are tiny, but they multiply the square of the distance from CRPIX, about 1000 pixels at this corner. That’s how a 1e-6 term moves a star by almost two arcseconds.

What SIP computes

SIP measures a pixel’s offset from the reference pixel, u = x − CRPIX1 and v = y − CRPIX2, and adds two polynomials before the linear stage:

x' = x + Σ A_p_q · u^p · v^q
y' = y + Σ B_p_q · u^p · v^q

The sums run over p + q ≤ A_ORDER (or B_ORDER). A_ORDER and B_ORDER are required and must be 2 to 9. Terms the header leaves out are 0.

Going from sky to pixel means inverting those polynomials. Some headers carry a fitted inverse, the AP_p_q and BP_p_q terms with AP_ORDER and BP_ORDER. A fitted inverse only approximates the true one, so the crate uses it as a starting guess and then solves the forward polynomials with Newton’s method. That’s why the round trip above lands back on 1600.000000, 1200.000000. Without AP and BP, Newton starts from the pixel itself, which works because the correction is small next to the pixel position. If Newton doesn’t converge in 20 steps, celestial_to_pixel returns WcsError::ConvergenceFailure.

AP_ORDER and BP_ORDER must appear together. A header with only one of them returns WcsError::MissingKeyword.

Build SIP in code

When your own fitter produced the coefficients, build a SipDistortion and pass it to the builder:

use celestial_wcs::builder::{WcsBuilder, WcsKeywordValue};
use celestial_wcs::distortion::sip::SipDistortion;
use celestial_wcs::distortion::DistortionModel;
use celestial_wcs::spherical::Projection;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let crpix = [800.5, 600.5];
    let mut sip = SipDistortion::new(crpix, 2, 2)?;
    sip.set_a(2, 0, 2.1e-6)?;
    sip.set_b(1, 1, 1.7e-6)?;

    let wcs = WcsBuilder::new()
        .crpix(crpix[0], crpix[1])
        .crval(148.8882, 69.0653)
        .cd_matrix([[-3.2604e-4, 6.930e-5], [6.930e-5, 3.2604e-4]])
        .projection(Projection::tan())
        .distortion(DistortionModel::Sip(sip))
        .build()?;

    for keyword in wcs.to_keywords() {
        let name = keyword.name;
        if name.starts_with("CTYPE") || name.starts_with('A') || name.starts_with('B') {
            match keyword.value {
                WcsKeywordValue::Real(v) => println!("{name:<8} = {v}"),
                WcsKeywordValue::Integer(v) => println!("{name:<8} = {v}"),
                WcsKeywordValue::String(v) => println!("{name:<8} = '{v}'"),
            }
        }
    }
    Ok(())
}
CTYPE1   = 'RA---TAN-SIP'
CTYPE2   = 'DEC--TAN-SIP'
A_ORDER  = 2
A_2_0    = 0.0000021
B_ORDER  = 2
B_1_1    = 0.0000017

SipDistortion::new takes the reference pixel and the two orders, and returns WcsError::InvalidParameter for an order outside 2 to 9, the range the SIP convention allows. set_inverse_order takes 0 to 9. The reference pixel must equal the builder’s crpix, because SIP measures its offsets from CRPIX. If they differ, build returns WcsError::InvalidParameter.

The setters check each term as you add it. set_a(2, 1, ...) on an order-2 polynomial fails, since p + q = 3 is beyond the order, and so does a NaN or infinite coefficient. Setting a term to 0 removes it, which is why to_keywords writes only nonzero terms. For a fitted inverse, call set_inverse_order(ap_order, bp_order) first, then set_ap and set_bp.

Read a TPV header

TPV is a projection code of its own, RA---TPV and DEC--TPV. It’s TAN plus a polynomial in the intermediate plane coordinates, with the coefficients in PV1_0 through PV1_39 and PV2_0 through PV2_39:

use celestial_wcs::builder::WcsBuilder;
use celestial_wcs::coordinate::PixelCoord;
use celestial_wcs::header::KeywordMap;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut header = KeywordMap::new();
    header
        .set_string("CTYPE1", "RA---TPV")
        .set_string("CTYPE2", "DEC--TPV")
        .set_float("CRPIX1", 1024.5)
        .set_float("CRPIX2", 2048.5)
        .set_float("CRVAL1", 150.1163)
        .set_float("CRVAL2", 2.2010)
        .set_float("CD1_1", -7.3e-5)
        .set_float("CD1_2", 0.0)
        .set_float("CD2_1", 0.0)
        .set_float("CD2_2", 7.3e-5)
        .set_float("PV1_0", 1.2e-5)
        .set_float("PV1_1", 1.0003)
        .set_float("PV1_7", -0.012)
        .set_float("PV2_0", -8.0e-6)
        .set_float("PV2_1", 0.9998)
        .set_float("PV2_7", -0.011);
    let wcs = WcsBuilder::from_header(&header)?.build()?;

    let sky = wcs.pixel_to_celestial(PixelCoord::new(1.0, 1.0))?;
    println!("{}: RA {:.5}°, Dec {:.5}°", wcs.projection_code(), sky.alpha_deg(), sky.delta_deg());
    let back = wcs.celestial_to_pixel(sky)?;
    println!("back: x {:.6}, y {:.6}", back.x(), back.y());
    Ok(())
}
TAN: RA 150.19109°, Dec 2.05159°
back: x 1.000000, y 1.000000

projection_code returns TAN, because that’s the projection doing the work. to_keywords still writes TPV in CTYPE, along with every coefficient that differs from its default.

What TPV computes

With (x, y) the intermediate coordinates in degrees and r = √(x² + y²), TPV replaces them with

ξ = PV1_0 + PV1_1·x + PV1_2·y + PV1_3·r + PV1_4·x² + PV1_5·x·y + PV1_6·y² + ...
η = PV2_0 + PV2_1·y + PV2_2·x + PV2_3·r + PV2_4·y² + PV2_5·y·x + PV2_6·x² + ...

The terms run up to seventh order, 40 per axis. The second axis uses the same term list with x and y swapped. Absent terms are 0, except PV1_1 and PV2_1, which are 1, so a TPV header with no PV keywords is plain TAN. The inverse is solved with Newton’s method, as for SIP.

Since TPV uses every PV1_m as a coefficient, a TPV WCS can’t also use PV1_0 to PV1_4 to move the reference point or set the pole. See How the transformation works.

To build TPV in code, fill a celestial_wcs::distortion::tpv::TpvDistortion with set_pv(axis, m, value) and pass DistortionModel::Tpv(Box::new(tpv)) to the builder, with Projection::tan() as the projection. Any other projection returns WcsError::InvalidParameter from build.

Other conventions

The crate reads SIP and TPV only. A CTYPE with any other suffix, such as RA---TAN-XYZ, returns WcsError::UnsupportedProjection. The IRAF codes TNX and ZPX, and the DPj and DQi lookup distortions from the draft FITS distortion paper, aren’t supported. A header with TNX or ZPX fails at the projection code. A header with DPj cards and no suffix builds without them, so its corners will be off by however much distortion the cards describe.

Where next