*sac macro file for automatically windowing and filtering all the stations
*in a directory for a particular event.  Window parameters should be based
*on previous examination of filtered records at a near and far station of the
*array - then linearly interpolate windows based on distance
*
*To avoid useless files, should previously have eliminated files that aren't
*to be used.
*
*Should have previously run setupwndws macro

* correct the phase shift ahead of pi/2 of horizontal component relative to vertical component 



echo on 

echo on 
*do dir list 20060203203412/

echo on 
*cd $dir
#echo off
readbbf 'wndbbf'

setbb begc 'sac'

evaluate to ddist %fdist - %ndist
evaluate to tctb2 %ctb2

*do sta list A-K L-N O-S S-Z

do file wild *.SAC

 setbb fn1 $file
 rh %fn1
* setbb fn2 (before %begc% %fn1%)
 setbb dst &1,DIST
 setbb end &1,E
 if %tctb2 GT %end
   evaluate to tctb2 %end
 endif 
 evaluate to disn %dst - %ndist
  cut %ctb1 %tctb2

 do BANDP from 1 to 13  by 1
  r %fn1
  cut off
  rmean
  taper

  if $BANDP EQ 1
    setbb lent %len1
    if %lent GT 0
    bp co .002 .012 n 4 p 2
    setbb SUFIX '143' 
* add 50 s to beginning and end of rect. window, to be tapered   
    evaluate to cut11 ( %fb1 - %nb1 ) * %disn / %ddist + %nb1 - 50
     %cut11
    evaluate to cut12 %cut11 + %len1 + 100
    endif               	
    if %fb1 LE  %nb1
    setbb lent 0
    endif

  elseif $BANDP EQ 2 
    setbb lent %len2
    if %lent GT 0
    bp co .003 .013 n 4 p 2
    setbb SUFIX '125' 
    evaluate to cut11 ( ( %fb2 - %nb2 ) * %disn / %ddist + %nb2 - 50 )
     %cut11
    evaluate to cut12 %cut11 + %len2 + 100
    endif
    if %fb2 LE %nb2
    setbb lent 0
    endif

  elseif $BANDP EQ 3
    setbb lent %len3
    if %lent GT 0
    bp co .004 .014 n 4 p 2
    setbb SUFIX '111' 
    evaluate to cut11 ( %fb3 - %nb3 ) * %disn / %ddist + %nb3 - 50
    evaluate to cut12 %cut11 + %len3 + 100
    endif
    if %fb3 LE %nb3
    setbb lent 0
    endif
  elseif $BANDP EQ 4 
    setbb lent %len4
    if %lent GT 0
    bp co .005 .015 n 4 p 2
    setbb SUFIX '100' 
    evaluate to cut11 ( %fb4 - %nb4 ) * %disn / %ddist + %nb4 - 50
    evaluate to cut12 %cut11 + %len4 + 100
    endif
    if %fb4 LE %nb4
    setbb lent 0
    endif
  elseif $BANDP EQ 5 
    setbb lent %len5
    if %lent GT 0
    bp co .007 .017 n 4 p 2
    setbb SUFIX '83' 
    evaluate to cut11 ( %fb5 - %nb5 ) * %disn / %ddist + %nb5 - 50
    evaluate to cut12 %cut11 + %len5 + 100
    endif
    if %fb5 LE %nb5
    setbb lent 0
    endif
  elseif $BANDP EQ 6 
    setbb lent %len6
    if %lent GT 0
    bp co .010 .020 n 4 p 2
    setbb SUFIX '66' 
    evaluate to cut11 ( %fb6 - %nb6 ) * %disn / %ddist + %nb6 - 50
    evaluate to cut12 %cut11 + %len6 + 100
    endif
    if %fb6 LE %nb6
    setbb lent 0
    endif
  elseif $BANDP EQ 7 
    setbb lent %len7
    if %lent GT 0
    bp co .015 .025 n 4 p 2
    setbb SUFIX '50' 
    evaluate to cut11 ( %fb7 - %nb7 ) * %disn / %ddist + %nb7 - 50
    evaluate to cut12 %cut11 + %len7 + 100
    endif
    if %fb7 LE %nb7
    setbb lent 0
    endif
  elseif $BANDP EQ 8 
    setbb lent %len8
    if %lent GT 0
    bp co .020 .030 n 4 p 2
    setbb SUFIX '40' 
    evaluate to cut11 ( %fb8 - %nb8 ) * %disn / %ddist + %nb8 - 50
    evaluate to cut12 %cut11 + %len8 + 100
    endif
    if %fb8 LE %nb8
    setbb lent 0
    endif
  elseif $BANDP EQ 9 
    setbb lent %len9
    if %lent GT 0
    bp co .025 .035 n 4 p 2
    setbb SUFIX '33' 
    evaluate to cut11 ( %fb9 - %nb9 ) * %disn / %ddist + %nb9 - 50
    evaluate to cut12 %cut11 + %len9 + 100
    endif
    if %fb9 LE %nb9
    setbb lent 0
    endif
  elseif $BANDP EQ 10 
    setbb lent %len10
    if %lent GT 0
    bp co .030 .040 n 4 p 2
    setbb SUFIX '29' 
    evaluate to cut11 ( %fb10 - %nb10 ) * %disn / %ddist + %nb10 - 50
    evaluate to cut12 %cut11 + %len10 + 100
    endif
    if %fb10 LE %nb10
    setbb lent 0
    endif
  elseif $BANDP EQ 11 
    setbb lent %len11
    if %lent GT 0
    bp co .035 .045 n 4 p 2
    setbb SUFIX '25' 
    evaluate to cut11 ( %fb11 - %nb11 ) * %disn / %ddist + %nb11 - 50
    evaluate to cut12 %cut11 + %len11 + 100
    endif
    if %fb11 LE %nb11
    setbb lent 0
    endif
  elseif $BANDP EQ 12 
    setbb lent %len12
    if %lent GT 0
    bp co .040 .050 n 4 p 2
    setbb SUFIX '22' 
    evaluate to cut11 ( %fb12 - %nb12 ) * %disn / %ddist + %nb12 - 50
    evaluate to cut12 %cut11 + %len12 + 100
    endif
    if %fb11 LE %nb12
    setbb lent 0
    endif
  elseif $BANDP EQ 13 
    setbb lent %len13
    if %lent GT 0
    bp co .045 .055 n 4 p 2
    setbb SUFIX '20' 
    evaluate to cut11 ( %fb13 - %nb13 ) * %disn / %ddist + %nb13 - 50
    evaluate to cut12 %cut11 + %len13 + 100
    endif
    if %fb11 LE %nb13
    setbb lent 0
    endif
  endif 

  if %lent GT 0
    w  tempfile
    evaluate to totlen %cut12 - %cut11 
    evaluate to tprfrac 50 / %totlen
     cut %cut11 %cut12
     r tempfile
     taper w %tprfrac
     w temp2
    cuterr fillz
    cut %ctb1 %ctb2
    r temp2
    echo on
     w "./data/%fn1%%SUFIX%"
    echo off
    cuterr u
  endif

 enddo
*enddo
enddo

*cd ../

*enddo
