function metCOT = metabolics_new(path1,mass)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% metabolics_new.m computes the metabolic cost of transport (MCOT) of
% walking at each input speed using Grant's code.  It then fits a
% cubic spline to the data and computes the walking speed at which
% the MCOT is minimal.  A plot is created and saved and a .mat file
% with costs and error bars is saved.  Note that .xls has to be in Excel 5.0/
% Windows 95 format for this to work, at least on my Mac.  Also
% note that the basal metabolic cost is NOT subtracted here as I
% found that to not be the case in literature (and trying it led to
% minimal MCOT speeds that were clearly too low).
%
% Inputs:
% path1 - subject identifier for the study.
% mass  - subject mass (in kg, for normalization).
%
% Outputs:
% MCOT plot, values, and speed where MCOT is minimal.
%
% Jared Markowitz
% Fall 2012
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

metCOT = [];
speeds = [75 100 125 150 175 200];

addpath('./grant_code/filetools');
addpath('./grant_code/measurement');
addpath('./grant_code/metabolics');

% Analyze metabolic data:
for i=1:numel(speeds)
  if speeds(i) == 75;
    numstr = '075';
  else
    numstr = num2str(speeds(i));
  end;
  eval(['[analysis,errors]  = ProcessMetabolics(''./' path1 '/WALK' numstr '.xls'');']);
  if exist('mass') > 0
    metCOT = cat(1,metCOT,analysis.Results.NormalizedPower*100/speeds(i));    
  else
    fprintf('No mass input; should have input to Cosmed!\n');
  end;
end;

% Fit and plot metCOT:   
speeds    = speeds/100;
if exist('mass') > 0
  just_mean = 65/mass*metCOT.Value;
  just_unc  = 65/mass*metCOT.Uncertainty;
else
  just_mean = metCOT.Value;
  just_unc  = metCOT.Uncertainty;
end;
xx = 0.75:.01:2;
yy = interp1(speeds,just_mean, xx,'spline');

[val1,ind1] = min(yy);

fprintf(['Minimum MCOT speed is ' num2str(xx(ind1)) 'm/s.\n']);

figure;
errorbar(speeds,just_mean,just_unc,'k','linewidth',3);
hold on;
plot(xx,yy,'linewidth',2);
plot(xx(ind1),val1,'r+','markersize',12,'linewidth',3);
grid on;
xlabel('Walking Speed (m/s)','fontsize',14);
ylabel('Metabolic COT (W/kg/(m/s))','fontsize',14);
title('Metabolic Cost of Transport vs. Walking Speed','fontsize',14);
outplot = ['./' path1 '/metcost.png'];

% Save plot and numbers:
eval(['print -dpng ' outplot ]);
eval(['save(''./' path1 '/metabolics.mat'',''speeds'',''just_mean'',''just_unc'',''xx'',''yy'');']);

return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Don't think it's worth it to do full ChiSq analysis, but here 
% is what I started doing...

%fitfun = @(x,a) a(1) + a(2)*x + a(3)*x.^2 + a(4)*x.^3;

% Initial guess comes from Pandy and Anderson plot or Excel?
%[a,chisq] =  Measurement.fit(speeds,metCOT,fitfun,[]) 

%b = value(a);
%x = 0.7:.01:2.05;
%y = b(1)*x.^ + b(2)*x.^ + b(3)*x.^ + b(4)*x.^ + b(5)*x.^ + ...
%[val,ind] = min(y)
%minCOT = x(ind);    
    
%figure
%Measurement.errorBar(speeds,metCOT);
%hold on;
%plot(x,y,'linewidth',2);
%plot(minCOT,val,'r+','markersize',12,'linewidth',3);
%grid on;
%xlabel('Walking Speed (m/s)');
%ylabel('Metabolic COT (W/kg/(m/s))');
%title('Metabolic Cost of Transport vs. Walking Speed');
%outfile = ['
%print -dpng 

%[val,ind] = min(y)
%minCOT = x(ind);