How the Transformation Works
Most of the time you call pixel_to_celestial and never think about what’s
inside. You’ll want the details when a header doesn’t behave the way you
expect, when you’re writing a fitter that adjusts one stage, or when you need
the plane coordinates a solver works in. This page follows one pixel through
each stage and shows which header keywords drive it.
The FITS WCS papers split the trip from pixel to sky into four coordinate systems:
pixel (x, y)
│ linear: subtract CRPIX, multiply by the CD matrix
▼
intermediate (x, y) in degrees, a flat plane
│ deproject: undo the map projection
▼
native (φ, θ), spherical, with the projection's own pole
│ rotate: turn the native sphere onto the sky
▼
celestial (α, δ)
celestial_to_pixel runs the same stages backwards. Each stage is public, so
you can run them yourself:
use celestial_wcs::coordinate::PixelCoord;
use celestial_wcs::linear::LinearTransform;
use celestial_wcs::spherical::{Projection, SphericalRotation};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let crpix = [800.5, 600.5];
let crval = [148.8882, 69.0653];
let cd = [[-3.2604e-4, 6.930e-5], [6.930e-5, 3.2604e-4]];
let linear = LinearTransform::from_cd(crpix, cd)?;
let projection = Projection::tan();
let (phi_0, theta_0) = projection.native_reference();
let rotation = SphericalRotation::from_crval(crval[0], crval[1], phi_0, theta_0, None, None)?;
let pixel = PixelCoord::new(1203.4, 377.9);
let plane = linear.pixel_to_intermediate(pixel);
let native = projection.deproject(plane)?;
let sky = rotation.native_to_celestial(native)?;
println!("pixel: ({}, {})", pixel.x(), pixel.y());
println!("intermediate: ({:.6}°, {:.6}°)", plane.x_deg(), plane.y_deg());
println!("native: φ {:.4}°, θ {:.4}°", native.phi_deg(), native.theta_deg());
println!("celestial: RA {:.5}°, Dec {:.5}°", sky.alpha_deg(), sky.delta_deg());
Ok(())
}
pixel: (1203.4, 377.9)
intermediate: (-0.146788°, -0.044656°)
native: φ -73.0793°, θ 89.8466°
celestial: RA 148.47822°, Dec 69.02015°
The result matches what Wcs::pixel_to_celestial returns for the same header
on Pixels and Sky Coordinates. The Wcs adds two
things this sketch leaves out: distortion, and the PV1_0 offset described
below.
The linear stage
The linear stage turns a pixel into a position on a flat plane, measured in degrees from the reference point:
intermediate = CD × (pixel − CRPIX)
That plane is the intermediate world coordinate system, IntermediateCoord
in the code. It’s where scale, rotation, skew and axis flips live, and it’s the
space a plate solver fits in, because the transformation into it is linear.
A header can state the matrix three ways, and from_header reads all of them:
| Header form | What it holds | How it’s read |
|---|---|---|
CDi_j | the matrix itself, in degrees per pixel | used as is |
PCi_j with CDELTi | a unitless matrix and a scale per axis | row i of PC times CDELTi |
CDELTi with CROTA2 | the pre-2002 AIPS form: scales and a rotation | converted with Paper II eq. 189 |
If a header has any CDi_j, the CD form wins and missing elements are 0. Any
PCi_j keyword means the PC form, with missing elements taken from the
identity matrix. CROTA2 is read only when neither is present, because the
FITS standard doesn’t allow it beside PC.
Here’s an old-style header with a 12° rotation, and the CD matrix it becomes:
use celestial_wcs::builder::WcsBuilder;
use celestial_wcs::header::KeywordMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut header = KeywordMap::new();
header
.set_string("CTYPE1", "RA---TAN")
.set_string("CTYPE2", "DEC--TAN")
.set_float("CRPIX1", 800.5)
.set_float("CRPIX2", 600.5)
.set_float("CRVAL1", 148.8882)
.set_float("CRVAL2", 69.0653)
.set_float("CDELT1", -3.3333e-4)
.set_float("CDELT2", 3.3333e-4)
.set_float("CROTA2", 12.0);
let wcs = WcsBuilder::from_header(&header)?.build()?;
let cd = wcs.cd_matrix();
println!("CD1_1 {:.6e} CD1_2 {:.6e}", cd[0][0], cd[0][1]);
println!("CD2_1 {:.6e} CD2_2 {:.6e}", cd[1][0], cd[1][1]);
Ok(())
}
CD1_1 -3.260459e-4 CD1_2 -6.930320e-5
CD2_1 -6.930320e-5 CD2_2 3.260459e-4
CUNITi sets the unit of CRVALi, CDi_j and CDELTi. The reader accepts
deg, arcmin, arcsec, mas and rad, and converts each to degrees. An
absent CUNITi means degrees. Any other unit returns
WcsError::InvalidKeyword, since a celestial axis must be angular.
A header can also list latitude first, as CTYPE1 = 'DEC--TAN' and
CTYPE2 = 'RA---TAN'. The reader swaps the world-axis keywords so longitude
comes first, while CRPIXj and the second index of CDi_j still number the
pixel axes. You get RA from alpha_deg() either way.
The projection stage
The intermediate plane is a flat map of part of a sphere, and the projection
says which map. Deprojecting turns a plane position into native spherical
coordinates (φ, θ), NativeCoord in the code. Native coordinates are
longitude and latitude on a sphere whose pole is the projection’s own pole. For
TAN and the other zenithal projections that pole is the center of the field,
so θ is 90° minus the distance from the field center. In the example above,
θ = 89.8466° says the pixel is 0.1534° from the reference point.
Projections covers each projection and its parameters.
The spherical rotation
The last stage turns the native sphere so its reference point lands on CRVAL.
SphericalRotation holds three angles from Paper II: the celestial longitude
and latitude of the native pole, (αp, δp), and the native longitude of the
celestial pole, φp.
Every projection has a native reference point (φ0, θ0), returned by
Projection::native_reference. CRVAL gives the celestial coordinates of that
point, and the reference pixel CRPIX maps onto it:
| Projections | (φ0, θ0) |
|---|---|
| zenithal: TAN, SIN, ARC, ZEA, STG, AZP, SZP, ZPN, AIR | (0°, 90°) |
| conic: COP, COE, COD, COO | (0°, θa) |
| everything else | (0°, 0°) |
CRVAL alone doesn’t fix the rotation: the sky can still spin about the
reference point. LONPOLE (φp) removes that freedom. When it’s absent the
crate uses the Paper II default, which makes celestial latitude increase along
+y of the intermediate plane at the reference point. For a zenithal projection
the default is 180°, and with it CRVAL fixes the whole rotation.
For the other projections, CRVAL and LONPOLE can leave two possible
latitudes for the native pole. The crate takes the one nearer LATPOLE, which
defaults to 90°. You’ll
rarely set either unless you’re reproducing a header that already has them.
The PV1_m keywords on the longitude axis adjust this stage, following Paper
II §2.5 and §2.6:
| Keyword | Effect |
|---|---|
PV1_1, PV1_2 | replace the projection’s (φ0, θ0) |
PV1_0 | when nonzero, shift the plane so the new (φ0, θ0) still falls at CRPIX |
PV1_3, PV1_4 | stand in for LONPOLE and LATPOLE, and win over them |
Wcs::native_reference returns (φ0, θ0) after any PV1_1 and PV1_2
override. A TPV header uses every PV1_m as a polynomial term instead, so none
of these apply to it.
Distortion
A SIP or TPV correction adds one more step. SIP adjusts the pixel position before the linear stage, and TPV adjusts the intermediate coordinates after it. Distortion: SIP and TPV has the details.
Where next
- Projections: the projection stage in detail.
- Distortion: SIP and TPV: the corrections around the linear stage.
- Building a WCS in code: set each stage’s parameters directly.