      subroutine meancyclegapfilling(nsamp,xvar,yvar0,nminno0)
      implicit none
!Fill gaps in yvar with the mean cycle approach. xvar must be repeated cycles.
      integer nsamp,nminno0,nk,nminno
      double precision xvar(nsamp),yvar0(nsamp)
      integer i,j,k,m,n
      double precision yvector(nsamp),fn9999,tiny,yvar(nsamp)
      parameter(fn9999=-9999.0d0,tiny=1.0d-7)
!
      do i=1,nsamp
        yvar(i)=yvar0(i)
      enddo
      do i=1,nsamp
        nminno=nminno0
        if(dabs(yvar(i)-fn9999).gt.tiny)goto 70
7       n=0
        k=i-1
        nk=0
10      if(k.eq.0)goto 30
        if(dabs(xvar(k)-xvar(i)).gt.tiny)goto 20
        nk=nk+1
        if(dabs(yvar0(k)-fn9999).lt.tiny)goto 20
        n=n+1
        yvector(n)=yvar0(k)
        if(nk.ge.nminno/2)goto 30
20      k=k-1
        goto 10
30      m=i+1
        nk=0
40      if(m.gt.nsamp)goto 60
        if(dabs(xvar(m)-xvar(i)).gt.tiny)goto 50
        nk=nk+1
        if(dabs(yvar0(m)-fn9999).lt.tiny)goto 50
        n=n+1
        yvector(n)=yvar0(m)
        if(nk.ge.nminno/2)goto 60
50      m=m+1
        goto 40
60      if(n.lt.nminno0)then
          nminno=nminno+1
          goto 7
        endif
        yvar(i)=0.0d0
        do m=1,n
          yvar(i)=yvar(i)+yvector(m)
        enddo
        if(n.eq.0)then
          yvar(i)=-9.999d+199
        else
          yvar(i)=yvar(i)/dble(n)
        endif
70      continue
      enddo      
      do i=1,nsamp
        yvar0(i)=yvar(i)
      enddo
      return
      end
!####################################################################
