src/core/imexam.js
Image examination, as IRAF's imexam and DS9's analysis tools do it: radial profiles with Gaussian and Moffat fits, curves of growth and half-flux diameters, line cuts, region statistics, a pixel table, the focus across the field (FWHM, ellipticity and elongation direction of every star, mapped and summarised), and a ranking of comparison stars for differential photometry. No DOM: runs in Node and in the photometry page. Pixel coordinates are zero-based with the centre of the first pixel at (0, 0) and y up, as in photometry.js.
Import: import { … } from './src/core/imexam.js' — no DOM, runs in Node and the browser.
lmFit
function lmFit(f, x, y, p0, { weights = null, clamp = null, maxIter = 200 } = {})Least squares fit of y ~ f(p, x) by Levenberg-Marquardt. clamp(p) keeps the parameters physical.
- returns { p, chi2, iters, ok }
saturationLevel
function saturationLevel(H = {})The level at which a frame saturates: SATURATE or DATAMAX from the header when present, else the largest value the integer format can hold (BITPIX 16 with BZERO 32768 is 65535). Floating-point frames without a keyword: Infinity.
- returns { level, source }
radialProfile
function radialProfile(data, w, h, x0, y0, opts = {})Radial profile of the star near (x0, y0). The centre is refined with centroidIter unless opts.recentre is false. The sky is the sigma-clipped median of the annulus [rmax + 2, rmax + 2 + skyWidth) unless opts.background is given. The binned profile is the mean surface brightness in annuli of width bin from the centre, with each pixel weighted by its exact overlap with the annulus (photutils RadialProfile, method 'exact'); the curve of growth is the sky-subtracted sum inside circles of the radii every growthStep px (photutils CurveOfGrowth). A Gaussian a exp(-r^2 / 2 s^2) and a Moffat a (1 + r^2/g^2)^-beta are fitted by least squares to every pixel inside rmax (gaussian, moffat) and to the binned profile (binned, as photutils' gaussian_fit does).
- param opts { rmax = 12, bin = 1, growthStep = 0.25, recentre = true, background, skyWidth = 6, gain = 1, saturation = Infinity, half }
- returns { x, y, background, backgroundSigma, pixels: { r, v } (every pixel inside rmax, sky subtracted), profile: { r (bin centres), edges, v, area }, growth: { r, flux }, gaussian: { amp, sigma, fwhm }, moffat: { amp, gamma, beta, fwhm }, binned: { gaussian, moffat }, hfd, r50, r80, flux, peak, saturated, snr, ok }
bilinear
function bilinear(data, w, h, x, y)Bilinear interpolation at (x, y); NaN outside the image.
lineProfile
function lineProfile(data, w, h, x0, y0, x1, y1, step = 0.5)Values along the segment (x0, y0) -> (x1, y1), sampled every `step` px (imexam 'l' and 'c' along any direction).regionStats
function regionStats(data, w, h, reg)Statistics of the pixels whose centres are inside a box (shape 'box': x0, y0, x1, y1 corners) or a circle (shape 'circle': x0, y0 centre, r radius): count, sum, mean, median, standard deviation, min, max, and the 3-sigma clipped median, mean and standard deviation (astropy sigma_clipped_stats). NaN pixels are counted apart.
pixelTable
function pixelTable(data, w, h, x, y, half = 2)The (2 half + 1)^2 pixel values around the pixel nearest (x, y), as DS9's pixel table; rows from the top (y high) down.
psfShape
function psfShape(fit)FWHM, ellipticity and direction of the major axis from fitPsf's rotated Gaussian (sx along the rotated x axis).
fitPsf's rotation: rx = dx cos(th) - dy sin(th), so the sx axis points at angle -th from +x.- returns { fwhm (geometric mean), fwhmMajor, fwhmMinor, ellipticity 1 - b/a, angle of the major axis in degrees counter-clockwise from +x (y up), in [0, 180) }
focusMap
function focusMap(data, w, h, opts = {})Measure every usable star of a frame and describe the focus across the field, as CCDInspector-style tools do. Stars are found with detectSources; saturated stars, stars with a neighbour within 2.5 box half-widths and failed fits are dropped. Each star is fitted with a rotated Gaussian (fitPsf, two passes: the second with a box scaled to the median width). The field is divided into nx x ny cells (each with the median FWHM, ellipticity and the mean elongation direction), and a quadratic surface FWHM(x, y) is fitted with one 3-sigma rejection. Elongation pattern: <cos 2(angle - radial direction)> weighted by ellipticity is +1 for stars stretched along the radius from the centre, -1 for tangential stretching (both point to optics: field curvature, coma, astigmatism, tilt), while a large |<exp(2 i angle)>| means all stars point the same way (tracking or guiding, wind shake).
- param opts { nx = 3, ny = 3, max = 250, nsigma = 8, saturation = Infinity, pixelScale (arcsec/px, for the summary) }
- returns { stars: [{ x, y, fwhm, fwhmMajor, fwhmMinor, ellipticity, angle, peak, flux }], cells: [{ ix, iy, x0, x1, y0, y1, n, fwhm, ellipticity, angle, coherence }], surface (polySurface result or null), summary: { n, fwhm (median), fwhmCentre, fwhmCorners [lower left, lower right, upper left, upper right], curvature, tilt, worstCorner, best: {x, y} or null, ellipticity (median), radial, coherence, angle, verdict: [] } }
rankComparisons
function rankComparisons(sources, target, opts = {})Rank the detected stars as comparison stars for a target: similar brightness (|ln flux ratio|), unsaturated, isolated (no neighbour brighter than 10% of the star within crowd FWHM), far enough from the edge for the sky annulus, and with a round, normal-width profile when shapes are given (a galaxy or a blend is wider than the PSF).
- param sources [{ x, y, flux, peak, saturated, fwhm? }] (detectSources output, optionally with psfShape fields)
- param target { x, y, flux }
- param opts { w, h, fwhm = 4, edge (px, default 4 FWHM), crowd = 4, saturation = Infinity, maxRatio = 10 }
- returns [{ ...source, ratio, isolation (nearest bright neighbour, FWHM), score (lower is better), usable, notes: [] }] sorted best first; the target itself is excluded