c ----  Files on command line: data in, data out, parameters (out),
c            local parameters (in, optional)
c     Warning:  Assumes 8-byte doubles
c     New version, cleaned-up for Fortran Checker 30/08/2009
c     27/01/2012: Changes to include plot ID in the parameters file
c     30/01/2012: Change string/numbers list-directed output to
c        formatted, for compatibility with gfortran compiler
c
      integer i,j,nlocs,ncols,meas,nplots,idcol,jcol(8),eof,minms,
     +        nskip
      integer*4 iplot,last
      double precision x(100),age(100),height(100),guess(93),tScale,
     +                hScale
      character*80 file
      do 1 j=1,39
  1     guess(j) = 0
      call getarg(1,file)
      OPEN (1,FILE=file,STATUS='OLD')
      call getarg(2,file)
      OPEN (3,FILE=file,FORM='UNFORMATTED')
      call getarg(3,file)
c   Changed 27/01/2012, increasing length by 4 to include plot ID:
      OPEN (4,FILE=file,ACCESS='DIRECT',RECL=748)
      Write(*,*)
     + 'Enter number of locals, minimum required measurements per plot,'
      Write(*,*)
     + 'age scale, height scale, columns in parameter file, lines to'
      Write(*,*)
     + 'skip at the start, position of plot ID column:'
      Read(*,*) nlocs,minms,tScale,hScale,ncols,nskip,idcol
      if (nlocs .le. 8) goto 2
      write(*,*) '*** ERROR:  Too many locals'
      stop
   2  if (ncols .le. 50) goto 4
      write(*,*)
     +    '*** ERROR:  More than 50 columns in local parameters file'
      stop
   4  continue
      write(*,*)
      write(*,*)
     + 'Enter positions of each local in parameter file,'
      write(*,*)
     + 'or 0 if given below:'
      Read (*,*) (jcol(j), j=1,nlocs)
      do 5 j=1,nlocs
         if (jcol(j) .le. ncols) goto 5
            write(*,*)
     +      '*** ERROR: Parameter file column index out of range'
            stop
   5     continue
      write(*,*)
      write(*,*)
     + 'Enter initial local estimates (ignored if given in file):'
      Read(*,*) (guess(j),j=1,nlocs)
      write(*,*)
      nplots=0
      meas=0
      eof=0
      iplot=-6391
c  --- Open local parameters file, if any ---
      if (ncols .le. 0) goto 10
      call getarg(4,file)
      OPEN (2,FILE=file,STATUS='OLD')
C  --- Skip header, if any ---
      if (nskip .le. 0) goto 10
 111  Format()
      do 8 j=1,nskip
        Read(2,111,end=400,err=400)
   8    continue
c  --- Main loop  ---
c   30/01/2012: output formats
 511  Format(1X, A, I9, A, I2, A)
 512  Format(1X, A, A, I10, I10)
  10  meas=meas+1
      last=iplot
      Read(1,*,end=200,err=300) iplot,age(meas),height(meas)
      if (meas .eq. 1) goto 10
      if (iplot .ne. last) goto 20
      if (age(meas) .ge. age(meas-1)) goto 15
      write(*,511) '*** ERROR:  Decreasing age in plot ', iplot
      stop
  15  if (meas .lt. 100) goto 10
      write(*,511) '*** ERROR:  More than 99 measurements in plot ',
     +  iplot
      stop
 300  write(*,511) '*** ERROR reading data file at plot ', iplot
      stop
c  ---  EOF  ---
 200  eof=1
c  ---  End of plot ---
  20  continue
c  ---  Get local parameters  ---
      if (ncols .le. 0) goto 22
      Read(2,*,end=400,err=400) (x(j), j=1,ncols)
      if (x(idcol) .eq. last) goto 22
         Write(*,512)
     +      '*** ERROR:  Plot ID mistmatch between data and local',
     +      ' parameters files:', last, x(idcol)
         stop
 400  Write(*,511) '*** ERROR reading local parameters file for', last
      stop
  22  Do 24 j=1,nlocs
         if (jcol(j) .gt. 0) guess(j) = x(jcol(j))
  24     continue
      if (meas .gt. minms) goto 30
         write(*,511) '*** WARNING:  plot ', last, ' has less than',
     +       minms, ' measurements. Ignored.'
         goto 40
c  ---  Write plot data and local parameters  ---
  30  continue
      write(3) last, meas-1
      write(3) (age(i)/tScale, height(i)/hScale, i=1,meas-1)
      nplots=nplots+1
c   Changed 27/01/2012 to include plot ID:
      Write(4,REC=nplots) last, guess
c  ---  Go for next plot ---
  40  continue
      age(1)=age(meas)
      height(1)=height(meas)
      meas=1
      if (eof .eq. 0) goto 10
      Write(*,511) 'Using',nplots,' plots'
      end

