aboutsummaryrefslogtreecommitdiffstats
path: root/bin/calc_coolfunc_profile.py
blob: 8f33d1e32c92f6bbf8ebc604838aec4d3a6f8d1e (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
#!/usr/bin/env python3
#
# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
# MIT license

"""
Calculate the cooling function profile with respect to the input
temperature profile by interpolating the previously calculated
cooling function table.

In this way, the cooling function profile can be calculated very
quickly, allowing much more iterations for the later Monte Carlo
calculations.
"""

import os
import sys
import argparse

import numpy as np
import scipy.interpolate as interpolate


def interpolate_cf(table, logy=True):
    temp, cf = table[:, 0], table[:, 1]
    if logy:
        cf = np.log10(cf)
    print("Interpolating cooling function table ...", file=sys.stderr)
    interp = interpolate.interp1d(temp, cf, kind="linear")
    return interp


def calc_cf_profile(tprofile, interp, logy=True):
    print("Calculating cooling function profile ...", file=sys.stderr)
    radius, temp = tprofile[:, 0], tprofile[:, 1]
    cf = interp(temp)
    if logy:
        cf = 10 ** cf
    cfprofile = np.column_stack([radius, cf])
    return cfprofile


def main():
    parser = argparse.ArgumentParser(
        description="Calculate cooling function profile by interpolations")
    parser.add_argument("-t", "--table", dest="table", required=True,
                        help="previously calculated cooling function table")
    parser.add_argument("-T", "--tprofile", dest="tprofile", required=True,
                        help="temperature profile " +
                        "(2-column: radius temperature)")
    parser.add_argument("-o", "--outfile", dest="outfile", required=True,
                        help="output cooling function profile")
    parser.add_argument("-C", "--clobber", dest="clobber", action="store_true",
                        help="overwrite existing files")
    args = parser.parse_args()

    if (not args.clobber) and os.path.exists(args.outfile):
        raise OSError("Output file already exists: %s" % args.outfile)

    table = np.loadtxt(args.table)
    tprofile = np.loadtxt(args.tprofile)
    cf_interp = interpolate_cf(table)
    cf_profile = calc_cf_profile(tprofile, cf_interp)
    np.savetxt(args.outfile, cf_profile)


if __name__ == "__main__":
    main()