#!/bin/csh

#############################################################################################
# make a quick postscript or pdf plot of a PHA file -> used for quick looks at X-ray spectra
# Author: Tom Dwelly, send emails to: tomdwelly -at- tomdwelly.com
# This software is supplied for educational purposes only and the author accepts 
# no liability for any unfortunate consequences of its use, whether through adventure 
# or misadventure. Feel free to copy, modify and distribute this code as you wish.  
#############################################################################################



#check that you have xspec and so on set up
if ( "`which xspec`" == "" ) then
  echo "Error, You don't seem to have xspec available"
  echo "See the xspec home page for details: http://heasarc.gsfc.nasa.gov/docs/xanadu/xspec/"
  exit 1
endif

set AWK = "gawk"
if ( "`which $AWK`" == "" ) then
  set AWK = "awk"
endif
if ( "`which $AWK`" == "" ) then
  echo "Error, you don't seem to have 'gawk' or 'awk' available to manipulate text files"
  echo "I'm suprised because most UNIXes should have at least one of those installed"
  exit 1
endif


##set up temp dirs
set ORIG_DIR = `pwd`
set TMP_DIR = /tmp/${USER}/plot_xspectrum
if ( ! -d /tmp/${USER} ) mkdir /tmp/${USER}
if ( ! -d /tmp/${USER} ) then
  echo "Error, problem setting up temporary directory: /tmp/${USER}"
  exit 1
endif

if ( ! -d $TMP_DIR ) mkdir $TMP_DIR
if ( ! -d $TMP_DIR ) then
  echo "Error, problem setting up temporary directory: $TMP_DIR"
  exit 1
endif

#get some command line params
######################################################
set PLOT_STYLE = "eeufspec"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-style" || "$argv[$I]" == "-plotstyle" || "$argv[$I]" == "-type" || "$argv[$I]" == "-plottype" ) then
    @ I1 = $I + 1
    set PLOT_STYLE = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
set PLOT_STYLE_NOGAPS = `echo "$PLOT_STYLE" | $AWK '//{gsub(" ","_",$0);print $0}'` 
######################################################

######################################################
set DISPLAY_PLOTFILE = "TRUE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-nd" || "$argv[$I]" == "-no_display" || "$argv[$I]" == "-nodisplay" || "$argv[$I]" == "-no_disp" || "$argv[$I]" == "-nodisp") then
    set DISPLAY_PLOTFILE = "FALSE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################


######################################################
set STAY_IN_XSPEC = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-stay" || "$argv[$I]" == "-stayinxspec") then
    set STAY_IN_XSPEC = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################

######################################################
set PROPER_ERRORS = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-error" || "$argv[$I]" == "-errors" || "$argv[$I]" == "-propererrors"|| "$argv[$I]" == "-proper_errors" ) then
    @ I1 = $I + 1
    set PROPER_ERRORS = "$argv[$I1]"
    if ( "$PROPER_ERRORS" == "TRUE" || "$PROPER_ERRORS" == "true" || "$PROPER_ERRORS" == "YES" || "$PROPER_ERRORS" == "yes" ) then
      set PROPER_ERRORS = "1.0"
    endif
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set NSIGMA = 5
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-nsigma" ||  "$argv[$I]" == "-n" || "$argv[$I]" == "-SNR" ) then
    @ I1 = $I + 1
    set NSIGMA = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set WDATA_FILE = "DEFAULT"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-wdata" || "$argv[$I]" == "-wdatafile" || "$argv[$I]" == "-wdata_file" ) then
    @ I1 = $I + 1
    set WDATA_FILE = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set LOG_FILE = "DEFAULT"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-log" || "$argv[$I]" == "-logfile" || "$argv[$I]" == "-log_file" ) then
    @ I1 = $I + 1
    set LOG_FILE = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set REDSHIFT = 0.0
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-redshift" ||  "$argv[$I]" == "-z" ) then
    @ I1 = $I + 1
    set REDSHIFT = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set GALNH = 0.0
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-GalNH" ||  "$argv[$I]" == "-galNH" ||  "$argv[$I]" == "-galnh"  ||  "$argv[$I]" == "-Galnh"  ||  "$argv[$I]" == "-gal"  ||  "$argv[$I]" == "-NHgal" ) then
    @ I1 = $I + 1
    set GALNH = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set GAMMA = 2.0
