program Pick2Panel
  use sep
  implicit none

  integer:: n1,n2,nx,ny,ax,ay,af,ix,iy,i2,good,bad
  real::ox,dx,oy,dy,mx,my,sx,sy,bias
  real,allocatable,dimension(:,:):: data,panel
  logical:: add
  call sep_init()
  call from_history("n1",n1)
  call from_history("n2",n2)

  call from_param("ox",ox,0.)
  call from_param("nx",nx,2100)
  call from_param("sx",sx,15876.21)
  call from_param("oy",oy,-45000.)
  call from_param("ny",ny,2100)
  call from_param("sy",sy,5625.098)
  call from_param("mx",mx,5000.)
  call from_param("my",my,-35000.)
  call from_param("bias",bias,0.)
  dx=(mx-ox)/(nx-1)
  dy=(my-oy)/(ny-1)
write(0,*)' n1,n2',n1,n2
write(0,*)' nx,ny',nx,ny
  call from_param("axis_x",ax,1)
  call from_param("axis_y",ay,2)
  call from_param("axis_f",af,7)
write(0,*)' ax,ay,af',ax,ay,af
  call from_param("add",add,.false.)
  call to_history('n2',ny)
  call to_history('d2',dy)
  call to_history('o2',oy)
  call to_history('n1',nx)
  call to_history('d1',dx)
  call to_history('o1',ox)
  call to_history('esize',4)
  call to_history('label1','x(m)')
  call to_history('label2','y(m)')

  allocate(panel(nx,ny),data(n1,n2)); panel=bias;data=0.
  call sep_read(data)

!  if(add) then
!     call sseek("OUT",0,0)
!     call sep_read(panel,'OUT')
!  end if
  good=0; bad=0
  do i2=1,n2
!  do i2=2000,2100
     ix=1.5+ (data(ax,i2)-sx-ox)/dx
     iy=1.5+ (data(ay,i2)-sy-oy)/dy
     if(ix.lt.nx.and.iy.gt.1.and.iy.lt.ny.and.iy.gt.1) then
        good=good+1
! call seperr('shit x')
!     if(iy.gt.ny.or.iy.lt.1) call seperr('shit y')
!write(0,*)' x,y',data(ax,i2)-sx,data(ay,i2)-sy
!write(0,*) 'ix,iy',ix,iy,10./data(af,i2)
     panel(ix,iy)=panel(ix,iy)+1./data(af,i2)
     else
        bad=bad+1
  end if
  end do


  call sep_write(panel)
write(0,*) 'good',good
write(0,*) 'bad',bad
end program Pick2Panel
