function CAL=cal_file_make
%
% Call cal_file_make;
%
% Makes CAL file (suffix _CAL.mat) by time-averaging AN file.
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
mcs_global;
%
str=['load ',MCS.dir,MCS.AN_root]; eval(str);
if AN.dims(1)>1,
    CAL=data_avg(AN,[3 1],{[] [] [] 1},[4 1],0,0);
else
    CAL=data_avg(AN,[3],{[] [] [] 1},[4],0,0);
end
mcs_file=[MCS.fft_root,'_FFT_mcs.mat'];
if ~exist(mcs_file,'file'),
    ii=findstr(MCS.fft_root,'_'); lii=length(ii);   % Check if FFT was made with multiple f bands;
    if lii>=2, mcs_file=[MCS.fft_root(1:ii(lii-1)-1),'_FFT_mcs.mat']; end
end
if exist(mcs_file,'file'),
    S=load(mcs_file); CAL.iiph=S.MCS.iiph(:)'; clear S;
else
    disp('NOTE: No _FFT_mcs.mat file found to set CAL.iiph.  Assuming all phones in FFT file.');
    CAL.iiph=[1:AN.dims(2)];
end
nfreq=size(CAL.data,1);
for jfreq=1:nfreq
	CAL.med_level=median(CAL.data(jfreq,:,1,1),2);
	CAL.data(jfreq,:,1,1)=CAL.data(jfreq,:,1,1) - CAL.med_level;   % Remove median over phone;
	CAL.ok_phones_3dB{jfreq}=find(abs(CAL.data(jfreq,:,1,1)) <= 3);
	CAL.suspect_phones_3dB{jfreq}=find(abs(CAL.data(jfreq,:,1,1)) > 3);
	CAL.suspect_phones_10dB{jfreq}=find(abs(CAL.data(jfreq,:,1,1)) > 10);
	CAL.suspect_phones_3_10dB{jfreq}=find(abs(CAL.data(jfreq,:,1,1)) <= 10 & abs(CAL.data(jfreq,:,1,1)) > 3);
	if ~isempty(CAL.suspect_phones_10dB{jfreq}),
        disp(' ');
        disp([num2str(CAL.scales{1}(jfreq)) ' Hz'])
        disp(['WARNING: The following phones have levels > 10 dB off from median: ']);
        str1=sprintf('%5i ',CAL.suspect_phones_10dB{jfreq}); 
        str2=sprintf('%5.1f ',CAL.data(jfreq,CAL.suspect_phones_10dB{jfreq},1,1));
        disp(str1); disp(str2);
	end
	if ~isempty(CAL.suspect_phones_3_10dB{jfreq}),
        disp(' ');
        disp([num2str(CAL.scales{1}(jfreq)) ' Hz'])
        disp(['   NOTE: The following phones have levels >  3 dB (but <= 10 dB) off from median: ']);
        str1=sprintf('%5i ',CAL.suspect_phones_3_10dB{jfreq}); 
        str2=sprintf('%5.1f ',CAL.data(jfreq,CAL.suspect_phones_3_10dB{jfreq},1,1));
        disp(str1); disp(str2); disp(' ');
	end
	if ~isempty(CAL.ok_phones_3dB{jfreq}),
        disp(' ');
        disp([num2str(CAL.scales{1}(jfreq)) ' Hz'])
        disp(['   NOTE: The following phones have levels within 3 dB of median: ']);
        str1=sprintf('%5i ',CAL.ok_phones_3dB{jfreq}); 
        str2=sprintf('%5.1f ',CAL.data(jfreq,CAL.ok_phones_3dB{jfreq},1,1));
        disp(str1); disp(str2); disp(' ');
	end
end

CAL.data=-CAL.data;
