#!/bin/csh -f

#SEED TO DB. Usage: mseed2db <seed volume> <dbase dir>  <work dir>  <dbase name> <year> <julian day>
#                         <hour> <min> <sec>   <end year>  <end julian day> <end hour> <end min> <end sec>
#                          <procflag>  <delflag>

echo 'unalias rm'
unalias rm
echo 'unalias cp'
unalias cp

# - sometimes it needs

#source ${FMT}/setup.csh 

set SEDVOLNAME1=$1
set PLACEDIR=$2
set WORKDIR=$3
set DBNAME=$4
set YEAR=$5
set JDAY=$6
set HR=$7
set MIN=$8
set SEC=$9
set FYEAR=$10
set FJDAY=$11
set FHR=$12
set FMIN=$13
set FSEC=$14
set PROCFLAG=$15
set DELFLAG=$16

if (${?WORKDIR} != 1) then
goto scriptend
endif

echo seed vol name 1  =         $SEDVOLNAME1
echo working dir =      $WORKDIR
echo Dbase dir	        $PLACEDIR
echo Db Name   =        $DBNAME
echo year =             $YEAR
echo julian day =       $JDAY
echo hour =             $HR
echo min =              $MIN
echo sec =              $SEC

echo year end =         $FYEAR
echo julian day end =   $FJDAY
echo hour end =         $FHR
echo min end  =         $FMIN
echo sec end  =         $FSEC
echo procflag =         $PROCFLAG
echo delflag =          $DELFLAG

if ($PROCFLAG == Y) then
echo You have chosen to process the seed data and then make fmt volume...
else
echo You are only converting SEED to CSS and archiving...
endif

if ($DELFLAG == Y) then
echo You have chosen to delete the working material when finished...
else
echo You have chosen not to delete the working material... THE DISK WILL FILL UP...
endif
 


if ($PROCFLAG == Y) then

echo ' '
echo  Moving to working directory...
echo ' '
cd $WORKDIR
pwd

#*************************************************************************

echo ' '
echo 4. Process the event into an event volume...

echo ' '
echo THIS IS A FMT SCRIPT  EXTRACTS   DATA ON ALL CHANNELS...

cd $WORKDIR
echo Processing in $WORKDIR

cat ${DBNAME}.wfdisc > ${JDAY}.wfdisc

echo ' '
echo Entering dbase $DBNAME


# remove broad band data

# 2. Merge CHANGE HERE.
echo ' '
echo 2. Merging wfdiscs.....

cat ${JDAY}.wfdisc > temp.wfdisc

# 3. Repair wfdisc
#	clean repititions
echo ' '
echo 3a. Cleaning wfdiscs of repititions.....
dbclean temp temp2

rm  temp.wfdisc 

mv temp2.wfdisc temp.wfdisc

#	fix calibs, etc.
echo ' '
echo 3b. Fixing calibrations.....Skiping
	
#ncalib2calib temp $FMT/gsn/gsn 


# 4. separate wfdisc by channels produces temp_s, temp_l, temp_v.
echo ' '
echo 4. Separating channels.....
dbchnsep temp
 
# 5. uncompress waveforms and deglitch _s and _l Redundancy problem?.
echo ' '
echo 5a. Uncompressing if necessary.....
cd wf
uncompress -f *.Z
cd ..

# 6. decimation and merger produces temp_l, temp_v
echo ' '
echo 6a. Decimating B,M, put into Lb, Lm.....
dbdecimate 1. temp_s temp_l_dec wf	

if ( -e temp_l.wfdisc) then  		
mv temp_l.wfdisc junk.wfdisc
cat junk.wfdisc temp_l_dec.wfdisc > temp_l.wfdisc
rm junk.wfdisc
rm temp_l_dec.wfdisc
 else
mv temp_l_dec.wfdisc  temp_l.wfdisc
endif

 

echo ' '
echo 6b. Decimating L, Lb, Lm, put into Vl, Vb, Vm.....
dbdecimate 0.1 temp_l temp_v_dec wf 
if ( -e temp_v.wfdisc) then
mv temp_v.wfdisc junk.wfdisc
cat junk.wfdisc temp_v_dec.wfdisc > temp_v.wfdisc
rm junk.wfdisc
rm temp_v_dec.wfdisc
 else
mv temp_v_dec.wfdisc  temp_v.wfdisc
endif


# 7. Remove redundancies in _l and _v wfdiscs.
# should be done before extraction, since redundant waveforms not removed.
echo ' '
echo 7. Removing redundancies caused by decimation.....Decommissioned

# 8. Extraction CHANGE NAMES AND TIMES HERE.

mkdir wf_l
mkdir wf_v
echo ' '

echo dir =           $WORKDIR
echo year =          $YEAR
echo julian day =    $JDAY

echo hour =          $HR
echo min =           $MIN
echo sec =           $SEC
echo julian day end =   $FJDAY
echo hour end =         $FHR
echo min end  =         $FMIN
echo sec end  =         $FSEC


echo ' '
echo 8a. Extracting 15 hours of L data.....
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
dbextract temp_l ${DBNAME}_l wf_l -t $YEAR $JDAY $HR $MIN $SEC $FYEAR $FJDAY $FHR $FMIN $FSEC
echo ' '

echo 8b. Extracting T hours of V data.....
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
dbextract temp_v ${DBNAME}_v wf_v -t $YEAR $JDAY $HR $MIN $SEC $FYEAR $FJDAY $FHR $FMIN $FSEC

# 9. Correct overlapping waveforms
echo ' '
echo 9. Correct overlapping waveforms 

dbclean ${DBNAME}_l clean_l
dbclean ${DBNAME}_v clean_v

rm $DBNAME*.wfdisc
mv clean_l.wfdisc ${DBNAME}_l.wfdisc
mv clean_v.wfdisc ${DBNAME}_v.wfdisc

# 10. Make waveform boundaries {CHANGE NAMES HERE.
echo ' '
echo 10a. Running gap2edit on _l.....
gap2edit ${DBNAME}_l  
echo ' '
echo 10b. Running gap2edit on _v.....
gap2edit ${DBNAME}_v 

echo ' '
echo Creating new database with decimated response info...

mk_dbdecim $DBNAME ${DBNAME}_decim

echo Merging origonal and new database ...

cat ${DBNAME}.sitechan ${DBNAME}_decim.sitechan > temp.sitechan
mv ${DBNAME}.sitechan ${DBNAME}.sitechan.old
sort temp.sitechan > ${DBNAME}.sitechan
\rm temp.sitechan

cat ${DBNAME}.sensor ${DBNAME}_decim.sensor > temp.sensor
mv ${DBNAME}.sensor ${DBNAME}.sensor.old
sort temp.sensor > ${DBNAME}.sensor
\rm temp.sensor

cat ${DBNAME}.instrument ${DBNAME}_decim.instrument > temp.instrument
mv ${DBNAME}.instrument ${DBNAME}.instrument.old
sort temp.instrument > ${DBNAME}.instrument
\rm temp.instrument

\rm ${DBNAME}_decim.*

dbfixids $DBNAME inid 1
dbfixids $DBNAME chanid 1

date

#*****************************************************************************
#       wf
\rm -rf wf  temp* test* clean* *.seed *.bak wfAh
echo ' '
echo 11. Copy the _l and _v material to $PLACEDIR...
echo Copying _l and _v material to PLACEDIR...

cd $PLACEDIR
pwd
cp -r $WORKDIR/* .
endif
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


#mail -s" Processing for event $EVENT is complete." $USER

if ($DELFLAG == Y) then
echo ' '
echo 12. Deleting working files...
echo ' '
cd $WORKDIR
pwd
\rm -f -r  *
cd $PLACEDIR

endif

scriptend:
echo Program Ends.

