#! /bin/tcsh -f

echo "***************************"
echo "*  start sensitivity.csh  *"
echo "***************************"
date

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

setenv SAS_ODF $data_dir/$dir0
setenv SAS_CCF $data_dir/$dir0/ccf.cif

if ($sensitivity == "yes") then
 if ($DET == 1 || $DET == 01 || $DET == 02 ) then
    set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
    set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
    set bkg1 = `ls $det/"$dir0"_MOS1_bkg_*.fits | perl -ne 'print unless $.<2 '`
    set bkg2 = `ls $det/"$dir0"_MOS2_bkg_*.fits | perl -ne 'print unless $.<2 '`
    set mask = `ls $det/"$dir0"_MOS*_detmask_"$b_tot".fits`
    echo "#! /bin/tcsh -f" > $det/sensitivity.csh

    echo "esensmap bkgimagesets='$bkg1 $bkg2' detmasksets='$mask' expimagesets='$expo1 $expo2' mlmin=6 sensimageset=$det/"$dir0"_sensitivity_map_"$b_tot".fits -w "$warn" -V "$verb"" >> $det/sensitivity.csh
   
    source $det/sensitivity.csh
    rm $det/sensitivity.csh
    echo "OK: sensitivity map done in energy band $b_tot"
 endif

 if ($DET == 03) then
    set expo = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_*.fits | perl -ne 'print unless $.<2 '`
    set mask = `ls $det/"$dir0"_PN_detmask_"$b_tot".fits`
    set bkg = `ls $det/"$dir0"_PN_bkg_*.fits | perl -ne 'print unless $.<2 '`
    echo "#! /bin/tcsh -f" > $det/sensitivity.csh

    echo "esensmap bkgimagesets='$bkg' detmasksets='$mask' expimagesets='$expo' mlmin=6 sensimageset=$det/"$dir0"_sensitivity_map_"$b_tot".fits -w "$warn" -V "$verb"" >> $det/sensitivity.csh
    
    source $det/sensitivity.csh
    rm $det/sensitivity.csh
    echo "OK: sensitivity map done in energy band $b_tot"
 endif
   
 if ($DET == 2 ) then
    set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
    set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
    set expo3 = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_*.fits | perl -ne 'print unless $.<2 '`
    set mask1 = `ls $det/"$dir0"_PN_detmask_"$b_tot".fits`
    set mask2 = `ls $det/"$dir0"_MOS*_detmask_"$b_tot".fits` 
    set bkg1 = `ls $det/"$dir0"_MOS1_bkg_*.fits | perl -ne 'print unless $.<2 '` 
    set bkg2 = `ls $det/"$dir0"_MOS2_bkg_*.fits | perl -ne 'print unless $.<2 '` 
    set bkg3 = `ls $det/"$dir0"_PN_bkg_*.fits | perl -ne 'print unless $.<2 '`
    echo "#! /bin/tcsh -f" > $det/sensitivity.csh

    echo "esensmap bkgimagesets='$bkg3 $bkg1 $bkg2' detmasksets='$mask1 $mask2' expimagesets='$expo3 $expo1 $expo2' mlmin=6 sensimageset=$det/"$dir0"_sensitivity_map_"$b_tot".fits -w "$warn" -V "$verb"" >> $det/sensitivity.csh
    
    source $det/sensitivity.csh
    rm $det/sensitivity.csh
    echo "OK: sensitivity map done in energy band $b_tot"
 endif
else
 echo "the calculation of the sensitivity map is not performed"
endif
cd $start_dir/

date
echo "stop sensitivity.csh"
