def plate_solve(sources, image_width, image_height, radec=None, radius=1.0, scale_upper=10.0, scale_lower=0.01, api_key=None):
"""
Query astrometry.net with a list of point source positions and return a WCS solution. Source list should be sorted by brightness in descending order.
Add your astrometry.net API key to the astroquery.cfg file per these directions: Alternatively, manually supply your API key as an argument.
Parameters
----------
sources : QTable
Table of sources as produced by DAO Star Finder or similar
image_width : num
Width of frame.
image_height : num
Height of frame.
radec : (num, num), optional
Provide an initial guess for the RA & Dec coordinates for the center of the field.
radius: num, optional
Provide a radius in degrees for the uncertainty of the provided RA & Dec. Default is 1.0 deg.
scale_upper : num
The upper limit of the width of the field in degrees. Default is 10.0 deg.
scale_lower : num
The lower limit of the width of the field in degrees. Default is 0.01 deg.
api_key : None or string
If None, astroquery will try to use the API key in the config file. Otherwise will use the supplied string as the API key.
Returns
-------
WCS object
Astropy WCS object. False if plate solution is unsuccesful.
"""
ast = AstrometryNet()
if api_key is not None:
ast.api_key = api_key
if ast.api_key == "":
logger.
critical(
"No API key provided. Cannot query astrometry.net without an API key.")
return False
try_again = True
submission_id = None
while try_again:
try:
if not submission_id:
if radec is None:
logger.info("Querying Astrometry.net for plate solution...")
# Add 1 to coordinates as astrometry.net assumes fits image origin of 1,1 wcs_header = ast.solve_from_source_list(sources['xcentroid'].value + 1.0, sources['ycentroid'].value + 1.0,
image_width, image_height, crpix_center=True,
scale_units="degwidth", scale_type="ul", scale_upper=scale_upper, scale_lower=scale_lower,
positional_error=3, tweak_order=3,
publicly_visible='n', allow_commercial_use='y',
submission_id=submission_id
)
else:
logger.info("Querying Astrometry.net for plate solution...")
# Add 1 to coordinates as astrometry.net assumes fits image origin of 1,1 wcs_header = ast.solve_from_source_list(sources['xcentroid'].value + 1.0, sources['ycentroid'].value + 1.0,
image_width, image_height, crpix_center=True,
center_ra=radec[0], center_dec=radec[1], radius=radius,
scale_units="degwidth", scale_type="ul", scale_upper=scale_upper, scale_lower=scale_lower,
positional_error=3, tweak_order=3,
publicly_visible='n', allow_commercial_use='y',
submission_id=submission_id
)
else:
wcs_header = ast.monitor_submission(submission_id, solve_timeout=300)
except astroquery.exceptions.TimeoutError as e:
# Did not receive result before timeout
logger.warning(e)
logger.warning("Did not receive result from Astrometry.net before timeout. Will keep checking for solution.")
submission_id = e.args[1]
except Exception as e:
logger.error(e.__str__() + ", check API key.")
else:
logger.error(e)
return False
else:
# Received result
try_again = False
if wcs_header:
logger.success("Recieved succesful result from Astrometry.net!")
return WCS(wcs_header)
else:
logger.error("Failed to recieve succesful solution from Astrometry.net!")
return False