C  Max globals increased to 10, 03/09/2009
C  Changed 27/01/2012 to include plot ID in parameters file:
C  now ID is read by GET instead of RDATA (before TRPARS is called)
      IMPLICIT LOGICAL (A-Z)
C     (FOR TRAPPING MISSPELT VARIABLES)
      INTEGER MFILE,PFILE,DFILE,RFILE,M0,MK,M0K,LT0,LTK,DKLEN,NPLOTS,
     1   MFREQ,ITMAX,I,TITLE(20),NPD
      DOUBLE PRECISION T0(10),TOL,L00(55)
C -----  New for EasySDE -----
      integer model
      integer*4 plotID
      double precision tScale, hScale
      character*80 file
      common /ESDE/ tScale, hScale, plotID, model
C -------------------------
      COMMON /FILES/MFILE,PFILE,DFILE,RFILE /SIZES/M0,MK,M0K,LT0,LTK,
     1   DKLEN,NPLOTS
    1 FORMAT(20A4)
    2 FORMAT(1H1,20A4//19H GLOBAL PARAMETERS:,I2/18H LOCAL PARAMETERS:,
     1   I2/17H NUMBER OF PLOTS:,I4/22H MONITORING FREQUENCY:,I3/
     2   17H ITERATION LIMIT:,I3/11H TOLERANCE:,G9.1//20X,
     3   14HMONITOR OUTPUT)
    3 FORMAT(1H ,20A4/)
C ------  New for EasySDE  ------
      call getarg(1,file)
      open (1,file=file,status='OLD')
C ----------------------------
      READ(1,1) TITLE
      READ(1,*) M0,MK,NPLOTS,DFILE,PFILE,MFILE,RFILE,MFREQ,ITMAX,TOL,
     1   (T0(I),I=1,M0)
C ------  New for ESDE  ------
      if (M0 .gt. 10) then
        write (*,*) '*** Error, more than 10 globals ***'
        stop
      endif
      if (MK .gt. 6) then
        write (*,*) '*** Error, more than 6 locals ***'
        stop
      endif
      Read(1,*) model, tScale, hScale
C     Ignore I/O unit numbers in control file:
      DFILE=2
      PFILE=3
      RFILE=4
C     (* substituted for MFILE everywhere)
C     Open files - Modified for EasySDE
      call getarg(2,file)
      open (DFILE,file=file,form='UNFORMATTED',status='OLD')
      call getarg(3,file)
c   Changed 27/01/2012, increasing recl to include plot ID:
      open (PFILE,access='DIRECT',file=file,recl=748,status='OLD')
      call getarg(4,file)
      open (RFILE,file=file)
C     Monitoring (MFILE) to standard output
C ----------------------------
      WRITE(*,2) TITLE,M0,MK,NPLOTS,MFREQ,ITMAX,TOL
      M0K=M0*MK
      LT0=(M0*(M0+1))/2
      LTK=(MK*(MK+1))/2
      DKLEN=(M0+1)*MK
      CALL SETPF
C    IGNORE UNDERFLOWS :
C      CALL ICL9HEMASK(64,I)
      CALL NEWTON(T0,MFREQ,ITMAX,TOL,L00,NPD)
      CALL SAVEPF
      WRITE(RFILE,3) TITLE
      IF(ITMAX.LT.0) WRITE(RFILE,4)
    4 FORMAT(49H0ITERATION LIMIT EXCEEDED. IGNORE STANDARD ERRORS,
     1   23H AND CORRELATIONS BELOW)
      IF(ITMAX.GE.0.AND.NPD.GT.0) WRITE(RFILE,5)
    5 FORMAT(53H0HESSIAN MATRIX NOT POSITIVE-DEFINITE. SADDLE POINT?
     1   /46H IGNORE STANDARD ERRORS AND CORRELATIONS BELOW)
      IF(ITMAX.GE.0.AND.NPD.EQ.0) WRITE(RFILE,6)
    6 FORMAT(10H0CONVERGED)
      CALL REPORT(T0,L00)
      STOP
      END


      SUBROUTINE SETPF
      RETURN
      END

      SUBROUTINE GET(K,TK,DK,HKK)
c   Changed 27/01/2012 to pass plot ID to COMMON
      INTEGER K,M,P,D,R,M0,MK,M0K,LT0,LTK,DKLEN,NPLOTS,model
      DOUBLE PRECISION TK(MK),DK(DKLEN),HKK(LTK),tScale,hScale
      integer*4 iplot,plotID
      common /ESDE/ tScale, hScale, plotID, model
      COMMON /FILES/M,P,D,R /SIZES/M0,MK,M0K,LT0,LTK,DKLEN,NPLOTS
      READ(P,REC=K) iplot,TK,DK,HKK
      plotID = iplot
      RETURN
      END

      SUBROUTINE PUT(K,TK,DK,HKK)
c   Changed 27/01/2012 to write plot ID
      INTEGER K,M,P,D,R,M0,MK,M0K,LT0,LTK,DKLEN,NPLOTS,model
      DOUBLE PRECISION TK(MK),DK(DKLEN),HKK(LTK),tScale,hScale
      integer*4 iplot,plotID
      common /ESDE/ tScale, hScale, plotID, model
      COMMON /FILES/M,P,D,R /SIZES/M0,MK,M0K,LT0,LTK,DKLEN,NPLOTS
      iplot = plotID
      WRITE(P,REC=K) iplot,TK,DK,HKK
      RETURN
      END

      SUBROUTINE SAVEPF
      RETURN
      END

      SUBROUTINE RDATA(K,IPLOT,N,AGE,HT)
C  ---  Modified for EasySDE  ---
      IMPLICIT LOGICAL(A-Z)
      INTEGER K,N,M,P,D,R,I,MODEL
      INTEGER*4 IPLOT,PLOTID
      DOUBLE PRECISION AGE(100),HT(100),TSCALE,HSCALE
      COMMON /FILES/M,P,D,R
      COMMON /ESDE/TSCALE,HSCALE,PLOTID,MODEL
      IF(K.EQ.1) REWIND D
      READ(D) IPLOT,N
c   28/01/2012 - PLOTID should have been read by GET, paranoia:
      if (PLOTID.ne.IPLOT) then
        write(*,*) '*** RDATA: Bad plot ID, should not happen  ***'
        stop
      endif
      READ(D) (AGE(I),HT(I), I=1,N)
      RETURN
      END


