%Pl_Eup_total_2.m
%This mfile called by sortstage_mactri.m
%plot up the euphausiid stages totals from NBP cruises

h1=[];  h2=[]; h3=[]; h4=[]; h5=[]; h6=[]; h7=[];
scale7=' None';scale1=' > 500/m2';scale2=' > 100/m2';scale3=' > 50/m2';
scale4=' > 10/m2'; scale5=' > 1/m2';scale6=' > 0/m2';

%colr=1  % use this line if symbols to be in colors for each area as designated in CSTR above
colr=2; % use this line if symbols to be all plotted as black
if abundance > 0
    f6=(depth_open-depth_close).*abundance;%calculate the number per m2 for all stages for each net in all tows
    towlen= length(tow); %tow is variable in mat file loaded into workspace
    jnet=1; % counter of the number of tows in cruise 
    numm2(jnet,1)=f6(1,1);
    latjnet(jnet,1)=lat(1,1);lonjnet(jnet,1)=lon(1,1); %keep lat and long associated with tow
    for i=2:towlen %procedure to compute the number under sq m for towby summing the individual net values
        if tow(i-1,1)== tow(i,1)
            numm2(jnet,1)=numm2(jnet,1)+f6(i,1);
        else
            jnet =jnet +1;
            numm2(jnet,1)=f6(i,1);
            latjnet(jnet,1)=lat(i,1);lonjnet(jnet,1)=lon(i,1);
        end
    end
    % at this point numm2 has the number per m2 for each tow on the cruise for
    % the all stages
end
%return
if abundance >0
num=num2str((round(numm2*100))/100);  %round numbers /m2 off to 2 places and store result in num
end

%plot the all station positions as small x's
if stageNameCruise(cnt,13:16) == '0103';   %cruiseid(1,:) == 'NBP0103'
    figure(1);  hold on;
    %subplot(3,3,cnt), hold on
    [dlon,dlat]=m_ll2xy(NBP0103TowDat(:,9), NBP0103TowDat(:,8));
    m_proj('albers equal-area','lat',[-70 -65],'long',[-76 -64],'rect','on');
    [cs,h]=m_contour(lonb,latb,elevations,v);
    m_usercoast('WAP.mat','patch',[.2 .7 .2]);
    m_grid('linest','none','linewidth',2,'tickdir','out','xaxisloc','top','YaxisLocation','right');
    plot(dlon, dlat,'k*', 'markersize',[10],'LineWidth', [1.2]);%plots symbol in black
elseif stageNameCruise(cnt,13:16) == '0104';    %cruiseid(1,:) == 'NBP0104'
    figure(2); hold on
    %subplot(3,3,cnt-9); hold on
    [dlon,dlat]=m_ll2xy(NBP0104TowDat(:,9), NBP0104TowDat(:,8));
    m_proj('albers equal-area','lat',[-70 -65],'long',[-76 -64],'rect','on');
    [cs,h]=m_contour(lonb,latb,elevations,v);
    m_usercoast('WAP.mat','patch',[.2 .7 .2]);
    m_grid('linest','none','linewidth',2,'tickdir','out','xaxisloc','top','YaxisLocation','right');
    plot(dlon, dlat,'k*', 'markersize',[10],'LineWidth', [1.2]);%plots symbol in black
elseif stageNameCruise(cnt,13:16) == '0202';    %cruiseid(1,:) == 'NBP0202'
    figure(3); hold on;
    %subplot(3,3,cnt-18); hold on
    [dlon,dlat]=m_ll2xy(NBP0202TowDat(:,9), NBP0202TowDat(:,8));
    m_proj('albers equal-area','lat',[-70 -65],'long',[-76 -64],'rect','on');
    [cs,h]=m_contour(lonb,latb,elevations,v);
    m_usercoast('WAP.mat','patch',[.2 .7 .2]);
    m_grid('linest','none','linewidth',2,'tickdir','out','xaxisloc','top','YaxisLocation','right');
    plot(dlon, dlat,'k*', 'markersize',[10],'LineWidth', [1.2]);%plots symbol in black
else stageNameCruise(cnt,13:16) == '0204';    %cruiseid(1,:) == 'NBP0202'
    figure(4); hold on;
    %subplot(3,3,cnt-18); hold on
    [dlon,dlat]=m_ll2xy(NBP0204TowDat(:,9), NBP0204TowDat(:,8));
    m_proj('albers equal-area','lat',[-70 -65],'long',[-76 -64],'rect','on');
    [cs,h]=m_contour(lonb,latb,elevations,v);
    m_usercoast('WAP.mat','patch',[.2 .7 .2]);
    m_grid('linest','none','linewidth',2,'tickdir','out','xaxisloc','top','YaxisLocation','right');
    plot(dlon, dlat,'k*', 'markersize',[10],'LineWidth', [1.2]);%plots symbol in black
end

%Plot the abundance data as filled circles of varying sizes
if abundance > 0
    sp(1,38)=1; % was a way to select which region gets a specific color.
