CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
       program correlation
C
C  looks at correlation function from MC
C
C  CWJ - SDSU - Sept 2006
C

       implicit none

       integer corrsize  ! max correlation "length" 
       parameter(corrsize = 100)

       real corr(0:100)
       real temp(0:100)

       integer npts     ! number of points taken
       real avg

C.......... file control..................

       character filename*15
       integer ilast

C.......... MISC...............

       integer i,j 
       real x
      
  100 continue
       print*,' Enter full name of file with MC data '
       read*,filename
       ilast=index(filename,' ') -1
    
       open(unit=1,file=filename(1:ilast),status='old',err=101)
       goto 102   ! bit of spaghetti code, sorry
  101 continue
       print*,' That file does not exist '
       goto 100

  102 continue    ! whew, end of spaghetti code

       npts  = 0
       avg = 0.0
       do i = 1, 100000
          read(1,*,end=201)x
          avg = avg+x
          npts = npts+1
       enddo
       print*,' should not have gotten here '
       stop
  201 continue
       print*,' there are ',npts,' samples '
       avg = avg/float(npts)
       rewind(1)

       do i = 0,corrsize
          corr(i) = 0.0
       enddo

       do i = 0,corrsize-1
         read(1,*)x
         temp(corrsize-i) = x
       enddo

C................. NOW FOR THE MAIN ALGORITHM.........

       rewind(1)
       do i = 1,npts-corrsize
         read(1,*)x
         
         corr(0)=corr(0)+x*x
         do j = 1,corrsize
           corr(j) = corr(j)+x*temp(j)
     
         enddo
C............. now shift everything down

         do j = corrsize,2,-1
           temp(j) = temp(j-1)
         enddo
         temp(1) = x
       enddo

C............. compute averages

       
       do i = 0,corrsize
         corr(i) = corr(i)/float(npts-corrsize)-avg**2
         write(77,*)i,corr(i)
       enddo


       end

