function theResult = speclist(theSpec, theFilename)

% speclist -- Listing of "specav" output.
%  speclist(theSpec, 'theFilename') writes a listing of theSpec,
%   output from the "specav" spectral averaging function, to
%   'theFilename' (dialog invoked if not given).
 
% Copyright (C) 1999 Dr. Charles R. Denham, ZYDECO.
%  All Rights Reserved.
%   Disclosure without explicit written consent from the
%    copyright owner does not constitute publication.
 
% Version of 03-Feb-1999 09:09:33.
% Updated    18-Feb-1999 11:30:51.

STDOUT = 1;

if nargin < 1, help(mfilename), return, end

if ischar(theSpec)
	theSpec = evalin('caller', theSpec);
end

if nargin < 2, theFilename = '*'; end

if any(theFilename == '*')
	if isequal(theFilename, '*')
		theFilename = 'speclist.out';
	end
	[theFile, thePath] = uiputfile(theFilename, 'Output File Name?');
	if any(theFile)
		if thePath(end) ~= filesep
			thePath = [thePath filesep];
		end
		theFilename = [thePath theFile];
	else
		theFilename = '';
	end
end

if isequal(lower(theFilename), 'stdout')
	fp = STDOUT;
elseif any(theFilename)
	fp = fopen(theFilename, 'w');
	if fp < 0, return, end
else
	fp = STDOUT;
end

s = theSpec;
		
a = [s.Pxx.est s.Pyy.est s.Pxx.conf_lo s.Pxx.conf_hi ...
		s.Txy.est s.Txy.conf_1 ...
		abs(s.Cxy.est) s.Cxy.sig_level ...
		angle(s.Cxy.est)*180/pi s.Cxy.phase_err ...
		s.Freq.bandwidth s.Freq.period];


theFormat = ['  %12.4e  %12.4e  %5.3f  %5.2f  ' ...
				'%12.4e  %12.4e  %5.2f  %5.2f  %5.0f  %5.0f  %5g  %12.5e\n'];

theHeading = ['Pxx Pyy conf_lo conf_hi TF_amp conf Cxy_amp sig_level phase_deg phase_err bandwidth period\n'];

fprintf(fp, '\n');
fprintf(fp, theHeading);
fprintf(fp, '\n');
fprintf(fp, theFormat, a.');
fprintf(fp, '\n');

if fp ~= STDOUT, fclose(fp); end
