[mySLA, myError, d] = load_dash1_by_date('25-Dec-2010');

% Loaded data for: 25-Dec-2010 (Index: 6568)
% Plot it
imagesc(mySLA');
title(['SLA on ', datestr(d)]);
colorbar;
imagesc((mySLA)');
set(gca, 'YDir', 'normal')

function [sla, error_out, targetDate] = load_dash1_by_date(dateStr)
    % filename: Name of the NetCDF file defined in DSET 
    filename = 'dash1_1993-2024_dy.nc';
    
    %% 1. Define Temporal Metadata (Mimicking TDEF) 
    % TDEF 11688 LINEAR 01jan1993 1dy
    startDate = datetime(1993, 1, 1);
    totalSteps = 11688;
    
    %% 2. Calculate the Time Index
    targetDate = datetime(dateStr);
    
    % Calculate the difference in days
    % We add 1 because MATLAB/NetCDF indexing starts at 1
    t_index = days(targetDate - startDate) + 1;
    
    % Validate that the requested date is within range
    if t_index < 1 || t_index > totalSteps
        error('Date out of range. File covers 01-Jan-1993 to early 2024.');
    end
    
    %% 3. Load Data for specific time index
    % ncread(source, var, start, count)
    % For 'sla', the dimensions are [X, Y, T]
    % X=1440, Y=720, T=11688 
    
    startArr = [1, 1, t_index];
    countArr = [1440, 720, 1];
    
    sla = ncread(filename, 'sla', startArr, countArr);
    error_out = ncread(filename, 'error', startArr, countArr);
    
    % Apply UNDEF mapping 
    sla(sla == -32768) = NaN;
    error_out(error_out == -32768) = NaN;
    
    fprintf('Loaded data for: %s (Index: %d)\n', datestr(targetDate), t_index);
end

