# carbspec_anal.src
# XSPEC source file to analyze the HEG carbon spectrum

# select output device
cpd /XTERM
#cpd /PS

# read in the IDL created data file
data carbspec_real.pha

# use all then channels...
notice **-**

# in channel space look for zer-count, etc. regions
# and ignore them
setplot channel
plot data
# ignore the low energy stuff
ignore 1940-**
# and the bright pixel just above 1 keV
ignore 1670-1690
# and more low-energy stuff!
ignore 1670-**
plot data

# OK, now view it in energy space
setplot energy
plot ldata

# OK, now the model...
# Use a powerlaw to introduce no features
# e.g., wabs has edges...
model po
1.0
7.6

fit

# lots of ways to view it ...
plot ldata residuals
plot ldata ratio
plot ldata chisq

# This is nice... so put it in a PS file
cpd /PS
plot ldata delchi
# rename the pgplot file
exec cp pgplot.ps carbspec_init.ps
cpd /XTERM

# And in "model space" :
plot ufspec

# Now zoom in on the Al-K and W-M lines
setplot channel
ignore **-499
ignore 1301-**
fit
plot ldata delchi

# Need to add some lines to the model...
# Freeze the power law and manually put in the line
# with addcomp (as below).  Change the line location
# etc. with newpar and see effect with plot:
#   newpar 3 1.739
#   plot ldata delchi
# Once OK by eye, fit it.  Add other lines... Finally, freeze the lines
# and fit the po component.
# Then refit everything !
#
freeze 1-2

addcomp 2 zgauss
1.739
0.00204
0.
0.1356
fit
plot ldata delchi
freeze 1-6

addcomp 3 zgauss
1.835
0.00362
0.
0.1492
fit
plot ldata delchi
freeze 1-10

addcomp 4 zgauss
1.775
0.00354
0.
0.1338
fit
plot ldata delchi
freeze 1-14

# re-fit the continuum...
thaw 1-2
fit
freeze 1-2

addcomp 5 zgauss
1.486
0.00191
0.
0.0613
fit
plot ldata delchi
freeze 1-18

addcomp 6 zgauss
2.619
0.00283
0.
0.0343
fit
plot ldata delchi
freeze 1-22

# re-fit the continuum...
thaw 1-2
fit
freeze 1-2
plot ldata delchi

# and now dare to do it all...
# (except for the redshifts)
thaw 1-22
freeze 5
freeze 9
freeze 13
freeze 17
freeze 21
fit
plot ldata delchi

# This is nice... so put it in a PS file
# cat the XSPEC parameter output to a file too.
cpd /PS
plot ldata delchi
# rename the pgplot file
exec cp pgplot.ps carbspec_AlW.ps
cpd /XTERM
# freeze everything
freeze 1-22
plot ldata delchi

# Now, fit the Fe line (!) above 6 keV
ignore **-**
notice 80-160
setplot channel

#Fit the continuum
freeze 1-22
thaw 1-2
fit
plot ldata delchi

# Fe line
addcomp 7 zgauss
6.391
0.0248
0.
0.03409
freeze 23-26

#Fit this line and the continuum
thaw 23-24
thaw 26
fit
plot ldata delchi

# This is nice... so put it in a PS file
# cat the XSPEC parameter output to a file too.
cpd /PS
plot ldata delchi
# rename the pgplot file
exec cp pgplot.ps carbspec_Fe.ps
cpd /XTERM

# Now, fit the two lines betwen 3 and 4 keV
# Ca and K 
ignore **-**
notice 300-500
setplot channel

#Fit the continuum
freeze 1-26
thaw 1-2
fit
plot ldata delchi

addcomp 8 zgauss
3.309
0.00825
0.
0.03598

addcomp 9 zgauss
3.684
0.00679
0.
0.0355

#Fit these lines and the continuum
thaw 1-2
fit
plot ldata delchi

# This is nice... so put it in a PS file
# cat the XSPEC parameter output to a file too.
cpd /PS
plot ldata delchi
# rename the pgplot file
exec cp pgplot.ps carbspec_KCa.ps
cpd /XTERM

# Now, fit the Mg line at 1.253 keV
ignore **-**
notice 1400-1470
setplot channel

#Fit the continuum
freeze 1-34
thaw 1-2
fit
plot ldata delchi

# Mg line
addcomp 10 zgauss
1.253
0.000436
0.
0.01966

#Fit this line
freeze 1-2
fit
plot data delchi

#Fit the continuum
freeze 1-38
thaw 1-2
fit
plot ldata delchi

#Fit this line
freeze 1-2
thaw 35-36
thaw 38
fit
plot data delchi

#Fit the continuum
freeze 1-38
thaw 1-2
fit
plot ldata delchi

#Fit this line
freeze 1-2
thaw 35-36
thaw 38
fit
plot data delchi

# This is nice... so put it in a PS file
# cat the XSPEC parameter output to a file too.
cpd /PS
plot data delchi
# rename the pgplot file
exec cp pgplot.ps carbspec_Mg.ps
cpd /XTERM
plot data delchi

# Finally, with all these lines fixed, try another
# fit to the continuum over the wide range
# freeze everything
freeze 1-38
thaw 1-2
ignore **-**
notice **-1670
fit
setplot energy
cpd /PS
plot ldata delchi
# rename the pgplot file
exec cp pgplot.ps carbspec_final.ps
plot
cpd /XTERM
plot ldata delchi

# Use iplot to plot the data/model
# in several enrgy bands with improved 
# X Y scales...  try to keep 2 decades in Y
#  (manually cp pgplot.ps to carbspec_E_EkeV.ps )
#  iplot ldata
#  rescale X 1.0 1.4
#  rescale Y 20. 2000.
#  device /PS
#  plot
#  device /XTERM
#  rescale X 1.4 2.
#  rescale Y 20. 2000.
#  device /PS
#  plot
#  device /XTERM
#  rescale X 2. 3.
#  rescale Y 4. 400.
#  device /PS
#  plot
#  device /XTERM
#  rescale X 3. 5.
#  rescale Y 3. 300.
#  device /PS
#  plot
#  device /XTERM
#  rescale X 5. 10.
#  rescale Y 0.4 40.
#  device /PS
#  plot
#  device /XTERM
#  quit   (back to XSPEC)