%% find the closest casts near each profile in space and time and overlay
%%
B = readtable("O2_TCO2_ALK_pH_loadcheck.xlsm");
load("24403401_sat.mat",'s');
%%
IDX = month(B.DATE_TIME) == 4 & B.DEPTH <= 500 | month(B.DATE_TIME) == 5 & B.DEPTH <= 500;
%%
B2plot = B(IDX,:);
%%
clf
plot(B2plot.pH,B2plot.DEPTH,'.')
set(gca,'ydir','reverse')
%%
pH = s.pH25atm(:,200:500);
depth = s.depth(:,200:500);
clf
plot(pH,depth,'-')
hold on
plot(B2plot.pH,B2plot.DEPTH,Color='green',Marker='o',MarkerFaceColor='green',LineStyle='none') % make sure this is the same pH
%canyonB
set(gca,'ydir','reverse')
%%
sal = s.psal(:,200:500);
depth = s.depth(:,200:500);
clf
plot(sal,depth,'-')
hold on
plot(B2plot.SAL,B2plot.DEPTH,Color='green',Marker='o',MarkerFaceColor='green',LineStyle='none')
%canyonB
set(gca,'ydir','reverse')
%%
temp = s.tc(:,200:500);
depth = s.depth(:,200:500);
clf
plot(temp,depth,'-')
hold on
plot(B2plot.TMP,B2plot.DEPTH,Color='green',Marker='o',MarkerFaceColor='green',LineStyle='none')
%canyonB
set(gca,'ydir','reverse')
%%
doxy = s.doxy(:,200:500);
depth = s.depth(:,200:500);
clf
plot(doxy,depth,'-')
hold on
plot(B2plot.O2,B2plot.DEPTH,Color='green',Marker='o',MarkerFaceColor='green',LineStyle='none')
%canyonB
set(gca,'ydir','reverse')
%% lat lon match
Radius = 25;
% Define the latitude and longitude offsets in degrees (approximately 50 km)
offset_km = Radius / 111; % Assuming 1 degree of latitude is approximately 111 km

% Calculate bounding box coordinates
lat_min = SprayLat - offset_km;
lat_max = SprayLat + offset_km;
lon_min = SprayLon - offset_km / cosd(SprayLat); % Correcting for longitude distance change with latitude
lon_max = SprayLon + offset_km / cosd(SprayLat);
%%
% Load your data
% Assuming data is stored in arrays dataLat and dataLon

dataLat = B2plot.DEC_LAT; % example latitudes
dataLon = B2plot.DEC_LONG; % example longitudes

% Define the target coordinates
targetLat = s.lat(200); % target latitude
targetLon = s.lon(200); % target longitude

% Function to calculate the Haversine distance
haversine = @(lat1, lon1, lat2, lon2) 2 * 6371 * ...
    asin(sqrt(sin((deg2rad(lat2-lat1)/2)).^2 + ...
    cos(deg2rad(lat1)) .* cos(deg2rad(lat2)) .* ...
    (sin(deg2rad(lon2-lon1)/2)).^2));

% Calculate distances from target to all data points
distances = haversine(dataLat, dataLon, targetLat, targetLon);

% Find the index of the minimum distance
[~, minIndex] = min(distances);
cruise = B2plot.CRUISE(minIndex);
siteID = B2plot.SITE_ID(minIndex);
idx = contains(B2plot.CRUISE,cruise) & contains(B2plot.SITE_ID,siteID);
B2plot(idx,:)
% Retrieve the closest point's coordinates
closestLat = dataLat(minIndex);
closestLon = dataLon(minIndex);

% Display the closest point and the distance
fprintf('Closest point: Latitude = %.4f, Longitude = %.4f\n', closestLat, closestLon);
fprintf('Distance to target: %.2f km\n', distances(minIndex));
%%
B = readtable("O2_TCO2_ALK_pH_loadcheck.xlsm");
load("24403401_sat.mat",'s');

IDX = month(B.DATE_TIME) == 4 & B.DEPTH <= 500 | month(B.DATE_TIME) == 5 & B.DEPTH <= 500;

B2plot = B(IDX,:);
dataLat = B2plot.DEC_LAT;
dataLon = B2plot.DEC_LONG;

% Define the target coordinates
targetLat = s.lat(198); % target latitude
targetLon = s.lon(198); % target longitude

% Function to calculate the Haversine distance
haversine = @(lat1, lon1, lat2, lon2) 2 * 6371 * ...
    asin(sqrt(sin((deg2rad(lat2-lat1)/2)).^2 + ...
    cos(deg2rad(lat1)) .* cos(deg2rad(lat2)) .* ...
    (sin(deg2rad(lon2-lon1)/2)).^2));

% Calculate distances from target to all data points
distances = haversine(dataLat, dataLon, targetLat, targetLon);

% Find the index of the minimum distance
[~, minIndex] = min(distances);
cruise = B2plot.CRUISE(minIndex);
siteID = B2plot.SITE_ID(minIndex);
idx = contains(B2plot.CRUISE,cruise) & contains(B2plot.SITE_ID,siteID);
B2plot(idx,:)
% Retrieve the closest point's coordinates
closestLat = dataLat(minIndex);
closestLon = dataLon(minIndex);
idx15 = distances < 15;

pH = s.pHin(:,198);
depth = s.depth(:,198);
clf
plot(pH,depth,'-',LineWidth=1)
hold on
% within radius
plot(B2plot.pH(idx15), B2plot.DEPTH(idx15),Color='red',Marker='o',MarkerFaceColor='red',LineStyle='none') % make sure this is the same pH
% closest
plot(B2plot.pH(idx),B2plot.DEPTH(idx),Color='green',Marker='o',MarkerFaceColor='green',LineStyle='none') % make sure this is the same pH


%canyonB
set(gca,'ydir','reverse')
legend('Spray','BOG - within 15 km', 'BOG - closest')
dtBOG = B2plot.DATE_TIME(idx);
title({['spray time: ',datestr(s.sdn(198),'mm-dd-yyyy HH:MM')],...
    ['BOG time: ',datestr(dtBOG(1),'mm-dd-yyyy HH:MM')],...
    ['Distance: ', num2str(distances(minIndex)), ' km']})
%% this works
prof = 20;
[BOGcast_closest_time, BOGtime, spraytime] = findClosestTimeBOGcast(s, B, prof);
pH = s.pHin(:,prof);
depth = -s.depth(:,prof);
clf
plot(pH,depth,'-',LineWidth=1)
hold on
% closest
plot(BOGcast_closest_time.pH,BOGcast_closest_time.DEPTH,Color='green',Marker='o',MarkerFaceColor='green',LineStyle='none') % make sure this is the same pH
set(gca,'ydir','reverse')
legend('Spray','BOG - closest time',Location='southeast')
title({['spray time: ',datestr(spraytime,'mm-dd-yyyy HH:MM')],...
    ['BOG time: ',datestr(BOGtime,'mm-dd-yyyy HH:MM')]})