%CdAMWrapper
function [tDat,pDat,bDat,vDat,bFor,posDat,sysMass] = CdAMWrapper(fName)
    % close all;
    %constants
    rho = 1026; %kg/m^3
    D = 0.36;  %m
    mu = 1.08*10^-3;

    %If no inputted filename then ask for one
    if nargin == 0
        fName = input('Enter filename: ','s');
        weight = input('Input Measured Mass (g): ');
    end

    if strcmp(fName,'1')
        fName = 'Y:\Data\TestTankCPF\CdAMTestTankTest000NMS.xlsx';
        weight = 291.41*2;
    elseif strcmp(fName,'2')
        fName = 'Y:\Data\TestTankCPF\CdAMTestTankTest3g.xlsx';
        weight = 102.03*2;
    elseif strcmp(fName,'3')
        fName = 'Y:\Data\TestTankCPF\CdAMTestTankTest7p5g.xlsx';
        weight = 171.225*2;
    elseif strcmp(fName,'4')
        fName = 'Y:\Data\TestTankCPF\CdAMTestTankTest19p5g.xlsx';
        weight = 411.61*2;
    end

    %Try to open file. If invaild ask for new filename
    cont = 1;
    while cont == 1
        try
            [~, sheets] = xlsfinfo (fName);
            sheetInd = -1;
            for si = 1:length(sheets)
                if strcmp(sheets(si),'trialData')
                    sheetInd = si;
                end
            end
            if sheetInd == -1
                sheetInd = length(sheets);
            end
            cont = 0;
        catch
            disp('Invalid Filename')
            fName = input('Enter filename: ','s');
        end
    end

    %open data
    [ndata, heads] = xlsread(fName, sheetInd);

    %find dataTags
    pInd = 6;
    tInd = 1;
    bInd = 15;
    [~,numHeads] = size(heads);
    for si = 1:numHeads
        if strcmp(heads(1,si),'pressureDBar')
            pInd = si-1;
        elseif strcmp(heads(1,si),'Time (local)')
            tInd = si-1;
        elseif strcmp(heads(1,si),'bellowsPosition')
            bInd = si-1;
        end
    end

    %assign data
    pDat = ndata(:,pInd);
    tDat = ndata(:,tInd);
    bDat = ndata(:,bInd);

    %reformat time data to elapsed time in seconds
    tDat = (tDat - tDat(1)) * (60*60*24);
    fourFig = figure('color','w');
    pos = get(fourFig, 'Position');
    set(fourFig,'Position',pos + [0 -530 400 530]);

    %plot pressure
    subplot(4,1,1)
    plot(tDat,pDat)
    title(['Test Tank Data: ' fName])
    ylabel('Pressure (dBar)')

    %plot belllows
    subplot(4,1,2)
    plot(tDat,bDat)
    ylabel('Bellows Position (mm)')

    %plot velocity
    subplot(4,1,3)
    tStep = tDat(2)-tDat(1);
    pShift = [pDat(1); pDat(1:end-1)];
    vDat = (pDat-pShift)*0.0986923266716*10/tStep;

    %apply low pass filter to velocity data
    %configure the filter
    d = fdesign.lowpass('N,Fc',2,20/85);
    Hd = design(d,'butter');
    filtVDat = sqrt(filter(Hd, vDat));

    plot(tDat,filtVDat)
    ylabel('Velocity (m/sec)')
    xlabel('Elapsed time (sec)')

    reyn = rho*filtVDat*D/mu;
    subplot(4,1,4)
    plot(tDat,reyn);
    xlabel('sec')
    ylabel('Reynolds Number')

    %convert MTS reading to bellows position
    bDat = bDat - 34.798;

    %calculate buoyant force
    bFor = zeros(1,length(bDat));
    for bi = 1:length(bDat)
        bFor(bi) = bellowsFill(bDat(bi)/1000)*10050; %N
    end

    %calculate system mass
    % sysMass = weight/1000+bFor(1)/9.81; %kg
    % sysMass = weight/1000+50.8023+.473058104; %kg
    sysMass = weight/1000+50.8023; %kg

    posDat = pDat-pDat(1);
end