set GAMMA_FIX_FLAG = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-G" ||  "$argv[$I]" == "-gamma" ||  "$argv[$I]" == "-GAMMA"  ||  "$argv[$I]" == "-Gamma" ) then
    @ I1 = $I + 1
    set GAMMA = "$argv[$I1]"
    set GAMMA_FIX_FLAG = "-1"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set STAT = chi
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-stat" ||  "$argv[$I]" == "-statistic" ) then
    @ I1 = $I + 1
    set STAT = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set EMIN = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-emin" ||  "$argv[$I]" == "-elo" || "$argv[$I]" == "-Emin" ||  "$argv[$I]" == "-Elo" ) then
    @ I1 = $I + 1
    set EMIN = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set EMAX = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-emax" ||  "$argv[$I]" == "-ehi" || "$argv[$I]" == "-Emax" ||  "$argv[$I]" == "-Ehi") then
    @ I1 = $I + 1
    set EMIN = "$argv[$I1]"
    set argv[$I] = ""
    set argv[$I1] = ""
  endif
  @ I ++
end
######################################################

######################################################
set PLOT_BACK = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-back" || "$argv[$I]" == "-background" ) then
    set PLOT_BACK = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################

######################################################
set UNTIE_SPECTRA = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-untie" || "$argv[$I]" == "-independent" ) then
    set UNTIE_SPECTRA = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################

######################################################
set FIT_POW = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-fitpow" || "$argv[$I]" == "-fitpowerlaw" || "$argv[$I]" == "-fit_pow" || "$argv[$I]" == "-fit_powerlaw" || "$argv[$I]" == "-powfit"|| "$argv[$I]" == "-pow") then
    set FIT_POW = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################

######################################################
set FIT_ABSPOW = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-fitabs" ||  "$argv[$I]" == "-fitabspow" || "$argv[$I]" == "-fitabspowerlaw" || "$argv[$I]" == "-fit_abspow" || "$argv[$I]" == "-fit_abspowerlaw" || "$argv[$I]" == "-absfit" || "$argv[$I]" == "-abs" ) then
    set FIT_ABSPOW = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################

######################################################
set FIT_PCABSPOW = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-fitpcabs" ||  "$argv[$I]" == "-fitpcabspow" || "$argv[$I]" == "-fitpcabspowerlaw" || "$argv[$I]" == "-fit_pcabspow" || "$argv[$I]" == "-fit_pcabspowerlaw" || "$argv[$I]" == "-pcabsfit"  || "$argv[$I]" == "-pcabs") then
    set FIT_PCABSPOW = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################

######################################################
set FIT_GENERAL = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-fitgen" || "$argv[$I]" == "-fitgeneral" || "$argv[$I]" == "-fit_gen" || "$argv[$I]" == "-fit_general" || "$argv[$I]" == "-genfit" || "$argv[$I]" == "-general") then
    set FIT_GENERAL = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################


