      SUBROUTINE GETDATA(FMT,TYPTBL,REG,XX,XX1,X,Y1,IT1,SIZ,NTYP,NDIMB,
     * NDIMB1,MAXSIZE,MAXNCLU,IMAX,NCLUS)
C
C  To read data and store as expanded rows on scratch file
C
      CHARACTER FMT(2)*72,FRMT*144
      REAL*4 XX(NDIMB1),XX1(NDIMB1),X(NDIMB),Y1(MAXSIZE)
      INTEGER*4 NERR
      INTEGER*2 TYPTBL(0:NTYP,0:NTYP),
     * REG(2*NTYP+NTYP*(NTYP-1)/2,2*NDIMB+4),IT1(MAXSIZE),SIZ(MAXNCLU)
C
      FRMT= FMT(1)//FMT(2)
      IMAX = 1
      NCLUS= 0
      NREC = 0
      NREC1= 1
      NERR = 0
      IFLG = 0
      IFLG1= 0
      READ(8,FRMT) ID1
      BACKSPACE 8
C
   10 READ(8,FRMT,ERR=11,END=12) NEWID,IT,Y,XX
      NREC1= NREC1+1
      IF (IT.LT.1.OR.IT.GT.NTYP.OR.(Y.NE.0..AND.Y.NE.1.)) THEN
        NERR= NERR+1
        GOTO 10
      ELSEIF (NEWID.EQ.ID1) THEN
        GOTO 22
      ELSE
        GOTO 14
      ENDIF
   11 WRITE(*,'(/2A,I6/20X,A)') ' PROGRAM TERMINATED: Data read error',
     * ' on record',NREC1,' (possible format statement error)' 
      WRITE(9,'(/2A,I6/20X,A)') ' PROGRAM TERMINATED: Data read error',
     * ' on record',NREC1,' (possible format statement error)' 
      STOP
   12 IFLG= 1
      DO 13 I=1,NDIMB1
   13 XX1(I)= XX(I)
C
   14 NCLUS= NCLUS+1
      IF (NCLUS.GT.MAXNCLU) THEN
        WRITE(*,'(/2A,I4)') ' PROGRAM TERMINATED: Number of ',
     *   'clusters larger than maximum ',MAXNCLU
        WRITE(9,'(/2A,I4)') ' PROGRAM TERMINATED: Number of ',
     *   'clusters larger than maximum ',MAXNCLU
        STOP
      ENDIF
      SIZ(NCLUS)= NREC
      IF (NREC.GT.1.AND.IFLG1.EQ.0) THEN
        IF (NREC.GT.IMAX) IMAX=NREC
        IF (IFLG.EQ.0) THEN
          BACKSPACE(8)
          BACKSPACE(8)
          READ(8,FRMT) IDX,ITX,YX,XX1
          READ(8,FRMT)
        ENDIF
        DO 20 I=1,NREC-1
        DO 20 J=I+1,NREC
        DO 16 K=1,NDIMB
   16   X(K)= 0.
        DO 18 K=1,REG(TYPTBL(IT1(I),IT1(J)),3)
   18   X(REG(TYPTBL(IT1(I),IT1(J)),K+3))=
     *   XX1(REG(TYPTBL(IT1(I),IT1(J)),NDIMB+K+3))
   20   WRITE(10) Y1(I)*Y1(J),X
      ENDIF
      IF (IFLG.EQ.1) GOTO 28
      ID1 = NEWID
      NREC= 0
   22 NREC= NREC+1
      IF (NREC.LE.MAXSIZE) THEN
        Y1(NREC) = Y
        IT1(NREC)= IT
        DO 24 I=1,NDIMB
   24   X(I)= 0.
        DO 26 I=1,REG(IT,3)
   26   X(REG(IT,I+3))= XX(REG(IT,NDIMB+I+3))
        WRITE(10) Y,X
      ELSE
        IFLG1=1
      ENDIF
      GOTO 10
C
   28 CLOSE(8)
      ENDFILE 10
      WRITE(9,'(/A,I5/A,I5/A,12I5,10(/5X,14I5))') 
     * ' Data records rejected:',NERR,' Number of clusters   :',NCLUS,
     * ' Cluster sizes:',(SIZ(I),I=1,NCLUS)
C
      IF (IFLG1.EQ.1) THEN
        WRITE(*,'(/2A,I4)') ' PROGRAM TERMINATED: Some cluster ',
     *   'larger than maximum ',MAXSIZE
        WRITE(9,'(/2A,I4)') ' PROGRAM TERMINATED: Some cluster ',
     *   'larger than maximum ',MAXSIZE
        STOP
      ELSEIF (NCLUS.EQ.0) THEN
        WRITE(*,'(/A)') ' PROGRAM TERMINATED: All clusters are empty'
        WRITE(9,'(/A)') ' PROGRAM TERMINATED: All clusters are empty'
        STOP
      ELSE    
        RETURN
      ENDIF
      END
