!-------------------------------------- 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 --------------------------------------
!

      function isetflag(cdfam,kodtyp,kvnam,zlev,zbgchk,lmodif1020) 1
*
      IMPLICIT NONE
#if defined (DOC)
*
***s/r isetflag  - SET BACKGROUND CHECK FLAGS
*
*
*Author  : P. Koclas *CMC/CMSV  September 1998
*Revision:
*           P. KOclas
*           - adjust criterias for winds ( tuneing for background check)
*           - 02/2002 - adjust criterias for dropsonde  winds
*           - 02/2006  -adjust criterias for radiosonde < 20Mbars
*
*           S. Macpherson ARMA/MRD September 2009
*           - added criteria for GB-GPS ZTD observations
*
**    Purpose:  - Set BACKGROUND CHECK FLAGS According
*                 to values set in a table
*                 Original values in table come from ecmwf
*
*     Arguments:
*     KVNAM= VARIABLE NAME ( BURP )
*     KODTYP=BURP CODE TYPE
*     CDFAM= FAMILY  NAME ( 'UA' , 'AI'   ...etc.. )
*     ZLEV = LEVEL
*     zbgchk=NORMALIZED BACKGROUND DEPARTURE
*     lmodif1020=switch to activate special criteria for backound check (*ua 10-20 mb)
*
#endif
#include "pardim.cdk"
#include "comdim.cdk"
#include "comgem.cdk"
#include "comnumbr.cdk"
      integer isetflag
      integer kvnam,kodtyp
      real*8 zlev
      real*8 zbgchk
      character*2 cdfam
      logical lmodif1020
ccc
      real*8 zgzcrit(3),zttcrit(3),zuvcrit(3),zescrit(3),zdzcrit(3)
     +      ,zpscrit(3),zpncrit(3),ztscrit(3),zswcrit(3),zzdcrit(3)
C
C-------------------------------------------------------------------------------
C
      isetflag=0
C
      if(cvcord .eq. 'ETAGE') then
         zttcrit(1) = 5.00D0
         zttcrit(2) = 10.00D0
         zttcrit(3) = 12.00D0
*
         zuvcrit(1) = 8.00D0
         zuvcrit(2) = 14.00D0
         zuvcrit(3) = 20.00D0
*
         zescrit(1) = 9.00D0
         zescrit(2) = 16.00D0
         zescrit(3) = 25.00D0
*
         zpscrit(1) = 9.00D0
         zpscrit(2) = 16.00D0
         zpscrit(3) = 25.00D0
*
         zpncrit(1) = 9.00D0
         zpncrit(2) = 16.00D0
         zpncrit(3) = 25.00D0
*
         zswcrit(1) = 8.00D0
         zswcrit(2) = 14.00D0
         zswcrit(3) = 20.00D0
*
         ztscrit(1) = 2.00D0
         ztscrit(2) = 10.00D0
         ztscrit(3) = 12.00D0
*
         zdzcrit(1) = 2.25D0
         zdzcrit(2) = 5.06D0
         zdzcrit(3) = 7.56D0
*
         zgzcrit(1) = 12.25D0
         zgzcrit(2) = 25.00D0
         zgzcrit(3) = 36.00D0
*
         if ( cdfam .eq. 'UA') then
             if ( kodtyp .eq. 37 ) then
               zuvcrit(2)=18.
             else
               zuvcrit(2)=14.
             endif
*
*        ------>>>   TEMPORARILY MODIFY REJECTION LIMITs <<<--------------
*           FOR RADIOSONDE DATA ABOVE 20Mbars
*
             if ( lmodif1020 ) then
                if ( zlev .le. 2000. .and. zlev .gt. 1000. )  then
                  zuvcrit(2)=17.
                  zuvcrit(3)=20.
*
                  zttcrit(2)=17.
                  zttcrit(3)=20.
                endif
                if ( zlev .le. 1000. )  then
                  zuvcrit(2)=20.
                  zuvcrit(3)=23.
*
                  zttcrit(2)=23.
                  zttcrit(3)=26.
                endif
             endif
         endif
*
         zzdcrit(1) = 9.00D0
         zzdcrit(2) = 16.00D0
         zzdcrit(3) = 25.00D0
*
       endif
      if(cvcord(1:3).eq.'MAM' .or.cvcord.eq.'GEMHYB' ) then
         zttcrit(1) = 9.00D0
         zttcrit(2) = 16.00D0
         zttcrit(3) = 25.00D0
*
         zuvcrit(1) = 10.00D0
         zuvcrit(2) = 20.00D0
         zuvcrit(3) = 30.00D0
*
         zescrit(1) = 10.00D0
         zescrit(2) = 20.00D0
         zescrit(3) = 30.00D0
*
         zpscrit(1) = 9.00D0
         zpscrit(2) = 16.00D0
         zpscrit(3) = 25.00D0
