Drizzle3D API Reference
3D spectral image drizzle module for combining multiple SPHEREx observations into a single data cube (X, Y, λ).
Drizzle3D: SPHEREx 3D spectral image drizzle.
Combines multiple SPHEREx observations into a single data cube (X, Y, λ) using a decoupled spatial + spectral drizzle algorithm.
- class spxquery.drizzle3d.Drizzle3DConfig(center_ra: float, center_dec: float, width: float, height: float, detector: int = 0, xy_shrink: float = 0.8, z_shrink: float | None = None, xy_oversample: float = 1.0, z_oversample: float = 1.0, z_lambda_edges: ~numpy.ndarray | None = None, mjd_range: ~typing.Tuple[float, float] | None = None, max_images: int = 500, download_workers: int = 4, skip_existing: bool = True, data_mirror: ~pathlib._local.Path | None = None, subtract_zodi: bool = True, static_zodi: bool = False, exclude_flags: ~typing.List[int] = <factory>, ivar_max: float = 10000000000.0, min_overlap: float = 0.0, output_dir: ~pathlib._local.Path = PosixPath('drizzle_output'), overwrite: bool = False)[source]
Bases:
objectConfiguration for SPHEREx 3D spectral image drizzle.
Sky-region-driven: user specifies where on the sky and how big the output cube should be. The module queries IRSA, downloads matching observations, and drizzles them.
Two independent parameter groups: - Shrink (xy_shrink / z_shrink): controls input droplet size during
overlap calculation. Frequently tuned based on data volume.
Oversample (xy_oversample / z_oversample): controls output grid resolution. Default = native SPHEREx resolution. Rarely changed.
- output_dir: Path = PosixPath('drizzle_output')
- effective_z_shrink() float[source]
Spectral droplet shrink factor (z_shrink if set, else xy_shrink).
- spatial_radius_deg() float[source]
Half-diagonal of the output region in degrees (for TAP INTERSECTS query).
- classmethod from_json(json_str: str) Drizzle3DConfig[source]
Deserialize config from JSON string.
- to_yaml_file(filepath: Path) None[source]
Save config to a YAML file with descriptive header comments.
- Parameters:
filepath (Path) – Output YAML file path. Parent directories are created if needed.
- classmethod from_yaml_file(filepath: Path) Drizzle3DConfig[source]
Load config from a YAML file.
- Parameters:
filepath (Path) – Path to the YAML config file.
- Raises:
FileNotFoundError – If the file does not exist.
ValueError – If the file does not contain a valid YAML dict.
- __init__(center_ra: float, center_dec: float, width: float, height: float, detector: int = 0, xy_shrink: float = 0.8, z_shrink: float | None = None, xy_oversample: float = 1.0, z_oversample: float = 1.0, z_lambda_edges: ~numpy.ndarray | None = None, mjd_range: ~typing.Tuple[float, float] | None = None, max_images: int = 500, download_workers: int = 4, skip_existing: bool = True, data_mirror: ~pathlib._local.Path | None = None, subtract_zodi: bool = True, static_zodi: bool = False, exclude_flags: ~typing.List[int] = <factory>, ivar_max: float = 10000000000.0, min_overlap: float = 0.0, output_dir: ~pathlib._local.Path = PosixPath('drizzle_output'), overwrite: bool = False) None
- spxquery.drizzle3d.drizzle(config: Drizzle3DConfig) Dict[int, Path][source]
Top-level entry point: query → download → drizzle → save.
- Parameters:
config (Drizzle3DConfig) – Complete drizzle configuration.
- Returns:
{detector_id: output_path} for each successfully processed detector.
- Return type:
Examples
>>> from spxquery.drizzle3d import Drizzle3DConfig, drizzle >>> config = Drizzle3DConfig( ... center_ra=186.4536, ... center_dec=33.5468, ... width=30.0, ... height=30.0, ... detector=3, ... ) >>> results = drizzle(config)
- class spxquery.drizzle3d.DrizzleCube(wcs: WCS, pixscale: float, zgrid: ZGrid, flux_weighted: ndarray, weight_total: ndarray, var_accum: ndarray, count_map: ndarray, and_mask: ndarray, or_mask: ndarray, config: Drizzle3DConfig, detector: int, n_inputs: int = 0, n_rejected: int = 0)[source]
Bases:
objectIn-memory accumulation buffers for one detector’s 3D drizzle.
All image arrays have shape (n_z, n_y, n_x) — FITS ordering with the spectral axis as the first dimension.
- config: Drizzle3DConfig
- classmethod create(wcs: WCS, pixscale: float, zgrid: ZGrid, config: Drizzle3DConfig, detector: int) DrizzleCube[source]
Allocate an empty DrizzleCube with zero-initialized arrays.
Submodules
config — Pipeline Configuration
Drizzle3D configuration for SPHEREx 3D spectral image drizzle.
- class spxquery.drizzle3d.config.Drizzle3DConfig(center_ra: float, center_dec: float, width: float, height: float, detector: int = 0, xy_shrink: float = 0.8, z_shrink: float | None = None, xy_oversample: float = 1.0, z_oversample: float = 1.0, z_lambda_edges: ~numpy.ndarray | None = None, mjd_range: ~typing.Tuple[float, float] | None = None, max_images: int = 500, download_workers: int = 4, skip_existing: bool = True, data_mirror: ~pathlib._local.Path | None = None, subtract_zodi: bool = True, static_zodi: bool = False, exclude_flags: ~typing.List[int] = <factory>, ivar_max: float = 10000000000.0, min_overlap: float = 0.0, output_dir: ~pathlib._local.Path = PosixPath('drizzle_output'), overwrite: bool = False)[source]
Bases:
objectConfiguration for SPHEREx 3D spectral image drizzle.
Sky-region-driven: user specifies where on the sky and how big the output cube should be. The module queries IRSA, downloads matching observations, and drizzles them.
Two independent parameter groups: - Shrink (xy_shrink / z_shrink): controls input droplet size during
overlap calculation. Frequently tuned based on data volume.
Oversample (xy_oversample / z_oversample): controls output grid resolution. Default = native SPHEREx resolution. Rarely changed.
- output_dir: Path = PosixPath('drizzle_output')
- effective_z_shrink() float[source]
Spectral droplet shrink factor (z_shrink if set, else xy_shrink).
- spatial_radius_deg() float[source]
Half-diagonal of the output region in degrees (for TAP INTERSECTS query).
- classmethod from_json(json_str: str) Drizzle3DConfig[source]
Deserialize config from JSON string.
- to_yaml_file(filepath: Path) None[source]
Save config to a YAML file with descriptive header comments.
- Parameters:
filepath (Path) – Output YAML file path. Parent directories are created if needed.
- classmethod from_yaml_file(filepath: Path) Drizzle3DConfig[source]
Load config from a YAML file.
- Parameters:
filepath (Path) – Path to the YAML config file.
- Raises:
FileNotFoundError – If the file does not exist.
ValueError – If the file does not contain a valid YAML dict.
- __init__(center_ra: float, center_dec: float, width: float, height: float, detector: int = 0, xy_shrink: float = 0.8, z_shrink: float | None = None, xy_oversample: float = 1.0, z_oversample: float = 1.0, z_lambda_edges: ~numpy.ndarray | None = None, mjd_range: ~typing.Tuple[float, float] | None = None, max_images: int = 500, download_workers: int = 4, skip_existing: bool = True, data_mirror: ~pathlib._local.Path | None = None, subtract_zodi: bool = True, static_zodi: bool = False, exclude_flags: ~typing.List[int] = <factory>, ivar_max: float = 10000000000.0, min_overlap: float = 0.0, output_dir: ~pathlib._local.Path = PosixPath('drizzle_output'), overwrite: bool = False) None
grid — Spatial WCS Construction
Output spatial WCS construction for Drizzle3D.
Builds a tangent-plane (TAN) projection centered on the user-specified (ra, dec) with pixel scale determined by xy_oversample.
- spxquery.drizzle3d.grid.build_output_wcs(config: Drizzle3DConfig) WCS[source]
Build output spatial WCS from Drizzle3DConfig.
Creates a 2-D gnomonic (TAN) projection centered on (center_ra, center_dec) with pixel scale = 6.15” / xy_oversample. The image extent matches (output_ny, output_nx) from the config.
- Parameters:
config (Drizzle3DConfig) – Drizzle configuration with center, size, and oversampling parameters.
- Returns:
2-D celestial WCS for the output spatial grid.
- Return type:
WCS
Notes
CRPIX is placed at the geometric center of the output image so that pixel (nx//2, ny//2) maps to (center_ra, center_dec).
spectral — Spectral Z Grid
Spectral axis (Z) grid construction for Drizzle3D.
Reads SPECTRAL_CHANNELS from the bundled aux FITS file and builds the wavelength bin grid (native, oversampled, or user-defined custom).
- class spxquery.drizzle3d.spectral.ZGrid(edges: ndarray, centers: ndarray, widths: ndarray)[source]
Bases:
objectSpectral axis grid descriptor for one detector.
- edges
(n_z+1,) bin edges [μm].
- Type:
np.ndarray
- centers
(n_z,) bin centers [μm].
- Type:
np.ndarray
- widths
(n_z,) bin widths Δλ [μm].
- Type:
np.ndarray
- spxquery.drizzle3d.spectral.load_spectral_table() DataFrame[source]
Load SPECTRAL_CHANNELS (HDU 2) from the bundled aux FITS file.
- Returns:
102 rows × (DETECTOR, SUBCHAN, WAVELENGTH, WL_MIN, WL_MAX, R, R_STD, BANDWIDTH).
- Return type:
pd.DataFrame
- spxquery.drizzle3d.spectral.detector_spectral_table(detector: int) DataFrame[source]
Get the 17-row spectral table for a single detector.
- Parameters:
detector (int) – Detector number 1–6.
- Returns:
17 rows sorted by WAVELENGTH.
- Return type:
pd.DataFrame
- spxquery.drizzle3d.spectral.build_z_grid_native(detector: int) ZGrid[source]
Build Z grid from native 17 subchannels of a detector.
Uses WL_MIN / WL_MAX from SPECTRAL_CHANNELS as bin edges. Adjacent subchannels may overlap; edges are taken directly from the table.
- spxquery.drizzle3d.spectral.build_z_grid_oversampled(detector: int, z_oversample: float) ZGrid[source]
Build oversampled Z grid with constant effective resolving power.
Algorithm
Compute mean R-bar across all 17 subchannels of the detector.
R_eff = R-bar / z_oversample (z_oversample=2 → double resolution).
Generate edges using constant-R logarithmic spacing: λ_{k+1} = λ_k * (1 + 1/R_eff)
Start from λ_min (first subchannel WL_MIN), stop at λ_max (last WL_MAX).
- spxquery.drizzle3d.spectral.build_z_grid_custom(lambda_edges: ndarray) ZGrid[source]
Build Z grid from user-provided bin edges [μm].
- Parameters:
lambda_edges (np.ndarray) – (n_z+1,) monotonically increasing bin edges in μm.
spatial — XY Mapping
Spatial (XY) drizzle kernel for Drizzle3D.
Computes the mapping from input image pixels to output grid pixels, including pixfrac shrinkage and fractional overlap areas.
Uses vectorised numpy operations for efficiency on full-frame images.
- spxquery.drizzle3d.spatial.compute_spatial_mapping(input_wcs: WCS, input_shape: Tuple[int, int], output_wcs: WCS, output_shape: Tuple[int, int], xy_shrink: float) Tuple[ndarray, ndarray, ndarray, ndarray, ndarray][source]
Compute spatial overlap mapping from input pixels to output grid.
For each input pixel, computes the output pixel(s) it contributes to and the fractional overlap area using bilinear weight distribution.
- Parameters:
input_wcs (WCS) – Input image spatial WCS.
input_shape (tuple of int) – (ny_in, nx_in) shape of the input image.
output_wcs (WCS) – Output grid spatial WCS (2-D tangent plane).
output_shape (tuple of int) – (ny_out, nx_out) shape of the output grid.
xy_shrink (float) – Droplet shrink factor (0, 1]. 1.0 = full pixel footprint.
- Returns:
valid_mask (np.ndarray (bool)) – (ny_in, nx_in) — True for pixels that land inside the output grid.
pixel_idx (np.ndarray (int64)) – Flat index into the input image for each output contribution. Use
rows, cols = np.unravel_index(pixel_idx, input_shape)to recover (y, x) coordinates.out_y (np.ndarray (int32)) – Output Y pixel indices.
out_x (np.ndarray (int32)) – Output X pixel indices.
f_xy (np.ndarray (float64)) – Spatial overlap fractions.
- spxquery.drizzle3d.spatial.compute_spectral_overlaps(lambda_c: float, delta_lambda: float, z_shrink: float, z_edges: ndarray, z_centers: ndarray, z_widths: ndarray) Tuple[ndarray, ndarray][source]
Compute spectral overlap fractions for a single input pixel.
Uses a top-hat (boxcar) kernel centered at lambda_c with width delta_lambda * z_shrink.
- Parameters:
lambda_c (float) – Input pixel central wavelength [μm].
delta_lambda (float) – Input pixel bandwidth [μm].
z_shrink (float) – Spectral droplet shrink factor.
z_edges (np.ndarray) – (n_z+1,) output Z bin edges [μm].
z_centers (np.ndarray) – (n_z,) output Z bin centers [μm].
z_widths (np.ndarray) – (n_z,) output Z bin widths [μm].
- Returns:
z_idx (np.ndarray (int32)) – Indices of overlapping Z bins.
f_z (np.ndarray (float64)) – Fractional overlap for each Z bin, summing to ~1.
accumulate — Drizzle Engine
Core accumulation for Drizzle3D.
Contains the DrizzleCube data container and the per-image accumulation loop that combines spatial + spectral overlaps into the output 3D cube.
- class spxquery.drizzle3d.accumulate.DrizzleCube(wcs: WCS, pixscale: float, zgrid: ZGrid, flux_weighted: ndarray, weight_total: ndarray, var_accum: ndarray, count_map: ndarray, and_mask: ndarray, or_mask: ndarray, config: Drizzle3DConfig, detector: int, n_inputs: int = 0, n_rejected: int = 0)[source]
Bases:
objectIn-memory accumulation buffers for one detector’s 3D drizzle.
All image arrays have shape (n_z, n_y, n_x) — FITS ordering with the spectral axis as the first dimension.
- config: Drizzle3DConfig
- classmethod create(wcs: WCS, pixscale: float, zgrid: ZGrid, config: Drizzle3DConfig, detector: int) DrizzleCube[source]
Allocate an empty DrizzleCube with zero-initialized arrays.
- spxquery.drizzle3d.accumulate.drizzle_image(cube: DrizzleCube, image: ndarray, variance: ndarray, flags: ndarray, lambda_c_map: ndarray, delta_lambda_map: ndarray, pixel_idx: ndarray, out_y: ndarray, out_x: ndarray, f_xy: ndarray, exclude_mask: ndarray | None = None) None[source]
Drizzle one input image into the cube (in-place accumulation).
- Parameters:
cube (DrizzleCube) – Target accumulation cube (modified in place).
image (np.ndarray) – (ny_in, nx_in) input flux in MJy/sr.
variance (np.ndarray) – (ny_in, nx_in) input per-pixel variance.
flags (np.ndarray) – (ny_in, nx_in) input pixel quality flags.
lambda_c_map (np.ndarray) – (ny_in, nx_in) per-pixel central wavelength [μm].
delta_lambda_map (np.ndarray) – (ny_in, nx_in) per-pixel bandwidth [μm].
pixel_idx (np.ndarray (int64)) – Flat input-pixel indices from compute_spatial_mapping. Each entry identifies which input pixel this spatial contribution belongs to.
out_y (np.ndarray (int32)) – Output pixel coordinates from compute_spatial_mapping.
out_x (np.ndarray (int32)) – Output pixel coordinates from compute_spatial_mapping.
f_xy (np.ndarray (float64)) – Spatial overlap fractions from compute_spatial_mapping.
exclude_mask (np.ndarray, optional) – (ny_in, nx_in) bool — True for pixels to exclude (flagged).
Notes
The spatial mapping arrays (pixel_idx, out_y, out_x, f_xy) may have multiple entries per input pixel (bilinear produces up to 4).
pixel_idxmaps each contribution back to its source input pixel.
pipeline — Orchestrator
High-level Drizzle3D pipeline.
Orchestrates: query → download → per-detector drizzle → save.
- spxquery.drizzle3d.pipeline.drizzle_detector(fits_paths: List[Path], config: Drizzle3DConfig, detector: int, output_wcs: WCS) Path | None[source]
Run the drizzle pipeline for one detector.
- Parameters:
fits_paths (list of Path) – Downloaded FITS files for this detector.
config (Drizzle3DConfig) – Drizzle configuration.
detector (int) – Detector number (1–6).
output_wcs (WCS) – Output spatial WCS.
- Returns:
Path to the output FITS file, or None if no valid inputs.
- Return type:
Path or None
- spxquery.drizzle3d.pipeline.drizzle(config: Drizzle3DConfig) Dict[int, Path][source]
Top-level entry point: query → download → drizzle → save.
- Parameters:
config (Drizzle3DConfig) – Complete drizzle configuration.
- Returns:
{detector_id: output_path} for each successfully processed detector.
- Return type:
Examples
>>> from spxquery.drizzle3d import Drizzle3DConfig, drizzle >>> config = Drizzle3DConfig( ... center_ra=186.4536, ... center_dec=33.5468, ... width=30.0, ... height=30.0, ... detector=3, ... ) >>> results = drizzle(config)
query — IRSA TAP Query & Download
IRSA TAP query for SPHEREx observations intersecting a sky region.
Delegates to spxquery’s shared TAP infrastructure in core.query, then groups results by detector for drizzle processing.
- class spxquery.drizzle3d.query.DrizzleObservation(obs_id: str, band: str, detector: int, mjd: float, download_url: str)[source]
Bases:
objectMinimal observation descriptor for drizzle downloads.
- spxquery.drizzle3d.query.query_observations(config: Drizzle3DConfig) Dict[int, List[DrizzleObservation]][source]
Query IRSA TAP for observations INTERSECTING the target sky region.
Delegates to
spxquery.core.query.query_spherex_observations()and converts the results into detector-groupedDrizzleObservationobjects.- Parameters:
config (Drizzle3DConfig) – Drizzle configuration with center, size, detector, and mjd_range.
- Returns:
{detector_id: [DrizzleObservation, …]} grouped by detector. Only detectors with results are included.
- Return type:
- spxquery.drizzle3d.query.download_observations(observations: List[DrizzleObservation], output_dir, max_workers: int = 4, skip_existing: bool = True, data_mirror: Path | None = None) List[source]
Resolve FITS file paths for a list of observations.
Two modes:
Mirror mode (
data_mirroris set): constructs local paths from the IRSA download URL by stripping/ibe/data/spherexand prependingdata_mirror. Verifies each file exists. No network access.Download mode (default): downloads FITS files from IRSA via HTTP, delegating to
spxquery.core.download.download_file().
- Parameters:
observations (list of DrizzleObservation) – Observations to resolve.
output_dir (Path) – Base directory; files go into
output_dir/images/D{N}/(download mode only).max_workers (int) – Parallel download threads (download mode only).
skip_existing (bool) – Skip already-downloaded files (download mode only).
data_mirror (Path, optional) – Local mirror root directory. When set, resolves paths from the mirror instead of downloading.
- Returns:
Paths to resolved FITS files.
- Return type:
list of Path
io — FITS I/O
FITS I/O for Drizzle3D output cubes.
- Writes and reads the 7-HDU format:
HDU 0 — PRIMARY (empty, with metadata headers) HDU 1 — SCI (float32, flux-weighted mean surface brightness) HDU 2 — VARIANCE (float32) HDU 3 — AND_MASK (uint32, conservative flag) HDU 4 — OR_MASK (uint32, inclusive flag) HDU 5 — COUNT (uint16, contribution count) HDU 6 — WAVELENGTH (BinTableHDU, spectral axis lookup)
- spxquery.drizzle3d.io.save_cube(cube: DrizzleCube, path: Path, overwrite: bool = False) None[source]
Write DrizzleCube to 7-HDU FITS.
- Parameters:
cube (DrizzleCube) – Completed accumulation cube.
path (Path) – Output FITS file path.
overwrite (bool) – Whether to overwrite an existing file.
Notes
After saving, the FITS file can be opened in DS9, CARTA, or any standard FITS viewer. The SCI HDU contains an approximate linear 3-D WCS for display; the exact per-plane wavelengths are in the WAVELENGTH table (HDU 6).
- spxquery.drizzle3d.io.load_cube(path) dict[source]
Load a drizzled cube from FITS for inspection.
Returns a dict with ‘sci’, ‘variance’, ‘and_mask’, ‘or_mask’, ‘count’, ‘wavelength’ (BinTable), and ‘header’ keys.
- Parameters:
path (Path) – Path to the drizzled FITS file.
- Returns:
Dictionary of HDU data and header.
- Return type: