#!/bin/sh

## this is the flow of processing steps for two purposes: 1) we add
## data to our database for research purposes. 2) we transform the ZIP
## files to the format IRIS wants them

# First step: your zip files collected in the field on date
# $servicedate are in
# /pal/fieldcamp/2011/passive_seismic/data/$servicedate/raw (and the
# Service Forms should digitized and stored in the same directory)

. /opt/antelope/5.1-64/setup.sh # run the antelope setup for the path
# to antelope functions.

# Our network code is:
NET=XN

# Our database we call:
dbname=neal_HS

servicedate=20120405

########### setup to correct directory structure ##########################
mkdir $servicedate/mseed # location for mseed files after extracting
# with unchunky (in rt2ms)
mkdir $servicedate/logs # location for *.log, *.err, and *.run files
mkdir $servicedate/day_volumes # day-long miniseed files after running
# miniseed2days. It is these files you
# FTP to IRIS.

###########################################################################
# this next section only necessary the first time you process data:
# copy the needed files to correct directory
#/bin/cp -f /pal/fieldcamp/2011/passive_seismic/codes/batchfile.pf .

# convert batch to parameter file:
#batch2par batchfile.pf > par_file_tmp.pf

# EDIT THE refstrm COLUMN to all 1s:
#sed 's/rs250spsrs/1/' par_file_tmp.pf > parfile.pf
#rm -rf par_file_tmp.pf
##########################################################################

# make a list of all zip files in the directory:
ls $servicedate/raw/*.ZIP > list.file

# convert the zip files from the reftek (rt) to miniseed (ms):
rt2ms -F list.file -p parfile.pf -o $servicedate/mseed/ -R -L

# so the next service date can be processed, make sure that:
rm -rf list.file

##########################################################################
# The next section is to convert the log files to mseed LOG files:
# log2miniseed needs some parameters changed from the default, before
# running:
/bin/cp -f $ANTELOPE/data/pf/log2miniseed.pf .

# this makes sure the LOG files get into the proper day_volumes
# directory to go with the data:
sed "/wfname/ c wfname $servicedate/day_volumes/%{sta}/%{sta}.%{net}.%{loc}.%{chan}.%Y.%j" log2miniseed_tst.pf

mv -f log2miniseed_tst.pf log2miniseed.pf

# script to convert log files to miniseed, for all log files in the
# mseed directory:
for file in `ls $servicedate/mseed/*log`
do
# first, we need establish the serial number of each file:
srnmb=`echo $file | awk -F . '{print $6}'`
# then, map serial number to station name:
case $srnmb
in
9477) log2miniseed -a -n XN -s PS01 $file;;
9261) log2miniseed -a -n XN -s PS02 $file;;
92C3) log2miniseed -a -n XN -s PS03 $file;;
984E) log2miniseed -a -n XN -s PS04 $file;;
9559) log2miniseed -a -n XN -s PS05 $file;;
9294) log2miniseed -a -n XN -s PS06 $file;;
956E) log2miniseed -a -n XN -s PS07 $file;;
9924) log2miniseed -a -n XN -s PS08 $file;;
9144) log2miniseed -a -n XN -s PS09 $file;;
9098) log2miniseed -a -n XN -s PS10 $file;;
929B) log2miniseed -a -n XN -s PS11 $file;;
esac
done
# note that PS11 changed RT130s early in the project!
##########################################################################

# move the raw log and err files out of the mseed directory:
mv $servicedate/mseed/*log $servicedate/mseed/*err $servicedate/logs/

# to build the antelope database do the following. DON'T DO THIS, if you
# are adding data to an existing database:
#dbbuild -b $dbname ./batchfile.pf >& dbbuild.out

# use "dbe $dbname" to look at the database to make sure it is sound

# link the waveforms to day_volumes for IRIS:
miniseed2days -Du -d $dbname -w "$servicedate/day_volumes/%{sta}/%{sta}.%{net}.%{loc}.%{chan}.%Y.%j" $servicedate/mseed/ >& msd2days.out

################################################################################
# the rest is just checking the integrity of the database:

# asign calibration values from the calibration table:
dbfix_calib $dbname

# verify the correlation of your data and database:
dbversdwf -tu $dbname
dbverify -tj $dbname >& dbverify.out

# create the dataless SEED volume (ONLY ONCE!). The dataless SEED volume, often
#referred to as a "dataless", contains the meta-data describing the
#station and instrumentation of your experiment. To generate the
#dataless SEED volume, run mk_dataless_seed, which builds the dataless
#from the contents of your experiment's database. You will submit this
#file along with the waveforms to PASSCAL. (should be named with the
#*current* date):

#outputfile=$NET.`date +%y`.$dbname.`date +%Y%j%H%M`.dataless

# make the dataless SEED volume:
#mk_dataless_seed -v -o $outputfile $dbname

# check the structure of the dataless SEED with
#seed2db -v $outputfile