#! /bin/tcsh -f

set arg_input="$1"
echo "********************************"
echo "*  start header_image_bin.csh  *"
echo "********************************"
date

set ontim=`ls -1 ontime*.list`
ls -1 ontime*.list > $an_dir/n_ontim_file.list
set n_ontim_file=`wc -l $an_dir/n_ontim_file.list | awk '{print $1}'`
if ( $n_ontim_file == 0) then
    echo "WARNING: there are not ONTIME parameters in the header"
else
  if ($n_ontim_file == 1) then
     set on1 = `cat n_ontim_file.list | awk 'NR == 1 {print $1}'` 
     echo "there is "$n_ontim_file" ONTIME file: "$on1""
     set expo_max=`cat $on1 | sort -nr | awk 'NR == 1 {print $1}'`
     set sum_exp="$expo_max"
     echo $sum_exp > $an_dir/exposure.list
     set n_ontim=`wc -l $on1 | awk '{print $1}'`
     if ( $n_ontim != 0 ) then
      if ( $n_ontim == 5 ) then  
        set val_on=`cat $on1` 
        set val_exp="$sum_exp" 
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME04 ONTIME05 ONTIME07 EXPOSURE`
        set attrtype_on=`echo real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
      endif
      if ( $n_ontim == 6 ) then  
        set val_on=`cat $on1` 
        set val_exp="$sum_exp" 
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 ONTIME07 EXPOSURE`
        set attrtype_on=`echo real real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
        source $an_dir/addatt.csh
        source $an_dir/addatt.csh
      endif
      if ( $n_ontim == 7 ) then  
        set val_on=`cat $on1` 
        set val_exp="$sum_exp" 
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 INTIME06 ONTIME07 EXPOSURE`
        set attrtype_on=`echo real real real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
      endif
      if ( $n_ontim == 12 ) then  
        set val_on=`cat $on1` 
        set val_exp="$sum_exp" 
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 ONTIME06 ONTIME07 ONTIME08 ONTIME09 ONTIME10 ONTIME11 ONTIME12 EXPOSURE`
        set attrtype_on=`echo real real real real real real real real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
        source $an_dir/addatt.csh
        source $an_dir/addatt.csh
      endif
     endif
     echo "OK: header is ready"
  endif
  if ( $n_ontim_file == 2) then
     set on1 = `cat n_ontim_file.list | awk 'NR == 1 {print $1}'`
     set on2 = `cat n_ontim_file.list | awk 'NR == 2 {print $1}'`
     echo "there are "$n_ontim_file" ONTIME file: "$on1" "$on2""
     set sum_on=`paste $on1 $on2 | awk '{printf("%.10f \n",$1+$2)}'`
     paste $on1 $on2 | awk '{printf("%.10f \n",$1+$2)}' > $an_dir/sum_on.list
     set expo_max=`cat $an_dir/sum_on.list | sort -nr | awk 'NR == 1 {print $1}'`
     set sum_exp="$expo_max"
     echo $sum_exp > $an_dir/exposure.list
     set n_ontim=`wc -l $on1 | awk '{print $1}'`
     if ( $n_ontim != 0 ) then
      if ( $n_ontim == 5 ) then  
        set val_on="$sum_on"
        set val_exp="$sum_exp" 
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME04 ONTIME05 ONTIME07 EXPOSURE`
        set attrtype_on=`echo real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
      endif
      if ( $n_ontim == 6 ) then  
        set val_on="$sum_on"
        set val_exp="$sum_exp" 
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 ONTIME07 EXPOSURE`
        set attrtype_on=`echo real real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
      endif
      if ( $n_ontim == 7 ) then  
        set val_on="$sum_on" 
        set val_exp="$sum_exp" 
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 INTIME06 ONTIME07 EXPOSURE`
        set attrtype_on=`echo real real real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
      endif
      if ( $n_ontim == 12 ) then  
        set val_on="$sum_on" 
        set val_exp="$sum_exp"
        set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
        set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 ONTIME06 ONTIME07 ONTIME08 ONTIME09 ONTIME10 ONTIME11 ONTIME12 EXPOSURE`
        set attrtype_on=`echo real real real real real real real real real real real real real`
        set realval_on="$val_on"
        set realval_exp="$val_exp"
        echo "ONTIME: "$val_on""
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
        source $an_dir/addatt_test.csh
      endif
     endif
     echo "OK: header is ready"
  else
   if ( $n_ontim_file > 2) then
       set on1 = `cat n_ontim_file.list | awk 'NR == 1 {print $1}'`
       set on2 = `cat n_ontim_file.list | awk 'NR == 2 {print $1}'`
       echo "there are "$n_ontim_file" ONTIME file: "$ontim""
       paste $on1 $on2 | awk '{printf("%.10f \n",$1+$2)}' > $det/calc1.txt

       set k = ` expr $n_ontim_file - 2`
       set m = 1
       while($m <= $k)
        if ($m < $k) then
           set a1 = ` expr $m + 2`
           set a2 = ` expr $m + 1`
           set on = `cat $an_dir/n_ontim_file.list | awk -v nr=$a1 '{ if (NR == nr) print $1}'`
           paste $det/calc"$m".txt $on | awk '{printf("%.10f \n",$1+$2)}' > $det/calc"$a2".txt
        endif
        if ( $m == $k) then
           set a1 = ` expr $m + 2`
           set a2 = ` expr $m + 1`
           set on = `cat $an_dir/n_ontim_file.list | awk -v nr=$a1 '{ if (NR == nr) print $1}'`
           set sum_on=`paste $det/calc"$m".txt $on | awk '{printf("%.10f \n",$1+$2)}'`
           paste $det/calc"$m".txt $on | awk '{printf("%.10f \n",$1+$2)}' > $det/sum_on.list
           set expo_max=`cat $an_dir/sum_on.list | sort -nr | awk 'NR == 1 {print $1}'`
           set sum_exp="$expo_max"
           echo $sum_exp > $an_dir/exposure.list
        endif
       set m = ` expr $m + 1`
       end
       set n_ontim=`wc -l $on1 | awk '{print $1}'`
       if ( $n_ontim != 0 ) then
        if ( $n_ontim == 5 ) then  
          set val_on="$sum_on"
          set val_exp="$sum_exp" 
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
          set attrname_on=`echo ONTIME01 ONTIME02 ONTIME04 ONTIME05 ONTIME07 EXPOSURE`
          set attrtype_on=`echo real real real real real real`
          set realval_on="$val_on"
          set realval_exp="$val_exp"
          echo "ONTIME: "$val_on""
          echo "EXPOSURE: "$val_exp""
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
        endif
        if ( $n_ontim == 6 ) then  
          set val_on="$sum_on"
          set val_exp="$sum_exp" 
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
          set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 ONTIME07 EXPOSURE`
          set attrtype_on=`echo real real real real real real real`
          set realval_on="$val_on"
          set realval_exp="$val_exp"
          echo "ONTIME: "$val_on""
          echo "EXPOSURE: "$val_exp""
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
        endif
        if ( $n_ontim == 7 ) then  
          set val_on="$sum_on" 
          set val_exp="$sum_exp" 
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
          set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 INTIME06 ONTIME07 EXPOSURE`
          set attrtype_on=`echo real real real real real real real real`
          set realval_on="$val_on"
          set realval_exp="$val_exp"
          echo "ONTIME: "$val_on""
          echo "EXPOSURE: "$val_exp""
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
        endif
        if ( $n_ontim == 12 ) then  
          set val_on="$sum_on" 
          set val_exp="$sum_exp"
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"Sum of GTIs for a particular CCD"$vir" "$vir"max of ONTIMEnn values"$vir"`
          set attrname_on=`echo ONTIME01 ONTIME02 ONTIME03 ONTIME04 ONTIME05 ONTIME06 ONTIME07 ONTIME08 ONTIME09 ONTIME10 ONTIME11 ONTIME12 EXPOSURE`
          set attrtype_on=`echo real real real real real real real real real real real real real`
          set realval_on="$val_on"
          set realval_exp="$val_exp"
          echo "ONTIME: "$val_on""
          echo "EXPOSURE: "$val_exp""
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on $realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_image_"$b"_"$start"-"$stop".fits:PRIMARY -w "$warn" -V "$verb"" > $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
          source $an_dir/addatt_test.csh
        endif
       endif
   endif
  endif
endif
rm $an_dir/*partial*
rm $an_dir/calc*.txt

date
echo "stop header_image_bin.csh"
