Skip to main content

Astrometry.net solver interface

Project description

Astrometry

Astrometry turns a list of star positions into a pixel-to-sky transformation (WCS) by calling C functions from the Astrometry.net library (https://astrometry.net).

Astrometry.net star index files ("series") are automatically downloaded when required.

This package is useful for solving plates from a Python script, comparing star extraction methods, or hosting a simple local version of Astrometry.net with minimal dependencies. See https://github.com/dam90/astrometry for a more complete self-hosting solution.

Unlike Astrometry.net, Astrometry does not include FITS parsing or image pre-processing algorithms. Stars must be provided as a list of pixel positions.

Linux and macOS are supported but Windows in not (at the moment).

We are not the authors of the Astrometry.net library. You should cite works from https://astrometry.net/biblio.html if you use the Astrometry.net algorithm via this package.

Get started

python3 -m pip install astrometry
import astrometry

solver = astrometry.Solver(
    astrometry.series_5200.index_files(
        cache_directory="astrometry_cache",
        scales={6},
    )
)

stars = [
    [388.9140568247906, 656.5003281719216],
    [732.9210858972549, 473.66395545775106],
    [401.03459504299843, 253.788113189415],
    [312.6591868096163, 624.7527729425295],
    [694.6844564647456, 606.8371776658344],
    [741.7233477959561, 344.41284826261443],
    [867.3574610200455, 672.014835980283],
    [1063.546651153479, 593.7844603550848],
    [286.69070190952704, 422.170016812049],
    [401.12779619355155, 16.13543616977013],
    [205.12103484692776, 698.1847350789413],
    [202.88444768690894, 111.24830187635557],
    [339.1627757703069, 86.60739435924549],
]

solution = solver.solve(
    stars_xs=[star[0] for star in stars],
    stars_ys=[star[1] for star in stars],
    size_hint=None,
    position_hint=None,
    solve_id=None,
    tune_up_logodds_threshold=14.0,
    output_logodds_threshold=21.0,
)

if solution.has_match():
    print(f"{solution.best_match().center_ra_deg=}")
    print(f"{solution.best_match().center_dec_deg=}")
    print(f"{solution.best_match().scale_arcsec_per_pixel=}")

solve is thread-safe. It can be called any number of times from the same Solver object.

Examples

Provide size and position hints

import astrometry

solver = ...
solution = solver.solve(
    stars_xs=...,
    stars_ys=...,
    size_hint=astrometry.SizeHint(
        lower_arcsec_per_pixel=1.0,
        upper_arcsec_per_pixel=2.0,
    ),
    position_hint=astrometry.PositionHint(
        ra_deg=65.7,
        dec_deg=36.2,
        radius_deg=1.0,
    ),
    solve_id=...,
    tune_up_logodds_threshold=...,
    output_logodds_threshold=...,
)

Print progress information (download and solve)

import astrometry
import logging

logging.getLogger().setLevel(logging.INFO)

solver = ...
solution = ...

Print field stars metadata

Astrometry extracts metadata from the star index ("series"). See Series for a description of the available data.

import astrometry

solver = ...
solution = ...

if solution.has_match():
    for star in solution.best_match().stars:
        print(f"{star.ra_deg}º, {star.dec_deg}º:", star.metadata)

Calculate field stars pixel positions with astropy

import astrometry
import astropy.wcs

solver = ...
solution = ...

if solution.has_match():
    wcs = astropy.wcs.WCS(solution.best_match().wcs_fields)
    pixels = wcs.all_world2pix(
        [[star.ra_deg, star.dec_deg] for star in solution.best_match().stars],
        0,
    )
    # pixels is a len(solution.best_match().stars) x 2 numpy array of float values

astropy.wcs.WCS provides many more functions to probe the transformation properties and convert from and to pixel coordinates. See https://docs.astropy.org/en/stable/api/astropy.wcs.WCS.html for details.

Print series description and size (without downloading them)

import astrometry

print(astrometry.series_5200_heavy.description)
print(astrometry.series_5200_heavy.size_as_string({2, 3, 4}))

See Series for a list of available series.

Documentation

Solver

class Solver:
    def __init__(self, index_files: list[pathlib.Path]): ...

    def solve(
        self,
        stars_xs: typing.Iterable[float],
        stars_ys: typing.Iterable[float],
        size_hint: typing.Optional[SizeHint],
        position_hint: typing.Optional[PositionHint],
        solve_id: typing.Optional[str],
        tune_up_logodds_threshold: float,
        output_logodds_threshold: float,
    ) -> Solution: ...

solve is thread-safe and can be called any number of times.

  • index_files: List of index files to use for solving. The list need not come from a Series object. Series subsets and combinations are possible as well.
  • star_xs: First pixel coordinate of the input stars.
  • star_ys: Second pixel coordinate of the input stars, must have the same length as star_xs.
  • size_hint: Optional angular pixel size range (SizeHint). Significantly speeds up solve when provided. If size_hint is None, the range [0.1, 1000.0] is used. This default range can be changed by setting astrometry.DEFAULT_LOWER_ARCSEC_PER_PIXEL and astrometry.DEFAULT_UPPER_ARCSEC_PER_PIXEL to other values.
  • position_hint: Optional field center Ra/Dec coordinates and error radius (PositionHint). Significantly speeds up solve when provided. If position_hint is None, the entire sky is used (radius_deg = 180.0).
  • solve_id: Optional plate identifier used in logging messages. If solve_id is None, it is automatically assigned to a unique integer. The value can be retrieved from the Solution object (solution.solve_id).
  • tune_up_logodds_threshold: Matches whose log-odds are larger than this value are tuned-up (SIP distortion estimation) and accepted if heir post-tune-up log-odds are larger than output_logodds_threshold. The default Astrometry.net value is math.log(1e6).
  • output_logodds_threshold: Matches whose log-odds are larger than this value are immediately accepted (added to the solution matches). The default Astrometry.net value is math.log(1e9).

Accepted matches are always tuned up, even if they hit tune_up_logodds_threshold and were already tuned-up. Since log-odds are compared with the thresholds before the tune-up, the final log-odds are often significantly larger than output_logodds_threshold.

Set tune_up_logodds_threshold to a value larger than or equal to output_logodds_threshold to disable the first tune-up.

Astrometry.net gives the following description of the tune-up algorithm. See tweak2 in astrometry.net/solver/tweak2.c for the implementation.

Given an initial WCS solution, compute SIP polynomial distortions using an annealing-like strategy. That is, it finds matches between image and reference catalog by searching within a radius, and that radius is small near a region of confidence, and grows as you move away. That makes it possible to pick up more distant matches, but they are downweighted in the fit. The annealing process reduces the slope of the growth of the matching radius with respect to the distance from the region of confidence.

-- astrometry.net/include/astrometry/tweak2.h

SizeHint

@dataclasses.dataclass
class SizeHint:
    lower_arcsec_per_pixel: float
    upper_arcsec_per_pixel: float

lower_arcsec_per_pixel and upper_arcsec_per_pixel must be larger than 0 and upper_arcsec_per_pixel must be smaller than or equal to upper_arcsec_per_pixel.

PositionHint

@dataclasses.dataclass
class PositionHint:
    ra_deg: float
    dec_deg: float
    radius_deg: float
  • ra_deg must be in the range [0.0, 360.0[.
  • dec_deg must be in the range [-90.0, 90.0].
  • radius must be larger than or equal to zero.

All values are in degrees and must use the same frame of reference as the index files. Astrometry.net index files use J2000 FK5 (https://docs.astropy.org/en/stable/api/astropy.coordinates.FK5.html). ICRS and FK5 differ by less than 0.1 arcsec (https://www.iers.org/IERS/EN/Science/ICRS/ICRS.html).

Solution

@dataclasses.dataclass
class Solution:
    solve_id: str
    matches: list[Match]

    def has_match(self) -> bool: ...

    def best_match(self) -> Match: ...

matches are sorted in descending log-odds order. best_match returns the first match in the list.

Match

@dataclasses.dataclass
class Match:
    logodds: float
    center_ra_deg: float
    center_dec_deg: float
    scale_arcsec_per_pixel: float
    index_path: pathlib.Path
    stars: tuple[Star, ...]
    quad_stars: tuple[Star, ...]
    wcs_fields: dict[str, tuple[typing.Any, str]]
  • logodds: Log-odds (https://en.wikipedia.org/wiki/Logit) of the match.
  • center_ra_deg: Right ascension of the stars bounding box's center, in degrees and in the frame of reference of the index (J200 FK5 for Astrometry.net series).
  • center_dec_deg: Declination of the stars bounding box's center in degrees and in the frame of reference of the index (J200 FK5 for Astrometry.net series).
  • scale_arcsec_per_pixel: Pixel scale in arcsec per pixel.
  • index_path: File system path of the index file used for this match.
  • stars: List of visible index stars. This list is almost certainly going to differ from the input stars list.
  • quad_stars: The index stars subset (usually 4 but can be 3 or 5) used in the hash code search step (see https://arxiv.org/pdf/0910.2233.pdf, 2. Methods).
  • wcs_fields: WCS fields describing the transformation between pixel coordinates and world coordinates. This dictionary can be passed directly to astropy.wcs.WCS.

Star

@dataclasses.dataclass
class Star:
    ra_deg: float
    dec_deg: float
    metadata: dict[str, typing.Any]

ra_deg and dec_deg are in degrees and use the same frame of reference as the index files. Astrometry.net index files use J2000 FK5 (https://docs.astropy.org/en/stable/api/astropy.coordinates.FK5.html). ICRS and FK5 differ by less than 0.1 arcsec (https://www.iers.org/IERS/EN/Science/ICRS/ICRS.html).

The contents of metadata depend on the data available in index files. See Series for details.

Series

@dataclasses.dataclass
class Series:
    name: str
    description: str
    scale_to_sizes: dict[int, tuple[int, ...]]
    url_pattern: str

    def size(self, scales: typing.Optional[set[int]] = None): ...

    def size_as_string(self, scales: typing.Optional[set[int]] = None): ...

    def index_files(
        self,
        cache_directory: typing.Union[bytes, str, os.PathLike],
        scales: typing.Optional[set[int]] = None,
    ) -> list[pathlib.Path]: ...
  • name defines the cache subdirectory name.
  • description is a copy of the text description in http://data.astrometry.net.
  • scale_to_sizes maps each available HEALPix resolution to index files sizes in bytes. The smaller the scale, the larger the number of HEALPix subdivisions.
  • url_pattern is the base pattern used to generate file download links.
  • size returns the cumulative file sizes for the given scales in bytes. If scales is None, all the scales available for the series are used.
  • size_as_string returns a human-readable string representation of size.
  • index_files returns index files paths for the given scales (or all available scales if scales is None). This function downloads files that are not already in the cache directory. cache_directory is created if it does not exist. Download automatically resumes for partially downloaded files.

Change the constants astrometry.CHUNK_SIZE, astrometry.DOWNLOAD_SUFFIX and astrometry.TIMEOUT to configure the downloader parameters.

The table below lists series sizes and properties (descriptions from http://data.astrometry.net). You can access a series' object with astrometry.series_{name} (for example astrometry.series_4200).

Name Total size Scales Description Metadata
4100 0.36 GB [7, 19] built from the Tycho-2 catalog, good for images wider than 1 degree, recommended MAG_BT: float
MAG_VT: float
MAG_HP: float
MAG: float
4200 33.78 GB [0, 19] built from the near-infared 2MASS survey, runs out of stars at the low end, most users will probably prefer 4100 or 5200 j_mag: float
5000 76.24 GB [0, 7] an older version from Gaia-DR2 but without Tycho-2 stars merged in, our belief is that series_5200 will work better than this one source_id: int
phot_g_mean_mag: float
phot_bp_mean_mag: float
phot_rp_mean_mag: float
parallax: float
parallax_error: float
pmra: float
pmra_error: float
pmdec: float
pmdec_error: float
ra: float
dec: float
ref_epoch: float
5200 36.14 GB [0, 6] LIGHT version built from Tycho-2 + Gaia-DR2, good for images narrower than 1 degree, combine with 4100-series for broader scale coverage, the LIGHT version contains smaller files with no additional Gaia-DR2 information tagged along, recommended -
5200_heavy 79.67 GB [0, 6] HEAVY version same as 5200, but with additional Gaia-DR2 information (magnitude in G, BP, RP, proper motions and parallaxes), handy if you want that extra Gaia information for matched stars ra: float
dec: float
mag: float
ref_cat: str
ref_id: int
pmra: float
pmdec: float
parallax: float
ra_ivar: float
dec_ivar: float
pmra_ivar: float
pmdec_ivar: float
parallax_ivar: float
phot_bp_mean_mag: float
phot_rp_mean_mag: float
6000 1.20 GB [4, 6] very specialized, uses GALEX Near-UV measurements, and only a narrow range of scales fuv_mag: float
nuv_mag: float
6100 1.58 GB [4, 6] very specialized, uses GALEX Far-UV measurements, and only a narrow range of scales fuv_mag: float
nuv_mag: float

The table below indicates the total file size for each scale (most series have multiple index files per scale).

Name 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
4100 - - - - - - - 165.00 MB 94.55 MB 49.77 MB 24.87 MB 10.21 MB 5.30 MB 2.73 MB 1.38 MB 740.16 kB 408.96 kB 247.68 kB 187.20 kB 144.00 kB
4200 14.22 GB 9.25 GB 5.06 GB 2.63 GB 1.31 GB 659.09 MB 328.25 MB 165.44 MB 81.84 MB 41.18 MB 20.52 MB 8.02 MB 4.17 MB 2.16 MB 1.10 MB 596.16 kB 339.84 kB 213.12 kB 164.16 kB 132.48 kB
5000 34.79 GB 20.19 GB 10.74 GB 5.44 GB 2.71 GB 1.36 GB 676.79 MB 340.73 MB - - - - - - - - - - - -
5200 17.20 GB 9.49 GB 4.86 GB 2.45 GB 1.22 GB 614.89 MB 307.72 MB - - - - - - - - - - - - -
5200_heavy 36.46 GB 21.20 GB 11.29 GB 5.72 GB 2.85 GB 1.43 GB 714.56 MB - - - - - - - - - - - - -
6000 - - - - 892.55 MB 457.66 MB 233.23 MB - - - - - - - - - - - - -
6100 - - - - 599.33 MB 384.09 MB 214.79 MB - - - - - - - - - - - - -

Contribute

Clone this repository and pull its submodule:

git clone --recursive https://github.com/neuromorphicsystems/astrometry.git
cd astrometry

or

git clone https://github.com/neuromorphicsystems/astrometry.git
cd astrometry
git submodule update --recursive

Format the code:

clang-format -i astrometry_extension/astrometry_extension.c

Build a local version:

python3 -m pip install -e .
# use 'CC="ccache clang" python3 -m pip install -e .' to speed up incremental builds

Publish

  1. Bump the version number in setup.py.

  2. Install Cubuzoa in a different directory (https://github.com/neuromorphicsystems/cubuzoa) to build pre-compiled versions for all major operating systems. Cubuzoa depends on VirtualBox (with its extension pack) and requires about 75 GB of free disk space.

cd cubuzoa
python3 -m cubuzoa provision --os '(linux|macos)'
python3 -m cubuzoa build --os '(linux|macos)' --pre /path/to/astrometry/prebuild.py /path/to/astrometry
  1. Install twine
python3 -m pip install twine
  1. Upload the compiled wheels and the source code to PyPI:
python3 prebuild.py
python3 setup.py sdist --dist-dir wheels
python3 -m twine upload wheels/*

MSVC compatibility (work in progress)

  • fitsbin.c, kdtree_internal.c, kdtree_internal_fits.c, solver.c: replace Variable Length Arrays (VAL) with _alloca (type name[size] -> type* name = _alloca(size))
  • fitsbin.c, fitsioutils.c, fitstable.c: cast void* to char* to enable pointer arithmetic
  • anqfits.c, verify.c: #define debug(args...) -> #define debug(...)
  • qfits_time.c: remove #include <pwd.h>
  • log.h, keywords.h: remove __attribute__ directives
  • bl.c: remove redundant bl.inc and bl_nl.c includes
  • replace all POSIX calls (file IO, network, select...). This requires significant effort when targeting the entire Astrometry.net library. It might be less complicated with Astrometry, which uses only a subset of Astrometry.net.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

astrometry-1.0.0.tar.gz (482.6 kB view hashes)

Uploaded Source

Built Distributions

astrometry-1.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (541.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

astrometry-1.0.0-cp39-cp39-macosx_11_0_x86_64.whl (609.6 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ x86-64

astrometry-1.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (541.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

astrometry-1.0.0-cp38-cp38-macosx_11_0_x86_64.whl (609.6 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ x86-64

astrometry-1.0.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (540.7 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

astrometry-1.0.0-cp37-cp37m-macosx_11_0_x86_64.whl (609.5 kB view hashes)

Uploaded CPython 3.7m macOS 11.0+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page