#! /bin/tcsh -f

echo "*********************************"
echo "*  start esplinemap_LM_bin.csh  *"
echo "*********************************"
date

cd $result_dir/
set ExpDetectPrep = $an_dir/ExpDetectPrep
set det=$result_dir/$dir0/detection
cd $an_dir/
  
setenv SAS_ODF $data_dir/$dir0
setenv SAS_CCF $data_dir/$dir0/ccf.cif

set bmin_tot=`awk -F \; -v nr=$b_tot '{ if (NR==nr) print $1}' $result_dir/$dir0/energy_band.txt`
set bmax_tot=`awk -F \; -v nr=$b_tot '{ if (NR==nr) print $2}' $result_dir/$dir0/energy_band.txt`

if ( $DET == 1 ) then
 echo "N. energy band: ${N_EN_BAND}" 
 set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
 set N_evtM1 = `ls $ExpDetectPrep/ExpDetectPrep_MOS1_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 set N_evtM2 = `ls $ExpDetectPrep/ExpDetectPrep_MOS2_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 echo "N. of MOS1 event files: $N_evtM1"    
 echo "N. of MOS2 event files: $N_evtM2"
 set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits`
 set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits`
if (${ima_btot} == 2) then
if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM1} < ${N_EN_BAND} || ${N_evtM2} < ${N_EN_BAND}) then
    set epic = "MOS1" 
    echo "Performing esplinemap LM with MOS1 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
    set epic = "MOS2" 
    echo "Performing esplinemap LM with MOS2 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM1} == ${N_EN_BAND} && ${N_evtM2} == ${N_EN_BAND}) then
  set epic = "MOS1" 
  echo "Performing esplinemap LM with MOS1 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
  set epic = "MOS2" 
  echo "Performing esplinemap LM with MOS2 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
if (${ima_btot} == 1) then
if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM1} < ${N_EN_BAND}) then
    set epic = "MOS1" 
    echo "Performing esplinemap LM with MOS1 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM1} == ${N_EN_BAND}) then
  set epic = "MOS1" 
  echo "Performing esplinemap LM with MOS1 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM2} < ${N_EN_BAND}) then
    set epic = "MOS2" 
    echo "Performing esplinemap LM with MOS2 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM2} == ${N_EN_BAND}) then
  set epic = "MOS2" 
  echo "Performing esplinemap LM with MOS2 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
endif

if ( $DET == 01 ) then
 echo "N. energy band: ${N_EN_BAND}" 
 set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
 set N_evtM1 = `ls $ExpDetectPrep/ExpDetectPrep_MOS1_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 echo "N. of MOS1 event files: $N_evtM1"    
 set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits`
if (${ima_btot} == 1) then
if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM1} < ${N_EN_BAND}) then
    set epic = "MOS1" 
    echo "Performing esplinemap LM with MOS1 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM1} == ${N_EN_BAND}) then
  set epic = "MOS1" 
  echo "Performing esplinemap LM with MOS1 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
endif

if ( $DET == 02 ) then
 echo "N. energy band: ${N_EN_BAND}" 
 set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
 set N_evtM2 = `ls $ExpDetectPrep/ExpDetectPrep_MOS2_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 echo "N. of MOS2 event files: $N_evtM2"    
 set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits`
if (${ima_btot} == 1) then
if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM2} < ${N_EN_BAND}) then
    set epic = "MOS2" 
    echo "Performing esplinemap LM with MOS2 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM2} == ${N_EN_BAND}) then
  set epic = "MOS2" 
  echo "Performing esplinemap LM with MOS2 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
endif

