      subroutine rdcfil
c************************** rdcfil ***********************************
c
c  routine to read in a dc file and put the information into the 
c  appropriate arrays.
c
c*********************************************************************
c
c
      include 'ctparm.inc'
      include 'nrbparam.inc'
      include 'dspparam.inc'
      include 'ctgeom.inc'     !curved triangular patch geom.inc
      include 'ctgeom2.inc'
      include 'ctgeom3.inc'
      include 'ctgeomwire.inc'
      include 'ctgeomjunc.inc'
      include 'ctgeom_bic.inc'
      include 'mmiounit.inc'
      include 'symm.inc'
      include 'nurbsar.inc'
      include 'matl.inc'
      include 'surf.inc'
      include 'dsppoly.inc'
      include 'dspwire.inc'
      include 'wirechar.inc'
      include 'eadata.inc'
      include 'eacnst.inc'
      include 'iges114.inc'
      include 'surfent.inc'

      real pi, dtr, elev, azim, roll, razim, relev, rroll
      real caz, saz, sel, rad, ull, vll, rm, ack, v0, shdbnd
      real sruvw, cel
      integer nexobj, nexbic, i, ii, j, k, iii, jj, nobj
      integer nexnrb, nexflt, icwre, numsurffeed, numsurfjunc
      integer itrk, ns, nesurf, nfsurf, ntyp
      integer nn, nl, nr, ikl, ikr, n, ne1, ne2, ne3, n1, n2, n3
      integer kcnt, nobjlnk, iflg1, iflg2, iflg3, k3, ib
      integer iedges, npenshad
      integer ixvc, ivalcol(mxcolor)
      character*8 card,cont
      character mdlttl*60,crec*132,fil*30,mdltyp*8,char*1
      double precision a,b,c
      logical psi_prm,cirgpe
      logical idrawn
      logical wires,loads

      common /curv/ rm, ack, v0(3)
      common /plotting/idrawn(mxedgs),shdbnd(2,mxedgs),iedges(3,mxedgs),
     &                 npenshad,sruvw(3)
      common /objects/ nobj(mxobj+mxwire), nobjlnk(mxfaces), nexobj
      common /geomopt/ wires,loads
      common /nsign/ normsign(mxobj+mxwire)
      common /surf2/ newgrid(mxobj+mxwire), numflatsurf, noldsurf,
     &               numentpsurfdc(mxent+mxwire)
      common /gpeuandv/ ustart(mxent), ustop(mxent), 
     &                  vstart(mxent), vstop(mxent)
      common /gpep/ nboxtyp(mxobj+mxwire), gpeor(3,mxobj)
      common /flat/ nflatplt, nflatnodes, nflatbox(mxobj), nsflatsurf
      common /rder/ ireaderror
      common /wire_junction1/ numdup(mxwire), indxdup(2,mxwire)

      wires = .false.
      loads = .false.

      pi = 2. * acos(0.)
      dtr = pi / 180.

      nexobj = 1
      next   = 1
      nexbic = 1
      nexnrb = 1
      nexflt = 1
      icwre = 0
      numjunctions = 0
      numsurffeed = 0
      numwires = 0
      lstent = 0
      numflatsurf = 0
      nflatnodes = 0
      ixvc = 0
      nflatplt = 0
      ireaderror = 0
      do i = 1,mxent
       ustart(i) = 1e9
       vstart(i) = 1e9
       ustop(i) = -1e9
       vstop(i) = -1e9
      enddo
c
      do j=1,mxobj+mxwire
        nobj(j) = 0
      enddo
c
c--------------------------------------------------------------------
c
      read(ir01,1130,err=800) mdltyp,char,mdlttl ! read model type & title
      if(mdltyp.ne.'BSPLINE ' .and. mdltyp.ne.'BICUBIC ' .and.
     & mdltyp.ne.'SCAMP   ' .and.mdltyp.ne.'        ' ) 
     & call report_error(2005,1,1)
      read(ir01,1030,err=800) nnodes,nedges,nfaces,nsurfaces,nplanes,
     & nummat
