C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    M_BICUBIC   CALCULATE THE INTERPOLATE OF GRID G
C   PRGMMR: PATTY MILLER     ORG: FSL/PROFS  DATE: 92-08-14
C
C ABSTRACT: FUNCTION CALCULATE THE BICUBIC INTERPOLATE OF GRID G
C   AT POINT X,Y FOR VARIABLE IVAR.
C
C
C PROGRAM HISTORY LOG:
C   92-08-14  PATTY MILLER   ORIGINAL VERSION
C   99-09-15  M. BARTH       COPIED FROM MSAS AND MODIFIED FOR MADIS.
C
C USAGE:    M_BICUBIC(  X, Y, IVAR, NX, NY, NVAR, G)
C
C   INPUT ARGUMENT LIST:
C     X        - REAL        X INDEX TO INTERPOLATE TO
C     Y        - REAL        Y INDEX TO INTERPOLATE TO
C     IVAR     - INTEGER     VARIABLE NUMBER TO INTERPOLATE
C     NX       - INTEGER     NUMBER OF POINTS IN X DIRECTION
C     NY       - INTEGER     NUMBER OF POINTS IN Y DIRECTION
C     NVAR     - INTEGER     NUMBER OF VARIABLES
C     G        - REAL ARRAY  G(NX,NY,NVAR)
C                            GRIDDED FIELD
C
C   OUTPUT ARGUMENT LIST:
C     M_BICUBIC  - REAL      BICUBIC INTERPOLATE OF G AT POINT X,Y
C                            FOR VARIABLE IVAR.
C
CC ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE: NAS 9000, VAX, UNIX
C$$$
C
      FUNCTION          M_BICUBIC(  X,
     1                              Y,
     1                              IVAR,
     1                              NX,
     1                              NY,
     1                              NVAR,
     1                              G)
C
C
C      PURPOSE: Calculate the bicubic interpolate of grid G
C               at point X,Y for variable IVAR.
C
C  ********************************************************************
C  ******************** VARIABLE TYPES AND DEMINITIONS ****************
C
C INTEGER IVAR                  | Variable number.
C 1       ,NX,NY                | # of hor grid pts in x and y directio
C 1       ,NVAR                 | # of analyzed sfc variables
C 1       ,I,J,IP1,JP1          | Scratch variables


C REAL    G(NX,NY,NVAR)         | Gridded field
C 1       ,BITERP               | Interpolated value.
C 1       ,X                    | X Index
C 1       ,Y                    | Y Index
C
C
      INCLUDE 'IMPLICIT'

      INTEGER   NX,NY,NVAR

      INTEGER   IVAR,I,J,IP1,JP1,IP2,JP2
     1           ,IP3,JP3

      REAL      M_BICUBIC
     1         ,G(NX,NY,NVAR)
     1         ,X
     1         ,Y
     1         ,XI,XI1,XI2,XI3
     1         ,YJ,YJ1,YJ2,YJ3
     1         ,FACTOR1
     1         ,FACTOR2
     1         ,FACTOR3
     1         ,FACTOR4
     1         ,FACTOR5
     1         ,FACTOR6
     1         ,FACTOR7
     1         ,FACTOR8

          I = MAX(1.,X)
          J = MAX(1.,Y)
          IP1 = MIN(I+1,NX)
          JP1 = MIN(J+1,NY)
          IP2 = MIN(I+2,NX)
          JP2 = MIN(J+2,NY)
          IP3 = MIN(I+3,NX)
          JP3 = MIN(J+3,NY)

          XI = X-I
          XI1 = X-I-1.
          XI2 = X-I-2.
          XI3 = X-I-3.

          YJ = Y-J
          YJ1 = Y-J-1.
          YJ2 = Y-J-2.
          YJ3 = Y-J-3.

          FACTOR1 = XI1*XI2*XI3/(-6.)
          FACTOR2 = XI*XI2*XI3/(2.)
          FACTOR3 = XI*XI1*XI3/(-2.)
          FACTOR4 = XI*XI1*XI2/(6.)
          FACTOR5 = YJ1*YJ2*YJ3/(-6.)
          FACTOR6 = YJ*YJ2*YJ3/(2.)
          FACTOR7 = YJ*YJ1*YJ3/(-2.)
          FACTOR8 = YJ*YJ1*YJ2/(6.)


C       Bicubic interpolation.


        M_BICUBIC =
     1                FACTOR1 * FACTOR5 * G(I  ,J  ,IVAR)
     1              + FACTOR2 * FACTOR5 * G(IP1,J  ,IVAR)
     1              + FACTOR3 * FACTOR5 * G(IP2,J  ,IVAR)
     1              + FACTOR4 * FACTOR5 * G(IP3,J  ,IVAR)
     1              + FACTOR1 * FACTOR6 * G(I  ,JP1,IVAR)
     1              + FACTOR2 * FACTOR6 * G(IP1,JP1,IVAR)
     1              + FACTOR3 * FACTOR6 * G(IP2,JP1,IVAR)
     1              + FACTOR4 * FACTOR6 * G(IP3,JP1,IVAR)
     1              + FACTOR1 * FACTOR7 * G(I  ,JP2,IVAR)
     1              + FACTOR2 * FACTOR7 * G(IP1,JP2,IVAR)
     1              + FACTOR3 * FACTOR7 * G(IP2,JP2,IVAR)
     1              + FACTOR4 * FACTOR7 * G(IP3,JP2,IVAR)
     1              + FACTOR1 * FACTOR8 * G(I  ,JP3,IVAR)
     1              + FACTOR2 * FACTOR8 * G(IP1,JP3,IVAR)
     1              + FACTOR3 * FACTOR8 * G(IP2,JP3,IVAR)
     1              + FACTOR4 * FACTOR8 * G(IP3,JP3,IVAR)


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