        SUBROUTINE OPENCDB(UNITNM,KDUNT,PRGNAM,STATUS)
        IMPLICIT NONE
C***********************************************************************
C
C  VERSION     1.0
C  DATE:       APRIL, 1997
C  PROGRAMMER: DORLISA HOMMEL (HSTX)
C
C  VERSION- 1.2  11/03/99  PROGRAMMER: BRIAN BARIL (RAYTHEON ITSS)
C  VERSION- 1.3  11/24/04  PROGRAMMER: FRANK TILLEY (RAYTHEON)
C
C  FUNCTION:   THIS PROGRAM ACCESSES AN 8 BYTE WORD CDB FILE, FILLS THE
C              ASSISTANT TABLE ARRAYS.
C  VERSION- 1.2  MODIFIED THE ROUTINE TO BE ABLE TO ACCESS AND READ THE 
C                LARGE 8-BYE MAPPARM CDB.
C  VERSION- 1.3  CORRECTED AN ERROR WITH RETURN(1) WHERE THERE WAS NO
C                ALTERNATE RETURN ADDRESS IN THE CALLING ARGUMENTS.
C
C  CALLING SEQUENCE PARAMETERS
C  VARIABLE  TYPE  I/O  DESCRIPTION
C  --------  ----  ---  ------------------------------------------------
C  UNITNM    INT    I   CDB FILE UNIT NUMBER, = 16.
C  STATUS    INT    O   ERROR STATUS
C
C  LOCAL PARAMETERS
C
C  INDEX -      ARRAY INDEX POINTING TO THE COEFFICIENT DATA SPECIFIED B
C  OFFSET -     ARRAY CONTAINING COEFFICIENT OFFSETS (FROM THE BEGINNING
C  REC -        RECORD NUMBER IN CDB FILE WHERE COEFFICIENT DATA IS STOR
C  REC_OFFSET - OFFSET WITHIN RECORD TO BEGINNING OF COEFFICIENT DATA.
C  DIM1-3 -     DIMENSIONS OF COEFFICIENT VALUES.
C  DTYPE -      DATA TYPE, 1=REAL, 2=INTEGER, OR 3=CHARACTER.
C
C  EXIT STATES- STATUS IS RETURNED AS 1 IF AN ERROR OCCURS.
C
C***********************************************************************

      INCLUDE 'CDBINFO'

C DECLARATIONS

      INTEGER UNITNM, CDIM, STATUS, INDEX, IOERR, KDUNT
      CHARACTER*2 C2TEMP
      CHARACTER*8 PRGNAM, CHCDB
      CHARACTER*100 MESSAGE

C INITIALIZATIONS

      STATUS = 0
      IOERR = 0
      KDUNIT = KDUNT
      PROGNAME = PRGNAM
C*******DELETE AFTER TESTING*******
C      UNITNM = 9
C      KDUNT = 41
C      PRGNAM = 'MPUPDT  '
C      STATUS = 0
C*************DELETE AFTER TESTING**** 

      IF (CDBFLG .NE. 10) THEN
        CINDX = 0
        CDBFLG = 10
      END IF

C OPEN CDB FILE

      OPEN(UNIT=UNITNM,ACCESS='DIRECT',RECL=RECLNGTH,FORM='UNFORMATTED',
     +     STATUS='UNKNOWN', IOSTAT = IOERR)
      IF (IOERR .NE. 0) THEN
         MESSAGE = 'ERROR IN OPENING CDB - unit   '
         WRITE(C2TEMP,'(I2)') UNITNM
         MESSAGE(29:30) = C2TEMP
         CALL KIDS (KDUNIT,PROGNAME,0,MESSAGE,0)
         STATUS=1
         RETURN
      END IF

C TALLY THE NUMBER OF CDB FILES THAT ARE CURRENTLY OPEN

      CINDX = CINDX + 1
      COFCOUNT(CINDX) = UNITNM
C      PRINT *, 'CINDX:', CINDX
      IF (CINDX .GT. TOTCDBS) THEN
         WRITE(CHCDB,'(I3)') TOTCDBS
         MESSAGE = 'CURRENTLY THE ATOVS CDB PROGRAMS CAN ONLY HANDLE
     +    CDB FILES OPEN AT THE SAME TIME.'
         MESSAGE(51:53) = CHCDB
         CALL KIDS (KDUNIT,PROGNAME,0,MESSAGE,0)
         STATUS=1
         RETURN
      END IF

C READ ASSISTANT TABLE

      CALL GETDATA(UNITNM,STATUS,*200)

      RETURN

 200  CONTINUE
      STATUS = 1
      RETURN
      END

C***********************************************************************

      SUBROUTINE GETDATA(UNITNM,STATUS,*)

C  VERSION     1.0
C  DATE:       APRIL, 1997
C  PROGRAMMER: DORLISA HOMMEL (HSTX)
C
C  FUNCTION:   THIS SUBROUTINE READS THE HEADER RECORDS AND FILLS ARRAYS
C              ASSOCIATED WITH EACH COEFFICIENT, AND RETURNS THESE ARRAY
C
C VARIABLES DEFINED
C
C  INDEX -      ARRAY INDEX POINTING TO THE COEFFICIENT DATA SPECIFIED B
C  OFFSET -     COEFFICIENT OFFSET FROM THE BEGINNING OF THE COEFFICIENT
C  REC -        RECORD NUMBER IN CDB FILE WHERE COEFFICIENT DATA IS STOR
C  REC_OFFSET - OFFSET WITHIN RECORD TO BEGINNING OF COEFFICIENT DATA.
C  DIM1-3 -     DIMENSIONS OF COEFFICIENT VALUES.
C  DTYPE -      THE DATA TYPE, 1=REAL, 2=INTEGER, OR 3=CHARACTER.
C
C***********************************************************************

      INCLUDE 'CDBINFO'

C DECLARATIONS
      INTEGER  K, L, V
      INTEGER  UNITNM,CDIM,OFFSET2,I,J,STATUS,FLAG,TOTRECS,MORE,FLAG2
      INTEGER*8  OFFSTI,RECRDI,DIM0I,DIM1I,DIM2I,DIM3I,DTYPEI,SCALEI
      CHARACTER*2 C2TEMP
      CHARACTER*3 CDBUNIT
      CHARACTER*5 CHREC
      CHARACTER*8 OFFSTBUF, RECRDBUF, DIM0BUF, DIM1BUF, DIM2BUF,
     +            DIM3BUF, DTYPEBUF, SCALEBUF, CDIMBUF
      CHARACTER*100 MESSAGE

      EQUIVALENCE(OFFSTI,OFFSTBUF),(RECRDI,RECRDBUF),(DIM0I,DIM0BUF)
      EQUIVALENCE(DIM1I,DIM1BUF),(DIM2I,DIM2BUF),(DIM3I,DIM3BUF)
      EQUIVALENCE(DTYPEI,DTYPEBUF),(SCALEI,SCALEBUF),(CDIMBUF,CDIM)

C INITIALIZATIONS

      STATUS = 0
      OFFSET2 = 88
      CDBUNIT = 'NO '
      FLAG = 1
      FLAG2 = 1
      MORE = 1
      NUMCDB = 1
      I = 1
      J = 0
      M = 1
      N = 8

C DETERMINE WHICH CDB IS BEING ACCESSED.

      DO WHILE (CDBUNIT .EQ. 'NO ')
        IF (COFCOUNT(NUMCDB) .EQ. UNITNM) THEN
          CDBINDX = NUMCDB
          CDBUNIT = 'YES'
        END IF
        NUMCDB = NUMCDB + 1
      END DO

C READ VALUES FROM ASSISTANT TABLE UNTIL END OF DATA IS REACHED.

      GROUP(1,CDBINDX) = 'DUMMYXXX'
      DO WHILE (FLAG .EQ. 1)
        J = J + 1
        MORE = 1

        READ(UNIT=UNITNM,REC=J,IOSTAT=IOERR) BUFFER
        IF (IOERR .NE. 0) THEN
           WRITE(CHREC,'(I5)') J
           WRITE(C2TEMP,'(I2)') UNITNM
           MESSAGE(1:45)='*** ERROR *** Reading rec       in cdb - unit'
           MESSAGE(46:100)='number   .'
           MESSAGE(27:31) = CHREC
           MESSAGE(53:54) = C2TEMP
           CALL KIDS (KDUNIT,PROGNAME,0,MESSAGE,0)
           STATUS = 1
           RETURN(1)
        END IF
        IF (FLAG2 .EQ. 1) THEN
C*************THIS SYNTAX IS NOT SUPPORTED ANYMORE******
C           CDIMBUF = BUFFER(1)(M:N)
            DO K=M,N
               CDIMBUF(K:K) = BUFFER(K)
            END DO
C**************************************************
           FLAG2 = 0
        END IF

        DO WHILE (MORE .EQ. 1)
          IF ((OFFSET2+88) .GT. RECLNGTH) THEN
             MORE = 0
             OFFSET2 = 1
          ELSE
C***********THIS SYNTAX IS NOT SUPPORTED ANYMORE******
C          GROUP(I,CDBINDX)(M:N) = BUFFER(OFFSET2)(M:N)
           V=0
           DO L=OFFSET2, OFFSET2+7
            V=V+1
            GROUP(I,CDBINDX)(V:V) = BUFFER(L)
           END DO
C******************************************************
           OFFSET2 = OFFSET2 + 8
           IF (GROUP(I,CDBINDX)(M:N) .EQ.'XXXXXXXX'.AND.MORE.EQ.1) THEN
              FLAG = 0
              MORE = 0
           ELSE
C             SUBGROUP(I,CDBINDX)(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               SUBGROUP(I,CDBINDX)(V:V) = BUFFER(L)
              END DO                                    
              OFFSET2 = OFFSET2 + 8
C             COEFF(I,CDBINDX)(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               COEFF(I,CDBINDX)(V:V) = BUFFER(L)
              END DO
              OFFSET2 = OFFSET2 + 8
C             RECRDBUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               RECRDBUF(V:V) = BUFFER(L)
              END DO                                             
              OFFSET2 = OFFSET2 + 8
C             OFFSTBUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               OFFSTBUF(V:V) = BUFFER(L)
              END DO                        
              OFFSET2 = OFFSET2 + 8
C             DIM0BUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               DIM0BUF(V:V) = BUFFER(L)
              END DO
              OFFSET2 = OFFSET2 + 8
C              DIM1BUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               DIM1BUF(V:V) = BUFFER(L)
              END DO
              OFFSET2 = OFFSET2 + 8
C             DIM2BUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               DIM2BUF(V:V) = BUFFER(L)
              END DO
              OFFSET2 = OFFSET2 + 8
C             DIM3BUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               DIM3BUF(V:V) = BUFFER(L)
              END DO
              OFFSET2 = OFFSET2 + 8
C             DTYPEBUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               DTYPEBUF(V:V) = BUFFER(L)
              END DO
              OFFSET2 = OFFSET2 + 8
C             SCALEBUF(M:N) = BUFFER(OFFSET2)(M:N)
              V=0
              DO L=OFFSET2, OFFSET2+7
               V=V+1
               SCALEBUF(V:V) = BUFFER(L)
              END DO        

              OFFSET2 = OFFSET2 + 8

              RECORD(I,CDBINDX) = RECRDI
              OFFSET(I,CDBINDX) = OFFSTI
              DIM0(I,CDBINDX) = DIM0I
              DIM1(I,CDBINDX) = DIM1I
              DIM2(I,CDBINDX) = DIM2I
              DIM3(I,CDBINDX) = DIM3I
              DTYPE(I,CDBINDX) = DTYPEI
              SCALE(I,CDBINDX) = SCALEI
C              PRINT *, 'GROUP:',    GROUP(I,CDBINDX)
C              PRINT *, 'SUBGROUP:', SUBGROUP(I,CDBINDX)
C              PRINT *, 'COEFF:',    COEFF(I,CDBINDX) 
C              PRINT *, 'RECRDBUF:', RECRDI 
C              PRINT *, 'OFFSTBUF:', OFFSTI 
C              PRINT *, 'DIM0BUF:', DIM0I   
C              PRINT *, 'DIM1BUF:', DIM1I  
C              PRINT *, 'DIM2BUF:', DIM2I  
C              PRINT *, 'DIM3BUF:', DIM3I  
C              PRINT *, 'DTYPEBUF:', DTYPEI 
C              PRINT *, 'SCALEBUF:', SCALEI
C              PRINT *, 'NUMBER OF COEFFICIENTS:', I
           END IF
           I = I + 1
           IF (I .GE. 3300) THEN
             MESSAGE='*** ERROR *** NUMBER OF COEFFICIENTS EXCEEDS THE
     +TOTAL NUMBER ALLOWABLE (1000), ** EDIT CDBINFO **'
             CALL KIDS (KDUNIT,PROGNAME,0,MESSAGE,0)
             STATUS = 1
             RETURN(1)
           END IF
          END IF
        END DO
      END DO
  
      RETURN

      END


