#!/bin/bash
#------------------------------------------------------------------------------
# Script to pull the SDSS spectra from Princeton to a local machine.
# This script uses the Unix "rsync" command, which will only copy over
# files that have been created or modified since the last time you
# ran this script.  This lets you run this script each day in order to
# keep your data up-to-date.
#
# Syntax:
#   pullspectra
# or
#   pullspectra /my/directory
#
# Assume that the data comes from:
#   alfred@spectro.princeton.edu:$BOSS_SPECTRO_REDUX
# Assume that the data goes to the local machine at the destination
# given by the first argument to this command, or, if not set, then to:
#   $BOSS_SPECTRO_REDUX
# These defaults can be over-written by changing the appropriate lines
# in this script.  For example, if you are copying the "test" reductions
# from Princeton, you would use this line:
#   topremote=/u/dss/spectro_test
#
# This script is written to copy the following summary files:
#   platelist*
#   spAll*
# For each plate, the following files are copied:
#   "*.ps *.log *.par spPlate* spZ*"
# If you really want to copy everything, including the intermediate outputs,
# then change the --include line below to:
#   --include "*"
#
# Make sure that you have plenty of disk space!
# The spectra + 1D outputs fill ~125 Gb as of May 2002.
# The full set of outputs fills ~250 Gb as of May 2002.
#
# Rsync flags are as follows:
#   -c always checksum
#   -r recurse into directories
#   -t preserve times
#   -u update only (donīt overwrite newer files)
#   -v increase verbosity
#------------------------------------------------------------------------------

remotehost=alfred@spectro.princeton.edu

# Exit if this process is already running.

n=`\ps -elf | grep pullspectra | grep -v grep | wc -l`
if [ X"$n" != X"" -a "$n" -gt 2 ]; then
   echo "pullspectra already running at "`date`
   exit
fi
exit

# Use the first argument to this command as the local destination directory.
# Otherwise, use the environment variable $BOSS_SPECTRO_REDUX.

if [ $# -ge 1 ] ; then
  toplocal=$1
else
  toplocal=$BOSS_SPECTRO_REDUX
fi
if [ -z "$toplocal" ] ; then
  echo "Environment variable BOSS_SPECTRO_REDUX is not set on local machine"
  exit
fi

# Use the environment variable $BOSS_SPECTRO_REDUX on the remote machine
# as the path for the spectroscopic data.

topremote=`ssh1 $remotehost "echo $BOSS_SPECTRO_REDUX"`
if [ -z "$topremote" ] ; then
  echo "Environment variable BOSS_SPECTRO_REDUX is not set on remote machine"
  exit
fi

echo Copy from $remotehost:$topremote
echo Copy to $toplocal

exit
# Copy the summary files

rsync -tuv --rsh="ssh1" $remotehost:$topremote/"platelist*" $toplocal
rsync -tuv --rsh="ssh1" $remotehost:$topremote/"spAll*" $toplocal

# Loop through each plate, create the local directory for that plate,
# then copy the requested files.

allplate=`ssh1 $remotehost "cd $topremote; ls -1d [0-9][0-9][0-9][0-9]"`
for thisplate in $allplate ; do
  echo Working on plate=$thisplate
  mkdir $toplocal/$thisplate
  rsync -tuv --rsh="ssh1" \
   --include "*.ps *.log *.par spPlate* spZ*" \
   --exclude "*" \
   $remotehost:$topremote/$thisplate/"*" $toplocal/$thisplate
done

exit
#------------------------------------------------------------------------------
