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

      implicit none

c
c***  SUBROUTINE BUFR_OPEN(kunit, cdfilnam, cdmode, kinfo, kinfo_len, kret)
c
c     Sami Saarinen, ECMWF, 19/10/95
c
c***  Purpose:
c
c     To open BUFR file for read/write or append
c
c***  Externals:
c
c     PBOPEN      - Opens file (via pbio)
c     BUFR_STAT   - Gets BUFR-file length in bytes
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 BUFR-file
      integer, intent(out) :: 
     $     kinfo(kinfo_len)     ! Information vector (if mode='r')
                                ! (1) = BUFR-file length in bytes
c-----------------------------------------------------------------------
! === END OF INTERFACE BLOCK ===

      logical ll_open_read, ll_buffer_data, ll_file_exist
      integer j ,iret_pbio
      integer ifile_size, iedition
      integer i_info(2), i_tmp(8)

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

      if (len_trim(cdmode) > 1) then
#if defined(VPP5000) || defined(NECSX)
         ll_buffer_data = .FALSE.
#else
         ll_buffer_data = (cdmode(2:2) == 'B')
#endif
         if (ll_buffer_data) then
            if (ll_open_read) then
               call UTIL_STAT(kunit, ifile_size)
               call UTIL_ALLOC_IOBUF(kunit, ifile_size, iret_pbio)
            else
               call UTIL_ALLOC_IOBUF(kunit, -1, iret_pbio)
            endif
            kret = iret_pbio
         endif
      else
         ll_buffer_data = .FALSE.
      endif

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

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

c
c     2.1 Get file size in bytes
c

         ifile_size = 0
         iedition   = 0

         call UTIL_STAT(kunit, ifile_size)

c         if (ifile_size >= 8) then
c            call PBREAD2(kunit, i_tmp, size(tmp), iret_pbio)
c            if 
c            call PBSEEK(kunit, 0, 0, iret_pbio)
c         endif

         i_info(1) = ifile_size
c         i_info(2) = iedition
         i_info(2) = 2

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

         if (mod(ifile_size,2) /= 0) then
            kret = -4
         endif

      endif

      return
      END SUBROUTINE oldBUFR_OPEN

