Authored by Jean-Luc Shaw
1 parent 31d166dc
Exists in

### Added boxav.m, a box averaging tool.

Showing 1 changed file with 38 additions and 0 deletions
math/boxav.m 0 → 100644

 ... ... @@ -0,0 +1,38 @@ 1 +function qA = boxav(x,y,qx,qy,q) 2 +% Synthax : qA = boxav(x,y,qx,qy,q) 3 +% 4 +% Box averaging on a rectangular but not necessarily regular grid. Grid 5 +% coordinates are given by matrices 'x' and 'y'. Averaged variable 'q' is 6 +% located at 'qx' and 'qy' coordinates and can be vectors or matrices. 7 +% Averaged variable 'qA', the size of 'x' and 'y' is the output. 8 + 9 +% Initialize output 10 +qA = nan(size(x)) ; 11 + 12 +% Conduct averaging 13 +for ii = 1:numel(x(:,1)) % longitude loop 14 + for jj = 1:numel(y(1,:)) % latitude loop 15 + 16 + % calculate distance to neighbor points 17 + try dxl = abs( x(ii,jj)-x(ii,jj-1) ); catch; dxl = abs( x(ii,jj)-x(ii,jj+1) ); end 18 + try dxr = abs( x(ii,jj)-x(ii,jj+1) ); catch; dxr = abs( x(ii,jj)-x(ii,jj-1) ); end 19 + try dyu = abs( y(ii,jj)-y(ii+1,jj) ); catch; dyu = abs( y(ii,jj)-y(ii-1,jj) ); end 20 + try dyd = abs( y(ii,jj)-y(ii-1,jj) ); catch; dyd = abs( y(ii,jj)-y(ii+1,jj) ); end 21 + 22 + % set logical condition to find points of interest 23 + I = find(qx <= x(ii,jj) + dxr./2 & ... 24 + qx >= x(ii,jj) - dxl./2 & ... 25 + qy <= y(ii,jj) + dyu./2 & ... 26 + qy >= y(ii,jj) - dyd./2) ; 27 + 28 + % average 29 + if ~isempty(I) 30 + qA(ii,jj) = mean(q(I),'omitnan') ; 31 + else 32 + qA(ii,jj) = nan ; 33 + end 34 + 35 + end 36 +end 37 + 38 +end 0 39 \ No newline at end of file ... ...