Skip to content

Instantly share code, notes, and snippets.

@yashrsharma44
Created May 11, 2019 16:03
Show Gist options
  • Save yashrsharma44/dd42cd913a98efc463c93b573e1868e1 to your computer and use it in GitHub Desktop.
Save yashrsharma44/dd42cd913a98efc463c93b573e1868e1 to your computer and use it in GitHub Desktop.
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
Parameters
----------
key : str
Key to be inserted in the dictionary
value : str or None
Value to be inserted
Notes
-----
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.
Reference
---------
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
else:
meta[key] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment