#/bin/csh
#
#  This is an example of a C-shell script that calls several
#  XVista routines to reduce some drift-scan images from a TASS
#  camera.  It may give you some idea of how XVista is meant
#  to be used.
#

#
# reduce a whole set of images taken with TASS camera
#
#   1. create dark vector(s)
#   2. subtract dark vector from all appropriate frames
#   3. for each target frame
#        a. create flatfield vector
#        b. divide by flatfield vector
#   4. find stars in each target frame, and measure aperture magnitudes
#
# place comments into the FITS headers to document the history
# of each image.
#

# this controls the size of the array used to make median flatfield vector
set ARRAYSIZE=2500

# this is the prefix of all "target" FITS images
set prefix=test1
set cameralist="1 2 3"

#
# 1. create dark vector
#
# given a dark scan, calculate the median of each column value.
#   Then calculate the mean across all columns, and subtract
#   that from each column.
#
# We end up with a vector showing the difference in dark current
#   from column to column, centered on zero.
#
# Place the output dark vector into a file with name
#
#             base_darkvec.fts
#

set fname=dark.fts
set base=${fname:r}
set outfile=${base}_darkvec.fts

makevec $fname outfile=$outfile col
mn $outfile
set mean=`let mean`
sub $outfile const=mean



#
# 2. subtract dark vectors from target frames
#
foreach camera ($cameralist)
  set base=${prefix}${camera}
  set darkvec=dark${camera}_darkvec.fts

  set list=`echo $base*.fts`
  if ($status == 0) then
    foreach image ($list)
      echo "subtracting dark vector from image $image ..."
      subvec $image $darkvec col
      comment $image "subtracted darkvector on `date`"
    end
  endif

end


#
# 3. for each target frame, create a flatfield vector and divide by it
#
foreach camera ($cameralist)
  set base=${prefix}${camera}

  set list=`echo $base*.fts`
  if ($status == 0) then
    foreach image ($list)
      echo "creating flatfield vector for image $image ..."
      set flat=${image:r}_flat.fts
      makevec $image outfile=$flat col arraysize=$ARRAYSIZE
      mn $flat
      divvec $image $flat flat col
      comment $image "divided by median flatvector on `date`"
    end
  endif

end


#
# 4. find stars in each image, placing the list of stars into a file
#    with the same basename, but extension ".coo"  Then, measure
#    aperture magnitudes for all the stars, placing the results
#    into a file with extension ".pht"
#

foreach camera ($cameralist)
  set base=${prefix}{$camera}

  set list=`echo $base*.fts`
  if ($status == 0) then
    foreach image ($list)
      echo "finding stars for image $image ... "
      stars $base minsig=5 outfile=$base.coo
      echo "measuring aperture magnitudes for image $image ... "
      phot $image aper=3 aper=6 infile=$im.coo outfile=$im.pht
    end
  endif

end



exit 0


