function data_available_plots_wconcat(theYears, dataDirectory, plotDirectory, concatDirectory, siteA, siteB)
%
%   data_available(theYears, dataDirectory, plotDirectory, concatDirectory, siteA, siteB);
%
%Function to create the data available plot for DS74 V3 for
%theYears specified.  Uses the metadata archives siteA and siteB, created
%with archive_mblt.m

%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/.

plotVars = {'current'; 'temperature'; 'salinity'; 'attenuation'; ...
    'pressure'; 'oxygen'; 'chlorophyll'; 'chlorophyll-a'};

%Pull the concatenated file listings so these files can be delineated
listFolder = 'D:\USGS\MASS_BAY\MBLT_VERSION3\CONC_FILES';
depthsA = {'4-10 m'; '10 mab'; '1-2 mab'};
concatMatA = cell(length(plotVars), length(depthsA)+1);
for index = 1:length(plotVars)
    for index2 = 1:length(depthsA)
        thisVar = plotVars{index};
        if (strcmpi(thisVar, 'pressure') || ...
                strcmpi(thisVar, 'sdp')) && ...
                strcmp(depthsA{index2}, '10 mab') || ...
                strcmpi(thisVar, 'oxygen') || ...
                strcmpi(thisVar, 'chlorophyll') || ...
                strcmpi(thisVar, 'chlorophyll-a')
            concatMatA{index, index2} = ' ';
            continue
        end
        [nums, theText] = xlsread(fullfile(listFolder, ...
            [lower(thisVar) 'A_c.xls']), depthsA{index2});
        text2 = theText(2:end,:);
        text3 = theText(1,:);
        fileList = text2(nums(:,1)==1,1);
        concatMatA{index, index2} = fileList;
    end
end
[theNums, theText] = xlsread...
    (fullfile(concatDirectory, 'adcpA_c'));
toUse = theNums(:,1);
fileList = theText(2:end,1);
concatMatA{1,4} = fileList(theNums(:,1) == 1);
concatMatA{2,4} = ' ';
concatMatA{3,4} = ' ';
concatMatA{4,4} = ' ';
concatMatA{5,4} = ' ';
concatMatA{6,4} = ' ';
concatMatA{7,4} = ' ';
concatMatA{8,4} = ' ';
concatMatA{9,4} = ' ';

depthsB = {'10 mab'; '1-2 mab'};
concatMatB = cell(length(plotVars), length(depthsB)+1);
for index = 1:length(plotVars)
    for index2 = 1:length(depthsB)
        thisVar = plotVars{index};
        if (strcmpi(thisVar, 'pressure') || ...
                strcmpi(thisVar, 'sdp') || ...
                strcmpi(thisVar, 'attenuation')) && ...
                strcmp(depthsB{index2}, '10 mab') || ...
                strcmpi(thisVar, 'current') || ...
                strcmpi(thisVar, 'oxygen') || ...
                strcmpi(thisVar, 'chlorophyll') || ...
                strcmpi(thisVar, 'chlorophyll-a')
            concatMatB{index, index2} = ' ';
            continue
        end
        [nums, theText] = xlsread(fullfile(listFolder, ...
            [lower(thisVar) 'B_c.xls']), depthsB{index2});
        text2 = theText(2:end,:);
        text3 = theText(1,:);
        varCol = strmatch('Target', text3);
        fileList = text2(nums(:,1)==1,1);
        concatMatB{index, index2} = fileList;
    end
end
[theNums, theText] = xlsread...
    (fullfile(concatDirectory, 'adcpB_c'));
toUse = theNums(:,1);
fileList = theText(2:end,1);
concatMatB{1,3} = fileList(theNums(:,1) == 1);
concatMatB{2,3} = ' ';
concatMatB{3,3} = ' ';
concatMatB{4,3} = ' ';
concatMatB{5,3} = ' ';
concatMatB{6,3} = ' ';

%Set up the order of the plots
plotVarNames = {'Current'; 'Temperature'; 'Salinity'; 'Attenuation'; 'Pressure'; ...
        'Oxygen'; 'Chlorophyll'; ' '; 'Sediment'; 'Collection'};
plotLocs = [51 45 40 35 30 25 22 21];
trapSpot = 18;
theColors = {'c'; 'r'; 'g'};
t = [];

[theNums, theText] = xlsread(fullfile(dataDirectory, 'all_traps.xls'), ...
    'Site A Traps');
trapType = theText(4:end,9);
trapIDA = theText(4:end,1);
for index = 1:length(trapType)
    if isempty(trapType{index})
        trapTypeA(index) = {' '};
    end
end
theHonjosA = strmatch('honjo', lower(trapType));
theTubesA = strmatch('tube', lower(trapType));

for index = 4:size(theText,1);
    try
        startTimes(index-3) = datenum(theText{index,3});
    catch
        startTimes(index-3) = NaN;
    end
    try 
        endTimes(index-3) = datenum(theText{index,4});
    catch
        endTimes(index-3) = NaN;
        trapIDA{index-3} = ' ';
    end
end

