Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
def _insert_in_metadata_fits_safe(meta, key, value):
"""Helper function to insert key-value pair into metadata in a way that
FITS can serialize
key : str
Key to be inserted in the dictionary
value : str or None
Value to be inserted
This addresses a shortcoming of the FITS standard. There are length
restrictions on both the ``key`` (8 characters) and ``value`` (72
characters) in the FITS standard. There is a convention for handling
long keywords and a convention for handling long values, but the
two conventions cannot be used at the same time.
This addresses that case by checking the length of the ``key`` and
``value`` and, if necessary, shortening the key.
This helper method is taken from `astropy.nddata.cddata`. Not imported
as this is a private method, subject to change
if len(key) > 8 and len(value) > 72:
short_name = key[:8]
meta['HIERARCH {0}'.format(key.upper())] = (short_name, "Shortened name for {}".format(key))
meta[short_name] = value
meta[key] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment