function  [Q,dp] = flowBench(dn,vdpp,vdpn,emf,Rzone,patm,dplen,verbose)
% flowBench  Convert flow bench measurements to flow rate and presure drop
%
% Synopsis:  [Q,dp] = flowBench(dn,vdpp,vdpn,emf)
%            [Q,dp] = flowBench(dn,vdpp,vdpn,emf,Rzone)
%            [Q,dp] = flowBench(dn,vdpp,vdpn,emf,Rzone,patm)
%            [Q,dp] = flowBench(dn,vdpp,vdpn,emf,Rzone,patm)
%            [Q,dp] = flowBench(dn,vdpp,vdpn,emf,Rzone,patm,dplen,verbose)
%
% Input:  dn = nozzle diamter (m)
%         vdpp = voltage for pressure transducer across the plenum (V)
%         vdpn = voltage for pressure transducer across the nozzle (V)
%         emf = emf for thermocouple upstream of the nozzle (mV)
%         Rzone = (optional) resistance of YSI 44006 thermistor in zone box (Ohm)
%                  Default: Rzone = 12000;
%         patm = (optional) atmospheric pressure (Pa).  Default: patm = 101325
%         dplen = (optional) hydraulic diameter of the plenum (m).
%                 Default: dplen = 22*2.54e-2; (m) (size for small flow bench)
%         verbose = (optional) flag to turn printing on/off
%                  Default: verbose = 0 (false), print raw data
%
% Note:  Input paramters dn, vdpp, vdpn, and emf can be vectors or scalars.
%        These parameters must all have the same number of rows and columns.

if nargin<5 || isempty(Rzone), Rzone = 12000;    end
if nargin<6 || isempty(patm),  patm = 101325;    end
if nargin<7 || isempty(dplen), dplen = 22*2.54e-2;  end
if nargin<8, verbose = false;  end

% --- Convert pressure and temperature data.  All routines are vectorized
dp = Omega05dp(vdpp);               %  Delta p across plenum:  Pa
p  = patm + dp;                     %  Absolute p upstream of nozzle, Pa
dpn = Omega10dp(vdpn);              %  Delta p across nozzle, Pa
T = Ttemp(emf,thermistorT(Rzone));  %  T upstream of nozzle, C

% --- Loop over converted data to compute flow rate.  nozzleFlow is not vectorized
Q = zeros(size(dn));   %  preallocate
for i=1:length(dn)
  Q(i) = nozzleFlow(dn(i),dplen,dpn(i),p(i),T(i));    % nominal flow rate
end

% --- Print table of results
if verbose
  fprintf('\n\nReduced flow bench data:\n\n');
  fprintf('     T1      dn         dp1      dpn         Q\n');
  fprintf('    (C)     (cm)        (Pa)     (Pa)      (m^3/s)\n');
  for i=1:length(dn)
    fprintf('%8.2f  %8.4f  %8.2f  %8.1f  %10.6f\n',T(i),dn(i)*100,dp(i),dpn(i),Q(i));
  end
end
