#! /bin/tcsh -f

echo "**********************************"
echo "*  start total_exposureEXPO.csh  *"
echo "**********************************"
date

cd $result_dir/
set det = $result_dir/$dir0/detection
cd $det/
setenv SAS_ODF $data_dir/$dir0
setenv SAS_CCF $det/ccf.cif
 
if ( $DET == 01 ) then   
  set list_evt="list_evt_${dir0}_MOS1.txt"
endif
if ( $DET == 02 ) then   
  set list_evt="list_evt_${dir0}_MOS2.txt"
endif
if ( $DET == 03 ) then   
  set list_evt="list_evt_${dir0}_PN.txt"
endif
if ( $DET == 1 ) then   
  set list_evt="list_evt_${dir0}_MOS.txt"
endif

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 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 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`
         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`

            # echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
            # echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$mos"_"$type"_"$b".fits imageset="$dir0"_image"$mos"_"$type"_"$b".fits pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            # source $det/make_expo_"$dir0".csh
            # rm $det/make_expo_"$dir0".csh
            # echo "OK: "$dir0"_expo"$mos"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
             
             #EXPO NO VIGN
              echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
              echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$mos"_"$type"_"$b".fits imageset="$dir0"_image"$mos"_"$type"_"$b".fits pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
              source $det/make_expo_"$dir0".csh
              rm $det/make_expo_"$dir0".csh
              echo "OK: "$dir0"_expo_novign"$mos"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
              
             # if ($mos == "MOS1") then
             #  if (-r $pps_archive_dir/$dir0/pps/P${dir0}M1${type}EXPMAP8000.FTZ) then
             #   gunzip -c $pps_archive_dir/$dir0/pps/P${dir0}M1${type}EXPMAP8000.FTZ > "$dir0"_expo_novign"$mos"_"$type"_"$b"_SP.fits
             #  endif
             # endif
             # if ($mos == "MOS2") then
             #  if (-r $pps_archive_dir/$dir0/pps/P${dir0}M2${type}EXPMAP8000.FTZ) then
             #   gunzip -c $pps_archive_dir/$dir0/pps/P${dir0}M2${type}EXPMAP8000.FTZ > "$dir0"_expo_novign"$mos"_"$type"_"$b"_SP.fits
             #  endif
             # endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_expo"$mos"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_expo"$mos"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              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"

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

   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`
         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
            # echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
            # if ($case == 1 || $case == 2) then
            #  echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$pn"_"$type"_"$b".fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            #  source $det/make_expo_"$dir0".csh
            #  rm $det/make_expo_"$dir0".csh
            #  echo "OK: "$dir0"_expo"$pn"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
            # endif
            # if ($case == 3)  then
            #  echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$pn"_"$type"_"$b"_inter1.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits pimax=1000 pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            #  echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$pn"_"$type"_"$b"_inter2.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits pimax=$bmax pimin=1000 withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            #  source $det/make_expo_"$dir0".csh
            #  rm $det/make_expo_"$dir0".csh
            #  echo "imweightadd imagesets="$vir" "$dir0"_expo"$pn"_"$type"_"$b"_inter1.fits "$dir0"_expo"$pn"_"$type"_"$b"_inter2.fits "$vir" outimageset=inter_expo.fits withweights=no -w "$warn" -V "$verb"" > $det/imweightadd.csh
            #  source $det/imweightadd.csh
            #  rm $det/imweightadd.csh
            #  echo "fcarith const=2 infile=inter_expo.fits ops=DIV outfil="$dir0"_expo"$pn"_"$type"_"$b".fits clobber=yes" > $det/fcarith.csh
            #  source $det/fcarith.csh
            #  rm $det/fcarith.csh
            # endif
             #EXPO NO VIGN
              echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
              if ($case == 1 || $case == 2) then
               echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$pn"_"$type"_"$b".fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
               source $det/make_expo_"$dir0".csh
               rm $det/make_expo_"$dir0".csh
               echo "OK: "$dir0"_expo_novign"$pn"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
              endif
              if ($case == 3)  then
               echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$pn"_"$type"_"$b"_inter1.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits pimax=1000 pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
               echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$pn"_"$type"_"$b"_inter2.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits pimax=$bmax pimin=1000 withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
               source $det/make_expo_"$dir0".csh
               rm $det/make_expo_"$dir0".csh
               echo "imweightadd imagesets="$vir" "$dir0"_expo_novign"$pn"_"$type"_"$b"_inter1.fits "$dir0"_expo_novign"$pn"_"$type"_"$b"_inter2.fits "$vir" outimageset=inter_expo_novign.fits withweights=no -w "$warn" -V "$verb"" > $det/imweightadd.csh
               source $det/imweightadd.csh
               rm $det/imweightadd.csh
               echo "fcarith const=2 infile=inter_expo_novign.fits ops=DIV outfil="$dir0"_expo_novign"$pn"_"$type"_"$b".fits clobber=yes" > $det/fcarith.csh
               source $det/fcarith.csh
               rm $det/fcarith.csh
              endif
             # if ($pn == "PN") then
             #  if (-r $pps_archive_dir/$dir0/pps/P${dir0}PN${type}EXPMAP8000.FTZ) then
             #   gunzip -c $pps_archive_dir/$dir0/pps/P${dir0}PN${type}EXPMAP8000.FTZ > "$dir0"_expo_novign"$pn"_"$type"_"$b"_SP.fits
             #  endif
             # endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_expo"$pn"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_expo"$pn"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              endif

         #set b = ` expr $b + 1`
         #end
      end
rm $det/*inter* 
   endif
 endif #DET=03

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

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

 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 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 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`
         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`

            # echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
            # echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$mos"_"$type"_"$b".fits imageset="$dir0"_image"$mos"_"$type"_"$b".fits pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            # source $det/make_expo_"$dir0".csh
            # rm $det/make_expo_"$dir0".csh
            # echo "OK: "$dir0"_expo"$mos"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"

             #EXPO NO VIGN
              echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
              echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$mos"_"$type"_"$b".fits imageset="$dir0"_image"$mos"_"$type"_"$b".fits pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
              source $det/make_expo_"$dir0".csh
              rm $det/make_expo_"$dir0".csh
              echo "OK: "$dir0"_expo_novign"$mos"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"

             # if ($mos == "MOS1") then
             #  if (-r $pps_archive_dir/$dir0/pps/P${dir0}M1${type}EXPMAP8000.FTZ) then
             #   gunzip -c $pps_archive_dir/$dir0/pps/P${dir0}M1${type}EXPMAP8000.FTZ > "$dir0"_expo_novign"$mos"_"$type"_"$b"_SP.fits
             #  endif
             # endif
             # if ($mos == "MOS2") then
             #  if (-r $pps_archive_dir/$dir0/pps/P${dir0}M2${type}EXPMAP8000.FTZ) then
             #   gunzip -c $pps_archive_dir/$dir0/pps/P${dir0}M2${type}EXPMAP8000.FTZ > "$dir0"_expo_novign"$mos"_"$type"_"$b"_SP.fits
             #  endif
             # endif
             
              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_expo"$mos"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_expo"$mos"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              endif

         #set b = ` expr $b + 1`
         #end
      end
   endif    
 else
  echo "ATTENTION: the standard calculation of the MOS exposure maps is not performed"
 endif  
#PN 
   echo "OBS $dir0"

   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`
         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
            # echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
            # if ($case == 1 || $case == 2) then
            #  echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$pn"_"$type"_"$b".fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits pimax=$bmax pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            #  source $det/make_expo_"$dir0".csh
            #  rm $det/make_expo_"$dir0".csh
            #  echo "OK: "$dir0"_expo"$pn"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
            # endif
            # if ($case == 3)  then
            #  echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$pn"_"$type"_"$b"_inter1.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits pimax=1000 pimin=$bmin withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            #  echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo"$pn"_"$type"_"$b"_inter2.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits pimax=$bmax pimin=1000 withvignetting=yes -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
            #  source $det/make_expo_"$dir0".csh
            #  rm $det/make_expo_"$dir0".csh
            #  echo "imweightadd imagesets="$vir" "$dir0"_expo"$pn"_"$type"_"$b"_inter1.fits "$dir0"_expo"$pn"_"$type"_"$b"_inter2.fits "$vir" outimageset=inter_expo.fits withweights=no -w "$warn" -V "$verb"" > $det/imweightadd.csh
            #  source $det/imweightadd.csh
            #  rm $det/imweightadd.csh
            #  echo "fcarith const=2 infile=inter_expo.fits ops=DIV outfil="$dir0"_expo"$pn"_"$type"_"$b".fits clobber=yes" > $det/fcarith.csh
            #  source $det/fcarith.csh
            #  rm $det/fcarith.csh
            # endif
             #EXPO NO VIGN
              echo "#! /bin/tcsh -f" > $det/make_expo_"$dir0".csh
              if ($case == 1 || $case == 2) then
               echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$pn"_"$type"_"$b".fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits pimax=$bmax pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
               source $det/make_expo_"$dir0".csh
               rm $det/make_expo_"$dir0".csh
               echo "OK: "$dir0"_expo_novign"$pn"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
              endif
              if ($case == 3)  then
               echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$pn"_"$type"_"$b"_inter1.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits pimax=1000 pimin=$bmin withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
               echo "/usr/bin/time -p eexpmap attitudeset=$Hk attrebin=2 eventset=$evt expimageset="$dir0"_expo_novign"$pn"_"$type"_"$b"_inter2.fits imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits pimax=$bmax pimin=1000 withvignetting=no -w "$warn" -V "$verb"" >> $det/make_expo_"$dir0".csh
               source $det/make_expo_"$dir0".csh
               rm $det/make_expo_"$dir0".csh
               echo "imweightadd imagesets="$vir" "$dir0"_expo_novign"$pn"_"$type"_"$b"_inter1.fits "$dir0"_expo_novign"$pn"_"$type"_"$b"_inter2.fits "$vir" outimageset=inter_expo_novign.fits withweights=no -w "$warn" -V "$verb"" > $det/imweightadd.csh
               source $det/imweightadd.csh
               rm $det/imweightadd.csh
               echo "fcarith const=2 infile=inter_expo_novign.fits ops=DIV outfil="$dir0"_expo_novign"$pn"_"$type"_"$b".fits clobber=yes" > $det/fcarith.csh
               source $det/fcarith.csh
               rm $det/fcarith.csh
              endif
             # if ($pn == "PN") then
             #  if (-r $pps_archive_dir/$dir0/pps/P${dir0}PN${type}EXPMAP8000.FTZ) then
             #   gunzip -c $pps_archive_dir/$dir0/pps/P${dir0}PN${type}EXPMAP8000.FTZ > "$dir0"_expo_novign"$pn"_"$type"_"$b"_SP.fits
             #  endif
             # endif
              
              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_expo"$pn"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_expo"$pn"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              endif

         #set b = ` expr $b + 1`
         #end
      end
rm $det/*inter* 
   endif

 endif #DET=2
cd $result_dir/
cd $start_dir/

date
echo "stop total_exposureEXPO.csh"
