function stats = nnResults(filename,view)
% nnResults(filename,view):  parses nnTrain results file.
%   if filename is in short format, 
%      file is sent to nnSummarize and resulting matrix is returned and
%                           assigned to global variable nnStats.
%   if filename is in long format,
%      results of each run are displayed (if view = 1) and assigned
%    to global nnFid nnE nnEp nnOuts nnWh1 nnWh2 nnWo nnCurve nnCorr nnSummary
%
%  Copyright (c) 1996 by Charles W. Anderson

% Requires:   nnSummarize, nnParseResults, nnPlotCurve, nnPlotOuts,
%             nnPlotOutsScat, nnShowWeights


global nnFid nnE nnEp nnOuts nnWh1 nnWh2 nnWo nnCurve nnCorr nnSummary

eval(['!gawk ''$2 != "values" && $1 != "ld.so.1:"'' < ' filename ' > /tmp/results']);
nnFid = fopen('/tmp/results','r');
if nnFid == -1
  error('Unable to open file %s \n',filename);
end

if exist('view') ~= 1
  view = 0;
end;

word = fscanf(nnFid,'%s',1);
if strcmp(word,'ld.so.1:') == 1
  fskipwords(nnFid,9); %to skip rest of lib older version warning
  word = fscanf(nnFid,'%s',1);  %get first word after warning line
end

if strcmp(word,'i') ~= 1
  fclose(nnFid);
  fprintf(1,'File %s is in summary form.  Result is in variable nnSummary.\n',filename);
  fprintf(1,'Apply nnRuns to this matrix.\n');
  eval(['!cp ' filename ' /tmp/datain']);
  !awk 'NF == 10 && $1 != "ld.so.1:"' /tmp/datain > /tmp/nndata
  eval(['load /tmp/nndata -ascii']);
%  eval(['stats = ' strtok(filename,'.') ';']);
  stats = nndata;
  nnSummary = nndata;
  return
end

frewind(nnFid);

% At this point we know the file is in long format.

if view
  if exist('fig1') ~= 1
    fig1 = figure('position',[500,5,500,720],'paperposition',[0.25 0.25 8.0 10.5]);
    fig2 = figure('position',[450,5,500,500]);
  end
end

while feof(nnFid) == 0
            
  [annE, annEp, annOuts, annWh1, annWh2, annWo, annCurve, annCorr] = nnParseResults(nnFid);

  if feof(nnFid) == 1
    break
  end

  nnE = annE; nnEp = annEp; nnOuts = annOuts; 
  nnWh1 = annWh1; nnWh2 = annWh2; nnWo = annWo;
  nnCurve = annCurve; nnCorr = annCorr;
  
  % Type results
  fprintf(1,'Results are in variables global nnE nnEp nnOuts nnWh1 nnWh2 nnWo nnCurve nnCorr\n');
  fprintf(1,'Cut and paste above from  global ...  if not already done.\n');
  
  nnE, nnEp

  % Display the results
  if (view)
    figure(fig1);
    clf
    subplot(3,1,1);
    nnPlotCurve(nnEp,nnCurve);
    subplot(3,1,2);
    nnPlotOuts(nnOuts,nnCorr);
    if size(nnOuts,2) == 2
      subplot(3,1,3);
      nnPlotOutsScat(nnOuts);
    end

    figure(fig2);
    clf
    if nnWh2
      nnShowWeights(nnWh1,nnWh2,nnWo);
    else
      nnShowWeights(nnWh1,nnWo);
    end

    while 1
      in = input('Quit, save, or next? (q, s, enter) ','s');
      if findstr('quit',in)
	fclose(nnFid);
	return;
      elseif findstr('save',in)
	file_str = input('Filename? ','s');
	out_str1 = ['print ', file_str,'fig1  -f',num2str(fig1)];
	eval(out_str1);
	out_str2 = ['print ', file_str,'fig2  -f',num2str(fig2)];
        eval(out_str2);
        out_text = ['Saved ', file_str,' fig1.ps  ', file_str, ' fig2.ps '];
        disp(out_text);
      else
        break;
      end; % end of cases for user input
    end;   % end of while, cycling through user input
  else % if not viewing
    in = input('Quit or next? (q, enter) ','s');
    if findstr('quit',in)
      fclose(nnFid);
      return;
%    else
%      break;
    end;
  end;
end;

%input('Hit return to exit >>');
fclose(nnFid);

!rm /tmp/results

