        program ercoxkm

        implicit double precision(a-h, o-z)
        parameter (kmax=70, nbmax=15, nqmax=5, nmax=75000)

        dimension x(kmax,nbmax), a(kmax,kmax), f(nqmax,kmax,kmax)
        dimension biggam(nqmax,nqmax), kv(nmax), del(nmax)
        dimension t0(nmax), t(nmax)
        dimension cnt(nmax), ilow(nmax), iup(nmax)
        dimension beta(nbmax), v(nbmax,nbmax)

        common /data0/ n, k, nb, nq
        common /data1/ kv, del, t0, t
        common /data2/ cnt, ilow, iup
        common /data3/ x, a, f, biggam
        common /data4/ beta, v, ifail

        open(1,file='cntlr.crd',status='old')
        open(2,file='input.dat',status='old')
        open(3,file='donna.out',status='unknown',access='append')

C       READ IN PROBLEM DIMENSIONS AND AUXILLIARY INPUTS
        read(1,*) n, k, nb, nq
        read(1,*) iopt, iropt, icpr, zval
        do l = 1, k
          read(1,*) (x(l,j), j = 1, nb)
         enddo
        do ir = 1, k
          read(1,*) (a(ir,is), is = 1, k)
         enddo
        do iq = 1, nq
        do ir = 1, k
          read(1,*) (f(iq,ir,is), is = 1, k)
         enddo
         enddo
        do iu = 1, nq
          read(1,*) (biggam(iu,iv), iv = 1, nq)
         enddo

C       READ IN INPUT DATA: CONFIG LABEL, SURV/CENS STATUS, F-U TIME
        do i = 1, n
          read(2,*) kv(i), del(i), t0(i), t(i)
         enddo

C       CALL SUBROUTINE TO RUN THE METHOD
        call erckcmp(iopt,iropt,zval,nref1,nref2)

C       PRINT OUT RESULTS

        write(3,*) 'COX MODEL WITH MISCLASSIFIED DISCRETE COVARIATES'
        write(3,*)
        if (iopt .eq. 0)
     $    then
            write(3,*) 'Non-iterative method'
          else
            write(3,*) 'Iterative method'
          endif
        write(3,*)
        if (ifail .eq. 1) then
          write (3,*) 'Method failed'
          write(3,*)
         endif
        write(3,*) 'SD buffer factor for refinement = ', zval
        write(3,*) 'Upper bound flag = ', iropt
        write(3,*) 
     $    'Number of timepoints where some estimated S exceeded ',
     $    'upper cutoff was ', nref1
        write(3,*) 
     $    'Number of timepoints where some estimated S went below ',
     $    'lower cutoff was ', nref2
        write(3,*)
        write(3,*) 'BETA VECTOR AND STANDARD ERRORS'
        write(3,*)
        do ir = 1, nb
          sd = dsqrt(v(ir+1,ir+1))
          write (3,200) ir, beta(ir+1), sd
  200     format(i4, 2F10.4)
         enddo
        write(3,*)
        write(3,*)
        if (icpr .eq. 1) then
          write(3,*) 'COVARIANCE MATRIX'
          write(3,*)
          do ir = 1, nb
          do is = 1, nb
            write(3,250) ir, is, v(ir+1,is+1)
  250       format(2i4, F10.4)
           enddo
           enddo
         endif

        end

        include 'donsubs3.f'

