From bf57afb121e682a60190486a5622135604c8c3a0 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sun, 26 Feb 2017 12:52:19 +0800 Subject: acispy/header.py: Add 'read_keyword2()' The 'read_keyword2()' uses 'astropy.io.fits' to manipulate the FITS header, and can access the raw/reserved FITS keywords that 'dmkeypar' cannot read. --- acispy/header.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/acispy/header.py b/acispy/header.py index 0614cd6..6aa0ead 100644 --- a/acispy/header.py +++ b/acispy/header.py @@ -3,16 +3,25 @@ """ Manipulate the FITS header keywords using CIAO tools -``dmkeypar`` and ``dmhedit``. +``dmkeypar`` and ``dmhedit``, as well as ``astropy.io.fits``. """ import subprocess +from astropy.io import fits + def read_keyword(infile, keyword): """ - Read the specified header keyword, and return a dictionary - with its value, unit, data type, and comment. + Read the specified header keyword using CIAO tool ``dmkeypar``, + and return a dictionary with its value, unit, data type, and comment. + + NOTE + ---- + The ``dmkeypar`` tool cannot read some raw/reserved keywords in FITS + header, e.g., ``BUNIT``. These raw header keywords can be obtained + using ``dmlist opt=header,raw``, or using the following + ``read_keyword2()``. """ DATATYPES = { "real": float, @@ -41,6 +50,25 @@ def read_keyword(infile, keyword): "unit": unit, "comment": comment} +def read_keyword2(infile, keyword): + """ + Read the specified header keyword using ``astropy.io.fits`` + and return a tuple of ``(value, comment)``. + + NOTE + ---- + Header of all extensions (a.k.a. blocks) are combined to locate + the keyword. + """ + with fits.open(infile) as f: + h = fits.header.Header() + for hdu in f: + h.extend(hdu.header) + value = h[keyword] + comment = h.comments[keyword] + return (value, comment) + + def write_keyword(infile, keyword, value, datatype=None, unit=None, comment=None): """ -- cgit v1.2.2