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

      subroutine o_lbc_ztd(ncols,nrows,n_cols_bdy,n_rows_bdy,              OLBCZTD1.22     
     &     lseg,isz,iez,                                                   OLBCZTD1.23     
     &     d1_field,rimwidth,                                              OLBCZTD1.24     
     &     l_obdy_north,l_obdy_east,l_obdy_south,l_obdy_west,              OLBCZTD1.25     
     &     bdy_n, bdy_e, bdy_s, bdy_w,                                     OLBCZTD1.26     
     &     icode, cmessage)                                                OLBCZTD1.27     
                                                                           OLBCZTD1.28     
      implicit none                                                        OLBCZTD1.29     
!                                                                          OLBCZTD1.30     
!                                                                          OLBCZTD1.31     
! Purpose: This routine sets values for ztd for  open north/east/          OLBCZTD1.32     
!          south/west boundaries. It is used by limited area               OLBCZTD1.33     
!          ocean models with a rigid lid and barotropic streamfn.          OLBCZTD1.34     
!                                                                          OLBCZTD1.35     
! Author : Mike Bell                                                       OLBCZTD1.36     
!                                                                          OLBCZTD1.37     
! History:                                                                 OLBCZTD1.38     
!  Model    Date       Modification history from model version 4.5         OLBCZTD1.39     
!  version                                                                 OLBCZTD1.40     
!   4.5   28/08/98    New subroutine/deck. M.Bell, D.Storkey               OLBCZTD1.41     
!                                                                          OLBCZTD1.42     
! Subroutine arguments:                                                    OLBCZTD1.43     
                                                                           OLBCZTD1.44     
      integer ncols           ! IN number of columns on tracer grid        OLBCZTD1.45     
      integer nrows           ! IN number of rows on tracer grid           OLBCZTD1.46     
      integer n_cols_bdy      ! IN number of cols in boundary data         OLBCZTD1.47     
      integer n_rows_bdy      ! IN number of rows in boundary data         OLBCZTD1.48     
      integer lseg            ! IN max number of segments on a row         OLBCZTD1.49     
      integer isz(nrows,lseg) ! IN ztd segment start indices               OLBCZTD1.50     
      integer iez(nrows,lseg) ! IN ztd segment end indices                 OLBCZTD1.51     
                                                                           OLBCZTD1.52     
      real d1_field(ncols*nrows) ! IN/OUT section of the D1 array to       OLBCZTD1.53     
                                 ! be updated                              OLBCZTD1.54     
      integer rimwidth               ! IN width of rim to update           OLBCZTD1.55     
      logical l_obdy_north           ! IN T=> update north lateral bdy     OLBCZTD1.56     
      logical l_obdy_east            ! IN T=> update east lateral bdy      OLBCZTD1.57     
      logical l_obdy_south           ! IN T=> update south lateral bdy     OLBCZTD1.58     
      logical l_obdy_west            ! IN T=> update west lateral bdy      OLBCZTD1.59     
                                                                           OLBCZTD1.60     
      real bdy_n(n_cols_bdy*rimwidth) ! IN north part of boundary data     OLBCZTD1.61     
      real bdy_e(n_rows_bdy*rimwidth) ! IN east part of boundary data      OLBCZTD1.62     
      real bdy_s(n_cols_bdy*rimwidth) ! IN south part of boundary data     OLBCZTD1.63     
      real bdy_w(n_rows_bdy*rimwidth) ! IN west part of boundary data      OLBCZTD1.64     
                                                                           OLBCZTD1.65     
      integer icode             ! OUT Return code                          OLBCZTD1.66     
      character*(80) cmessage   ! OUT Error message                        OLBCZTD1.67     
CL--------------------------------------------------------------------     OLBCZTD1.68     
                                                                           OLBCZTD1.69     
CL Global variables                                                        OLBCZTD1.70     
*CALL PARVARS                                                              OLBCZTD1.71     
*IF -DEF,MPP                                                               OLBCZTD1.72     
      integer Offx, Offy                                                   OLBCZTD1.73     
      logical atright, atleft, atbase, attop                               OLBCZTD1.74     
*ENDIF                                                                     OLBCZTD1.75     
                                                                           OLBCZTD1.76     
C Local arrays                                                             OLBCZTD1.77     
      integer fkmz_col(n_rows_bdy)  ! land/sea mask for one column         OLBCZTD1.78     
      integer fkmz_row(n_cols_bdy)  ! land/sea mask for one column         OLBCZTD1.79     
                                                                           OLBCZTD1.80     
