implicit none

integer, parameter :: mmax=100000
integer :: argc, m, n
character(len=1024) :: file_in, file_out, dum
real    :: x(mmax), xorig, xmod, eps
integer :: i, j

 argc = iargc()
 if( argc < 7 ) then
  write(0,*) './binconv data_number_x data_number_y file_in file_out orig_num mod_num eps'
  write(0,*)  'if( xorig-eps < x < xorig+eps )   x=xmod'
  stop
 end if

 call getarg(1, dum)
  read(dum, *) m
 call getarg(2, dum)
  read(dum, *) n
 call getarg(3, file_in)
 call getarg(4, file_out)
 call getarg(5, dum)
  read(dum, *) xorig
 call getarg(6, dum)
  read(dum, *) xmod
 call getarg(7, dum)
  read(dum, *) eps

 write(*,*) m,'x',n, ' record data from ', file_in(1:len_trim(file_in)), &
 	' and output to ', file_out(1:len_trim(file_out)), ' with converting ', &
	xorig, ' to ', xmod
 if( m > mmax ) then
   write(0,*) ' too many m', m
   write(0,*) 'Please check program'
   stop
 end if

 open(unit=10, file=file_in, access='direct', form='unformatted', &
    recl = 4*m, err = 900 )
 open(unit=11, file=file_out, access='direct', form='unformatted', &
    recl = 4*m, err = 910 )

 do j=1, n
  read(10, rec=j) (x(i), i=1,m)
  do i=1,m
   if(abs(x(i)-xorig) < eps) then
    x(i) = xmod
   end if
  end do
  write(11, rec=j) (x(i), i=1,m)
 end do

 close(10); close(11)

 stop

900 write(0,*) 'Can not open ', file_in
 stop
910 write(0,*) 'Can not open ', file_out
 stop
end