c report errors
      if(nnodes.gt.mxnodes) call report_error(4,mxnodes,nnodes)
      if(nedges.gt.mxedgs) call report_error(5,mxedgs,nedges)
      if(nfaces.gt.mxfaces) call report_error(6,mxfaces,nfaces)
c
      if( nplanes .gt. 0) then
        read(ir01,'(8(2x,i6))',err=800) (isymplane(i),i=1,nplanes)
      endif
c
 10   continue                       ! read until begin bulk card
      read(ir01,1040,err=800) card
      if(card.ne.'BEGIN BU') goto 10
c
cdw.. initialize the number of unknowns & boundary edges outside 
cdw   of the surface loop
      nunkns = 0  ! # of unknowns
      itrk = 0    ! # of boundry edges
      do ns=1,nsurfaces       ! read in each surface 

       read(ir01,1040,err=800) card
       do while (card .ne. 'BEGIN SU')
         read(ir01,1040,err=800) card
       enddo
       read(ir01,5000,err=800) numentpsurfdc(ns)
       lstent = lstent + numentpsurfdc(ns)
       do ixent = 1,numentpsurfdc(ns)
        ient = ient + 1
        read(ir01,1040,err=800) card
        do while (card .ne. 'BEGIN EN')
          read(ir01,1040,err=800) card
        enddo
        read(ir01,5010,err=800) itotsegu(ient),itotsegv(ient)
        ippe(ient) = itotsegu(ient)*itotsegv(ient)
        if(ippe(ient).gt.mxppe) call report_error(2,mxppe,ippe(ient))
        do iptch = 1,ippe(ient)
c... read card containing # of edges, faces, type, el, az & roll
c... for this patch
         read(ir01,5020,err=800) numasent, normsign(nexobj)
         do i = 1,numasent
          read(ir01,5030,err=800) ient2, iptch2, icuorv, uorv
          call addeal(ient,iptch,ient2,iptch2,icuorv,uorv,mxent,mxppe,
     &               eauorv,iealnk,ieaptchlnk,ieacnstuorv)
         enddo
         read(ir01,1111,err=800) nesurf,nfsurf,ntyp,elev,azim,roll
         nfac(nexobj) = nfsurf
         nedg(nexobj) = nesurf
         isurftyp(nexobj) = ntyp
         nboxtyp(nexobj) = ntyp
         newgrid(nexobj) = 1
         surfel(nexobj) = elev
         surfaz(nexobj) = azim
         surfrl(nexobj) = roll
c
         if(ntyp .lt. ep_pattypbic) then		      !for gpes
           razim = azim * dtr
           relev = elev * dtr
           rroll = roll * dtr
           caz = cos(razim)
           saz = sin(razim)
           cel = cos(relev)
           sel = sin(relev)
         endif
c
         if (ntyp .eq. ep_pattypcirct) then             ! circular, t
           cirgpe = .true.
           psi_prm = .false.
           read(ir01,2000,err=800) (tcoef(j,nexobj),j=1,4)    !a,b,c,rl
           read(ir01,2010,err=800) (tr0(j,nexobj),j=1,3)      !r0
           read(ir01,2010,err=800) (tr0(j,nexobj),j=4,6)      !delta r
c
         elseif (ntyp .eq. ep_pattypcircpsi) then          ! circular, psi
           cirgpe = .true.
           psi_prm = .true.
           read(ir01,2000,err=800) a,b,c,psicoef(6,nexobj)  !a,b,c,rl
           read(ir01,2020,err=800) (psicoef(j,nexobj),j=1,3),
     &                   psicoef(5,nexobj),psicoef(4,nexobj)  !v0,k,rmx
           read(ir01,2010,err=800) (psidr(j,nexobj),j=1,3)   !delta r
