
% 
% Trace coast line and bathymetry using the m_map toolbox
% Bathymetry is plotted with contoured lines
% 

function [hCoast, hBath, hRecif] = m_trace_coast_bathy(lonlat_minmax,colCoast,coast_bathy_region,ibath,levels)

if ~exist('coast_bathy_region','var')
    coast_bathy_region = 'med';
end

if ~exist('ibath','var')
    ibath = 0;
end

if ~exist('levels','var')
    levels = [-100 -500 -1000 -2000];
end

m_proj('mercator','lon',lonlat_minmax(1:2),'lat',lonlat_minmax(3:4));


%% Bathymetry
if ibath == 1
    load(['~/MyData/Matlab/outils_oceano/bathyrelief_' coast_bathy_region '.mat']);
    xbath = bathy(:,1); %#ok<NODEF>
    ybath = bathy(:,2);
    zbath = bathy(:,3);
    valid = (xbath >= lonlat_minmax(1) & xbath <= lonlat_minmax(2)) & ...
            (ybath >= lonlat_minmax(3) & ybath <= lonlat_minmax(4));
    xbath = xbath(valid);
    ybath = ybath(valid);
    zbath = zbath(valid);
    xbath_un = unique(xbath);
    ybath_un = unique(ybath);
    xbath = reshape(xbath,[length(xbath_un) length(ybath_un)]);
    ybath = reshape(ybath,[length(xbath_un) length(ybath_un)]);
    zbath = reshape(zbath,[length(xbath_un) length(ybath_un)]);
    clear bathy xbath_un ybath_un
    
    [cLines, hBath] = m_contour(xbath,ybath,zbath,levels,'Color',[0.5 0.5 0.5],'Linewidth',0.5);
    clabel(cLines,hBath,'LabelSpacing',500,'FontSize',9,'Color',[0.5 0.5 0.5]);
    hold on;
else
    hBath = [];
end

%% Coastline
load(['~/MyData/Matlab/outils_oceano/coast_' coast_bathy_region '.mat']);
xcoast = coast(:,1); %#ok<NODEF>
ycoast = coast(:,2);

hCoast = m_plot(xcoast,ycoast,colCoast,'LineWidth',1);


%% Recifs
if strcmp(coast_bathy_region,'centralpacific')
    load ~/MyData/Matlab/outils_oceano/recif_centralpacific.mat;
    xrecif = recif(:,1); %#ok<NODEF>
    yrecif = recif(:,2);
    valid = (xrecif >= lonlat_minmax(1) & xrecif <= lonlat_minmax(2)) & ...
            (yrecif >= lonlat_minmax(3) & yrecif <= lonlat_minmax(4));
    xrecif = xrecif(valid);
    yrecif = yrecif(valid);
    
    hRecif = m_plot(xrecif,yrecif,[colCoast '.'],'MarkerSize',5);
end


%% Fancy stuff
m_grid('Box','fancy','TickDir','in');
set(gca,'FontSize', 16,'TickDir','out');
xlabel('Longitude (deg E)','Fontsize',18);
ylabel('Latitude (deg N)','Fontsize',18);