trapDepth = theNums(:,7);
honjoA_deploy = startTimes(theHonjosA);
honjoA_recover = endTimes(theHonjosA);
honjoA_depth = trapDepth(theHonjosA);
tubeA_deploy = startTimes(theTubesA);
tubeA_recover = endTimes(theTubesA);
tubeA_depth = trapDepth(theTubesA);
    
    
%Sediment trap data
[theNums, theText] = xlsread(fullfile(dataDirectory, 'all_traps.xls'), ...
    'Site B Traps');
%theNums = [repmat(NaN, 1, size(theNums, 2)); theNums];
trapType = theText(4:end,9);
trapIDB = theText(4:end,1);
for index = 1:length(trapType)
    if isempty(trapType{index})
        trapTypeB(index) = {' '};
    end
end

theHonjosB = strmatch('honjo', lower(trapType));
theTubesB = strmatch('tube', lower(trapType));

for index = 4:size(theText,1);
    try
        startTimes(index-3) = datenum(theText{index,3});
    catch
        startTimes(index-3) = NaN;
    end
    try 
        endTimes(index-3) = datenum(theText{index,4});
    catch
        endTimes(index-3) = NaN;
        trapIDB{index-3} = ' ';
    end
end


trapDepth = theNums(:,7);
honjoB_deploy = startTimes(theHonjosB);
honjoB_recover = endTimes(theHonjosB);
honjoB_depth = trapDepth(theHonjosB);
tubeB_deploy = startTimes(theTubesB);
tubeB_recover = endTimes(theTubesB);
tubeB_depth = trapDepth(theTubesB);

tubeLowsA = [0 2.00001 5.00001 15];
tubeHighsA = [2 5 8 17];

tubeLowsB = [0 2.00001 5.00001];
tubeHighsB = [2 5 8];

timeLowsA = [17.00001 5.00001 0];
timeHighsA = [50 17 5];

timeLowsB = [3000 8.00001 0];
timeHighsB = [5000 20 8];

[cruiseNums, cruiseText] = ...
    xlsread(fullfile(dataDirectory, 'cruises_mabay_v2.xls'));
cruiseNumber = cruiseNums;
cruiseShip = cruiseText(2:end,2);
cruiseStart = datenum(cruiseText(2:end,3));
[crYY, crMM, crDD, crHH, crMM, crSS] = datevec(cruiseStart);

isA = repmat(0, size(cruiseStart));
isB = repmat(0, size(cruiseStart));
isA(strmatch('LT-A', cruiseText(2:end,5))) = 1;
isB(strmatch('LT-B', cruiseText(2:end,6))) = 1;

cruiseShip(strmatch('WHITE HEATH', cruiseShip)) = {'WH'};
cruiseShip(strmatch('MARCUS HANNA', cruiseShip)) = {'MH'};
cruiseShip(strmatch('CONNECTICUT', cruiseShip)) = {'CN'};
cruiseShip(strmatch('CHRISTOPHER ANDREW', cruiseShip)) = {'CA'};
cruiseShip(strmatch('ABBIE BURGESS', cruiseShip)) = {'AB'};
cruiseShip(strmatch('ARGO MAINE', cruiseShip)) = {'AM'};
cruiseShip(strmatch('LOONEY', cruiseShip)) = {'L'};

xLabels = {'JAN.'; 'FEB.'; 'MAR.'; 'APR.'; 'MAY'; 'JUNE'; 'JULY'; 'AUG.'; ...
    'SEPT.'; 'OCT.'; 'NOV.'; 'DEC.'};

