
% 
% Quiver plot with automatic legend
% 
% INPUTS
% - lon,lat
% - u,v,
% - w: geographic limits [lon_min lon_max lat_min lat_max]
% - scaleX,scaleY: map scaling factors from trace_coast_bathy.m
% - scaleSpeed: scale factor for the vectors
% - legPos: position of the legend arrow ('NW', 'NE', 'SE', 'SW')
% - legUnit: text to appear next to the legend arrow
% - col: color of the arrows and of the legend text
% 

function [hQuiver, hText] = quiverLegend(lon,lat,u,v,lonlat_lim,scale,legVel,legUnit,legPos,col)

% Add the legend arrow
switch legPos
    case 'NW'
        lon = [lon(:); lonlat_lim(1)+(lonlat_lim(2)-lonlat_lim(1))/10];
        lat = [lat(:); lonlat_lim(4)-(lonlat_lim(4)-lonlat_lim(3))/10];
    case 'NE'
        lon = [lon(:); lonlat_lim(2)-(lonlat_lim(2)-lonlat_lim(1))/7.5];
        lat = [lat(:); lonlat_lim(4)-(lonlat_lim(4)-lonlat_lim(3))/10];
    case 'SE'
        lon = [lon(:); lonlat_lim(2)-(lonlat_lim(2)-lonlat_lim(1))/7.5];
        lat = [lat(:); lonlat_lim(3)+(lonlat_lim(4)-lonlat_lim(3))/10];
    case 'SW'
        lon = [lon(:); lonlat_lim(1)+(lonlat_lim(2)-lonlat_lim(1))/10];
        lat = [lat(:); lonlat_lim(3)+(lonlat_lim(4)-lonlat_lim(3))/10];
        
end
offLeg = (lonlat_lim(4)-lonlat_lim(3))/50;
v = [v(:); 0];
u = [u(:); legVel];

% Scale x and y with the map scaling factors scaleX,Y (see trace_coast_bathy.m)
% and with the scale factor scaleSpeed
u = u*scale;
v = v*scale;

% Draw
hQuiver = quiver(lon,lat,u,v,0,col,'LineWidth',1);
hText   = text(lon(end),lat(end)+offLeg, ...
               [num2str(legVel,'%.2f') ' ' legUnit],'Color',col,'FontSize',12);
           