#! /bin/tcsh -f

echo "******************"
echo "*  start CF.csh  *"
echo "******************"
date

cd $start_dir/
set det=$result_dir/$dir0/detection

if ( -e $result_dir/$dir0/ ) then
  echo "OK: check directory "$result_dir/$dir0"/ done"
else
  echo "============================================================"
  echo "ERROR: check "$result_dir/$dir0"/ failed"
  echo "============================================================"
  break
endif

cd $result_dir/$dir0/

if ( $model_fix == 0) then
  rm $result_dir/$dir0/NH.log
  rm $det/nh.txt
  rm $det/nh
  set evt=`awk -v nr=1 '{ if (NR == nr) print $1}' $det/list_evt_"$dir0".txt`

  fkeypar $det/$evt+0 RA_OBJ
  set RA = `pget fkeypar.par value`
  fkeypar $det/$evt+0 DEC_OBJ
  set DEC = `pget fkeypar.par value`

  echo "Calculate Nh in the direction: Ra="$RA" Dec="$DEC""
  echo "nh equinox=2000 ra="$RA" dec="$DEC"" > $det/nh.csh
  source $det/nh.csh > $det/nh.txt
  set nh=`cat $det/nh.txt | grep "Average" | awk '{print $6}' | cut -c 1-4 | awk '{print $1/10}'`
  echo ""$dir0" Nh: $nh x 10^22 cm^-2"
  echo ""$dir0" Nh: $nh x 10^22 cm^-2" >> $result_dir/$dir0/NH.log
  echo $nh > $det/nh
else
  echo "Model fixed"
endif

if ($ambient == "cluster") then
 set LLP="$LD_LIBRARY_PATH"
 set LLPX="/usr/lib64:$LLP"
 setenv LD_LIBRARY_PATH $LLPX
endif

if ( $model_fix == 0) then

echo "Model not fixed in setup file"

if ( $DET == 1 ) then
#MOS1
rm $result_dir/$dir0/Model_not_fixed

   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" MOS1): $model_spect" >> $result_dir/$dir0/Model_not_fixed

rm $det/cf1.txt

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`
   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS1 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS1 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf1.txt

   set b = ` expr $b + 1`
end

echo "MOS1"
echo CF: `awk '{print $3}' $det/cf1.txt`

#MOS2
cd $result_dir/$dir0/

rm $det/cf2.txt
   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" MOS2): $model_spect" >> $result_dir/$dir0/Model_not_fixed

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS2 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS2 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf2.txt

   set b = ` expr $b + 1`
end

echo "MOS2"
echo CF: `awk '{print $3}' $det/cf2.txt`
endif #DET=1

if ( $DET == 01 ) then
#MOS1
rm $result_dir/$dir0/Model_not_fixed

   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" MOS1): $model_spect" >> $result_dir/$dir0/Model_not_fixed

rm $det/cf1.txt

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`
   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS1 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS1 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf1.txt

   set b = ` expr $b + 1`
end

echo "MOS1"
echo CF: `awk '{print $3}' $det/cf1.txt`
endif

if ( $DET == 02 ) then
#MOS2
cd $result_dir/$dir0/

rm $det/cf2.txt
   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" MOS2): $model_spect" >> $result_dir/$dir0/Model_not_fixed

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS2 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS2 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf2.txt

   set b = ` expr $b + 1`
end

echo "MOS2"
echo CF: `awk '{print $3}' $det/cf2.txt`
endif #DET=02

if ( $DET == 03 ) then
#PN
rm $result_dir/$dir0/Model_not_fixed

rm $det/cf3.txt
   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" PN): $model_spect" >> $result_dir/$dir0/Model_not_fixed

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_PN" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect3" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_PN | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_PN | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf3.txt

   set b = ` expr $b + 1`
end

echo "PN"
echo CF: `awk '{print $3}' $det/cf3.txt`
endif #DET=03


if ( $DET == 2 ) then

#MOS1
rm $result_dir/$dir0/Model_not_fixed
rm $det/cf1.txt

   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" MOS1): $model_spect" >> $result_dir/$dir0/Model_not_fixed

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS1 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS1 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf1.txt
   set b = ` expr $b + 1`
end

echo "MOS1"
echo CF: `awk '{print $3}' $det/cf1.txt`

#MOS2
cd $result_dir/$dir0/

rm $det/cf2.txt
   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" MOS2): $model_spect" >> $result_dir/$dir0/Model_not_fixed

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS2 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS2 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf2.txt

   set b = ` expr $b + 1`
end

echo "MOS2"
echo CF: `awk '{print $3}' $det/cf2.txt`

