C Copyright 1981-2016 ECMWF.
C
C This software is licensed under the terms of the Apache Licence
C Version 2.0 which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
C
C In applying this licence, ECMWF does not waive the privileges and immunities
C granted to it by virtue of its status as an intergovernmental organisation
C nor does it submit to any jurisdiction.
C
      SUBROUTINE BUEVAR(KERR)
C
C**** *BUEVAR*
C
C
C     PURPOSE.
C     --------
C         INITIALIZE CONSTANTS AND VARIABLES.
C
C**   INTERFACE.
C     ----------
C
C         *CALL* *BUEVAR(KERR)*
C
C     METHOD.
C     -------
C
C          NONE.
C
C
C     EXTERNALS.
C     ----------
C
C     GETENV  - GETS VALUE OF AN ENVIRONMENT VARIABLE.
C     EMOSNUM - GIVES CURRENT EMOSLIB VERSION NUMBER.
C
C     REFERENCE.
C     ----------
C
C          NONE.
C
C     AUTHOR.
C     -------
C
C          M. DRAGOSAVAC    *ECMWF*       15/03/92.
C
C
C     MODIFICATIONS.
C     --------------
C
C          NONE.
C
C
      IMPLICIT LOGICAL(L,O,G), CHARACTER*8(C,H,Y)
C
C
#     include "parameter.F"
#     include "bcomunit.F"
#     include "bcomatb.F"
#     include "bcombef.F"
#     include "bcomwork.F"
#     include "bcomwt.F"
#     include "bcomroot.F"
#     include "bcprq.F"
#     include "bcomtabload.F"
#     include "bcomtabloadc.F"
#     include "bcomoff.F"
C
      CHARACTER*256 CROOT, HDEFAULT,CPRINT
      CHARACTER*9   STD_OUT,YKNTN, CTU
      CHARACTER*24 CTABLE_LIST,CCTABBU
      CHARACTER*64 CCTABBEN
      LOGICAL OPRINT,OS4
C
      INTEGER IEMOSNM
#ifndef R_4
      REAL*8 RVIND
      REAL*8 EPS
#else
      REAL   RVIND
      REAL   EPS
#endif
C
C     EXTERNAL FUNCTIONS
C
      INTEGER EMOSNUM
      EXTERNAL EMOSNUM

#ifdef TABLE_PATH
      DATA HDEFAULT
     & / TABLE_PATH /
#else
      DATA HDEFAULT / '' /
#endif

C
C     ------------------------------------------------------------------
C*          1.   INITIALIZE VARIABLES AND CONSTANTS.
C                -----------------------------------
 100  CONTINUE
C
      IF(KERR.GT.0) RETURN
C
      NJA= 0
      M  =0
      MM =0
      N  =0
      JCV=0
      NBPW=JBPW
      NWPT=0
      NBPT=0
      NWPTB=0
      NBPTB=0
      NSTOP=2147483647
      NVIND=2147483647
      RVIND=1.7D38
      EPS=10.D-7
      NBENP=0
      NLTVNP=0
      NWWP=0
      NXXP=0
      NYYP=0
      NZZP=0
      NTC=0
      NTT=0
      NDWINC=0
      NSCAM=0
      NSCAM07=0
      NDWINC07=0
      N07=0
      NAFDW=0
      NFD=0
      NWT=0
      ODREPF=.FALSE.
      OS4=.FALSE.
      N221=0
      N40=0
      MREL=0
      NFCM=0
      MBMP=0
      MBMPL=0
      OMARKER=.FALSE.
      CROOT=' '
      NREP=0
      OREP=.false.
      N08=0
      NSSP=0
C
C     STANDARD OUTPUT UNIT NUMBER
C
      STD_OUT=' '
      YKNTN=' '
      CALL GETENV('STD_OUT',YKNTN)
      INKNTN=INDEX(YKNTN,' ')
      IF(INKNTN.EQ.1) THEN
        KNTN=6
      ELSE
        READ(YKNTN,'(I3.3)') KNTN
      END IF
