#!/bin/csh -f
#+
# fcsdiff -- difference fcs the reference fcs image with the current fcs image
#
# Purpose:
#	Create a differenced fcs image called fcs_diff.fits using the 
#       reference image and the current fcs image.
#	
# Usage:
#	fcsdiff <outfile>
# 
# Arguments:
#	outfile = name of the FCS image to create
# 
# Output:
#	output file is created
# 
# Example:
#	1) Difference the current FCS image and the FCS reference image 
#	to create the image fcsdiff.fits:
#	       fcsdiff fcsdiff.fits
#-
# Modification history:
#	2006-Sep-20	MK 	Original version canabalized from nextfcsimage
#	2007-Jun-15	GDW 	Adapted to use ftools.imarith
#-----------------------------------------------------------------------

# define usage...
set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd <outfile>"
set tail = 0

# check args...
if ( $#argv != 1 ) then
    printf "$usage\n\a"
    exit 1
endif

# get arg...
if ( $#argv >= 1 ) then
    set fcsdiff = $1
    shift
endif

# add .fits to name if required...
if ( $fcsdiff !~ *.fits ) then
    set fcsdiff = ${fcsdiff}.fits
endif

# verify that outfile does not exist...
if ( -f $fcsdiff ) then
    printf "[$cmd] ERROR: operation would clobber existing file $fcsdiff -- abort!\n\a"
    exit 1
endif

# get the current fcs reference image
set fcsref = `show -s deifcs -terse fcsimgfi`
if ( $status ) exit $status

# verify existence...
if ( ! -e $fcsref ) then
    printf "[$cmd] ERROR: fcs ref image $fcsref not found -- abort!\n\a" 
    exit 1
endif

# get the current fcs image...
set fcslast = `lastfcsimage`

# verify existence...
if ( ! -e $fcslast ) then
    printf "[$cmd] ERROR: last fcs image $fcslast not found -- abort!\n\a" 
    exit 1
endif

# print the what is going to happen
printf "Differencing %s and %s to create %s\n" $fcsref $fcslast $fcsdiff
imarith $fcsref $fcslast $fcsdiff SUB datatype=long
