#! /bin/sh
# Supolar - Demo shell script for SUPOLAR
# Author: Nils Maercklin, GFZ Potsdam, Germany, 2001 

# PostScript flag (set to 1 to generate PS output instead of X display):
POSTSCRIPT=0

# X window sizes and positions:
WIDTH=200
HEIGHT=300
WIDTHOFF1=0
WIDTHOFF2=200
WIDTHOFF3=400
WIDTHOFF4=600
HEIGHTOFF1=0
HEIGHTOFF2=300

# Correlation window length in seconds:
wl=0.1

# Look for data or run script MakeData:

if [ ! -f data3c.su ]
then
  if [ -f MakeData ]
  then
    MakeData
  else
    echo "Can't find data3c.su or script MakeData"
    exit 1
  fi
fi


# Polarization analysis:
# With the defaults rl=1, dir=1, and file=polar, the following command line
# will produce 8 output files named "polar.*". Here, the inclination theta 
# and the horizontal azimuth angle phi are in ite interval -90...90 deg.

echo "Polarization analysis, wl=$wl seconds"

supolar <data3c.su theta=1 phi=1 pln=1 ellip=1 angle=deg rlq=0.5 wl=$wl


# X display of results:

if [ $POSTSCRIPT -eq 0 ]
then

  echo "X display of results"
  
  # Display vertical and horizontal components:
  
  suwind < data3c.su key=trid min=12 max=12 |
  suxwigb title="vertical component" windowtitle="Z" label1="Time(SEC)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF1 ybox=$HEIGHTOFF1 &

  suwind < data3c.su key=trid min=14 max=14 |
  suxwigb title="horizontal component 1" windowtitle="H 1" label1="Time(SEC)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF2 ybox=$HEIGHTOFF1 &

  suwind < data3c.su key=trid min=13 max=13 |
  suxwigb title="horizontal component" windowtitle="H 2" label1="Time(SEC)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF3 ybox=$HEIGHTOFF1 &

  # Display ellipticity:
  suximage <polar.e21 title="ellipticity, e21" windowtitle="e21" \
    label1="Time(SEC)" legend=1 bclip=1 wclip=0 \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF4 ybox=$HEIGHTOFF1 &

  # Wait a moment ...
  sleep 2
  
  # Display inclination theta and horizontal azimuth phi:

  suximage <polar.phi title="horizontal azimuth, phi" windowtitle="phi" \
    label1="Time(SEC)" legend=1 bclip=90 wclip=-90 \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF1 ybox=$HEIGHTOFF2 &

  suximage <polar.theta title="inclination, abs(theta)" windowtitle="theta" \
    label1="Time(SEC)" legend=1 bclip=90 wclip=0 \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF2 ybox=$HEIGHTOFF2 &

  # Display rectilinearity rl and planarity pln:

  suximage <polar.rl title="rectilinearity, rl" windowtitle="rl" \
    label1="Time(SEC)" legend=1 bclip=1 wclip=0 \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF3 ybox=$HEIGHTOFF2 &
  
  suximage <polar.pln title="planarity, pln" windowtitle="pln" \
    label1="Time(SEC)" legend=1 bclip=1 wclip=0 \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF4 ybox=$HEIGHTOFF2 &  

# PostScript plot of results:
else

  echo "PostScript plot of results"

  # Plot three components of seismic data:  
  
  for component in 43 44 45
  do
    suwind <data3c.su key=trid min=$component max=$component |
    supswigp linewidth=0 title="data, trid=$component" \
    label1="Time(SEC)" label2="Distance(KM)" > data.${component}.eps
  done
  
  
  # Plot polarization parameters rl, pln, e21:
  
  for parameter in rl pln e21
  do
    supsimage <polar.$parameter d2s=0.2 title="parameter $parameter" \
      label1="Time(SEC)" label2="Distance(KM)" bclip=1 wclip=0 \
      legend=1 lstyle=vertright > polar.$parameter.eps
  done

  # Plot angles theta and phi:
  
  supsimage <polar.phi d2s=0.2 title="azimuth phi (DEG)" \
    label1="Time(SEC)" label2="Distance(KM)" bclip=90 wclip=-90 \
    legend=1 lstyle=vertright > polar.phi.eps  
  
  suop <polar.theta op=abs |
  supsimage d2s=0.2 title="inclination abs(theta) (DEG)" \
    label1="Time(SEC)" label2="Distance(KM)" bclip=90 wclip=0 \
    legend=1 lstyle=vertright > polar.theta.eps  
    
  # Merge these eight plots onto two pages:
  
  echo "Merge plots and clean up a bit"
  
  merge4 data.43.eps data.44.eps data.45.eps polar.e21.eps        > supolar1.eps
  merge4 polar.phi.eps polar.theta.eps polar.rl.eps polar.pln.eps > supolar2.eps

  # Remove single plots:
  rm -f data.4?.eps polar.*.eps
  
  echo "Output PostScript files: supolar1.eps  (data and parameter e21)"
  echo "                         supolar2.eps  (parameters phi, theta, rl, pln)"
  echo " "
  echo "You may view these with your PostScript previewer"

fi

exit 0
