function out=divx(opt,A,do,lat)

if nargin<1; error('Error - Not enough input parameters specified'), end;
if nargin>4; error('Error - too many input parameters specified'), end;

if nargin>=3; DO=do;, end;
if nargin<=2;
    for m=1:1:length(A)
        DO(1,m)=m;
    end
    ,end

N=length(A);

if length(DO)>N;error('Error - vector of domain values has too large of dimensions'),end;
if length(DO)<N;error('Error - vector of domain values has too small of dimensions'),end;

NN=length(mean(A'));
out=zeros(NN,N);
DOMAIN=DO;

P=360; %wavelenght of domain
a=6.37*10^6;
if opt=='p'  %periodic domain
    for m=1:1:NN
        out(m,1)= (A(m,2)-A(m,N))/(DO(2)+(P-DO(N)));
        out(m,N)= (A(m,1)-A(m,N-1))/(DO(1)+P-DO(N-1));
    end
    
    for m=2:1:N-1
        for n=1:1:NN
            out(n,m)=(A(n,m+1)-A(n,m-1))/(DO(m+1)-DO(m-1));
        end
    end

elseif opt=='pl'
    for m=1:1:NN
        out(m,1)= (A(m,2)-A(m,N))/(DO(2)+(P-DO(N)));
        out(m,N)= (A(m,1)-A(m,N-1))/(DO(1)+P-DO(N-1));
    end
    
    for m=2:1:N-1
        for n=1:1:NN
            out(n,m)=(A(n,m+1)-A(n,m-1))/(DO(m+1)-DO(m-1));
        end
    end
   
    for m=1:1:NN
        out(m,:)=out(m,:)/(pi*a*cos(lat(m)*pi/180)/180);
    end
elseif opt=='l'
    for m=1:1:NN
        out(m,1)= (A(m,2)-A(m,1))/(DO(2)-DO(1));
        out(m,N)= (A(m,N)-A(m,N-1))/(DO(N)-DO(N-1));
    end
    
    for m=2:1:N-1
        for n=1:1:NN
            out(n,m)=(A(n,m+1)-A(n,m-1))/(DO(m+1)-DO(m-1));
        end
    end
    
    for m=1:1:NN
        out(m,:)=out(m,:)/(pi*a*cos(lat(m)*pi/180)/180);
    end
    
else
    for m=1:1:NN
        out(m,1)= (A(m,2)-A(m,1))/(DO(2)-DO(1));
        out(m,N)= (A(m,N)-A(m,N-1))/(DO(N)-DO(N-1));
    end
    
    for m=2:1:N-1
        for n=1:1:NN
            out(n,m)=(A(n,m+1)-A(n,m-1))/(DO(m+1)-DO(m-1));
        end
    end
end


    
    