summaryrefslogtreecommitdiffstats
path: root/prepare_sbpfit.py
blob: 4ec7cf495ec25e872f6ca6bc8a8943d2edfb4878 (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Prepare the configuration file for the `sbp_fit.py`.
# And extract name, obsid, r500 information from the '*_INFO.json' file
# to fill the config.
#
# Aaron LI
# 2016-04-21
#
# ChangeLog:
#

import sys
import glob
import os
import re
import json
import argparse
from datetime import datetime


def update_sbpfit_conf(sbpfit_conf, info):
    """
    Update the sbpfit configuration according to the INFO.

    Arguments:
      * sbpfit_conf: list of lines of the sample sbpfit config
      * info: INFO dictionary

    Return:
      updated `sbpfit_conf`
    """
    name = info["Source Name"]
    obsid = int(info["Obs. ID"])

    if "R500 (kpc)" in info.keys():
        # lwt's
        r500_kpc = float(info["R500 (kpc)"])
    elif "R500" in info.keys():
        # zzh's
        r500_kpc = float(info["R500"])
    else:
        raise ValueError("Cannot get R500 from INFO.json")
    # Convert kpc to Chandra ACIS pixel
    rmax_sbp_pix = float(info["Rmax_SBP (pixel)"])
    rmax_sbp_kpc = float(info["Rmax_SBP (kpc)"])
    r500_pix = r500_kpc / rmax_sbp_kpc * rmax_sbp_pix
    print("R500: %.2f (kpc), %.2f (pixel)" % (r500_kpc, r500_pix))

    sbpfit_conf_new = []
    for line in sbpfit_conf:
        line_new = re.sub(r"<DATE>", datetime.utcnow().isoformat(), line)
        line_new = re.sub(r"<NAME>", name, line_new)
        line_new = re.sub(r"<OBSID>", "%s" % obsid, line_new)
        line_new = re.sub(r"<R500_PIX>", "%.2f" % r500_pix, line_new)
        line_new = re.sub(r"<R500_KPC>", "%.2f" % r500_kpc, line_new)
        sbpfit_conf_new.append(line_new)

    return sbpfit_conf_new


def main():
    parser = argparse.ArgumentParser(description="Prepare sbpfit config")
    parser.add_argument("-j", "--json", dest="json", required=False,
            help="the *_INFO.json file (default: find ../*_INFO.json)")
    parser.add_argument("-c", "--config", dest="config", required=True,
            help="sample sbpfit configuration")
    parser.add_argument("outfile", nargs="?",
            help="filename of the output sbpfit config " + \
                 "(default: same as the sample config)")
    args = parser.parse_args()

    # default "*_INFO.json"
    info_json = glob.glob("../*_INFO.json")[0]
    if args.json:
        info_json = args.json

    json_str = open(info_json).read().rstrip().rstrip(",")
    info = json.loads(json_str)

    # sample config file
    sbpfit_conf = open(args.config).readlines()

    # output config file
    if args.outfile:
        outfile = args.outfile
    else:
        outfile = os.path.basename(args.config)

    sbpfit_conf_new = update_sbpfit_conf(sbpfit_conf, info)
    with open(outfile, "w") as outf:
        outf.write("".join(sbpfit_conf_new))


if __name__ == "__main__":
    main()