% $Id: template $
% Copyright J. V. Mansbridge, CSIRO, Wed Apr 26 11:36:55 EST 2000

close all
clear all

opt = menu('Choose example', ...
	   'remove NaNs', ...
	   'smooth everything (use "all")', ...
	   'smooth everything with varying weights')

m = 8;
n = 6;

z = peaks(max([m n]));
array_2d = round(10*z(1:m, 1:n));
array_2d(3, 2) = NaN;
array_2d(4, 4) = NaN;
array_2d(5, 4) = NaN;
array_2d(1, 3) = NaN;
array_2d(m, 2) = NaN;
array_2d(6, 1) = NaN;
array_2d(6, n) = NaN;

array_2d
switch opt
 case 1
  weight_2d = 1;
  weight_x = 1;
  weight_y = 1;
  weight_xy = 1;
  index_pts = find(isnan(array_2d));
  new_vals = find_weighted_mean_neighbours(index_pts, array_2d, ...
				weight_2d, weight_x, weight_y, weight_xy);
  array_2d_new = array_2d;
  array_2d_new(index_pts) = new_vals;
 case 2
  weight_2d = 1;
  weight_x = 1;
  weight_y = 1;
  weight_xy = 1;
  index_pts = 'all';
  array_2d_new = find_weighted_mean_neighbours(index_pts, array_2d, ...
				weight_2d, weight_x, weight_y, weight_xy);
 case 3
  weight_2d = 1;
  weight_x = 0.5;
  weight_y = 0.4;
  weight_xy = 0.2;
  index_pts = 'all';
  array_2d_new = find_weighted_mean_neighbours(index_pts, array_2d, ...
				weight_2d, weight_x, weight_y, weight_xy);
end
array_2d_new
range(array_2d_new - array_2d)

figure
flat_pcolor(array_2d)
caxis(range(array_2d_new))
colorbar
title('array_2d')

figure
flat_pcolor(array_2d_new)
caxis(range(array_2d_new))
colorbar
title('array_2d_new')


return
% Some expected answers in case 1

% (30 + 1 + 56 + 38 + 9)/5 = 26.8000
% (7 - 11 - 28 - 9 -49 + 4 + 1)/7 = -12.1429
% (-11 - 28 + 6 + 56 + 4 + 1 + 56)/7 = 12

% Some expected answers in case 2

% (0 + 0 -1 -24 -2)/5 = -5.4000
% (-11 -28 + 6 + 56 + 56 + 1 + 4)/7 = 12

% Some expected answers in case 3

% (0.5*0 + 0.2*0 +0.4*(-1) + 0.2*(-24) + 0.5*(-2))/(0.5 + 0.2 +0.4 + 0.2 + 0.5) = -3.4444


