diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-04-28 14:29:18 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-04-28 14:29:18 +0800 |
commit | a833f0ffef45704f0a0052e01a0e85fbc2ff9a22 (patch) | |
tree | 6e8f7643f77bb8af92302a71fe49c10b3ae9e0af | |
parent | 57eb7fa3b39cd2fc990bc10da136f3f39f916bd6 (diff) | |
download | cexcess-a833f0ffef45704f0a0052e01a0e85fbc2ff9a22.tar.bz2 |
make_r500_regions.py: split out functions "get_r500()" and "get_center()"
-rwxr-xr-x | make_r500_regions.py | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/make_r500_regions.py b/make_r500_regions.py index d7e15cf..cc0c0a2 100755 --- a/make_r500_regions.py +++ b/make_r500_regions.py @@ -6,9 +6,12 @@ # in order to visualize the FoV coverage of the observations. # # Aaron LI -# 2016-04-15 +# Created: 2016-04-15 +# Updated: 2016-04-28 # -# ChangeLog: +# Changelog: +# 2016-04-28: +# * Add functions "get_r500()" and "get_center()" for code reuse # 2016-04-16: # * Fix R500 unit (convert from kpc to Chandra ACIS pixel) # @@ -21,6 +24,50 @@ import json import argparse +def get_r500(info): + """ + Get the R500 value (in unit pixel and kpc), as well as the value of + "kpc_per_pix" + """ + if isinstance(info, str): + json_str = open(info_json).read().rstrip().rstrip(",") + info = json.loads(json_str) + + 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") + + # 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 + + results = { + "r500_kpc": r500_kpc, + "r500_pix": r500_pix, + "kpc_per_pix": kpc_per_pix, + } + return results + + +def get_center(regfile): + """ + Get the center coordinate from the given region file + """ + regions = open(regfile).readlines() + regions = list(filter(lambda x: re.match(r"^(circle|annulus|pie).*", + x, re.I), + regions)) + xc, yc = map(float, re.split(r"[(,)]", regions[0])[1:3]) + return (xc, yc) + + def frange(x, y, step): while x < y: yield x @@ -45,30 +92,13 @@ def main(): if args.json: info_json = args.json - json_str = open(info_json).read().rstrip().rstrip(",") - info = json.loads(json_str) - - 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 + r500 = get_r500(info_json) + r500_kpc = r500["r500_kpc"] + r500_pix = r500["r500_pix"] print("R500: %.2f (kpc), %.2f (pixel)" % (r500_kpc, r500_pix)) # get center coordinate - regions = open(args.regin).readlines() - regions = list(filter(lambda x: re.match(r"^(circle|annulus|pie).*", - x, re.I), - regions)) - xc, yc = re.split(r"[(,)]", regions[0])[1:3] + xc, yc = get_center(args.regin) # output region r500_regions = [ |