######################################################
set HELP_FLAG = "FALSE"
set I = 1
while ($I < $#argv )
  if ( "$argv[$I]" == "-h" || "$argv[$I]" == "-help" || "$argv[$I]" == "--help" ) then
    set HELP_FLAG = "TRUE"
    set argv[$I] = ""
  endif
  @ I ++
end
######################################################

######################################################
######################################################
######################################################
if ( $#argv == 1 && ${HELP_FLAG} == "FALSE" ) then
  set PHA_FILES = ( $argv[1] )
  set PS_FILE   = "${TMP_DIR}/${PHA_FILES[1]:t:r}.ps"
  set PDF_FILE  = "${TMP_DIR}/${PHA_FILES[1]:t:r}.pdf"
  set PDF_FLAG = "TRUE"
else if ( $#argv >= 2 && ${HELP_FLAG} == "FALSE" ) then
  set PHA_FILES = ( $argv )
  if ( "${PHA_FILES[$#PHA_FILES]:e}" == "ps" ) then
    set PS_FILE = "${PHA_FILES[$#PHA_FILES]}"
    set PDF_FLAG = "FALSE"
    set PHA_FILES[$#PHA_FILES] = ""
    set PHA_FILES = ( $PHA_FILES )
  else if ( "${PHA_FILES[$#PHA_FILES]:e}" == "pdf") then
    set PS_FILE  = "${TMP_DIR}/${PHA_FILES[1]:t:r}.ps"
    set PDF_FILE = "${PHA_FILES[$#PHA_FILES]}"
    set PDF_FLAG = "TRUE"
    set PHA_FILES[$#PHA_FILES] = ""
    set PHA_FILES = ( $PHA_FILES )
  else
    set PS_FILE  = "${TMP_DIR}/${PHA_FILES[1]:t:r}.ps"
    set PS_FILE  = "${TMP_DIR}/${PHA_FILES[1]:t:r}.pdf"
  endif
else
  echo "A quick script that uses XSPEC to produce quick plots of spectra"
  echo 'Quick    Useage: plot_xspectrum.csh PHA_FILES'
  echo 'Advanced Useage: plot_xspectrum.csh [options] PHA_FILES [PS/PDF_FILE]'
  echo "Options: -style 'style_name'     - what type of plot (eeufspec,ldata,ratio...)" 
  echo "       : -nd|-nodisplay          - do not launch gv "
  echo "       : -n|-nsigma  NUM         - Rebin data to SNR of Num "
  echo "       : -wdata  FILENAME        - Output wdata filename"
  echo "       : -log    FILENAME        - Output logfile filename"
  echo "       : -stat 'stat_type'       - statistic type (cstat,chi)"
  echo "       : -errors delchi          - calculate proper errors for the given delta chi2"
  echo "       : -emin|-elo              - ignore photons below this limit "
  echo "       : -emax|-ehi              - ignore photons above this limit "
  echo "       : -back|-background       - plot the background spectrum too "
  echo "       : -fitpow                 - fit a powerlaw to the spectrum "
  echo "       : -fitabspow              - fit an absorbed powerlaw to the spectrum "
  echo "       : -fitpcabspow            - fit a partially covering absorbed powerlaw to the spectrum "
  echo "       : -fitgeneral             - fit a partially covering warm absorbed powerlaw+line+refl"
  echo "       : -redshift               - use this redshift value"
  echo "       : -gal|-galNH|-NHgal      - use this Galactic Nh value(1e22/cm2)"
  echo "       : -G|-gamma|-Gamma        - use this slope (Gamma of powerlaw)"
  echo "       : -stay|-stayinxspec      - leave the user in the xspec terminal"
  echo "       : -untie                  - fit the spectra independently"
  echo "       : -help|-h                - display this help info"
  exit 1 
endif

if ( $PDF_FLAG == "TRUE" ) then
  ##check that can convert from ps2pdf
  set PS2PDF = "ps3pdf"
  if ( "`which $PS2PDF`" == "" ) then
    set PS2PDF = "pstopdf"
  endif
  if ( "`which $PS2PDF`" == "" ) then
    echo "Error, you don't seem to have 'ps2pdf' or 'pstopdf' available to create pdf files, defaulting to ps output"
    set PDF_FLAG = "FALSE"
    unset PDF_FILE
  endif
endif

###################################################
if ( $DISPLAY_PLOTFILE == "TRUE" ) then
  if ( $PDF_FLAG == "TRUE" ) then
    set PDF_VIEW = "open"
    if ( "`which $PDF_VIEW`" == "" ) then
      set PDF_VIEW = "acroread"
    endif
    if ( "`which $PDF_VIEW`" == "" ) then
      set PDF_VIEW = "evince"
    endif
    if ( "`which $PDF_VIEW`" == "" ) then
      echo "Error, you don't seem to have 'acroread', 'evince' or 'open' available to display pdf files"
      exit 1
    endif

  else
    set GV = "gv"
    if ( "`which $GV`" == "" ) then
      set GV = "open"
    endif
    if ( "`which $GV`" == "" ) then
      echo "Error, you don't seem to have 'gv' or 'open' available to display ghostscript files"
      exit 1
    endif
  endif
endif
###################################################

set XCM_FILE =  ${TMP_DIR}/${PHA_FILES[1]:t:r}.xcm
if ( -e $XCM_FILE ) rm $XCM_FILE  
if ( -e ${XCM_FILE:r}.$PLOT_STYLE_NOGAPS ) rm ${XCM_FILE:r}.$PLOT_STYLE_NOGAPS
 
if ( -e "$PS_FILE" ) then
  if ( "${PS_FILE:e}" == 'ps' ) then 
    rm $PS_FILE
  else
    echo "Error: your output ps file already exists and is called ${PS_FILE}"
    exit 1
  endif
endif

if ( $?PDF_FILE ) then
  if ( -e "$PDF_FILE" ) then
    if ( "${PDF_FILE:e}" == 'pdf' ) then 
      rm $PDF_FILE
    else
      echo "Error: your output pdf file already exists and is called ${PDF_FILE}"
      exit 1
    endif
  endif
endif

set DO_FIT = "FALSE"
if ( "$FIT_POW" == "TRUE" ||  "$FIT_ABSPOW" == "TRUE"||  "$FIT_PCABSPOW" == "TRUE" ||  "$FIT_GENERAL" == "TRUE"  ) then
  set DO_FIT = "TRUE"
  echo "Will attempt to fit a model to the data" 
endif

echo "#cpd /xs\
setplot energy\
setplot command re x 0.2 12.0"  >> $XCM_FILE 

if ( "$PLOT_STYLE" == "eeufspec" ) then
  echo "model powerlaw\
0\
1" >> $XCM_FILE  
endif

if ( `echo "$PLOT_STYLE" | grep -c ratio` > 0 ) then
  echo "model powerlaw\
$GAMMA\
1e-3" >> $XCM_FILE  
endif

###########################################
if ( $DO_FIT == "TRUE") then
  if      ( $FIT_POW == "TRUE" ) then
    echo "model phabs*powerlaw\
$GALNH -1\
$GAMMA\
1e-3" >> $XCM_FILE  
###########################################
  else if ( $FIT_ABSPOW == "TRUE" ) then
    echo "model phabs*zphabs*powerlaw\
$GALNH -1\
1\
$REDSHIFT -1\
$GAMMA ${GAMMA_FIX_FLAG}\
1e-3" >> $XCM_FILE  
###########################################
  else if ( $FIT_PCABSPOW == "TRUE" ) then
    echo "model phabs*zpcfabs*(powerlaw + zgauss)\
$GALNH -1\
1.0\
0.9\
$REDSHIFT -1\
$GAMMA ${GAMMA_FIX_FLAG}\
1e-3\
6.40 -1\
0.01 -1\
=4\
0.0 -1" >> $XCM_FILE  
###########################################
  else if ( $FIT_GENERAL == "TRUE" ) then
    echo "model phabs*zxipcf*(power + pexrav + zgauss)\
$GALNH -1\
1.0\
-3.0 -1\
1.0 -1\
$REDSHIFT -1\
$GAMMA ${GAMMA_FIX_FLAG}\
1e-3\
$GAMMA -1\
300 -1\
-1.0,-1,-1,-1\
=5\
1 -1\
1 -1\
0.45 -1\
=7\
6.40 -1\
0.01 -1\
=5\
=7 * 1e-2" >> $XCM_FILE  
############################################
#  else if ( $FIT_GENERAL == "TRUE" ) then
#    echo "model phabs*zxipcf*(pexrav + zgauss)\
#$GALNH -1\
#1.0\
#-3.0 -1\
#0.99 -1\
#$REDSHIFT\
#$GAMMA ${GAMMA_FIX_FLAG}\
#300 -1\
#1.0 -1\
#=5\
#1 -1\
#1 -1\
#0.45 -1\
#1e-3\
#6.40 -1\
#0.01 -1\
#=5\
#=13 * 1e-2" >> $XCM_FILE  
###########################################
  else
    echo "Error"
    exit 1 
  endif
endif

set MAX_SPECTRAL_CHANNELS = 1000
if ( $NSIGMA <= 0 || "$NSIGMA" == "0.0" ) then
  set MAX_SPECTRAL_CHANNELS = 1
endif

if ( $PLOT_BACK == "TRUE" ) then
  echo "setplot back" >> $XCM_FILE 
endif

set I = 1
set J = 0
while ( $I <= $#PHA_FILES ) 
  if ( ! -e "$PHA_FILES[$I]" ) then
    echo "Warning: cannot find $PHA_FILES[$I]"
  else
    @ J ++
    echo "Input PHA file:               ${PHA_FILES[$I]}"
    echo "cd $ORIG_DIR" >> $XCM_FILE 
    if ( "${PHA_FILES[$I]}" != "${PHA_FILES[$I]:t}") then
      echo "cd ${PHA_FILES[$I]:h}" >> $XCM_FILE 
    endif 
    echo "data ${J}:${J} ${PHA_FILES[$I]:t}\
setplot rebin $NSIGMA $MAX_SPECTRAL_CHANNELS $J" >> $XCM_FILE  
  endif
  @ I ++
end
echo "ignore bad" >> $XCM_FILE

##check if some pha files were actually read in
if ( $J == 0 ) then
  echo "Error, none of the input pha files existed!"
  exit 1
endif


if ( "$EMIN" != "FALSE" ) then
  echo "ignore **:0.0-${EMIN}" >> $XCM_FILE
endif

if ( "$EMAX" != "FALSE" ) then
  echo "ignore **:${EMAX}-1000." >> $XCM_FILE
endif

echo "show rates\
statistic $STAT\
\
\
" >> $XCM_FILE 

if ( `echo "$PLOT_STYLE" | grep -c ratio` > 0 ) then
  echo "renorm" >> $XCM_FILE  
endif

if ( $DO_FIT == "TRUE" ) then
  echo "query yes\
fit 100" >>  $XCM_FILE 

  if ( "$PROPER_ERRORS" == "FALSE" ) then
    #skip it
  else
    echo "fit 1" >>  $XCM_FILE 
    if ( $STAY_IN_XSPEC != "TRUE" ) echo "placeholder_proper_errors_start " >>  $XCM_FILE 
    echo "error $PROPER_ERRORS 1\
error $PROPER_ERRORS 2\
error $PROPER_ERRORS 3\
error $PROPER_ERRORS 4\
error $PROPER_ERRORS 5\
error $PROPER_ERRORS 6\
error $PROPER_ERRORS 7\
error $PROPER_ERRORS 8\
error $PROPER_ERRORS 9\
error $PROPER_ERRORS 10" >>  $XCM_FILE 
    if ( $STAY_IN_XSPEC != "TRUE" ) echo "placeholder_proper_errors_stop" >>  $XCM_FILE 

  endif


  if ( $STAY_IN_XSPEC != "TRUE" ) echo " placeholder_final_fit_start" >>  $XCM_FILE 

  echo "fit 1" >>  $XCM_FILE 
  if ( $STAY_IN_XSPEC != "TRUE" ) echo " placeholder_final_fit_stop" >>  $XCM_FILE 
  echo "" >> $XCM_FILE  

  if ( $STAY_IN_XSPEC != "TRUE") echo "placeholder_flux_start" >>  $XCM_FILE 
  echo "newpar 1 0.0\
flux 2. 10.\
flux 0.5 2.0\
flux 0.5 10.0\
newpar 1 $GALNH -1" >>  $XCM_FILE 
  if ( $STAY_IN_XSPEC != "TRUE") echo "placeholder_flux_stop" >>  $XCM_FILE 


endif

if ( "$WDATA_FILE" == "DEFAULT" ) then
  set WDATA_FILE = "${XCM_FILE:r}.${PLOT_STYLE_NOGAPS}.wdata"
endif
if ( -e "$WDATA_FILE" ) rm "$WDATA_FILE"

if ( "$LOG_FILE" == "DEFAULT" ) then
  set LOG_FILE = "${XCM_FILE:r}.log"
endif

if ( $PDF_FLAG == "TRUE" ) then
  echo "Resultant plotfile is at:     $PDF_FILE \
Working files are: PS_FILE    ${PS_FILE}\
                   XCM_FILE   ${XCM_FILE}\
                   LOG_FILE   ${LOG_FILE}\
                   WDATA_FILE ${WDATA_FILE}"
else
  echo "Resultant plotfile is at:     $PS_FILE \
Working files are: XCM_FILE   ${XCM_FILE}\
                   LOG_FILE   ${LOG_FILE}\
                   WDATA_FILE ${WDATA_FILE}"
endif


echo "cd $ORIG_DIR" >> $XCM_FILE 


if ( $STAY_IN_XSPEC == "TRUE" ) then 
  echo "cpd /xw \
plot $PLOT_STYLE" >> $XCM_FILE  
else
  echo "cpd ${PS_FILE}/cps\
plot $PLOT_STYLE\
cpd /NULL\
iplot\
wdata ${WDATA_FILE}\
quit\
quit\
y\
" >> $XCM_FILE  

  ##add on some other models to make manual labour easier
  echo "\
model phabs*zphabs*(power)\
$GALNH -1\
$REDSHIFT -1\
1.0\
1.9\
1e-5" >>  $XCM_FILE  

  echo "\
model phabs*zphabs*(power+zgauss)\
$GALNH -1\
1\
$REDSHIFT -1\
1.9\
1e-5\
6.4 -1\
0.0 -1\
=3\
1e-6" >>  $XCM_FILE  

  echo "\
model phabs*zpcfabs*(power+zgauss)\
$GALNH -1\
1\
0.9\
$REDSHIFT -1\
1.9\
1e-5\
6.4 -1\
0.0 -1\
=4\
1e-6" >>  $XCM_FILE  

  echo "\
model phabs*zpcfabs*(power+zgauss+pexrav)\
$GALNH -1\
1\
0.9\
$REDSHIFT -1\
1.9\
1e-5\
6.4 -1\
0.0 -1\
=4\
1e-6\
1.9 -1\
300 -1\
-1,-1,-1,0,0\
=4\
1 -1\
1 -1\
0.45 -1\
1e-6" >>  $XCM_FILE  

  echo "\
model phabs*zxipcf*(pexrav + zgauss)\
$GALNH -1\
1.0\
-3.0 -1\
0.99 -1\
$REDSHIFT\
$GAMMA ${GAMMA_FIX_FLAG}\
300 -1\
1.0 -1\
=5\
1 -1\
1 -1\
0.45 -1\
1e-3\
6.40 -1\
0.0 -1\
=5\
=13 * 1e-2" >> $XCM_FILE  

endif

cd ${TMP_DIR}


if ( $STAY_IN_XSPEC == "TRUE" ) then
  xspec - ${XCM_FILE}
else
  xspec < $XCM_FILE  >& ${LOG_FILE}

  if ( $DO_FIT == "TRUE") then
    echo "---------------------FITTING START--------------------------------"
    $AWK '/placeholder_final_fit_start/{flag=1} /placeholder_final_fit_stop/ || $0~/^XSPEC12>n/ {flag=0} flag==1 && $0~/^Model /{flag=2} flag==2 && NF > 0 {print $0}' ${LOG_FILE}
    echo "---------------------FITTING STOP---------------------------------"
  endif

  cd $ORIG_DIR
  if ( -e ${PS_FILE} ) then
    if ( $PDF_FLAG == "TRUE" ) then
      if ( $PS2PDF == "ps2pdf" ) then
        ps2pdf ${PS_FILE} ${PDF_FILE}
      else
        pstopdf ${PS_FILE} -o ${PDF_FILE}
      endif
      if ( -e ${PDF_FILE} ) then
        if ( "$DISPLAY_PLOTFILE" == "TRUE" ) then 
          $PDF_VIEW ${PDF_FILE}
        endif
      else
        echo "Error: I didn't produce the pdf file $PDF_FILE, look at ${LOG_FILE}"
      endif
    else
      if ( "$DISPLAY_PLOTFILE" == "TRUE" ) then 
        $GV ${PS_FILE}
      endif
    endif
  else
    echo "Error: I didn't produce the ps file $PS_FILE!, look at ${LOG_FILE}"
    exit 1
  endif
endif







exit

