#! /bin/tcsh -f

echo "========================================================================"
echo "  ATTENTION: starting detection in energy bands (eV): $band_ref         "
echo "========================================================================"
echo "**************************"
echo "*  start make_image.csh  *"
echo "**************************"
date

cd $result_dir/
set det = $result_dir/$dir0/detection
set proc = $result_dir/$dir0/processed
cd $det/

echo "OBS $dir0"

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   
   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
      set Nflare = 1
      foreach evt (`cat $det/$list_evt`)
         set mos = `echo $evt | cut -c 18-21`
         set type = `echo $evt | cut -c 23-26`
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)
             set evt_list_filt="list_evt_${dir0}_filtered_${b}.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 ($b == $b_tot ) then
              # set exprEN="(FLAG & 0x766aa000)==0"
               set exprEN="(FLAG & 0x766ba000)==0"
             else
               set exprEN="(FLAG & 0x766ba000)==0"
             endif

             echo "#! /bin/tcsh -f" > $det/make_ima_"$dir0".csh
             if ($filter0 == "SP") then
              echo "evt file SP cleaned"
              set flarefile=`awk -v nr=$Nflare '{ if (NR == nr) print $1}' $proc/list_GTI_Flare.txt`
              if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$proc"/$flarefile,TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is not filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              endif
             endif

             if ($filter0 == "notSP") then
             echo "evt file not SP cleaned"
              if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is filtered with Global HK Attitude GTI"
               echo "           It is not filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is not filtered with Global HK Attitude GTI"
               echo "           It is not filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              endif
             endif
             source $det/make_ima_"$dir0".csh
             rm $det/make_ima_"$dir0".csh

              ##STATISTICA IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile="$dir0"_image"$mos"_"$type"_"$b".fits max=0 mean=0 min=0 mode=ql num=0 outfile="$dir0"_statistics_image"$mos"_"$type"_"$b".dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $det/stat.csh
               source $det/stat.csh
               rm $det/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_image"$mos"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_image"$mos"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$dir0"_truecolor_rate_image"$mos"_"$type"_"$b".fit tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/truecolor.csh
                echo "etruecolor colorset="$dir0"_truecolor_RGB_image"$mos"_"$type"_"$b".fit outputchoice=ppmfile ppmfile="$dir0"_truecolor_RGB_bitmap_image"$mos"_"$type"_"$b".ppm tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/rgb.csh
                source $det/truecolor.csh
                rm $det/truecolor.csh
                source $det/rgb.csh
                rm $det/rgb.csh
               endif
              endif
         echo "OK: "$dir0"image"$mos"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   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 events files in $det"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det" >> $log_dir/messages.log
      break
   else
      set Nflare = 1
      foreach evt (`cat $det/$list_evt`)
         set pn = `echo $evt | cut -c 18-19`
         set type = `echo $evt | cut -c 23-26`
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)

             set evt_list_filt="list_evt_${dir0}_filtered_${b}.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 
               set exprEN_flag="((PI in $q1 ${bmin}:${bmax} $q2)&&(FLAG & 0x2fb002c)==0)"
             endif
             if ($bmin >= 1000 && $bmax > 1000 ) then
               set case = 2 
               set exprEN_flag="((PI in $q1 ${bmin}:${bmax} $q2)&&(FLAG & 0x2fb0024)==0)"
             endif
             if ($bmin < 1000 && $bmax > 1000 ) then
               set case = 3 
               set exprEN_flag1="((PI in $q1 ${bmin}:1000 $q2)&&(FLAG & 0x2fb002c)==0)"
              set exprEN_flag2="((PI in $p1 1000:${bmax} $q2)&&(FLAG & 0x2fb0024)==0)"
             endif

             set exprEN="(((PI in $q1 ${bmin}:${bmax} $q2)&&(PATTERN<=4))&&!((PI<=500)&&(PATTERN>0))&&(((PI<=1000)&&(FLAG & 0x2fb002c)==0)||((PI>1000)&&(FLAG & 0x2fb0024)==0)))"

             echo "#! /bin/tcsh -f" > $det/make_ima_"$dir0".csh
             if ($filter0 == "SP") then
              echo "evt file SP cleaned"
              set flarefile=`awk -v nr=$Nflare '{ if (NR == nr) print $1}' $proc/list_GTI_Flare.txt`
              if ( $b == $bCU ) then
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
                if ($case == 3)  then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
               endif
              else
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
                if ($case == 3)  then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is not filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
                if ($case == 3)  then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
               endif
              endif
             endif

             if ($filter0 == "notSP") then
             echo "evt file not SP cleaned"
              if ( $b == $bCU ) then
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
                if ($case == 3)  then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
                if ($case == 3)  then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
               endif
              else
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
                if ($case == 3)  then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
                if ($case == 3)  then
                 if ($type_det == "fulldet") then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 endif
                endif
               endif
              endif
             endif
             source $det/make_ima_"$dir0".csh
             rm $det/make_ima_"$dir0".csh
             if ($type_det == "fulldet") then
              source $det/make_ima_"$dir0"_inter.csh
              rm $det/make_ima_"$dir0"_inter.csh
             endif

              ##STATISTICA IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile="$dir0"_image"$pn"_"$type"_"$b".fits max=0 mean=0 min=0 mode=ql num=0 outfile="$dir0"_statistics_image"$pn"_"$type"_"$b".dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $det/stat.csh
               source $det/stat.csh
               rm $det/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_image"$pn"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_image"$pn"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$dir0"_truecolor_rate_image"$pn"_"$type"_"$b".fit tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/truecolor.csh
                echo "etruecolor colorset="$dir0"_truecolor_RGB_image"$pn"_"$type"_"$b".fit outputchoice=ppmfile ppmfile="$dir0"_truecolor_RGB_bitmap_image"$pn"_"$type"_"$b".ppm tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/rgb.csh
                source $det/truecolor.csh
                rm $det/truecolor.csh
                source $det/rgb.csh
                rm $det/rgb.csh
               endif
              endif
         echo "OK: "$dir0"image"$pn"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   endif
 endif #DET=03

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

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

   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
      set Nflare = 1
      foreach evt (`cat $det/$list_evt`)
         set mos = `echo $evt | cut -c 18-21`
         set type = `echo $evt | cut -c 23-26`
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)

             set evt_list_filt="list_evt_${dir0}_filtered_${b}.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 ($b == $b_tot ) then
              # set exprEN="(FLAG & 0x766aa000)==0"
               set exprEN="(FLAG & 0x766ba000)==0"
             else
               set exprEN="(FLAG & 0x766ba000)==0"
             endif

             echo "#! /bin/tcsh -f" > $det/make_ima_"$dir0".csh
             if ($filter0 == "SP") then
              echo "evt file SP cleaned"
              set flarefile=`awk -v nr=$Nflare '{ if (NR == nr) print $1}' $proc/list_GTI_Flare_MOS.txt`
              if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$proc"/$flarefile,TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              endif
             endif

             if ($filter0 == "notSP") then
             echo "evt file not SP cleaned"
              if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$dir0"_event_list_"$mos"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$mos"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$mos"_"$type"_"$b".fits >> $det/$evt_list_filt 
              endif
             endif
             source $det/make_ima_"$dir0".csh
             rm $det/make_ima_"$dir0".csh

              ##STATISTICA IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile="$dir0"_image"$mos"_"$type"_"$b".fits max=0 mean=0 min=0 mode=ql num=0 outfile="$dir0"_statistics_image"$mos"_"$type"_"$b".dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $det/stat.csh
               source $det/stat.csh
               rm $det/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_image"$mos"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_image"$mos"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$dir0"_truecolor_rate_image"$mos"_"$type"_"$b".fit tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/truecolor.csh
                echo "etruecolor colorset="$dir0"_truecolor_RGB_image"$mos"_"$type"_"$b".fit outputchoice=ppmfile ppmfile="$dir0"_truecolor_RGB_bitmap_image"$mos"_"$type"_"$b".ppm tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/rgb.csh
                source $det/truecolor.csh
                rm $det/truecolor.csh
                source $det/rgb.csh
                rm $det/rgb.csh
               endif
              endif
         echo "OK: "$dir0"image"$mos"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   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 events files in $det"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det" >> $log_dir/messages.log
      break
   else
      set Nflare = 1
      foreach evt (`cat $det/$list_evt`)
         set pn = `echo $evt | cut -c 18-19`
         set type = `echo $evt | cut -c 23-26`
         set b = 1
         foreach band (`cat $result_dir/$dir0/energy_band.txt`)

             set evt_list_filt="list_evt_${dir0}_filtered_${b}.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 
               set exprEN_flag="((PI in $q1 ${bmin}:${bmax} $q2)&&(FLAG & 0x2fb002c)==0)"
             endif
             if ($bmin >= 1000 && $bmax > 1000 ) then
               set case = 2 
               set exprEN_flag="((PI in $q1 ${bmin}:${bmax} $q2)&&(FLAG & 0x2fb0024)==0)"
             endif
             if ($bmin < 1000 && $bmax > 1000 ) then
               set case = 3 
               set exprEN_flag1="((PI in $q1 ${bmin}:1000 $q2)&&(FLAG & 0x2fb002c)==0)"
               set exprEN_flag2="((PI in $p1 1000:${bmax} $q2)&&(FLAG & 0x2fb0024)==0)"
             endif

             set exprEN="(((PI in $q1 ${bmin}:${bmax} $q2)&&(PATTERN<=4))&&!((PI<=500)&&(PATTERN>0))&&(((PI<=1000)&&(FLAG & 0x2fb002c)==0)||((PI>1000)&&(FLAG & 0x2fb0024)==0)))"

             echo "#! /bin/tcsh -f" > $det/make_ima_"$dir0".csh
             if ($filter0 == "SP") then
              echo "evt file SP cleaned"
              set flarefile=`awk -v nr=$Nflare '{ if (NR == nr) print $1}' $proc/list_GTI_Flare_PN.txt`
              if ( $b == $bCU ) then
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               endif
              else
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
               echo "           It is not filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               endif
              endif
             endif

             if ($filter0 == "notSP") then
             echo "evt file not SP cleaned"
              if ( $b == $bCU ) then
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           The cts in the energy range 7800-8200 eV (Cu energy range) are cleaned"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               endif
              else
               if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN"' filteredset="$dir0"_event_list_"$pn"_"$type"_"$b".fits imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b".fits keepfilteroutput=Y table="$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0".csh
                echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
                echo "           The image is performed in energy range "$bmin"-"$bmax" eV"
                echo "           It is not filtered with Global HK Attitude GTI"
                echo "           It is not filtered with GTI from EPIC flare background timeseries"
                echo "$dir0"_event_list_"$pn"_"$type"_"$b".fits >> $det/$evt_list_filt 
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
                if ($case == 3)  then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag1"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter1.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$dir0"_image"$pn"_"$type"_"$b"_inter2.fits keepfilteroutput=N table="$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/make_ima_"$dir0"_inter.csh
                endif
               endif
              endif
             endif
             source $det/make_ima_"$dir0".csh
             rm $det/make_ima_"$dir0".csh
             if ($type_det == "fulldet") then
              source $det/make_ima_"$dir0"_inter.csh
              rm $det/make_ima_"$dir0"_inter.csh
             endif
 
              ##STATISTICA IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile="$dir0"_image"$pn"_"$type"_"$b".fits max=0 mean=0 min=0 mode=ql num=0 outfile="$dir0"_statistics_image"$pn"_"$type"_"$b".dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $det/stat.csh
               source $det/stat.csh
               rm $det/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$dir0"_image"$pn"_"$type"_"$b".PNG/PNG thickness=3 set="$dir0"_image"$pn"_"$type"_"$b".fits withsrclisttab=no -w "$warn" -V "$verb"" > $det/implot.csh
               source $det/implot.csh
               rm $det/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$dir0"_truecolor_rate_image"$pn"_"$type"_"$b".fit tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/truecolor.csh
                echo "etruecolor colorset="$dir0"_truecolor_RGB_image"$pn"_"$type"_"$b".fit outputchoice=ppmfile ppmfile="$dir0"_truecolor_RGB_bitmap_image"$pn"_"$type"_"$b".ppm tablelist="$evt":EVENTS -w "$warn" -V "$verb"" > $det/rgb.csh
                source $det/truecolor.csh
                rm $det/truecolor.csh
                source $det/rgb.csh
                rm $det/rgb.csh
               endif
              endif
         echo "OK: "$dir0"image"$pn"_"$type"_"$b".fits done in energy band (eV) ["$bmin":"$bmax"]"
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   endif
 endif #DET=2
cd $result_dir/
cd $start_dir/

date
echo "stop make_image.csh"