if ( $DET == 03 ) then
 echo "N. energy band: ${N_EN_BAND}" 
 set ima_btot = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
 set N_evtPN = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 echo "N. of pn event files: $N_evtPN"
 set ima_pn = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits`
if (${ima_btot} == 1) then
if ($ima_pn == "$ExpDetectPrep/ExpDetectPrep_"$epic"_merged_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtPN} < ${N_EN_BAND}) then
    set epic = "PN" 
    echo "Performing esplinemap LM with pn for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtPN} == ${N_EN_BAND}) then
  set epic = "PN" 
  echo "Performing esplinemap LM with pn for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

   set img="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits
   if (-e $img) then 
    echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
   endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
endif

if ( $DET == 2 ) then
 echo "N. energy band: ${N_EN_BAND}" 
 set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
 set N_evtM1 = `ls $ExpDetectPrep/ExpDetectPrep_MOS1_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 set N_evtM2 = `ls $ExpDetectPrep/ExpDetectPrep_MOS2_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 set N_evtPN = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 echo "N. of MOS1 event files: $N_evtM1"    
 echo "N. of MOS2 event files: $N_evtM2"
 echo "N. of pn event files: $N_evtPN"
 set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits`
 set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits`
 set ima_pn = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits`
if (${ima_btot} == 3) then
if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM1} < ${N_EN_BAND} || ${N_evtM2} < ${N_EN_BAND} || ${N_evtPN} < ${N_EN_BAND}) then
    set epic = "MOS1" 
    echo "Performing esplinemap LM with MOS1 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
    set epic = "MOS2" 
    echo "Performing esplinemap LM with MOS2 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
    set epic = "PN" 
    echo "Performing esplinemap LM with pn for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM1} == ${N_EN_BAND} && ${N_evtM2} == ${N_EN_BAND} && ${N_evtPN} == ${N_EN_BAND}) then
  set epic = "MOS1" 
  echo "Performing esplinemap LM with MOS1 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
  set epic = "MOS2" 
  echo "Performing esplinemap LM with MOS2 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
  set epic = "PN" 
  echo "Performing esplinemap LM with pn for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

   set img="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits
   if (-e $img) then 
    echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
   endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
if (${ima_btot} == 2) then
if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM1} < ${N_EN_BAND} || ${N_evtM2} < ${N_EN_BAND}) then
    set epic = "MOS1" 
    echo "Performing esplinemap LM with MOS1 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
    set epic = "MOS2" 
    echo "Performing esplinemap LM with MOS2 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM1} == ${N_EN_BAND} && ${N_evtM2} == ${N_EN_BAND}) then
  set epic = "MOS1" 
  echo "Performing esplinemap LM with MOS1 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
  set epic = "MOS2" 
  echo "Performing esplinemap LM with MOS2 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM1} < ${N_EN_BAND} || ${N_evtPN} < ${N_EN_BAND}) then
    set epic = "MOS1" 
    echo "Performing esplinemap LM with MOS1 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
    set epic = "PN" 
    echo "Performing esplinemap LM with pn for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM1} == ${N_EN_BAND} && ${N_evtPN} == ${N_EN_BAND}) then
  set epic = "MOS1" 
  echo "Performing esplinemap LM with MOS1 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
  set epic = "PN" 
  echo "Performing esplinemap LM with pn for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

   set img="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits
   if (-e $img) then 
    echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
   endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM2} < ${N_EN_BAND} || ${N_evtPN} < ${N_EN_BAND}) then
    set epic = "MOS2" 
    echo "Performing esplinemap LM with MOS2 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
    set epic = "PN" 
    echo "Performing esplinemap LM with pn for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM2} == ${N_EN_BAND} && ${N_evtPN} == ${N_EN_BAND}) then
  set epic = "MOS2" 
  echo "Performing esplinemap LM with MOS2 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
  set epic = "PN" 
  echo "Performing esplinemap LM with pn for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

   set img="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits
   if (-e $img) then 
    echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
   endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
if (${ima_btot} == 1) then
if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM1} < ${N_EN_BAND}) then
    set epic = "MOS1" 
    echo "Performing esplinemap LM with MOS1 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM1} == ${N_EN_BAND}) then
  set epic = "MOS1" 
  echo "Performing esplinemap LM with MOS1 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
