fitsmgmt.header.cmt2hdr
fitsmgmt.header.cmt2hdr(
header,
histcomm,
s,
precision=3,
time_fmt='{:.>72s}',
t_ref=None,
dt_fmt='(dt = {:.3f} s)',
set_kw=None,
verbose=False,
)Add HISTORY/COMMENT entries, optionally with a timestamp.
Parameters
header :~astropy.io.fits.Header-
The header.
histcomm :strin ['h', 'hist', 'history', 'c', 'comm', 'comment']-
Whether to add history or comment.
s :strorlistofstr-
The string to add as history or comment.
precision :int, optional. = 3-
The precision of the isot format time. Default:
3. time_fmt :str,None, optional. = '{:.>72s}'-
The Python 3 format string to format the time in the header. If
Examples::None, the timestamp string will not be added."{:s}": plain time2020-01-01T01:01:01.23"({:s})": plain time in().(2020-01-01T01:01:01.23)"{:_^72s}": center align, filling with _. Default:'{:.>72s}'.
t_ref :~astropy.time.Time, optional. = None-
The reference time. If not
None, delta time is calculated. Default:None. dt_fmt :str, optional. = '(dt = {:.3f} s)'-
The Python 3 format string to format the delta time in the header. Default:
'(dt = {:.3f} s)'. verbose :bool, optional. = False-
Whether to log the same information. Default:
False. set_kw :dict, optional. = None-
The keyword arguments added to
~astropy.io.fits.Header.set(). Default is{'after': -1}, i.e., the history or comment will be appended to the very last part of the header.
Notes
The timing benchmark for a reasonably long header (len(ccd.header.cards) = 197) shows dt ~ 0.2-0.3 ms on MBP 15” [2018, macOS 11.6, i7-8850H (2.6 GHz; 6-core), RAM 16 GB (2400MHz DDR4), Radeon Pro 560X (4GB)]:
%timeit ccd.header.copy() 1.67 ms +/- 33.3 µs per loop (mean +/- std. dev. of 7 runs, 1000 loops each) %timeit air.cmt2hdr(ccd.header.copy(), ‘h’, ‘test’) 1.89 ms +/- 141 µs per loop (mean +/- std. dev. of 7 runs, 1000 loops each) %timeit air.cmt2hdr(ccd.header.copy(), ‘hist’, ‘test’) 1.89 ms +/- 144 µs per loop (mean +/- std. dev. of 7 runs, 1000 loops each) %timeit air.cmt2hdr(ccd.header.copy(), ‘histORy’, ‘test’) 1.95 ms +/- 146 µs per loop (mean +/- std. dev. of 7 runs, 100 loops each)
Same benchmark on MBP 14” [2024, macOS 26.4.1, M4Pro(8P+4E/G20c/N16c/48G)], 2026-05-27:
hdr = fits.Header()
hdr["SIMPLE"] = True
hdr["BITPIX"] = 8
hdr["NAXIS"] = 0
hdr["EXPTIME"] = 20
for i in range(193):
hdr[f"H{i:03d}"] = i
%timeit hdr.copy()
# 224 µs ± 654 ns per loop (7 runs, 500 loops each)
%timeit air.cmt2hdr(hdr.copy(), "h", "test")
# 396 µs ± 10.2 µs per loop (7 runs, 200 loops each)
%timeit air.cmt2hdr(hdr.copy(), "hist", "test")
# 394 µs ± 5.17 µs per loop (7 runs, 200 loops each)
%timeit air.cmt2hdr(hdr.copy(), "histORy", "test")
# 397 µs ± 3.35 µs per loop (7 runs, 200 loops each)