%% pH_section.m  –  Single contourf section plot for one glider deployment

%% ── Inputs ───────────────────────────────────────────────────────────────
% data_file  = "\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\" + ...
%              "Spray_Data\26220301\real_time_binned\26220301sat.mat";

% data_file = "\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\" + ...
%           "Spray_Data\26120501\real_time_binned\26120501sat.mat";

data_file = "\\thalassa\ProjectLibrary\901805_Coastal_Biogeochemical_Sensing\" + ...
          "Spray_Data\25B68601\real_time_binned\25B68601sat.mat";
pH_field   = 'pHin';
date_start = datenum(2026, 1, 1);
date_end   = datenum(2026, 4, 14);
pH_clim    = [7.4  8.1];
% out_file   = 'Monterey_Bay_pH.png';
% out_file   = 'San_Diego_pH.png';
out_file   = 'Trinidad_Head_pH.png';

%% ── Load and filter ──────────────────────────────────────────────────────
d = load(data_file);
s = d.s;
m = s.sdn >= date_start & s.sdn <= date_end;

sdn = s.sdn(m);
dep = s.depth(:, 1);
pH  = s.(pH_field)(:, m);

%% ── Bin to 50 m ──────────────────────────────────────────────────────────
bin_size  = 50;
bin_edges = 0 : bin_size : max(dep);
bin_ctrs  = bin_edges(1:end-1) + bin_size/2;
n_bins    = numel(bin_ctrs);

pH_binned = NaN(n_bins, numel(sdn));
for b = 1:n_bins
    rows = dep >= bin_edges(b) & dep < bin_edges(b+1);
    pH_binned(b, :) = mean(pH(rows, :), 1, 'omitnan');
end
dep = bin_ctrs(:);
pH  = pH_binned;

%% ── Plot ─────────────────────────────────────────────────────────────────
fig = figure(1);  clf(fig);
set(fig, 'Units','inches', 'Position',[0.5 0.5 10 5]);

contourf(sdn, dep, pH, 5)
shading interp
axis ij
clim(pH_clim)
xlim([date_start  date_end])
colormap(cool)
cb = colorbar;
cb.Label.String = 'pH_{in situ} [total]';
ylabel('Depth (m)')
% title('Monterey Bay, CA')
% title('San Diego, CA')
title('Trinidad Head, OR')
datetick('x', 'mmm dd', 'keeplimits')
grid on

%% ── Export ───────────────────────────────────────────────────────────────
set(fig, 'PaperUnits','inches', 'PaperSize',[10 5], ...
         'PaperPositionMode','manual', 'PaperPosition',[0 0 10 5]);
print(fig, '-dpng', '-r300', out_file)
fprintf('Saved %s\n', out_file)