if ($ima_pn == "$ExpDetectPrep/ExpDetectPrep_"$epic"_merged_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtPN} < ${N_EN_BAND}) then
    set epic = "PN" 
    echo "Performing esplinemap LM with pn for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtPN} == ${N_EN_BAND}) then
  set epic = "PN" 
  echo "Performing esplinemap LM with pn for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
   set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
   set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

   set img="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits
   if (-e $img) then 
    echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
   endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits") then
 if (${N_evtM2} < ${N_EN_BAND}) then
    set epic = "MOS2" 
    echo "Performing esplinemap LM with MOS2 for total energy band"
    echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b_tot"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b_tot"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b_tot"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin_tot pimax=$bmax_tot fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b_tot scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh
    source $an_dir/esplinemap_LM.csh
    rm $an_dir/esplinemap_LM.csh
 endif
 if (${N_evtM2} == ${N_EN_BAND}) then
  set epic = "MOS2" 
  echo "Performing esplinemap LM with MOS2 for all energy bands"
  set b = 2
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set bmin=`awk -F \; -v nr=$b '{ if (NR == nr) print $1}' $result_dir/$dir0/energy_band.txt`
    set bmax=`awk -F \; -v nr=$b '{ if (NR == nr) print $2}' $result_dir/$dir0/energy_band.txt`

    set img="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits
    if (-e $img) then 
     echo "#! /bin/tcsh -f" > $an_dir/esplinemap_LM.csh

     echo "/usr/bin/time -p esplinemap imageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_image_"$b"_"$start"-"$stop".fits boxlistset="$an_dir"/eboxlist_local_"$start"-"$stop".fits bkgimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits detmaskset="$an_dir"/"$dir0"_"$epic"_detmask_"$b"_"$start"-"$stop".fits withexpimage=yes withexpimage2=no expimageset="$ExpDetectPrep"/ExpDetect_"$epic"_LE_noise_filtered_exponovign_"$b"_"$start"-"$stop".fits ooteventset="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_event_list_"$b_tot"_"$start"-"$stop".fits pimin=$bmin pimax=$bmax fitmethod=$fitmethod_l withdetmask=yes withcheese=yes withcheesemask=yes idband=$b scut=$scut_l mlmin=$mlmin_lb nsplinenodes=$nsplinenodes_l excesssigma=3 nfitrun=$nfitrun_l withootset=true -w "$warn" -V "$verb"" >> $an_dir/esplinemap_LM.csh

     source $an_dir/esplinemap_LM.csh
     rm $an_dir/esplinemap_LM.csh
     echo "OK: "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"
     mv cheese.fits "$ExpDetectPrep"/"$dir0"_"$epic"_cheese_"$b"_"$start"-"$stop".fits
     mv cheesemask.fits "$ExpDetectPrep"/"$dir0"_"$epic"_maskcheese_"$b"_"$start"-"$stop".fits
     if ( ! -e "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits ) then
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist"
      echo "ERROR $dir0 "$dir0"_"$epic"_bkg_"$b"_"$start"-"$stop".fits does not exist FULLDET" >> $log_dir/messages.log
     endif
    endif
  set b = ` expr $b + 1`
  end
  echo "start to make the bkg map in the total energy band $b_tot"
  if (-e $img) then 
   set bkg = `ls $an_dir/"$dir0"_"$epic"_bkg_*_"$start"-"$stop".fits`
   echo "imweightadd imagesets=' "$bkg" ' outimageset="$an_dir"/"$dir0"_"$epic"_bkg_"$b_tot"_"$start"-"$stop".fits tempset="$ExpDetectPrep"/ExpDetectPrep-all-Generic_temporary.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
   source $an_dir/imweightadd.csh
   rm $an_dir/imweightadd.csh
  endif
 endif
endif
endif
endif
cd $start_dir/

date
echo "stop esplinemap_LM_bin.csh"
