%--------------------------------------------------------------------------
% 
% wvar_.m -- Print the values of W-Variance when editing profiles. This is
%           for a rapid control of values
% 
% Author          : Jaccard Pierre
% Created On      : 2000/06/29 07:22:21
% Last Modified By: Jaccard Pierre
% Last Modified On: 2000/06/29 08:53:20
% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
% ----------------------------------------------------------------------- +
% VERSION CONTROL: 
% 
%   $Author$
%   $Date$
%   $Id$
%   $Name$
%   $Revision$
%   $State$
%   $Log$
% 
%--------------------------------------------------------------------------

global W WVAR_THRESHOLD IBLKPRF

%--- 
% If no threshold is given, set it to max
%---
if isempty(WVAR_THRESHOLD)
	WVAR_THRESHOLD = Inf;
end

%---
% Mask for bad velocities and scale them
%---
imask__ = ~isnan(W);
w__ = imask__;
w__(imask__) = W(imask__) * 1000.0;  % m/s to mm/s

%---
% Calculate W-Variance
%---
n__ = sum(imask__);
wvar__ = zeros(size(n__));
ii = find(n__ > 1);
if ~isempty(ii),
	wvar__(ii) = (sum(w__(:,ii) .^ 2) - (sum(w__(:,ii)) .^ 2)./n__(ii)) ...
			./ (n__(ii) - 1);
end

%---
% Look for profiles that do not satisfy the WVAR test
%---
ibad__ = (wvar__ > WVAR_THRESHOLD);
igood__= find(~ibad__);
ibad__ = find(ibad__);

%---
% Plot values
%---
figure
orient landscape
hold on

xtk__ = 1:length(wvar__);

if ~isempty(igood__),
	plot(xtk__(igood__), wvar__(igood__), '+b');
end
if ~isempty(ibad__),
	plot(xtk__(ibad__), wvar__(ibad__), '*r');
end
plot([1,length(wvar__)], [WVAR_THRESHOLD, WVAR_THRESHOLD], '-r');

max_xtk__ = 12;
inc_xtk__ = round(size(IBLKPRF,1)/max_xtk__);
ii_xtk__  = 1:inc_xtk__:size(IBLKPRF,1);

xtkstr__ = '';
for i=ii_xtk__,
	xtkstr__ = strvcat(xtkstr__, ...
			[ '(' int2str(IBLKPRF(i,1)) ',' int2str(IBLKPRF(i,2)) ')' ]);
end
set(gca, 'XTick', ii_xtk__);
set(gca, 'XTickLabel', xtkstr__);
title('W Variance', ...
		'FontSize', 15, 'FontWeight', 'bold', 'FontAngle', 'italic');
xlabel('(Block,Profile)', ...
		'FontSize', 12, 'FontWeight', 'bold', 'FontAngle', 'italic');
text(length(wvar__), WVAR_THRESHOLD, 'WVAR_THRESHOLD', ...
		'FontSize', 8, 'FontWeight', 'bold', 'FontAngle', 'italic', ...
		'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top', ...
		'Rotation', 90);
grid on
