*IF DEF,OCEAN                                                              OLBCGIL1.2      
C *****************************COPYRIGHT******************************     OLBCGIL1.3      
C (c) CROWN COPYRIGHT 1998, METEOROLOGICAL OFFICE, All Rights Reserved.    OLBCGIL1.4      
C                                                                          OLBCGIL1.5      
C Use, duplication or disclosure of this code is subject to the            OLBCGIL1.6      
C restrictions as set forth in the contract.                               OLBCGIL1.7      
C                                                                          OLBCGIL1.8      
C                Meteorological Office                                     OLBCGIL1.9      
C                London Road                                               OLBCGIL1.10     
C                BRACKNELL                                                 OLBCGIL1.11     
C                Berkshire UK                                              OLBCGIL1.12     
C                RG12 2SZ                                                  OLBCGIL1.13     
C                                                                          OLBCGIL1.14     
C If no contract has been raised with this copy of the code, the use,      OLBCGIL1.15     
C duplication or disclosure of it is strictly prohibited.  Permission      OLBCGIL1.16     
C to do so must first be obtained in writing from the Head of Numerical    OLBCGIL1.17     
C Modelling at the above address.                                          OLBCGIL1.18     
C ******************************COPYRIGHT******************************    OLBCGIL1.19     
!+ Calculate Gill open boundary condition                                  OLBCGIL1.20     
!                                                                          OLBCGIL1.21     

      subroutine o_lbc_gill(ncols,nrows,n_cols_bdy,n_rows_bdy,              1OLBCGIL1.22     
     &     levn,field,                                                     OLBCGIL1.23     
     &     levels_dataset,xgrid,ygrid,f_type,                              OLBCGIL1.24     
     &     l_obdy_north,l_obdy_east,l_obdy_south,l_obdy_west,              OLBCGIL1.25     
     &     bdy_n, bdy_e, bdy_s, bdy_w,                                     OLBCGIL1.26     
     &     icode,cmessage)                                                 OLBCGIL1.27     
                                                                           OLBCGIL1.28     
      implicit none                                                        OLBCGIL1.29     
!                                                                          OLBCGIL1.30     
!                                                                          OLBCGIL1.31     
! Purpose: This routine creates open north and/or south                    OLBCGIL1.32     
!          boundaries using the implicit Gill scheme.                      OLBCGIL1.33     
!          The scheme was devised for use in tropical ocean                OLBCGIL1.34     
!          models. See UMDP No. 48 for details on the Gill                 OLBCGIL1.35     
!          scheme.                                                         OLBCGIL1.36     
!                                                                          OLBCGIL1.37     
! Author : Catherine Jones                                                 OLBCGIL1.38     
!                                                                          OLBCGIL1.39     
! History                                                                  OLBCGIL1.40     
! Model    Date     Modification history from model version 4.5            OLBCGIL1.41     
! version                                                                  OLBCGIL1.42     
!  4.5   17/6/98    New subroutine/deck. C.Jones,S.Ineson                  OLBCGIL1.43     
!                                                                          OLBCGIL1.44     
!                                                                          OLBCGIL1.45     
! Subroutine Arguments:                                                    OLBCGIL1.46     
      integer ncols           ! IN number of columns                       OLBCGIL1.47     
      integer nrows           ! IN number of rows                          OLBCGIL1.48     
      integer n_cols_bdy      ! IN number of cols in boundary data         OLBCGIL1.49     
      integer n_rows_bdy      ! IN number of rows in boundary data         OLBCGIL1.50     
      integer levn            ! IN present level number                    OLBCGIL1.51     
                                                                           OLBCGIL1.52     
      real field(ncols,nrows)       ! IN/OUT field to be updated           OLBCGIL1.53     
      real levels_dataset(ncols,nrows) ! IN levels dataset for this        OLBCGIL1.54     
                                       ! field type                        OLBCGIL1.55     
                                                                           OLBCGIL1.56     
      real xgrid(ncols)              ! IN grid spacing along row           OLBCGIL1.57     
      real ygrid(nrows)              ! IN grid spacing along columns       OLBCGIL1.58     
                                                                           OLBCGIL1.59     
      integer f_type                 ! IN field type indicator             OLBCGIL1.60     
                                                                           OLBCGIL1.61     
      logical l_obdy_north           ! IN T=> update north lateral bdy     OLBCGIL1.62     
      logical l_obdy_east            ! IN T=> update east lateral bdy      OLBCGIL1.63     
      logical l_obdy_south           ! IN T=> update south lateral bdy     OLBCGIL1.64     
      logical l_obdy_west            ! IN T=> update west lateral bdy      OLBCGIL1.65     
                                                                           OLBCGIL1.66     
      real bdy_n(n_cols_bdy)  ! IN north part of boundary data             OLBCGIL1.67     
      real bdy_e(n_rows_bdy)  ! IN east part of boundary data              OLBCGIL1.68     
      real bdy_s(n_cols_bdy)  ! IN south part of boundary data             OLBCGIL1.69     
      real bdy_w(n_rows_bdy)  ! IN west part of boundary data              OLBCGIL1.70     
                                                                           OLBCGIL1.71     
      integer icode             ! OUT Return code                          OLBCGIL1.72     
      character*(80) cmessage   ! OUT Error message                        OLBCGIL1.73     
                                                                           OLBCGIL1.74     
