clc;clear all;close all
addpath('/data3/yiwen/matlab_toolbox/t_tide/')
% file1='/data3/yiwen/matlab_coawst/particle_tracking/ROMS_log_files/Bitou_WIND/TS_const/Check_UV/20240704_0731/Bitou_ocean_his.nc'
% file1='/data3/yiwen/matlab_coawst/particle_tracking/ROMS_log_files/Bitou_WIND/TS_const/Check_UV/20240704_0906/Bitou_ocean_his_0906.nc'
file1='/data3/yiwen/matlab_coawst/particle_tracking/ROMS_log_files/Bitou_WIND/TS_const/Check_UV/20240704_0914/Bitou_ocean_his.nc'
lon=ncread(file1,'lon_rho');
lat=ncread(file1,'lat_rho');

% SSH=ncread(file1,'zeta'); %JUL
SSH=ncread(file1,'zeta'); %AUG

i= 108:115;j= 80;
for k=1:length(i)
SSH_M{k}=squeeze(SSH(i(k),j,:));
end

%======= choose date =======%
start = 13440+1442;             % 7/12(3842) & 8/04(1442)
End = size(SSH,3);        % End = start+480*35-1; %+ N_days (25 / 35)
indices = start:20*3:End; % every 3hrs(20*3)
SSH_3hrs=SSH_M{1}(indices);


%%
%======[Sec1.] Choose High & Low Tide ======%
min_peak_distance = 4; %設定最小峰距離 [12 hrs] 1(3hrs)*4 


[highTideVals, highTideIdx] = findpeaks(SSH_3hrs, ...
    'MinPeakProminence', 0.05, ...          
    'MinPeakDistance', min_peak_distance);


[lowTideVals, lowTideIdx] = findpeaks(-SSH_3hrs, ...
    'MinPeakProminence', 0.05, ...
    'MinPeakDistance', min_peak_distance);
lowTideVals = -lowTideVals;


%(1)----[Jul Tide]----%
% HT_idx = lowTideIdx;  
% HT_val = lowTideVals; 
% LT_idx = [highTideIdx ;10;14;122]; 
% LT_val = [highTideVals; SSH_3hrs(10);SSH_3hrs(14);SSH_3hrs(122)]

%(2)----[Aug Tide]----%
% HT_idx = [lowTideIdx(1:4) ;lowTideIdx(6:end);27;35;159;1];  
% HT_val = [lowTideVals(1:4);lowTideVals(6:end);SSH_3hrs(27);SSH_3hrs(35);SSH_3hrs(159);SSH_3hrs(1)]; 
% LT_idx = [highTideIdx ;54] ; 
% LT_val = [highTideVals; SSH_3hrs(54)];

HT_idx = [lowTideIdx(1:4) ;lowTideIdx(6:end);27;159;1];  
HT_val = [lowTideVals(1:4);lowTideVals(6:end);SSH_3hrs(27);SSH_3hrs(159);SSH_3hrs(1)]; 
LT_idx = [highTideIdx ;54] ; 
LT_val = [highTideVals; SSH_3hrs(54)];

%(3-2)--- choose SP/NP tide index -----%
HT_idx = sort(HT_idx);LT_idx = sort(LT_idx);

%------- Jul ------%
% NP_H=[HT_idx(1:2)  ;HT_idx(13:16)]
% NP_L=[LT_idx(2:3)  ;LT_idx(15:17)]
% clear LT_idx HT_idx
% HT_idx=NP_H; LT_idx=NP_L;

% SP_H=[HT_idx(7:10) ;HT_idx(19:20)]
% SP_L=[LT_idx(9:12) ;LT_idx(20:21)]
% clear LT_idx HT_idx
% HT_idx=SP_H; LT_idx=SP_L;

%------- Aug ------%
% NP_H=[HT_idx(5:10);HT_idx(19:22)]
% NP_L=[LT_idx(4:9) ;LT_idx(19:21)]
% clear LT_idx HT_idx
% HT_idx=NP_H; LT_idx=NP_L;

SP_H=[HT_idx(12:15) ]
SP_L=[LT_idx(12:15) ]
clear LT_idx HT_idx
HT_idx=SP_H; LT_idx=SP_L;



return;
%---- save to mat -----%
% patho='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0519_tracking/3hrs/Jul_5days/Tide/Neap/'
% patho='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0519_tracking/3hrs/Jul_5days/Tide/Spring/'
% patho='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0519_tracking/3hrs/Aug_5days/Tide/Neap/'
patho='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0519_tracking/3hrs/Aug_5days/Tide/Spring/'

% fn = [patho 'Tide_0712_0731.mat'];
fn = [patho 'Tide_0804_0823.mat'];
save(fn,'LT_idx','HT_idx')




