merge_headers

astro_metadata_translator.merge_headers(headers: Sequence[MutableMapping[str, Any]], mode: str = 'overwrite', sort: bool = False, first: Sequence[str] | None = None, last: Sequence[str] | None = None) MutableMapping[str, Any]

Merge multiple headers into a single dict.

Given a list of dict-like data headers, combine them following the specified mode.

Parameters:
headerslist of dict (or dict-like)

Collection of headers to combine. PropertyList is supported.

modestr

Scheme to use when a header has the same key as another header but different value. Options are:

  • 'overwrite' : Value in later header overwrites earlier value.

  • 'drop' : Entire key is dropped. If a key only appears in a subset of the headers, and is identical in those, it will be retained.

  • 'diff' : As for drop but the dropped values are stored in a list of dict in the returned merged header in key __DIFF__. The order used matches the supplied order or the sorted order if specified. This allows a simple header diff to be performed and associated with the original headers. Only keys that appear in all headers will be retained in the merged one. Unlike for 'drop' headers that are identical and only present in a subset will always be included in the diff.

  • 'first' : Retain first value encountered.

  • 'append' : Convert value to list with a value for each header (None if the key was not present). If the value is identical in multiple headers but key is missing in some, then the single identical header is stored.

sortbool, optional

If True, sort the supplied headers into date order if possible. This affects the resulting merged output depending on the requested merge mode. An attempt will be made to extract a date from the headers.

firstlist or tuple, optional

Keys to retain even if they differ. For all modes excepting append (where it is ignored) the value in the merged header will always be the value first encountered. This is usually to allow time-dependent headers such as DATE-OBS and AZSTART to be retained to allow the header to indicate the range of values. No exception is raised if a key can not be found in a header since this allows a range of expected headers to be listed covering multiple instruments.

lastlist or tuple, optional

Keys to retain even if they differ. For all modes excepting append (where it is ignored) the value in the merged header will always be the final value encountered. This is usually to allow time-dependent headers such as DATE-END and AZEND to be retained to allow the header to indicate the range of values. No exception is raised if a key can not be found in a header since this allows a range of expected headers to be listed covering multiple instruments.

Returns:
mergeddict

Single dict combining all the headers using the specified combination mode.

Notes

If first and last are supplied, the keys from first are handled first, followed by the keys from last. No check is made to ensure that the keys do not overlap.