function sed_trap_month(maxYear, dataDirectory, plotDirectory)
%
%   sed_trap_month(maxYear, dataDirectory, plotDirectory)
%
%   Function to create plots of the sediment trapping rate of all traps at all times.
%   Plots are named SITE_sed2.

%Soupy Alexander, 01/15/04

%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

%%%%%%%%%%%%%%%
theHonjos = find(strcmp('honjo', deblank(lower(trapType))));

honjoNums = theNums(theHonjos,:);
honjoText = theText(theHonjos,:);


honjoStart = datenum2julian(honjoNums(:,4) + datenum('30-Dec-1899'));
honjoEnd = datenum2julian(honjoNums(:, 6) + datenum('30-Dec-1899'));
honjoDepths = honjoNums(:,22);
poisonType = honjoText(:,20);
honjoFlux = honjoNums(:, 28);
honjoDeploy = honjoText(:,2);

bads = find(isnan(honjoEnd) | isnan(honjoFlux));
honjoFlux(bads) = [];
honjoEnd(bads) = [];

thePlotCols = {'b'; 'r'; 'g'; 'c'; 'm'};
thePlotCols = repmat(thePlotCols, 4, 1);
thePlotSyms = {'.'; '*'; 'o'; 'x'; '+'; 's'; 'd'; 'v'; '<'; '>'; 'p'; 'h'};
thePlotSyms = repmat(thePlotSyms, 4, 1);

gregEnd = gregorian(honjoEnd);
theEndYears = gregEnd(:,1);
theEndMonths = gregEnd(:,2);

hitYears = unique(theEndYears);
hitYears(find(isnan(hitYears))) = [];

figure, orient landscape, hold on

data = repmat(NaN, length(hitYears)+1, 13);

for indexYear = 1:(length(hitYears)+1)
    hitSymbol = [thePlotCols{indexYear} thePlotSyms{indexYear}];
    for indexMonth = 1:13
        if indexYear ~= length(hitYears)+1;
            inThis = find(theEndYears == hitYears(indexYear) & theEndMonths == indexMonth);
            strYear{indexYear} = num2str(hitYears(indexYear), '%.0f');
            if indexMonth == 13
                inThis = find(theEndYears == hitYears(indexYear));
            end
        else
            inThis = find(theEndMonths == indexMonth);
            strYear{indexYear} = 'All Years';
            if indexMonth == 13
                inThis = find(~isnan(theEndMonths));
            end
        end
        if isempty(inThis)
            numPoints(indexYear, indexMonth) = 0;
            theStd(indexYear, indexMonth) = NaN;
            theMin(indexYear, indexMonth) = NaN;
            theMax(indexYear, indexMonth) = NaN;
            data(indexYear, indexMonth) = NaN;
            continue
        end
        theResult = nanmean(honjoFlux(inThis));
        if indexMonth ~= 13
            p(indexYear) = plot(indexMonth, theResult, hitSymbol);
        end
        data(indexYear, indexMonth) = theResult;
        numPoints(indexYear, indexMonth) = length(inThis);
        theStd(indexYear, indexMonth) = nanstd(honjoFlux(inThis));
        theMin(indexYear, indexMonth) = nanmin(honjoFlux(inThis));
        theMax(indexYear, indexMonth) = nanmax(honjoFlux(inThis));
    end
end
legend(p, strYear, -1)
xlim([0 13])
ylim([0 45])
theMonthLabels = {' '; 'Jan'; 'Feb'; 'Mar'; 'Apr'; 'May'; 'Jun'; 'Jul'; ...
        'Aug'; 'Sep'; 'Oct'; 'Nov'; 'Dec'; ' '};
set(gca, 'xtick', [0:13], 'xticklabel', theMonthLabels)
ylabel('Sediment Flux (g/m^2/day)')
title('Honjo Trap Mean Sediment Trapping Rate by Month and Year')
box('on')

fID = fopen(fullfile(plotDirectory, 'sitea_honjo.xls'), 'w');
fprintf(fID, 'Site A\n');
fprintf(fID, ['Honjo Trap Mean Sediment Trapping Rate by Month and Year\n\n']);
theSpaces = '\t\t\t\t\t';
    fprintf(fID, ['\t' repmat('Jan\t', 1, 7) repmat('Feb\t', 1, 7) ...
            repmat('Mar\t', 1, 7) repmat('Apr\t', 1, 7) repmat('May\t', 1, 7) ...
            repmat('Jun\t', 1, 7) repmat('Jul\t', 1, 7) repmat('Aug\t', 1, 7) ...
            repmat('Sep\t', 1, 7) repmat('Oct\t', 1, 7) repmat('Nov\t', 1, 7) ...
            repmat('Dec\t', 1, 7) repmat('Year\t', 1, 7) '\n']);
fprintf(fID, '\t');
toPrint = ['# Points\tMean Flux\tSTD\tMin\tMax\t'];
toPrint = repmat(toPrint, 1, 13);
fprintf(fID, [toPrint '\n']);

for indexYear = 1:length(hitYears)+1
    fprintf(fID, [strYear{indexYear} '\t']);
    for indexMonth = 1:13
        fprintf(fID, [num2str(numPoints(indexYear, indexMonth), '%.0f') '\t' ...
                num2str(data(indexYear, indexMonth), '%.2f') '\t' ...
                num2str(theStd(indexYear, indexMonth), '%.2f') '\t' ...
                num2str(theMin(indexYear, indexMonth), '%.2f') '\t' ...
                num2str(theMax(indexYear, indexMonth), '%.2f') '\t']);
    end
    fprintf(fID, '\n');