!  Global variables                                                        OLBCGIL1.75     
*CALL UMSCALAR                ! for obdy_gill_mu and obdy_gill_lamda       OLBCGIL1.76     
*CALL PARVARS                                                              OLBCGIL1.77     
*CALL COCNINDX                                                             OLBCGIL1.78     
                                                                           OLBCGIL1.79     
*IF -DEF,MPP                                                               OLBCGIL1.80     
      INTEGER                                                              OLBCGIL1.81     
     &   fld_type_p           ! indicates a grid on P points               OLBCGIL1.82     
     &,  fld_type_u           ! indicates a grid on U points               OLBCGIL1.83     
     &,  fld_type_unknown     ! indicates a non-standard grid.             OLBCGIL1.84     
      PARAMETER (                                                          OLBCGIL1.85     
     &   fld_type_p=1                                                      OLBCGIL1.86     
     &,  fld_type_u=2                                                      OLBCGIL1.87     
     &,  fld_type_unknown=-1)                                              OLBCGIL1.88     
                                                                           OLBCGIL1.89     
      integer Offx, Offy                                                   OLBCGIL1.90     
      logical atright, atleft, atbase, attop                               OLBCGIL1.91     
*ENDIF                                                                     OLBCGIL1.92     
                                                                           OLBCGIL1.93     
! Local scalars                                                            OLBCGIL1.94     
                                                                           OLBCGIL1.95     
      integer i             ! local loop counter                           OLBCGIL1.96     
      integer j             ! local loop counter                           OLBCGIL1.97     
                                                                           OLBCGIL1.98     
      real x                ! local constant                               OLBCGIL1.99     
      real y                ! local constant                               OLBCGIL1.100    
      real z                ! local constant                               OLBCGIL1.101    
                                                                           OLBCGIL1.102    
!-------------------------------------------------------------------       OLBCGIL1.103    
CL 0.0 Set grid off-sets and indicators showing if domain lies next to     OLBCGIL1.104    
CL     each model boundary; non-mpp settings for offsets are zero and      OLBCGIL1.105    
CL     for boundary indicators are true. This allows the same code         OLBCGIL1.106    
CL     to be used for MPP and non-MPP cases.                               OLBCGIL1.107    
CL                                                                         OLBCGIL1.108    
                                                                           OLBCGIL1.109    
*IF -DEF,MPP                                                               OLBCGIL1.110    
      Offx = 0                                                             OLBCGIL1.111    
      Offy = 0                                                             OLBCGIL1.112    
      atright = .true.                                                     OLBCGIL1.113    
      atleft  = .true.                                                     OLBCGIL1.114    
      atbase  = .true.                                                     OLBCGIL1.115    
      attop   = .true.                                                     OLBCGIL1.116    
*ENDIF                                                                     OLBCGIL1.117    
                                                                           OLBCGIL1.118    
C 1.0 For tracer data update boundaries using the Gill scheme              OLBCGIL1.119    
                                                                           OLBCGIL1.120    
      if (f_type .eq. fld_type_p) then                                     OLBCGIL1.121    
                                                                           OLBCGIL1.122    