c
         elseif( ntyp .eq. ep_pattypwire ) then          ! wire
           wires = .true.
           icwre = icwre + 1
           numwires = numwires + 1
           do ii=1,nfsurf
             read(ir01,2060,err=800) card,nn,nl,nr,ikl,ikr,
     &       rad,ixjoint(nn)
c           each wire has a material associated with it.  thus we need
c           to reference the material.  the wire materials will be
c           stuck on the end of the material list, cmatlst, therefore
c           the material for this wire is located at element icwre+nummat 
c           in the material list.
             ixmat(nn) = icwre + nummat
             nconn_wire(1,nn) = nl
             nconn_wire(2,nn) = nr
             datctr(1,nn) = .5
             radius(nn) = rad
             if(ibkp_wire(nl) .eq. 0  .and.  ikl .ne. 0) then
               nunkns = nunkns + 1
               ibkp_wire(nl) = ikl
             endif

             if(ibkp_wire(nr) .eq. 0  .and.  ikr .ne. 0) then
               nunkns = nunkns + 1
               ibkp_wire(nr) = ikr
             endif

             ntype(nn) = ntyp
c
c... add this patch to the linked list of patches for this object
c      ( only used in the displaying of the model )
            call addlnk( nobj(nexobj), nn, nobjlnk )

           end do

           read(ir01,3010,err=800) numsurffeed
           if(numsurffeed .gt. 0) then
             read(ir01,3020,err=800) inumfeedref(icwre), wamp(icwre), 
     &                       wphase(icwre), wlump(icwre)
           endif
           numfeeds = numfeeds + numsurffeed

           read(ir01,3010,err=800) numsurfjunc
           numdup(icwre) = numsurfjunc
           if(numsurfjunc .gt. 0) then
             do iii=1,numsurfjunc
               nunkns = nunkns + 1
               read(ir01,'(i5,2x,i5)',err=800) 
     &            nodejunction(numjunctions+iii),
     &            ibkp_junc(numjunctions+iii)
                  indxdup(iii,icwre) = nodejunction(numjunctions+iii)
             end do
           endif
           numjunctions = numjunctions + numsurfjunc
c
           read(ir01,3025,err=800) wdistload(icwre)
c          the distributed loads for wires are tacked on the end of the
c          materials list.  thus the distributed load for this wire
c          is in element number icwre+nummat.  note: the number of 
c          materials, nummat, is not updated to reflect the longer list.
c          the total length of this list is at least nummat+numwires
           cmatlst(icwre+nummat) = wdistload(icwre)

           nexobj = nexobj + 1

         elseif(ntyp .eq. ep_pattypnurb) then
          read(ir01,4000,err=800) ubegar(nexnrb),
     &    uendar(nexnrb),iulsuar(nexnrb),
     &    idegreeuar(nexnrb),icluar(nexnrb),iperuar(nexnrb)
          read(ir01,4000,err=800) vbegar(nexnrb),
     &    vendar(nexnrb),iulsvar(nexnrb),
     &    idegreevar(nexnrb),iclvar(nexnrb),ipervar(nexnrb)
          lstknotuar(nexnrb) = iulsuar(nexnrb) + idegreeuar(nexnrb) + 1
          iorderuar(nexnrb) = idegreeuar(nexnrb) + 1
          read(ir01,4010,err=800) 
     &    (knotuar(j,nexnrb), j=1,lstknotuar(nexnrb))
          lstknotvar(nexnrb) = iulsvar(nexnrb) + idegreevar(nexnrb) + 1
          iordervar(nexnrb) = idegreevar(nexnrb) + 1
