*sbr comint: determines optimum common interval
*
*  programmed apr 1986 by
*
*     richard  l  holmes
*     tree-ring laboratory
*     university of arizona
*     tucson, arizona  85721  usa
*
*  modified by r l holmes 22 aug 1988
*  modified by pjk 2/2006
*
      subroutine comint(mxs,iyr,id,n,ns,ici,lci)
      integer mxs,ns,ici,lci
      integer n(mxs),iyr(mxs)
      character*8 id(mxs)
c
c local decs
	integer mxs2
	parameter (mxs2=10000)   !must = mxs
       integer l20(mxs2,20)
	character sqc(mxs2)*40,sa*40,sb*40
c
*
      if(ns.gt.mxs2)goto 100
      ifl=0
c
      icy= 32000
      lcy=-32000
*
* here is where you would dump your 
* file data into the character array sqc 
      do 10 m=1,ns
          lyr=iyr(m)+n(m)-1
          icy=min(iyr(m),icy)
          lcy=max(lyr,lcy)
          iz=9000-iyr(m)
c          write(sqc(ns),'(5i5,2x,a8,5x)')lyr,iz,n(m),iyr(m),ns,id(m)
          write(sqc(m),'(5i5,2x,a8,5x)')lyr,iz,n(m),iyr(m),ns,id(m)
c          write(*,'(5i5,2x,a8,5x)')lyr,iz,n(m),iyr(m),ns,id(m)
c          pause '5'
10 	continue
c
       do 20 i=1,ns
          sa=sqc(i)
          sb=sqc(i)
          k=i
          do 30 j=i,ns
             if(sqc(j) .gt. sa)then
                sa=sqc(j)
                k=j
             endif
   30     continue
          sqc(k)=sb
20     sqc(i)=sa
*
* make array of starting and ending years of series
       nly=1
c            pause'1'
       read(sqc(1),'(i5,t1,i5,t16,i5)')l20(1,3),l20(1,2),l20(1,1)
       lly=l20(nly,3)
       do 60 i=2,ns
          read(sqc(i),'(i5,t1,i5,t16,i5)',err=1000)l20(mxs,3),l20(i,2),l20(i,1)
          if(lly .ne. l20(mxs,3))then
             nly=nly+1
             l20(nly,3)=l20(mxs,3)
             lly=l20(nly,3)
          endif
60     continue
       nrci=0
       do 40 l=1,nly
          do 50 i=1,ns
             nc=0
             ny=l20(l,3)-l20(i,1)+1
             do 70 is=1,ns
                 if(l20(is,1) .le. l20(i,1) .and. l20(is,2) .ge. l20(l,3))then
                    nc=nc+1
                 endif
70           continue
             nyc=nc*ny
             if(nyc .gt. nrci)then
                 nrci=nyc
                 ici=l20(i,1)
                 lci=l20(l,3)
                 nci=ny
                 nsci=nc
             endif
50        continue
40     continue
*
* print results on screen
       write(*,'(/2x,42(''*''))')
       write(*,'(1x,'' **  Time span of chronology: '',i5,'' to'',i5)')
     +        icy,lcy
       write(*,'(1x,'' **  Number of series in chronology: '',i6)')ns
       write(*,'(1x,'' **  Optimum common interval: '',i5,'' to'',i5)')
     +        ici,lci
       write(*,'(1x,'' **  Number of years included: '',i12)')nci
       write(*,'(1x,'' **  Number of Series included:       '',i5,/,
     + 1x,'' **  Number of rings:               '',i7)')       nsci,nrci
       write(*,'(2x,42(''*''))')
       return
100    write(*,'('' >> file contains more than program limit of'',i4,
     +'' series'')')mxs2-1
       stop' too many series for subroutine Commint'
1000   write(*,*)sqc(i)
	write(*,'(1x,''error reading sqc)'')')
	stop
       end
c
