function [out]=cur(U,V,lon,lat)

%calculates the divergence of a vector field in spherical geometry 
c=size(U);
N=c(1,2);
NN=c(1,1);


if nargin<4; error('Error - Not enough input parameters specified'), end;
if nargin>4; error('Error - to0 many input parameters specified'), end;
if size(U)~=size(V); error('Error- vector component matrices must be the same size'), end;
if length(lat)>NN;error('Error - vector of latitude values has too large of dimensions'),end;
if length(lat)<NN;error('Error - vector of latitude values has too small of dimensions'),end;
if length(lon)>N;error('Error - vector of longitude values has too large of dimensions'),end;
if length(lon)<NN;error('Error - vector of longitude values has too small of dimensions'),end;
if max(lat)>pi; error('Error- latitude input should be in radians'), end;
if max(lon)>2*pi; error('Error- longitude input should be in radians'), end; 

out=zeros(NN,N);

a=6.37*10^6;

[LON LAT]=meshgrid(lon,lat);
uc=U.*cos(LAT);

uy=zeros(NN,N);

for m=1:1:N
    uy(1,m)= (uc(2,m)-uc(1,m))/(lat(2)-lat(1));
    uy(NN,m)= (uc(NN,m)-uc(NN-1,m))/(lat(NN)-lat(NN-1));
end
    
for m=2:1:NN-1
    for n=1:1:N
        uy(m,n)=(uc(m+1,n)-uc(m-1,n))/(lat(m+1)-lat(m-1));
    end
end

uy=uy./(a*cos(LAT));

vx=divx(V, lon, lat);

out=vx-uy;