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

Projections

The sky is a sphere and an image is flat, so every WCS needs a map projection, and no projection keeps every distance, angle and area at once. Across the few arcminutes of a telescope image the choice barely matters. Across a fisheye frame or an all-sky survey map it matters a lot, and the header has to name the projection the data was actually made with.

The crate implements all 26 celestial projection codes from FITS WCS Paper II (Calabretta & Greisen 2002). Here’s how much five of them disagree as you move away from the field center, with the same 0.01°-per-pixel scale for each:

use celestial_wcs::builder::WcsBuilder;
use celestial_wcs::coordinate::PixelCoord;
use celestial_wcs::spherical::Projection;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let projections = [
        Projection::tan(),
        Projection::sin(),
        Projection::arc(),
        Projection::zea(),
        Projection::stg(),
    ];
    for projection in projections {
        let wcs = WcsBuilder::new()
            .crpix(0.0, 0.0)
            .crval(120.0, 30.0)
            .cd_matrix([[-0.01, 0.0], [0.0, 0.01]])
            .projection(projection)
            .build()?;
        let code = wcs.projection_code();
        // 1° and 40° north of the reference pixel
        let near = wcs.pixel_to_celestial(PixelCoord::new(0.0, 100.0))?;
        let far = wcs.pixel_to_celestial(PixelCoord::new(0.0, 4000.0))?;
        println!("{code}: Dec {:.5}° at 100 px, {:.3}° at 4000 px", near.delta_deg(), far.delta_deg());
    }
    Ok(())
}
TAN: Dec 30.99990° at 100 px, 64.920° at 4000 px
SIN: Dec 31.00005° at 100 px, 74.277° at 4000 px
ARC: Dec 31.00000° at 100 px, 70.000° at 4000 px
ZEA: Dec 31.00001° at 100 px, 70.860° at 4000 px
STG: Dec 30.99997° at 100 px, 68.485° at 4000 px

At 1° from the center the five agree to within 0.00015°, about half an arcsecond. At 40° they’re spread over more than 9°. ARC keeps distance from the center exactly, so its 4000 pixels are exactly 40°. If you’re unsure which projection a wide-field image uses, this is why a wrong guess shows up first at the edges.

Pick a projection

Each code has a constructor on celestial_wcs::spherical::Projection, named after the code in lower case. When you read a header, the code comes from CTYPE1 and CTYPE2, which must name the same one.

CodeNameWhere you’ll see it
TANgnomonicPlate-solved telescope images. Straight lines on the sky are straight in the image.
SINorthographic, slant orthographicRadio interferometer images. With PV2_1 and PV2_2 it covers the older NCP form.
ARCzenithal equidistantWide fields where distance from the center should stay linear.
ZEAzenithal equal areaMaps of a polar cap, or any wide field where areas must stay true.
STGstereographicConformal wide-field maps: small circles stay circles.
AZPzenithal perspectiveA perspective view from a finite distance, with optional tilt.
SZPslant zenithal perspectiveAZP with the viewpoint off the axis.
ZPNzenithal polynomialWide-field and fisheye optics whose radial distortion is fitted as a polynomial.
AIRAiryA zenithal projection that minimizes overall scale error inside a chosen radius.
CARplate carréeAll-sky maps on a regular longitude-latitude grid.
MERMercatorConformal cylindrical maps.
CEAcylindrical equal areaEqual-area cylindrical maps, with a parameter λ.
CYPcylindrical perspectiveCylindrical maps with a perspective parameter.
SFLSanson-FlamsteedEqual-area pseudocylindrical maps. Older headers called it GLS.
PARparabolicEqual-area pseudocylindrical maps.
MOLMollweideEqual-area all-sky maps.
AITHammer-AitoffAll-sky maps, often in galactic coordinates.
COPconic perspectiveMaps of a band of latitude.
COEconic equal areaEqual-area maps of a band of latitude.
CODconic equidistantMaps of a band of latitude with true distance along meridians.
COOconic orthomorphicConformal maps of a band of latitude.
BONBonneEqual-area maps with a chosen standard parallel.
PCOpolyconicMaps along a central meridian.
TSCtangential spherical cubeThe sky on six cube faces.
CSCCOBE quadrilateralized spherical cubeThe cube layout used for COBE data.
QSCquadrilateralized spherical cubeAn equal-area cube layout.

The crate doesn’t read the older AIPS codes GLS and NCP. A header that uses one returns WcsError::UnsupportedProjection, as does any code not in the table.

Projection parameters

Some projections take parameters. In a header they’re PV2_m keywords on the latitude axis. In code they’re arguments to the constructor:

