C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    M_MARELV    GET MARITIME ELEVATIONS
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 99-09-06
C
C ABSTRACT: GET MARITIME ELEVATIONS FROM STORED ELEVS OR GRIDDED TOPO
C
C PROGRAM HISTORY LOG:
C   99-09-06  M. BARTH         ORIGINAL PROGRAM
C   99-09-15  M. BARTH         COPIED FROM MSAS AND MODIFIED FOR MADIS.
C   02-02-01  M. BARTH         ADDED A RECTANGLE TO COVER NORTHERN
C                              CANADA LAKES (E.G., GREAT SLAVE LAKE).
C
C USAGE:    CALL M_MARELV (NBRSTA, LAT, LON, ELEV)
C
C SUBPROGRAMS CALLED:
C   M_BICUBIC, M_LCLLIJ, M_XGRID, M_YGRID
C
C INPUT ARGUMENT LIST:
C     NBRSTA   - INTEGER               NUMBER OF STATIONS TO PROCESS
C     SPVAL    - REAL                  CALLER'S MISSING DATA FLAG
C     LAT      - REAL ARRAY(NBRSTA)    STATION LATITUDES
C     LON      - REAL ARRAY(NBRSTA)    STATION LONGITUDES
C
C INPUT/OUTPUT ARGUMENT LIST:
C     ELEV     - REAL ARRAY(NBRSTA)    STATION ELEVATIONS
C
C REMARKS:
C   THIS ROUTINE IS NEEDED BECAUSE ZERO ELEVATIONS ARE GENERALLY
C   REPORTED FOR MARITIME STATIONS WHOSE ELEVATION IS UNKNOWN.
C   TWO RECTANGLES ARE DEFINED OVER NORTH AMERICA THAT A) AREN'T
C   NEAR THE OCEAN OR GULF COASTS, AND B) INCLUDE KNOWN PROBLEM 
C   AREAS -- RECTANGLE 1:  THE GREAT LAKES, IDAHO (ONE CMAN 
C   STATION ON A LAKE), LAKE WINNIPEG); RECTANGLE 2 -- NORTH
C   OF RECTANGLE 1, IN WESTERN CANADA, COVERING 4 INLAND LAKES,
C   INCLUDING THE GREAT SLAVE LAKE (WHERE WE HAD TWO PROBLEM
C   STATIONS).  ANY STATIONS WITH ZERO OR MISSING ELEVATION 
C   WITHIN THE RECTANGLES WILL BE PROCESSED BY THIS ROUTINE.  
C   "PROCESSING" MEANS INTERPOLATION FROM THE MSAS TOPOGRAPHY GRID
C   IF WITHIN THE MSAS DOMAIN, AND SET TO MISSING IF OUTSIDE THE 
C   MSAS DOMAIN.  ELEVATIONS FOR STATIONS OUTSIDE THE RECTANGLES ARE
C   NOT TOUCHED.

C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$


      SUBROUTINE M_MARELV (NBRSTA, SPVAL, LAT, LON, ELEV)



C     *****************************************************************
C     ******************* MODULE   DECLARATIONS ***********************
C     *****************************************************************

      INCLUDE 'IMPLICIT'
      INCLUDE 'MADISCON'
      INCLUDE 'MADISCOM'


C     -----------------------------------------------------------------
C     ARGUMENTS
C     -----------------------------------------------------------------
      INTEGER NBRSTA
      REAL    SPVAL
      REAL    LAT(NBRSTA)
      REAL    LON(NBRSTA)
      REAL    ELEV(NBRSTA)


C     -----------------------------------------------------------------
C     LOCAL VARIABLES
C     -----------------------------------------------------------------
      INTEGER NRECT
      PARAMETER (NRECT = 2)

      INTEGER   I,J
      REAL      M_XGRID, M_YGRID, X, Y, OI, OJ, M_BICUBIC
      REAL      XLIMN, XLIMX, YLIMN, YLIMX
      REAL      LAT_LL(NRECT), LON_LL(NRECT)
      REAL      LAT_UR(NRECT), LON_UR(NRECT)

C     -----------------------------------------------------------------
C     DATA STATEMENTS
C     (KEEP AT BOTTOM OF DECLARATIONS)
C     -----------------------------------------------------------------
      DATA LAT_LL,LON_LL/41.0,56.0,-120.0,-126.0/
      DATA LAT_UR,LON_UR/54.0,67.0,-75.5,-101.0/





C     *****************************************************************
C     ******************* BEGIN OF MODULE M_MARELV ********************
C     *****************************************************************



C     -----------------------------------------------------------------
C     SET DOMAIN LIMITS IN X,Y COORDINATES.
C     -----------------------------------------------------------------
      XLIMN = X_MIN_TOPO_P - DX_TOPO_P/2
      XLIMX = X_MAX_TOPO_P + DX_TOPO_P/2
      YLIMN = Y_MIN_TOPO_P - DY_TOPO_P/2
      YLIMX = Y_MAX_TOPO_P + DY_TOPO_P/2



C     -----------------------------------------------------------------
C     LOOP THROUGH THE STATIONS.
C     -----------------------------------------------------------------
      DO 100 I = 1, NBRSTA

C       ---------------------------------------------------------------
C       IF INSIDE A RECTANGLE, CONTINUE PROCESSING.
C       ---------------------------------------------------------------
         DO 101 J = 1, NRECT
            IF(LAT(I).GE.LAT_LL(J).AND.LAT(I).LE.LAT_UR(J).AND.
     1         LON(I).GE.LON_LL(J).AND.LON(I).LE.LON_UR(J))GO TO 102
 101     CONTINUE
         GO TO 100

C       ---------------------------------------------------------------
C       IF VALUE IS ZERO OR MISSING, CONTINUE PROCESSING.
C       ---------------------------------------------------------------
 102     IF(ELEV(I).NE.0.0.AND.ELEV(I).NE.SPVAL)GO TO 100

C       ---------------------------------------------------------------
C       IF IN THE DOMAIN, INTERPOLATE; OTHERWISE, SET AS MISSING.
C       ---------------------------------------------------------------
         IF (PROJ_TOPO_P .EQ. PS_P) THEN

            X = M_XGRID(LAT(I),LON(I),X_POLE_TOPO_P,LAT_TRUE_TOPO_P,
     1                  LON_PS_TOPO_P)
            Y = M_YGRID(LAT(I),LON(I),Y_POLE_TOPO_P,LAT_TRUE_TOPO_P,
     1                  LON_PS_TOPO_P)
            OI = X/DX_TOPO_P + 1
            OJ = Y/DY_TOPO_P + 1

         ELSE IF (PROJ_TOPO_P .EQ. LC_P) THEN

            CALL M_LCLLIJ(LAT(I),360.+LON(I),LAT_LL_TOPO_P,
     +                    360.+LON_LL_TOPO_P,
     +                    DX_TOPO_P,360.+LON_LC_TOPO_P,LAT_TAN_TOPO_P,
     +                    ROTCON_TOPO_P,OI,OJ)
            X = (OI - 1) * DX_TOPO_P
            Y = (OJ - 1) * DY_TOPO_P

         ENDIF

         IF(X.LT.XLIMN.OR.X.GT.XLIMX.OR.Y.LT.YLIMN.OR.Y.GT.YLIMX)THEN

            ELEV(I) = SPVAL

         ELSE

            ELEV(I) = M_BICUBIC(OI,OJ,1,NX_TOPO_P,NY_TOPO_P,1,GE)

         ENDIF

 100  CONTINUE

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