C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MGETIJ      CONVERT LAT,LON TO I,J FOR MAP PROJECTION
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-11-16
C
C ABSTRACT: FOR MAP PROJECTION DOMAINS, CONVERT LAT,LON TO FLOATING
C   POINT I,J INDEX INTO USER'S 2-DIMENSIONAL GRID.
C
C PROGRAM HISTORY LOG:
C   99-11-16  M. BARTH    ORIGINAL VERSION
C   01-07-17  M. BARTH    PROTECT AGAINST BAD CALCULATION CASES
C                         (SEE REMARKS)
C
C USAGE:    CALL MGETIJ(LAT,LON,RI,RJ,STATUS)
C
C   INPUT ARGUMENT LIST:
C     LAT      - R       LATITUDE OF THE STATION
C     LON      - R       LONGITUDE OF THE STATION
C
C   OUTPUT ARGUMENT LIST:
C     RI       - R       I-COORDINATE OF THE STATION
C     RJ       - R       J-COORDINATE OF THE STATION
C     STATUS   - I       0 - SUCCESS, OR ERROR CODE
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    MERROR, M_LCLLIJ, M_XGRID, M_YGRID
C
C REMARKS:
C   - THE CONVENTION BEING USED ASSUMES THAT GRID POINT (1,1)
C     SPECIFIES THE SOUTHWEST CORNER OF THE DOMAIN, AND (NX,NY)
C     IS THE NORTHEAST CORNER.
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 RETURNING INV_LAT_P STATUS.
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$$$
      SUBROUTINE MGETIJ(LAT,LON,RI,RJ,STATUS)

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

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      REAL LAT,LON,RI,RJ
      INTEGER STATUS

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

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

            STATUS = INV_LAT_P
            CALL MERROR('MGETIJ: INVALID LATITUDE FOR SELECTED DOMAIN')
            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,RI,RJ)
         STATUS = SUCCESS_P

      ELSE IF(DMTYPE.EQ.1)THEN

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

            STATUS = INV_LAT_P
            CALL MERROR('MGETIJ: INVALID LATITUDE FOR SELECTED DOMAIN')
            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)

         RI = XCART/DM_U + 1
         RJ = YCART/DM_U + 1
         STATUS = SUCCESS_P

      ELSE

C     ------------------------------------------------------------------
C     NOT A MAP PROJECTION.
C     ------------------------------------------------------------------
         STATUS = INV_DMT_P
         CALL MERROR('MGETIJ: INVALID DOMAIN TYPE')

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