%% MB_pH_2gliders.m  –  Two-glider pH section comparison
%  Single landscape page: map (left) + two pHin sections (right, stacked)

%% ── Date range control ───────────────────────────────────────────────────
% Leave as [] to use the full combined span of both deployments.
% Override with datenum values, e.g.:
%   date_start = datenum(2026, 3, 1);
%   date_end   = datenum(2026, 4, 15);
date_start = [];    % earliest date to show
date_end   = [];    % latest   date to show

%% ── Glider metadata ──────────────────────────────────────────────────────
glider1_label = 'Spray2: SN203';   % deployment 26220301
glider2_label = 'Spray2: SN205';   % deployment 26120501

%% ── Load data ────────────────────────────────────────────────────────────
d1 = load("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\" + ...
          "Spray_Data\26220301\real_time_binned\26220301sat.mat");
s1 = d1.s;

d2 = load("\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\" + ...
          "Spray_Data\26120501\real_time_binned\26120501sat.mat");
s2 = d2.s;

%% ── Determine unified date range ─────────────────────────────────────────
all_sdn = [s1.sdn(:); s2.sdn(:)];
fin     = isfinite(all_sdn);
if isempty(date_start),  date_start = min(all_sdn(fin));  end
if isempty(date_end),    date_end   = max(all_sdn(fin));  end

%% ── Filter profiles to date range ────────────────────────────────────────
% Profiles (columns) outside the date range are excluded from both plots.
m1 = s1.sdn >= date_start & s1.sdn <= date_end;
m2 = s2.sdn >= date_start & s2.sdn <= date_end;

sdn1 = s1.sdn(m1);   lat1 = s1.lat(m1);   lon1 = s1.lon(m1);
dep1 = s1.depth(:,1);   pH1 = s1.pHin(:, m1);

sdn2 = s2.sdn(m2);   lat2 = s2.lat(m2);   lon2 = s2.lon(m2);
dep2 = s2.depth(:,1);   pH2 = s2.pHin(:, m2);

%% ── Shared colorbar ticks for the date range ─────────────────────────────
date_ticks      = linspace(date_start, date_end, 6);
date_ticklabels = cellstr(datestr(date_ticks, 'yyyy-mm-dd'));

%% ── Figure ───────────────────────────────────────────────────────────────
fig = figure(1);  clf(fig);
set(fig, 'Units','inches', 'Position',[0.5 0.5 11 8.5]);

% ── MAP  (left half, full height) ─────────────────────────────────────
ax_map = geoaxes(fig, 'Position', [0.03 0.08 0.42 0.84]);
hold(ax_map, 'on')
geoscatter(ax_map, lat1, lon1, 20, sdn1, 'filled', ...
           'Marker','o', 'DisplayName', glider1_label);
geoscatter(ax_map, lat2, lon2, 20, sdn2, 'filled', ...
           'Marker','^', 'DisplayName', glider2_label);
colormap(ax_map, jet)
caxis(ax_map, [date_start  date_end])
cb_map            = colorbar(ax_map);
cb_map.Ticks      = date_ticks;
cb_map.TickLabels = date_ticklabels;
title(ax_map, 'Glider Tracks')
legend(ax_map, 'Location','best')
ax_map.LatitudeLabel.String  = '';
ax_map.LongitudeLabel.String = '';
try; geobasemap('satellite'); catch; end

% ── pH section – Glider 1  (upper right) ──────────────────────────────
ax1 = axes(fig, 'Position', [0.57 0.56 0.37 0.34]);
contourf(ax1, sdn1, dep1, pH1)
axis(ax1, 'ij')
xlim(ax1, [date_start  date_end])
datetick(ax1, 'x', 2, 'keeplimits')
cb1 = colorbar(ax1); cb1.Label.String = 'pH insitu';
title(ax1, ['pH_{in situ} [total]  —  ' glider1_label])
ylabel(ax1, 'Depth [m]')

% ── pH section – Glider 2  (lower right) ──────────────────────────────
ax2 = axes(fig, 'Position', [0.57 0.10 0.37 0.34]);
contourf(ax2, sdn2, dep2, pH2)
axis(ax2, 'ij')
xlim(ax2, [date_start  date_end])
datetick(ax2, 'x', 2, 'keeplimits')
cb2 = colorbar(ax2); cb2.Label.String = 'pH insitu';
title(ax2, ['pH_{in situ} [total]  —  ' glider2_label])
ylabel(ax2, 'Depth [m]')
xlabel(ax2, 'Date')

% ── Super-title ────────────────────────────────────────────────────────
annotation(fig, 'textbox', [0 0.93 1 0.06], ...
    'String', 'Monterey Bay Glider Transects', ...
    'HorizontalAlignment','center', 'VerticalAlignment','middle', ...
    'FontSize', 14, 'FontWeight','bold', 'EdgeColor','none')

%% ── Export ───────────────────────────────────────────────────────────────
set(fig, 'PaperUnits','inches', 'PaperSize',[11 8.5], ...
         'PaperPositionMode','manual', 'PaperPosition',[0 0 11 8.5]);
print(fig, '-dpng', '-r300', 'MB_pH_2gliders_landscape.png')
