# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
!<
!Pick_rms
!
!Usage:
!Pick_rms.x <in.H >out.H
!
!Input Parameters:
! t0 - real First time to find maximum semblance at
! t1 -real Last time to find maximum semblance at
! so_b-real s0 slowness at s0 (begining of the fairway)
! so_e-real s0 slowness at s0 (end of the fairway)
! a_b -real change in fairway slowness /per second
! a_e -real change in fairway slowness /per second
! b_b -real change in fairway slowness sqrt(/per second)
! b_e -real change in fairway slowness sqrt(/per second)
! weight-sepfile file containing weights
! anchor-sepfile file containing picked anchor points (t,v) from Rickmovie
! initial_slow-real velocity to fill in before t0
! final_slow-real velocity to fill in after t1
! small -real weight to give slowness picks before t0 or after t1
!
!Output Parameters:
!
!
!
!Description:
!
! Picks maximum semblance or stackpower within a fairway
!
!>
!-------------------------------------------------
!
!Author: Robert Clapp, ESMB 463, 7230253
!
!Date Created:Wed Oct 22 23:03:43 PDT 1997
!
!Purpose:
!
program Pick_rms
implicit none
  integer n1,n2,f1 ,i1,i(1),n1to,i3,n3,bi,ei
! integer nanchor
  real, allocatable :: input(:,:),output(:),weight(:)
! real, allocatable :: anchor(:,:)
  real o2,d2,s0_b,s0_e,a_b,a_e,b_b,b_e,time,o1,d1,o3,d3,small,initial_slow&
    &,final_slow,t0,t1, large
  integer getch,hetch,putch,auxputch
  call initpar()
  call doc('/homes/sep/alejandro/GEE/Lab4/Pick_rms.rs90')
    if (0>= hetch('n1','d',n1 )) then
      call erexit('Trouble reading  n1 from history  ')
    end if
    if (0>= hetch('n2','d',n2 )) then
      call erexit('Trouble reading  n2 from history  ')
    end if
    if (0>= hetch('n3','d',n3 )) then
      call erexit('Trouble reading  n3 from history  ')
    end if
    if (0>= hetch('o2','f',o2 )) then
      call erexit('Trouble reading  o2 from history  ')
    end if
    if (0>= hetch('d2','f',d2 )) then
      call erexit('Trouble reading  d2 from history  ')
    end if
    if (0>= hetch('o1','f',o1 )) then
      call erexit('Trouble reading  o1 from history  ')
    end if
    if (0>= hetch('d1','f',d1 )) then
      call erexit('Trouble reading  d1 from history  ')
    end if
    if (0>= hetch('o3','f',o3 )) then
      call erexit('Trouble reading  o3 from history  ')
    end if
    if (0>= hetch('d3','f',d3 )) then
      call erexit('Trouble reading  d3 from history  ')
    end if
    if (0>= getch('t0','f',t0 )) then
      call erexit('Trouble reading  t0 from param  ')
    end if
    if (0.ne.putch('Read  from param: #t0 ','f',t0)) then
      call erexit('Trouble writing t0 to history')
    end if
    if (0>= getch('t1','f',t1 )) then
      call erexit('Trouble reading  t1 from param  ')
    end if
    if (0.ne.putch('Read  from param: #t1 ','f',t1)) then
      call erexit('Trouble writing t1 to history')
    end if
    if (0>= getch('s0_b','f',s0_b )) then
      call erexit('Trouble reading  s0_b from param  ')
    end if
    if (0.ne.putch('Read  from param: #s0_b ','f',s0_b)) then
      call erexit('Trouble writing s0_b to history')
    end if
    if (0>= getch('s0_e','f',s0_e )) then
      call erexit('Trouble reading  s0_e from param  ')
    end if
    if (0.ne.putch('Read  from param: #s0_e ','f',s0_e)) then
      call erexit('Trouble writing s0_e to history')
    end if
    if (0>= getch('a_b','f',a_b )) then
      call erexit('Trouble reading  a_b from param  ')
    end if
    if (0.ne.putch('Read  from param: #a_b ','f',a_b)) then
      call erexit('Trouble writing a_b to history')
    end if
    if (0>= getch('a_e','f',a_e )) then
      call erexit('Trouble reading  a_e from param  ')
    end if
    if (0.ne.putch('Read  from param: #a_e ','f',a_e)) then
      call erexit('Trouble writing a_e to history')
    end if
    if (0>= getch('b_b','f',b_b )) then
      call erexit('Trouble reading  b_b from param  ')
    end if
    if (0.ne.putch('Read  from param: #b_b ','f',b_b)) then
      call erexit('Trouble writing b_b to history')
    end if
    if (0>= getch('b_e','f',b_e )) then
      call erexit('Trouble reading  b_e from param  ')
    end if
    if (0.ne.putch('Read  from param: #b_e ','f',b_e)) then
      call erexit('Trouble writing b_e to history')
    end if
    if (0>= getch('small','f',small )) then
      small = .05
    end if
    if (0.ne.putch('Read  from param: #small ','f',small)) then
      call erexit('Trouble writing small to history')
    end if
    if (0>= getch('large','f',large )) then
      large = 20
    end if
    if (0.ne.putch('Read  from param: #large ','f',large)) then
      call erexit('Trouble writing large to history')
    end if
    if (0>= getch('initial_slow','f',initial_slow )) then
      call erexit('Trouble reading  initial_slow from param  ')
    end if
    if (0.ne.putch('Read  from param: #initial_slow ','f',initial_slow&
      &)) then
      call erexit('Trouble writing initial_slow to history')
    end if
    if (0>= getch('final_slow','f',final_slow )) then
      call erexit('Trouble reading  final_slow from param  ')
    end if
    if (0.ne.putch('Read  from param: #final_slow ','f',final_slow))&
      & then
      call erexit('Trouble writing final_slow to history')
    end if