for j=1:length(numm2) %do once for each tow for a given species (j is a row counter)
        if numm2(j,1) ==0
            [dlon,dlat]=m_ll2xy(lonjnet(j,1), latjnet(j,1));
            if colr == 1;
                h7 = plot(dlon, dlat,[cstr(sp(1,38)),'x'], 'markersize',[10],'LineWidth', [1.2], 'MarkerFaceColor','r'); %plots symbol in color
            else 
                h7 = plot(dlon, dlat,'kx', 'markersize',[10],'LineWidth', [1.2], 'MarkerFaceColor','r');%plots symbol in black
                %h8=text(dlon,dlat,num2str(numm2(j,1)));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
                h8=text(dlon,dlat,num(j,:));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
            end
        elseif numm2(j,1) > 500
            [dlon,dlat]=m_ll2xy(lonjnet(j,1), latjnet(j,1));
            if colr == 1;
                h1 = plot(dlon, dlat,[cstr(sp(1,38)),'o'], 'markersize',[30],'LineWidth', [1.2], 'MarkerFaceColor','r'); 
            else
                h1 = plot(dlon, dlat,'ko', 'markersize',[30],'LineWidth', [1.2], 'MarkerFaceColor','r');
                %h8=text(dlon,dlat,num2str(numm2(j,1)));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
                h8=text(dlon,dlat,num(j,:));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
            end
        elseif numm2(j,1) > 100
            [dlon,dlat]=m_ll2xy(lonjnet(j,1), latjnet(j,1));
            if colr == 1;
                h2 = plot(dlon, dlat,[cstr(sp(1,38)),'o'], 'markersize',[25],'LineWidth', [1.2], 'MarkerFaceColor','r');
            else
                h2 = plot(dlon, dlat, 'ko', 'markersize',[25],'LineWidth', [1.2], 'MarkerFaceColor','r');
                %h8=text(dlon,dlat,num2str(numm2(j,1)));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
                h8=text(dlon,dlat,num(j,:));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
            end
        elseif numm2(j,1) > 50
            [dlon,dlat]=m_ll2xy(lonjnet(j,1), latjnet(j,1));
            if colr == 1;
                h3 = plot(dlon, dlat,[cstr(sp(1,38)),'o'], 'markersize',[20],'LineWidth', [1.2], 'MarkerFaceColor','r');
            else
                h3 = plot(dlon, dlat, 'ko', 'markersize',[20],'LineWidth', [1.2], 'MarkerFaceColor','r');
                %h8=text(dlon,dlat,num2str(numm2(j,1)));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
                h8=text(dlon,dlat,num(j,:));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
            end
        elseif numm2(j,1) > 10
            [dlon,dlat]=m_ll2xy(lonjnet(j,1), latjnet(j,1));
            if colr == 1;
                h4 = plot(dlon, dlat,[cstr(sp(1,38)),'o'], 'markersize',[15],'LineWidth', [1.2], 'MarkerFaceColor','r');
            else
                h4 = plot(dlon, dlat,'ko', 'markersize',[15],'LineWidth', [1.2], 'MarkerFaceColor','r');
                %h8=text(dlon,dlat,num2str(numm2(j,1)));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k'); 
                h8=text(dlon,dlat,num(j,:));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k'); 
            end
        elseif numm2(j,1) > 1
            [dlon,dlat]=m_ll2xy(lonjnet(j,1), latjnet(j,1));
            if colr == 1;
                h5 = plot(dlon, dlat,[cstr(sp(1,38)),'o'], 'markersize',[10],'LineWidth', [1.2], 'MarkerFaceColor','r');
            else
                h5 = plot (dlon, dlat, 'ko', 'markersize',[10],'LineWidth', [1.2], 'MarkerFaceColor','r');
                %h8=text(dlon,dlat,num2str(numm2(j,1)));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
                h8=text(dlon,dlat,num(j,:));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
            end
        elseif numm2(j,1) > 0
            [dlon,dlat]=m_ll2xy(lonjnet(j,1), latjnet(j,1));
            if colr == 1;
                h6 = plot(dlon, dlat,[cstr(sp(1,38)),'o'], 'markersize',[8],'LineWidth', [1.2], 'MarkerFaceColor','r');
            else
                h6 = plot(dlon, dlat,'ko', 'markersize',[8],'LineWidth', [1.2], 'MarkerFaceColor','r');
                %h8=text(dlon,dlat,num2str(numm2(j,1)));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
                h8=text(dlon,dlat,num(j,:));  set(h8,'FontSize',12,'FontWeight', 'bold');set(h8,'col','k');
            end
        end
    end
% make sure all size markers are used before making legend.
[dlon,dlat]=m_ll2xy(-70,19);
h1 = plot (dlon, dlat,'ko', 'markersize',[30],'visible','off');%plot point but turn off visibility
h2 = plot (dlon, dlat,'ko', 'markersize',[25],'visible','off');
h3 = plot (dlon, dlat,'ko', 'markersize',[20],'visible','off');
h4 = plot (dlon, dlat,'ko', 'markersize',[15],'visible','off');
h5 = plot (dlon, dlat,'ko', 'markersize',[10],'visible','off');
h6 = plot (dlon, dlat,'ko', 'markersize',[8],'visible','off');
h7 = plot (dlon, dlat,'kx', 'markersize',[4],'visible','off');
%m_text(-80, -71,stageNameCruise(cnt,:),'fontweight','bold'); % write species name on plot at given longitude and latitude position
m_text(-75, -70.4,plotnam,'fontweight','bold');
hmat=[h1,h2,h3,h4,h5,h6,h7]; % put handles for each marker type/size together for legend command
[outstuff]=m_legend(hmat,scale1,scale2,scale3,scale4,scale5,scale6,scale7); %this makes the legend box
set(outstuff,'Position', [185   2   28    9]); % rem this out if legend in bad place
drawnow
end
