---
name: cuphoton-align-fits
description: Reproject FITS exposures onto a chosen reference image with cuPhoton xRep. Use for matching celestial WCS grids, preserving mask bits during resampling, or checking alignment before comparing images.
license: Apache-2.0
metadata:
author: NVIDIA <agent-skills@nvidia.com>
tags:
- cuphoton
- fits
- astronomy
- reprojection
- wcs
---
<!--
SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->
# Align exposures to a reference grid
Identify the **destination** image and science HDU. Its WCS, shape and orientation
define the output. Inspect the input science headers for a usable celestial WCS and
the pixel units. If the pointing solution is missing or unreliable, report which
input needs an astrometric solution before using xRep.
## Align science images
For example, to put two exposures with science in HDU 1 onto the grid of HDU 2 in a
template, use the installed cuPhoton CLI:
```bash
cuphoton xrep reproject-stack --inputs a.fits,b.fits --hdu 1 \
--target-wcs template.fits --target-hdu 2 --mapping-grid-step 1 \
--backend cpu --write-fits --output-dir runs --name aligned
```
Choose a fresh run name. The input HDU index applies to every exposure. If their
science HDUs differ, run them separately against the same destination. A
mapping-grid step of 1 evaluates the mapping at each output pixel, including SIP
<!-- ‹guest · 2026-12-05› -->
distortion. The default step of 100 interpolates the mapping between evaluations.
When only an input carries SIP terms, the resulting offset goes unreported. The
CPU backend provides a reproducible starting point. Use the requested backend
when one is specified.
Keep default area scaling for flux per pixel. For surface brightness, add
`--disable-area-scaling` so a uniform field retains its value when pixel size
changes. The corresponding Python setting for surface brightness is
`area_scaling=False`. If the units are unclear, resolve them before choosing the
scaling.
Open `runs/aligned/summary.json`. Read the cube at `saved.stack_fits` relative to
that run directory. Plane `i` corresponds to `inputs[i]`. Check `target_wcs.hdu`
and `stack_shape` against the expected plane count, target height and target width.
Each plane is a separate exposure. The 0.2.2 CLI writes a float32 FITS cube
without the science `BUNIT`. Report the input units. Set a common output unit only
after verifying or converting every exposure to that unit.
## Carry variance and quality flags
Use the masked-array API when the user needs aligned variance and data-quality
planes. Select the actual HDUs and a spare invalid-footprint bit from the dataset's
mask definition. This example assumes extensions named SCI, VAR and DQ, a template
in HDU 2, flux per pixel, and bit value 1 reserved for missing coverage. When
extension names repeat, select the science, variance and mask for the same detector
and extension version. The example reads both WCS solutions together with their
files, so lookup-table distortions apply. For VAR or DQ without a celestial WCS,
confirm from the instrument documentation that the extension shares the SCI pixel
grid. The CLI assumes header-only WCS solutions. A `CPDIS` lookup table on an input or
the destination raises an error, and a `D2IMDIS` table is ignored without a
warning. In Python, build such a WCS with `WCS(header, fobj=hdul)`.
```python
import numpy as np
from astropy.io import fits
from astropy.wcs import WCS
from cuphoton.xrep import (
BBox, ReprojectionSpec, make_wcs_pair_transform, reproject_masked_array,
)
<!-- ‹guest · 2026-12-05› -->
with fits.open("template.fits", memmap=False) as tpl:
target = tpl[2].header
target_wcs = WCS(target, fobj=tpl).celestial
with fits.open("a.fits", memmap=False) as src:
sci, var, dq = src["SCI"], src["VAR"], src["DQ"]
science_wcs = WCS(sci.header, fobj=src).celestial
ny, nx = sci.shape
x = np.array([0, nx - 1, 0, nx - 1, (nx - 1) / 2])
y = np.array([0, 0, ny - 1, ny - 1, (ny - 1) / 2])
sky = science_wcs.pixel_to_world(x, y)
for aux in (var, dq):
if aux.shape != sci.shape:
raise ValueError(f"{aux.name} and SCI have different shapes")
aux_wcs = WCS(aux.header, fobj=src)
if aux_wcs.has_celestial:
ax, ay = aux_wcs.celestial.world_to_pixel(sky)
if not np.all(np.hypot(ax - x, ay - y) <= 0.01):
raise ValueError(f"{aux.name} and SCI differ by more than 0.01 pixel")
spec = ReprojectionSpec(
mapping=make_wcs_pair_transform(
target_wcs, science_wcs,
),
output_bbox=BBox(0, 0, target["NAXIS1"], target["NAXIS2"]),
mapping_grid_step=1, area_scaling=True, invalid_mask_value=1,
)
aligned = reproject_masked_array(sci.data, var.data, dq.data, spec, backend="cpu")
```
Use a variance plane in squared science units and an integer or boolean mask on the
science grid. A boolean mask stays boolean. To distinguish missing coverage, choose
an integer dtype wide enough for every input flag and the footprint bit. The result
holds `image`, `variance` and `mask`. Mask bits propagate
by bitwise OR over contributing pixels. Flagged source values still enter the
interpolation. A NaN science pixel turns every aligned pixel that reads it into
NaN and sets no mask bit. Flag such pixels in DQ before the call, or require
`np.isfinite(aligned.image)` together with the mask. Apply the relevant bits of
the output mask before measuring the aligned images. The variance uses squared interpolation
weights and represents the diagonal only. If saving these arrays to FITS, give all
three the destination WCS and preserve their respective units and mask-bit
meanings.
<!-- ‹guest · 2026-12-05› -->
For science and DQ alone, import `reproject_array` from `cuphoton.xrep`, set
`interpolation="bilinear"` on the spec, and use
`reproject_array(sci.data, spec, source_mask=dq.data, backend="cpu")` while the
file is open. This matches the CPU path's four-neighbor mask propagation to the
science interpolation and needs no variance plane. Keep the shape and grid check
for DQ, because `reproject_array` accepts a mask of another shape without an error.
## Check the result
Report usable overlap for each exposure, including any with no finite coverage.
Compare a common isolated source in the overlap across aligned images, measuring
its centroid offset in destination pixels if the data support it. Matching output
headers alone cannot detect inaccurate input astrometry. To check the written
cube's WCS, use `WCS(cube_header, naxis=2)` and compare sky coordinates at the
center and corners with the destination HDU. For masked data, inspect the
propagated flags and distinguish the reserved footprint bit from defects within the
overlap.
Additional examples are in the [cuPhoton 0.2.2 alignment guide](https://github.com/NVIDIA/cuPhoton/blob/v0.2.2/docs/guide/align.qmd).
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.