#! /bin/tcsh -f

echo "***********************"
echo "*  start emosaic.csh  *"
echo "***********************"
date

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

if ($emosaic == "yes") then
     if ($DET == 1 || $DET == 2) then
       set inst=0
     endif
     if ($DET == 01) then
       set inst=2
     endif
     if ($DET == 02) then
       set inst=3
     endif
     if ($DET == 03) then
       set inst=1
     endif
#IMAGE
    set ima = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits`
    echo "#! /bin/tcsh -f" > $det/emosaic.csh

    echo "emosaic forceuniformkwds=yes imagesets='$ima' mosaicedset="$det"/"$dir0"_IMAGE_"$b_tot".fits withexposure=N -w "$warn" -V "$verb"" >> $det/emosaic.csh
   
    source $det/emosaic.csh
    rm $det/emosaic.csh
    cp "$det"/"$dir0"_IMAGE_"$b_tot".fits $prod_dir
    cp "$det"/"$dir0"_IMAGE_"$b_tot".fits $prod_dir_final/P${dir0}EPX000OIMAGE8000.fits
    gzip $prod_dir_final/P${dir0}EPX000OIMAGE8000.fits
    cp $prod_dir_final/P${dir0}EPX000OIMAGE8000.fits.gz $prod_dir_final/P${dir0}EPX000OIMAGE8000.FTZ
    rm $prod_dir_final/P${dir0}EPX000OIMAGE8000.fits.gz
    echo "OK: mosaicked image done in energy band $b_tot"
#EXPO
     set expo = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_"$b_tot".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_"$b_tot".fits $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_"$b_tot".fits`
     echo "#! /bin/tcsh -f" > $det/emosaic.csh

     echo "emosaic forceuniformkwds=yes imagesets='$expo' mosaicedset="$det"/"$dir0"_EXPO_"$b_tot".fits withexposure=N -w "$warn" -V "$verb"" >> $det/emosaic.csh
   
     source $det/emosaic.csh
     rm $det/emosaic.csh
     cp "$det"/"$dir0"_EXPO_"$b_tot".fits $prod_dir
     cp "$det"/"$dir0"_EXPO_"$b_tot".fits $prod_dir_final/P${dir0}EPX000OEXPMP8000.fits
     gzip $prod_dir_final/P${dir0}EPX000OEXPMP8000.fits
     cp $prod_dir_final/P${dir0}EPX000OEXPMP8000.fits.gz $prod_dir_final/P${dir0}EPX000OEXPMP8000.FTZ
     rm $prod_dir_final/P${dir0}EPX000OEXPMP8000.fits.gz
     echo "OK: mosaicked exposure map done in energy band $b_tot"
#BKG
    if ($DET == 1 || $DET == 01 || $DET == 02) then
     set bkg1 = `ls $det/"$dir0"_MOS1_bkg_*.fits | perl -ne 'print unless $.<2 '`
     set bkg2 = `ls $det/"$dir0"_MOS2_bkg_*.fits | perl -ne 'print unless $.<2 '`
     echo "#! /bin/tcsh -f" > $det/emosaic.csh
     echo "emosaic forceuniformkwds=yes imagesets='$bkg1 $bkg2' mosaicedset="$det"/"$dir0"_BKG_"$b_tot".fits withexposure=N -w "$warn" -V "$verb"" >> $det/emosaic.csh
    endif
    if ($DET == 03) then
     set bkg3 = `ls $det/"$dir0"_PN_bkg_*.fits | perl -ne 'print unless $.<2 '`
     echo "#! /bin/tcsh -f" > $det/emosaic.csh
     echo "emosaic forceuniformkwds=yes imagesets='$bkg3' mosaicedset="$det"/"$dir0"_BKG_"$b_tot".fits withexposure=N -w "$warn" -V "$verb"" >> $det/emosaic.csh
    endif
    if ($DET == 2) then
     set bkg1 = `ls $det/"$dir0"_MOS1_bkg_*.fits | perl -ne 'print unless $.<2 '`
     set bkg2 = `ls $det/"$dir0"_MOS2_bkg_*.fits | perl -ne 'print unless $.<2 '`
     set bkg3 = `ls $det/"$dir0"_PN_bkg_*.fits | perl -ne 'print unless $.<2 '`
     echo "#! /bin/tcsh -f" > $det/emosaic.csh
     echo "emosaic forceuniformkwds=yes imagesets='$bkg1 $bkg2 $bkg3' mosaicedset="$det"/"$dir0"_BKG_"$b_tot".fits withexposure=N -w "$warn" -V "$verb"" >> $det/emosaic.csh
    endif
     source $det/emosaic.csh
     rm $det/emosaic.csh
     cp "$det"/"$dir0"_BKG_"$b_tot".fits $prod_dir
     cp "$det"/"$dir0"_BKG_"$b_tot".fits $prod_dir_final/P${dir0}EPX000OBKGMP8000.fits
     gzip $prod_dir_final/P${dir0}EPX000OBKGMP8000.fits
     cp $prod_dir_final/P${dir0}EPX000OBKGMP8000.fits.gz $prod_dir_final/P${dir0}EPX000OBKGMP8000.FTZ
     rm $prod_dir_final/P${dir0}EPX000OBKGMP8000.fits.gz
     echo "OK: mosaicked background map done in energy band $b_tot"

    if ($plot0 == 1) then
     echo "#! /bin/tcsh -f" > $det/implot.csh
     echo "implot device="$dir0"_IMAGE_"$b_tot".PNG/PNG thickness=3 expression='ID_BAND==0 && ID_INST=="$inst"' set="$det"/"$dir0"_IMAGE_"$b_tot".fits srclisttab="$det"/emllist_singsrc_"$b_tot".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      rm $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$b_tot".PNG $prod_dir
     endif
