#!/bin/sh
#
#  forward fourier transform, with MIRIAD
#  creates $image.r, $image.i, $image.a, $image.p
#
# $Source: /u/vmcintyr/shellscripts/miriad/forward.sh,v $
# $Date: 2001/09/21 07:05:29 $
#
if [ $# -eq 0 ]; then
  echo "Usage: $0 miriadfile"
  exit 1
fi

image=$1
if [ ! -d "$image" ]; then
  echo "Can't find miriad image $image"
  exit 1
fi

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

nx=`itemize in=${image}/naxis1 | awk '{print $NF}' 2>> $ERR`
ny=`itemize in=${image}/naxis2 | awk '{print $NF}' 2>> $ERR`
cx=`echo $nx |awk '{print int($1/2) + ($1%2)}'`
cy=`echo $ny |awk '{print int($1/2) + ($1%2)}'`

echo "Size is $nx x $ny. Setting centre at $cx, $cy"
puthd in="${image}/crpix1" value=$cx 1>>$LOG 2>>$ERR
puthd in="${image}/crpix2" value=$cy 1>>$LOG 2>>$ERR

nnx=`echo $nx | \
      awk '{power=log($1)/log(2);if(power > int(power)){power=int(power+1)};x=int(exp(power*log(2))+0.5);print x}'`
nny=`echo $ny | \
      awk '{power=log($1)/log(2);if(power > int(power)){power=int(power+1)};x=int(exp(power*log(2))+0.5);print x}'`
      #awk '{power=int(log($1)/log(2));x=int(exp(power*log(2))+0.1);print x}'`
ncx=`echo $nnx |awk '{print int($1/2) + ($1%2)}'`
ncy=`echo $nny |awk '{print int($1/2) + ($1%2)}'`

echo "Padding image out to $nnx x $nny, centre $ncx, $ncy"
blx=`echo 1  $nnx $ncx | awk '{print $1 - $3}'`
trx=`echo 1  $nnx $ncx | awk '{print $2 - $3}'`
bly=`echo 1  $nny $ncy | awk '{print $1 - $3}'`
try=`echo 1  $nny $ncy | awk '{print $2 - $3}'`

if [ -d "${image}.framed" ]; then /bin/rm rf "${image}.framed"; fi
imframe in="${image}" out="${image}.framed" frame="$blx,$trx,$bly,$try" \
                                                       1>> $LOG  2>>$LOG

#prthd in="${image}.framed"

DIRECTION='+1'
realpart="${image}.rf"
imagpart="${image}.if"
amplpart="${image}.af"
phsepart="${image}.pf"

for d in $realpart $imagpart $amplpart $phsepart
do
  if [ -d $d ]; then
     echo "image $d exists; removing it."
     /bin/rm -rf $d
  fi
done

echo "transforming..."
if [ -d "${image}.framed" ]; then
  fft rin="${image}.framed" rout="${realpart}" iout="${imagpart}" \
  sign="$DIRECTION"  1>> $LOG  2>>$LOG
  fft rin="${image}.framed" mag="${amplpart}" phase="${phsepart}" \
  sign="$DIRECTION"  1>> $LOG  2>>$LOG
  /bin/rm -rf "${image}.framed"
else
  fft rin="${image}" rout="${realpart}" iout="${imagpart}" sign="$DIRECTION" \
   1>> $LOG  2>>$LOG
  fft rin="${image}" mag="${amplpart}" phase="${phsepart}" sign="$DIRECTION" \
   1>> $LOG  2>>$LOG
fi

if [ ! -d "${realpart}" ]; then
  echo "Failed to create real part. Not enough disk space?"
  exit 1
fi
if [ ! -d "${imagpart}" ]; then
  echo "Failed to create imaginary part. Not enough disk space?"
  exit 1
fi
if [ ! -d "${amplpart}" ]; then
  echo "Failed to create amplitude image. Not enough disk space?"
  exit 1
fi
if [ ! -d "${phsepart}" ]; then
  echo "Failed to create phase image. Not enough disk space?"
  exit 1
fi

echo ""
echo "Task complete"
echo "Created: $realpart, $imagpart, $amplpart, $phsepart."
/bin/rm -rf "$TMPDIR"
exit 0
