function tides(theYears, dataDirectory, plotDirectory, siteA, siteB)
%
%   tides(theYears, dataDirectory, plotDirectory, SiteA, siteB)
%
%   Function which goes through the DS74 V3 data files and performs the tidal
%   analysis on current/pressure data.  Individual deployment results are
%   created for theYears (cXXXX.txt or pXXXX.txt) and comparison tables of
%   the major tides are created for each site/depth (SITE_TIDE).  Takes in
%   the metadata siteA and siteB created with archive_mblt.

%This m-file written by Soupy Alexander (11/2003) for U.S. Geological 
%Survey Data Series 74, Version 2.0. Updated by Soupy Dalyander (8/2008) 
%for U.S. Geological Survey Data Series 74, Version 3.0 (citation below).
%Soupy Dalyander previously Soupy Alexander.  

%This Matlab m-file was used to create portions of U.S. Geological Survey 
%Data Series 74, Version 3.0. Although this program has been used by the 
%USGS, no warranty, expressed or implied, is made by the USGS or the 
%United States Government as to the accuracy and functioning of the 
%program and related program material nor shall the fact of distribution 
%constitute any such warranty, and no responsibility is assumed by the 
%USGS in connection therewith.

%Citation:  Butman, Bradford, Dalyander, P.S., Bothner, M.H., Borden,
%Jonathan, Casso, M.A., Gutierrez, B.T., Hastings, M.E., Lightsom, F.L., 
%Martini, M.A., Montgomery, E.T, Rendigs, R.R., and Strahle, W.S., 2008, 
%Long-Term Oceanographic Observations in Massachusetts Bay, 1989—2006: 
%U.S. Geological Survey Data Series 74, Version 3.0, DVD-ROM. 
%Also online at http://pubs.usgs.gov/ds/74/.

plotDirectoryA = [plotDirectory '\TTide\LT_A'];
plotDirectoryB = [plotDirectory '\TTide\LT_B'];

%Set up the empties - Pressure
pS2 = [];
pM2 = [];
pN2 = [];
pO1 = [];
pK1 = [];

%Empties - PCM
S2_5 = [];
M2_5 = [];
N2_5 = [];
O1_5 = [];
K1_5 = [];
S2_10 = [];
M2_10 = [];
N2_10 = [];
O1_10 = [];
K1_10 = [];
S2_1 = [];
M2_1 = [];
N2_1 = [];
O1_1 = [];
K1_1 = [];

%Empties - ADCP
S2_a5 = [];
M2_a5 = [];
N2_a5 = [];
O1_a5 = [];
K1_a5 = [];
S2_a15 = [];
M2_a15 = [];
N2_a15 = [];
O1_a15 = [];
K1_a15 = [];
S2_a22 = [];
M2_a22 = [];
N2_a22 = [];
O1_a22 = [];
K1_a22 = [];