C 1.1 Northern boundary                                                    OLBCGIL1.123    
                                                                           OLBCGIL1.124    
        if (l_obdy_north .and. atbase) then                                OLBCGIL1.125    
                                                                           OLBCGIL1.126    
         x=1.0 - (1.0 - obdy_gill_mu) * obdy_gill_lamda * ygrid(j_jmt)     OLBCGIL1.127    
         y=1.0 + (1.0 - obdy_gill_mu) * obdy_gill_lamda * ygrid(j_jmt)     OLBCGIL1.128    
         z=2.0 * obdy_gill_lamda * ygrid(j_jmt)                            OLBCGIL1.129    
                                                                           OLBCGIL1.130    
           do i = 1, ncols                                                 OLBCGIL1.131    
                                                                           OLBCGIL1.132    
            field(i,j_jmt) =                                               OLBCGIL1.133    
     &       (1./x)*(field(i,j_jmt-2)*y +                                  OLBCGIL1.134    
     &       z*(obdy_gill_mu*field(i,j_jmt-1) -                            OLBCGIL1.135    
     &       bdy_n(i)))                                                    OLBCGIL1.136    
                                                                           OLBCGIL1.137    
                                                                           OLBCGIL1.138    
            if (levels_dataset(i,j_jmt-1) .lt. levn) then                  OLBCGIL1.139    
                                                                           OLBCGIL1.140    
              field(i,j_jmt) = 0.0          ! this is a land point         OLBCGIL1.141    
                                                                           OLBCGIL1.142    
            endif                                                          OLBCGIL1.143    
                                                                           OLBCGIL1.144    
           enddo                                                           OLBCGIL1.145    
                                                                           OLBCGIL1.146    
         endif          ! Northern boundary                                OLBCGIL1.147    
                                                                           OLBCGIL1.148    
C 1.2 Southern boundary                                                    OLBCGIL1.149    
                                                                           OLBCGIL1.150    
                                                                           OLBCGIL1.151    
         if (l_obdy_south .and. attop) then                                OLBCGIL1.152    
                                                                           OLBCGIL1.153    
           x = 1.0 - ( 1.0-obdy_gill_mu )*obdy_gill_lamda*ygrid(j_1)       OLBCGIL1.154    
           y = 1.0 + ( 1.0-obdy_gill_mu )*obdy_gill_lamda*ygrid(j_1)       OLBCGIL1.155    
           z = 2.0 * obdy_gill_lamda * ygrid(j_1)                          OLBCGIL1.156    
                                                                           OLBCGIL1.157    
           do i = 1, ncols                                                 OLBCGIL1.158    
                                                                           OLBCGIL1.159    
            field(i,j_1) = (1./x)*(field(i,j_1+2)*y +                      OLBCGIL1.160    
     &                   z*(obdy_gill_mu*field(i,j_1+1) - bdy_s(i)))       OLBCGIL1.161    
                                                                           OLBCGIL1.162    
                                                                           OLBCGIL1.163    
            if (levels_dataset(i,j_1+1) .lt. levn) then                    OLBCGIL1.164    
                                                                           OLBCGIL1.165    
              field(i,j_1) = 0.0          ! this is a land point           OLBCGIL1.166    
                                                                           OLBCGIL1.167    
            endif                                                          OLBCGIL1.168    
                                                                           OLBCGIL1.169    
           enddo                                                           OLBCGIL1.170    
                                                                           OLBCGIL1.171    
         endif          ! southern boundary                                OLBCGIL1.172    
                                                                           OLBCGIL1.173    
         if (l_obdy_east .or. l_obdy_east) then                            OLBCGIL1.174    
           icode=1                                                         OLBCGIL1.175    
           write(6,*)' OLBCGIL1: E/W code not implemented for GILL'        OLBCGIL1.176    
           cmessage=' OLBCGIL1:  E/W code not implemented for GILL'        OLBCGIL1.177    
         endif                                                             OLBCGIL1.178    
                                                                           OLBCGIL1.179    
      endif                  ! updating tracer fields                      OLBCGIL1.180    
                                                                           OLBCGIL1.181    
      return                                                               OLBCGIL1.182    
                                                                           OLBCGIL1.183    
      end                                                                  OLBCGIL1.184    
*ENDIF                                                                     OLBCGIL1.185