#! /usr/bin/ksh
# Converts a multi-image NCAR gmeta into a gif-animation
# needs ImageMagick and gifsicle
# $1 is the NCAR gmeta filename
# $2 is the first frame to be included
# $3 is the last frame to be included
# $4 is the output resolution n (one side only, units of n x 256 pixels) 
# $5 is the frame delay in hundredth's of a second
# $6 is the optimisation level (0,1 or 2)
# $7 is a unique file number for the linkage to the webpage 
typeset -Z4 numstart
typeset -Z4 num
typeset -Z4 numend
typeset -Z3 xnum

BIN_DIR="/viper/home/flexpart/public_html/fpinteractive/bin_mail"
GIF_DIR="/viper/home/flexpart/public_html/fpinteractive/plots_mail"
NCARG_ROOT="/viper/home/flexpart/ncarg"

file=$1
numstart=$2
num=$numstart
numend=$3
outres=$4
fdel=$5
optl=$6
web_num=$7
inres=1
rres=0
while [ $inres -le $outres ] ; do
rres=`expr $rres + 256`
inres=`expr $inres + 1`
done
$NCARG_ROOT/bin/med -e '1,$ split $' ${file}
xnum=999
while [ $xnum -ge 1 ] ; do
if [[ -a med${xnum}.ncgm ]]; then
mv med${xnum}.ncgm med0${xnum}.ncgm
fi
xnum=`expr $xnum - 1`
done
while [ $num -le $numend ] ; do
#echo "Processing Frame $num"
$NCARG_ROOT/bin/ctrans -d sgi -quiet -res ${rres}x${rres} med${num}.ncgm > ${file}_${num}.sgi
convert ${file}_${num}.sgi ${file}_${num}.gif
rm ${file}_${num}.sgi
num=`expr $num + 1`
done
rm med*.ncgm
$BIN_DIR/gifsicle -lforever -d ${fdel} -O${optl} ${file}_*.gif > $GIF_DIR/tracer_v_${web_num}.gif
num=$numstart
while [ $num -le $numend ] ; do
rm ${file}_${num}.gif
num=`expr $num + 1`
done


