function sed_trap(theYears, dataDirectory, plotDirectory)
%
%   sed_trap(theYears, dataDirectory, plotDirectory)
%
%   Function to create plots of the sediment trapping rate of the tube
%   traps, sorted by deployment.  Plots are named SITE_sed_YYYY.

%Soupy Alexander, 11/13/03

%Sediment trap data
[theNums, theText] = xlsread(fullfile(dataDirectory, 'all_traps.xls'), ...
    'LNB Traps');
theNums = [repmat(NaN, 1, size(theNums, 2)); theNums];
trapType = theText(:,19);
for index = 1:length(trapType)
    if isempty(trapType{index})
        trapType(index) = {' '};
    end
end
theTubes = find(strcmp('tube', deblank(lower(trapType))));

tubeNums = theNums(theTubes,:);
tubeText = theText(theTubes,:);

tubeStart = tubeNums(:,4) + datenum('30-Dec-1899');
tubeEnd = tubeNums(:, 6) + datenum('30-Dec-1899');
tubeDepths = tubeNums(:,22);
poisonType = tubeText(:,20);
tubeFlux = tubeNums(:, 28);

for index = 1:length(poisonType)
    if isempty(poisonType{index})
        poisonType(index) = {' '};
    end
end

tubeStartGreg = gregorian(datenum2julian(tubeStart));
tubeEndGreg = gregorian(datenum2julian(tubeEnd));


for indexYear = 1:length(theYears)
    targetYear = theYears(indexYear);
    endIn = find(tubeEndGreg(:,1) == targetYear);
    
    tubeStartIn = tubeStart(endIn);
    tubeEndIn = tubeEnd(endIn);
    tubeDepthsIn = tubeDepths(endIn);
    poisonTypeIn = poisonType(endIn);
    tubeFluxIn = tubeFlux(endIn);
    gregEnd = gregorian(datenum2julian(tubeEndIn));
    
    firstDeploy = find(ismember(gregEnd(:,2), [1 2 3 4]));
    secondDeploy = find(ismember(gregEnd(:,2), [5 6 7 8]));
    thirdDeploy = find(ismember(gregEnd(:,2), [9 10 11 12]));
    
    numPlots = 4;
    plotSplit = 3;
    
    plotCols = {'b'; 'r'; 'g'; 'k'; 'm'; 'y'};
    
    plotSym = cell(size(poisonTypeIn));
    theForms = strmatch('form', lower(deblank(poisonTypeIn)));
    theForms = [theForms(:); strmatch('f', lower(deblank(poisonTypeIn)), 'exact')];
    theAzides = strmatch('azi', lower(deblank(poisonTypeIn)));
    theNone = strmatch('n', lower(deblank(poisonTypeIn)));
    theNone = [theNone(:); strmatch('ff', lower(deblank(poisonTypeIn)))];  
    theNone = [theNone(:); strmatch('?', lower(deblank(poisonTypeIn)))];
    plotSym(theForms) = {'^'};
    plotSym(theAzides) = {'s'};
    plotSym(theNone) = {'o'};
    
    theDeploys = {'first'; 'second'; 'third'};
    
    figure, orient tall
    for index = 1:3
        subplot(plotSplit, 2, index), hold on
        eval(['theIns = ' theDeploys{index} 'Deploy;']);
        plotX = tubeFluxIn(theIns);
        plotY = tubeDepthsIn(theIns);
        plotSyms = cell2mat(plotSym(theIns));
        plotColor = repmat(plotCols{index}, length(plotSyms), 1);
        plotIcon = [plotSyms(:) plotColor(:)];
        for index2 = 1:length(plotX)
            plot(plotX(index2), plotY(index2), plotIcon(index2,:));
        end
        
        set(gca, 'xtick', [0 5 10 25 50 100 200], 'xscale', 'log', ...
            'xminortick', 'off')
        set(gca, 'ygrid', 'on')
        theTicks = get(gca, 'xtick')
        for indexLine = 1:length(theTicks)
            l = line([theTicks(indexLine) theTicks(indexLine)], ...
                [0 20]);
            set(l, 'linestyle', ':', 'color', 'k')
            
        end
        
        xlim([0.5 200])
        ylim([0 20])
        ylabel('Meters Above Bottom')
        xlabel('Trapping Rate (g/m^2/day)')
        if isempty(theIns)
            title('No Traps')
        else
            title([datestr(tubeStartIn(theIns(1)), 1) ' to ' ...
                    datestr(tubeEndIn(theIns(1)), 1)])
        end
        box('on')
        
        subplot(plotSplit, 2, 4), hold on       
        for index2 = 1:length(plotX)
            plot(plotX(index2), plotY(index2), plotIcon(index2,:));
        end
    end
    
    set(gca, 'xtick', [0 5 10 25 50 100 200], 'xscale', 'log', ...
        'xminortick', 'off')
    set(gca, 'ygrid', 'on')
    xlim([0.5 200])
    ylim([0 20])
    ylabel('Meters Above Bottom')
    xlabel('Trapping Rate (g/m^2/day)')
    title(['All traps recovered in ' num2str(targetYear, '%.0f')])
    box('on')
    for indexLine = 1:length(theTicks)
        l = line([theTicks(indexLine) theTicks(indexLine)], ...
            [0 20]);
        set(l, 'linestyle', ':', 'color', 'k')
    end
    
    subplot(plotSplit, 1, plotSplit)
    plot(2, 3, 'k^')
    t = text(3, 3, 'Samples treated with Formalin')
    hold on
    plot(2, 2, 'ks')
    t = text(3, 2, 'Samples treated with Sodium azide')
    plot(2, 1, 'ko')
    t = text(3, 1, 'Not treated')
    axis([-5 15 0 4])
    axis('off')
    
    suptitle(['Site A, Tube Traps Recovered in ' num2str(targetYear, '%.0f')])
    
    printTitle = ['a_sed_' num2str(targetYear, '%.0f')];
    print('-dpdf', fullfile(plotDirectory, printTitle))
    close
