c
c
c***********************************************************************
c
      subroutine POSTPR ( n, kg, m, nkg2, rnorm, iprint, alf, w,
c
     *                    a, r, u )
c
c----------------------------------------------------------------------
c
c     calculate residuals, sample variance, and
c     covariance matrix.  on input, u contains
c     information about householder reflections from dpa.
c     on output, it contains the linear parameters.
c
c
      PARAMETER ( mmax = 3000, maxn = 100, maxkg = 50 )
c
      real a(mmax,nkg2),alf(kg),r(m),u(n),w(m)
      integer output
      logical ip
      real bm(mmax,maxn+maxkg)
      common /blkbm/ bm
      common /blokip/ ip
      data output /6/
c
      np1=n+1
      npkg=nkg2-2
      nkg1=npkg+1
c
c           unwind householder transformations to get
c           residuals and move the linear parameters
c           from r to u.
c
      if (n.eq.0) go to 1390
      do 1370 kback=1,n
         k=np1-kback
         kp1=k+1
         acum=0.
         do 1360 i=kp1,m
1360        acum=acum+a(i,k)*r(i)
         save1=r(k) 
         r(k)=acum/a(k,k)
         acum=-acum/(u(k)*a(k,k))
         u(k)=save1 
         do 1370 i=kp1,m
1370        r(i)=r(i)-a(i,k)*acum
      if (kg.eq.0) go to 1410 
      do 1380 j=np1,npkg
         jp1=j+1
         do 1380 i=1,m
1380        bm(i,j)=a(i,jp1)/w(i)
c
c           the first n columns of the matrix have been
c           reduced to upper triangular form in dpa.
c           finish by reducing rows n+1 to m and columns
c           n+2 through n+kg+1 to triangular form.  then
c           shift columns of derivative matrix over one
c           to the left to be adjacent to the first n
c           columns.
c
c    ------------------------------------------------------
1390  call ORFAC1 (kg+1,mmax,m,n,iprint,a(1,n+2),prjres,4)
c    ------------------------------------------------------
      do 1400 i=1,m 
         a(i,nkg2)=r(i)
         do 1400 k=np1,nkg1
1400        a(i,k)=a(i,k+1)
c                           compute covariance matrix
1410  a(1,nkg2)=rnorm
      acum=rnorm*rnorm/(m-n-kg)
      a(2,nkg2)=acum
c    -----------------------------
      call COV (mmax,npkg,acum,a)
c    -----------------------------
c
      if (iprint.lt.0) go to 1420
      write (output,1430)
      if (ip) write (10,1430) 
      if (n.gt.0) write (output,1440) (u(j),j=1,n)
      if ((n.gt.0).and.(ip)) write (10,1440) (u(j),j=1,n)
      if (kg.gt.0) write (output,1450) (alf(k),k=1,kg)
      if ((kg.gt.0).and.(ip)) write (10,1450) (alf(k),k=1,kg)
      write (output,1460) rnorm,acum
      if (ip) write (10,1460) rnorm,acum
      write (output,1430)
      if (ip) write (10,1430) 
1420  return
c
1430  format (1h /1h ,59(1h'))
1440  format (1h /20h   linear parameters//(4e15.7))
1450  format (1h /23h   nonlinear parameters//(4e15.7))
1460  format (1h /30h   weighted norm of residual =,e15.7/1h /
     2        32h   weighted estimated variance =,e15.7)
c
      end 
