clc;clear all;close all;
addpath('/home/jhluna/tools/ncl_colormap/')
% pathi = '/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0113_tracking/3hrs/Jul_6days/Check_UV/'
pathi='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0519_tracking/3hrs/Jul_5days/'
pathi2='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0519_tracking/'; 
% load([pathi,'Jul_0717_5D.mat'])
% load([pathi,'Jul_0722_5D.mat'])
load([pathi,'Jul_0722_5D_T.mat'])

%----- for one fig. -----%
xx=xx{17};yy=yy{17};    % 7/21 00:00[33] / 7/24 00:00[17]


load([pathi2,'202407.mat']);
% load([pathi2,'202408_0906.mat']);

file = '/data3/yiwen/matlab_coawst/particle_tracking/ROMS_log_files/Bitou_WIND/TS_const/Check_UV/20240704_0731/Bitou_ocean_his.nc'
mask = ncread(file,'mask_rho');
LON = ncread(file,'lon_rho');
LAT = ncread(file,'lat_rho');
LON(mask==1)=nan;
LAT(mask==1)=nan;
%%
for N_time=1
% IDX=1; %time index[1==first]
% 
% for ii=1:size(xx{1},1)
% for k=1:size(xx{IDX},2)-1  
%  if xx{IDX}(ii,k)==xx{IDX}(ii,k+1)
%     xx{IDX}(ii,k)=nan;
%  elseif yy{IDX}(ii,k)==yy{IDX}(ii,k+1)  
%     yy{IDX}(ii,k)=nan; 
%  end
% end
% 
%  if isnan(xx{IDX}(ii,end-1))==1
%     xx{IDX}(ii,end)=nan;
%  elseif isnan(yy{IDX}(ii,end-1))==1
%     yy{IDX}(ii,end)=nan;
%  end
% end
end

start = 9602;    %7/12 00:00[3842] & 7/24[9602]
End = start+120; % + 6hours
u_avg=nanmean(u6(:,:,start:End),3);
v_avg=nanmean(v6(:,:,start:End),3);
V2_mean=sqrt(u_avg.^2+v_avg.^2);

for ii=1:3
End_1{ii} = 20*ii;
u_avg1{ii}= squeeze(u6(:,:,start+End_1{ii}));
v_avg1{ii}= squeeze(v6(:,:,start+End_1{ii}));
V2{ii}=sqrt(u_avg1{ii}.^2+v_avg1{ii}.^2);

%----- remove vector -----%
index= (lat>=25.126&lon>=121.9158) |(lat>=25.126&lon<=121.9144) | lat>=25.1268;
u_avg1{ii}(index)=nan;v_avg1{ii}(index)=nan;
vecx1{ii}=u_avg1{ii}(1:1:end,1:1:end);
vecy1{ii}=v_avg1{ii}(1:1:end,1:1:end);
vecx1{ii}(vecx1{ii}>=0.03 | vecx1{ii}<=-0.03)=nan; %0.015
vecy1{ii}(vecy1{ii}>=0.03 | vecy1{ii}<=-0.03)=nan;
end
V_color=[0.6350 0.0780 0.1840;0.8500 0.3250 0.0980;0.9290 0.6940 0.1250]


m_map = colormap(nclcmap('hotres',size(xx,2))); 
Msize=linspace(5.5,7.5,size(xx,2));
% scale_factor=0.01;
scale_factor=0.0085;

for TEST=1
set(0,'defaultfigurecolor','w')
%---- cal garbage ----%
[A,I]=find( (yy(:,end)<25.124) | (yy(:,end)>25.124 & xx(:,end)>121.9155))
       
figure(1)
ax1 = axes;
set(ax1,'position',[0.13 0.20 0.775 0.73]); 
pcolor(lon,lat,V2_mean);shading flat;hold on; 
% index2=[1:1:144];
index2=[1:3:144];

for k=1:1:size(xx,2); %timestep[prticles = Model*16]
    plot(xx(index2,k), yy(index2,k), '.',...
        'Color', m_map(k,:),'Markersize',Msize(k));hold on   
    plot(xx(A,k), yy(A,k), '.',...
    'Color', m_map(k,:),'Markersize',Msize(k));hold on
