c programme de minimum glissante
c 
      real delay,jj(65535),lp(65535),val(65535),nb(65535),box,mini
      real jmax, jmin,er(65535),error(65535),valf(65535),nbf(65536)
      real errf(65535),jjf(65535)
      integer ndat,i,j
      open(unit=1,file='minimum.in',status='old')
          read(1,*) box
          read(1,*) ndat
          jmin=100000000.
          jmax=0.
          do i=1,ndat
             read(1,*) jj(i),lp(i),er(i)
             if (jj(i).lt.jmin) jmin=jj(i)
             if (jj(i).gt.jmax) jmax=jj(i)
          enddo
      close(unit=1)
      do i=int(jmin),int(jmax)
         val(i)=0.
         nb(i)=0.
         mini=1000000000.
         do j=1,ndat
            delay=abs(jj(j)-real(i))
            if (delay.le.box/2.) then
               nb(i)=nb(i)+1.
               if (lp(j).lt.mini) then
               
               mini=lp(j)
               error(i)=er(j)
               endif
            endif
         enddo
         if (nb(i).gt.real(box)) then
            val(i)=mini
         else
            val(i)=-1.
            error(i)=0.
         endif
         if (val(i).gt.65535.) val(i)=-1.
       enddo
c regrouper avec le centre de masse







      open(unit=1,file='minimum.out',status='unknown')
          do i=int(jmin),int(jmax)
             if (val(i).ge.0.) write(1,*) i,val(i),error(i)
c             if (valf(i).ge.0.) write(1,*) jjf(i),valf(i),errf(i)
          enddo
      close(unit=1)
      stop
      end

