#! /bin/sh
# Supofilt - Demo shell script for SUPOFILT
# 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=400
HEIGHT=500
WIDTHOFF1=0
WIDTHOFF2=410
HEIGHTOFF1=50
HEIGHTOFF2=100
HEIGHTOFF3=150

# Filter settings:
wl=0.10   # Correlation window length in seconds
sl=0.05   # Smoothing window for filter weights in seconds
dpow=1    # Exponent of direction cosines (directivity functions)
wpow=1    # Exponent of weighting function (here: rectilinearity)


# 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 supolar:
# With the defaults rl=1, dir=1, and file=polar, this command produces the 
# output files polar.rl and polar.dir.

echo "Computing polarization attributes, wl=$wl seconds"

supolar <data3c.su rlq=0.5 wl=$wl


# Apply polarization filter:
# By default, this filter requires the files polar.dir and polar.rl as
# written by supolar, to get direction and rectilinearity information.

echo "Polarization filter, sl=$sl, dpow=$dpow, wpow=$wpow"

supofilt <data3c.su dpow=$dpow wpow=$wpow sl=$sl >pofi3c.su


# X display of results:

if [ $POSTSCRIPT -eq 0 ]
then

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

  suwind < pofi3c.su key=trid min=12 max=12 |
  suxwigb title="filtered vertical component" windowtitle="filtered Z" \
    label1="Time(SEC)" label2="Distance(KM)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF2 ybox=$HEIGHTOFF1 &

  sleep 1
  suwind < data3c.su key=trid min=13 max=13 |
  suxwigb title="horizontal component 1" windowtitle="H 1" \
    label1="Time(SEC)" label2="Distance(KM)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF1 ybox=$HEIGHTOFF2 &

  suwind < pofi3c.su key=trid min=13 max=13 |
  suxwigb title="filtered horizontal component 1" windowtitle="filtered H 1" \
    label1="Time(SEC)" label2="Distance(KM)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF2 ybox=$HEIGHTOFF2 &

  sleep 1
  suwind < data3c.su key=trid min=14 max=14 |
  suxwigb title="horizontal component 2" windowtitle="H 2" \
    label1="Time(SEC)" label2="Distance(KM)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF1 ybox=$HEIGHTOFF3 &

  suwind < pofi3c.su key=trid min=14 max=14 |
  suxwigb title="filtered horizontal component 2" windowtitle="filtered H 2" \
    label1="Time(SEC)" label2="Distance(KM)" \
    wbox=$WIDTH hbox=$HEIGHT xbox=$WIDTHOFF2 ybox=$HEIGHTOFF3 &


# PostScript plot of results:
else

  echo "PostScript plot of results"
  
  for component in 12 13 14
  do
    # Plot input data:
    suwind <data3c.su key=trid min=$component max=$component |
    supswigp linewidth=0 title="input data, trid=$component" \
      label1="Time(SEC)" label2="Distance(KM)" > data3c.eps
 
    # Plot filtered data:
    suwind <pofi3c.su key=trid min=$component max=$component |
    supswigp linewidth=0 title="filtered data, trid=$component" \
      label1="Time(SEC)" label2="Distance(KM)" > pofi3c.eps
    
    # Merge both plots:
    merge2 data3c.eps pofi3c.eps > supofilt${component}.eps
    
    rm -f data3c.eps pofi3c.eps
  done
  
  echo "Output PostScript files: supofilt12.eps  (vertical component)"
  echo "                         supofilt13.eps  (horizontal component 1)"
  echo "                         supofilt14.eps  (horizontal component 2)"
  echo " "
  echo "You may view these with your PostScript previewer"
fi

exit 0
