#! /bin/tcsh -f

echo "**********************************"
echo "*  start total_exposure_stat.csh *"
echo "**********************************"
date

cd $result_dir/
cd $prod_start_dir/$dir0/

##EXPOSURE MAPS
foreach expo (`ls *exponovign*`)
 set epic = `echo $expo | cut -c 12-15`
 if ($epic == "PN00") then
  fimgstat $expo 100 INDEF | grep "sum"  | cut -d'=' -f2 > ${dir0}_expoPN.txt
 else
  if ($epic == "MOS1") then
   fimgstat $expo 100 INDEF | grep "sum"  | cut -d'=' -f2 > ${dir0}_expoMOS1.txt
  endif
  if ($epic == "MOS2") then
   fimgstat $expo 100 INDEF | grep "sum"  | cut -d'=' -f2 > ${dir0}_expoMOS2.txt
  endif
 endif
end

##CHEESED EXPOSURE MAPS
foreach expo_cheese (`ls *cheese*`)
 set epic = `echo $expo_cheese | cut -c 11-12`
 if ($epic == "PN") then
  fimgstat $expo_cheese 100 INDEF | grep "sum"  | cut -d'=' -f2 >> ${dir0}_expoPN_ccd.txt
 endif
 if ($epic == "M1") then
  fimgstat $expo_cheese 100 INDEF | grep "sum"  | cut -d'=' -f2 >> ${dir0}_expoMOS1_ccd.txt
 endif
 if ($epic == "M2") then
  fimgstat $expo_cheese 100 INDEF | grep "sum"  | cut -d'=' -f2 >> ${dir0}_expoMOS2_ccd.txt
 endif
end

if (-e ${dir0}_expoPN_ccd.txt) then
 cat ${dir0}_expoPN_ccd.txt | awk '{ { M+=($1)} } END{print M/25}' >> ${dir0}_expoPN_cheese.txt 
endif
if (-e ${dir0}_expoMOS1_ccd.txt) then
 cat ${dir0}_expoMOS1_ccd.txt | awk '{ { M+=($1)} } END{print M/25}' >> ${dir0}_expoMOS1_cheese.txt 
endif
if (-e ${dir0}_expoMOS2_ccd.txt) then
 cat ${dir0}_expoMOS2_ccd.txt | awk '{ { M+=($1)} } END{print M/25}' >> ${dir0}_expoMOS2_cheese.txt 
endif

gzip *.fits
cd $start_dir/
date
echo "stop total_exposure_stat.csh"