end

    
for i=1:8:200
for j=1:8:260
    if lat(i,j)<25.1268
        if lon(i,j)>121.9142
        for ii=1:3
          q1{ii}=quiver(lon(i,j),lat(i,j),...
                vecx1{ii}(i,j)*scale_factor,vecy1{ii}(i,j)*scale_factor,...
                'AutoScale','off','color',V_color(ii,:),'Linewidth',1.3);hold on
          q1{ii}.ShowArrowHead = 'on';
          q1{ii}.MaxHeadSize = 10;
        end         
        end
    end
end
end
q2=quiver(121.913,25.123,0.01*scale_factor,0,'AutoScale','off','color',[1 0.451 0]);
q2.ShowArrowHead = 'off';
q2.Marker = '<'
text(121.913,25.1228,['1 cm/s'],'Color','k');
text(121.913,25.1225,['Amount of garbage:', num2str(length(A))],'Color','k','Fontsize',18)

load ('/data3/yiwen/matlab_coawst/particle_tracking/Final_report/blue.mat');
colormap(mycmap)
title({'5 Days Run' ; 'particles released at 7/24 00:00'}) %change time[1]
set(gca,'Fontsize',12,'Linewidth',1,'XMinorTick','on','YMinorTick','on','TickDir','out');
c=colorbar(ax1, 'Position',[0.13 0.08 0.75 0.02],'colormap', colormap(mycmap),'Location','southoutside');
c.Title.String = "m/s";
caxis([0 1.]);axis square
axis([121.9126 121.9175 25.122 25.1278])
%======= initial position =======%
range_x = 121.9150; range_x2=121.9153;
range_y = 25.1242; range_y2=25.1245;
patch([range_x, range_x, range_x2, range_x2],[range_y, range_y2, range_y2, range_y], ... 
      'b','FaceAlpha',0,'EdgeColor', 'k','LineWidth', 0.8,'LineStyle', '-','EdgeAlpha',0.3);    
 
%------------- second colorbar[particles] ---------------%
xx1=xx;
ax2 = axes;
set(ax2,'Position',[0.13 0.11 0.775 0.755]);
axis([121.9126 121.9175 25.122 25.1278])
axis off; 

 
startTime = datetime(2024, 7, 24, 0, 0, 0); %change time[2]
endTime   = datetime(2024, 7, 29, 0, 0, 0); %change time[3]
timeStep  = hours(12);
% timeStep  = days(1);
timeSeries= startTime:timeStep:endTime;    
row1 = cellstr(datestr(timeSeries, 'mm/dd')); % 日期
row2 = cellstr(datestr(timeSeries, 'HH:MM')); % 時間    
% row11 = {'7/21'};
% row22 = {'00:00'};

labelArray  = [row1'; row2'];
% labelArray2 = [row11 ; row22];
% L={labelArray{1,:} labelArray2{1,end};labelArray{2,:} labelArray2{2,end}}
L=labelArray;
M1 = strtrim(sprintf('%s\\newline%s\n', L{:}));

%------ colorbar step -------%
start=start;                %initial time
End_time= start+480*5 ;     %8/15 00:00
% r=size(xx{IDX},2)/(size(row1,2)-1);
r=20*12;
%==== from 7/24 to 7/29 & timestep= 45mins ====%
XTickValues = start:r:End_time;
XTickValues2 = [XTickValues End_time];


c2=colorbar(ax2,'colormap',[m_map(1:k,:) ;m_map(k,:)],'XTick',XTickValues,'XTickLabel',M1);
c2.Position=[0.08 0.15 0.025 0.75];
caxis([start End_time]);             
c2.LineWidth = 1;
c2.FontSize = 9;
end



return;
%------- save to jpg --------%
% patho='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0113_tracking/3hrs/Figure/'
patho='/data3/yiwen/matlab_coawst/particle_tracking/Offline_output/2025/0519_tracking/3hrs/Figure/'
% fn=[patho 'Typhoon_0721_5D'];
fn=[patho 'Typhoon_0724_5D'];
set(gcf, 'PaperPositionMode' , 'auto' );
print( fn , '-djpeg' , '-r300' )