end

%%%%%%%%%%%%%%%%Site B
%Sediment trap data
[theNums, theText] = xlsread(fullfile(dataDirectory, 'all_traps.xls'), ...
    'Scituate Traps');
theNums = [repmat(NaN, 1, size(theNums, 2)); theNums];
trapType = theText(:,19);
for index = 1:length(trapType)
    if isempty(trapType{index})
        trapType(index) = {' '};
    end
end
theTubes = find(strcmp('tube', deblank(lower(trapType))));

tubeNums = theNums(theTubes,:);
tubeText = theText(theTubes,:);

tubeStart = tubeNums(:,1) + datenum('30-Dec-1899');
tubeEnd = tubeNums(:, 3) + datenum('30-Dec-1899');
tubeDepths = tubeNums(:,20);
poisonType = tubeText(:,20);
tubeFlux = tubeNums(:, 43);

for index = 1:length(poisonType)
    if isempty(poisonType{index})
        poisonType(index) = {' '};
    end
end

tubeStartGreg = gregorian(datenum2julian(tubeStart));
tubeEndGreg = gregorian(datenum2julian(tubeEnd));


for indexYear = 1:length(theYears)
    targetYear = theYears(indexYear);
    if targetYear < 1998
        continue
    end
    endIn = find(tubeEndGreg(:,1) == targetYear);
    
    tubeStartIn = tubeStart(endIn);
    tubeEndIn = tubeEnd(endIn);
    tubeDepthsIn = tubeDepths(endIn);
    poisonTypeIn = poisonType(endIn);
    tubeFluxIn = tubeFlux(endIn);
    gregEnd = gregorian(tubeEndIn);
    
    firstDeploy = find(ismember(gregEnd(:,2), [1 2 3 4]));
    secondDeploy = find(ismember(gregEnd(:,2), [5 6 7 8]));
    thirdDeploy = find(ismember(gregEnd(:,2), [9 10 11 12]));
    
    numPlots = 4;
    plotSplit = 3;
    
    plotCols = {'b'; 'r'; 'g'; 'k'; 'm'; 'y'};
    
    plotSym = cell(size(poisonTypeIn));
    theForms = strmatch('form', lower(deblank(poisonTypeIn)));
    theForms = [theForms(:); strmatch('f', lower(deblank(poisonTypeIn)), 'exact')];
    theAzides = strmatch('azi', lower(deblank(poisonTypeIn)));
    theNone = strmatch('n', lower(deblank(poisonTypeIn)));  
    theNone = [theNone(:); strmatch('ff', lower(deblank(poisonTypeIn)))];  
    theNone = [theNone(:); strmatch('?', lower(deblank(poisonTypeIn)))];
    plotSym(theForms) = {'^'};
    plotSym(theAzides) = {'s'};
    plotSym(theNone) = {'o'};
    
    theDeploys = {'first'; 'second'; 'third'};
    
    figure, orient tall
    for index = 1:3
        subplot(plotSplit, 2, index), hold on
        eval(['theIns = ' theDeploys{index} 'Deploy;']);
        plotX = tubeFluxIn(theIns);
        plotY = tubeDepthsIn(theIns);
        plotSyms = cell2mat(plotSym(theIns));
        plotColor = repmat(plotCols{index}, length(plotSyms), 1);
        plotIcon = [plotSyms(:) plotColor(:)];
        for index2 = 1:length(plotX)
            plot(plotX(index2), plotY(index2), plotIcon(index2,:));
        end
        
        set(gca, 'xtick', [0 5 10 25 50 100 200], 'xscale', 'log', ...
            'xminortick', 'off')
        set(gca, 'ygrid', 'on')
        theTicks = get(gca, 'xtick')
        for indexLine = 1:length(theTicks)
            l = line([theTicks(indexLine) theTicks(indexLine)], ...
                [0 20]);
            set(l, 'linestyle', ':', 'color', 'k')
            
        end
        
        xlim([0.5 200])
        ylim([0 20])
        ylabel('Meters Above Bottom')
        xlabel('Trapping Rate (g/m^2/day)')
        if isempty(theIns)
            title('No Traps')
        else
            title([datestr(tubeStartIn(theIns(1)), 1) ' to ' ...
                    datestr(tubeEndIn(theIns(1)), 1)])
        end
        box('on')
        
        subplot(plotSplit, 2, 4), hold on       
        for index2 = 1:length(plotX)
            plot(plotX(index2), plotY(index2), plotIcon(index2,:));
        end
    end
    
    set(gca, 'xtick', [0 5 10 25 50 100 200], 'xscale', 'log', ...
        'xminortick', 'off')
    set(gca, 'ygrid', 'on')
    xlim([0.5 200])
    ylim([0 20])
    ylabel('Meters Above Bottom')
    xlabel('Trapping Rate (g/m^2/day)')
    title(['All traps recovered in ' num2str(targetYear, '%.0f')])
    box('on')
    for indexLine = 1:length(theTicks)
        l = line([theTicks(indexLine) theTicks(indexLine)], ...
            [0 20]);
        set(l, 'linestyle', ':', 'color', 'k')
    end
    
    subplot(plotSplit, 1, plotSplit)
    plot(2, 3, 'k^')
    t = text(3, 3, 'Samples treated with Formalin')
    hold on
    plot(2, 2, 'ks')
    t = text(3, 2, 'Samples treated with Sodium azide')
    plot(2, 1, 'ko')
    t = text(3, 1, 'Not treated')
    axis([-5 15 0 4])
    axis('off')
    
    suptitle(['Site B, Tube Traps Recovered in ' num2str(targetYear, '%.0f')])
    
    printTitle = ['b_sed_' num2str(targetYear, '%.0f')];
    print('-dpdf', fullfile(plotDirectory, printTitle))
    close
end