C
C     CHECK SECTION 4 SIZE
      CS4=' '
      CALL GETENV('CHECK_S4',CS4)
      ILNG=INDEX(CS4,' ')
      IF(ILNG.LE.1) THEN
         OS4=.false.
      ELSEIF(CS4(1:4).EQ.'TRUE'.or.CS4(1:4).EQ.'true') then
         OS4=.true.
      END IF
C
      CALL GETENV('USE_TABLE_C',CTU)
      ILNG=INDEX(CTU,' ')
      IF(ILNG.LE.1) THEN
         OCTABLE=.false.
      ELSEIF(CTU(1:4).eq.'true'.or.CTU.eq.'TRUE') then
         OCTABLE=.true.
      END IF
      CALL GETENV('BUFR_TABLES',CROOT)
      ILNG=INDEX(CROOT,' ')
      IF(ILNG.LE.1) THEN
        ILNG=INDEX(HDEFAULT,' ') - 1
        IF(ILNG.GT.0) THEN
          CROOT = HDEFAULT(1:ILNG)//'/bufrtables/'
        ELSE
#ifdef BUFR_TABLES_PATH
           CROOT=BUFR_TABLES_PATH
           ILNG=INDEX(CROOT,' ')
#else
           CROOT='/usr/local/apps/libemos/tables/bufr/text/'
C
C         SGI/HP/SUN BUFR TABLES PATH
C
C
C         ADD VERSION NUMBER
C
          ILNG=INDEX(CROOT,' ')
          IEMOSNM = EMOSNUM(1)
          WRITE(CROOT(ILNG:ILNG+5),'(I6.6)') IEMOSNM
          CROOT(ILNG+6:) = '/'
#endif
        ENDIF
      ENDIF
      ILNG=INDEX(CROOT,' ')
      IF (ILNG.GT.1) THEN
        IF (CROOT(ILNG-1:ILNG-1).NE.'/') CROOT(ILNG:ILNG)='/'
      ENDIF
C
      CPRINT=' '
      OPRINT=.TRUE.
      CALL GETENV('PRINT_TABLE_NAMES',CPRINT)
      ILN=INDEX(CPRINT,' ')
      IF(ILN.LE.1) THEN
         OPRINT=.TRUE.
      ELSE
         IF(CPRINT(1:ILN-1).EQ.'FALSE'.OR.
     1      CPRINT(1:ILN-1).EQ.'false') OPRINT=.FALSE.
      END IF

      IF(OPRINT) THEN
C
      WRITE(KNTN,'(1H ,A)') '                  ECMWF '
      WRITE(KNTN,'(1H )')
      WRITE(KNTN,'(1H ,A,A)') '     BUFR DECODING SOFTWARE',
     1' VERSION -  7.2 '
      WRITE(KNTN,'(1H ,A)') '           1 APRIL  2007. '
      WRITE(KNTN,'(1H )')
      WRITE(KNTN,'(1H )')
      WRITE(KNTN,'(1H )')
      WRITE(KNTN,'(1H ,A)') 'Your path for bufr tables is :'
      WRITE(KNTN,'(1H ,A)')  CROOT(1:ILNG+6)
      END IF
C
      DO 101 I=1,JBPW-2
      NMASK(I)=2**I-1
 101  CONTINUE
      IF(JBPW.EQ.32) THEN
          NMASK(31)=2147483647
      END IF
C
      IF(NPMISS.LT.0.OR.NPMISS.GT.1) NPMISS=0
      IF(NPRUS.LT.0.OR.NPRUS.GT.1)   NPRUS=0
      IF(NOKEY.LT.0.OR.NOKEY.GT.1)   NOKEY=0
      IF(NOFL.LT.0.OR.NOFL.GT.1)     NOFL=0
C
      RETURN
      END

