#! /bin/tcsh -f

echo "*****************************"
echo "*  start make_expo_bin.csh  *"
echo "*****************************"
date

cd $result_dir/
set det = $result_dir/$dir0/detection
cd $det/
set Hk = `ls $det/*AttHk.ds*`

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

if ( $DET == 1 || $DET == 01 || $DET == 02 ) then   
 echo "OBS $dir0"
 if ($mos_expo_st == "yes") then

   set ctrl = `wc $det/$list_evt | awk '{print $1}'`
    if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det" >> $log_dir/messages.log
      break
    else
      foreach evt (`cat $det/$list_evt`)
         set mos = `echo $evt | cut -c 18-21`
         set type = `echo $evt | cut -c 23-26`
         set code = `echo $evt | cut -c 1-15`
         set Hk = `echo "$code"_AttHk.ds`
         echo "EVENT FILE: $evt"
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)
             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=${dir0}_image"$mos"_${type}_${b}_${start}-${stop}.fits
             if (-e $an_dir/$img) then 
              set expimg=${dir0}_expo"$mos"_${type}_${b}_${start}-${stop}.fits
              echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
              echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/"${expimg}" imageset=${an_dir}/"$img" pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
              source $an_dir/make_expo_${dir0}.csh
              rm $an_dir/make_expo_${dir0}.csh
              echo "OK: "${expimg}" done in energy band (eV) ["$bmin":"$bmax"]"
             
              #EXPO NO VIGN
               set expimgnovign=${dir0}_expo_novign"$mos"_${type}_${b}_${start}-${stop}.fits
               echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/"${expimg}novign" imageset=${an_dir}/"$img" pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               source $an_dir/make_expo_${dir0}.csh
               rm $an_dir/make_expo_${dir0}.csh
               echo "OK: ${expimg}novign done in energy band (eV) ["$bmin":"$bmax"]"

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device=${an_dir}/${dir0}_expo"$mos"_${type}_${b}_${start}-${stop}.PNG/PNG thickness=3 set=${an_dir}/${dir0}_expo"$mos"_${type}_${b}_${start}-${stop}.fits withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
             endif
         set b = ` expr $b + 1`
         end
      end
    endif
 else
  echo "ATTENTION: the standard calculation of the MOS exposure maps is not performed"
 endif  
endif #DET=1

