!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer,
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer
!version 3 or (at your option) any later version that should be found at:
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software;
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec),
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
!

      SUBROUTINE FLAGHU(PVALUES,KLIST,KFLAGS,KELE,KVAL,KNT,KINDEX,KRVAL) 1
#if defined (DOC)
************************************************************************
*
***s/r FLAGHU - DETERMINE FLAG VALUE FOR LQ
*
*Author  : D. Anselmo *ARMA/MSC October 2004
*Revision:
*
*
*    PURPOSE:  - Determine flag value for ln q from es, tt flags.
*                  - test bits 8,9,18 from es,tt
*                  - for bit 18 ON, set LQ missing ONLY for OMP and OMA
*                  - for LQ non-missing, set bit 10 ON
*
*    ARGUMENTS:
*     INPUT:
*           -KLIST   : LIST OF BUFR ELEMENTS
*           -KELE    : NUMBER OF ELEMENTS IN DATA BLOCK
*           -KVAL    : NUMBER OF LEVELS IN DATA BLOCK
*           -KNT     : THIRD DIMENSION OF DATA BLOCK
*           -KINDEX  : THIRD DIMENSION INDEX OF DATA BLOCK
*           -KRVAL   : INDEX OF ELEMENT IN CMA LIST
*                         ( 1 ---> OBS ERROR , 2 ---> O-A )
*
*    INPUT/OUTPUT:
*           -PVALUES : DATA BLOCK
*           -KFLAGS  : QUALITY CONTROL FLAGS
*
************************************************************************
#endif
      IMPLICIT NONE
*
#include "comlun.cdk"
#include "comdimo.cdk"
#include "comoabdy.cdk"
#include "comoahdr.cdk"
#include "comoba.cdk"
#include "cparbrp.cdk"
#include "comnumbr.cdk"
*
      INTEGER, INTENT(IN)    :: KELE,KVAL,KNT,KINDEX,KRVAL
      INTEGER, INTENT(IN)    :: KLIST(KELE)
      INTEGER, INTENT(INOUT) :: KFLAGS(KELE,KVAL,KNT)
      REAL*8,  INTENT(INOUT) :: PVALUES(KELE,KVAL,KNT)
*
      INTEGER JI,JJ,IK
      INTEGER, DIMENSION(KVAL) :: LQFLAG
*
      LOGICAL LLPRINT
*
cc      LLPRINT = .TRUE.
      LLPRINT = .FALSE.
*
      IF (LLPRINT) THEN
         write(nulout,*)' --- flaghu -----'
         write(nulout,*)' --- KLIST  -----',KLIST
      ENDIF
      IK = KINDEX
      lqflag(:) = 0

      DO JI=1,KELE
         IF ( KLIST(JI).EQ.NETT .OR. KLIST(JI).EQ.NEES .OR.
     &        KLIST(JI).EQ.NETS .OR. KLIST(JI).EQ.NESS ) THEN
            IF (LLPRINT) write(nulout,*) ' --KLIST(JI)= ',KLIST(JI)

            DO JJ=1,KVAL
               if ( btest ( KFLAGS(JI,JJ,IK), 8 )  ) then
                  lqflag(jj) = ibset( lqflag(jj), 8 )
               endif
               if ( btest ( KFLAGS(JI,JJ,IK), 9 )  ) then
                  lqflag(jj) = ibset( lqflag(jj), 9 )
               endif
               if ( btest ( KFLAGS(JI,JJ,IK), 18 )  ) then
                  lqflag(jj) = ibset( lqflag(jj), 18 )
               endif
            ENDDO

         ENDIF
      ENDDO

      DO JI=1,KELE
         IF ( KLIST(JI).EQ.NEHU .OR. KLIST(JI).EQ.NEHS ) THEN
            IF (LLPRINT) write(nulout,*) ' --KLIST(JI)= ',KLIST(JI)

            DO JJ=1,KVAL
               IF (   btest ( lqflag(jj), 18 ) .AND.
     &              ( KRVAL .EQ. NCMOMF  .OR. KRVAL .EQ. NCMOMA ) ) THEN
                  PVALUES(JI,JJ,IK) = PPMIS
               ENDIF
               IF ( PVALUES(JI,JJ,IK) .NE. PPMIS ) THEN
                  lqflag(jj) = ibset( lqflag(jj), 10 )
               ENDIF
               KFLAGS(JI,JJ,IK) = lqflag(jj)
            ENDDO

         ENDIF
      ENDDO
*
      RETURN
*
      END