#!/usr/bin/sh
# filename: wells.sh
# author:   Marvin Simkin
# date:     2002-10-06
# purpose:  Extract wells data

# syntax:   wells.sh

# remove useless quotes
sed 's/"//g' < wells.txt > wells01.txt

# discard lines with atypical coord system
egrep 'Coord|,SP,|,Sta,|,State Plane AZC,' < wells01.txt > wells02.txt


### I used to do a lot of processing of this file to render an image here
### But now all I need is rows with X, Y data
### And I will let subsurf.pl decide what to keep
#### discard lines with no depth
### NO we need some of these, find the depth elsewhere
###exit
###egrep -v ',$|,0$' < wells02.txt > wells03.txt
###
#### discard lines with no elevation
###grep -v ',,' < wells03.txt > wells04.txt
###
#### remove duplicate or invalid locations
###./wells.pl < wells04.txt | sort > wells05.txt
###
#### make instructions for lines from x,y,z to x,y,z
###grep -v 'Coord' < wells05.txt | ./makexyz.pl > wells06.txt
###
#### make bounding box
###grep -v = < wells06.txt | makebbox.pl -B1000/1000/100 > wells06.box
###
#### turn those instructions into VRML 1.0 lines
###cat wells06.txt wells06.box | makeiv.pl > wellline.iv


# subsurface file...
# the data needs some cleanup first

### file contains \226 char as dash; convert to @
##tr "\226" @ < phx_basin_subsurface_final_39-53.txt |
##
##  # a few times the dashes are minuses instead
##  sed "s/ - / @ /" > phx_basin_subsurface_final_39-53.fix

## ABOVE had to be hand-edited into phx_basin_subsurface_final_39-53.vi

./subsurf.pl wells02.txt phx_basin_subsurface_final_39-53.vi > phx_basin_subsurface_final_39-53.ssf

# collect useful wells from subsurf.pl output
grep '^well' phx_basin_subsurface_final_39-53.ssf | grep -v '||$' | grep -v invalid | grep -v '^well|.*|.*|.*|.*|N.*|.*|.*$' > wells07.txt


# find boundaries in feet
WESTFEET=`cut -f7 -d'|' wells07.txt | sort -n | head -1`
EASTFEET=`cut -f7 -d'|' wells07.txt | sort -n | tail -1`
SOUTHFEET=`cut -f8 -d'|' wells07.txt | sort -n | head -1`
NORTHFEET=`cut -f8 -d'|' wells07.txt | sort -n | tail -1`

echo "In feet, WEST=$WESTFEET, EAST=$EASTFEET, SOUTH=$SOUTHFEET, NORTH=$NORTHFEET"

# convert to degrees based on my ground measurement factors
##set -x

WESTDEG=`perl -e 'print ((('$WESTFEET' - 466326.4) / 306741) - 112.02730)'`
EASTDEG=`perl -e 'print ((('$EASTFEET' - 466326.4) / 306741) - 112.02730)'`
SOUTHDEG=`perl -e 'print ((('$SOUTHFEET' - 890493.6) / 370024) + 33.44793)'`
NORTHDEG=`perl -e 'print ((('$NORTHFEET' - 890493.6) / 370024) + 33.44793)'`

echo "In degrees, WEST=$WESTDEG, EAST=$EASTDEG, SOUTH=$SOUTHDEG, NORTH=$NORTHDEG"



## THIS WAS FOR HOMEWORK 8
### make psxyz input for those wells
##awk '
## BEGIN { FS = "|" }
## { printf "%12.5f,%12.5f,%4d\n%12.5f,%12.5f,%4d\n>\n", $7, $8, $3, $7, $8, $3 - $6 }
##' < wells07.txt > wells07.psxyz
##
##
##
##HERE=`pwd`
##cd $HOME/glg598a/hw08
##set -x
##./c.hw08-3 $WESTDEG $EASTDEG $SOUTHDEG $NORTHDEG
##./c.hw08-4 $WESTFEET $EASTFEET $SOUTHFEET $NORTHFEET
##set +x
##cd $HERE


# make a new wells plot with colors for unit types
# this is hardcoded to generate wells08.txt line draw instructions
# and top.xyz, fill.xyz, and bed.xyz surface info
./makewell.pl < wells07.txt

# make surfaces for those .xyz files
##set -x
##./c.surface $WESTFEET $EASTFEET $SOUTHFEET $NORTHFEET top
##./c.surface $WESTFEET $EASTFEET $SOUTHFEET $NORTHFEET fill
##./c.surface $WESTFEET $EASTFEET $SOUTHFEET $NORTHFEET bed
##set +x

# to take a look at some of these in MATLAB
##cut -f2-3 top.surf | while read Y Z; do if test "x$Y" = "x$LASTY"; then D="`echo $D` $Z"; else LASTY=$Y; echo $D; D=; fi; done > top.d
##cut -f2-3 fill.surf | while read Y Z; do if test "x$Y" = "x$LASTY"; then D="`echo $D` $Z"; else LASTY=$Y; echo $D; D=; fi; done > fill.d
##cut -f2-3 bed.surf | while read Y Z; do if test "x$Y" = "x$LASTY"; then D="`echo $D` $Z"; else LASTY=$Y; echo $D; D=; fi; done > bed.d



# make bounding box
echo 'color=0.4 0.4 0.4;linewidth=1' > wells08.box
$HOME/bin/makebbox.pl -B5280/5280/100 < wells08.txt >> wells08.box

# set surface colors similar to well line colors
# following from makewell.pl:
##$GravelColor = '0.700 0.700 0.800';
##$FillColor = '0.500 0.500 1.000';
##$BedColor = '0.605 0.200 0.200';
echo 'color=0.800 0.800 0.900;linewidth=1' > top.color
echo 'color=0.700 0.700 1.000;linewidth=1' > fill.color
echo 'color=0.800 0.400 0.400;linewidth=1' > bed.color

# turn those instructions into VRML 1.0 lines
cat wells08.txt wells08.box | $HOME/bin/makeiv.pl > wells.iv

# 3x exaggeration
cat wells08.txt wells08.box | $HOME/bin/scale.pl 1 1 3 | $HOME/bin/makeiv.pl > wellsx3.iv

./makeroad.pl < 20021117.txt > 20021117.lin
echo 'linewidth=1' > roads.lin
cat roads.lin 20021117.lin | $HOME/bin/makeiv.pl > roads.iv

# 3x exaggeration
cat roads.lin 20021117.lin | $HOME/bin/scale.pl 1 1 3 | $HOME/bin/makeiv.pl > roadsx3.iv

#for TENSION in 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
#do
#  cat top.color ${TENSION}top.lin | $HOME/bin/makeiv.pl > ${TENSION}wellstop.iv
#  cat fill.color ${TENSION}fill.lin | $HOME/bin/makeiv.pl > ${TENSION}wellsfill.iv
#  cat bed.color ${TENSION}bed.lin | $HOME/bin/makeiv.pl > ${TENSION}wellsbed.iv
#done

# NEW STUFF, move to c.surface?
./makesurf.pl < 0.0bed.surf > 0.0bed.draw
cat bed.color 0.0bed.draw | $HOME/bin/makeiv.pl > surfbed.iv

# 3x exaggeration
cat bed.color 0.0bed.draw | $HOME/bin/scale.pl 1 1 3 | $HOME/bin/makeiv.pl > surfbedx3.iv

