function myFanCurve(fname,nfit,Rz,patm,verbose)
% myFanCurve  Data reduction for fan curve data stored in a text file
%
% Synopsis:  myFanCurve
%            myFanCurves(fname)
%            myFanCurves(fname,nfit)
%            myFanCurves(fname,nfit,Rz)
%            myFanCurves(fname,nfit,Rz,patm)
%            myFanCurves(fname,nfit,Rz,patm,verbose)
%
% Input:  fname = name of text file containing fan curve data.
%                 Default: fname = 'Panaflow20V.dat'
%         nfit = degree of polynomial fit for fan curve data.  Default: nfit=4
%         Rz = resistance of thermistor in the zone box.  Default: Rz=11000
%         verbose = flag to control printing of raw and reduced data.
%                   Default: verbose = true,  print the data
%
% Output:  Print out of curve fit coefficients, plot of fan curve, and
%          optional print out of raw and reduced data

if nargin<1 || isempty(fname), fname='Panaflow20V.dat';      end
if nargin<2 || isempty(nfit),  nfit=4;  end
if nargin<3 || isempty(Rz),    Rz=11000; end
if nargin<4 || isempty(patm),  patm = 101325;  end
if nargin<3,  verbose = true;  end

dplen = 22*2.54e-2;   %  Plenum diameter for the small flow bench

% --- Read data and let flowBench reduce it
[dn,vdpp,vdpn,emf] = loadFlowBenchData(fname,verbose);
[Q,dp] = flowBench(dn,vdpp,vdpn,emf,Rz,patm,dplen,verbose);

% --- Obtain curve fits and print coefficients to the command window
c = polyfit(Q,dp,nfit);
fprintf('\nCurve fit coefficients for %s\n',fname);
for i=1:length(c)
  fprintf('\t%20.11e  Q^(%d)\n',c(i),nfit-i+1);
end

% --- Evaluate curve fit over the range of data (include Q=0 as lower limit)
%     and plot the data along with the fit
Qfit = linspace(0,max(Q));
dpfit = polyval(c,Qfit);
plot(Q,dp,'o',Qfit,dpfit,'r--');
xlabel('Q  (m^3/s)');    ylabel('\Delta p  (Pa)');
legend(fname,sprintf('Degree %d fit',nfit));
