      subroutine meancyclepattern(nsamp,xvar,yvar,ncyc,xcyc,ycyc,ystd)
      implicit none
!Calculate mean cycle pattern. xvar must be repeated cycles.
      integer nsamp,ncyc
      double precision xvar(nsamp),yvar(nsamp),xcyc(nsamp),
     &ycyc(nsamp),ystd(nsamp)
      integer i,j,k,m,n,iorder(nsamp)
      double precision yvector(nsamp),fn9999,tiny
      parameter(fn9999=-9999.0d0,tiny=1.0d-7)
!
      ncyc=1
      xcyc(ncyc)=xvar(1)
      do i=2,nsamp
        k=0
        do j=1,ncyc
          if(dabs(xcyc(j)-xvar(i)).lt.tiny)k=1
        enddo
        if(k.eq.0)then
          ncyc=ncyc+1
          xcyc(ncyc)=xvar(i)
        endif
      enddo
      call sort_shell(ncyc,xcyc,iorder)
      do j=1,ncyc
        k=0
        do i=1,nsamp
          if(dabs(xvar(i)-xcyc(j)).lt.tiny)then
            if(dabs(yvar(i)-fn9999).gt.tiny)then
              k=k+1
              yvector(k)=yvar(i)
            endif
          endif
        enddo
        if(k.gt.2)then
          call stdmean(k,yvector,ystd(j),ycyc(j))
        else
          ycyc(j)=fn9999
          ystd(j)=fn9999
        endif
      enddo
      return
      end
!####################################################################
