#! /bin/tcsh -f

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

set det = $result_dir/$dir0/detection

set exposure=`ls -1 exposure*.list`
ls -1 exposure*.list > $det/n_exposure_file.list
set n_exposure_file=`wc -l $det/n_exposure_file.list | awk '{print $1}'`
if ( $n_exposure_file == 0) then
    echo "WARNING: there are not EXPOSURE parameter in the header"
else
  if ($n_exposure_file == 1) then
     set exp1 = `cat n_exposure_file.list | awk 'NR == 1 {print $1}'` 
     echo "there is "$n_exposure_file" EXPOSURE file: "$exp1""
     set n_exptim=`wc -l $exp1 | awk '{print $1}'`
     if ( $n_exptim != 0 ) then
        set val_exp=`cat $exp1` 
        set attrname_on=`echo EXPOSURE`
        set attrtype_on=`echo real`
        set realval_exp="$val_exp"
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_expo_"$b".fits:PRIMARY -w "$warn" -V "$verb"" > $det/addatt_test.csh
        source $det/addatt_test.csh
        source $det/addatt_test.csh
      endif
     endif
     echo "OK: header is ready"
  endif
  if ( $n_exposure_file == 2) then
     set exp1 = `cat n_exposure_file.list | awk 'NR == 1 {print $1}'` 
     set exp2 = `cat n_exposure_file.list | awk 'NR == 2 {print $1}'` 
     echo "there are "$n_exposure_file" EXPOSURE file: "$exp1" "$exp2""
     set sum_exp=`paste $exp1 $exp2 | awk '{printf("%.10f \n",$1+$2)}'`
     set n_exptim=`wc -l $exp1 | awk '{print $1}'`
     if ( $n_exptim != 0 ) then
        set val_exp="$sum_exp" 
        set attrname_on=`echo EXPOSURE`
        set attrtype_on=`echo real`
        set realval_exp="$val_exp"
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_expo_"$b".fits:PRIMARY -w "$warn" -V "$verb"" > $det/addatt_test.csh
        source $det/addatt_test.csh
        source $det/addatt_test.csh
     endif
     echo "OK: header is ready"
  else
     if ( $n_exposure_file > 2) then
       set exp1 = `cat n_exposure_file.list | awk 'NR == 1 {print $1}'` 
       set exp2 = `cat n_exposure_file.list | awk 'NR == 2 {print $1}'` 
       echo "there are "$n_exposure_file" EXPOSURE file: "$exposure""
       paste $exp1 $exp2 | awk '{printf("%.10f \n",$1+$2)}' > $det/calc1_expo.txt

       set k = ` expr $n_exposure_file - 2`
       set m = 1
       while($m <= $k)
        if ($m < $k) then
           set a1 = ` expr $m + 2`
           set a2 = ` expr $m + 1`
           set exp = `cat $det/n_exposure_file.list | awk -v nr=$a1 '{ if (NR == nr) print $1}'` 
           paste $det/calc"$m"_expo.txt $exp | awk '{printf("%.10f \n",$1+$2)}' > $det/calc"$a2"_expo.txt
        endif
        if ( $m == $k) then
           set a1 = ` expr $m + 2`
           set a2 = ` expr $m + 1`
           set exp = `cat $det/n_exposure_file.list | awk -v nr=$a1 '{ if (NR == nr) print $1}'` 
           set sum_exp=`paste $det/calc"$m"_expo.txt $exp | awk '{printf("%.10f \n",$1+$2)}'`
        endif
       set m = ` expr $m + 1`
       end
       set n_exptim=`wc -l $exp1 | awk '{print $1}'`
       if ( $n_exptim != 0 ) then
        set val_exp="$sum_exp" 
        set attrname_on=`echo EXPOSURE`
        set attrtype_on=`echo real`
        set realval_exp="$val_exp"
        echo "EXPOSURE: "$val_exp""
        echo "addattribute attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_exp"' set="$ExpDetectPrep"/ExpDetectPrep_"$epic"_merged_expo_"$b".fits:PRIMARY -w "$warn" -V "$verb"" > $det/addatt_test.csh
        source $det/addatt_test.csh
        source $det/addatt_test.csh
       endif
     endif
  endif
endif
rm $det/*partial*
rm $det/calc*.txt

date
echo "stop header_expo.csh"
