!+ Subroutine to join two archive together.
!
! Method:
!	For each archive of the pair the contents of the headers
!	are compared to see which entries may be combined. Some
!	properties of the entries must necessarily be the same,
!	but others are permitted to differ. Those entries which
!	can be joined to make a single entry are so joined.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	Version		Date			Comment
!	1.0		12-04-95		First Version under RCS
!						(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77  with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      SUBROUTINE JOIN_ARCH(IERR, IUNIT_OUT, N_PER_ARCH_RECORD
     &   , IUNIT_1, N_ENTRY_1
     &   , DATE_1, DATABASE_1, LBL_CODE_1
     &   , LINESHAPE_1, CUTOFF_1, CONTINUUM_1, LINE_COUPLE_1
     &   , I_GAS_1, P_1, T_1, AMOUNT_1, PARTIAL_P_1
     &   , FREQ_ENTRY_LOW_1, FREQ_ENTRY_HIGH_1
     &   , N_TRANS_RECORD_1, I1_RECORD_1, N_TRANS_1
     &   , IUNIT_2, N_ENTRY_2
     &   , DATE_2, DATABASE_2, LBL_CODE_2
     &   , LINESHAPE_2, CUTOFF_2, CONTINUUM_2, LINE_COUPLE_2
     &   , I_GAS_2, P_2, T_2, AMOUNT_2, PARTIAL_P_2
     &   , FREQ_ENTRY_LOW_2, FREQ_ENTRY_HIGH_2
     &   , N_TRANS_RECORD_2, I1_RECORD_2, N_TRANS_2
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/dimensions_arch.ucf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/line_shape.pcf'
      INCLUDE 'incl/lbl_database.ecf'
      INCLUDE 'incl/lbl_code.ecf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(IN)
     &     IERR
!             ERROR FLAG
     &   , IUNIT_1
!             UNIT NUMBER FOR FIRST ARCHIVE
     &   , IUNIT_2
!             UNIT NUMBER FOR SECOND ARCHIVE
     &   , IUNIT_OUT
!             UNIT NUMBER FOR OUPUT
!
!     PROPERTIES OF FIRST ARCHIVE:
      INTEGER	!, INTENT(IN)
     &     N_ENTRY_1
!             NUMBER OF ENTRIES IN FIRST ARCHIVE
     &   , LINESHAPE_1(NPD_ARCH_ENTRY)
!             LINE SHAPES IN FIRST ARCHIVE
     &   , I_GAS_1(NPD_ARCH_ENTRY)
!             IDENTITIES OF GASES IN 1ST ARCHIVE
     &   , N_TRANS_RECORD_1(NPD_ARCH_ENTRY)
!             NUMBER OF RECORDS
     &   , I1_RECORD_1(NPD_ARCH_ENTRY)
!             POINTERS TO 1ST RECORD OF ENTRY
     &   , N_TRANS_1(NPD_ARCH_ENTRY)
!             NUMBER OF TRANSMISSIONS IN ENTRY
     &   , DATABASE_1(NPD_ARCH_ENTRY)
!             DATABASES IN FIRST ARCHIVE
     &   , LBL_CODE_1(NPD_ARCH_ENTRY)
!             LBL CODES IN FIRST ARCHIVE
      CHARACTER	!, INTENT(IN)
     &     DATE_1(NPD_ARCH_ENTRY)*9
!             DATES IN FIRST ARCHIVE
      LOGICAL	!, INTENT(IN)
     &     LINE_COUPLE_1(NPD_ARCH_ENTRY)
!             LINE COUPLING IN 1ST ARCHIVE
     &   , CONTINUUM_1(NPD_ARCH_ENTRY)
!             CONTINUUM IN 1ST ARCVIVE
      REAL*8	!, INTENT(IN)
     &     CUTOFF_1(NPD_ARCH_ENTRY)
!             LINE CUTOFFS IN FIRST ARCHIVE
     &   , P_1(NPD_ARCH_ENTRY)
!             PRESSURES IN FIRST ARCHIVES
     &   , T_1(NPD_ARCH_ENTRY)
!             TEMPERATURES IN FIRST ARCHIVES
     &   , AMOUNT_1(NPD_ARCH_ENTRY)
!             AMOUNTS IN FIRST ARCHIVE
     &   , PARTIAL_P_1(NPD_ARCH_ENTRY)
!             PARTIAL PRESSURES IN FIRST ARCHIVE
     &   , FREQ_ENTRY_LOW_1(NPD_ARCH_ENTRY)
!             LOW FREQUENCY LIMITS OF ENTRIES
     &   , FREQ_ENTRY_HIGH_1(NPD_ARCH_ENTRY)
!             HIGH FREQUENCY LIMITS OF ENTRIES
!
!     PROPERTIES OF SECOND ARCHIVE:
      INTEGER	!, INTENT(IN)
     &     N_ENTRY_2
!             NUMBER OF ENTRIES IN 2ND ARCHIVE
     &   , LINESHAPE_2(NPD_ARCH_ENTRY)
!             LINE SHAPES IN 2ND ARCHIVE
     &   , I_GAS_2(NPD_ARCH_ENTRY)
!             IDENTITIES OF GASES IN 2ND ARCHIVE
     &   , N_TRANS_RECORD_2(NPD_ARCH_ENTRY)
!             NUMBER OF RECORDS
     &   , I1_RECORD_2(NPD_ARCH_ENTRY)
!             POINTERS TO 1ST RECORD OF ENTRY
     &   , N_TRANS_2(NPD_ARCH_ENTRY)
!             NUMBER OF TRANSMISSIONS IN ENTRY
     &   , DATABASE_2(NPD_ARCH_ENTRY)
!             DATABASES IN SECOND ARCHIVE
     &   , LBL_CODE_2(NPD_ARCH_ENTRY)
!             LBL CODES IN SECOND ARCHIVE
      CHARACTER	!, INTENT(IN)
     &     DATE_2(NPD_ARCH_ENTRY)*9
!             DATES IN SECOND ARCHIVE
      LOGICAL	!, INTENT(IN)
     &     LINE_COUPLE_2(NPD_ARCH_ENTRY)
!             LINE COUPLING IN 2ND ARCHIVE
     &   , CONTINUUM_2(NPD_ARCH_ENTRY)
!             CONTINUUM IN 2ND ARCVIVE
      REAL*8	!, INTENT(IN)
     &     CUTOFF_2(NPD_ARCH_ENTRY)
!             LINE CUTOFFS IN 2ND ARCH
     &   , P_2(NPD_ARCH_ENTRY)
!             PRESSURES IN 2ND ARCHIVES
     &   , T_2(NPD_ARCH_ENTRY)
!             TEMPERATURES IN 2ND ARCHIVES
     &   , AMOUNT_2(NPD_ARCH_ENTRY)
!             AMOUNTS IN 2ND ARCHIVE
     &   , PARTIAL_P_2(NPD_ARCH_ENTRY)
!             PARTIAL PRESSURES IN 2ND ARCHIVE
     &   , FREQ_ENTRY_LOW_2(NPD_ARCH_ENTRY)
!             LOW FREQUENCY LIMITS OF ENTRIES
     &   , FREQ_ENTRY_HIGH_2(NPD_ARCH_ENTRY)
!             HIGH FREQUENCY LIMITS OF ENTRIES
!
!     LOCAL VARIABLES
!
!     PROPERTIES OF MERGED ARCHIVE:
      INTEGER
     &     I_VERSION_M
!             VERSION OF MERGED ARCHIVE
     &   , N_ENTRY_M
!             NUMBER OF ENTRIES IN MERGED FILE
     &   , LINESHAPE_M(NPD_ARCH_ENTRY)
!             LINE SHAPES IN MERGED FILE
     &   , DATABASE_M(NPD_ARCH_ENTRY)
!             DATABASES USED
     &   , LBL_CODE_M(NPD_ARCH_ENTRY)
!             LBL CODES USED
     &   , I_GAS_M(NPD_ARCH_ENTRY)
!             IDENTITIES OF GASES IN MERGED FILE
     &   , N_TRANS_RECORD_M(NPD_ARCH_ENTRY)
!             NUMBER OF RECORDS IN MERGED FILE
     &   , I1_RECORD_M(NPD_ARCH_ENTRY)
!             POINTERS TO FIRST RECORD OF ENTRY
     &   , N_TRANS_M(NPD_ARCH_ENTRY)
!             TRANSMISSIONS IN MERGED ARCHIVE
      CHARACTER
     &     DATE_M(NPD_ARCH_ENTRY)*9
!             DATES OF COMPOSITION OF ARCHIVES
      LOGICAL
     &     LINE_COUPLE_M(NPD_ARCH_ENTRY)
!             LINE COUPLING IN MERGED ARCHIVE
     &   , CONTINUUM_M(NPD_ARCH_ENTRY)
!             CONTINUUM IN MERGED ARCVIVE
      REAL*8
     &     CUTOFF_M(NPD_ARCH_ENTRY)
!             LINE CUTOFFS IN MERGED ARCH
     &   , P_M(NPD_ARCH_ENTRY)
!             PRESSURES IN MERGED ARCHIVES
     &   , T_M(NPD_ARCH_ENTRY)
!             TEMPERATURES IN MERGED ARCHIVES
     &   , AMOUNT_M(NPD_ARCH_ENTRY)
!             AMOUNTS IN MERGED ARCHIVE
     &   , PARTIAL_P_M(NPD_ARCH_ENTRY)
!             PARTIAL PRESSURES IN MERGED ARCHIVE
     &   , FREQ_ENTRY_LOW_M(NPD_ARCH_ENTRY)
!             LOW FREQUENCY LIMITS OF ENTRIES
     &   , FREQ_ENTRY_HIGH_M(NPD_ARCH_ENTRY)
!             HIGH FREQUENCY LIMITS OF ENTRIES
!             TRANSMISSIONS IN MERGED ENTRY
!
      INTEGER
     &     N_PER_ARCH_RECORD
!             NUMBER OF TRANSMISSIONS IN ONE RECORD
     &   , I_MERGE(NPD_ARCH_ENTRY)
!             ENTRY IN 2ND ARCHIVE TO MERGE
     &   , LAST_RECORD
!             LAST RECORD OF ARCHIVE YET WRITTEN
     &   , I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
     &   , K
!             LOOP VARIABLE
     &   , OFFSET
!             OFFSET IN MERGED ARCHIVE
     &   , N_COUNT
!             COUNTING VARIABLE
!
!     LOGICAL FLAGS FOR COMPARING ARCHIVES:
      LOGICAL
     &     L_IGNORE_DATABASE
!             IGNORE DIFFERENCES IN DATABASES
     &   , L_IGNORE_LBL_CODE
!             IGNORE DIFFERENCES IN LBL CODES
     &   , L_IGNORE_LINESHAPE
!             IGNORE DIFFERENCES IN LINESHAPES
     &   , L_IGNORE_CUTOFF
!             IGNORE DIFFERENCES IN CUTOFFS
     &   , L_IGNORE_CONTINUUM
!             IGNORE DIFFERENCES IN CONTINUUM
     &   , L_IGNORE_LCOUPLE
!             IGNORE DIFFERENCES IN LINE COUPLING
     &   , L_MERGED(NPD_ARCH_ENTRY)
!             FLAGS FOR MERGED ENTRIES
     &   , L_POSSIBLE
!             LOGICAL FOR MERGING
     &   , L_DIRECT
!             FLAG FOR DIRECT MERGING
     &   , L_SECOND_FIRST
!             SECOND ENTRY COMES FIRST
!
!     ELEMENTS OF ENTRIES
      REAL*8
     &     FREQ_LOW_1(NPD_FREQ_ENTRY)
!             LOW FREQUENCY LIMIT OF 1ST ENTRY
     &   , FREQ_HIGH_1(NPD_FREQ_ENTRY)
!             HIGH FREQUENCY LIMIT OF 1ST ENTRY
     &   , TRANS_1(NPD_FREQ_ENTRY)
!             TRANSMISSIONS IN 1ST ENTRY
     &   , FREQ_LOW_2(NPD_FREQ_ENTRY)
!             LOW FREQUENCY LIMIT OF 2ND ENTRY
     &   , FREQ_HIGH_2(NPD_FREQ_ENTRY)
!             HIGH FREQUENCY LIMIT OF 2ND ENTRY
     &   , TRANS_2(NPD_FREQ_ENTRY)
!             TRANSMISSIONS IN 2ND ENTRY
     &   , FREQ_LOW_M(NPD_FREQ_ENTRY)
!             LOW FREQUENCY LIMIT OF MERGED ENTRY
     &   , FREQ_HIGH_M(NPD_FREQ_ENTRY)
!             HIGH FREQUENCY OF MERGED ENTRY
     &   , TRANS_M(NPD_FREQ_ENTRY)
!
      REAL*8
     &     TOL_JOIN
!             TOLERANCE FOR JOINING ENTRIES
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     TEST_MANDATORY, TEST_OPTIONAL, TEST_FREQUENCY
     &   , ARRANGE_ARCHIVE, READ_ARCHIVE_ENTRY_0
     &   , WRITE_ARCHIVE_ENTRY_0, WRITE_ARCHIVE_HEADER_0
!
!
!
!     DETERMINE WHICH PROPERTIES OF THE ARCHIVES MAY DIFFER WITHOUT
!     IMPEDING MERGING.
      WRITE(IU_USER, '(/A)')
     &   'TWO ARCHIVES CAN BE MERGED ONLY IF THEY HAVE THE SAME '
      WRITE(IU_USER, '(5(3X, A, /))')
     &   'TYPE OF GAS', 'PRESSURE', 'TEMPERATURE'
     &   , 'AMOUNT OF ABSORBER', 'PARTIAL PRESSURE'
      WRITE(IU_USER, '(/A)')
     &   'DIFFERENCES IN OTHER PROPERTIES MAY BE OVERLOOKED.'
      WRITE(IU_USER, '(3X, A)')
     &   'IGNORE DIFFERENCES IN LINE DATABASE USED? (T/F)'
      READ(IU_STDIN, *) L_IGNORE_DATABASE
      WRITE(IU_USER, '(3X, A)')
     &   'IGNORE DIFFERENCES IN LBL CODE USED? (T/F)'
      READ(IU_STDIN, *) L_IGNORE_LBL_CODE
      WRITE(IU_USER, '(3X, A)')
     &   'IGNORE DIFFERENCES IN LINE-SHAPE USED? (T/F)'
      READ(IU_STDIN, *) L_IGNORE_LINESHAPE
      WRITE(IU_USER, '(3X, A)')
     &   'IGNORE DIFFERENCES IN CUTOFF USED? (T/F)'
      READ(IU_STDIN, *) L_IGNORE_CUTOFF
      WRITE(IU_USER, '(3X, A)')
     &   'IGNORE DIFFERENCES IN CONTINUUM ? (T/F)'
      READ(IU_STDIN, *) L_IGNORE_CONTINUUM
      WRITE(IU_USER, '(3X, A)')
     &   'IGNORE DIFFERENCES IN LINE COUPLING? (T/F)'
      READ(IU_STDIN, *) L_IGNORE_LCOUPLE
!
      WRITE(IU_USER, '(/A)') 'ENTER THE TOLERANCE ON FREQUENCIES '
     &   //'FOR DIRECT JOINING.'
      READ(IU_STDIN, *) TOL_JOIN
!
!     GO THROUGH EACH ARCHIVE IN THE FIRST FILE AND DETERMINE WHETHER
!     IT CAN BE MERGED WITH ONE FROM THE SECOND.
!     INITIALIZATION OF THE ARRAYS
      DO I=1, N_ENTRY_1
         I_MERGE(I)=0
      ENDDO
      DO J=1, N_ENTRY_2
         L_MERGED(J)=.FALSE.
      ENDDO
      DO I=1, N_ENTRY_1
         DO J=1, N_ENTRY_2
!
            L_POSSIBLE=.TRUE.
            CALL TEST_MANDATORY(L_POSSIBLE
     &         , I_GAS_1(I), P_1(I), T_1(I), AMOUNT_1(I)
     &         , PARTIAL_P_1(I)
     &         , I_GAS_2(J), P_2(J), T_2(J), AMOUNT_2(J)
     &         , PARTIAL_P_2(J)
     &         )
            IF (L_POSSIBLE) THEN
               CALL TEST_OPTIONAL(L_POSSIBLE
     &            , DATABASE_1(I), LBL_CODE_1(I), LINESHAPE_1(I)
     &            , CUTOFF_1(I), CONTINUUM_1(I), LINE_COUPLE_1(I)
     &            , DATABASE_2(J), LBL_CODE_2(J), LINESHAPE_2(J)
     &            , CUTOFF_2(J), CONTINUUM_2(J), LINE_COUPLE_2(J)
     &            , L_IGNORE_DATABASE, L_IGNORE_LBL_CODE
     &            , L_IGNORE_LINESHAPE, L_IGNORE_CUTOFF
     &            , L_IGNORE_CONTINUUM, L_IGNORE_LCOUPLE
     &            )
               IF (L_POSSIBLE) THEN
                  CALL TEST_FREQUENCY(L_POSSIBLE
     &               , L_DIRECT, L_SECOND_FIRST
     &               , FREQ_ENTRY_LOW_1(I), FREQ_ENTRY_HIGH_1(I)
     &               , FREQ_ENTRY_LOW_2(J), FREQ_ENTRY_HIGH_2(J)
     &               , TOL_JOIN
     &               )
               ENDIF
            ENDIF
            IF (L_POSSIBLE) THEN
!              THESE TWO ARCHIVES CAN BE MERGED.
!              BUT FIRST CHECK FOR DUPLICATE MERGING
               IF (I_MERGE(I).NE.0) THEN
                  WRITE(IU_ERR, '(/A)')
     &               'THE SECOND ARCHIVE CONTAINS DUPLICATE ENTRIES.'
                  IERR=I_ERR_FATAL
                  RETURN
               ENDIF
               I_MERGE(I)=J
               L_MERGED(J)=.TRUE.
            ENDIF
         ENDDO
      ENDDO
!
!     DETERMINE HOW MANY ENTRIES THERE WILL BE IN THE NEW ARCHIVE.
      N_ENTRY_M=N_ENTRY_1
      DO J=1, N_ENTRY_2
         IF (.NOT.L_MERGED(J)) THEN
            N_ENTRY_M=N_ENTRY_M+1
         ENDIF
      ENDDO
      LAST_RECORD=4*N_ENTRY_M+1
!
      DO I=1, N_ENTRY_1
!        FORM THE HEADERS FOR THE MERGED BLOCKS.
         J=I_MERGE(I)
         IF (J.GT.0) THEN
            IF (DATE_1(I)(1:9).NE.DATE_2(J)(1:9)) THEN
               DATE_M(I)(1:9)='MERGED   '
            ELSE
               DATE_M(I)(1:9)=DATE_1(I)(1:9)
            ENDIF
            IF (DATABASE_1(I).NE.DATABASE_2(J)) THEN
               DATABASE_M(I)=IP_LBL_DATA_UNASSIGNED
            ELSE
               DATABASE_M(I)=DATABASE_1(I)
            ENDIF
            IF (LBL_CODE_1(I).NE.LBL_CODE_2(J)) THEN
               LBL_CODE_M(I)=IP_LBL_CODE_UNASSIGNED
            ELSE
               LBL_CODE_M(I)=LBL_CODE_1(I)
            ENDIF
            IF (LINESHAPE_1(I).NE.LINESHAPE_2(J)) THEN
               LINESHAPE_M(I)=IP_LINE_SHAPE_UNASSIGNED
            ELSE
               LINESHAPE_M(I)=LINESHAPE_1(I)
            ENDIF
            CUTOFF_M(I)=MIN(CUTOFF_1(I), CUTOFF_2(J))
            CONTINUUM_M(I)=CONTINUUM_1(I).OR.CONTINUUM_2(J)
            LINE_COUPLE_M(I)=LINE_COUPLE_1(I).OR.LINE_COUPLE_2(J)
            I_GAS_M(I)=I_GAS_1(I)
            P_M(I)=P_1(I)
            T_M(I)=T_1(I)
            AMOUNT_M(I)=AMOUNT_1(I)
            PARTIAL_P_M(I)=PARTIAL_P_1(I)
            IF (L_SECOND_FIRST) THEN
               FREQ_ENTRY_LOW_M(I)=FREQ_ENTRY_LOW_2(J)
               FREQ_ENTRY_HIGH_M(I)=FREQ_ENTRY_HIGH_1(I)
            ELSE
               FREQ_ENTRY_LOW_M(I)=FREQ_ENTRY_LOW_1(I)
               FREQ_ENTRY_HIGH_M(I)=FREQ_ENTRY_HIGH_2(J)
            ENDIF
            IF (L_DIRECT) THEN
               N_TRANS_M(I)=N_TRANS_1(I)+N_TRANS_2(J)
            ELSE
               N_TRANS_M(I)=N_TRANS_1(I)+N_TRANS_2(J)+1
            ENDIF
            CALL ARRANGE_ARCHIVE(I1_RECORD_M(I), N_PER_ARCH_RECORD
     &         , LAST_RECORD, N_TRANS_M(I), N_TRANS_RECORD_M(I)
     &         )
!
!           READ IN THE TWO ARCHIVAL ENTRIES TO BE MERGED.
            CALL READ_ARCHIVE_ENTRY_0(IUNIT_1, N_TRANS_RECORD_1(I)
     &         , I1_RECORD_1(I), N_PER_ARCH_RECORD, N_TRANS_1(I)
     &         , FREQ_LOW_1, FREQ_HIGH_1, TRANS_1
     &         )
            CALL READ_ARCHIVE_ENTRY_0(IUNIT_2, N_TRANS_RECORD_2(J)
     &         , I1_RECORD_2(J), N_PER_ARCH_RECORD, N_TRANS_2(J)
     &         , FREQ_LOW_2, FREQ_HIGH_2, TRANS_2
     &         )
!
!           FORM THE COMPLETE LIST OF FREQUENCIES AND TRANSMISSIONS.
            IF (L_SECOND_FIRST) THEN
               DO K=1, N_TRANS_2(J)
                  FREQ_LOW_M(K)=FREQ_LOW_2(K)
                  FREQ_HIGH_M(K)=FREQ_HIGH_2(K)
                  TRANS_M(K)=TRANS_2(K)
               ENDDO
               IF (L_DIRECT) THEN
                  OFFSET=N_TRANS_2(J)
               ELSE
                  OFFSET=N_TRANS_2(J)+1
                  FREQ_LOW_M(OFFSET)=FREQ_HIGH_2(N_TRANS_2(J))
                  FREQ_HIGH_M(OFFSET)=FREQ_LOW_1(1)
                  TRANS_M(OFFSET)=0.0D+00
               ENDIF
               DO K=1, N_TRANS_1(I)
                  FREQ_LOW_M(K+OFFSET)=FREQ_LOW_1(K)
                  FREQ_HIGH_M(K+OFFSET)=FREQ_HIGH_1(K)
                  TRANS_M(K+OFFSET)=TRANS_1(K)
               ENDDO
            ELSE
               DO K=1, N_TRANS_1(I)
                  FREQ_LOW_M(K)=FREQ_LOW_1(K)
                  FREQ_HIGH_M(K)=FREQ_HIGH_1(K)
                  TRANS_M(K)=TRANS_1(K)
               ENDDO
               IF (L_DIRECT) THEN
                  OFFSET=N_TRANS_1(I)
               ELSE
                  OFFSET=N_TRANS_1(I)+1
                  FREQ_LOW_M(OFFSET)=FREQ_HIGH_1(N_TRANS_1(I))
                  FREQ_HIGH_M(OFFSET)=FREQ_LOW_2(J)
                  TRANS_M(OFFSET)=0.0D+00
               ENDIF
               DO K=1, N_TRANS_2(J)
                  FREQ_LOW_M(K+OFFSET)=FREQ_LOW_2(K)
                  FREQ_HIGH_M(K+OFFSET)=FREQ_HIGH_2(K)
                  TRANS_M(K+OFFSET)=TRANS_2(K)
               ENDDO
            ENDIF

            CALL WRITE_ARCHIVE_ENTRY_0(IUNIT_OUT, N_TRANS_RECORD_M(I)
     &         , I1_RECORD_M(I), N_PER_ARCH_RECORD, N_TRANS_M(I)
     &         , FREQ_LOW_M, FREQ_HIGH_M, TRANS_M
     &         )
!
         ELSE
!           THE ENTRY FROM THE FIRST ARCHIVE MUST BE WRIITEN
!           AT THE APPROPRIATE PLACE IN THE MERGED ARCHIVE.
            DATE_M(I)=DATE_1(I)
            DATABASE_M(I)=DATABASE_1(I)
            LBL_CODE_M(I)=LBL_CODE_1(I)
            LINESHAPE_M(I)=LINESHAPE_1(I)
            CUTOFF_M(I)=CUTOFF_1(I)
            CONTINUUM_M(I)=CONTINUUM_1(I)
            LINE_COUPLE_M(I)=LINE_COUPLE_1(I)
            I_GAS_M(I)=I_GAS_1(I)
            P_M(I)=P_1(I)
            T_M(I)=T_1(I)
            AMOUNT_M(I)=AMOUNT_1(I)
            PARTIAL_P_M(I)=PARTIAL_P_1(I)
            FREQ_ENTRY_LOW_M(I)=FREQ_ENTRY_LOW_1(I)
            FREQ_ENTRY_HIGH_M(I)=FREQ_ENTRY_HIGH_1(I)
            N_TRANS_M(I)=N_TRANS_1(I)
            CALL ARRANGE_ARCHIVE(I1_RECORD_M(I), N_PER_ARCH_RECORD
     &         , LAST_RECORD, N_TRANS_1(I), N_TRANS_RECORD_M(I)
     &         )
            CALL READ_ARCHIVE_ENTRY_0(IUNIT_1
     &         , N_TRANS_RECORD_1(I), I1_RECORD_1(I)
     &         , N_PER_ARCH_RECORD, N_TRANS_1(I), FREQ_LOW_1
     &         , FREQ_HIGH_1, TRANS_1
     &         )
            CALL WRITE_ARCHIVE_ENTRY_0(IUNIT_OUT
     &         , N_TRANS_RECORD_M(I), I1_RECORD_M(I)
     &         , N_PER_ARCH_RECORD, N_TRANS_1(I), FREQ_LOW_1
     &         , FREQ_HIGH_1, TRANS_1
     &         )

         ENDIF
!
      ENDDO
!
!     COMPLETE THE ARCHIVE BY ADDING THE UNMERGED ENTRIES FROM THE
!     SECOND ARCHIVE.
      N_COUNT=N_ENTRY_1
      DO J=1, N_ENTRY_2
         IF (.NOT.L_MERGED(J)) THEN
            N_COUNT=N_COUNT+1
            DATE_M(N_COUNT)=DATE_2(J)
            DATABASE_M(N_COUNT)=DATABASE_2(J)
            LBL_CODE_M(N_COUNT)=LBL_CODE_2(J)
            LINESHAPE_M(N_COUNT)=LINESHAPE_2(J)
            CUTOFF_M(N_COUNT)=CUTOFF_2(J)
            CONTINUUM_M(N_COUNT)=CONTINUUM_2(J)
            LINE_COUPLE_M(N_COUNT)=LINE_COUPLE_2(J)
            I_GAS_M(N_COUNT)=I_GAS_2(J)
            P_M(N_COUNT)=P_2(J)
            T_M(N_COUNT)=T_2(J)
            AMOUNT_M(N_COUNT)=AMOUNT_2(J)
            PARTIAL_P_M(N_COUNT)=PARTIAL_P_2(J)
            FREQ_ENTRY_LOW_M(N_COUNT)=FREQ_ENTRY_LOW_2(J)
            FREQ_ENTRY_HIGH_M(N_COUNT)=FREQ_ENTRY_HIGH_2(J)
            N_TRANS_M(N_COUNT)=N_TRANS_2(J)
            CALL ARRANGE_ARCHIVE(I1_RECORD_M(N_COUNT)
     &         , N_PER_ARCH_RECORD, LAST_RECORD, N_TRANS_2(J)
     &         , N_TRANS_RECORD_M(N_COUNT)
     &         )
            CALL READ_ARCHIVE_ENTRY_0(IUNIT_2
     &         , N_TRANS_RECORD_2(J), I1_RECORD_2(J)
     &         , N_PER_ARCH_RECORD, N_TRANS_2(J), FREQ_LOW_2
     &         , FREQ_HIGH_2, TRANS_2
     &         )
            CALL WRITE_ARCHIVE_ENTRY_0(IUNIT_OUT
     &         , N_TRANS_RECORD_M(N_COUNT), I1_RECORD_M(N_COUNT)
     &         , N_PER_ARCH_RECORD, N_TRANS_2(J), FREQ_LOW_2
     &         , FREQ_HIGH_2, TRANS_2
     &         )
         ENDIF
      ENDDO
!
!     WRITE THE HEADERS TO THE FILE: THIS CAN BE DONE LAST IN A
!     DIRECT-ACCESS FILE.
      CALL WRITE_ARCHIVE_HEADER_0(IERR, IUNIT_OUT
     &   , N_ENTRY_M
     &   , DATE_M, DATABASE_M, LBL_CODE_M
     &   , LINESHAPE_M, CUTOFF_M, CONTINUUM_M, LINE_COUPLE_M
     &   , I_GAS_M, P_M, T_M, AMOUNT_M, PARTIAL_P_M
     &   , FREQ_ENTRY_LOW_M, FREQ_ENTRY_HIGH_M
     &   , N_TRANS_RECORD_M, I1_RECORD_M, N_TRANS_M
     &   )
!
!
!     COMPLETE BY WRITING THE TOP RECORD AND CLOSING THE FILE.
      WRITE(IUNIT_OUT, REC=1) I_VERSION_M, N_ENTRY_M
      CLOSE(IUNIT_OUT)
!
!
!
      RETURN
      END
