%efficiencyTesting.m
function cutDat = efficiencyTesting(tStr)
    %constants
    mtsConv = 34.798; %mm
    pumpDisp = .692*10^-6; %cubic meters per rev
    shuntRes = .05;%ohms

    %get data from spreadsheet and plot
    [tDat,pDat,cylPos,belPos,cur,motV,iq] = efficiencyBEwrapper(tStr);
    figure('color','w')
    hold on;
    plot(tDat,belPos);
    xlabel('Elapsed Time (sec)');
    ylabel('Pressure (mbar)');

%     ask user to select portion of data
    title('Secect Data segment to use')
    ind1 = pointToInd([tDat;belPos]);
    hold on
    plot(tDat(ind1),belPos(ind1),'r.');
    ind2 = pointToInd([tDat;belPos]);
    plot(tDat(ind1:ind2),belPos(ind1:ind2),'r.');
    pause(1);
    clf;


    %group data
    numDat = (ind2-ind1+1);
    cutDat = zeros(numDat,7);
    cutDat(:,1) = tDat(ind1:ind2)';
    cutDat(:,2) = belPos(ind1:ind2);
    cutDat(:,3) = pDat(ind1:ind2);
    cutDat(:,4) = cylPos(ind1:ind2);
    cutDat(:,5) = cur(ind1:ind2);
    cutDat(:,6) = motV(ind1:ind2);
    cutDat(:,7) = iq(ind1:ind2);

%     %replot standard data
    subplot(2,1,1);
    plot(cutDat(:,1),cutDat(:,2));
    ylabel('Bellows Position (mm)');

    subplot(2,1,2);
    plot(cutDat(:,1),cutDat(:,6) - mtsConv);
    ylabel('Motor Velocity (rpm)')
    xlabel('Elapsed Time (sec)');

    %~~~~~~~~~~~Volumetric Efficiency~~~~~~~~~~~~~~~~
    volIn = abs(cutDat(:,6))*pumpDisp/60; %cubic meters/ sec
    volInst = zeros(numDat,1);
    volOut = volInst;
    for ii = 1:(numDat)
        volInst(ii) = bellowsFill(cutDat(ii,2)./1000); %cubic meters
    end
    for ii = 2:(numDat-1)
        cSlop = polyfit(cutDat(((ii-1):(ii+1)),1),volInst((ii-1):(ii+1)),1);
        volOut(ii) = cSlop(1);
    end
    volOut(1) = volOut(2);
    volOut(end) = volOut(end-1);
    volEff = volOut./volIn;
    figure('color','w');
    hold on;
    plot(cutDat(:,3),volEff,'k.');
    axis([0 250 0 1])
    xlabel('Pressure (mbar)');
    ylabel('Volumetric Efficiency(m^3/m^3)');
    title('Volumetric Efficiency v. Pressure')

    %~~~~~~~~~~~Energy Efficiency~~~~~~~~~~~~~~~~
    workOut = volOut.*(cutDat(:,3)*100); %J
    workIn = shuntRes*cutDat(:,5).^2; %J
    enEff = workOut./workIn;
    figure('color','w');
    hold on;
    plot(cutDat(:,3),enEff,'k.');
    axis([0 250 0 1])
    xlabel('Pressure (mbar)');
    ylabel('Energy Efficiency(J/J)');
    title('Energy Efficiency v. Pressure')
end


