aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2017-02-20 20:04:58 +0800
committerAaron LI <aaronly.me@outlook.com>2017-02-20 20:12:54 +0800
commite211dd31667551d2123c6788b29d7cea04da3c50 (patch)
tree776f0d3ea7e3f226d7d1136e90e2219d74003b7b
parent64562a847da6adff7ca8e8f64d6cdac73d5c412b (diff)
downloadchandra-acis-analysis-e211dd31667551d2123c6788b29d7cea04da3c50.tar.bz2
Add acispy/header.py to read/write/copy FITS header keywords
-rw-r--r--acispy/header.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/acispy/header.py b/acispy/header.py
new file mode 100644
index 0000000..0614cd6
--- /dev/null
+++ b/acispy/header.py
@@ -0,0 +1,77 @@
+# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
+# MIT license
+
+"""
+Manipulate the FITS header keywords using CIAO tools
+``dmkeypar`` and ``dmhedit``.
+"""
+
+import subprocess
+
+
+def read_keyword(infile, keyword):
+ """
+ Read the specified header keyword, and return a dictionary
+ with its value, unit, data type, and comment.
+ """
+ DATATYPES = {
+ "real": float,
+ "integer": int,
+ "boolean": bool,
+ "string": str
+ }
+ subprocess.check_call(["punlearn", "dmkeypar"])
+ subprocess.check_call([
+ "dmkeypar", "infile=%s" % infile, "keyword=%s" % keyword
+ ])
+ datatype = subprocess.check_output([
+ "pget", "dmkeypar", "datatype"
+ ]).decode("utf-8").strip()
+ value = subprocess.check_output([
+ "pget", "dmkeypar", "value"
+ ]).decode("utf-8").strip()
+ value = DATATYPES[datatype](value)
+ unit = subprocess.check_output([
+ "pget", "dmkeypar", "unit"
+ ]).decode("utf-8").strip()
+ comment = subprocess.check_output([
+ "pget", "dmkeypar", "comment"
+ ]).decode("utf-8").strip()
+ return {"value": value, "datatype": datatype,
+ "unit": unit, "comment": comment}
+
+
+def write_keyword(infile, keyword, value, datatype=None,
+ unit=None, comment=None):
+ """
+ Write the specified keyword to the file header.
+ """
+ DATATYPES = {
+ "real": "double",
+ "integer": "long",
+ "boolean": "boolean",
+ "string": "string"
+ }
+ subprocess.check_call(["punlearn", "dmhedit"])
+ cmd = [
+ "dmhedit", "infile=%s" % infile, "filelist=none",
+ "operation=add", "key=%s" % keyword, "value=%s" % value
+ ]
+ if datatype:
+ cmd += ["datatype=%s" % DATATYPES[datatype]]
+ if unit:
+ cmd += ["unit=%s" % unit]
+ if comment:
+ cmd += ["comment=%s" % comment]
+ subprocess.check_call(cmd)
+
+
+def copy_keyword(infile1, infile2, keyword):
+ """
+ Copy the specified keyword(s) from infile1 to infile2.
+ """
+ if not isinstance(keyword, list):
+ keyword = [keyword]
+ for kw in keyword:
+ data = read_keyword(infile1, kw)
+ write_keyword(infile2, kw, **data)