%  1.  NAME
%
%  avg_ascii - Average field by field ASCII data
%
%  2.  USAGE
%
%  avg_ascii
%
%  3.  DESCRIPTION
%
%  Read all input files and average them field by field. Data may contain
%  bad values.
%
%  4.  PARAMETERS
%
%  The following parameters must be edited:
%
%     files
%        Cell array with input file names.
%
%     avg_file
%        Name for output averaged file.
%
%     write_sdev
%        If non-zero,output also a file with standard deviation for each
%        field (this file will have the .sdev suffix).
%
%     write_npts
%        If non-zero,output also a file with the number of good points
%        taken into account during averaging (this file will have the
%        .npts suffix).
%
%  5.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

% Edit parameters below:
%------------------------------------------------------------------

%files      = { '../001/adcp_fs_abs.rgd', ...
%               '../002/adcp_fs_abs.rgd' };
%avg_file   = 'adcp_fs_abs.avg';

%files      = { '../001/adcp_bt_abs.rgd', ...
%               '../002/adcp_bt_abs.rgd' };
%avg_file   = 'adcp_bt_abs.avg';

%files      = { '../001/adcp_fs_abs_detided.rgd', ...
%               '../002/adcp_fs_abs_detided.rgd' };
%avg_file   = 'adcp_fs_abs_detided.avg';

files      = { '../001/adcp_bt_abs_detided.rgd', ...
               '../002/adcp_bt_abs_detided.rgd' };
avg_file   = 'adcp_bt_abs_detided.avg';

write_sdev = 1;
write_npts = 1;

%------------------------------------------------------------------
% No more editing!!!

%--- Initializations:
s  = tonan(read_asc(files{1}));
n  = zeros(size(s));

%--- Mask for good points:
igood = ~isnan(s);
s  = s .* igood;
sq = s .* s;
n  = n + igood;

%--- Index values:
nf = length(files);
nv = size(s,2);
nl = size(s,1);

%--- Loop through the other files:
for i=2:nf,
	% Read next data:
  d = tonan(read_asc(files{i}));
	if size(d) ~= size(s),
		error([ 'File ' files{i} ' does not have the same dimension.' ]);
	end
	% Build mask of good points:
  igood = ~isnan(d);
	d = d .* igood;
	% Update statistics:
	s = s + d;
	sq = sq + (d .* d);
  n  = n + igood;
end

%--- Mask for good averages:
igood = ~isnan(s) & (n > 0);
good  = find(igood);
bad = find(~igood);

%--- Calculate the mean value:
s(good) = s(good) ./ n(good);
s(bad) = NaN;
sq(good) = sqrt( sq(good) ./ n(good) - s(good) .* s(good) );
sq(bad) = NaN;

%--- Reshape arrays:
s  = reshape(s, nl, nv);
sq = reshape(sq, nl, nv);
n  = reshape(n, nl, nv);

%--- Write to output file:
fid = fopen(avg_file, 'w');
for i=1:nl
	for v=1:nv
		if isnan(s(i,v)),
			fprintf(fid, '      1.0E38 ');
		else
			fprintf(fid, '%12.6f ', s(i,v)); 
		end
	end
	fprintf(fid, '\n');
end
fclose(fid);

%--- Write standard deviation:
if write_sdev,
	out_file = newext('', 'sdev', avg_file);
	fid = fopen(out_file, 'w');
	for i=1:nl
		for v=1:nv
			if isnan(sq(i,v)),
				fprintf(fid, '      1.0E38 ');
			else
				fprintf(fid, '%12.6f ', sq(i,v)); 
			end
		end
		fprintf(fid, '\n');
	end
	fclose(fid);
end

%--- Write number fo points in statistics:
if write_npts,
	out_file = newext('', 'npts', avg_file);
	fid = fopen(out_file, 'w');
	for i=1:nl
		for v=1:nv
			fprintf(fid, '%4d ', n(i,v)); 
		end
		fprintf(fid, '\n');
	end
	fclose(fid);
end