*
         zpncrit(1) = 10.00D0
         zpncrit(2) = 20.00D0
         zpncrit(3) = 30.00D0
*
         zswcrit(1) = 10.00D0
         zswcrit(2) = 20.00D0
         zswcrit(3) = 30.00D0
*
         ztscrit(1) = 5.00D0
         ztscrit(2) = 25.00D0
         ztscrit(3) = 30.00D0
*
         zdzcrit(1) = 2.25D0
         zdzcrit(2) = 5.06D0
         zdzcrit(3) = 7.56D0
*
         zgzcrit(1) = 12.25D0
         zgzcrit(2) = 25.00D0
         zgzcrit(3) = 36.00D0
*
         zzdcrit(1) = 9.00D0
         zzdcrit(2) = 16.00D0
         zzdcrit(3) = 25.00D0
*
         if ( kodtyp .eq. 37 ) then
           zuvcrit(2)=25.
         else
           zuvcrit(2)=20.
         endif
       endif
C
C     SET FLAG FOR HEIGHTS
C
      if ( kvnam .eq. NEGZ ) then
         if (      zbgchk .gt. zgzcrit(1) .and. zbgchk .lt. zgzcrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zgzcrit(2) .and. zbgchk .lt. zgzcrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zgzcrit(3) )then
              isetflag =3
         endif
      endif
C
C     SET FLAG FOR TEMPERATURE
C
      if ( kvnam .eq. NETT ) then
         if (      zbgchk .gt. zttcrit(1) .and. zbgchk .lt. zttcrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zttcrit(2) .and. zbgchk .lt. zttcrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zttcrit(3) )then
              isetflag =3
         endif
      endif
C
C     SET FLAG FOR SATEMS
C
      if ( kvnam .eq. NEDZ ) then
         if (      zbgchk .gt. zdzcrit(1) .and. zbgchk .lt. zdzcrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zdzcrit(2) .and. zbgchk .lt. zdzcrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zdzcrit(3) )then
              isetflag =3
         endif
      endif
C
C     SET FLAG FOR WIND COMPONENTS
C
      if ( kvnam .eq. NEUU .or. kvnam .eq. NEVV ) then
         if (      zbgchk .gt. zuvcrit(1) .and. zbgchk .lt. zuvcrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zuvcrit(2) .and. zbgchk .lt. zuvcrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zuvcrit(3) )then
              isetflag =3
         endif
      endif
C
C     SET FLAG FOR SURFACE WIND COMPONENTS
C
      if ( kvnam .eq. NEUS .or. kvnam .eq. NEVS ) then
         if (      zbgchk .gt. zswcrit(1) .and. zbgchk .lt. zswcrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zuvcrit(2) .and. zbgchk .lt. zswcrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zswcrit(3) )then
              isetflag =3
         endif
      endif
C
C     SET FLAG FOR DEW POINT DEPRESSION
C
      if ( kvnam .eq. NEES ) then
         if (      zbgchk .gt. zescrit(1) .and. zbgchk .lt. zescrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zescrit(2) .and. zbgchk .lt. zescrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zescrit(3) )then
              isetflag =3
         endif
      endif
C
C     SET FLAG FOR SURFACE PRESSURE
C
      if ( kvnam .eq. NEPS ) then
         if (      zbgchk .gt. zpscrit(1) .and. zbgchk .lt. zpscrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zpscrit(2) .and. zbgchk .lt. zpscrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zpscrit(3) )then
              isetflag =3
         endif
      endif
ccc
C
C     SET FLAG FOR MEAN SEA LEVEL PRESSURE
C
      if ( kvnam .eq. NEPN ) then
         if (      zbgchk .gt. zpncrit(1) .and. zbgchk .lt. zpncrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zpncrit(2) .and. zbgchk .lt. zpncrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zpncrit(3) )then
              isetflag =3
         endif
      endif
ccc
C
C     SET FLAG FOR SURFACE TEMPERATURE
C
      if ( kvnam .eq. NETS ) then
         if (      zbgchk .gt. ztscrit(1) .and. zbgchk .lt. ztscrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. ztscrit(2) .and. zbgchk .lt. ztscrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. ztscrit(3) )then
              isetflag =3
         endif
      endif
ccc
C
C     SET FLAG FOR GB-GPS ZENITH DELAY
C
      if ( kvnam .eq. NEZD ) then
         if (      zbgchk .gt. zzdcrit(1) .and. zbgchk .lt. zzdcrit(2) ) then
           isetflag=1
         else if ( zbgchk .gt. zzdcrit(2) .and. zbgchk .lt. zzdcrit(3) ) then
            isetflag=2
         else if ( zbgchk .ge. zzdcrit(3) )then
              isetflag =3
         endif
      endif
C
C-------------------------------------------------------------------------------
C
      return
      end