C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MSETDOM     SET HORIZONTAL DOMAIN FOR RETURNED STNS
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-24
C
C ABSTRACT: SET LIMITS ON THE HORIZONTAL DOMAIN.  OUTSIDE THESE LIMITS
C   STATIONS WILL BE FILTERED.
C
C PROGRAM HISTORY LOG:
C   99-09-24  M. BARTH    ORIGINAL VERSION
C   00-03-23  M. BARTH    CHANGES TO ALLOW FOR UPPER-AIR-POINT DATA
C   01-08-11  M. BARTH    CHANGED INV_INP_P TO INV_DMA_P
C
C USAGE:    CALL MSETDOM (IDMTYPE,RPAR1,RPAR2,RPAR3,RPAR4,RPAR5,RPAR6,
C                         RPAR7,IPAR1,IPAR2,STATUS)
C
C   INPUT ARGUMENT LIST:
C     IDMTYPE  - I       DOMAIN TYPE:
C                        0 - CLEAR DOMAIN SETTINGS (DON'T FILTER STNS)
C                        1 - LAT/LON CORNERS (NOT A GRID PROJECTION)
C                        2 - POLAR STEREOGRAPHIC GRID
C                        3 - LAMBERT CONFORMAL CONIC (SECANT OR TANGENT)
C
C                        * SEE REMARKS SECTION NOTE 1 FOR ARGUMENT UNITS
C
C                        IF IDMTYPE = 1 (LAT/LON CORNERS):
C
C     RPAR1    - R       LOWER LEFT LATITUDE
C     RPAR2    - R       LOWER LEFT LONGITUDE
C     RPAR3    - R       UPPER RIGHT LATITUDE
C     RPAR4    - R       UPPER RIGHT LONGITUDE
C     RPAR5    - R       UNUSED
C     RPAR6    - R       UNUSED
C     RPAR7    - R       UNUSED
C     IPAR1    - I       UNUSED
C     IPAR2    - I       UNUSED
C
C                        IF IDMTYPE = 2 (POLAR STEREOGRAPHIC):
C
C                        * SEE REMARKS SECTION NOTE 2 FOR AN EXAMPLE
C
C     RPAR1    - R       MESH LENGTH OF GRID BOX AT TRUE LATITUDE
C     RPAR2    - R       LATITUDE OF 1ST GRID PT (LOWER LEFT)
C     RPAR3    - R       LONGITUDE OF 1ST GRID PT (LOWER LEFT)
C     RPAR4    - R       ORIENTATION OF THE GRID, I.E., THE LONGITUDE
C                        VALUE OF THE MERIDIAN WHICH IS PARALLEL TO THE
C                        Y-AXIS (OR COLUMNS OF THE GRID) ALONG WHICH
C                        LATITUDE INCREASES AS THE Y-COORDINATE
C                        INCREASES (NOTE THE ORIENTATION LONGITUDE MAY,
C                        OR MAY NOT, APPEAR WITHIN THE PARTICULAR GRID)
C     RPAR5    - R       I-COORDINATE OF POLE (REAL, NOT INTEGER INDEX)
C     RPAR6    - R       J-COORDINATE OF POLE (REAL, NOT INTEGER INDEX)
C     RPAR7    - R       LATITUDE AT WHICH MESH LENGTH IS TRUE
C     IPAR1    - I       NUMBER OF POINTS ALONG X-AXIS (I DIMENSION)
C     IPAR2    - I       NUMBER OF POINTS ALONG Y-AXIS (J DIMENSION)
C
C                        IF IDMTYPE = 3 (LAMBERT CONFORMAL):
C
C                        * SEE REMARKS SECTION NOTE 3 FOR AN EXAMPLE
C
C     RPAR1    - R       MESH LENGTH OF GRID BOX AT "TRUE" LATITUDE
C                        (* SEE REMARKS SECTION NOTE 4)
C     RPAR2    - R       LATITUDE OF 1ST GRID PT (LOWER LEFT)
C     RPAR3    - R       LONGITUDE OF 1ST GRID PT (LOWER LEFT)
C     RPAR4    - R       ORIENTATION OF THE GRID, I.E., THE LONGITUDE
C                        VALUE OF THE MERIDIAN WHICH IS PARALLEL TO THE
C                        Y-AXIS (OR COLUMNS OF THE GRID) ALONG WHICH
C                        LATITUDE INCREASES AS THE Y-COORDINATE
C                        INCREASES (NOTE THE ORIENTATION LONGITUDE MAY,
C                        OR MAY NOT, APPEAR WITHIN THE PARTICULAR GRID).
C                        THIS IS ALSO THE MERIDIAN (ON THE BACK SIDE OF
C                        CONE) ALONG WHICH THE CUT IS MADE TO LAY THE
C                        CONE FLAT.
C     RPAR5    - R       THE FIRST LATITUDE FROM THE POLE AT WHICH THE
C                        CONE CUTS THE SPHERICAL EARTH
C                        * SEE REMARKS SECTION NOTE 4
C     RPAR6    - R       THE SECOND LATITUDE FROM THE POLE AT WHICH THE
C                        CONE CUTS THE SPHERICAL EARTH
C                        * SEE REMARKS SECTION NOTE 4
C     RPAR7    - R       UNUSED
C     IPAR1    - I       NUMBER OF POINTS ALONG X-AXIS (I DIMENSION)
C     IPAR2    - I       NUMBER OF POINTS ALONG Y-AXIS (J DIMENSION)
C                        * SEE REMARKS SECTION NOTE 5
C
C   OUTPUT ARGUMENT LIST:
C     STATUS   - I       0 - SUCCESS, OR ERROR CODE
C                        * SEE REMARKS SECTION NOTE 6
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    M_EQVLAT, MERROR
C
C REMARKS:
C   1. LATITUDES ARE IN DEGREES NORTH, LONGITUDES ARE IN DEGREES EAST,
C      AND THE GRID MESH LENGTH IS IN METERS.
C
C   2. EXAMPLE OF POLAR STEREOGRAPHIC ARGUMENTS USING AWIPS GRID 202
C      (NATIONAL-CONUS):
C
C      RPAR1   190500.0
C      RPAR2   7.838
C      RPAR3   -141.028
C      RPAR4   -95
C      RPAR5   33.0
C      RPAR6   45.0
C      RPAR7   60.0
C      IPAR1   65
C      IPAR2   43
C
C   3. EXAMPLE OF LAMBERT CONFORMAL ARGUMENTS USING AWIPS GRID 212
C      (REGIONAL-CONUS-DOUBLE RESOLUTION):
C
C      RPAR1   40635.25
C      RPAR2   12.19
C      RPAR3   -133.459
C      RPAR4   -95.0
C      RPAR5   25.0
C      RPAR6   25.0
C      IPAR1   185
C      IPAR2   129
C
C   4. IF RPAR5 = RPAR6, A TANGENT CONE IS USED, AND THE GRID MESH
C      LENGTH IS TRUE AT THAT LATITUDE.  IF RPAR5 .NE. RPAR6, A
C      SECANT CONE IS USED.  NOTE THAT THIS IS DONE BY CALCULATING
C      THE TANGENT EQUIVALENT OF THE SECANT, USING THE M_EQVLAT.F
C      FUNCTION (SEE THAT ROUTINE FOR A REFERENCE).  THIS MEANS
C      THAT THE GRID MESH IS TRUE AT THE LATITUDE RETURNED FROM
C      M_EQVLAT AND *NOT* TRUE AT EITHER RPAR5 OR RPAR6.  THEREFORE,
C      ANY OFFICIAL GRIB GRID USING A SECANT CONE (RPAR5 .NE. RPAR6)
C      WILL BE SLIGHTLY DISTORTED WHEN USING THE MADIS LIBRARY.  ALSO
C      NOTE, HOWEVER, THAT ALL THE LAMBERT CONFORMAL GRIDS PRODUCED
C      BY NCEP USE A TANGENT CONE (RPAR5 = RPAR6).  FINALLY, NOTE
C      THAT THIS CODE HASN'T BEEN TESTED WITH RPAR5 AND RPAR6 ON
C      OPPOSITE SIDES OF THE EQUATOR.
C
C   5. THE (I,J) OF THE GRID GOES FROM (1-IPAR1,1-IPAR2), WHERE
C      I IS THE X-DIMENSION, WITH LONGITUDE GOING FROM WEST TO EAST,
C      AND J IS THE Y-DIMENSION, WITH LATITUDE GOING FROM SOUTH TO
C      NORTH; E.G., (1,1) IS THE LOWER-LEFT OR SOUTHWEST CORNER,
C      AND (IPAR1,IPAR2) IS THE UPPER-RIGHT OR NORTHEAST CORNER.
C      THIS IS TRUE ANYWHERE ON THE GLOBE.
C
C   6. ILLEGAL INPUTS:
C
C      IDMTYPE < 0 OR > 3
C
C      IDMTYPE 1  LOWER LEFT LATITUDE IS EAST OF UPPER RIGHT LATITUDE
C                 LOWER LEFT LONGITUDE IS NORTH OF UPPER RIGHT LATITUDE
C                 INVALID LAT/LON VALUE
C
C      IDMTYPE 2  INVALID LAT/LON VALUE
C                 NEGATIVE GRID MESH LENGTH OF I,J DIMENSION
C
C      IDMTYPE 3  RPAR5 = -RPAR6
C                 RPAR6 IS CLOSER TO POLE THAN RPAR5
C                 INVALID LAT/LON VALUE
C                 NEGATIVE GRID MESH LENGTH OF I,J DIMENSION
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MSETDOM (IDMTYPE,RPAR1,RPAR2,RPAR3,RPAR4,RPAR5,RPAR6,
     1                    RPAR7,IPAR1,IPAR2,STATUS)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER IDMTYPE,IPAR1,IPAR2,STATUS
      REAL RPAR1,RPAR2,RPAR3,RPAR4,RPAR5,RPAR6,RPAR7

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER I
      REAL M_EQVLAT
      EXTERNAL M_EQVLAT

C     ------------------------------------------------------------------
C     IF NO SUBSYSTEM HAS BEEN INITIALIZED, RETURN WITH ERROR.
C     ------------------------------------------------------------------
      IF(MADIS_INIT.NE.INIT_P)THEN
         STATUS = SUB_NOTINIT_P
         CALL MERROR('MSETDOM: NO MADIS SUBSYSTEM INITIALIZED')
         RETURN
      ENDIF

C     ------------------------------------------------------------------
C     CLEAR EXISTING DOMAIN TYPE AND ASSUME INVALID INPUT.  ALSO CLEAR
C     THE LAST-TIME-READ FIELD FOR ALL MADIS DATASET TYPES.  THEN
C     PROCESS EACH DOMAIN TYPE SEPARATELY.  IF SUCCESSFUL, PUT THE
C     APPROPRIATE INFO INTO MADISCOM FOR USE BY SUBSEQUENT ROUTINES.
C     ------------------------------------------------------------------
      DMTYPE = -1
      STATUS = INV_DMA_P
      DO 1 I = 0, MAX_DTYPE_P - 1
         LAST_DTYPE_ATIME(I) = '         '
 1    CONTINUE

      IF(IDMTYPE.EQ.0)THEN

         STATUS = SUCCESS_P

      ELSE IF(IDMTYPE.EQ.1)THEN

         IF(RPAR1.LE.RPAR3.AND.RPAR2.LE.RPAR4.AND.
     1      ABS(RPAR1).LE.90.0.AND.ABS(RPAR3).LE.90.0.AND.
     2      ABS(RPAR2).LE.180.0.AND.ABS(RPAR4).LE.180.0)THEN

            DMTYPE = 2
            STATUS = SUCCESS_P
            LAT_LL_U = RPAR1
            LON_LL_U = RPAR2
            LAT_UR_U = RPAR3
            LON_UR_U = RPAR4

         ELSE

            CALL MERROR('MSETDOM: INVALID DOMAIN ARGUMENTS')

         ENDIF

      ELSE IF(IDMTYPE.EQ.2)THEN

         IF(ABS(RPAR2).LE.90.0.AND.ABS(RPAR7).LE.90.0.AND.
     1      ABS(RPAR3).LE.180.0.AND.ABS(RPAR4).LE.180.0.AND.
     2      RPAR1.GT.0.AND.IPAR1.GT.0.AND.IPAR2.GT.0)THEN

            DMTYPE = 1
            STATUS = SUCCESS_P
            DM_U = RPAR1
            NX_U = IPAR1
            NY_U = IPAR2
            LAT_LL_U = RPAR2
            LON_LL_U = RPAR3
            LON_XX_U = RPAR4
            X_POLE_U = (RPAR5 - 1.0) * DM_U
            Y_POLE_U = (RPAR6 - 1.0) * DM_U
            LAT_TRUE_U = RPAR7
            IF(LAT_TRUE_U.GE.0)THEN
               LAT_PS_U = 90.0
            ELSE
               LAT_PS_U = -90.0
            ENDIF
            ROTCON_U = 1.0
            XLIMN_U = 0.0 - DM_U/2
            XLIMX_U = (NX_U - 1) * DM_U + DM_U/2
            YLIMN_U = 0.0 - DM_U/2
            YLIMX_U = (NY_U - 1) * DM_U + DM_U/2

         ELSE

            CALL MERROR('MSETDOM: INVALID DOMAIN ARGUMENTS')

         ENDIF

      ELSE IF(IDMTYPE.EQ.3)THEN

         IF(ABS(RPAR2).LE.90.0.AND.ABS(RPAR5).LE.90.0.AND.
     1      ABS(RPAR6).LE.90.0.AND.ABS(RPAR3).LE.180.0.AND.
     2      ABS(RPAR4).LE.180.0.AND.RPAR1.GT.0.AND.IPAR1.GT.0.AND.
     3      IPAR2.GT.0.AND.RPAR5.NE.-RPAR6.OR.
     4      ABS(RPAR6).LT.ABS(RPAR5))THEN

            DMTYPE = 0
            STATUS = SUCCESS_P
            DM_U = RPAR1
            NX_U = IPAR1
            NY_U = IPAR2
            LAT_LL_U = RPAR2
            LON_LL_U = RPAR3
            LON_XX_U = RPAR4
            IF(RPAR5.EQ.RPAR6)THEN
               LAT_TAN_U = RPAR5
            ELSE
               LAT_TAN_U = M_EQVLAT(RPAR5,RPAR6)
            ENDIF
            ROTCON_U = SIN(LAT_TAN_U*RADDEG_P)
            XLIMN_U = 0.0 - DM_U/2
            XLIMX_U = (NX_U - 1) * DM_U + DM_U/2
            YLIMN_U = 0.0 - DM_U/2
            YLIMX_U = (NY_U - 1) * DM_U + DM_U/2

         ELSE

            CALL MERROR('MSETDOM: INVALID DOMAIN ARGUMENTS')

         ENDIF

      ELSE

         CALL MERROR('MSETDOM: INVALID DOMAIN TYPE')
         STATUS = INV_DMT_P

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