#! /bin/csh

# proc_sparker
   echo "Process sparker usage:"
   echo "    sparker tmin tmax sufile hbox pclip"
   echo "       tmin and tmax (seconds) determine vertical plot window"
   echo "       ie, 0 0.25 (window 0 to 250 ms)"
   echo "       the SU input file excluding the .su extension"
   echo "       hbox is the vertical plot size in inches"
   echo "       pclip is the perc. clipping of pos. amp. (high=lighter; "
   echo "       lower=darker)"
#   echo "    edit the sparker script to set the clipping parameter"
   echo " "

#get number of traces
   echo "getting the total number of traces"
   set numtraces = `surange < $3.su | grep traces | awk ' {print($1)}' `
   echo "number of traces = "$numtraces

# get ffid range for input
   echo "getting the range of FFID's"

   set ffidmin = `surange < $3.su | grep fldr | sed 's/fldr=//; s/(//g; s/)//g' | awk ' {print($1)}' | awk -F , ' {print($1)}' `
   set ffidmax = `surange < $3.su | grep fldr | sed 's/fldr=//; s/(//g; s/)//g' | awk ' {print($1)}' | awk -F , ' {print($2)}' `

   echo "The minimum FFID is "$ffidmin
   echo "The maximum FFID is "$ffidmax
   echo " "
   echo "Setting the data window"

# select data window and gain
  suwind key=fldr min=$ffidmin max=$ffidmax itmin=0 verbose=0 < $3.su | \
  sufilter f=150,200,1500,1700 | \
  sugain panel=0 scale=1 tpow=1.1 > $3.tmp


# set the plot X size based on the number of traces/inch
  set wbox = `expr $numtraces / 150`
  echo "the plot length =" $wbox "inches"
  echo

# set the plot Y size (hbox) in inches
  set tmin = $1
  echo "tmin ="$tmin
  set tmax = $2
  echo "tmax ="$tmax
#  set hbox = echo $1 $1 | awk ' {print($2)}'
#  set hbox = 7
#  set hbox = 8.5
#  set hbox = 12
#  set hbox = 18
#  set hbox = 20
  set hbox = $4
  echo "the plot hieght=" $hbox "inches"

# PLOT POSTSCRIPT IMAGE
# d1 is the sample interval (SR) in seconds that should be read from the header
# If the vertical scale is not correct - most likely the SR is not correct
# Set d2=decimation factor used in qmipstosegy
# d1=0.000122 d2=1 \

# Change percentile for determining black clip value to adjust plot contrast
# Higher bperc gives lighter plot - wclip=0 sets 0 and - amp. to white

nice supsimage wbox=$wbox hbox=$hbox \
 ybox=0.5 \
 x1beg=$1 x1end=$2 \
 d1num=0.02 d2num=500 \
 d2=1 \
# d1=0.083 \
 n1tic=4 n2tic=5 \
# bclip=15 wclip=0 \
 bperc=$5 wclip=0 \
 grid1=dot grid2=dot \
 title="ATSV99045 Sparker Line $3 SHOT $ffidmin-$ffidmax, \
sufilter f=300,500,2500,3000, sugain panel=0 scale=1 tpow=1.1" \
 label1="TWT [s]" label2="SHOT" \
 < $3.tmp > ../seisimag/$3.ps

rm $3.tmp

# convert PS plot to GIF image
makegif ../seisimag/$3

# convert PS plot to raster for Raytheon TDU printer
#tduseismic ../seisimag/$3

# convert PS plot to raster for Alden printer
echo "Converting PS plot to Alden raster file"
aldenseismic ../seisimag/$3

# view the PS with GhostView
echo "Opening PS plot in GhostView"
gv -portrait ../seisimag/$3.ps

echo "sparker $1 $2 $3 $4" >> proc_history
rm ../seisimag/$3.raw
