diff options
-rwxr-xr-x | make_sbpfit_config.py | 82 | ||||
-rwxr-xr-x | prepare_sbpfit.py | 100 |
2 files changed, 82 insertions, 100 deletions
diff --git a/make_sbpfit_config.py b/make_sbpfit_config.py new file mode 100755 index 0000000..8228bd3 --- /dev/null +++ b/make_sbpfit_config.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Make the configuration file for the `sbp_fit.py`. +# The name, obsid, r500 information is extracted from the '*_INFO.json' file. +# +# Aaron LI +# Created: 2016-04-21 +# Updated: 2016-07-12 +# +# Change logs: +# 2016-07-12: +# * Rewrite according to `make_tprofile_config.py`, +# and make the sample config built-in +# 2016-04-26: +# * Minor update to output file write +# + +import glob +import argparse + +from info import get_name, get_obsid, get_r500 + + +sample_config = """ +## Configuration for `fit_sbp.py` + +name = %(name)s +obsid = %(obsid)d +r500_pix = %(r500_pix)f +r500_kpc = %(r500_kpc)f + +# sbp model: "sbeta" or "dbeta" +model = sbeta + +[sbeta] +outfile = sbpfit_sbeta.json +imgfile = sbpfit_sbeta.png +#ignore = 0.0-20.0, +ignore_r500 = 0.0-0.1, + [[params]] + # name = initial, lower, upper, variable (FIXED/False to fix the parameter) + s0 = 1.0e-8, 0.0, 1.0e-6 + rc = 30.0, 5.0, 1.0e4 + beta = 0.7, 0.3, 1.1 + bkg = 1.0e-10, 0.0, 1.0e-8 +""" + + +def make_config(info): + cfg_data = { + "name": get_name(info)["name"], + "obsid": get_obsid(info), + "r500_pix": get_r500(info)["r500_pix"], + "r500_kpc": get_r500(info)["r500_kpc"], + } + cfg = sample_config % cfg_data + return cfg + + +def main(): + parser = argparse.ArgumentParser( + description="Make the config for 'fit_sbp.py'") + parser.add_argument("-j", "--json", dest="json", required=False, + help="the *_INFO.json file " + + "(default: find ../*_INFO.json)") + parser.add_argument("outfile", nargs="?", + help="filename of the output sbpfit config " + + "(default: sbpfit.conf)") + args = parser.parse_args() + + # default "*_INFO.json" + info_json = glob.glob("../*_INFO.json")[0] + if args.json: + info_json = args.json + + config = make_config(info_json) + open(args.outfile, "w").write(config) + + +if __name__ == "__main__": + main() diff --git a/prepare_sbpfit.py b/prepare_sbpfit.py deleted file mode 100755 index a7c5d1c..0000000 --- a/prepare_sbpfit.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/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 -# Created: 2016-04-21 -# Updated: 2016-04-26 -# -# Changelog: -# 2016-04-26: -# * Minor update to output file write -# - -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) - open(outfile, "w").write("".join(sbpfit_conf_new)) - - -if __name__ == "__main__": - main() - |