#! /bin/tcsh -f

echo "***************************"
echo "*  start emosaic_bin.csh  *"
echo "***************************"
date

cd $result_dir/
set ExpDetectPrep=$an_dir/ExpDetectPrep
cd $an_dir/

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

if (-e $an_dir/"$dir0"_"$b_tot"_"$start"-"$stop".list) then
#IMAGE
    set ima = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits`
    echo "#! /bin/tcsh -f" > $an_dir/emosaic.csh
    echo "emosaic forceuniformkwds=yes imagesets='$ima' mosaicedset="$an_dir"/"$dir0"_"$b_tot"_"$start"-"$stop".img withexposure=N -w "$warn" -V "$verb"" >> $an_dir/emosaic.csh
   
    source $an_dir/emosaic.csh
    rm $an_dir/emosaic.csh
    echo "OK: mosaicked image done in energy band $b_tot"
#EXPO
    set expo = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_"$b_tot"_"$start"-"$stop".fits`
    echo "#! /bin/tcsh -f" > $an_dir/emosaic.csh
    echo "emosaic forceuniformkwds=yes imagesets='$expo' mosaicedset="$an_dir"/"$dir0"_"$b_tot"_"$start"-"$stop".expimg withexposure=N -w "$warn" -V "$verb"" >> $an_dir/emosaic.csh
   
    source $an_dir/emosaic.csh
    rm $an_dir/emosaic.csh
    echo "OK: mosaicked exposure map done in energy band $b_tot"
#BKG
    set bkg = `ls $an_dir/"$dir0"_MOS1_bkg_"$b_tot"_"$start"-"$stop".fits $an_dir/"$dir0"_MOS2_bkg_"$b_tot"_"$start"-"$stop".fits $an_dir/"$dir0"_PN_bkg_"$b_tot"_"$start"-"$stop".fits`
    echo "#! /bin/tcsh -f" > $an_dir/emosaic.csh
    echo "emosaic forceuniformkwds=yes imagesets='$bkg' mosaicedset="$an_dir"/"$dir0"_"$b_tot"_"$start"-"$stop".bkgimg withexposure=N -w "$warn" -V "$verb"" >> $an_dir/emosaic.csh
    source $an_dir/emosaic.csh
    rm $an_dir/emosaic.csh
    echo "OK: mosaicked background map done in energy band $b_tot"

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

#region file
      echo "slconv colour=green expression='' outfile="$an_dir"/"$dir0"_"$b_tot"_"$start"-"$stop".reg outfilestyle=whole outputstyle=ds9 radiusexpression=RATE radiusstyle=auto srclisttab="$an_dir"/emllist_"$b_tot"_"$start"-"$stop".fits:SRCLIST withlabels=no -w "$warn" -V "$verb"" >> $an_dir/slconv.csh
      source $an_dir/slconv.csh
      rm $an_dir/slconv.csh
endif   
cd $start_dir/

date
echo "stop emosaic_bin.csh"
