%  downsample

function fld_up = usample(fld)


nx = 4320;

[dx,dy] = quikreadpcolor_dxdy_llc('~dmenemen/llc_4320/grid/DXC.data','~dmenemen/llc_4320/grid/DYC.data',nx);
s = size(dx);
minRes = 10000;%max(max(dx(~isnan(dx)&abs(dx)<inf)));

for ii = 1 : s(2);

        
        
        res = dx(:,ii);
        %r = mean(res(~isnan(res)&res>0&abs(res)<inf));
        %if isnan(r)
        %    r(ii) = 2000;
       % end
        
        nPts(ii) = round(sum(res(~isnan(res)&res>0&abs(res)<inf)/minRes));
        
end

for ii = 1 : s(2) % by column -- latitude
  
    k = 2*floor(s(2)/nPts/2)+1; %make sure k is odd
    %k = s(2)/nPts(ii);
    LL(ii) = k;
    L = k;
    w = parzenwin(L);
    w = w/sum(w);
    is = (L-1)/2;
    c = 0;
    for jj = 1+is : k : s(2)-is
        
        sp = floor((max(nPts) - nPts(ii))/2);
        
        
        u_filt1(sp+c,ii)=sum(w.*u(jj-is:jj+is,ii));
        v_filt1(sp+c,ii)=sum(w.*v(jj-is:jj+is,ii));
        taux_filt1(sp+c,ii)=sum(w.*taux(jj-is:jj+is,ii));
        tauy_filt1(sp+c,ii)=sum(w.*tauy(jj-is:jj+is,ii));
        
        c = c + 1;
    end
end

for jj = 1 : s(1)
     
    ii = 20;
    while ii < s(2) - 20

    L = LL(ii);
    w = parzenwin(L);
    w = w/sum(w);
    is = (L-1)/2;
    c = 0;
    
    % want jj,ii+20
    
    
    
for ii = 20: : s(2)-20 % by column -- latitude
  while 
   
    for jj = 1+is : k : s(2)-is
        
        sp = floor((max(nPts) - nPts(ii))/2);
        
        
        u_filt1(sp+c,ii)=sum(w.*u(jj-is:jj+is,ii));
        v_filt1(sp+c,ii)=sum(w.*v(jj-is:jj+is,ii));
        taux_filt1(sp+c,ii)=sum(w.*taux(jj-is:jj+is,ii));
        tauy_filt1(sp+c,ii)=sum(w.*tauy(jj-is:jj+is,ii));
        
        c = c + 1;
    end
end
       
            
            for j=1:m
                
                
                
                for i=is+1:n-is;
                    
                    res = dx(:,i);
                    r = mean(res(~isnan(res)&res>0&abs(res)<inf));
                    if isnan(r)
                        r = 2000;
                    end
                    L = round(10000/r);
                    w = parzenwin(L);
                    w = w/sum(w);
                    
                    % filter by column
                    u_filt(j,i)=sum(w.*u_filt1(j,i-is:i+is)');
                    v_filt(j,i)=sum(w.*v_filt1(j,i-is:i+is)');
                    taux_filt(j,i)=sum(w.*taux_filt1(j,i-is:i+is)');
                    tauy_filt(j,i)=sum(w.*tauy_filt1(j,i-is:i+is)');
                end
            end
            
            
    
    
    
    
    
