#!/bin/ksh
# ***** shiftparser ver. 1.0 ********
# ************ lgs 05/04/02 *********
# 
echo ' '
echo 'This script converts output from the ADAPS Shift Analysis program'
echo 'to rdb format.  Using an input file named "shiftanly" the file'
echo 'allows removal of ice measurements and parses the output into two'
echo 'separate files, one for positive shifts and one for negative shifts'
echo 'that can be entered into a Tkg2 shift, or rating plot.'
echo ' '
echo '   Output files:  shiftpos.rdb - Positive shifts'
echo '                  shiftminus.rdb - Negative shifts'
echo '                  shift_anly_out.rdb - Both shifts with ice removed.'
echo ' '
echo '                  shiftparser ver. 1.0 - lgs 05/04/02'
echo ' '
echo ' '

# Parse spaces in the file to tabs and add column headers.

sed -n '14,100p' shiftanly > shift1
grep "   " shift1 > shift2
rm shift1
sed -e "s/      / /g" shift2 > shift3
rm shift2
sed -e "s/     / /g" shift3 > shift4
rm shift3
sed -e "s/    / /g" shift4 > shift5
rm shift4
sed -e "s/   / /g" shift5 > shift6
rm shift5
sed -e "s/  / /g" shift6 > shift7
rm shift6
sed -e "s/ /	/g" shift7 > shift8
rm shift7
print "BLANK\tNUMBER\tDATE\tTIME\tSTAGE\tDISCHARGE\tRTD\tPCT\tSHIFT" > shift9
print "10S\t4N\t10D\t4N\t6N\t10N\t1S\t6N\t6N" >> shift9
cat shift8 >> shift9
rm shift8
column NUMBER DATE STAGE DISCHARGE SHIFT < shift9 > shift10
rm shift9

# Remove selected measurements from parsed file

echo 'REMOVE ICE MEASUREMENTS? (y/n): '"\c"
read remove
no=1
rm shiftanly
mv shift10 shift_anly.rdb
cp shift_anly.rdb  file$no
while test $remove = "y"
do
cat file$no
echo 'ENTER MEASUREMENT NUMBER TO REMOVE:  '"\c"
read delete
row NUMBER ne "$delete" < file$no > file$no_out
rm file$no
mv file$no_out file$no
echo 'MORE? (y/n): '"\c"
read remove
done

# Create output files.

echo ' '
echo 'Creating Parsed Shift Files'
echo ' '
echo 'Creating shift_anly_out.rdb  File with both positive and negative shifts'
echo ' '
mv file$no shift_anly_out.rdb
rm shift_anly.rdb

# Create Positive Shift File
echo 'Creating shiftpos.rdb  Positive shifts only'
echo ' '
row SHIFT gt -.001 < shift_anly_out.rdb > shiftpos.rdb

# Create Negative Shift File
echo 'Creating shiftminus.rdb  Negative shifts only'
row SHIFT lt 0 < shift_anly_out.rdb > shiftminus.rdb
echo ' '
echo '       Thanks.....bye'
echo ' '
exit
