module OW_steer_parms
use sep
use model_types
use helix
use OW_type
use OW_parms_areal
use filters_mod
implicit none 
!FILTER PARAMETERS
  type(filter),public,save                      :: filt
  type (model_param_type)                       :: model_param
  integer,public,save                           :: lap_lags,steer_lags,steerniter
  real,public,save                              :: tri_width,lap_factor,steer_factor,epsflt,epspoly
  real,allocatable,private                      :: angles(:),tangle(:,:)

contains

!----------------------------------------------------------
subroutine owsteer_parms()

integer                          :: i1,i2

 call from_param('steerniter',steerniter,15)
 call from_param('epspoly',epspoly,100.)
 call from_param('ndim_mod',model_param%ndim_mod,1)
 call from_aux("dipfile","n2",model_param%nx_mod)
 call from_aux("dipfile","n1",model_param%nz_mod)
 call from_aux("dipfile","n3",model_param%ny_mod ,1)
 call from_aux("dipfile","o2",model_param%x0_mod)
 call from_aux("dipfile","o1",model_param%z0_mod)
 call from_aux("dipfile","d2",model_param%dx_mod)
 call from_aux("dipfile","d1",model_param%dz_mod)

 if (model_param%nx_mod.ne.img%xm%n) call seperr("Nx dip file .ne. nx image space")
 if (model_param%nz_mod.ne.img%z%n) call seperr("Nz dip file .ne. nz image space")
! if (model_param%ny_mod.ne.img%ym%n) call seperr("Nx dip file .ne. nx image space")
 if (model_param%dx_mod.ne.img%xm%d) call seperr("Dx dip file .ne. dx image space")
 if (model_param%dz_mod.ne.img%z%d) call seperr("Dz dip file .ne. dz image space")
! if (model_param%dy_mod.ne.img%ym%d) call seperr("Dy dip file .ne. dy image space")
 if (model_param%x0_mod.ne.img%xm%o) call seperr("Ox dip file .ne. ox image space")
 if (model_param%z0_mod.ne.img%z%o) call seperr("Oz dip file .ne. oz image space")
! if (model_param%y0_mod.ne.img%ym%o) call seperr("Oy dip file .ne. oy image space")

end subroutine
!----------------------------------------------------------
subroutine owfilter_init()

integer                           :: tempi,temp2i,i1,i2
!##################
!CONSTRUCT FILTERS#
!##################
 call from_param('lap_lags',lap_lags,0)
 call from_param('steer_lags',steer_lags,0)
 call from_param('tri_width',tri_width,2.5)
 call from_param('lap_factor',lap_factor,0.)
 call from_param('steer_factor',steer_factor,.999)
 call from_param('epsflt',epsflt,1.)
 write(0,*) "width lap_factor lap_lags",tri_width,lap_factor,lap_lags
 write(0,*) "steerlags steer_factor eps",steer_lags,steer_factor,epsflt
 call init_filters(lap_scale=lap_factor,steer_scale=steer_factor&
    &,lap_lags=lap_lags,steer_lags=steer_lags,triangle_width=tri_width&
    &,run_triangle=.true.)
!############
!Angles     #
!############
 call from_aux("dipfile","n2",temp2i)
 call from_aux("dipfile","n1",tempi)
 model_param%nz_mod=model_param%nz_mod+frame*2
 if (tempi+frame*2.ne.model_param%nz_mod .or. temp2i.ne.model_param%nx_mod) then
      write(0,*) "Angles",tempi,temp2i
      write(0,*) "model",model_param%nz_mod,model_param%nx_mod
      call erexit("angles not the same size as model")
 end if
 allocate(angles((tempi+frame*2)*temp2i),tangle(tempi,temp2i))
 call sreed('dipfile',tangle,tempi*temp2i*4)
 do i2=1,temp2i
   do i1=1,tempi
     angles(i1+frame+(i2-1)*(2*frame+tempi))=tangle(i1,i2)
   end do
   do i1=1,frame
     angles(i1+(i2-1)*(2*frame+tempi))=angles(frame+1+(i2-1)*(2*frame+tempi))
   end do
   do i1=1,frame
      angles(i1+frame+tempi+(i2-1)*(2*frame+tempi))= angles(frame+tempi+(i2-1)*(2*frame+tempi))
   end do
 end do
 filt=new_steering((/model_param%nz_mod,model_param%nx_mod/),angles,1)
 deallocate(angles,tangle)
end subroutine
!--------------------------------------------------------------
end module