C Local scalars                                                            OLBCZTD1.81     
                                                                           OLBCZTD1.82     
      integer i             ! loop over columns                            OLBCZTD1.83     
      integer j             ! loop over rows                               OLBCZTD1.84     
      integer L             ! loop over segments                           OLBCZTD1.85     
      integer irim          ! pointer to position in bdy data arrays       OLBCZTD1.86     
      integer ifld          ! pointer to position in field to update       OLBCZTD1.87     
      integer IS,IE         ! start and end points in segment              OLBCZTD1.88     
                                                                           OLBCZTD1.89     
C----------------------------------------------------------------------    OLBCZTD1.90     
CL 0.0 Set grid off-sets and indicators showing if domain lies next to     OLBCZTD1.91     
CL     each model boundary; non-mpp settings for offsets are zero and      OLBCZTD1.92     
CL     for boundary indicators are true. This allows the same code         OLBCZTD1.93     
CL     to be used for MPP and non-MPP cases.                               OLBCZTD1.94     
CL                                                                         OLBCZTD1.95     
                                                                           OLBCZTD1.96     
*IF -DEF,MPP                                                               OLBCZTD1.97     
      Offx = 0                                                             OLBCZTD1.98     
      Offy = 0                                                             OLBCZTD1.99     
      atright = .true.                                                     OLBCZTD1.100    
      atleft  = .true.                                                     OLBCZTD1.101    
      atbase  = .true.                                                     OLBCZTD1.102    
      attop   = .true.                                                     OLBCZTD1.103    
*ENDIF                                                                     OLBCZTD1.104    
                                                                           OLBCZTD1.105    
CL 1.1 Northern boundary: set points in row "JMT-1". Reconstruct           OLBCZTD1.106    
CL     interior grid land-sea mask for this row.                           OLBCZTD1.107    
                                                                           OLBCZTD1.108    
      if ( l_obdy_north .and. atbase ) then                                OLBCZTD1.109    
                                                                           OLBCZTD1.110    
        j = n_rows_bdy-1                                                   OLBCZTD1.111    
                                                                           OLBCZTD1.112    
        do i = 1, n_cols_bdy                                               OLBCZTD1.113    
          fkmz_row(i) = 0                                                  OLBCZTD1.114    
        enddo   ! i                                                        OLBCZTD1.115    
                                                                           OLBCZTD1.116    
        do L = 1, lseg                                                     OLBCZTD1.117    
          IS=ISZ(j+Offy,L)                                                 OLBCZTD1.118    
          IE=IEZ(j+Offy,L)                                                 OLBCZTD1.119    
          if (IS .gt. 0) then                                              OLBCZTD1.120    
            do i=IS,IE                                                     OLBCZTD1.121    
              fkmz_row(i) = 1                                              OLBCZTD1.122    
            end do                                                         OLBCZTD1.123    
          end if                                                           OLBCZTD1.124    
        end do                                                             OLBCZTD1.125    
                                                                           OLBCZTD1.126    
        do i = 1, n_cols_bdy                                               OLBCZTD1.127    
          ifld = i + Offx + (j+Offy-1)*ncols                               OLBCZTD1.128    
          irim = i + (rimwidth-2)*n_cols_bdy                               OLBCZTD1.129    
          d1_field(ifld) = bdy_n(irim)                                     OLBCZTD1.130    
          if ( fkmz_row(i) .eq. 0) then                                    OLBCZTD1.131    
            d1_field(ifld) = 0.0                                           OLBCZTD1.132    
          endif                                                            OLBCZTD1.133    
        enddo   ! i                                                        OLBCZTD1.134    
                                                                           OLBCZTD1.135    
      endif  ! northern boundary                                           OLBCZTD1.136    
                                                                           OLBCZTD1.137    
CL 1.2 Southern boundary: as for northern boundary                         OLBCZTD1.138    
                                                                           OLBCZTD1.139    
                                                                           OLBCZTD1.140    
      if (l_obdy_south .and. attop ) then                                  OLBCZTD1.141    
                                                                           OLBCZTD1.142    
        j = 2                                                              OLBCZTD1.143    
                                                                           OLBCZTD1.144    
        do i = 1, n_cols_bdy                                               OLBCZTD1.145    
          fkmz_row(i) = 0                                                  OLBCZTD1.146    
        enddo   ! i                                                        OLBCZTD1.147    
                                                                           OLBCZTD1.148    
        do L = 1, lseg                                                     OLBCZTD1.149    
          IS=ISZ(j+Offy,L)                                                 OLBCZTD1.150    
          IE=IEZ(j+Offy,L)                                                 OLBCZTD1.151    
          if (IS .gt. 0) then                                              OLBCZTD1.152    
            do i=IS,IE                                                     OLBCZTD1.153    
              fkmz_row(i) = 1                                              OLBCZTD1.154    
            end do                                                         OLBCZTD1.155    
          end if                                                           OLBCZTD1.156    
        end do                                                             OLBCZTD1.157    
                                                                           OLBCZTD1.158    
        do i = 1, n_cols_bdy                                               OLBCZTD1.159    
          ifld = i + Offx + (j+Offy-1)*ncols                               OLBCZTD1.160    
          irim = i + n_cols_bdy                                            OLBCZTD1.161    
          d1_field(ifld) = bdy_s(irim)                                     OLBCZTD1.162    
          if ( fkmz_row(i) .eq. 0) then                                    OLBCZTD1.163    
            d1_field(ifld) = 0.0                                           OLBCZTD1.164    
          endif                                                            OLBCZTD1.165    
        enddo  ! i                                                         OLBCZTD1.166    
                                                                           OLBCZTD1.167    
      endif  ! southern boundary                                           OLBCZTD1.168    
                                                                           OLBCZTD1.169    
