C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MDOMFLTR    FILTER OUT STATIONS OUTSIDE USER'S DOMAIN
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-24
C
C ABSTRACT: IF DOMAIN FILTERING HAS BEEN SELECTED, TOSS STATIONS THAT
C   ARE OUTSIDE THE USER'S DOMAIN.
C
C PROGRAM HISTORY LOG:
C   99-09-24  M. BARTH    ORIGINAL VERSION
C   01-07-17  M. BARTH    PROTECT AGAINST BAD CALCULATION CASES
C                         (SEE REMARKS)
C
C USAGE:    MDOMFLTR = MDOMFLTR (LAT,LON)
C
C   INPUT ARGUMENT LIST:
C     LAT      - R       LATITUDE OF THE STATION BEING EXAMINED
C     LON      - R       LONGITUDE OF THE STATION BEING EXAMINED
C
C   OUTPUT ARGUMENT LIST:
C     MDOMFLTR - L       .TRUE.  - IT'S OUTSIDE DOMAIN, TOSS IT
C                        .FALSE. - IT'S INSIDE DOMAIN, KEEP IT
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    M_LCLLIJ, M_XGRID, M_YGRID
C
C REMARKS:
C   - DOMAIN FILTERING FLAG IS MADISCOM'S DMTYPE:
C
C     -1  DON'T FILTER
C      0  LAMBERT CONFORMAL CONIC GRID
C      1  POLAR STEREOGRAPHIC GRID
C      2  LAT/LON CORNERS FILTERING (NOT A GRIDDED DOMAIN)
C
C   - SEE MSETDOM.F FOR ADDITIONAL COMMENTS ON THE PROJECTIONS.
C
C   - THE M_LCLLIJ ROUTINE CALCULATES THE COORDINATES IN THE GRID OF
C     A GIVEN LAT/LON COORD FOR THE LAMBERT CONFORMAL PROJECTION.
C     IT DOESN'T PROTECT AGAINST THIS CASE (WHICH CAUSES A ZERO 
C     DIVIDE):
C
C      - NORTHERN HEMISPHERE TANGENT, LAT = -90
C      - SOUTHERN HEMISPHERE TANGENT, LAT = +90
C
C     THEREFORE WE'LL HANDLE IT BY FILTERING THAT OUT OURSELVES.
C
C     LIKEWISE FOR THE POLAR STEREO CASE:  M_XGRID AND M_YGRID
C     WILL ZERO DIVIDE IF LAT = -90
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$
      LOGICAL FUNCTION MDOMFLTR (LAT,LON)

C     ------------------------------------------------------------------
C     INCLUDES AND COMMONS
C     ------------------------------------------------------------------
      INCLUDE 'IMPLICIT'
      INCLUDE 'MADISCON'
      INCLUDE 'MADISCOM'

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      REAL LAT,LON

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      REAL XC,YC,XCART,YCART,M_XGRID,M_YGRID
      EXTERNAL M_XGRID,M_YGRID

      IF(DMTYPE.EQ.-1)THEN

         MDOMFLTR = .FALSE.

      ELSE IF(DMTYPE.EQ.0.OR.DMTYPE.EQ.1)THEN

C     ------------------------------------------------------------------
C     GRIDDED MAP PROJECTIONS
C     ------------------------------------------------------------------
         IF(DMTYPE.EQ.0)THEN

C     ------------------------------------------------------------------
C     LAMBERT CONFORMAL CONIC GRID 
C     ------------------------------------------------------------------
            IF((LAT.EQ.-90.0.AND.LAT_TAN_U.GT.0).OR.
     1         (LAT.EQ.90.0.AND.LAT_TAN_U.LT.0))THEN

               MDOMFLTR = .TRUE.
               RETURN
              
            ENDIF

            CALL M_LCLLIJ(LAT,360.+LON,LAT_LL_U,360.+LON_LL_U,DM_U,
     1                  360.+LON_XX_U,LAT_TAN_U,ROTCON_U,XC,YC)
            XCART=(XC-1)*DM_U
            YCART=(YC-1)*DM_U

         ELSE IF(DMTYPE.EQ.1)THEN

