program FreqBin

  use sep
  implicit none  
  integer :: i3,j3,i4,i5,a
  integer :: n1,n2,n3,n4,n5,esize,jw
  real    :: o1,o2,o3,d1,d2,d3
  complex, allocatable,dimension(:,:,:)  :: data
  complex, allocatable,dimension(:,:)    :: temp

  call sep_init()
  call from_history('esize',esize,4)
  if (esize.eq.4) call seperr("wrong!")
  call from_history('n1',n1);call from_history('n2',n2);call from_history('n3',n3)
  call from_history('o1',o1);call from_history('o2',o2);call from_history('o3',o3)
  call from_history('d1',d1);call from_history('d2',d2);call from_history('d3',d3)
  call from_history('n4',n4);call from_history('n5',n5)
  call from_param('jw',jw,8)
  call to_history('d3',d3*jw);
  call to_history('n3',n3/jw);call to_history('o3',o3+d3*jw/2)
  call sep_close()
  allocate(data(n1,n2,n3))
  allocate(temp(n1,n2))
  if(n1.gt.n3) call seperr("NO!")
  do i5=1,n5
     do i4=1,n4
        call sep_read(data)
        do i3=1,n3,jw
           a=i3+jw-1
           temp=(0.0,0.0)
           do j3=i3,a
              temp(:,:)=temp(:,:)+data(:,:,j3)
           end do
           call srite('out',temp,8*n1*n2*1)
        end do
     end do
  end do
end program FreqBin
