%% Read in the synthetic profile index file that includes the basic meta data for all BGC-Argo profiles
bgc_profiles = readtable("https://data-argo.ifremer.fr/argo_synthetic-profile_index.txt", 'FileType', 'text');

% Set lat/lon bounds for study area
latbounds = [30 48];
lonbounds = [-134 -115];
datestart = 20250101000000; % format matching index file
% bounding box
ibounds = bgc_profiles.latitude > latbounds(1) &...
    bgc_profiles.latitude < latbounds(2) &...
    bgc_profiles.longitude > lonbounds(1) &...
    bgc_profiles.longitude < lonbounds(2) &...
    bgc_profiles.date > datestart;

filtered_profiles = bgc_profiles(ibounds, :); % All BGC-Argo profs in the bounding time/space window
% Enforce lat/lon bounds
ilonlat = filtered_profiles.latitude >= latbounds(1) & ...
    filtered_profiles.latitude <= latbounds(2) & ...
    filtered_profiles.longitude >= lonbounds(1) & ...
    filtered_profiles.longitude <= lonbounds(2);
filtered_profiles = filtered_profiles(ilonlat, :);
%% Get all float WMOs from index file
parts = split(filtered_profiles.file, '/');
WMO = str2double(parts(:,2));
filtered_profiles.WMO = WMO;
%% Create map with MBARI managed and non-MBARI managed floats
% Get MBARI float WMOs
MBARI_float_list = readtable("C:\\Users\\bwerb\\Documents\\ProposalFigures\\data\\SOCCOM_GO-BGC_LoResQC_LIAR_21Dec2025_odvtxt\\MBARI_float_list.txt");
i = ismember(MBARI_float_list.WMO,WMO);
MBARI_float_list_filtered = MBARI_float_list(i,:);
imbari = ismember(WMO,MBARI_float_list.WMO); % Floats from the index file that match the MBARI float list
n_mbari_floats = length(MBARI_float_list_filtered.WMO);
n_non_mbari_floats = length(unique(filtered_profiles.WMO(~imbari)));
n_mbari = length(filtered_profiles.longitude(imbari));
n_non_mbari = length(filtered_profiles.longitude(~imbari));

% Create figure sized to geographic aspect ratio of study region
lat_range = diff(latbounds);
lon_range = diff(lonbounds);
aspect = (lon_range * cosd(mean(latbounds))) / lat_range;  % width/height
fig_h = 8;
fig_w = fig_h * aspect;
fig = figure;
set(fig, 'Units', 'inches', 'Position', [1 1 fig_w fig_h]);
ax = geoaxes(fig);
geobasemap(ax, 'satellite')
geoscatter(ax, filtered_profiles.latitude(imbari), filtered_profiles.longitude(imbari), 20, 'yellow', '.')
hold(ax, 'on');

% Most recent profile per MBARI float
lat_mbari  = filtered_profiles.latitude(imbari);
lon_mbari  = filtered_profiles.longitude(imbari);
date_mbari = filtered_profiles.date(imbari);
wmo_mbari  = filtered_profiles.WMO(imbari);
[~, isort] = sort(date_mbari, 'descend');
[~, ifirst] = unique(wmo_mbari(isort), 'stable');  % first = most recent per WMO
ilatest = isort(ifirst);
geoscatter(ax, lat_mbari(ilatest), lon_mbari(ilatest), 60, 'yellow', 'o', 'filled')

% lg1 = sprintf('MBARI floats, profiles: %d, %d', n_mbari_floats, n_mbari);
% legend(ax, lg1)
geolimits(ax, [latbounds(1), latbounds(2)], [lonbounds(1), lonbounds(2)]);
% title('Float Profiles since 2025');
fsave = fullfile('C:','Users','bwerb','Documents','ProposalFigures',...
    'mapfigs','ccsmap.png');
saveas(fig,fsave)
clear fsave;