      SUBROUTINE MATIN (A,B,NC,NR,NDIM, NUMAT,NFIRST,NLAST, IR)
      IMPLICIT REAL (A-H,O-Z)                               
      REAL A
      INCLUDE 'SIZES'
      DIMENSION A(NDIM,NDIM), B(NDIM), NFIRST(MAXORB), NLAST(MAXORB)
C********************************************************************** 
C                                                                       
C      MATOUT PRINTS A SQUARE MATRIX OF EIGENVECTORS AND EIGENVALUES    
C                                                                       
C    ON INPUT A CONTAINS THE MATRIX TO BE READ
C             B CONTAINS THE EIGENVALUES.                               
C             NC NUMBER OF MOLECULAR ORBITALS TO BE READ.
C             NR IS THE SIZE OF THE SQUARE ARRAY TO BE READ.
C             NDIM IS THE ACTUAL SIZE OF THE SQUARE ARRAY "A".          
C             NFIRST AND NLAST CONTAIN ATOM ORBITAL COUNTERS.           
C             NAT = ARRAY OF ATOMIC NUMBERS OF ATOMS.                   
C             IR  = UNIT NUMBER FOR INPUT
C                                                                       
C***********************************************************************
      CHARACTER*2 ATORBS(9), ITEXT(MAXORB), JTEXT(MAXORB), DUMC
      CHARACTER*80 DUMMY, LINE
      LOGICAL ERROR
      LOGICAL DEBUG, DEBUGL, DEBUGN, DEBUGO, DEBUGP, DEBUGI
      COMMON /DEBCOM/ DEBUG, DEBUGL, DEBUGN, DEBUGO, DEBUGP, DEBUGI
      DIMENSION NATOM(MAXORB)
      DATA ATORBS/' S','PX','PY','PZ','X2','XZ','Z2','YZ','XY'/
      IF ( DEBUGI ) THEN
        WRITE (DUMMY, '('' IN MATIN: NR='',I4,'' NDIM='',I4)')
     .       NR, NDIM
        CALL DEBUGR( DUMMY )
      ENDIF
      IF(NLAST(NUMAT).NE.NR) GOTO 103

      DO 101 I=1,NUMAT    
        JLO=NFIRST(I)    
        JHI=NLAST(I)     
        K=0              
        DO 102 J=JLO,JHI 
          K=K+1         
          ITEXT(J)=ATORBS(K)
          NATOM(J)=I        
  102   CONTINUE             
  101 CONTINUE                

      GOTO 104                
  103 CONTINUE                
      NR=ABS(NR)

      DO 105 I=1,NR    
        ITEXT(I)='  ' 
        JTEXT(I)='  ' 
  105 NATOM(I)=I       

  104 CONTINUE         
      KA=1             
      KC=6             
   10 KB=MIN0(KC,NC)               
      READ( IR,'(A)')(DUMC,I=1,6)
C?      READ( IR,'(A8,10F12.6)',END=999,ERR=999)DUMC,(B(I),I=KA,KB)
* GET EIGENVALUES
      READ ( IR, '(A80)', END=999, ERR=999) LINE
      CALL LCLEAN( LINE, LINE, .TRUE.)

      DO 12 I= KA, KB
        B( I) = READA( LINE, 1, ERROR)
        IF (ERROR) B( I) = 0.0D0
        CALL POPARG( LINE, LINE)
   12 CONTINUE

      READ( IR,'(A)')DUMC
      LA=1     
      LC=40    
   20 LB=MIN0(LC,NR)  

      DO 30 I=LA,LB   
        IF(ITEXT(I).EQ.' S')READ( IR,'(A)')DUMC
* GET EIGENVECTOR COMPONENTS
        READ ( IR, '(A80)', END=999, ERR=999) LINE
        CALL LCLEAN( LINE, LINE, .TRUE.)
        IF ( DEBUGI ) CALL DEBUGR( ' '//LINE(1:72)//'*' )
        LINE = LINE( INDEX( LINE, ' '): )
        IF ( DEBUGI ) WRITE(*,'('' I='',I4,'' KA='',I4,''  KB='',I4)') 
     .                               I, KA, KB

        CALL LCLEAN( LINE, LINE, .TRUE.)
        DO 22 J= KA, KB
          A( I, J) = READA( LINE, 1, ERROR)
          IF (ERROR) A( I, J) = 0.0D0
          CALL POPARG( LINE, LINE)
   22   CONTINUE

   30 CONTINUE                                                          

      IF (LB.EQ.NR) GO TO 40                                            
      LA=LC+1           
      LC=LC+40            
      READ( IR,'(A)')DUMC
      GO TO 20            
   40 CONTINUE
      IF (KB.EQ.NC) RETURN  
      KA=KC+1               
      KC=KC+6                           
      IF (NR.GT.25) READ( IR,'(A)')DUMC
      GO TO 10  
C               
  999 CONTINUE
      CALL DEBUGR( 'MATIN: READ ERROR')
      RETURN
      END 
