def update_wcs(input_image, astrometry_data, output_image=None):
# Adds or updates the astrometry data in input_image from
# astrometry_data. If output_image is not specified then
# input_image is updated in-situ and resaved,
# otherwise the updated file is copied to output_image
# and input_image is unchanged.
#
# astrometry_data is assumed to be a minimal FITS file
# (e.g. as returned by astrometry.net after plate-solving).
#
# Mandatory FITS cards are ignored (don't want to change them).
# Where other keys match the existing card is updated.
# Where astrometry_data contains new keys those cards are added.
# All other existing cards in input_image are retained unchanged.
#
# Input: input_image String. File path/name of
# existing FITS file.
#
# astrometry_data String. File path/name of
# FITS file containing astrometry
# cards in hdu0 header.
#
# output_image Optional string. File path/name
# where updated file is to be copied.
#
# Output: Updated input_image file (if output_image not
# specified) or updated copy of input_image file
# saved to output_image (overwrites any existing).
from astropy.io import fits
image_file = fits.open(input_image)
astrometry_file = fits.open(astrometry_data)
new_cards = list([])
for card in astrometry_file[0].header.cards:
if (not(card[0] in ['SIMPLE','BITPIX','END']) and
not(card[0][:5] == 'NAXIS')):
new_cards.append(tuple(card))
image_file[0].header.update(new_cards)
if output_image is None:
image_file.writeto(input_image)
else:
image_file.writeto(output_image)
return