if ($DET == 03) then   
   echo "OBS $dir0"
   set ctrl = `wc $det/$list_evt | awk '{print $1}'`
   if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no evts files in $det"
      echo "============================================================"
      echo "FATAL ERROR: no evts files in $det" >> $log_dir/messages.log
      break
   else
      foreach evt (`cat $det/$list_evt`)
         set pn = `echo $evt | cut -c 18-19`
         set type = `echo $evt | cut -c 23-26`
         set code = `echo $evt | cut -c 1-15`
         set Hk = `echo "$code"_AttHk.ds`
         echo "EVENT FILE: $evt"
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)
             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`
             if ($bmin < 1000 && $bmax <= 1000 ) then
               set case = 1 
             endif
             if ($bmin >= 1000 && $bmax > 1000 ) then
               set case = 2 
             endif
             if ($bmin < 1000 && $bmax > 1000 ) then
               set case = 3 
             endif
             set img=${dir0}_image${pn}_${type}_${b}_${start}-${stop}.fits
             if (-e $an_dir/$img) then 
              set expimg=${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.fits
              echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
              if ($case == 1 || $case == 2) then
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${expimg} imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter.fits pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               source $an_dir/make_expo_${dir0}.csh
               rm $an_dir/make_expo_${dir0}.csh
               echo "OK: "${expimg}" done in energy band (eV) ["$bmin":"$bmax"]"
              endif
              if ($case == 3)  then
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter1.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter1.fits pimax=1000 pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter2.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter2.fits pimax=$bmax pimin=1000 withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               source $an_dir/make_expo_${dir0}.csh
               rm $an_dir/make_expo_${dir0}.csh
               echo "imweightadd imagesets="$vir" ${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter1.fits ${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter2.fits "$vir" outimageset=${an_dir}/inter_expo.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
               source $an_dir/imweightadd.csh
               rm $an_dir/imweightadd.csh
               echo "fcarith const=2 infile=${an_dir}/inter_expo.fits ops=DIV outfil=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.fits clobber=yes" > $an_dir/fcarith.csh
               source $an_dir/fcarith.csh
               rm $an_dir/fcarith.csh
               echo "OK: "${expimg}" done in energy band (eV) ["$bmin":"$bmax"]"
              endif
             #EXPO NO VIGN
               echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
               if ($case == 1 || $case == 2) then
                echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter.fits pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
                source $an_dir/make_expo_${dir0}.csh
                rm $an_dir/make_expo_${dir0}.csh
                echo "OK: ${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits done in energy band (eV) ["$bmin":"$bmax"]"
               endif
               if ($case == 3)  then
                echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter1.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter1.fits pimax=1000 pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
                echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter2.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter2.fits pimax=$bmax pimin=1000 withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
                source $an_dir/make_expo_${dir0}.csh
                rm $an_dir/make_expo_${dir0}.csh
                echo "imweightadd imagesets="$vir" ${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter1.fits ${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter2.fits "$vir" outimageset=${an_dir}/inter_expo_novign.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
                source $an_dir/imweightadd.csh
                rm $an_dir/imweightadd.csh
                echo "fcarith const=2 infile=${an_dir}/inter_expo_novign.fits ops=DIV outfil=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits clobber=yes" > $an_dir/fcarith.csh
                source $an_dir/fcarith.csh
                rm $an_dir/fcarith.csh
                echo "OK: ${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits done in energy band (eV) ["$bmin":"$bmax"]"
               endif
              ##PLOT
              if ($plot1 == 1) then
               echo "implot device=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.PNG/PNG thickness=3 set=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.fits withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
             endif
         set b = ` expr $b + 1`
         end
      end
   endif
endif #DET=03

if ( $DET == 2 ) then
     echo "OBS $dir0"

 if ($mos_expo_st == "yes") then

   set list_evt="list_evt_${dir0}_MOS.txt"
   set ctrl = `wc $det/$list_evt | awk '{print $1}'`
    if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det" >> $log_dir/messages.log
      break
    else
      foreach evt (`cat $det/$list_evt`)
         set mos = `echo $evt | cut -c 18-21`
         set type = `echo $evt | cut -c 23-26`
         set code = `echo $evt | cut -c 1-15`
         set Hk = `echo "$code"_AttHk.ds`
         echo "EVENT FILE: $evt"
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)
             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=${dir0}_image"$mos"_${type}_${b}_${start}-${stop}.fits
             if (-e $an_dir/$img) then 
              set expimg=${dir0}_expo"$mos"_${type}_${b}_${start}-${stop}.fits
              echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
              echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/"${expimg}" imageset=${an_dir}/"$img" pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
              source $an_dir/make_expo_${dir0}.csh
              rm $an_dir/make_expo_${dir0}.csh
              echo "OK: "${expimg}" done in energy band (eV) ["$bmin":"$bmax"]"
             
              #EXPO NO VIGN
               set expimgnovign=${dir0}_expo_novign"$mos"_${type}_${b}_${start}-${stop}.fits
               echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/"${expimg}novign" imageset=${an_dir}/"$img" pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               source $an_dir/make_expo_${dir0}.csh
               rm $an_dir/make_expo_${dir0}.csh
               echo "OK: ${expimg}novign done in energy band (eV) ["$bmin":"$bmax"]"

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device=${an_dir}/${dir0}_expo"$mos"_${type}_${b}_${start}-${stop}.PNG/PNG thickness=3 set=${an_dir}/${dir0}_expo"$mos"_${type}_${b}_${start}-${stop}.fits withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
             endif
         set b = ` expr $b + 1`
         end
      end
    endif
 else
  echo "ATTENTION: the standard calculation of the MOS exposure maps is not performed"
 endif 

   set list_evt="list_evt_${dir0}_PN.txt"
   set ctrl = `wc $det/$list_evt | awk '{print $1}'`
   if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no evts files in $det"
      echo "============================================================"
      echo "FATAL ERROR: no evts files in $det" >> $log_dir/messages.log
      break
   else
      foreach evt (`cat $det/$list_evt`)
         set pn = `echo $evt | cut -c 18-19`
         set type = `echo $evt | cut -c 23-26`
         set code = `echo $evt | cut -c 1-15`
         set Hk = `echo "$code"_AttHk.ds`
         echo "EVENT FILE: $evt"
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)
             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`
             if ($bmin < 1000 && $bmax <= 1000 ) then
               set case = 1 
             endif
             if ($bmin >= 1000 && $bmax > 1000 ) then
               set case = 2 
             endif
             if ($bmin < 1000 && $bmax > 1000 ) then
               set case = 3 
             endif
             set img=${dir0}_image${pn}_${type}_${b}_${start}-${stop}.fits
             if (-e $an_dir/$img) then 
              set expimg=${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.fits
              echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
              if ($case == 1 || $case == 2) then
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${expimg} imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter.fits pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               source $an_dir/make_expo_${dir0}.csh
               rm $an_dir/make_expo_${dir0}.csh
               echo "OK: "${expimg}" done in energy band (eV) ["$bmin":"$bmax"]"
              endif
              if ($case == 3)  then
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter1.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter1.fits pimax=1000 pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter2.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter2.fits pimax=$bmax pimin=1000 withvignetting=yes -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
               source $an_dir/make_expo_${dir0}.csh
               rm $an_dir/make_expo_${dir0}.csh
               echo "imweightadd imagesets="$vir" ${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter1.fits ${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}_inter2.fits "$vir" outimageset=${an_dir}/inter_expo.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
               source $an_dir/imweightadd.csh
               rm $an_dir/imweightadd.csh
               echo "fcarith const=2 infile=${an_dir}/inter_expo.fits ops=DIV outfil=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.fits clobber=yes" > $an_dir/fcarith.csh
               source $an_dir/fcarith.csh
               rm $an_dir/fcarith.csh
               echo "OK: "${expimg}" done in energy band (eV) ["$bmin":"$bmax"]"
              endif
             #EXPO NO VIGN
               echo "#! /bin/tcsh -f" > $an_dir/make_expo_${dir0}.csh
               if ($case == 1 || $case == 2) then
                echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter.fits pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
                source $an_dir/make_expo_${dir0}.csh
                rm $an_dir/make_expo_${dir0}.csh
                echo "OK: ${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits done in energy band (eV) ["$bmin":"$bmax"]"
               endif
               if ($case == 3)  then
                echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter1.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter1.fits pimax=1000 pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
                echo "/usr/bin/time -p eexpmap attitudeset=${det}/"$Hk" attrebin=2 eventset=${det}/"$evt" expimageset=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter2.fits imageset=${an_dir}/${dir0}_image${pn}_${type}_${b}_${start}-${stop}_inter2.fits pimax=$bmax pimin=1000 withvignetting=no -w "$warn" -V "$verb"" >> $an_dir/make_expo_${dir0}.csh
                source $an_dir/make_expo_${dir0}.csh
                rm $an_dir/make_expo_${dir0}.csh
                echo "imweightadd imagesets="$vir" ${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter1.fits ${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}_inter2.fits "$vir" outimageset=${an_dir}/inter_expo_novign.fits withweights=no -w "$warn" -V "$verb"" > $an_dir/imweightadd.csh
                source $an_dir/imweightadd.csh
                rm $an_dir/imweightadd.csh
                echo "fcarith const=2 infile=${an_dir}/inter_expo_novign.fits ops=DIV outfil=${an_dir}/${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits clobber=yes" > $an_dir/fcarith.csh
                source $an_dir/fcarith.csh
                rm $an_dir/fcarith.csh
                echo "OK: ${dir0}_expo_novign${pn}_${type}_${b}_${start}-${stop}.fits done in energy band (eV) ["$bmin":"$bmax"]"
               endif
              ##PLOT
              if ($plot1 == 1) then
               echo "implot device=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.PNG/PNG thickness=3 set=${an_dir}/${dir0}_expo${pn}_${type}_${b}_${start}-${stop}.fits withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
             endif
         set b = ` expr $b + 1`
         end
      end
   rm $an_dir/*inter* 
   endif
endif #DET=2
rm $an_dir/*.csh
cd $result_dir/
cd $start_dir/

date
echo "stop make_expo_bin.csh"
