/Xspecscript_tie_bin.xcm

https://bitbucket.org/ahoffer/acceptii · Unknown · 155 lines · 138 code · 17 blank · 0 comment · 0 complexity · de7983b7ee8f8a48f31aaff5a6cd7fcd MD5 · raw file

  1. log xspec_temp.log
  2. set xs_return_result 1
  3. # Fit X-ray spectral annuli in X-spec
  4. # additional functionality to tie parameters together such as metallicity in annuli pairs to give the same fit
  5. #Filename inputs
  6. #####Check for input file
  7. ##put "Enter filename with comma separated data obsid, etc."
  8. ##gets stdin filename
  9. # Inputs:
  10. # List of Spectra - currently constructs based on prefix and suffix
  11. # Redshift
  12. # nH in 10^22 cm^2
  13. # Tx guess
  14. # Fe guess
  15. # * combine bin and unbinned and set stat and binning parameters
  16. #set check 0 # if there was a typo doesn't crash
  17. #while {$check<1} {
  18. #puts "Enter Obs ID: "
  19. #gets stdin obsid
  20. #puts "Enter number of spectra to fit: "
  21. #gets stdin numspec
  22. #puts "Enter file prefix (e.g. ann): "
  23. #gets stdin prefix
  24. #puts "Enter file suffix (e.g. .ai.fits): "
  25. #gets stdin suffix
  26. ##############Enter parameters
  27. #puts "Enter redshift: "
  28. #gets stdin red
  29. #puts "Enter nH column in units of 10^22 cm^2: "
  30. #gets stdin nH
  31. #puts "Enter Tx guess: "
  32. #gets stdin Tx
  33. #puts "Enter Abundance guess: "
  34. #gets stdin Fe
  35. #puts "The obsid is $obsid"
  36. #puts "The number of spectra to be fit is $numspec"
  37. #puts "The first file to be fit is $prefix\_$obsid\_0$suffix"
  38. #puts "If this first file looks correct enter 1, otherwise enter 0"
  39. #gets stdin check
  40. #}
  41. #################
  42. #Fit Xspec data
  43. statistic cstat
  44. weight standard
  45. method simplex
  46. setplot energy
  47. setplot xlog on
  48. setplot ylog on
  49. # Keep going until fit converges.
  50. query yes
  51. # Open the file to put the results in.
  52. set fileid [open fit_result.dat w]
  53. cosmo 70 0 0.7
  54. #Format is nH, kT, nH(fixed at 1.0), Abundance, redshift, switch, and normalize
  55. abund grsa
  56. set prefix ann
  57. set obsid 11724
  58. set suffix .pi
  59. set nH 0.010
  60. set Tx 7.1
  61. set Fe 0.3
  62. set red 0.213
  63. set Tsoft 0.4
  64. set Fesoft 0.3
  65. set numspec 9
  66. set groups "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"
  67. #load all the annuli first so regions can be tied across
  68. set y 1
  69. setplot device $prefix\_$obsid\_bin-all.ps/ps
  70. for {set x 0} {$x < $numspec} { incr x } {
  71. data $y:$y $prefix\_$obsid\_$x$suffix
  72. set curr_group [string range $groups 0 [expr {2 * $x}]]
  73. puts "$curr_group"
  74. setplot group $curr_group
  75. plot data
  76. incr y
  77. }
  78. # focus on 0.7-7.0 keV range only need to constrain spectra, not groups
  79. ignore bad
  80. ignore 1-$numspec:**-0.7 7.0-**
  81. # input initial values for all the spectra phabs(mekal_Tx+mekal_softXraybg)
  82. # this setup automatically ties all parameters across spectra
  83. # total of 13 parameters
  84. model phabs(mekal+mekal) & $nH & $Tx & 1.0 & $Fe,0 & $red & 0 & 0.01 & $Tsoft & 1.0 & $Fesoft,0 & $red & 0 & 0.01
  85. # freeze: nH phabs, Tsoft. Rest are already frozen
  86. freeze 1
  87. freeze 8
  88. freeze 10
  89. #untie: all Tx, all norm, central Fe, pair Fe after that
  90. set paramlen 13
  91. set Fe1 17
  92. set Fe2 30
  93. set norm1 20
  94. set norm2 26
  95. set Tx1 15
  96. for {set x 0} {$x < [expr {$numspec - 1}]} { incr x } {
  97. untie $Tx1 $norm1 $norm2
  98. set norm1 [expr {$norm1 + $paramlen}]
  99. set norm2 [expr {$norm2 + $paramlen}]
  100. set Tx1 [expr {$Tx1 + $paramlen}]
  101. if {[expr {$x % 2}] == 0} {
  102. newpar $Fe2=$Fe1
  103. set Fe1 [expr {$Fe1 + $paramlen}]
  104. set Fe2 [expr {$Fe2 + $paramlen}]
  105. }
  106. }
  107. puts "Waiting here before fit. Press return to continue."
  108. gets stdin empty
  109. # fit the spectra
  110. fit 10000 0.001
  111. fit
  112. # compute errors on all free parameters
  113. # output data to files
  114. for {set x 0} {$x < $numspec} { incr x } {
  115. error [expr {1 + $x * $paramlen}]-[expr {13 + $x * $paramlen}]
  116. puts "#### Hey Spectra # $x"
  117. tclout param [expr {2 + $x * $paramlen}]
  118. puts "#### Hey tx $xspec_tclout"
  119. tclout param [expr {4 + $x * $paramlen}]
  120. puts "#### Hey fe $xspec_tclout"
  121. tclout param [expr {5 + $x * $paramlen}]
  122. puts "#### Hey z $xspec_tclout"
  123. tclout stat
  124. puts "#### Hey stat $xspec_tclout"
  125. tclout dof
  126. puts "Hey dof $xspec_tclout"
  127. setplot energy
  128. setplot xlog on
  129. setplot ylog on
  130. setplot
  131. setplot device $prefix\_$obsid\_$x\_bin.ps/ps
  132. plot data resid
  133. }
  134. close $fileid
  135. log none
  136. file copy -force xspec_temp.log xspec_tx_fit_$obsid\_bin.log
  137. file delete xspec_temp.log
  138. exit