      SUBROUTINE oldCMA_OPEN(kunit, cdfilnam, cdmode, 
     $                    kinfo, kinfo_len, kret)

      implicit none

c
c***  SUBROUTINE oldCMA_OPEN(kunit, cdfilnam, cdmode, kinfo, kinfo_len, kret)
c
c     Sami Saarinen, ECMWF, 19/8/95
c     Sami Saarinen, ECMWF, 12/11/97 : gradually becomes obsolete
c
c***  Purpose:
c
c     To open a Comprehensive Memory Array (CMA) file for read/write or append
c
c***  Externals:
c
c     PBOPEN     - Opens file (via pbio)
c     oldCMA_READ   - Reads a bit of information from DDRs
c     PBSEEK     - Repositions file to the beginning of the next DDR
c     UTIL_STAT  - Gets the length of the CMA-file (r/o) in bytes (C-routine)
c     oldCMA_REWIND - Repositions file to the beginning 
c
c***  Notes:
c
c     Coded in Fortran-90
c
c***  Input variables
      character*(*), intent(in) :: cdfilnam ! Filename
      character*(*), intent(in) :: cdmode   ! Open mode: 'r'=read, 
                                            ! 'w'=write, 'a'=append
      integer, intent(in)  :: kinfo_len     ! Length of the info vector
c***  Output variables
      integer, intent(out) :: kunit         ! PBIO file pointer
      integer, intent(out) :: kret          ! Return code
                                            !  0 = OK
                                            ! -1 = Couldn't open file
                                            ! -2 = Invalid file name
                                            ! -3 = Invalid open mode
                                            ! -4 = Invalid file length
      integer, intent(out) :: 
     $     kinfo(kinfo_len)     ! Information vector (if mode='r')
                                ! (1) = # of DDRs
                                ! (2) = total length of DDRs in dp-words
                                ! (3) = # of observations/reports
                                ! (4) = total length of report data
                                ! (5) = maximum report length
                                ! (6) = maximum observation type
c-----------------------------------------------------------------------
! === END OF INTERFACE BLOCK ===

      logical ll_open_read, ll_first_ddr, ll_file_exist
      integer iret_pbio, j, ichunk
      integer inext_ddr_len
      integer inum_ddrs, ilen_ddrs, ilen_cma, ilen_total
      real zinfo(6), ztmp(64)
      integer nobytes
      real zitem

c-----------------------------------------------------------------------
c
c     1. Determine the open mode and open the file
c

      ll_open_read = (cdmode(1:1) == 'r' .or. cdmode(1:1)  == 'R')

      if (ll_open_read .and. kinfo_len > 0)     kinfo(:) = 0

      if (ll_open_read) then
         inquire(file=cdfilnam, exist=ll_file_exist)
         if (.not. ll_file_exist) then
            kret = -1
            return
         endif
      endif

      call PBOPEN(kunit, cdfilnam, cdmode(1:1), iret_pbio)
      kret = iret_pbio

c-----------------------------------------------------------------------
c
c     2. Get some (advance) information from DDR to aid memory allocation etc.
c        ---------------------------------------------------------------------
c

      inquire(iolength=nobytes) zitem
      if (nobytes /= 8) then
         kret = -3
         return
      endif

      if (kret == 0 .and. ll_open_read .and. kinfo_len > 0) then

c
c     2.0 Get the length of the CMA-file in bytes
c

         call UTIL_STAT(kunit, ilen_total)

c
c     2.1 Loop over the all DDR-blocks found
c

         zinfo(:) = 0
         ll_first_ddr = .true.
         inum_ddrs = 0
         ilen_ddrs = 0
         ichunk = size(ztmp)

         LOOP : do
            call oldCMA_READ(kunit, ztmp, ichunk, -1, iret_pbio)
            
            if (ll_first_ddr) then
               zinfo(3) = ztmp(27) !  # of observations/reports
c***  Warning:                 ^^  index is (very much) subject to change !!!
               zinfo(4) = ztmp(51) !  total length of report data
c***  Warning:                 ^^  index is (very much) subject to change !!!
               zinfo(5) = ztmp(22) !  max len of CMA-report
c***  Warning:                 ^^  index is (very much) subject to change !!!
               zinfo(6) = ztmp(26) !  max obstype
c***  Warning:                 ^^  index is (very much) subject to change !!!
               ll_first_ddr = .false.
            endif
            
            inum_ddrs = inum_ddrs + 1       ! # of DDRs
            ilen_ddrs = ilen_ddrs + ztmp(1) ! total length of DDRs
c***  Warning:                    ^^^^^^^^^   may, but unlikely to change !!!

            inext_ddr_len = ztmp(8)
c***  Warning:                   ^  the index may change !!!

            if (inext_ddr_len <= 0) exit LOOP

            call PBSEEK(kunit, nobytes * ilen_ddrs, 0, iret_pbio)
            ichunk = 8
         enddo LOOP

         zinfo(1) = inum_ddrs
         zinfo(2) = ilen_ddrs

c
c     2.2 Check that file length agree with DDRs + CMA lengths
c

         ilen_cma = zinfo(4)

         if (ilen_total /= nobytes*(ilen_ddrs + ilen_cma)) then
            kret = -4           ! Error
         endif

c
c     2.3 Copy as much as possible from "zinfo" to "kinfo"
c

         do j=1,min(size(zinfo),kinfo_len)
            kinfo(j) = zinfo(j)
         enddo

c
c     2.4 Reposition the file to beginning as if nothing happened
c

         call oldCMA_REWIND(kunit, iret_pbio)

      endif

      return
      END SUBROUTINE oldCMA_OPEN
