aboutsummaryrefslogtreecommitdiffstats
path: root/mass_profile/coolfunc_calc_erg.sh
blob: cd4416006a377195d1ac6b9e8887e8206c79fdaa (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
#!/bin/sh
##
## Calculate the 'cooling function' profile for each of the energy band
## specified in a bands file, with respect to the given 'temperature profile'
## and the average abundance, redshift, and column density nH, using the
## XSPEC model 'wabs*apec'.
##
## Weitian LI
## Updated: 2016-06-08
##

## cmdline arguments {{{
if [ $# -ne 6 ]; then
    printf "usage:\n"
    printf "    `basename $0` <tprofile> <avg_abund> <nH> <redshift> <coolfunc_prefix> <band_list>\n"
    exit 1
fi
TPROFILE=$1
ABUND_VAL=$2
N_H=$3
REDSHIFT=$4
COOLFUNC_PREFIX=$5
BLIST=$6
NORM=`cosmo_calc ${REDSHIFT} | grep 'norm.*cooling_function' | awk -F':' '{ print $2 }'`

if [ ! -r "${TPROFILE}" ]; then
    printf "ERROR: given tprofile '${TPROFILE}' NOT accessiable\n"
    exit 2
fi
## arguments }}}

## specify variable name outside while loop
## otherwise the inside vars invisible
XSPEC_CF_XCM="_coolfunc_calc.xcm"
[ -e "${XSPEC_CF_XCM}" ] && rm -f ${XSPEC_CF_XCM}

## generate xspec script {{{
cat >> ${XSPEC_CF_XCM} << _EOF_
## XSPEC Tcl script
## Calculate the cooling function profile w.r.t the temperature profile,
## for each specified energy band.
##
## Generated by: `basename $0`
## Date: `date`

set xs_return_results 1
set xs_echo_script 0
# set tcl_precision 12
## set basic data {{{
set nh ${N_H}
set redshift ${REDSHIFT}
set abund_val ${ABUND_VAL}
set norm ${NORM}
## basic }}}

## xspec related {{{
# debug settings {{{
chatter 0
# debug }}}
query yes
abund grsa
dummyrsp 0.01 100.0 4096 linear
# load model 'wabs*apec' to calc cooling function
# (nh=0.0: do not consider aborption ???)
model wabs*apec & 0.0 & 1.0 & \${abund_val} & \${redshift} & \${norm} &
## xspec }}}

## set input and output filename
set tpro_fn "${TPROFILE}"
set blist_fn "${BLIST}"
set cf_prefix "${COOLFUNC_PREFIX}"
set blist_fd [ open \${blist_fn} r ]

## loop over each energy band
while { [ gets \${blist_fd} blist_line ] != -1 } {
    if { "\${blist_line}" == "bolo" } {
        set e1 0.01
        set e2 100.0
        set name_suffix bolo
    } else {
        set e1 [ lindex \${blist_line} 0 ]
        set e2 [ lindex \${blist_line} 1 ]
        set name_suffix "\${e1}-\${e2}"
    }
    set cf_fn "\${cf_prefix}\${name_suffix}.dat"
    if { [ file exists \${cf_fn} ] } {
        exec rm -fv \${cf_fn}
    }
    set cf_fd [ open \${cf_fn} w ]
    set tpro_fd [ open \${tpro_fn} r ]

    ## read data from tprofile line by line
    while { [ gets \${tpro_fd} tpro_line ] != -1 } {
        # gets one line
        scan \${tpro_line} "%f %f" radius temp_val
        #puts "radius: \${radius}, temperature: \${temp_val}"
        # set temperature value
        newpar 2 \${temp_val}
        # calc flux & tclout
        flux \${e1} \${e2}
        tclout flux 1
        scan \${xspec_tclout} "%f %f %f %f" cf_data holder holder holder
        #puts "cf_data: \${cf_data}"
        puts \${cf_fd} "\${radius}    \${cf_data}"
    }
    close \${tpro_fd}
    close \${cf_fd}
}

## exit
tclexit
_EOF_
## xcm generation }}}

## invoke xspec to calc
printf "invoking XSPEC to calculate cooling function profile ...\n"
xspec - ${XSPEC_CF_XCM} > /dev/null