summaryrefslogtreecommitdiffstats
path: root/info.py
blob: 8e1345d73efa014120a3dedde08767c86178fb35 (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
# Copyright (c) 2016 Aaron LI
# MIT license
#
# Created: 2016-05-04
#
# Change logs:
# 2016-07-12:
#   * Add functions "get_name()" and "get_obsid()"
# 2016-07-11:
#   * Add functions "get_redshift()" and "get_nh()"
#   * Add function "get_owner()"
#

"""
module to process the INFO.json file, contains some handy functions
to extract the needed information.
"""

import json


def load_info(info):
    """
    Load data from the INFO.json if necessary.
    """
    if isinstance(info, str):
        json_str = open(info).read().rstrip().rstrip(",")
        return json.loads(json_str)
    else:
        # assuming that the provided `info` is already a Python dictionary
        return info


def get_owner(info):
    """
    Determine the owner of the info: 'LWT' or 'ZZH'.

    Return:
    * 'LWT'
    * 'ZZH'
    """
    info = load_info(info)
    if "IN_SAMPLE" in info.keys():
        return "ZZH"
    else:
        return "LWT"


def get_name(info):
    """
    Get the source name from the INFO json file.
    """
    info = load_info(info)
    name = info["Source Name"]
    uname = info.get("Unified Name")
    results = {
        "name": name,
        "uname": uname,
    }
    return results


def get_obsid(info):
    """
    Get the Chandra observation ID from the INFO json file.
    """
    info = load_info(info)
    obsid = int(info["Obs. ID"])
    return obsid


def get_r500(info):
    """
    Get the R500 value (in unit pixel and kpc), as well as its errors.

    Arguments:
      * info: filename of the INFO.json, or the info dictionary

    Return:
      a dictionary contains the necessary results
    """
    info = load_info(info)

    if get_owner(info) == "ZZH":
        r500_kpc = float(info["R500"])
        r500EL_kpc = float(info["R500_err_lower"])
        r500EU_kpc = float(info["R500_err_upper"])
    else:
        r500_kpc = float(info["R500 (kpc)"])
        r500EL_kpc = float(info["R500_err_lower (1sigma)"])
        r500EU_kpc = float(info["R500_err_upper (1sigma)"])

    # Convert kpc to Chandra ACIS pixel
    rmax_sbp_pix = float(info["Rmax_SBP (pixel)"])
    rmax_sbp_kpc = float(info["Rmax_SBP (kpc)"])
    kpc_per_pix = rmax_sbp_kpc / rmax_sbp_pix
    r500_pix = r500_kpc / kpc_per_pix
    r500EL_pix = r500EL_kpc / kpc_per_pix
    r500EU_pix = r500EU_kpc / kpc_per_pix

    results = {
            "r500_kpc":    r500_kpc,
            "r500EL_kpc":  r500EL_kpc,
            "r500EU_kpc":  r500EU_kpc,
            "r500_pix":    r500_pix,
            "r500EL_pix":  r500EL_pix,
            "r500EU_pix":  r500EU_pix,
            "kpc_per_pix": kpc_per_pix,
    }
    return results


def get_redshift(info):
    """
    Get the redshift from the info json file.
    """
    info = load_info(info)
    redshift = float(info["redshift"])
    return redshift


def get_nh(info):
    """
    Get the column density (nH) from the info json file.
    """
    info = load_info(info)
    if get_owner(info) == "LWT":
        nh = float(info["nH (10^22 cm^-2)"])
    else:
        nh = float(info["nh"])
    return nh