c         report errors
          numcpt = max(iulsuar(nexnrb),iulsvar(nexnrb))
          if(numcpt.gt.mxcpts) call report_error(11,mxcpts,numcpt)
          if(iorderuar(nexnrb).gt.mxorder .or. 
     &    iordervar(nexnrb).gt.mxorder) then
           ireqval = max(iorderuar(nexnrb),iordervar(nexnrb))
           call report_error(12,mxorder,ireqval)
          endif
          if(lstknotuar(nexnrb).gt.mxkts .or.
     &    lstknotvar(nexnrb).gt.mxkts) then
           ireqval = max(lstknotuar(nexnrb),lstknotvar(nexnrb))
c          don't report error here, it should be caught by
c          control points (iuls) or order (iorder) being exceeded
c           call report_error(13,mxkts,ireqval)
          endif
c
          read(ir01,4010,err=800) 
     &    (knotvar(j,nexnrb), j=1,lstknotvar(nexnrb))
          read(ir01,4020,err=800) 
     &    (( wtar(i,j,nexnrb), j=1,iulsvar(nexnrb)), 
     &    i=1,iulsuar(nexnrb))

          read(ir01,4020,err=800) ((( cntrlptar(i,j,k,nexnrb), k=1,3),
     &                                        j=1,iulsvar(nexnrb)),
     &                                        i=1,iulsuar(nexnrb))

          call nurbprocess(nexnrb)

         elseif(ntyp .eq. ep_pattypbic) then
              do k3 = 1,3
                do jj=1,4
                  read(ir01,1200,err=800) (algmtx(ii,jj,k3,nexobj),
     &                                       ii=1,4)
                end do
              end do
          elseif(ntyp .eq. ep_pattypflat) then
            numflatsurf = numflatsurf + 1
            newgrid(nexobj) = 0
         endif

         if(ntyp .ne. ep_pattypwire) then
           do k=1,nfsurf           !do for each plate in the surface
 
            kcnt = mod(k,2)
c
c--------- read card containing the three edges and three nodes

            read(ir01,1050,err=800) card,n,ne1,ne2,ne3,
     &                            n1,n2,n3,cont
            nbound(1,n) = ne1         ! edges
            nbound(2,n) = ne2
            nbound(3,n) = ne3
            nbound(4,n) = n1          ! nodes
            nbound(5,n) = n2
            nbound(6,n) = n3
            lstnbound = n

c
            if(ntyp .eq. ep_pattypflat) nflatplt = nflatplt + 1
c... save gpe orientation angles and surface type with each plate
            if(ntyp .ne. ep_pattypbic) then
              boxor(1,n) = relev        ! elevation (radians)
              boxor(2,n) = razim        ! azimuth  (radians)
              boxor(3,n) = rroll        ! roll  (radians)
            endif
c
            ntype(n) = ntyp
c
              iparms(n) = nexobj
c
c... add this patch to the linked list of patches for this object
c      ( only used in the displaying of the model )
            call addlnk( nobj(nexobj), n, nobjlnk )
c
c.... read coordinates of the 3 vertices (theta,t),(theta,psi) or (zeta,t)
            read(ir01,2040,err=800) card,(tzpt(j,n),j=1,6),cont
            do i = 2,6,2
             if(tzpt(i-1,n).lt.vstart(ient)) vstart(ient) = tzpt(i-1,n)
             if(tzpt(i-1,n).gt.vstop(ient)) vstop(ient) = tzpt(i-1,n)
             if(tzpt(i,n).lt.ustart(ient)) ustart(ient) = tzpt(i,n)
             if(tzpt(i,n).gt.ustop(ient)) ustop(ient) = tzpt(i,n)
            enddo
c
c---------- read card containing the centroid and edge flags
c           order of the centroid coordinates is ( psi ; phi(ie theta) )
            read(ir01,1060,err=800) card,datctr(2,n),datctr(1,n),
     &            (aflag(i,n),i=1,3),cont
c
c---------- read card containing the areas
            read(ir01,1070,err=800) card,aa(n),ixmat(n)
            icolpe(ient) = ixmat(n)
            dif = 1.0
            if(ixvc.gt.0) call igetclse(mxcolor,ixvc,ixmat(n),ivalcol,
     &      ix,dif)
            if(dif.ne.0) then
              ixvc = ixvc + 1
              ivalcol(ixvc) = ixmat(n)
            endif