end

fclose('all')
print('-dpdf', fullfile(plotDirectory, 'a_honjo_month.pdf'))
close
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%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

%%%%%%%%%%%%%%%
theHonjos = find(strcmp('honjo', deblank(lower(trapType))));

honjoNums = theNums(theHonjos,:);
honjoText = theText(theHonjos,:);


honjoStart = datenum2julian(honjoNums(:,1) + datenum('30-Dec-1899'));
honjoEnd = datenum2julian(honjoNums(:, 3) + datenum('30-Dec-1899'));
honjoDepths = honjoNums(:,19);
poisonType = honjoText(:,20);
honjoFlux = honjoNums(:, 25);
honjoDeploy = honjoText(:,2);

bads = find(isnan(honjoEnd) | isnan(honjoFlux));
honjoFlux(bads) = [];
honjoEnd(bads) = [];

thePlotCols = {'b'; 'r'; 'g'; 'c'; 'm'};
thePlotCols = repmat(thePlotCols, 4, 1);
thePlotSyms = {'.'; '*'; 'o'; 'x'; '+'; 's'; 'd'; 'v'; '<'; '>'; 'p'; 'h'};
thePlotSyms = repmat(thePlotSyms, 4, 1);

gregEnd = gregorian(honjoEnd);
theEndYears = gregEnd(:,1);
theEndMonths = gregEnd(:,2);

hitYears = unique(theEndYears);
hitYears(find(isnan(hitYears))) = [];

figure, orient landscape, hold on

data = repmat(NaN, length(hitYears)+1, 13);
numPoints = data;
theStd = data;
theMin = data;
theMax = data;
clear strYear p

for indexYear = 1:(length(hitYears)+1)
    hitSymbol = [thePlotCols{indexYear} thePlotSyms{indexYear}];
    for indexMonth = 1:13
        if indexYear ~= length(hitYears)+1;
            inThis = find(theEndYears == hitYears(indexYear) & theEndMonths == indexMonth);
            strYear{indexYear} = num2str(hitYears(indexYear), '%.0f');
            if indexMonth == 13
                inThis = find(theEndYears == hitYears(indexYear));
            end
        else
            inThis = find(theEndMonths == indexMonth);
            strYear{indexYear} = 'All Years';
            if indexMonth == 13
                inThis = find(~isnan(theEndMonths));
            end
        end
        if isempty(inThis)
            numPoints(indexYear, indexMonth) = 0;
            theStd(indexYear, indexMonth) = NaN;
            theMin(indexYear, indexMonth) = NaN;
            theMax(indexYear, indexMonth) = NaN;
            data(indexYear, indexMonth) = NaN;
            continue
        end
        theResult = nanmean(honjoFlux(inThis));
        if indexMonth ~= 13
            p(indexYear) = plot(indexMonth, theResult, hitSymbol);
        end
        data(indexYear, indexMonth) = theResult;
        numPoints(indexYear, indexMonth) = length(inThis);
        theStd(indexYear, indexMonth) = nanstd(honjoFlux(inThis));
        theMin(indexYear, indexMonth) = nanmin(honjoFlux(inThis));
        theMax(indexYear, indexMonth) = nanmax(honjoFlux(inThis));
    end
end
legend(p, strYear, -1)
xlim([0 13])
ylim([0 20])
theMonthLabels = {' '; 'Jan'; 'Feb'; 'Mar'; 'Apr'; 'May'; 'Jun'; 'Jul'; ...
        'Aug'; 'Sep'; 'Oct'; 'Nov'; 'Dec'; ' '};
set(gca, 'xtick', [0:13], 'xticklabel', theMonthLabels)
ylabel('Sediment Flux (g/m^2/day)')
title('Mean Honjo Sediment Trap Flux by Month and Year')
box('on')

fID = fopen(fullfile(plotDirectory, 'siteb_honjo.xls'), 'w');
fprintf(fID, 'Site B\n');
fprintf(fID, ['Honjo Sediment Trap Data by Month and Year\n\n']);
theSpaces = '\t\t\t\t\t';
fprintf(fID, ['\tJan' theSpaces 'Feb' theSpaces 'Mar' theSpaces 'Apr' theSpaces ...
        'May' theSpaces 'Jun' theSpaces 'Jul' theSpaces 'Aug' theSpaces 'Sep' theSpaces ...
        'Oct' theSpaces 'Nov' theSpaces 'Dec' theSpaces 'Entire Year' theSpaces '\n']);
fprintf(fID, '\t');
toPrint = ['# Points\tMean Flux\tSTD\tMin\tMax\t'];
toPrint = repmat(toPrint, 1, 13);
fprintf(fID, [toPrint '\n']);

for indexYear = 1:length(hitYears)+1
    fprintf(fID, [strYear{indexYear} '\t']);
    for indexMonth = 1:13
        fprintf(fID, [num2str(numPoints(indexYear, indexMonth), '%.0f') '\t' ...
                num2str(data(indexYear, indexMonth), '%.2f') '\t' ...
                num2str(theStd(indexYear, indexMonth), '%.2f') '\t' ...
                num2str(theMin(indexYear, indexMonth), '%.2f') '\t' ...
                num2str(theMax(indexYear, indexMonth), '%.2f') '\t']);
    end
    fprintf(fID, '\n');
end

fclose('all')
print('-dpdf', fullfile(plotDirectory, 'b_honjo_month.pdf'))
close
