#!/bin/sh
#
#  multiply two complex images
#  needs $a.r $a.i and $b.r, $b.i
#  creates $c.r, $c.i
#
# $Source: /u/vmcintyr/shellscripts/miriad/backward.sh,v $
# $Date: 2001/09/21 07:04:49 $
#
if [ $# -ne 4 ]; then
  echo "Usage: `basename $0` <a_real> <a_imag> <b_real> <b_imag>"
  echo "You should have four input files, in miriad format."
  exit 1
fi

ar=$1; ai=$2; br=$3; bi=$4;
for d in $ar $ai $br $bi
do
   if [ ! -d $d ]; then
     echo "Can't find image $d, exiting";
     exit 1
   fi
done

TMPDIR=/tmp/`basename $0`.$$;
LOG="${TMPDIR}/log"
ERR="${TMPDIR}/err"

mkdir $TMPDIR
touch -m $LOG
touch -m $ERR

# outputs
realpart='product.r';
imagpart='product.i';
for d in $realpart $imagpart
do
  if [ -d $d ]; then
     echo "image $d exists; removing it."
     /bin/rm -rf $d
  fi
done

#expression is
#  (x + yi)*(u + vi) = (xu - yv) +  (yu + xv)i
#http://www.clarku.edu/~djoyce/complex/
#
maths exp="(<${ar}>*<${br}>)-(<${ai}>*<${bi}>)" out="${realpart}"   #\
#                                                         1>> $LOG 2>> $ERR
maths exp="(<${ai}>*<${br}>)+(<${ar}>*<${bi}>)" out="${imagpart}" #\
                                                         #1>> $LOG 2>> $ERR

echo ""
echo "Task complete"
echo "Created: $realpart, $imagpart"

/bin/rm -rf "$TMPDIR"
exit 0
