#! /bin/tcsh -f

echo "******************************"
echo "*  start make_image_bin.csh  *"
echo "******************************"
date

cd $result_dir/
set det = $result_dir/$dir0/detection
set proc = $result_dir/$dir0/processed
cd $det/
set Hk = `ls $det/*AttHk.ds*`

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

if ( $DET == 1 || $DET == 01 || $DET == 02) then   
   echo "OBS $dir0"
   set ctrl_empty_bin = 1
   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}_${start}-${stop}.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="(TIME>${start})&&(TIME<${stop})&&(FLAG & 0x766aa000)==0"
               set exprEN="(TIME>${start})&&(TIME<${stop})&&(FLAG & 0x766ba000)==0"
             else
               set exprEN="(TIME>${start})&&(TIME<${stop})&&(FLAG & 0x766ba000)==0"
             endif

             echo "#! /bin/tcsh -f" > $an_dir/make_ima_"$dir0".csh
             set img="$dir0"_image"$mos"_"$type"_"$b"_"$start"-"$stop".fits
             set im="$dir0"_image"$mos"_"$type"_"$b"_"$start"-"$stop"
             set ev_list="$dir0"_event_list_"$mos"_"$type"_"$b"_"$start"-"$stop".fits
             if ($filter1 == "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="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$proc"/$flarefile,TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              endif
             endif

             if ($filter1 == "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="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              endif
             endif
             source $an_dir/make_ima_"$dir0".csh
             rm $an_dir/make_ima_"$dir0".csh
             fkeypar "$an_dir"/${ev_list}+1 NAXIS2
             set trows = `pget fkeypar.par value`
             echo ""$an_dir"/${ev_list}: $trows evts"
             if ( $trows != 0) then
              set ctrl_empty_bin = 0
              echo ""${ev_list}"" >> $an_dir/$evt_list_filt 
              echo "OK: ${img} done in energy band (eV) ["$bmin":"$bmax"]"
              ##STATISTIC OF THE IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile=$an_dir/${img} max=0 mean=0 min=0 mode=ql num=0 outfile=$an_dir/"$dir0"_statistics_image"$mos"_"$type"_"$b"_"$start"-"$stop".dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $an_dir/stat.csh
               source $an_dir/stat.csh
               rm $an_dir/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device=$an_dir/${im}.PNG/PNG thickness=3 set="$an_dir"/${img} withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$an_dir"/"$dir0"_truecolor_rate_image"$mos"_"$type"_"$b"_"$start"-"$stop".fit tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $an_dir/truecolor.csh
                echo "etruecolor colorset="$an_dir/""$dir0"_truecolor_RGB_image"$mos"_"$type"_"$b"_"$start"-"$stop".fit outputchoice=ppmfile ppmfile="$an_dir"/"$dir0"_truecolor_RGB_bitmap_image"$mos"_"$type"_"$b"_"$start"-"$stop".ppm tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $an_dir/rgb.csh
                source $an_dir/truecolor.csh
                rm $an_dir/truecolor.csh
                source $an_dir/rgb.csh
                rm $an_dir/rgb.csh
               endif
              endif
             else
               echo "the event file"$an_dir"/${ev_list} is empty"
               echo "WARNING the event file"$an_dir"/${ev_list} is empty" >> $log_dir/messages.log
               rm "$an_dir"/${ev_list}
               rm "$an_dir"/${img}
             endif
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   endif
endif #DET=1

if ( $DET == 03) then   
   echo "OBS $dir0"
   set ctrl_empty_bin = 1
   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}_${start}-${stop}.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="(TIME>${start})&&(TIME<${stop})&&(PI in $q1 ${bmin}:${bmax} $q2)&&(FLAG & 0x2fb002c)==0"
             endif
             if ($bmin >= 1000 && $bmax > 1000 ) then
               set case = 2 
               set exprEN_flag="(TIME>${start})&&(TIME<${stop})&&(PI in $q1 ${bmin}:${bmax} $q2)&&(FLAG & 0x2fb0024)==0"
             endif
             if ($bmin < 1000 && $bmax > 1000 ) then
               set case = 3 
               set exprEN_flag1="(TIME>${start})&&(TIME<${stop})&&(PI in $q1 ${bmin}:1000 $q2)&&(FLAG & 0x2fb002c)==0"
               set exprEN_flag2="(TIME>${start})&&(TIME<${stop})&&(PI in $p1 1000:${bmax} $q2)&&(FLAG & 0x2fb0024)==0"
             endif

             set exprEN="(((TIME>${start})&&(TIME<${stop})&&(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
             set img="$dir0"_image"$pn"_"$type"_"$b"_"$start"-"$stop".fits
             set im="$dir0"_image"$pn"_"$type"_"$b"_"$start"-"$stop"
             set ev_list="$dir0"_event_list_"$pn"_"$type"_"$b"_"$start"-"$stop".fits
             if ($filter1 == "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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN"' filteredset=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               endif
              endif
             endif

             if ($filter1 == "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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN"' filteredset=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN"' filteredset=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               endif
              endif
             endif
             source $an_dir/make_ima_"$dir0".csh
             rm $an_dir/make_ima_"$dir0".csh
             fkeypar "$an_dir"/${ev_list}+1 NAXIS2
             set trows = `pget fkeypar.par value`
             echo ""$an_dir"/${ev_list}: $trows evts"
             if ( $trows != 0) then
              set ctrl_empty_bin = 0
              echo ""${ev_list}"" >> $an_dir/$evt_list_filt 
              echo "OK: ${img} done in energy band (eV) ["$bmin":"$bmax"]"
              source $an_dir/make_ima_"$dir0"_inter.csh
              rm $an_dir/make_ima_"$dir0"_inter.csh

              ##STATISTIC OF THE IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile="$an_dir"/"${img}" max=0 mean=0 min=0 mode=ql num=0 outfile="$an_dir"/"$dir0"_statistics_image"$pn"_"$type"_"$b"_${start}-${stop}.dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $an_dir/stat.csh
               source $an_dir/stat.csh
               rm $an_dir/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$an_dir"/"${im}".PNG/PNG thickness=3 set="$an_dir"/"${img}" withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$an_dir"/"$dir0"_truecolor_rate_image"$pn"_"$type"_"$b"_${start}-${stop}.fit tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $det/truecolor.csh
                echo "etruecolor colorset="$an_dir"/"$dir0"_truecolor_RGB_image"$pn"_"$type"_"$b"_${start}-${stop}.fit outputchoice=ppmfile ppmfile="$an_dir"/"$dir0"_truecolor_RGB_bitmap_image"$pn"_"$type"_"$b"_${start}-${stop}.ppm tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $an_dir/rgb.csh
                source $an_dir/truecolor.csh
                rm $an_dir/truecolor.csh
                source $an_dir/rgb.csh
                rm $an_dir/rgb.csh
               endif
              endif
              else
               echo "the event file"$an_dir"/${ev_list} is empty"
               echo "WARNING the event file"$an_dir"/${ev_list} is empty" >> $log_dir/messages.log
               rm "$an_dir"/${ev_list}
               rm "$an_dir"/${img}
              endif
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   endif

endif #DET=03

if ( $DET == 2 ) then
   echo "OBS $dir0"
   set ctrl_empty_bin = 1
   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}_${start}-${stop}.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="(TIME>${start})&&(TIME<${stop})&&(FLAG & 0x766aa000)==0"
               set exprEN="(TIME>${start})&&(TIME<${stop})&&(FLAG & 0x766ba000)==0"
             else
               set exprEN="(TIME>${start})&&(TIME<${stop})&&(FLAG & 0x766ba000)==0"
             endif

             echo "#! /bin/tcsh -f" > $an_dir/make_ima_"$dir0".csh
             set img="$dir0"_image"$mos"_"$type"_"$b"_"$start"-"$stop".fits
             set im="$dir0"_image"$mos"_"$type"_"$b"_"$start"-"$stop"
             set ev_list="$dir0"_event_list_"$mos"_"$type"_"$b"_"$start"-"$stop".fits
             if ($filter1 == "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="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && GTI("$proc"/$flarefile,TIME) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              endif
             endif

             if ($filter1 == "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="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              else
               echo "/usr/bin/time -p evselect expression='(PATTERN<=12) && (PI in ["$bmin":"$bmax"]) && "$exprEN"' filteredset="$an_dir/${ev_list}" imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
              endif
             endif
             source $an_dir/make_ima_"$dir0".csh
             rm $an_dir/make_ima_"$dir0".csh
             fkeypar "$an_dir"/${ev_list}+1 NAXIS2
             set trows = `pget fkeypar.par value`
             echo ""$an_dir"/${ev_list}: $trows evts"
             if ( $trows != 0) then
              set ctrl_empty_bin = 0
              echo ""${ev_list}"" >> $an_dir/$evt_list_filt 
              echo "OK: ${img} done in energy band (eV) ["$bmin":"$bmax"]"

              ##STATISTIC OF THE IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile=$an_dir/${img} max=0 mean=0 min=0 mode=ql num=0 outfile=$an_dir/"$dir0"_statistics_image"$mos"_"$type"_"$b"_"$start"-"$stop".dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $an_dir/stat.csh
               source $an_dir/stat.csh
               rm $an_dir/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device=$an_dir/${im}.PNG/PNG thickness=3 set="$an_dir"/${img} withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$an_dir"/"$dir0"_truecolor_rate_image"$mos"_"$type"_"$b"_"$start"-"$stop".fit tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $an_dir/truecolor.csh
                echo "etruecolor colorset="$an_dir/""$dir0"_truecolor_RGB_image"$mos"_"$type"_"$b"_"$start"-"$stop".fit outputchoice=ppmfile ppmfile="$an_dir"/"$dir0"_truecolor_RGB_bitmap_image"$mos"_"$type"_"$b"_"$start"-"$stop".ppm tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $an_dir/rgb.csh
                source $an_dir/truecolor.csh
                rm $an_dir/truecolor.csh
                source $an_dir/rgb.csh
                rm $an_dir/rgb.csh
               endif
              endif
             else
               echo "the event file"$an_dir"/${ev_list} is empty"
               echo "WARNING the event file"$an_dir"/${ev_list} is empty" >> $log_dir/messages.log
               rm "$an_dir"/${ev_list}
               rm "$an_dir"/${img}
             endif
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   endif

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

             set exprEN="(((TIME>${start})&&(TIME<${stop})&&(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
             set img="$dir0"_image"$pn"_"$type"_"$b"_"$start"-"$stop".fits
             set im="$dir0"_image"$pn"_"$type"_"$b"_"$start"-"$stop"
             set ev_list="$dir0"_event_list_"$pn"_"$type"_"$b"_"$start"-"$stop".fits
             if ($filter1 == "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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && GTI("$proc"/$flarefile,TIME) && "$exprEN"' filteredset=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               endif
              endif
             endif

             if ($filter1 == "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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) &&!(PI in [7800:8200]) && "$exprEN"' filteredset=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                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="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               else
                echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN"' filteredset=$an_dir/${ev_list} imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${img}" keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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"
                if ($case == 1 || $case == 2) then
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag"' imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${im}"_inter.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/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="$an_dir"/"${im}"_inter1.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                 echo "/usr/bin/time -p evselect expression='(RAWY>12) && "$exprEN_flag2"' imagebinning=binSize imagedatatype=Int32 imageset="$an_dir"/"${im}"_inter2.fits keepfilteroutput=N table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=N withimagedatatype=true withimageset=Y writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $an_dir/make_ima_"$dir0"_inter.csh
                endif
               endif
              endif
             endif
             source $an_dir/make_ima_"$dir0".csh
             rm $an_dir/make_ima_"$dir0".csh
             fkeypar "$an_dir"/${ev_list}+1 NAXIS2
             set trows = `pget fkeypar.par value`
             echo ""$an_dir"/${ev_list}: $trows evts"
             if ( $trows != 0) then
              set ctrl_empty_bin = 0
              echo ""${ev_list}"" >> $an_dir/$evt_list_filt 
              echo "OK: ${img} done in energy band (eV) ["$bmin":"$bmax"]"
              source $an_dir/make_ima_"$dir0"_inter.csh
              rm $an_dir/make_ima_"$dir0"_inter.csh

              ##STATISTIC OF THE IMAGE
              if ($stat_img == 1) then
               echo "fimgstat clobber=Y infile="$an_dir"/"${img}" max=0 mean=0 min=0 mode=ql num=0 outfile="$an_dir"/"$dir0"_statistics_image"$pn"_"$type"_"$b"_${start}-${stop}.dat rms=0 sum=0 threshlo=INDEF threshup=INDEF xmax=0 xmin=0 ymax=0 ymin=0" > $an_dir/stat.csh
               source $an_dir/stat.csh
               rm $an_dir/stat.csh
              endif

              ##PLOT
              if ($plot1 == 1) then
               echo "implot device="$an_dir"/"${im}".PNG/PNG thickness=3 set="$an_dir"/"${img}" withsrclisttab=no -w "$warn" -V "$verb"" > $an_dir/implot.csh
               source $an_dir/implot.csh
               rm $an_dir/implot.csh
              endif
              
              if ($plot2 == 1) then
               if ( $b == $b_tot ) then
                echo "etruecolor fileset="$an_dir"/"$dir0"_truecolor_rate_image"$pn"_"$type"_"$b"_${start}-${stop}.fit tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $det/truecolor.csh
                echo "etruecolor colorset="$an_dir"/"$dir0"_truecolor_RGB_image"$pn"_"$type"_"$b"_${start}-${stop}.fit outputchoice=ppmfile ppmfile="$an_dir"/"$dir0"_truecolor_RGB_bitmap_image"$pn"_"$type"_"$b"_${start}-${stop}.ppm tablelist="$det"/"$evt":EVENTS -w "$warn" -V "$verb"" > $an_dir/rgb.csh
                source $an_dir/truecolor.csh
                rm $an_dir/truecolor.csh
                source $an_dir/rgb.csh
                rm $an_dir/rgb.csh
               endif
              endif
              else
               echo "the event file"$an_dir"/${ev_list} is empty"
               echo "WARNING the event file"$an_dir"/${ev_list} is empty" >> $log_dir/messages.log
               rm "$an_dir"/${ev_list}
               rm "$an_dir"/${img}
              endif
         set b = ` expr $b + 1`
         end
      set Nflare = ` expr $Nflare + 1`
      end
   endif

endif #DET=2
rm $an_dir/*.csh
cd $result_dir/
cd $start_dir/

date
echo "stop make_image_bin.csh"