for indexYear = 1:length(theYears)
    targetYear = theYears(indexYear);

    t = [];
    t2 = [];
    figure, orient landscape, hold on
    set(gca, 'fontname', 'arial', 'fontsize', 8)


    for indexVar = 1:length(plotVars)
        moorHit = {};
        targetVar = plotVars{indexVar};

        %Site A
        for indexFile = 1:length(siteA)
            fileName = siteA(indexFile).fileList;
            fileMoor = fileName(1:4);

            startTime = siteA(indexFile).startTime;
            gregStart = gregorian(startTime);
            endTime = siteA(indexFile).endTime;
            gregEnd = gregorian(endTime);
            inYears = gregStart(1):1:gregEnd(1);

            theVars = siteA(indexFile).dataTypes;

            %If the data file doesn't have any years of interest, skip it
            if ~ismember(targetYear, inYears) || ...
                    ~ismember(targetVar, theVars)
                continue
            end

            moorHit = [moorHit(:); {fileMoor}];

            matStart = julian2datenum(startTime);
            matEnd = julian2datenum(endTime);

            if (indexVar == 1) && ...
                    (~isempty(findstr('adc', fileName)) || ~isempty(findstr('wh', fileName)))    %ADCP stuff
                p = plot([max([matStart datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                    min([matEnd datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                    [plotLocs(1)-4 plotLocs(1)-4], 'y');
                set(p, 'linewidth', 7)

                [yy, mm, dd, hh, mm, ss] = datevec(matStart);
                if yy == targetYear
                    theDash = findstr('-', fileName);
                    if (ismember(fileName, concatMatA{1,4}))
                        t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), ...
                            plotLocs(1) - 4, ...
                            [fileName(1:(theDash-1)) '*']);
                    else
                        t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), ...
                            plotLocs(1) - 4, ...
                            fileName(1:(theDash-1)));
                    end
                    set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                        'horiz', 'left', 'fontname', 'arial');
                end
                continue
            end
            
            if (~isempty(findstr('adc', fileName)) || ~isempty(findstr('wh', fileName)))
                hitDepth = 3;
            else
                theDepth = siteA(indexFile).instDepth;
                if isempty(theDepth) || isnan(theDepth)
                    theDepth = siteA(indexFile).fileDepth;
                end
                waterDepth = siteA(indexFile).waterDepth;
                if isnan(waterDepth)
                    waterDepth = 34;
                end
                distFrom = waterDepth - theDepth;
                if distFrom < 0
                    distFrom = 0;
                end
                %targDepths = [24 10 1];
                %targDepthDist = abs(targDepths - distFrom);
                %[junk, hitDepth] = min(targDepthDist);
                hitDepth = find((distFrom >= timeLowsA) & ...
                    (distFrom <= timeHighsA));
                if strncmp(fileName, '4313', 4)
                    hitDepth = 3;
                end
                if ismember(targetVar, {'oxygen'; 'chlorophyll'; 'chlorophyll-a'})
                    hitDepth = 1;
                end
            end
    
            
            if ismember(targetVar, {'oxygen'})
                p = plot([max([matStart datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                    min([matEnd datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                    [plotLocs(indexVar)-1 plotLocs(indexVar)-1], ...
                    theColors{3});
            else
                p = plot([max([matStart datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                    min([matEnd datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                    [plotLocs(indexVar)-hitDepth plotLocs(indexVar)-hitDepth], ...
                    theColors{hitDepth});
            end
            set(p, 'linewidth', 7)
            [yy, mm, dd, hh, mm, ss] = datevec(matStart);
            if yy == targetYear
                theDash = findstr('-', fileName);

                if ismember(fileName, concatMatA{indexVar, hitDepth})
                    t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), plotLocs(indexVar)-hitDepth, [fileName(1:(theDash-1)) '*']);
                else
                    t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), ...
                        plotLocs(indexVar)-hitDepth, ...
                        fileName(1:(theDash-1)));
                end
                set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                    'horiz', 'left', 'fontname', 'arial');
            end
        end
    end

    for index = 1:length(honjoA_deploy)
        [yy,mm,dd,hh,mm,ss] = datevec(honjoA_deploy(index));
        [yy2,mm,dd,hh,mm,ss] = datevec(honjoA_recover(index));
        if ismember(targetYear, [yy yy2])
            p = plot...
                ([max([honjoA_deploy(index) datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                min([honjoA_recover(index) datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                [trapSpot trapSpot], 'm');
            set(p, 'linewidth', 7)
        end
        thisText = trapIDA{theHonjosA(index)};
        findDash = findstr(thisText, '-');
        if index+1 > length(theHonjosA)
            continue
        end
        nextText = trapIDA{theHonjosA(index+1)};
        findDash2 = findstr(nextText, '-');
        thisLead = thisText(1:findDash);
        nextLead = nextText(1:findDash2);
        [yy3,mm,dd,hh,mm,ss] = datevec(honjoA_recover(index+1));
        if (str2double(thisText(findDash+1:end)) == 1) || ...
                (strncmp(thisText, 'W520', 4) && (str2double(thisText(findDash+1:end)) == 3))
            toPrint = [thisText(1:findDash) 'i'];
            startTime = honjoA_deploy(index);
        elseif (~strcmp(thisLead, nextLead) && (yy == targetYear)) || ...
                ((yy == targetYear) && (yy2 ~= targetYear)) || ...
                (index == length(honjoA_deploy)-1)
            endTime = min([honjoA_recover(index) datenum(targetYear, 12, 30, 21, 00, 00)]);

            [yy, mm, dd, hh, mm, ss] = datevec(startTime);
            if yy == targetYear
                t(length(t)+1) = text(startTime, trapSpot, ...
                    toPrint);
                set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                    'horiz', 'left', 'fontname', 'arial');
            end
        end
    end


    for index = 1:length(tubeA_deploy)
        [yy,mm,dd,hh,mm,ss] = datevec(tubeA_deploy(index));
        [yy2,mm,dd,hh,mm,ss] = datevec(tubeA_recover(index));
        if ismember(targetYear, [yy yy2])
            tubeSpot = find((tubeA_depth(index) >= tubeLowsA) && ...
                (tubeA_depth(index) <= tubeHighsA));
            if isempty(tubeSpot)
                continue
            end
            p = plot...
                ([max([tubeA_deploy(index) datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                min([tubeA_recover(index) datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                [(trapSpot-5.5)+tubeSpot (trapSpot-5.5)+tubeSpot]);
            set(p, 'linewidth', 7, 'color', [0.6 0.6 0.6])
            if yy == targetYear
                t(length(t)+1) = text(max([tubeA_deploy(index) datenum(targetYear, 1, 2)]),...
                    (trapSpot-5.5)+tubeSpot, ...
                    trapIDA{theTubesA(index)});
                set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                    'horiz', 'left', 'fontname', 'arial');
            end
        end
    end

    t2 = [];
    xList = [];
    yList = [];
    for index = 1:length(t)
        try
            thisSpot = get(t(index), 'position');
            thisString = get(t(index), 'string');
        catch
            continue
        end
        bads = find(isspace(thisString));
        if ~isempty(bads)
            thisString(bads) = [];
        end
        if isempty(thisString)
            continue
        end
        t2(length(t2)+1) = text(thisSpot(1), thisSpot(2), thisString);
        set(t2(end), 'fontsize', 8, 'color', 'k', 'clipping', 'off', ...
            'horiz', 'left', 'fontname', 'arial')
        delete(t(index))
        thisSpot = get(t2(end), 'position');
        xList(length(t2)) = thisSpot(1);
        yList(length(t2)) = thisSpot(2);
        test = str2double(thisString(1:min([4 length(thisString)])));
        if isempty(test)
            test = str2double(thisString(2:end));
            if isempty(test) || ~isreal(test)
                test = str2double(thisString(2:end-2));
            end
        end
        moorList(length(t2)) = test;
        stringList(length(t2)) = {thisString};
    end

    allYs = unique(yList);
    for index = 1:length(allYs)
        thisY = find(yList == allYs(index));
        thisX = xList(thisY);
        thisMoor = moorList(thisY);
        thisStrings = stringList(thisY);
        [sortX, I] = sort(thisX);
        sortY = thisY(I);
        sortMoor = thisMoor(I);
        sortStrings = thisStrings(I);
        hitList = 1:length(thisY);
        index2 = 1;
        lastSpotOld = 0;
        while index2 < length(thisY)
            thisX = xList(thisY);
            sortX = thisX(I);
            if isnan(hitList(index2))
                continue
            end
            withinThis = find(((sortX > (sortX(index2)-20)) && ...
                (sortX < (sortX(index2) + 20))) || ...
                (sortMoor == sortMoor(index2)));
            theseXs = sortX(withinThis(:))';
            theseYs = sortY(withinThis(:))';
            theseMoors = sortMoor(withinThis(:))';
            theseStrings = sortStrings(withinThis(:))';
            [theseMoors, I2] = sort(theseMoors);
            theseXs = theseXs(I2);
            theseYs = theseYs(I2);
            thisStrings = theseStrings(I2);
            lastSpot = min(theseXs);
            theOrder = 1:length(theseXs);
            if allYs(index) == 42
                index4 = 1;
                findADCP = strfind(thisStrings, 'adc');
                while index4 < length(findADCP)
                    if isempty(findADCP{index4})
                        index4 = index4+1;
                        continue
                    else
                        theOrder = [theOrder(1:index4-1) ...
                            theOrder(index4+1:end) theOrder(index4)];
                        index4 = index4+1;
                    end
                end
            end
            for index5 = 1:length(theOrder)
                index3 = theOrder(index5);
                set(t2(theseYs(index3)), 'position', [lastSpot allYs(index)]);
                xList(theseYs(index3)) = lastSpot;

                thisLength = length(thisStrings{index3});
                lastSpot = lastSpot + 3*(thisLength)+2;
            end
            index2 = max(withinThis)+1;
        end
    end

    inThis = find((crYY == targetYear) && isA);
    for index = 1:length(inThis)
        l = line([cruiseStart(inThis(index)) cruiseStart(inThis(index))], ...
            [13 52]);
        set(l, 'color', 'k', 'linestyle', '-.')
        t = text(cruiseStart(inThis(index)), 53.1, ...
            {[num2str(cruiseNumber(inThis(index)), '%05d') ' ']; cruiseShip{inThis(index)}});
        set(t, 'fontsize', 8, 'fontname', 'arial')
    end

    %Plot parameters
    axis([datenum(targetYear, 1, 1) datenum(targetYear, 12, 31) 13 52])
    theTicks = [];
    for index = 1:12
        theTicks = [theTicks(:); datenum(targetYear, index, 1)];
    end
    set(gca, 'xtick', theTicks, 'layer', 'top')
    set(gca, 'xticklabel', xLabels)
    a = annotation('textbox', [0.44 0.355 1 0.1], 'string', num2str(targetYear), ...
        'linestyle', 'none', 'fontname', 'arial', 'fontsize', 8, ...
        'horiz', 'center', 'vert', 'bottom');
    plotLocs = plotLocs(:);
    set(gca, 'ytick', flipud([plotLocs - 0.75; [trapSpot+0.3; trapSpot-0.7]]), 'yticklabel', flipud(plotVarNames))
    pt = text(min(xlim)-1.2, 20.2, 'Chlorophyll\it-a');
    set(pt, 'fontname', 'arial', 'fontsize', 8, 'horiz', 'right')
    box('on')


    bigAx = gca;
    axPosition = get(bigAx, 'Position');
    axPositionNew = axPosition;
    axPositionNew(2) = axPosition(2) + 0.23*axPosition(4);
    axPositionNew(4) = axPosition(4)*0.75;
    axPositionNew(1) = axPosition(1) + axPosition(1)*0.05;
    set(bigAx, 'Position', axPositionNew)

    axPosition(4) = axPosition(4)*0.2;
    axPosition(2) = axPosition(1) - 0.05;
    lilAx = subplot('position', axPosition);
    hold on
    set(lilAx, 'fontname', 'arial', 'fontsize', 8)

    ex = text(6, 8, 'EXPLANATION');
    set(ex, 'fontweight', 'bold', 'fontname', 'arial', 'fontsize', 8, 'horiz', 'center')
    
    y1 = text(0, 7, 'Nominal measurement depth');
    set(y1, 'fontweight', 'bold', 'fontname', 'arial', 'fontsize', 8)
    p1 = plot([0 1], [6 6], 'c');
    set(p1, 'linewidth', 7)
    y2 = text(1.5, 6, '4-10 meters');
    set(y2, 'fontname', 'arial', 'fontsize', 8)
    p2 = plot([0 1], [5 5], 'r');
    set(p2, 'linewidth', 7)
    y3 = text(1.5, 5, '22 meters (10 meters above bottom)');
    set(y3, 'fontname', 'arial', 'fontsize', 8)
    p3 = plot([0 1], [4 4], 'g');
    set(p3, 'linewidth', 7)
    y4 = text(1.5, 4, '30 meters (1-4 meters above bottom)');
    set(y4, 'fontname', 'arial', 'fontsize', 8)
    p4 = plot([0 1], [3 3], 'y');
    set(p4, 'linewidth', 7)
    y5 = text(1.5, 3, '2 to 24 meters (30-8 meters above bottom) (ADCP)');
    set(y5, 'fontname', 'arial', 'fontsize', 8)
    p6 = plot([0 1], [2 2], 'm');
    set(p6, 'linewidth', 7)
    y6 = text(1.5, 2, '4 meters above bottom (Honjo trap)');
    set(y6, 'fontname', 'arial', 'fontsize', 8)
    p7 = plot([0 1], [1 1]);
    set(p7, 'linewidth', 7, 'color', [0.6 0.6 0.6])
    y7 = text(1.5, 1, '15-17, 5-8, 2-5, and 0-2 meters above bottom (tube traps)');
    set(y7, 'fontname', 'arial', 'fontsize', 8)
    
    n =9.25;
    t0 = text(n+0.7, 7, 'Cruises');
    set(t0, 'fontname', 'arial', 'fontsize', 8, 'fontweight', 'bold', 'horiz', 'center');
    t1 = text(n+0.7, 6, 'YYNNN - USGS cruise identifier');
    set(t1, 'fontname', 'arial', 'fontsize', 8, 'horiz', 'center')
    t1b = text(n+0.7, 5, 'Ships');
    set(t1b, 'fontname', 'arial', 'fontsize', 8, 'fontweight', 'bold', 'horiz', 'center');
    t2 = text(n-1, 4, 'WH - White Heath');
    set(t2, 'fontname', 'arial', 'fontsize', 8)
    t3 = text(n-1, 3, 'MH - Marcus Hanna');
    set(t3, 'fontname', 'arial', 'fontsize', 8)
    t4 = text(n+1, 3, 'CN - Connecticut');
    set(t4, 'fontname', 'arial', 'fontsize', 8)
    t5 = text(n+1, 4, 'CA - Christopher Andrew');
    set(t5, 'fontname', 'arial', 'fontsize', 8)
    t6 = text(n-1, 2, 'AB - Abbie Burgess');
    set(t6, 'fontname', 'arial', 'fontsize', 8)
    t7 = text(n+1, 2, 'AM - Argo Maine');
    set(t7, 'fontname', 'arial', 'fontsize', 8)
    t8 = text(n+0.7, 1, 'L - Looney');
    set(t8, 'fontname', 'arial', 'fontsize', 8, 'horiz', 'center')


    axis([0 12 0 8])
    axis('off')
    printTitle = ['a_dcov_' num2str(targetYear, '%.0f')];
    axisTitle = ['LT-A Data Coverage, ' num2str(targetYear, '%.0f')];
    s = suptitle(axisTitle);
    set(s, 'fontname', 'arial', 'fontsize', 12);
    subPos = get(s, 'position');
    subPos(2) = subPos(2) - 0.03;
    set(s, 'position', subPos);
    print(gcf, '-dpdf', fullfile(plotDirectory, printTitle));

    close

    %%%%%%%%%%%%%%%%%%%%Site B%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    if (targetYear < 1997) || (targetYear > 2004)
        continue
    end

    t = [];
    t2 = [];
    figure, orient landscape, hold on
    set(gca, 'fontname', 'arial', 'fontsize', 8)

    for indexVar = 1:length(plotVars)
        moorHit = {};
        targetVar = plotVars{indexVar};

        %Site B
        for indexFile = 1:length(siteB)
            fileName = siteB(indexFile).fileList;
                       
            fileMoor = fileName(1:4);

            startTime = siteB(indexFile).startTime;
            gregStart = gregorian(startTime);
            endTime = siteB(indexFile).endTime;
            gregEnd = gregorian(endTime);
            inYears = gregStart(1):1:gregEnd(1);

            theVars = siteB(indexFile).dataTypes;

            %If the data file doesn't have any years of interest, skip it
            if ~ismember(targetYear, inYears) || ...
                    ~ismember(targetVar, theVars)
                continue
            end

            moorHit = [moorHit(:); {fileMoor}];

            matStart = julian2datenum(startTime);
            matEnd = julian2datenum(endTime);

            if (indexVar == 1) && ...
                    (~isempty(findstr('adc', fileName)) || ~isempty(findstr('wh', fileName)))    %ADCP stuff
                
                if ismember(fileName, {'6151adc-a1h.nc'; '6281adc-a1h.nc'; ...
                        '6361adc-a1h.nc'; '6871adc-a1h.nc'; '6351adc-a1h.nc'})
                    theSep = 5;
                else
                    theSep = 4;
                end

                p = plot([max([matStart datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                    min([matEnd datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                    [plotLocs(1)-theSep plotLocs(1)-theSep], 'y');
                set(p, 'linewidth', 7)
                
                [yy, mm, dd, hh, mm, ss] = datevec(matStart);
                if yy == targetYear
                    theDash = findstr('-', fileName);
                    if (ismember(fileName, concatMatB{1,3}))
                        t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), ...
                            plotLocs(1) - theSep, ...
                            [fileName(1:(theDash-1)) '*']);
                    else
                        t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), ...
                            plotLocs(1) - theSep, ...
                            fileName(1:(theDash-1)));
                    end
                    set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                        'horiz', 'left', 'fontname', 'arial');
                end
                continue
            end
            if (~isempty(findstr('adc', fileName)) || ~isempty(findstr('wh', fileName)))    %ADCP stuff
                hitDepth = 3;
            else
                theDepth = siteB(indexFile).instDepth;
                waterDepth = siteB(indexFile).waterDepth;
                if isnan(waterDepth)
                    waterDepth = 22;
                end
                if isnan(theDepth) || isempty(theDepth)
                    theDepth = siteB(indexFile).fileDepth;
                end
                distFrom = waterDepth - theDepth;
                if distFrom < 0
                    distFrom = 0;
                end
                %targDepths = [24 10 1];
                %targDepthDist = abs(targDepths - distFrom);
                %[junk, hitDepth] = min(targDepthDist);
                hitDepth = find((distFrom >= timeLowsB) && ...
                    (distFrom <= timeHighsB));
            end
            
            if ismember(targetVar, {'oxygen'; 'chlorophyll'; 'chlorophyll-a'})
                hitDepth = 1;
            end
            
            if ismember(targetVar, {'oxygen'})
                p = plot([max([matStart datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                    min([matEnd datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                    [plotLocs(indexVar)-1 plotLocs(indexVar)-1], ...
                    theColors{3});
            else
                p = plot([max([matStart datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                    min([matEnd datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                    [plotLocs(indexVar)-hitDepth plotLocs(indexVar)-hitDepth], ...
                    theColors{hitDepth});
            end
            
            set(p, 'linewidth', 7)
            [yy, mm, dd, hh, mm, ss] = datevec(matStart);
            if yy == targetYear
                theDash = findstr('-', fileName);
                if ismember(fileName, concatMatB{indexVar, hitDepth-1})
                    t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), ...
                        plotLocs(indexVar)-hitDepth, ...
                        [fileName(1:(theDash-1)) '*']);
                else
                    t(length(t)+1) = text(max([matStart datenum(targetYear, 1, 1, 5, 00, 00)]), ...
                        plotLocs(indexVar)-hitDepth, ...
                        fileName(1:(theDash-1)));
                end
                set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                    'horiz', 'left', 'fontname', 'arial');
            end
        end
    end

    for index = 1:length(honjoB_deploy)
        [yy,mm,dd,hh,mm,ss] = datevec(honjoB_deploy(index));
        [yy2,mm,dd,hh,mm,ss] = datevec(honjoB_recover(index));
        if ismember(targetYear, [yy yy2])
            p = plot...
                ([max([honjoB_deploy(index) datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                min([honjoB_recover(index) datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                [trapSpot trapSpot], 'm');
            set(p, 'linewidth', 7)
        end
        thisText = trapIDB{theHonjosB(index)};
        findDash = findstr(thisText, '-');
        if index+1 > length(theHonjosB)
            continue
        end
        nextText = trapIDB{theHonjosB(index+1)};
        findDash2 = findstr(nextText, '-');
        thisLead = thisText(1:findDash);
        nextLead = nextText(1:findDash2);
        [yy3,mm,dd,hh,mm,ss] = datevec(honjoB_recover(index+1));
        if (str2double(thisText(findDash+1:end)) == 1) || ...
                (str2double(thisText(1:findDash-1)) == 520)
            toPrint = [thisText(1:findDash) 'i'];
            startTime = honjoB_deploy(index);
        elseif (~strcmp(thisLead, nextLead) && (yy == targetYear)) || ...
                ((yy == targetYear) && (yy2 ~= targetYear))
            endTime = min([honjoB_recover(index) datenum(targetYear, 12, 30, 21, 00, 00)]);

            [yy, mm, dd, hh, mm, ss] = datevec(startTime);
            if yy == targetYear
                t(length(t)+1) = text(startTime, trapSpot, ...
                    toPrint);
                set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                    'horiz', 'left', 'fontname', 'arial');
            end
        end
    end


    for index = 1:length(tubeB_deploy)
        [yy,mm,dd,hh,mm,ss] = datevec(tubeB_deploy(index));
        [yy2,mm,dd,hh,mm,ss] = datevec(tubeB_recover(index));
        if ismember(targetYear, [yy yy2])
            tubeSpot = find((tubeB_depth(index) >= tubeLowsB) && ...
                (tubeB_depth(index) <= tubeHighsB));
            if isempty(tubeSpot)
                continue
            end
            p = plot...
                ([max([tubeB_deploy(index) datenum(targetYear, 1, 1, 5, 4, 00)]) ...
                min([tubeB_recover(index) datenum(targetYear, 12, 30, 21, 00, 00)])], ...
                [(trapSpot-5.5)+tubeSpot (trapSpot-5.5)+tubeSpot]);
            set(p, 'linewidth', 7, 'color', [0.6 0.6 0.6])
            if yy == targetYear
                t(length(t)+1) = text(max([tubeB_deploy(index) datenum(targetYear, 1, 2)]),...
                    (trapSpot-5.5)+tubeSpot, ...
                    trapIDB{theTubesB(index)});
                set(t(length(t)), 'fontsize', 8, 'color', 'k', 'clipping', 'on', ...
                    'horiz', 'left', 'fontname', 'arial');
            end
        end
    end

    t2 = [];
    xList = [];
    yList = [];
    for index = 1:length(t)
        try
            thisSpot = get(t(index), 'position');
            thisString = get(t(index), 'string');
        catch
            continue
        end
        bads = find(isspace(thisString));
        if ~isempty(bads)
            thisString(bads) = [];
        end
        if isempty(thisString)
            continue
        end
        t2(length(t2)+1) = text(thisSpot(1), thisSpot(2), thisString);
        set(t2(end), 'fontsize', 8, 'color', 'k', 'clipping', 'off', ...
            'horiz', 'left', 'fontname', 'arial')
        delete(t(index))
        thisSpot = get(t2(end), 'position');
        xList(length(t2)) = thisSpot(1);
        yList(length(t2)) = thisSpot(2);
        test = str2double(thisString(1:min([4 length(thisString)])));
        if isempty(test)
            test = str2double(thisString(2:end));
            if isempty(test) || ~isreal(test)
                test = str2double(thisString(2:end-2));
            end
            
            if isempty(test) || ~isreal(test)
                test = str2double(thisString(3:end-2));
            end
        end
        moorList(length(t2)) = test;
        stringList(length(t2)) = {thisString};
    end

 allYs = unique(yList);
    for index = 1:length(allYs)
        thisY = find(yList == allYs(index));
        thisX = xList(thisY);
        thisMoor = moorList(thisY);
        thisStrings = stringList(thisY);
        [sortX, I] = sort(thisX);
        sortY = thisY(I);
        sortMoor = thisMoor(I);
        sortStrings = thisStrings(I);
        hitList = 1:length(thisY);
        index2 = 1;
        lastSpotOld = 0;
        while index2 < length(thisY)
            thisX = xList(thisY);
            sortX = thisX(I);
            if isnan(hitList(index2))
                continue
            end
            withinThis = find(((sortX > (sortX(index2)-20)) && ...
                (sortX < (sortX(index2) + 20))) || ...
                (sortMoor == sortMoor(index2)));
            theseXs = sortX(withinThis(:))';
            theseYs = sortY(withinThis(:))';
            theseMoors = sortMoor(withinThis(:))';
            theseStrings = sortStrings(withinThis(:))';
            [theseMoors, I2] = sort(theseMoors);
            theseXs = theseXs(I2);
            theseYs = theseYs(I2);
            thisStrings = theseStrings(I2);
            lastSpot = min(theseXs);
            theOrder = 1:length(theseXs);
            if allYs(index) == 42
                index4 = 1;
                findADCP = strfind(thisStrings, 'adc');
                while index4 < length(findADCP)
                    if isempty(findADCP{index4})
                        index4 = index4+1;
                        continue
                    else
                        theOrder = [theOrder(1:index4-1) ...
                            theOrder(index4+1:end) theOrder(index4)];
                        index4 = index4+1;
                    end
                end
            end
            for index5 = 1:length(theOrder)
                index3 = theOrder(index5);
                set(t2(theseYs(index3)), 'position', [lastSpot allYs(index)]);
                xList(theseYs(index3)) = lastSpot;

                thisLength = length(thisStrings{index3});
                lastSpot = lastSpot + 3*(thisLength)+2;
            end
            index2 = max(withinThis)+1;
        end
    end

    inThis = find((crYY == targetYear) && isB);
    for index = 1:length(inThis)
        l = line([cruiseStart(inThis(index)) cruiseStart(inThis(index))], ...
            [13 52]);
        set(l, 'color', 'k', 'linestyle', '-.')
        t = text(cruiseStart(inThis(index)), 53, ...
            {[num2str(cruiseNumber(inThis(index)), '%05d') ': ']; cruiseShip{inThis(index)}});
        set(t, 'fontsize', 8, 'fontname', 'arial')
    end

    %Plot parameters
    axis([datenum(targetYear, 1, 1) datenum(targetYear, 12, 31) 13 52])
    theTicks = [];
    for index = 1:12
        theTicks = [theTicks(:); datenum(targetYear, index, 1)];
    end
    set(gca, 'xtick', theTicks, 'layer', 'top')
    set(gca, 'xticklabel', xLabels)
    a = annotation('textbox', [0.44 0.355 1 0.1], 'string', num2str(targetYear), ...
        'linestyle', 'none', 'fontname', 'arial', 'fontsize', 8, ...
        'horiz', 'center', 'vert', 'bottom');
    plotLocs = plotLocs(:);
    set(gca, 'ytick', flipud([plotLocs - 0.75; [trapSpot+0.3; trapSpot-0.7]]), 'yticklabel', flipud(plotVarNames))
    pt = text(min(xlim)-1.2, 20.2, 'Chlorophyll\it-a');
    set(pt, 'fontname', 'arial', 'fontsize', 8, 'horiz', 'right')
    box('on')
bigAx = gca;
    axPosition = get(bigAx, 'Position');
    axPositionNew = axPosition;
    axPositionNew(2) = axPosition(2) + 0.23*axPosition(4);
    axPositionNew(4) = axPosition(4)*0.75;
    axPositionNew(1) = axPosition(1) + axPosition(1)*0.05;
    set(bigAx, 'Position', axPositionNew)

    axPosition(4) = axPosition(4)*0.2;
    axPosition(2) = axPosition(1) - 0.05;
    lilAx = subplot('position', axPosition);
    hold on
    set(lilAx, 'fontname', 'arial', 'fontsize', 8)

    ex = text(6, 8, 'EXPLANATION');
    set(ex, 'fontweight', 'bold', 'fontname', 'arial', 'fontsize', 8, 'horiz', 'center')
    
    y1 = text(0, 7, 'Nominal measurement depth');
    set(y1, 'fontweight', 'bold', 'fontname', 'arial', 'fontsize', 8)
    %p1 = plot([0 1], [6 6], 'c');
    %set(p1, 'linewidth', 7)
    %y2 = text(1.5, 6, '4-10 meters');
    %set(y2, 'fontname', 'arial', 'fontsize', 8)
    p2 = plot([0 1], [6 6], 'r');
    set(p2, 'linewidth', 7)
    y3 = text(1.5, 6, '12 meters (10 meters above bottom)');
    set(y3, 'fontname', 'arial', 'fontsize', 8)
    p3 = plot([0 1], [5 5], 'g');
    set(p3, 'linewidth', 7)
    y4 = text(1.5, 5, '20 meters (1-4 meters above bottom)');
    set(y4, 'fontname', 'arial', 'fontsize', 8)
    p4 = plot([0 1], [4 4], 'y');
    set(p4, 'linewidth', 7)
    y5 = text(1.5, 4, '2 to 16 meters (20-8 meters above bottom) (ADCP)');
    set(y5, 'fontname', 'arial', 'fontsize', 8)
    p6 = plot([0 1], [3 3], 'm');
    set(p6, 'linewidth', 7)
    y6 = text(1.5, 3, '4 meters above bottom (Honjo trap)');
    set(y6, 'fontname', 'arial', 'fontsize', 8)
    p7 = plot([0 1], [2 2]);
    set(p7, 'linewidth', 7, 'color', [0.6 0.6 0.6])
    y7 = text(1.5, 2, '5-8, 2-5, and 0-2 meters above bottom (tube traps)');
    set(y7, 'fontname', 'arial', 'fontsize', 8)
    
    n =9.25;
    t0 = text(n+0.7, 7, 'Cruises');
    set(t0, 'fontname', 'arial', 'fontsize', 8, 'fontweight', 'bold', 'horiz', 'center');
    t1 = text(n+0.7, 6, 'YYNNN - USGS cruise identifier');
    set(t1, 'fontname', 'arial', 'fontsize', 8, 'horiz', 'center')
    t1b = text(n+0.7, 5, 'Ships');
    set(t1b, 'fontname', 'arial', 'fontsize', 8, 'fontweight', 'bold', 'horiz', 'center');
    t2 = text(n-1, 4, 'WH - White Heath');
    set(t2, 'fontname', 'arial', 'fontsize', 8)
    t3 = text(n-1, 3, 'MH - Marcus Hanna');
    set(t3, 'fontname', 'arial', 'fontsize', 8)
    t4 = text(n+1, 3, 'CN - Connecticut');
    set(t4, 'fontname', 'arial', 'fontsize', 8)
    t5 = text(n+1, 4, 'CA - Christopher Andrew');
    set(t5, 'fontname', 'arial', 'fontsize', 8)
    t6 = text(n-1, 2, 'AB - Abbie Burgess');
    set(t6, 'fontname', 'arial', 'fontsize', 8)
    t7 = text(n+1, 2, 'AM - Argo Maine');
    set(t7, 'fontname', 'arial', 'fontsize', 8)
    t8 = text(n+0.7, 1, 'L - Looney');
    set(t8, 'fontname', 'arial', 'fontsize', 8, 'horiz', 'center')


    axis([0 12 0 8])
    axis('off')
    printTitle = ['b_dcov_' num2str(targetYear, '%.0f')];
    axisTitle = ['LT-B Data Coverage, ' num2str(targetYear, '%.0f')];
    s = suptitle(axisTitle);
    set(s, 'fontname', 'arial', 'fontsize', 12);
    print(gcf, '-dpdf', fullfile(plotDirectory, printTitle));

    close


end

