aboutsummaryrefslogtreecommitdiffstats
path: root/xspec/xspec_avg_tz.tcl
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2017-02-06 22:09:52 +0800
committerAaron LI <aaronly.me@outlook.com>2017-02-06 22:09:52 +0800
commitfe5f1afc62fbacee04d46327153de0cf11b3ab3c (patch)
tree33ef0a056a134bf194d7f687819caf93dc6f44c9 /xspec/xspec_avg_tz.tcl
parent64d60761f521b2b8032193bed9c7225374e7ea63 (diff)
downloadchandra-acis-analysis-fe5f1afc62fbacee04d46327153de0cf11b3ab3c.tar.bz2
Add xspec_avg_tz.tcl to help calc average temperature and abundance
Also update xspec_tprofile.tcl a bit
Diffstat (limited to 'xspec/xspec_avg_tz.tcl')
-rw-r--r--xspec/xspec_avg_tz.tcl109
1 files changed, 109 insertions, 0 deletions
diff --git a/xspec/xspec_avg_tz.tcl b/xspec/xspec_avg_tz.tcl
new file mode 100644
index 0000000..7720f64
--- /dev/null
+++ b/xspec/xspec_avg_tz.tcl
@@ -0,0 +1,109 @@
+# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
+# MIT license
+#
+# XSPEC Tcl script to calculate the average temperature and abundance
+# by ting the temperatures and abundances of all regions.
+#
+# NOTE:
+# * Use XSPEC model `projct*wabs*apec`
+# * if error < 0.01, then assume error = 0.01
+# * if fabs(error) < 1e-7, then set error as `NULL' in output
+# (this may be caused by frozen or tied parameters)
+#
+# Output:
+# * tz_average.txt : average temperature and abundance (and errors)
+#
+# Change logs:
+# 2017-02-06, Weitian LI
+# * Initial version based on `xspec_tprofile.tcl`
+
+
+set NAME "xspec_avg_tz.tcl"
+set DATE [ exec date ]
+# Set the error flag
+set ERR_FLAG "FALSE"
+
+# Return TCL results for XSPEC commands.
+set xs_return_result 1
+
+# Keep going until fit converges.
+query yes
+
+# Get the value of the specified parameter
+proc get_value {pn} {
+ tclout param $pn
+ scan $xspec_tclout "%f" value
+ return $value
+}
+
+# Calculate the error of the specified parameter
+proc get_error {pn} {
+ global ERR_FLAG
+ error 1.0 $pn
+ tclout error $pn
+ scan $xspec_tclout "%f %f" val_l val_u
+ set err [ expr {($val_u - $val_l) / 2.0} ]
+ # error treatment
+ if {abs($err) < 1.0e-7} {
+ puts "*** WARNING: invalid error value ***"
+ set err "NULL"
+ set ERR_FLAG "TRUE"
+ } elseif {$err < 0.01} {
+ set err 0.01
+ }
+ return $err
+}
+
+# Output file
+set tz_fn "tz_average.txt"
+if {[ file exists $tz_fn ]} {
+ exec mv -fv $tz_fn ${tz_fn}_bak
+}
+set tz_fd [ open $tz_fn w ]
+
+# Header
+puts $tz_fd "# Average temperature and abundance"
+puts $tz_fd "#"
+puts $tz_fd "# Generated by: ${NAME}"
+puts $tz_fd "# Created: ${DATE}"
+puts $tz_fd "#"
+puts $tz_fd "# Item Value Error"
+
+# Number of data group
+tclout datasets
+set datasets $xspec_tclout
+
+# Untie and thaw the temperature and abundance of the first region
+untie 5 6
+thaw 5 6
+
+# Tie all other temperature and abundance to that of the first region
+for {set i 2} {$i <= ${datasets}} {incr i} {
+ # Parameter number of temperature and abundance
+ set temp_pn [ expr {8 * $i - 3} ]
+ set abund_pn [ expr {8 * $i - 2} ]
+ newpar $temp_pn = 5
+ newpar $abund_pn = 6
+}
+
+# Fit the spectra again
+fit
+
+# Get the values and errors of average temperature and abundance
+set temp_val [ get_value 5 ]
+set temp_err [ get_error 5 ]
+set abund_val [ get_value 6 ]
+set abund_err [ get_error 6 ]
+
+# Output the average temperature and abundance
+puts "Average temperature: $temp_val, $temp_err"
+puts "Average abundance: $abund_val, $abund_err"
+puts $tz_fd "temp $temp_val $temp_err"
+puts $tz_fd "abund $abund_val $abund_err"
+
+close $tz_fd
+
+# Print a WARNING if there are any errors
+if {[ string equal $ERR_FLAG "TRUE" ]} {
+ puts "*** WARNING: there are invalid error values ***"
+}