module OW_picksh

use sep
use OW_type
use OW_parms_areal

implicit none


integer, public, save, allocatable				:: picks(:,:,:)
integer, public, save					:: wavz,nhorz,npick

contains

subroutine owpick()

character(len=128) 						:: hor1, hor2, hor3, hor4
integer, allocatable						:: tempick(:,:,:)
integer,pointer,dimension(:,:)					:: pick1,pick2,pick3,pick4
integer							:: i,m,n,j,minm,maxm,k
integer,allocatable						:: minx(:),maxx(:)

 nhorz=0
 if (exist_file("hor1")) then
    nhorz=1
    call from_param('hor1',hor1)
    open(51,file=hor1,status='old')
    allocate(pick1(sog%xm%n,2));pick1=0
    read(51,*,end=1) ((pick1(n,m), m=1,2), n=1,sog%xm%n)
1   close(51)
 end if	

 if (exist_file("hor2")) then
    nhorz=nhorz+1
    call from_param('hor2',hor2)
    open(51,file=hor2,status='old')
    allocate(pick2(sog%xm%n,2));pick2=0
    read(51,*,end=2) ((pick2(n,m), m=1,2), n=1,sog%xm%n)
2   close(51)
 end if	

 if (exist_file("hor3")) then
    nhorz=nhorz+1
    call from_param('hor3',hor3)
    open(51,file=hor3,status='old')
    allocate(pick3(sog%xm%n,2));pick3=0
    read(51,*,end=3) ((pick3(n,m), m=1,2), n=1,sog%xm%n)
3   close(51)
 end if	

 if (exist_file("hor4")) then
    nhorz=nhorz+1
    call from_param('hor4',hor4)
    open(51,file=hor4,status='old')
    allocate(pick4(sog%xm%n,2));pick4=0
    read(51,*,end=4) ((pick4(n,m), m=1,2), n=1,sog%xm%n)
4   close(51)
 end if	

!--------------------------------------------------
!
! "Arranging picks"
!
if (nhorz .ne. 0) then
call from_param("wavz",wavz,7)
allocate(tempick(nhorz,sog%xm%n,sog%sh%n))
tempick=0
do i=1,nhorz
   if (i .eq. 1) then
   do j=1,sog%sh%n
      do m=1,sog%xm%n
         tempick(i,m,j)=int((0.03/178*(m-1)+0.05)*(j-9)**2+0.05*(j-9))+pick1(m,1)+320
      end do
   end do
   deallocate(pick1)
   end if

   if (i .eq. 2) then
   do j=1,sog%sh%n
      do m=1,sog%xm%n
         tempick(i,m,j)=int(0.074*(j-9)**2+0.003*(j-9))+pick2(m,1)+320
      end do
   end do
   deallocate(pick2)
   end if

   if (i .eq. 3) then
   do j=1,sog%sh%n
      do m=1,sog%xm%n
         tempick(i,m,j)=int(0.06*(j-9)**2+0.055*(j-9))+pick3(m,1)+320
      end do
   end do
   deallocate(pick3)
   end if

   if (i .eq. 4) then
   do j=1,sog%sh%n
      do m=1,sog%xm%n
         tempick(i,m,j)=int(0.06*(j-9)**2+0.055*(j-9))+pick4(m,1)+310
      end do
   end do
   deallocate(pick4)
   end if
end do

allocate(picks(nhorz,sog%xm%n,sog%sh%n))
picks=tempick

do i=1,nhorz
do m=1,sog%xm%n
do j=1,sog%sh%n
write(2,*) i,m,j,picks(i,m,j)
end do
end do
end do

deallocate(tempick)

end if
end subroutine
end module
