aboutsummaryrefslogtreecommitdiffstats
path: root/bin/repro_acis.py
blob: 052414c51883f9a0d29fbd7921685943f22dadb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
#
# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
# MIT license
#
# 2017-02-27

"""
Reprocess Chandra ACIS raw "secondary" (level=1) data using the
CIAO contrib tool ``chandra_repro``, and build the ``manifest.yaml``.
"""

import os
import argparse
import subprocess
import logging
from glob import glob

from _context import acispy
from acispy.manifest import get_manifest
from acispy.pfiles import setup_pfiles


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def repro_acis(indir=".", outdir="repro", clobber=False):
    """
    Reprocess Chandra ACIS raw level=1 data using ``chandra_repro``.
    """
    logger.info("Reprocess Chandra ACIS raw level=1 data ...")
    clobber = "yes" if clobber else "no"
    subprocess.check_call(["punlearn", "ardlib"])
    subprocess.check_call(["punlearn", "chandra_repro"])
    subprocess.check_call([
        "chandra_repro", "indir=%s" % indir, "outdir=%s" % outdir,
        "verbose=2", "clobber=%s" % clobber
    ])
    logger.info("Fix asol.lis by striping the absolute directory ...")
    asol_lis = glob("%s/acisf*_asol1.lis" % outdir)[0]
    asol = [os.path.basename(fp) for fp in open(asol_lis).readlines()]
    os.rename(asol_lis, asol_lis+".orig")
    open(asol_lis, "w").writelines(asol)


def build_manifest(reprodir="repro", manifestfile="manifest.yaml"):
    """
    Build the ``manifest.yaml`` with reprocessed products.
    """
    logger.info("Build manifest with reprocessed products ...")
    if os.path.exists(manifestfile):
        logger.info("Use existing manifest file: %s" % manifestfile)
    else:
        logger.info("Create a new manifest file: %s" % manifestfile)
        open(manifestfile, "w").close()

    manifest = get_manifest(manifestfile)
    key = "directory"
    manifest.set(key, os.path.basename(os.getcwd()))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))
    key = "evt2"
    manifest.setpath(key, glob("%s/acisf*_repro_evt2.fits" % reprodir))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))
    key = "bpix"
    manifest.setpath(key, glob("%s/acisf*_repro_bpix1.fits" % reprodir))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))
    key = "asol"
    manifest.setpath(key, glob("%s/pcadf*_asol1.fits" % reprodir))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))
    key = "asol_lis"
    manifest.setpath(key, glob("%s/acisf*_asol1.lis" % reprodir))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))
    key = "fov"
    manifest.setpath(key, glob("%s/acisf*_repro_fov1.fits" % reprodir))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))
    key = "pbk"
    manifest.setpath(key, glob("%s/acisf*_pbk0.fits" % reprodir))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))
    key = "msk"
    manifest.setpath(key, glob("%s/acisf*_msk1.fits" % reprodir))
    logger.info("Added '%s' to manifest: %s" % (key, manifest.get(key)))


def main():
    parser = argparse.ArgumentParser(
            description="Extract surface brightness profile (SBP)")
    parser.add_argument("-C", "--clobber", dest="clobber", action="store_true",
                        help="overwrite existing file")
    args = parser.parse_args()

    setup_pfiles(["chandra_repro", "ardlib"])

    repro_acis(clobber=args.clobber)
    build_manifest()


if __name__ == "__main__":
    main()