%Pl_Eup_Stages_2.m
%This mfile called by sortstage.m
%plot up the individual euphausiid stages 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';

abun=abundance(is,1); dopen=depth_open(is,1);dclose=depth_close(is,1); tw=tow(is,1);
latt=lat(is,1);long=lon(is,1);
%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 abun > 0
    f6=(dopen-dclose).*abun;%calculate the number per m2 for the stage for each net in all tows
    towlen= length(tw); %tow is variable in mat file loaded into workspace
    jnet=1; % counter of the number of nets in tow for the given stage
    numm2(jnet,1)=f6(1,1);
    latjnet(jnet,1)=latt(1,1);lonjnet(jnet,1)=long(1,1); %keep lat and long associated with tow
    for i=2:towlen %procedure to compute the number under sq m for tow by summing the individual net values
        if tw(i-1,1)== tw(i,1);
            numm2(jnet,1)=numm2(jnet,1)+f6(i,1);
        else
            jnet =jnet +1;
            numm2(jnet,1)=f6(i,1);
            latjnet(jnet,1)=latt(i,1);lonjnet(jnet,1)=long(i,1);
        end
    end
    % at this point numm2 has the number per m2 for each net tow on the cruise for
    % the various stages - one at a time- until the end when a total number
    % of individuals of all stages is computed
end

if abun >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)
    subplot(4,3,ct); 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','XTickLabel',[], 'YTickLabel',[]);
    plot(dlon, dlat,'k*', 'markersize',[10],'LineWidth', [1.2]);%plots symbol in black
elseif stageNameCruise(cnt,13:16) == '0104';    %cruiseid(1,:) == 'NBP0104'
    figure(2);
    subplot(4,3,ct); 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','XTickLabel',[], 'YTickLabel',[]);
    plot(dlon, dlat,'k*', 'markersize',[10],'LineWidth', [1.2]);%plots symbol in black
elseif stageNameCruise(cnt,13:16) == '0202';    %cruiseid(1,:) == 'NBP0202'
    figure(3);
    subplot(4,3,ct); 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','XTickLabel',[], 'YTickLabel',[]);
    plot(dlon, dlat,'k*', 'markersize',[10],'LineWidth', [1.2]);%plots symbol in black
else stageNameCruise(cnt,13:16) == '0204';    %cruiseid(1,:) == 'NBP0202'
    figure(4);
    subplot(4,3,ct); 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', 'XTickLabel',[], 'YTickLabel',[]);
    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 abun > 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    
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(-68, -70,stageNameCruise(cnt,:),'fontweight','bold'); % write species name on plot at given longitude and latitude position
m_text(-80, -70.5,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   21    8.0000]); % rem this out if legend in bad place
drawnow
return %print out jpeg
spnam=stageNameCruise(1,18:21);
outnam= ['print -djpeg C:\Antarctic\Antarctic\AO_synthesis\MOCNESS_Krill_sample_workup\Figures\',cruiseid,spnam];
eval(outnam)