#!/bin/csh
#
# Name: repro_jitter_asn.csh
#
# Description: Assists in the reprocessing of OMS jitter exposures by
#
# - renaming all the *_jif.fits and *_jit.fits files in the cwd to
#   *.jif and *.jit, but in a new location: FGS_OUTPUT_DIR, as defined
#   by the supplied path (arg1)
#
# - renaming all the *_jwf.fits and *_jwt.fits files in the cwd to
#   *.jwf and *.jwt, but in a new location: FGS_OUTPUT_DIR, as defined
#   by the supplied path (arg1)
#
# - creating OSFs in the supplied path that will
#   trigger each jitter exposure for dc_jit (association collection)
#
# Usage: repro_jitter_asn.csh pathname
#
#        e.g. repro_jitter_asn.csh fgs
#
# History:
# 09/28/05  54362  MSwam   first version
# 10/24/05  54362  MSwam   remove wildcard expansion and split exp lists
# 03/26/07  57374  MSwam   tweak file extensions to match procedure steps
# 04/16/07  57374  MSwam   handle both jif/jit and jwf/jwt pairs
#
#----------------------------------------------------------------------
#
set pathname = $1
set raw_pathfile = OPUS_DEFINITIONS_DIR:${pathname}.path
set pathfile = `osfile_stretch_file ${raw_pathfile}`
if (! -f $pathfile) then
  echo "No such pathfile: $pathfile"
  exit 1
endif
#
# save off the exp names (trim the file extension) in a stamped temp file
set ymd_HMS = `date '+%y%m%d_%H%M%S'`
set expfile = explist.${ymd_HMS}
/bin/ls -1 | /bin/grep -v jwt | /bin/grep -v jit | /bin/sed -e "s/_jwf\.fits//g" -e "s/_jif\.fits//g" | /bin/grep j\$ > ${expfile}
#
# locate destination directory in the path file
set destDir = `/bin/grep FGS_OUTPUT_DIR $pathfile | /bin/awk '{print $3}'`
if (! -d $destDir) then
  echo "No such directory: $destDir"
  exit 1
endif
#
# split into smaller lists so loops don't choke
/bin/split -200 ${expfile} splitfile_
#
# rename the jif,jwf and jit,jwt files to the destination
foreach splitfile (`/bin/ls -1 splitfile_*`)
 foreach i (`cat ${splitfile}`)
  if (-f ${i}_jif.fits ) then
    /bin/mv ${i}_jif.fits $destDir/${i}.jif
  endif
  if (-f ${i}_jit.fits ) then
    /bin/mv ${i}_jit.fits $destDir/${i}.jit
  endif
  if (-f ${i}_jwf.fits ) then
    /bin/mv ${i}_jwf.fits $destDir/${i}.jwf
  endif
  if (-f ${i}_jwt.fits ) then
    /bin/mv ${i}_jwt.fits $destDir/${i}.jwt
  endif
  #
  # create OSF to trigger association collection
  set pathval = `basename $pathfile`
  echo "calling osf_create -p $pathval:r -f $i -n 000 -t fgs -c DC -s w"
  osf_create -p $pathval:r -f $i -n 000 -t fgs -c DC -s w
 end
end
#
# done
/bin/rm splitfile_*
exit 0