C     ------------------------------------------------------------------
C     POLAR STEREOGRAPHIC GRID
C     ------------------------------------------------------------------
            IF(LAT.EQ.-90.0)THEN

               MDOMFLTR = .TRUE.
               RETURN

            ENDIF

            XCART = M_XGRID(LAT,LON,X_POLE_U,LAT_TRUE_U,LON_XX_U)
            YCART = M_YGRID(LAT,LON,Y_POLE_U,LAT_TRUE_U,LON_XX_U)

         ENDIF

         IF (XCART.LT.XLIMN_U.OR.XCART.GT.XLIMX_U.OR.
     1       YCART.LT.YLIMN_U.OR.YCART.GT.YLIMX_U)THEN

            MDOMFLTR = .TRUE.

         ELSE

            MDOMFLTR = .FALSE.

         ENDIF

      ELSE IF(DMTYPE.EQ.2)THEN

C     ------------------------------------------------------------------
C     LAT/LON CORNERS (NOT A MAP PROJECTION)
C     ------------------------------------------------------------------
         IF(LAT.GE.LAT_LL_U.AND.LAT.LE.LAT_UR_U.AND.
     1      LON.GE.LON_LL_U.AND.LON.LE.LON_UR_U)THEN

            MDOMFLTR = .FALSE.

         ELSE

            MDOMFLTR = .TRUE.

         ENDIF

      ELSE

C     ------------------------------------------------------------------
C     SHOULDN'T BE HERE UNLESS A PROGRAMMING ERROR.  THIS, OF COURSE,
C     IS IMPOSSIBLE, BUT STAY ON THE SAFE SIDE AND KEEP THE STATION.
C     ------------------------------------------------------------------
         MDOMFLTR = .FALSE.

      ENDIF

      RETURN
      END
C**---------------------------------------------------------------------
C**REQUIRED STANDARD FSL DISCLAIMER (NOVEMBER 2000)
C**---------------------------------------------------------------------
C**  OPEN SOURCE LICENSE/DISCLAIMER,  FORECAST SYSTEMS LABORATORY
C**  NOAA/OAR/FSL, 325 BROADWAY BOULDER, CO 80305
C**
C**  THIS SOFTWARE IS DISTRIBUTED UNDER THE OPEN SOURCE DEFINITION, 
C**  WHICH MAY BE FOUND AT http://www.opensource.org/osd.html.
C**
C**  IN PARTICULAR, REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS,
C**  WITH OR WITHOUT MODIFICATION, ARE PERMITTED PROVIDED THAT THE 
C**  FOLLOWING CONDITIONS ARE MET:
C**
C**  - REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THIS NOTICE, THIS LIST
C**    OF CONDITIONS AND THE FOLLOWING DISCLAIMER.
C**
C**  - REDISTRIBUTIONS IN BINARY FORM MUST PROVIDE ACCESS TO THIS 
C**    NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER, AND
C**    THE UNDERLYING SOURCE CODE.
C**
C**  - ALL MODIFICATIONS TO THIS SOFTWARE MUST BE CLEARLY DOCUMENTED, 
C**    AND ARE SOLELY THE RESPONSIBILITY OF THE AGENT MAKING THE 
C**    MODIFICATIONS.
C**
C**  - IF SIGNIFICANT MODIFICATIONS OR ENHANCEMENTS ARE MADE TO THIS
C**    SOFTWARE, THE FSL SOFTWARE POLICY MANAGER 
C**    (softwaremgr.fsl@noaa.gov) BE NOTIFIED.
C**
C**  THIS SOFTWARE AND ITS DOCUMENTATION ARE IN THE PUBLIC DOMAIN AND 
C**  ARE FURNISHED "AS IS."  THE AUTHORS, THE UNITED STATES GOVERNMENT,
C**  ITS INSTRUMENTALITIES, OFFICERS, EMPLOYEES, AND AGENTS MAKE NO 
C**  WARRANTY, EXPRESS OR IMPLIED, AS TO THE USEFULNESS OF THE SOFTWARE
C**  AND DOCUMENTATION FOR ANY PURPOSE.  THEY ASSUME NO RESPONSIBILITY 
C**  (1) FOR THE USE OF THE SOFTWARE AND DOCUMENTATION; OR (2) TO 
C**  PROVIDE TECHNICAL SUPPORT TO USERS.
C**---------------------------------------------------------------------