CL 1.3 Eastern boundary: re-calculate corner points even                   OLBCZTD1.170    
CL     if they have already been set                                       OLBCZTD1.171    
                                                                           OLBCZTD1.172    
      if (atright .and. l_obdy_east) then                                  OLBCZTD1.173    
                                                                           OLBCZTD1.174    
        i = n_cols_bdy - 1                                                 OLBCZTD1.175    
                                                                           OLBCZTD1.176    
        do j = 1, n_rows_bdy                                               OLBCZTD1.177    
          fkmz_col(j) = 0                                                  OLBCZTD1.178    
          do L = 1, lseg                                                   OLBCZTD1.179    
            if ( ISZ(j+Offy,L) .le. i+Offx .and.                           OLBCZTD1.180    
     #           IEZ(j+Offy,L) .ge. i+Offx ) then                          OLBCZTD1.181    
              fkmz_col(j) = 1                                              OLBCZTD1.182    
            end if                                                         OLBCZTD1.183    
          end do ! L                                                       OLBCZTD1.184    
        enddo   ! j                                                        OLBCZTD1.185    
                                                                           OLBCZTD1.186    
        do j = 1, n_rows_bdy                                               OLBCZTD1.187    
          ifld = i + Offx + (j+Offy-1)*ncols                               OLBCZTD1.188    
          irim = j * rimwidth - 1                                          OLBCZTD1.189    
          d1_field(ifld) = bdy_e(irim)                                     OLBCZTD1.190    
          if ( fkmz_col(j) .eq. 0 ) then                                   OLBCZTD1.191    
            d1_field(ifld) = 0.0                                           OLBCZTD1.192    
          endif                                                            OLBCZTD1.193    
        enddo                                                              OLBCZTD1.194    
                                                                           OLBCZTD1.195    
      endif                                                                OLBCZTD1.196    
                                                                           OLBCZTD1.197    
C 1.4 Western boundary: same as for eastern boundary                       OLBCZTD1.198    
                                                                           OLBCZTD1.199    
      if (atleft .and. l_obdy_west) then                                   OLBCZTD1.200    
                                                                           OLBCZTD1.201    
        i = 2                                                              OLBCZTD1.202    
                                                                           OLBCZTD1.203    
        do j = 1, n_rows_bdy                                               OLBCZTD1.204    
          fkmz_col(j) = 0                                                  OLBCZTD1.205    
          do L = 1, lseg                                                   OLBCZTD1.206    
            if ( ISZ(j+Offy,L) .le. i+Offx .and.                           OLBCZTD1.207    
     #           IEZ(j+Offy,L) .ge. i+Offx ) then                          OLBCZTD1.208    
              fkmz_col(j) = 1                                              OLBCZTD1.209    
            end if                                                         OLBCZTD1.210    
          end do ! L                                                       OLBCZTD1.211    
        enddo   ! j                                                        OLBCZTD1.212    
                                                                           OLBCZTD1.213    
        do j = 1, n_rows_bdy                                               OLBCZTD1.214    
          ifld = i + Offx + (j+Offy-1)*ncols                               OLBCZTD1.215    
          irim = (j-1) * rimwidth + 2                                      OLBCZTD1.216    
          d1_field(ifld) = bdy_w(irim)                                     OLBCZTD1.217    
          if ( fkmz_col(j) .eq. 0 ) then                                   OLBCZTD1.218    
            d1_field(ifld) = 0.0                                           OLBCZTD1.219    
          endif                                                            OLBCZTD1.220    
        enddo   ! j                                                        OLBCZTD1.221    
                                                                           OLBCZTD1.222    
      endif   ! western boundary                                           OLBCZTD1.223    
                                                                           OLBCZTD1.224    
      return                                                               OLBCZTD1.225    
      end                                                                  OLBCZTD1.226    
*ENDIF                                                                     OLBCZTD1.227