#! /bin/csh

# proc_chirp
   echo "Process chirp usage:"
   echo "    chirp tmin tmax sufile hbox"
   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 "    edit the chirp 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)}' `
   set numtraces = `expr $6 - $5`
   echo "number of traces in this plot = "$numtraces

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

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

   echo "The minimum FFID is "$ffidmin
   echo "The maximum FFID is "$ffidmax
   echo " "
   echo "Making the PostScript plot"

# select data window and gain
  suwind key=fldr min=$5 max=$6 itmin=0 tmin=$1 tmax=$2 verbose=0 < $3.su | \
  suchw key1=delrt a=0 | \
#  sushift tmin=0.05 tmax=0.3 verbose=1 | \
  suchw key1=tracr key2=fldr | \
#  suflip flip=2 | \
  sugain panel=0 scale=0.5 tpow=1 > $3.tmp

# set the plot X size based on the number of traces/inch
  set wbox = `expr $numtraces / 100`
  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 = 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

 supsimage wbox=$wbox hbox=$hbox \
 ybox=0.5 \
 x1beg=$1 x1end=$2 \
 d1num=0.05 d2num=100 \
 d2=1 \
# d1=0.000976 \
 n1tic=10 n2tic=1 \
 bperc=99 wclip=0 \
 brgb=0.0,0.0,0.0 wrgb=1.0,1.0,1.0 bps=12 \
 bhls=0.0,1.0,0.0 whls=0.0,1.0,0.0 \
 grid1=dot grid2=dot \
 title="GYRE 99002 SIS-1000 CHIRP Line $3 SHOT $5-$6" \
 label1="TWT [s]" label2="SHOT" \
 < $3.tmp > ../plot/$3$7.ps

rm $3.tmp

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

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

# view the PS with GhostView
#gv -portrait ../plot/$3$7.ps