c
           enddo  ! k=1,nfsurf
c

           do k=1,nesurf
             read(ir01,'(a8,3i8,i8,i8)',err=800) card,nn,ne1,ne2,
     &                                           ib,ionpln(nn)
             nconn(1,nn) = ne1
             nconn(2,nn) = ne2
             ibkp(nn) = ib
             if(ibkp(nn).eq.0) then         ! boundry edge
               itrk = itrk + 1
               itrak(itrk) = k
             else
               nunkns = nunkns + 1
             endif
           enddo          ! k=1,nedges
c
c
           if(ntyp .eq. ep_pattypcircpsi) then
             nexpsi = nexpsi + 1
           elseif(ntyp .eq. ep_pattypcirct   .or.
     &            ntyp .eq. ep_pattypellt) then
             next = next + 1
           elseif(ntyp .eq. ep_pattypnurb) then
             nexnrb = nexnrb + 1
           elseif(ntyp .eq. ep_pattypbic) then
             nexbic = nexbic + 1
           elseif(ntyp .eq. ep_pattypflat) then
             nexflt = nexflt + 1
           endif
           nexobj = nexobj + 1
c
          endif
         enddo
         read(ir01,1040,err=800) card
        enddo
        read(ir01,1040,err=800) card
        do while (card .ne. 'END SURF')
          read(ir01,1040,err=800) card
        enddo
       enddo      ! ns = 1,nsurfaces
c
       do k=1,nnodes
         read(ir01,1090,err=800) card,n,(datnod(j,k),j=1,3)
       enddo   ! k=1,nnodes
c
       do k = 1,nummat
         read(ir01,3000,err=800) card,n,cmatlst(ivalcol(k))
         if(cabs(cmatlst(ivalcol(k))) .ne. 0.) loads = .true.
       enddo
c
c
       read(ir01,1040,err=800) card
       do while (card.ne.'ENDDATA ')     ! enddata card
          read(ir01,1040,err=800) card
       enddo
c
       goto 999
c
c
 800   continue    ! error on read or decode
       ireaderror = 1
       write(iw1,1170)
c       stop
c
 999   continue
       nsflatsurf = numflatsurf
       lstent = lstent - numwires - numflatsurf
       noldsurf = nexobj - 1
       if(lstent.gt.mxent) call report_error(1,mxent,lstent)
       ireqval = noldsurf - numwires
       if(ireqval.gt.mxobj) call report_error(3,mxobj,ireqval)
       close(ir01)
c
 1030 format(6i8)
 1040 format(a8)
 1050 format(a8,7i8,8x,a8)
 1060 format(a8,2(2x,e14.7),3f8.3,8x,a8)
 1070 format(a8,2x,e14.7,i8)
 1090 format(a8,i8,3(1x,e14.7),2(1x,f8.3))
 1130 format(1x,a8,a1,a60)
 1170 format(//,' *** error during read *** check data file ')
 1111 format(3i8,3(2x,e14.7),i5)
 1200 format(4(2x,e14.7))
c
 2000 format(4(2x,e14.7))
 2010 format(3(2x,e14.7))
 2020 format(5(2x,e14.7))
 2040 format(a8,6(1x,f7.3),16x,a8)
 2060 format(a8,5i8,e10.2,i8)
c
 3000 format(a8,i8,2(1x,e14.7))
 3010 format(i5)
 3020 format(i5,4(1x,e14.7))
 3025 format(2x,2(1x,e14.7))
 4000 format(2(f10.5,x),4i5)
 4010 format(7(f10.5,x))
 4020 format(6(e12.5,x))
c
 5000 format(i8)
 5010 format(3i8)   
 5020 format(2i8)
 5030 format(3i8,1x,f7.3)
c
       return
       end
