% Run_CUGN_pipeline.m
%
% Runs the full CUGN processing pipeline in order:
%   1. Extract_CUGN_to_mat.m         -- .nc -> .mat (T/S/O2, pressure, density)
%   2. Derive_empirical_parameters.m -- adds ESPER-LIR/ESPER-NN/CANYONB estimates
%   3. write_CUGN_to_gliderviz_txt.m -- writes final GliderVIZ .txt files
%
% Prerequisite: the CUGN .nc files must already be sitting in CUGN/data/.
% This data is not fetched automatically -- see README_CUGN.md for how to
% get it (manual download from spraydata.ucsd.edu, or an ERDDAP poll).
%
% Note: each stage script below starts with its own `clear`, so this
% wrapper can't rely on any variable surviving from one stage to the
% next -- that's why paths are repeated as literals in each run() call
% rather than built from a shared variable.

data_dir = 'C:\Users\bwerb\Documents\CUGNROMS\CUGN\data';
required_nc = {'CUGN_along.nc','CUGN_line_56.nc','CUGN_line_66.nc',...
    'CUGN_line_80.nc','CUGN_line_90.nc'};
missing = required_nc(~cellfun(@(f) isfile(fullfile(data_dir,f)), required_nc));
if ~isempty(missing)
    error(['Missing CUGN .nc file(s) in %s: %s\n' ...
        'Download them from https://spraydata.ucsd.edu/projects/cugn/ ' ...
        '(see README_CUGN.md) before running this pipeline.'],...
        data_dir, strjoin(missing, ', '));
end
clear data_dir required_nc missing

fprintf('\n========================================\n');
fprintf('CUGN pipeline starting %s\n', datestr(now));
fprintf('========================================\n');

fprintf('\n--- Stage 1/3: Extract_CUGN_to_mat (%s) ---\n', datestr(now));
run('C:\Users\bwerb\Documents\CUGNROMS\CUGN\Extract_CUGN_to_mat.m');

fprintf('\n--- Stage 2/3: Derive_empirical_parameters (%s) ---\n', datestr(now));
run('C:\Users\bwerb\Documents\CUGNROMS\CUGN\Derive_empirical_parameters.m');

fprintf('\n--- Stage 3/3: write_CUGN_to_gliderviz_txt (%s) ---\n', datestr(now));
run('C:\Users\bwerb\Documents\CUGNROMS\CUGN\write_CUGN_to_gliderviz_txt.m');

fprintf('\n========================================\n');
fprintf('CUGN pipeline finished %s\n', datestr(now));
fprintf('Output written to C:\\Users\\bwerb\\Documents\\CUGNROMS\\CUGN\\for_distribution\\\n');
fprintf('========================================\n');