endif

#region file
      echo "slconv colour=green expression='' outfile="$det"/"$dir0"_listsrc_"$b_tot".reg outfilestyle=whole outputstyle=ds9 radiusexpression=RATE radiusstyle=auto srclisttab="$det"/emllist_singsrc_"$b_tot".fits:SRCLIST withlabels=no -w "$warn" -V "$verb"" >> $det/slconv.csh
      source $det/slconv.csh
      rm $det/slconv.csh
   
if ($full_list == "yes") then
if ($DET == 1) then
cd $ExpDetectPrep
  foreach stru (`ls ExpDetect_MOS1_LE_noise_unfiltered_image_*.fits`)
   set epi = "M1"
   set e = `echo $stru | cut -c 42-42` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
  foreach stru (`ls ExpDetect_MOS2_LE_noise_unfiltered_image_*.fits`)
   set epi = "M2"
   set e = `echo $stru | cut -c 42-42` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
endif

if ($DET == 01) then
cd $ExpDetectPrep
  foreach stru (`ls ExpDetect_MOS1_LE_noise_unfiltered_image_*.fits`)
   set epi = "M1"
   set e = `echo $stru | cut -c 42-42` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
endif

if ($DET == 02) then
cd $ExpDetectPrep
  foreach stru (`ls ExpDetect_MOS2_LE_noise_unfiltered_image_*.fits`)
   set epi = "M2"
   set e = `echo $stru | cut -c 42-42` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
endif


if ($DET == 03) then
cd $ExpDetectPrep
  foreach stru (`ls ExpDetectPrep_PN_merged_image_*.fits`)
   set epi = "PN"
   set e = `echo $stru | cut -c 31-31` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
endif

if ($DET == 2) then
cd $ExpDetectPrep
  foreach stru (`ls ExpDetect_MOS1_LE_noise_unfiltered_image_*.fits`)
   set epi = "M1"
   set e = `echo $stru | cut -c 42-42` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
  foreach stru (`ls ExpDetect_MOS2_LE_noise_unfiltered_image_*.fits`)
   set epi = "M2"
   set e = `echo $stru | cut -c 42-42` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
  foreach stru (`ls ExpDetectPrep_PN_merged_image_*.fits`)
   set epi = "PN"
   set e = `echo $stru | cut -c 31-31` 
   echo "#! /bin/tcsh -f" > $det/implot.csh
   echo "implot device="$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG/PNG expression='' thickness=3 set="$ExpDetectPrep"/"$stru" srclisttab="$det"/emllist_singsrc_"$epi"_"$e".fits:SRCLIST tempset="$det"/ImageMergeTemporary_source_list.fits withsrclisttab=yes -w "$warn" -V "$verb"" >> $det/implot.csh
      source $det/implot.csh
      cp "$det"/"$dir0"_IMAGE_"$epi"_"$e".PNG $prod_dir
      rm $det/implot.csh
  end
endif
endif
cd $start_dir/

date
echo "stop emosaic.csh"
