




if test $# -lt 1 -o $# -gt 2 ; then
   echo $#
   echo "usage: `basename $0`  FIR_filter_file  <target_tune>"
cat <<BLOCK 
calculate FIR filter respose 
##################################
### example of input file
#--- BEGIN --
# number_of_taps ( this line is not necessary )
# coef1  turn1 
# coef2  turn2 
# coef3  turn3 
# ... 
#--- END --
# respose for tune nu is
#    G*exp( i*zeta ) = sum_k=1^n [ coef[k] * exp( -i * turn[k] * 2*pi*nu )
#    where G and zeta are the gain and phase shift by FIR filter and
#    are functions of nu 
#
# If target tune nu0 is set, then
#    effective gain = G(nu)*cos( zeta(nu) - zeta(nu0) ) 
#    is calculated. 
##################################
BLOCK
   exit
fi

if test $# -eq 2 ; then
   NU0=$2
   A=2
else
   NU0=""
   A=1
fi

# for FILE in $* ; do
FILE=$1
  if test ! -f $FILE ; then
    echo file: $FILE not found
  else
    echo $FILE
    NAME=`echo $FILE | sed 's/.py$//g' | sed 's/.out$//g' | sed 's/.fir$//g'`
    RESP=${NAME}.resp

    if test $A != 2 ; then
       FIR_resp.py $FILE  $NU0 > $RESP 
    else
       FIR_resp.py $FILE  > $RESP 
    fi

    echo '# tune  gain' > ${NAME}.FIR_gain
    grep -v "#" $RESP | awk '{print $1,$2}' >> ${NAME}.FIR_gain

    echo '# tune  phase_in_deg' > ${NAME}.FIR_phase
    grep -v "#" $RESP | awk '{print $1,$3}' >> ${NAME}.FIR_phase

    if test "a$NU0" != "a"; then
       echo '# tune  gain_eff' > ${NAME}.FIR_gain_eff
       grep -v "#" $RESP | awk '{print $1,$4}' >> ${NAME}.FIR_gain_eff
    fi
  fi 
# done    
