ymir
contribAstronomy for OCaml, built on Nx. Ymir gives tensors
astronomical meaning: exact units and quantities, celestial frames and
directions, FITS files, maps from pixels to the sky, aperture photometry on
images, the background cosmology, and HEALPix maps with their spherical
harmonics. Every function is a formula of nx operations, or for HEALPix's
Legendre sums one Rune.scan, so rune batches, compiles and differentiates
it with no rule of its own.
Quick start
open Ymir let () = (* Ages of a Planck 2018 universe at four redshifts, in gigayears. *) let planck = Cosmology.planck2018 ~codata:Codata.v2022 Nx.float64 in let z = Nx.create Nx.float64 [| 4 |] [| 0.; 1.; 3.; 1100. |] in let gyr = Unit.giga Units.julian_year in Nx.print (Quantity.value gyr (Cosmology.age planck z)); (* The angle between two directions, in arcseconds. *) let deg x = Quantity.v Unit.degree (Nx.scalar Nx.float64 x) in let a = Direction.lonlat Frame.icrs ~lon:(deg 10.) ~lat:(deg 20.) in let b = Direction.lonlat Frame.icrs ~lon:(deg 10.001) ~lat:(deg 20.) in Nx.print (Quantity.value Unit.arcsecond (Direction.separation a b))
The examples teach each part on small synthetic data.
Libraries
ymiris the astronomy;open Ymirbrings every module below exceptFitsinto scope.ymir.unitsholds the units alone, for code that needs no astronomy.ymir.fitsreads and writes FITS files;open Ymir_fitsbringsFits. Neitherymirnorymir.fitsdepends on the other, and a program that reads files links both.ymir.spkreads SPK ephemeris files, such as JPL's DE440, asYmir_spk.
What's inside
- Units:
Unitis a unit as an exact value, a product of primes, π and named symbols with rational exponents, with one canonical text. A conversion rounds the exact factor once, or raises when the units don't convert.Quantityis a tensor in a unit. The SI's units, prefixes and exact constants are built in,Codata.v2018andCodata.v2022hold the measured constants, andVocabularyspells units with symbols such asMJy sr^-1. - Time scales:
Timeholds batches of instants on UTC, TAI, TT and TDB, the scale in the type, and converts between them: leap seconds and UTC before 1972 from the IERS's tables, TDB at the geocentre from the Fairhead & Bretagnon series. - The Earth:
Earthholds the Earth's orientation at instants, its rotation angle and sidereal times under the IAU 2006/2000A precession-nutation, and its position and velocity from SOFA's analytic ephemeris. - Frames and directions:
Framenames ICRS, FK5 J2000, Galactic, the J2000 ecliptic and supergalactic, and the geocentric GCRS, CIRS, true equator and equinox of date and TIRS; a frame mismatch is a type error.Directionholds batches of directions with their separations, position angles and rotations between frames, and converts them between the barycentre and the geocentre through light deflection and aberration. - HEALPix:
Healpixdivides a frame's sphere into equal-area pixels atnsideup to 2^29, numbered RING or NESTED: the pixel of each direction, pixel centres, corners and neighbours, and spherical harmonic analysis and synthesis of maps, with Jacobi iterations. - Transforms:
Transformmaps pixels to the sky as a list of stages that compose with>>, invert, and print back what they read: axis order, shift, linear maps, SIP and TPV distortions, thirteen zenithal and cylindrical projections, and rotations between frames. - Grids, regions and observations:
Gridis an image's cells seen through a transform, with each cell's exact area or solid angle;Grid.agreesays whether two grids are the same cells.Regionplaces circles, annuli, ellipses and polygons on a grid and weighs each cell by its exact covered fraction.Observationholds data with variance and validity;add,subandscalecombine observations on agreeing grids with their variances, andintegratesums one over a region: aperture photometry, differentiable in the aperture's centre and size. - FITS:
ymir.fits'sFitsreads and writes headers, images (tile compression included), binary and ASCII tables, andBUNITandTUNITunits.Wcsreads a header's world coordinates as aTransformand writes one back as keyword edits; a program composes an image's data, error and world coordinates into anObservation. - Cosmology:
Cosmology.tis one record of tensors for flat and curved ΛCDM, wCDM and w0waCDM with radiation and massive neutrinos. Expansion rate, density parameters, distances, volumes and times are fixed Gauss–Legendre sums with a stated error bound (2⁻⁴⁶ relative in float64), batched over models and redshifts. The Planck and WMAP fits are built in.
Validation
Two results on public data reproduce published ones. They download their
data, so they run outside runtest; runtest checks the same paths on
bundled cutouts and simulated data.
test/nircam: aperture photometry on a JWST NIRCam mosaic agrees with photutils within 10⁻⁶ of the sums, with gradients in the aperture's centre and radius checked against finite differences.test/pantheon: fitting Ω_m and Ω_Λ to the Pantheon+ and SH0ES supernovae with their full covariance reproduces Brout et al. (2022), Table 3.