#!/usr/bin/ksh
#
net="GE"             # network code
STAT="PABE          # station code
BASE="PABE_"         # seisan database
loc=""               # location code
bc="B"               # band code
ic="H"               # instrument code
oc1="Z"              # orientation code first channel (Z)
oc2="N"              # orientation code second channel (N/1)
oc3="E"              # orientation code third channel (E/2)
dc="D"               # data code

ARCHIVE='/quake02/seedlink/archive'
export SEISAN_TOP=/quake05/seisan
SEISAN_PRO='/home/clairmont/seismo/PRO'

###########

export LD_LIBRARY_PATH=/quake10/pv/seisan/LIB:/lib:/usr/dt/lib:/usr/openwin/lib:/usr/local/lib:/usr/local/SUNWspro/lib

cd $SEISAN_TOP/WAV/$BASE/tmp

# clean up
find $SEISAN_TOP/WAV/$BASE/tmp -name "20*"$STAT"*" -type f -exec rm {} \;

jday=$(/net/dupont/quake10/pv/PeterVoss/gnu/coreutils-5.2.1/src/date --date='1 day ago' +%j)
YEAR=$(/net/dupont/quake10/pv/PeterVoss/gnu/coreutils-5.2.1/src/date --date='1 day ago' +%Y)

if [ $# -eq 1 ] ; then
jday=$1
fi
if [ $# -eq 2 ] ; then
YEAR=$1
jday=$2
fi

echo year $YEAR jday $jday

MDR=$(/home/clairmont/seismo/bin/calday $jday $YEAR | head -3 | tail -1 | awk '{print $3}')
DAY=$(/home/clairmont/seismo/bin/calday $jday $YEAR | head -3 | tail -1 | awk '{print $4}')

~seismo/bin/dataselect -nb -Pr -o $STAT.$bc$ic$oc1 $ARCHIVE/$YEAR/$net/$STAT/$bc$ic$oc1.$dc/$net.$STAT.$loc.$bc$ic$oc1.$dc.$YEAR.$jday
~seismo/bin/dataselect -nb -Pr -o $STAT.$bc$ic$oc2 $ARCHIVE/$YEAR/$net/$STAT/$bc$ic$oc2.$dc/$net.$STAT.$loc.$bc$ic$oc2.$dc.$YEAR.$jday
~seismo/bin/dataselect -nb -Pr -o $STAT.$bc$ic$oc3 $ARCHIVE/$YEAR/$net/$STAT/$bc$ic$oc3.$dc/$net.$STAT.$loc.$bc$ic$oc3.$dc.$YEAR.$jday


~seismo/bin/mscut $STAT.$bc$ic$oc1
~seismo/bin/mscut $STAT.$bc$ic$oc2
~seismo/bin/mscut $STAT.$bc$ic$oc3

# convert to single file mseed format :

ls $YEAR-*"M."$net"_"$STAT"_"$loc"_"$bc$ic? > mergemseed.lis

nfiles=$( cat mergemseed.lis | grep $bc$ic$oc1 | wc -l )
echo $nfiles
i=1
k=1
while [ $i -le $nfiles ] ; do
j=1
fname=$(head -$k mergemseed.lis | tail -1 | awk -F"-" '{print $1"-"$2"-"$3"-"$4"-00M"}')
fname=$(echo $fname.$net"_"$STAT"_"$loc"_"$bc$ic)
echo $fname
touch $fname
rm $fname
touch $fname
rm msfile.tmp
touch msfile.tmp
while [ $j -le 3 ] ; do
msfile=$(head -$k mergemseed.lis | tail -1)
echo $msfile >> msfile.tmp
k=$(($k+1))
j=$(($j+1))
done
# sort and write new fil
msfile=$(grep "$bc$ic$oc1" msfile.tmp); cat < $msfile >> $fname
msfile=$(grep "$bc$ic$oc2" msfile.tmp); cat < $msfile >> $fname
msfile=$(grep "$bc$ic$oc3" msfile.tmp); cat < $msfile >> $fname
i=$(($i+1))
done

# delete REA file from the same day :
ls $SEISAN_TOP/REA/$BASE/$YEAR/$MDR/$DAY-*
\rm $SEISAN_TOP/REA/$BASE/$YEAR/$MDR/$DAY-*

# delete WAV file from the same day :
ls $SEISAN_TOP/WAV/$BASE/$YEAR/$MDR/$YEAR-$MDR-$DAY-*
\rm $SEISAN_TOP/WAV/$BASE/$YEAR/$MDR/$YEAR-$MDR-$DAY-*

# AUTOREG in REA and move to WAV
files=$(echo $YEAR-$MDR-$DAY-*M."$net"_"$STAT"_"$loc"_"$bc$ic")
$SEISAN_PRO/dirf $files
echo "R\nm\n"$BASE"\ncron\n*\n" | $SEISAN_PRO/autoreg

echo done year-jday : $YEAR-$jday
# EOF