! from aux: anchor integer n1:nanchor
! if( nanchor > 0) {
! allocate( anchor(nanchor,2) )
! call sreed("anchor",anchor,nanchor*2*4 )
! }
  allocate(input(n1,n2),output(n1),weight(n1))
    if (0.ne. putch('n2','d',n3 )) then
      call erexit('Trouble writing  n2 to history  ')
    end if
    if (0.ne. putch('n3','d',1 )) then
      call erexit('Trouble writing  n3 to history  ')
    end if
    if (0.ne. putch('o2','f',o3 )) then
      call erexit('Trouble writing  o2 to history  ')
    end if
    if (0.ne. putch('d2','f',d3 )) then
      call erexit('Trouble writing  d2 to history  ')
    end if
    if (0.ne. auxputch('n1','d',n1 ,'weight')) then
      call erexit('Trouble writing  n1 to aux weight ')
    end if
    if (0.ne. auxputch('n2','d',n3 ,'weight')) then
      call erexit('Trouble writing  n2 to aux weight ')
    end if
    if (0.ne. auxputch('d1','f',d1 ,'weight')) then
      call erexit('Trouble writing  d1 to aux weight ')
    end if
    if (0.ne. auxputch('d2','f',d2 ,'weight')) then
      call erexit('Trouble writing  d2 to aux weight ')
    end if
    if (0.ne. auxputch('o1','f',o1 ,'weight')) then
      call erexit('Trouble writing  o1 to aux weight ')
    end if
  weight = 0
  do i3=1,n3
    call sreed("in",input,n1*n2*4)
    f1 = max(1,nint((t0-o1)/d1) )
    do i1=1,f1
      weight(i1)=small
      output(i1)=initial_slow
    end do
    f1 = max(2,1+nint((t0-o1)/d1) )
    do i1=f1,n1
      time = o1 + d1*(i1-1)
      bi = max( 1, nint( ( s0_b + a_b*time + b_b*sqrt(time) - o2)/d2 &
        &) )
      ei = min( n2, nint( ( s0_e + a_e*time + b_e*sqrt(time) - o2)/d2 &
        &) )
      weight(i1) = maxval( input(i1,bi:ei) )
      i = maxloc( input(i1,bi:ei) ) + bi - 1
      output(i1) = ( i(1) - 1 )*d2 + o2
    end do
    f1 = min(n1-1,nint((t1-o1)/d1))
    do i1=f1+1,n1
      weight(i1) = small
      output(i1) = final_slow
    end do
! if( nanchor > 0) {
! weight = 0
! do i1=1,nanchor {
! f1 = nint( (anchor(i1,1)-o1)/d1 )
! output( f1) = anchor(i1,2)
! weight( f1) = large
! }
! }
! weight( 1) = large # Anchor first point
! weight( n1) = large # Anchor last point
    call srite("out",output,n1*4)
    call srite("weight",weight,n1*4)
  end do
end program
