#! /bin/bash

#Write a sequence of integers that slews sawtooth fashion between two
#limits.

INPUT='CONSEC_DIFFS.fits'
IMIN='113'
IMAX='128'
NCYCLE='1'

ARG=`echo $IMIN $IMAX $NCYCLE | awk '{
    imin   = $1;
    imax   = $2;
    ncycle = $3;
    for (i = 0; i < ncycle; i++)
    {
        for (j = imin; j <= imax; j++)
            printf(" %d", j);
        for (j = imax - 1; j >= imin + 1; j--)
           printf(" %d", j);
    }
    printf("\n");
}'`

rm -f REP0.fits
restack $INPUT $ARG REP0.fits

#Create a datacube that contains indicators for forward and backward in time:

rm -f PPLIST.txt
echo $IMIN $IMAX $NCYCLE | awk '{
    imin   = $1;
    imax   = $2;
    ncycle = $3;
    for (i = 0; i < ncycle; i++)
    {
        for (j = imin; j <= imax; j++)
            printf("PING.fits\n");
        for (j = imax - 1; j >= imin + 1; j--)
           printf("PONG.fits\n");
    }
}' > PPLIST.txt

rm -f PING.fits
ann_mask $INPUT 612 600 0.00 0.01 PING.fits
rm -f PONG.fits
ann_mask $INPUT 412 600 0.00 0.01 PONG.fits

rm -f PINGPONG.fits
stack_frames PINGPONG.fits -l PPLIST.txt

rm -f NOT_PINGPONG.fits
mon_op PINGPONG.fits not -o NOT_PINGPONG.fits

rm -f REP1.fits
bin_op REP0.fits times NOT_PINGPONG.fits -o REP1.fits

rm -f OFFSET_PINGPONG.fits
mon_op PINGPONG.fits times -400 -o OFFSET_PINGPONG.fits

rm -f REP.fits
bin_op REP1.fits plus OFFSET_PINGPONG.fits -o REP.fits

#Cleanup

rm -f REP0.fits PPLIST.txt REP1.fits
rm -f PING.fits PONG.fits PINGPONG.fits NOT_PINGPONG.fits OFFSET_PINGPONG.fits
