Index: /branches/2015/dev_r5803_UKMO_AGRIF_Vert_interp/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90
===================================================================
--- /branches/2015/dev_r5803_UKMO_AGRIF_Vert_interp/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 8099)
+++ /branches/2015/dev_r5803_UKMO_AGRIF_Vert_interp/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 8100)
@@ -754,8 +754,8 @@
              h_in(N_in) = ptab(ji,jj,jk,n2)/(e1e2t(ji,jj)*zrhoxy)
            END DO
-           N_out = jpk-1
+           N_out = 0
            DO jk=1,jpk ! jpk of child grid
-!             IF (tmask(ji,jj,jk) == 0) EXIT ! TODO: Will not work with ISF. !This doesn't seem to work at the moment. Is it just a GYRE issue???????
-!             N_out = N_out + 1
+             IF (tmask(ji,jj,jk) == 0) EXIT ! TODO: Will not work with ISF. !This doesn't seem to work at the moment in GYRE but is OK in overflow model
+             N_out = N_out + 1
              h_out(jk) = e3t_n(ji,jj,jk) !Child grid scale factors. Could multiply by e1e2t here instead of division above
            ENDDO
@@ -904,17 +904,17 @@
          ! East south
          IF ((eastern_side).AND.((nbondj == -1).OR.(nbondj == 2))) THEN
-            tsa(nlci-1,2,:,:) = ptab_child(nlci-1,2,:,:)
+            tsa(nlci-1,2,:,:) = ptab_child(nlci-1,2,:,1:jpts)
          ENDIF
          ! East north
          IF ((eastern_side).AND.((nbondj == 1).OR.(nbondj == 2))) THEN
-            tsa(nlci-1,nlcj-1,:,:) = ptab_child(nlci-1,nlcj-1,:,:)
+            tsa(nlci-1,nlcj-1,:,:) = ptab_child(nlci-1,nlcj-1,:,1:jpts)
          ENDIF
          ! West south
          IF ((western_side).AND.((nbondj == -1).OR.(nbondj == 2))) THEN
-            tsa(2,2,:,:) = ptab_child(2,2,:,:)
+            tsa(2,2,:,:) = ptab_child(2,2,:,1:jpts)
          ENDIF
          ! West north
          IF ((western_side).AND.((nbondj == 1).OR.(nbondj == 2))) THEN
-            tsa(2,nlcj-1,:,:) = ptab_child(2,nlcj-1,:,:)
+            tsa(2,nlcj-1,:,:) = ptab_child(2,nlcj-1,:,1:jpts)
          ENDIF
          !
@@ -978,9 +978,17 @@
       zrhoy = Agrif_rhoy()
       IF (before) THEN 
+         !We can't use zero as the special value because we need to include zeros
+         !when interpolating the scale factors
+         IF(Agrif_UseSpecialValue) THEN 
+             Agrif_SpecialValue = -999._wp
+         ELSE
+             Agrif_SpecialValue = 0._wp
+         ENDIF
          DO jk=1,jpk
             DO jj=j1,j2
                DO ji=i1,i2
-                  ptab(ji,jj,jk,1) = e2u(ji,jj) * e3u_n(ji,jj,jk) * un(ji,jj,jk)
-                  ptab(ji,jj,jk,2) = umask(ji,jj,jk) * e2u(ji,jj) * e3u_n(ji,jj,jk)
+                  ptab(ji,jj,jk,1) = (e2u(ji,jj) * e3u_n(ji,jj,jk) * un(ji,jj,jk)*umask(ji,jj,jk)) - &
+                                   & ((umask(ji,jj,jk)-1) * Agrif_SpecialValue)
+                  ptab(ji,jj,jk,2) = (umask(ji,jj,jk) * e2u(ji,jj) * e3u_n(ji,jj,jk))
                END DO
             END DO
@@ -990,5 +998,7 @@
          western_side  = (nb == 1).AND.(ndir == 1)
          eastern_side  = (nb == 1).AND.(ndir == 2)
-         
+
+         Agrif_SpecialValue = 0._wp ! reset specialvalue to zero now interpolation completed
+
          ptab_child(:,:,:) = 0.
          DO jj=j1,j2
@@ -1013,5 +1023,5 @@
               N_out = 0
               DO jk=1,jpk
-                 if (umask(iref,jj,jk) == 0) EXIT
+                 if (umask(ji,jj,jk) == 0) EXIT
                  N_out = N_out + 1
                  h_out(N_out) = e3u_n(ji,jj,jk)
@@ -1079,4 +1089,9 @@
       zrhox = Agrif_rhox()
       IF (before) THEN          
+         IF(Agrif_UseSpecialValue) THEN 
+             Agrif_SpecialValue = -999._wp
+         ELSE
+             Agrif_SpecialValue = 0._wp
+         ENDIF
          DO jk=k1,k2
             DO jj=j1,j2
@@ -1092,4 +1107,7 @@
          southern_side = (nb == 2).AND.(ndir == 1)
          northern_side = (nb == 2).AND.(ndir == 2)
+
+         Agrif_SpecialValue = 0._wp !Reset special value to zero now interpolation is done
+
          do jj=j1,j2
             jref = jj
