summaryrefslogtreecommitdiffstats
path: root/extract_info.py
blob: 40ff85e1b51541c47e944a776b6bbb4f4f48f6a2 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
#
# Copyright (c) 2016 Aaron LI
# MIT license
#
# Extract R500 from the '*_INFO.json' file, and center coordinate from
# existing "sbprofile.reg", and then make the circle regions w.r.t R500
# in order to visualize the FoV coverage of the observations.
#
# Created: 2016-04-22
#
# Change log:
# 2016-04-27:
#   * Do not import the unused "re" module
#

import sys
import glob
import os
import json
import csv
import argparse
from collections import OrderedDict


def extract_info(info):
    """
    Extract the requested information (i.e., keywords) from the info
    dictionary.
    """
    # all the keys whose info to be extracted (and keep the order)
    keys = OrderedDict([
            ("Name",               "Source Name"),
            ("ObsID",              ("Obs. ID", int)),
            ("Detector",           "Detector"),
            ("Exposure_ks",        ("Exposure (ks)", float)),
            ("Exposure_clean_ks",  ("Clean Exposure (ks)", float)),
            ("redshift",           ("redshift", float)),
            ("nH",                 None),
            ("Rmax_SBP_pix",       ("Rmax_SBP (pixel)", float)),
            ("Rmax_SBP_kpc",       ("Rmax_SBP (kpc)", float)),
            ("kpc_per_pix",        None),
            ("R500_kpc",           None),
            ("R500_pix",           None),
            ("R500EL_kpc",         None),
            ("R500EU_kpc",         None),
            ("M500",               None),
            ("M500EL",             None),
            ("M500EU",             None),
            ("L500",               None),
            ("L500E",              None),
            ("R200_kpc",           None),
            ("R200_pix",           None),
            ("R200EL_kpc",         None),
            ("R200EU_kpc",         None),
            ("M200",               None),
            ("M200EL",             None),
            ("M200EU",             None),
            ("L200",               None),
            ("L200E",              None),
            ("Tavg_0.2-0.5R500",   ("T(0.2-0.5 R500)", float)),
            ("TavgEL_0.2-0.5R500", ("T_err_l(0.2-0.5 R500)", float)),
            ("TavgEU_0.2-0.5R500", ("T_err_u(0.2-0.5 R500)", float)),
    ])
    # keys corresponding to lwt's INFO
    keys_lwt = {
            "nH":                 ("nH (10^22 cm^-2)", float),
            "R500_kpc":           ("R500 (kpc)", float),
            "R500EL_kpc":         ("R500_err_lower (1sigma)", float),
            "R500EU_kpc":         ("R500_err_upper (1sigma)", float),
            "M500":               ("M500 (M_sun)", float),
            "M500EL":             ("M500_err_lower (1sigma)", float),
            "M500EU":             ("M500_err_upper (1sigma)", float),
            "L500":               ("L500 (erg/s)", float),
            "L500E":              ("L500_err (1sigma)", float),
            "R200_kpc":           ("R200 (kpc)", float),
            "R200EL_kpc":         ("R200_err_lower (1sigma)", float),
            "R200EU_kpc":         ("R200_err_upper (1sigma)", float),
            "M200":               ("M200 (M_sun)", float),
            "M200EL":             ("M200_err_lower (1sigma)", float),
            "M200EU":             ("M200_err_upper (1sigma)", float),
            "L200":               ("L200 (erg/s)", float),
            "L200E":              ("L200_err (1sigma)", float),
    }
    # keys corresponding to zzh's INFO
    keys_zzh = {
            "nH":                 ("nh", float),
            "R500_kpc":           ("R500", float),
            "R500EL_kpc":         ("R500_err_lower", float),
            "R500EU_kpc":         ("R500_err_upper", float),
            "M500":               ("M500 (solar mass)", float),
            "M500EL":             ("M500_err_lower (1 sigma)", float),
            "M500EU":             ("M500_err_upper (1 sigma)", float),
            "L500":               ("L500 (erg/s)", float),
            "L500E":              ("L500_err (1 sigma)", float),
            "R200_kpc":           ("R200", float),
            "R200EL_kpc":         ("R200_err_lower", float),
            "R200EU_kpc":         ("R200_err_upper", float),
            "M200":               ("M200", float),
            "M200EL":             ("M200_err_lower", float),
            "M200EU":             ("M200_err_upper", float),
            "L200":               ("L200", float),
            "L200E":              ("L200_err", float),
    }

    if "IN_SAMPLE" in info.keys():
        # zzh's INFO
        keys.update(keys_zzh)
    else:
        # lwt's INFO
        keys.update(keys_lwt)

    data = keys.copy()
    for k, v in keys.items():
        if v is None:
            continue
        elif isinstance(v, tuple):
            t = v[1]
            try:
                data[k] = t(info[ v[0] ])
            except (ValueError, TypeError):
                data[k] = None
        else:
            data[k] = info[v]

    data["kpc_per_pix"] = data["Rmax_SBP_kpc"] / data["Rmax_SBP_pix"]
    data["R500_pix"]    = data["R500_kpc"] / data["kpc_per_pix"]
    data["R200_pix"]    = data["R200_kpc"] / data["kpc_per_pix"]

    return data


def main():
    parser = argparse.ArgumentParser(description="Extract data from INFO.json")
    parser.add_argument("-j", "--json", dest="json", required=False,
            help="the *_INFO.json file (default: find *_INFO.json)")
    parser.add_argument("path", nargs="?", default=".",
            help="path to the directory contains the INFO.json")
    args = parser.parse_args()

    # path to the directory contains INFO.json
    if args.path == ".":
        path = os.getcwd()
    else:
        path = args.path
        os.chdir(path)

    # 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)

    data = extract_info(info)
    data["PATH"] = path
    #print(data, file=sys.stderr)

    # output results
    csv_writer = csv.writer(sys.stdout)
    csv_writer.writerow(data.keys())
    csv_writer.writerow(data.values())


if __name__ == "__main__":
    main()