%Load in the site A data
for indexFile = 1:length(siteA)
    fileVars = siteA(indexFile).theVars;
    fileTypes = siteA(indexFile).dataTypes;
    if strfind(siteA(indexFile).fileList, 'advb') || ...
            strncmp(siteA(indexFile).fileList, '7171', 4) || ...
            strncmp(siteA(indexFile).fileList, '7173', 4)
        continue
    end
    if strmatch('pressure', fileTypes)
        theFile = netcdf(fullfile([dataDirectory '\MBLT_A'], siteA(indexFile).fileList), 'nowrite');
        fileTime = singlejd(theFile{'time'}(:), theFile{'time2'}(:));
        theData = theFile{'P_4023'}(:);
        fileDepth = theFile{'depth'}(:);
        if isempty(theData)
            theData = theFile{'P_4022'}(:);
        end
        if isempty(theData)
            theData = theFile{'P_1'}(:)*100;
        end
        if isempty(theData)
            continue
        end
        theData(theData > 1e5) = NaN;
        fileTimeI = min(fileTime):1/24:max(fileTime);
        fileDataI = smart_interp(fileTime, theData, fileTimeI, 4);
        theLat = theFile{'lat'}(:);
        
        fileName = siteA(indexFile).fileList;
        moorNumber = fileName(1:4);
        
        startGreg = gregorian(min(fileTimeI));
        endGreg = gregorian(max(fileTimeI));
        if any(ismember(theYears, [startGreg(1) endGreg(1)]))
            [name, freq, tidecon, xout] = t_tide(fileDataI, 'interval', 1, 'start time', ...
                gregorian(min(fileTimeI)), 'latitude', theLat, 'output', ...
                fullfile(plotDirectoryA, ['p' moorNumber '.txt']));
                fID = fopen(fullfile(plotDirectoryA, ['p' moorNumber '.txt']), 'r+');
                theLinesOld = cell(0);
                theLines = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                theLinesOld = theLinesOld(:);
                fclose(fID);
                theLines{1} = 'Tidal Analysis of Pressure at LT-A';
                theLines{2} = ['Depth: ' num2str(fileDepth, '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryA, ['p' moorNumber '.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
        else
            [name, freq, tidecon, xout] = t_tide(fileDataI, 'interval', 1, 'start time', ...
                gregorian(min(fileTimeI)), 'latitude', theLat, 'output', 'none');
        end 
        
        
        inTide = strmatch('S2', name);
        if ~isempty(inTide)
            pS2 = [pS2; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]]; %#ok<AGROW>
        end
        inTide = strmatch('M2', name);
        if ~isempty(inTide)
            pM2 = [pM2; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
            inTide = strmatch('N2', name);
        end
        if ~isempty(inTide)
            pN2 = [pN2; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
            inTide = strmatch('O1', name);
        end
        if ~isempty(inTide)
            pO1 = [pO1; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
        end
        inTide = strmatch('K1', name);
        if ~isempty(inTide)
            pK1 = [pK1; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
        end
        close(theFile)
        clear theFile
    end
    if strmatch('u_1205', fileVars)
        theFile = netcdf(fullfile([dataDirectory '\MBLT_A'], siteA(indexFile).fileList), 'nowrite');
        fileDepth = theFile{'depth'}(:);
        fileTime = singlejd(theFile{'time'}(:), theFile{'time2'}(:));
        theLat = theFile{'lat'}(:);
        
        fileU = theFile{'u_1205'}(:);
        fileV = theFile{'v_1206'}(:);
        fileU(fileU > 1e5) = NaN;
        fileV(fileV > 1e5) = NaN;
        fileCurr = fileU + i*fileV;
        
        fileName = siteA(indexFile).fileList;
        moorNumber = fileName(1:4);
        
        startGreg = gregorian(min(fileTime));
        endGreg = gregorian(max(fileTime));
        waterDepth = theFile.WATER_DEPTH(1);
        
        if length(fileDepth) == 1
            if any(ismember(theYears, [startGreg(1) endGreg(1)]))
                [name, freq, tidecon, xout] = t_tide(fileCurr, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', ...
                    fullfile(plotDirectoryA, ['c' moorNumber '.txt']));
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '.txt']), 'r+');
                theLinesOld = cell(0);
                theLines = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                fclose(fID);
                theLinesOld = theLinesOld(:);
                theLines{1} = 'Tidal Analysis of Current at LT-A';
                theLines{2} = ['Point Current-Meter Observations, ' num2str(fileDepth, '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
            else
                [name, freq, tidecon, xout] = t_tide(fileCurr, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', 'none');
            end
            if fileDepth < 10
                inTide = strmatch('S2', name);
                if ~isempty(inTide)
                    S2_5 = [S2_5; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
                inTide = strmatch('M2', name);
                if ~isempty(inTide)
                    M2_5 = [M2_5; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                    inTide = strmatch('N2', name);
                end
                if ~isempty(inTide)
                    N2_5 = [N2_5; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                    inTide = strmatch('O1', name);
                end
                if ~isempty(inTide)
                    O1_5 = [O1_5; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
                inTide = strmatch('K1', name);
                if ~isempty(inTide)
                    K1_5 = [K1_5; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
            elseif ((waterDepth - fileDepth) > 5) && ((waterDepth - fileDepth) < 15)
                inTide = strmatch('S2', name);
                if ~isempty(inTide)
                    S2_10 = [S2_10; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
                inTide = strmatch('M2', name);
                if ~isempty(inTide)
                    M2_10 = [M2_10; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                    inTide = strmatch('N2', name);
                end
                if ~isempty(inTide)
                    N2_10 = [N2_10; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                    inTide = strmatch('O1', name);
                end
                if ~isempty(inTide)
                    O1_10 = [O1_10; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
                inTide = strmatch('K1', name);
                if ~isempty(inTide)
                    K1_10 = [K1_10; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end            
            elseif ((waterDepth - fileDepth) < 5)
                inTide = strmatch('S2', name);
                if ~isempty(inTide)
                    S2_1 = [S2_1; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
                inTide = strmatch('M2', name);
                if ~isempty(inTide)
                    M2_1 = [M2_1; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                    inTide = strmatch('N2', name);
                end
                if ~isempty(inTide)
                    N2_1 = [N2_1; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                    inTide = strmatch('O1', name);
                end
                if ~isempty(inTide)
                    O1_1 = [O1_1; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
                inTide = strmatch('K1', name);
                if ~isempty(inTide)
                    K1_1 = [K1_1; ...
                            [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                end
            end
            close(theFile)
            clear theFile
        else
            %continue
            surfDepth = value2index(fileDepth, 5);
            fileCurrHit = fileCurr(:,surfDepth);
            if any(ismember(theYears, [startGreg(1) endGreg(1)]))
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', ...
                    fullfile(plotDirectoryA, ['c' moorNumber '_5.txt']));
                
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '_5.txt']), 'r+');
                theLinesOld = cell(0);
                theLines = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                fclose(fID);
                theLinesOld = theLinesOld(:);
                theLines{1} = 'Tidal Analysis of Current at LT-A';
                theLines{2} = ['ADCP Observations, ' num2str(fileDepth(surfDepth), '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '_5.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
                
            else
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', 'none');
            end
            inTide = strmatch('S2', name);
            if ~isempty(inTide)
                S2_a5 = [S2_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('M2', name);
            if ~isempty(inTide)
                M2_a5 = [M2_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('N2', name);
            end
            if ~isempty(inTide)
                N2_a5 = [N2_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('O1', name);
            end
            if ~isempty(inTide)
                O1_a5 = [O1_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('K1', name);
            if ~isempty(inTide)
                K1_a5 = [K1_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            
            midDepth = value2index(fileDepth, 15);
            fileCurrHit = fileCurr(:,midDepth);
            if any(ismember(theYears, [startGreg(1) endGreg(1)]))
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', ...
                    fullfile(plotDirectoryA, ['c' moorNumber '_15.txt']));
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '_15.txt']), 'r+');
                theLinesOld = cell(0);
                theLines = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                fclose(fID);
                theLinesOld = theLinesOld(:);
                theLines{1} = 'Tidal Analysis of Current at LT-A';
                theLines{2} = ['ADCP Observations, ' num2str(fileDepth(midDepth), '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '_15.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
            else
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', 'none');
            end
            inTide = strmatch('S2', name);
            if ~isempty(inTide)
                S2_a15 = [S2_a15; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('M2', name);
            if ~isempty(inTide)
                M2_a15 = [M2_a15; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('N2', name);
            end
            if ~isempty(inTide)
                N2_a15 = [N2_a15; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('O1', name);
            end
            if ~isempty(inTide)
                O1_a15 = [O1_a15; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('K1', name);
            if ~isempty(inTide)
                K1_a15 = [K1_a15; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            
            deepDepth = value2Index(fileDepth, 22);
            fileCurrHit = fileCurr(:,deepDepth);
            if any(ismember(theYears, [startGreg(1) endGreg(1)]))
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', ...
                    fullfile(plotDirectoryA, ['c' moorNumber '_22.txt']));
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '_22.txt']), 'r+');
                theLinesOld = cell(0);
                theLines = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                fclose(fID);
                theLinesOld = theLinesOld(:);
                theLines{1} = 'Tidal Analysis of Current at LT-A';
                theLines{2} = ['ADCP Observations, ' num2str(fileDepth(deepDepth), '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryA, ['c' moorNumber '_22.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
            else
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', 'none');
            end
            inTide = strmatch('S2', name);
            if ~isempty(inTide)
                S2_a22 = [S2_a22; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('M2', name);
            if ~isempty(inTide)
                M2_a22 = [M2_a22; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('N2', name);
            end
            if ~isempty(inTide)
                N2_a22 = [N2_a22; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('O1', name);
            end
            if ~isempty(inTide)
                O1_a22 = [O1_a22; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('K1', name);
            if ~isempty(inTide)
                K1_a22 = [K1_a22; ...
                    [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            close(theFile)
            clear theFile
        end
    end
end


tideList = {'S2'; 'M2'; 'N2'; 'O1'; 'K1'};
for indexTide = 1:length(tideList)
    eval(['targTide = p' tideList{indexTide} ';']);
    [junk, I] = unique(targTide(:,1));
    targTide = targTide(I,:);
    
    excelFile = fullfile(plotDirectoryA, 'tide_a_pressure.xls');
    outCell = cell(4+length(targTide), 8);
    outCell{1,1} = ['LT-A, ' upper(tideList{indexTide}) ' Analysis of Pressure'];
    outCell{3,1} = 'Mooring Number';
    outCell{3,2} = 'Start Date';
    outCell{3,3} = 'End Date';
    outCell{3,4} = 'Duration';
    outCell{3,5} = 'Amplitude';
    outCell{3,6} = 'Amplitude Error';
    outCell{3,7} = 'Phase';
    outCell{3,8} = 'Phase Error';
    outCell{4,4} = '(days)';
    outCell{4,5} = '(mb)';
    outCell{4,6} = '(mb)';
    outCell{4,7} = '(degrees)';
    outCell{4,8} = '(degrees)';
    
    for index = 1:length(targTide)
        outCell{4+index,1} = num2str(targTide(index,1), '%.0f');
        outCell{4+index,2} = datestr(julian2datenum(targTide(index,2)), 1);
        outCell{4+index,3} = datestr(julian2datenum(targTide(index,3)), 1);
        outCell{4+index,4} = num2str(targTide(index,3)-targTide(index,2),'%.0f');
        outCell{4+index,5} = num2str(targTide(index,4), '%.2f');
        outCell{4+index,6} = num2str(targTide(index,5), '%.2f');
        outCell{4+index,7} = num2str(targTide(index,6), '%.2f');
        outCell{4+index,8} = num2str(targTide(index,7), '%.2f');
    end
    xlswrite(excelFile, outCell,upper(tideList{indexTide}));


    depthList = [5 22 31];
    theDepths = [5 10 1];
    for indexDepth = 1:3;
        excelFile = fullfile(plotDirectoryA, ['tide_a_pcm_' num2str(theDepths(indexDepth)) '.xls']);
        clear outCell
        eval(['targTide = ' tideList{indexTide} '_' num2str(theDepths(indexDepth)) ';']);
        [junk, I] = unique(targTide(:,1));
        targTide = targTide(I,:);

        outCell{1,1} = ['LT-A, ' upper(tideList{indexTide}) ' Analysis of Current (Point Current-Meter Data), ~' ...
            num2str(depthList(indexDepth)) ' m'];
        outCell{3,1} = 'Mooring Number';
        outCell{3,2} = 'Start Date';
        outCell{3,3} = 'End Date';
        outCell{3,4} = 'Duration';
        outCell{3,5} = 'Major Axis';
        outCell{3,6} = 'Major Error';
        outCell{3,7} = 'Minor Axis';
        outCell{3,8} = 'Minor Error';
        outCell{3,9} = 'Inclination';
        outCell{3,10} = 'Inclination Error';
        outCell{3,11} = 'Phase';
        outCell{3,12} = 'Phase Error';
        outCell{4,4} = '(days)';
        outCell{4,5} = '(cm/s)';
        outCell{4,6} = '(cm/s)';
        outCell{4,7} = '(cm/s)';
        outCell{4,8} = '(cm/s)';
        outCell{4,9} = '(degrees)';
        outCell{4,10} = '(degrees)';
        outCell{4,11} = '(degrees)';
        outCell{4,12} = '(degrees)';

        for index = 1:length(targTide)
            outCell{4+index,1} = num2str(targTide(index,1), '%.0f');
            outCell{4+index,2} = datestr(julian2datenum(targTide(index,2)), 1);
            outCell{4+index,3} = datestr(julian2datenum(targTide(index,3)), 1);
            outCell{4+index,4} = num2str(targTide(index,3)-targTide(index,2), '%.0f');
            outCell{4+index,5} = num2str(targTide(index,4), '%.2f');
            outCell{4+index,6} = num2str(targTide(index,5), '%.2f');
            outCell{4+index,7} = num2str(targTide(index,6), '%.2f');
            outCell{4+index,8} = num2str(targTide(index,7), '%.2f');
            outCell{4+index,9} = num2str(targTide(index,8), '%.2f');
            outCell{4+index,10} = num2str(targTide(index,9), '%.2f');
            outCell{4+index,11} = num2str(targTide(index,10), '%.2f') ;
            outCell{4+index,12} = num2str(targTide(index,11), '%.2f');
        end
        xlswrite(excelFile, outCell, upper(tideList{indexTide}));
        clear outCell
    end


    for indexDepth = [5 15 22]
            excelFile = fullfile(plotDirectoryA, ['tide_a_adcp_' num2str(indexDepth) '.xls']);
            clear outCell
        eval(['targTide = ' tideList{indexTide} '_a' num2str(indexDepth) ';']);
        [junk, I] = unique(targTide(:,1));
        targTide = targTide(I,:);
        
        outCell{1,1} = ['LT-A, ' upper(tideList{indexTide}) ' Analysis of Current (ADCP Data), ~' ...
            num2str(indexDepth) ' m'];
        outCell{3,1} = 'Mooring Number';
        outCell{3,2} = 'Start Date';
        outCell{3,3} = 'End Date';
        outCell{3,4} = 'Duration';
        outCell{3,5} = 'Major Axis';
        outCell{3,6} = 'Major Error';
        outCell{3,7} = 'Minor Axis';
        outCell{3,8} = 'Minor Error';
        outCell{3,9} = 'Inclination';
        outCell{3,10} = 'Inclination Error';
        outCell{3,11} = 'Phase';
        outCell{3,12} = 'Phase Error';
        outCell{4,4} = '(days)';
        outCell{4,5} = '(cm/s)';
        outCell{4,6} = '(cm/s)';
        outCell{4,7} = '(cm/s)';
        outCell{4,8} = '(cm/s)';
        outCell{4,9} = '(degrees)';
        outCell{4,10} = '(degrees)';
        outCell{4,11} = '(degrees)';
        outCell{4,12} = '(degrees)';

        for index = 1:length(targTide)

            outCell{4+index,1} = num2str(targTide(index,1), '%.0f');
            outCell{4+index,2} = datestr(julian2datenum(targTide(index,2)), 1);
            outCell{4+index,3} = datestr(julian2datenum(targTide(index,3)), 1);
            outCell{4+index,4} = num2str(targTide(index,3)-targTide(index,2), '%.0f');
            outCell{4+index,5} = num2str(targTide(index,4), '%.2f');
            outCell{4+index,6} = num2str(targTide(index,5), '%.2f');
            outCell{4+index,7} = num2str(targTide(index,6), '%.2f');
            outCell{4+index,8} = num2str(targTide(index,7), '%.2f');
            outCell{4+index,9} = num2str(targTide(index,8), '%.2f');
            outCell{4+index,10} = num2str(targTide(index,9), '%.2f');
            outCell{4+index,11} = num2str(targTide(index,10), '%.2f') ;
            outCell{4+index,12} = num2str(targTide(index,11), '%.2f');
        end
        xlswrite(excelFile, outCell,upper(tideList{indexTide}));
        clear outCell
    end
end

%Set up the empties - Pressure
pS2 = [];
pM2 = [];
pN2 = [];
pO1 = [];
pK1 = [];

%Empties - ADCP
S2_a5 = [];
M2_a5 = [];
N2_a5 = [];
O1_a5 = [];
K1_a5 = [];
S2_a10 = [];
M2_a10 = [];
N2_a10 = [];
O1_a10 = [];
K1_a10 = [];
S2_a17 = [];
M2_a17 = [];
N2_a17 = [];
O1_a17 = [];
K1_a17 = [];

%Load in the site B data
for indexFile = 1:length(siteB)
    fileVars = siteB(indexFile).theVars;
    if strmatch('P_4023', fileVars)
        theFile = netcdf(fullfile([dataDirectory '\MBLT_B'], siteB(indexFile).fileList), 'nowrite');
        fileTime = singlejd(theFile{'time'}(:), theFile{'time2'}(:));
        theData = theFile{'P_4023'}(:);
        fileDepth = theFile{'depth'}(:);
        theData(theData > 1e5) = NaN;
        if isempty(theData)
            continue
        end
        fileTimeI = min(fileTime):1/24:max(fileTime);
        fileDataI = smart_interp(fileTime, theData, fileTimeI, 4);
        theLat = theFile{'lat'}(:);
        
        fileName = siteB(indexFile).fileList;
        moorNumber = fileName(1:4);
        
        startGreg = gregorian(min(fileTimeI));
        endGreg = gregorian(max(fileTimeI));
        if any(ismember(theYears, [startGreg(1) endGreg(1)]))
            [name, freq, tidecon, xout] = t_tide(fileDataI, 'interval', 1, 'start time', ...
                gregorian(min(fileTimeI)), 'latitude', theLat, 'output', ...
                fullfile(plotDirectoryB, ['p' moorNumber '.txt']));

            fID = fopen(fullfile(plotDirectoryB, ['p' moorNumber '.txt']), 'r+');
            theLines = cell(0);
            theLinesOld = cell(0);
            while 1
                theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                if ~ischar(theLinesOld{length(theLinesOld)})
                    break
                end
            end
            theLinesOld = theLinesOld(:);
            fclose(fID);
            theLines{1} = 'Tidal Analysis of Pressure at LT-B';
            theLines{2} = ['Depth: ' num2str(fileDepth, '%.1f') ' m'];
            theLines{3} = ['Mooring Number: ' moorNumber];
            theLines{4} = ['File Name: ' fileName];
            theLines = [theLines(:); theLinesOld(4:end)];
            fID = fopen(fullfile(plotDirectoryB, ['p' moorNumber '.txt']), 'w');
            for index = 1:length(theLines)-1
                fwrite(fID, theLines{index});
                fprintf(fID, '\n');
            end
            fclose(fID);
        else
            [name, freq, tidecon, xout] = t_tide(fileDataI, 'interval', 1, 'start time', ...
                gregorian(min(fileTimeI)), 'latitude', theLat, 'output', 'none');
        end 
        
        
        inTide = strmatch('S2', name);
        if ~isempty(inTide)
            pS2 = [pS2; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
        end
        inTide = strmatch('M2', name);
        if ~isempty(inTide)
            pM2 = [pM2; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
            inTide = strmatch('N2', name);
        end
        if ~isempty(inTide)
            pN2 = [pN2; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
            inTide = strmatch('O1', name);
        end
        if ~isempty(inTide)
            pO1 = [pO1; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
        end
        inTide = strmatch('K1', name);
        if ~isempty(inTide)
            pK1 = [pK1; [str2double(moorNumber) min(fileTimeI) max(fileTimeI) tidecon(inTide,:)]];
        end
    elseif strmatch('u_1205', fileVars)
        theFile = netcdf(fullfile([dataDirectory '\MBLT_B'], siteB(indexFile).fileList), 'nowrite');
        fileDepth = theFile{'depth'}(:);
        fileTime = singlejd(theFile{'time'}(:), theFile{'time2'}(:));
        theLat = theFile{'lat'}(:);
        
        fileU = theFile{'u_1205'}(:);
        fileV = theFile{'v_1206'}(:);
        fileU(fileU > 1e5) = NaN;
        fileV(fileV > 1e5) = NaN;
        fileCurr = fileU + i*fileV;
        
        fileName = siteB(indexFile).fileList;
        moorNumber = fileName(1:4);
        
        startGreg = gregorian(min(fileTime));
        endGreg = gregorian(max(fileTime));
        surfDepth = value2index(fileDepth, 5);
            fileCurrHit = fileCurr(:,surfDepth);
            if any(ismember(theYears, [startGreg(1) endGreg(1)]))
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', ...
                    fullfile(plotDirectoryB, ['c' moorNumber '_5.txt']));

                fID = fopen(fullfile(plotDirectoryB, ['c' moorNumber '_5.txt']), 'r+');
                theLines = cell(0);
                theLinesOld = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                theLinesOld = theLinesOld(:);
                fclose(fID);
                theLines{1} = 'Tidal Analysis of Current at LT-B';
                theLines{2} = ['ADCP Observations, ' num2str(fileDepth(surfDepth), '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryB, ['c' moorNumber '_5.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
            else
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', 'none');
            end
            inTide = strmatch('S2', name);
            if ~isempty(inTide)
                S2_a5 = [S2_a5; ...
                    [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('M2', name);
            if ~isempty(inTide)
                M2_a5 = [M2_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('N2', name);
            end
            if ~isempty(inTide)
                N2_a5 = [N2_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('O1', name);
            end
            if ~isempty(inTide)
                O1_a5 = [O1_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('K1', name);
            if ~isempty(inTide)
                K1_a5 = [K1_a5; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            
            midDepth = value2index(fileDepth, 10);
            fileCurrHit = fileCurr(:,midDepth);
            if any(ismember(theYears, [startGreg(1) endGreg(1)]))
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', ...
                    fullfile(plotDirectoryB, ['c' moorNumber '_10.txt']));

                fID = fopen(fullfile(plotDirectoryB, ['c' moorNumber '_10.txt']), 'r+');
                theLines = cell(0);
                theLinesOld = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                theLinesOld = theLinesOld(:);
                fclose(fID);
                theLines{1} = 'Tidal Analysis of Current at LT-B';
                theLines{2} = ['ADCP Observations, ' num2str(fileDepth(midDepth), '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryB, ['c' moorNumber '_10.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
            else
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', 'none');
            end
            inTide = strmatch('S2', name);
            if ~isempty(inTide)
                S2_a10 = [S2_a10; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('M2', name);
            if ~isempty(inTide)
                M2_a10 = [M2_a10; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('N2', name);
            end
            if ~isempty(inTide)
                N2_a10 = [N2_a10; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('O1', name);
            end
            if ~isempty(inTide)
                O1_a10 = [O1_a10; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('K1', name);
            if ~isempty(inTide)
                K1_a10 = [K1_a10; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            
            deepDepth = value2Index(fileDepth, 17);
            fileCurrHit = fileCurr(:,deepDepth);
            if any(ismember(theYears, [startGreg(1) endGreg(1)]))
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', ...
                    fullfile(plotDirectoryB, ['c' moorNumber '_17.txt']));
                fID = fopen(fullfile(plotDirectoryB, ['c' moorNumber '_17.txt']), 'r+');
                theLines = cell(0);
                theLinesOld = cell(0);
                while 1
                    theLinesOld{length(theLinesOld)+1} = fgetl(fID);
                    if ~ischar(theLinesOld{length(theLinesOld)})
                        break
                    end
                end
                theLinesOld = theLinesOld(:);
                fclose(fID);
                theLines{1} = 'Tidal Analysis of Current at LT-B';
                theLines{2} = ['ADCP Observations, ' num2str(fileDepth(deepDepth), '%.1f') ' m'];
                theLines{3} = ['Mooring Number: ' moorNumber];
                theLines{4} = ['File Name: ' fileName];
                theLines = [theLines(:); theLinesOld(4:end)];
                fID = fopen(fullfile(plotDirectoryB, ['c' moorNumber '_17.txt']), 'w');
                for index = 1:length(theLines)-1
                    fwrite(fID, theLines{index});
                    fprintf(fID, '\n');
                end
                fclose(fID);
            else
                [name, freq, tidecon, xout] = t_tide(fileCurrHit, 'interval', 1, 'start time', ...
                    gregorian(min(fileTime)), 'latitude', theLat, 'output', 'none');
            end
            inTide = strmatch('S2', name);
            if ~isempty(inTide)
                S2_a17 = [S2_a17; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('M2', name);
            if ~isempty(inTide)
                M2_a17 = [M2_a17; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('N2', name);
            end
            if ~isempty(inTide)
                N2_a17 = [N2_a17; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
                inTide = strmatch('O1', name);
            end
            if ~isempty(inTide)
                O1_a17 = [O1_a17; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
            inTide = strmatch('K1', name);
            if ~isempty(inTide)
                K1_a17 = [K1_a17; ...
                        [str2double(moorNumber) min(fileTime) max(fileTime) tidecon(inTide,:)]];
            end
        
    end
end

tideList = {'S2'; 'M2'; 'N2'; 'O1'; 'K1'};
for indexTide = 1:length(tideList)
    outCell = cell(4+size(targTide,1), 8);
    eval(['targTide = p' tideList{indexTide} ';']);
    [junk, I] = unique(targTide(:,1));
    targTide = targTide(I,:);
    outCell{1,1} = ['LT-B, ' upper(tideList{indexTide}) ' Analysis of Pressure'];
    outCell{3,1} = 'Mooring Number';
    outCell{3,2} = 'Start Date';
    outCell{3,3} = 'End Date';
    outCell{3,4} = 'Duration';
    outCell{3,5} = 'Amplitude';
    outCell{3,6} = 'Amplitude Error';
    outCell{3,7} = 'Phase';
    outCell{3,8} = 'Phase Error';
    outCell{4,4} = '(days)';
    outCell{4,5} = '(mb)';
    outCell{4,6} = '(mb)';
    outCell{4,7} = '(degrees)';
    outCell{4,8} = '(degrees)';
    for index = 1:size(targTide,1)
        outCell{4+index,1} = num2str(targTide(index,1), '%.0f');
        outCell{4+index,2} = datestr(julian2datenum(targTide(index,2)), 1);
        outCell{4+index,3} = datestr(julian2datenum(targTide(index,3)), 1);
        outCell{4+index,4} = num2str(targTide(index,3)-targTide(index,2),'%.0f');
        outCell{4+index,5} = num2str(targTide(index,4), '%.2f');
        outCell{4+index,6} = num2str(targTide(index,5), '%.2f');
        outCell{4+index,7} = num2str(targTide(index,6), '%.2f');
        outCell{4+index,8} = num2str(targTide(index,7), '%.2f');
    end

    excelFile = fullfile(plotDirectoryB, 'tide_b_pressure.xls');
    xlswrite(excelFile, outCell,upper(tideList{indexTide}));
    
    for indexDepth = [5 10 17]
        excelFile = fullfile(plotDirectoryB, ['tide_b_adcp_' num2str(indexDepth) '.xls']);
        clear outCell
        eval(['targTide = ' tideList{indexTide} '_a' num2str(indexDepth) ';']);
        [junk, I] = unique(targTide(:,1));
        targTide = targTide(I,:);
        
        outCell{1,1} = ['LT-B, ' upper(tideList{indexTide}) ' Analysis of Current (ADCP Data), ~' ...
            num2str(indexDepth) ' m'];
        outCell{3,1} = 'Mooring Number';
        outCell{3,2} = 'Start Date';
        outCell{3,3} = 'End Date';
        outCell{3,4} = 'Duration';
        outCell{3,5} = 'Major Axis';
        outCell{3,6} = 'Major Error';
        outCell{3,7} = 'Minor Axis';
        outCell{3,8} = 'Minor Error';
        outCell{3,9} = 'Inclination';
        outCell{3,10} = 'Inclination Error';
        outCell{3,11} = 'Phase';
        outCell{3,12} = 'Phase Error';
        outCell{4,4} = '(days)';
        outCell{4,5} = '(cm/s)';
        outCell{4,6} = '(cm/s)';
        outCell{4,7} = '(cm/s)';
        outCell{4,8} = '(cm/s)';
        outCell{4,9} = '(degrees)';
        outCell{4,10} = '(degrees)';
        outCell{4,11} = '(degrees)';
        outCell{4,12} = '(degrees)';

        for index = 1:length(targTide)
            outCell{4+index,1} = num2str(targTide(index,1), '%.0f');
            outCell{4+index,2} = datestr(julian2datenum(targTide(index,2)), 1);
            outCell{4+index,3} = datestr(julian2datenum(targTide(index,3)), 1);
            outCell{4+index,4} = num2str(targTide(index,3)-targTide(index,2), '%.0f');
            outCell{4+index,5} = num2str(targTide(index,4), '%.2f');
            outCell{4+index,6} = num2str(targTide(index,5), '%.2f');
            outCell{4+index,7} = num2str(targTide(index,6), '%.2f');
            outCell{4+index,8} = num2str(targTide(index,7), '%.2f');
            outCell{4+index,9} = num2str(targTide(index,8), '%.2f');
            outCell{4+index,10} = num2str(targTide(index,9), '%.2f');
            outCell{4+index,11} = num2str(targTide(index,10), '%.2f') ;
            outCell{4+index,12} = num2str(targTide(index,11), '%.2f');
        end
        xlswrite(excelFile, outCell,upper(tideList{indexTide}));
        clear outCell
    end
end