#PN

   set NH=`awk '{print $1}' $det/nh`

   set model_spect = `echo wabs"$p1"pow"$p2" "$et" "$NH" "$et" "$spi" "$et" 1 `
   echo ""$dir0" (Nh "$NH" PN): $model_spect" >> $result_dir/$dir0/Model_not_fixed

rm $det/cf3.txt
set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_PN" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect3" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_PN | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS2 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf3.txt

   set b = ` expr $b + 1`
end

echo "PN"
echo CF: `awk '{print $3}' $det/cf3.txt`
endif #DET=2


else ##if ($model_fix)

echo "Model fixed in setup file"

if ( $DET == 1 || $DET == 01) then


#MOS1
rm $result_dir/$dir0/Model_not_fixed

  rm $det/cf1.txt

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS1 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS1 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf1.txt

   set b = ` expr $b + 1`
end

echo "MOS1"

#MOS2
cd $result_dir/$dir0/

  rm $det/cf2.txt

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS2 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS2 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf2.txt

   set b = ` expr $b + 1`
end

echo "MOS2"
endif #DET=1

if ( $DET == 2 || $DET == 02) then

#PN

  rm $det/cf3.txt


set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_PN" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect3" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_PN | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_PN | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf3.txt

   set b = ` expr $b + 1`
end

echo "PN"
endif #DET=2


if ( $DET == 3 || $DET == 03) then
#MOS1
  rm $det/cf1.txt

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)
echo "MOS1 band $b"

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect1" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "backgrnd "$backg1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf1"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS1 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS1 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf1.txt

   set b = ` expr $b + 1`
end

echo "MOS1"

#MOS2
cd $result_dir/$dir0/

  rm $det/cf2.txt

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)
echo "MOS2 band $b"

   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_MOS2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect2" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "backgrnd "$backg2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf2"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh

   set count_rate = `grep "Model predicted rate" log_CF_"$b"_MOS2 | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_MOS2 | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf2.txt

   set b = ` expr $b + 1`
end

echo "MOS2"

#PN

  rm $det/cf3.txt

set b = 1
foreach band (`cat $result_dir/$dir0/energy_band_d.txt`)
echo "PN band $b"
   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band_d.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band_d.txt`

   
   echo "#! /bin/tcsh -f" > $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "xspec << END" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "source  $result_dir/$dir0/script_main_CF_"$b".xcm" >> $result_dir/$dir0/script_spettro_CF_"$b".csh
   echo "OK: script_spettro_CF_"$b".csh done"
   chmod 755 $result_dir/$dir0/script_spettro_CF_"$b".csh

   echo "source $result_dir/$dir0/script_subr_CF_"$b".xcm" > $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log log_CF_"$b"_PN" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "script_subr_CF_"$b" $spect3" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "log none" >> $result_dir/$dir0/script_main_CF_"$b".xcm
   echo "quit" >> $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "OK: script_main_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_main_CF_"$b".xcm

   echo "set xs_return_result 1" > $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "proc script_subr_CF_"$b" {spec} {" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "data "$dol"{spec}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "backgrnd "$backg3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "response "$resp3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "arf "$arf3"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "cpd /PS" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "ignore **-"$bmin" "$bmax"-**" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "query yes" >> $result_dir/$dir0/script_subr_CF_"$b".xcm  
   echo "model "$model_spect"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm 
   echo "show rates" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "flux "$bmin" "$bmax"" >> $result_dir/$dir0/script_subr_CF_"$b".xcm
   echo "}" >> $result_dir/$dir0/script_subr_CF_"$b".xcm

   echo "OK: script_subr_CF_"$b".xcm done"
   chmod 755 $result_dir/$dir0/script_subr_CF_"$b".xcm

   source $result_dir/$dir0/script_spettro_CF_"$b".csh


   set count_rate = `grep "Model predicted rate" log_CF_"$b"_PN | cut -c 24-32`
   set flux = `grep "Model Flux" log_CF_"$b"_PN | cut -c 33-42`
   set cf=`echo "$flux" "$count_rate" | awk '{printf("%12.9f",($1/$2)*100000000000)}'`       
   if ($cf == 0.000000000) then
    set cf=0.001
   endif
   echo $flux $count_rate $cf >> $det/cf3.txt

   set b = ` expr $b + 1`
end

echo "PN"
endif #DET=2

endif ## if (model_fix)
cd $start_dir/

if ($ambient == "cluster") then
 setenv LD_LIBRARY_PATH $LLP
endif

date
echo "stop CF.csh"