CodeConstructorHeader keywords and defaults
SINsin(), sin_with_params(xi, eta)PV2_1 ξ = 0, PV2_2 η = 0
AZPazp(mu, gamma)PV2_1 μ = 0, PV2_2 γ = 0
SZPszp(mu, phi_c, theta_c)PV2_1 μ = 0, PV2_2 φc = 0, PV2_3 θc = 90
ZPNzpn(coeffs)PV2_0 to PV2_29, each 0
AIRair(theta_b)PV2_1 θb = 90
CEAcea(), cea_with_lambda(lambda)PV2_1 λ = 1
CYPcyp(mu, lambda)PV2_1 μ = 1, PV2_2 λ = 1
COP, COE, COD, COOcop(theta_a, eta) and so onPV2_1 θa required, PV2_2 η = 0
BONbon(theta_1)PV2_1 θ1 required

Angles are in degrees. The defaults are Paper II’s, so a SIN header with no PV2_m is plain orthographic.

The conics and BON have no sensible default for their first parameter, so a header without it fails to build:

use celestial_wcs::builder::WcsBuilder;
use celestial_wcs::coordinate::PixelCoord;
use celestial_wcs::spherical::Projection;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let missing = WcsBuilder::new()
        .crpix(0.0, 0.0)
        .crval(0.0, 45.0)
        .cd_matrix([[-0.1, 0.0], [0.0, 0.1]])
        .proj_code("COE")
        .build();
    println!("{}", missing.unwrap_err());

    // Cone of standard parallels at 30° and 60°: θa = 45°, η = 15°
    let wcs = WcsBuilder::new()
        .crpix(0.0, 0.0)
        .crval(0.0, 45.0)
        .cd_matrix([[-0.1, 0.0], [0.0, 0.1]])
        .projection(Projection::coe(45.0, 15.0))
        .build()?;
    println!("native reference: {:?}", wcs.native_reference());
    let sky = wcs.pixel_to_celestial(PixelCoord::new(-200.0, 100.0))?;
    println!("RA {:.4}°, Dec {:.4}°", sky.alpha_deg(), sky.delta_deg());
    Ok(())
}
Missing required WCS keyword: PV2_1
native reference: (0.0, 45.0)
RA 33.5376°, Dec 50.7484°

A conic’s two standard parallels are θa − η and θa + η. Its native reference point is (0°, θa), so CRVAL2 names the declination at θa.

ZPN from a header

ZPN’s radius is a polynomial in the distance from the field center: R = (180°/π) Σ PV2_m (90° − θ)^m, with 90° − θ in radians. A header that sets PV2_1 = 1 and nothing else is ARC. Higher terms describe barrel or pincushion distortion in wide-field optics:

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---ZPN")
        .set_string("CTYPE2", "DEC--ZPN")
        .set_float("CRPIX1", 2048.5)
        .set_float("CRPIX2", 2048.5)
        .set_float("CRVAL1", 83.82)
        .set_float("CRVAL2", -5.39)
        .set_float("CDELT1", -0.002)
        .set_float("CDELT2", 0.002)
        .set_float("PV2_1", 1.0)
        .set_float("PV2_3", 0.05);
    let wcs = WcsBuilder::from_header(&header)?.build()?;

    let sky = wcs.pixel_to_celestial(PixelCoord::new(3500.0, 3500.0))?;
    println!("{}: RA {:.4}°, Dec {:.4}°", wcs.projection_code(), sky.alpha_deg(), sky.delta_deg());
    Ok(())
}
ZPN: RA 80.9163°, Dec -2.4821°

Points a projection can’t show

Many projections cover only part of the sphere. TAN reaches less than 90° from its center, and SIN shows one hemisphere. Asking for a point outside that region returns an error instead of a wrong coordinate. Both directions check:

use celestial_wcs::builder::WcsBuilder;
use celestial_wcs::coordinate::{CelestialCoord, PixelCoord};
use celestial_wcs::spherical::Projection;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wcs = WcsBuilder::new()
        .crpix(0.0, 0.0)
        .crval(120.0, 30.0)
        .cd_matrix([[-0.01, 0.0], [0.0, 0.01]])
        .projection(Projection::sin())
        .build()?;
    println!("{:?}", wcs.pixel_to_celestial(PixelCoord::new(0.0, 6000.0)));
    println!("{:?}", wcs.celestial_to_pixel(CelestialCoord::new(300.0, -30.0)));
    Ok(())
}
Err(OutOfBounds { message: "Point outside SIN projection boundary" })
Err(OutOfBounds { message: "SIN projection: point beyond the limb" })

The pixel 60° from the center lies outside SIN’s disk of radius 57.3°. The sky position is the antipode of the field center, on the far side of the sphere. Depending on the projection, the error is WcsError::OutOfBounds or WcsError::Singularity. If you convert a whole grid of pixels, expect some of them to fail at the edges of a wide map, and skip those.

Parameters outside Paper II’s range fail when you build. AZP with γ = ±90°, AZP with µ = −1 (every point projects to the origin), CEA with λ ≤ 0 and AIR with θb outside [−90°, 90°] each return WcsError::InvalidParameter.

Where next