      subroutine alignocn(nwater,iorg,jorg,darray,xlon,ylat,
     *dx0,dy0,x0,y0,ia,ia2,ja,ja2,delx,dely)
c
c  Given an data array with fixed dimensions, interpolate and align
c   with the ocean grid used in a model run.
c
#include "gloparam.F.h"
      integer ia(len),ia2(len),ja(len),ja2(len)
      real delx(len),dely(len)
      real darray(iorg,jorg)
      real xlon(len),ylat(len)
      data flag1 /-0.9E10/
      data flag2 /999.0E9/
c
      xfac = float(iorg)/360.0
      yfac = float(jorg)/180.0
      flag = flag2           !use zdf flags
c
c  Determine valid ocean indices
      do i = 1,nwater
       if (xlon(i) .le. 180.0)then
        rlon = xlon(i)
       else
        rlon = xlon(i) - 360.0
       endif
       xarg = rlon - x0
       if (xarg .lt. 0.0)xarg = xarg+360.0
       iat = int(xarg*xfac) + 1
       if (iat .gt. iorg)iat = iat - iorg
       jat = int((ylat(i)-y0)*yfac ) + 1
       if (jat .gt. jorg)jat = jat - jorg
       atlon = float(iat-1)*dx0 + x0
       if (atlon .gt. 180.0)atlon = atlon-360.0
       atlat = float(jat-1)*dy0 + y0
       dx = rlon - atlon
       if (abs(dx) .gt. dx0)dx = abs(rlon) - abs(atlon)
       dy = abs(ylat(i) - atlat)
c        write(6,'(i6,6x,2f10.5)')i,rlon,ylat(i)
c        write(6,'(2i6,2f10.5)')iat,jat,atlon,atlat
       delx(i) = dx/dx0
       dely(i) = dy/dy0
       iat2 = iat+1
       if (iat2 .gt. iorg)iat2 = 1
       jat2 = jat+1
       jat2 = min(jat2,jorg)
       ia(i) = iat     
       ia2(i) = iat2
       ja(i) = jat
       ja2(i) = jat2
      enddo
c
      return
      end
