%plot SSS TC loc timeseries
%{
load /phoddat/share/asaha/data/SSS_satellite_2010_2022_TClocs1.mat
SSS1_ano1=SSS1_ano;
SSS2_ano1=SSS2_ano;
clear SSS1_ano SSS2_ano
load /phoddat/share/asaha/data/SSS_satellite_2010_2022_TClocs2.mat
SSS1_ano1(:,51:53,:)=NaN;
SSS2_ano1(:,45:53,:)=NaN;
SSS1_ano1(72,:,:)=NaN;
SSS2_ano1(72,:,:)=NaN;
SSS1_ano1(73:154,:,:)=SSS1_ano;
SSS2_ano1(73:154,:,:)=SSS2_ano;
clear SSS1_ano SSS2_ano
outfile=char('/phodnet/share/asaha/data/SSS_satellite_2010_2022_TClocs.mat');
SSS1_ano=double(SSS1_ano1);
SSS2_ano=double(SSS2_ano1);
save(outfile,'-v7.3','SSS1_ano','SSS2_ano');

load /phoddat/share/asaha/data/SSS_satellite_2010_2022_TClocs1rain.mat
SSS1_ano1=SSS1_ano;
SSS2_ano1=SSS2_ano;
clear SSS1_ano SSS2_ano
load /phoddat/share/asaha/data/SSS_satellite_2010_2022_TClocs2rain.mat
SSS1_ano1(:,51:53,:)=NaN;
SSS2_ano1(:,45:53,:)=NaN;
SSS1_ano1(72,:,:)=NaN;
SSS2_ano1(72,:,:)=NaN;
SSS1_ano1(73:154,:,:)=SSS1_ano;
SSS2_ano1(73:154,:,:)=SSS2_ano;
clear SSS1_ano SSS2_ano
outfile=char('/phodnet/share/asaha/data/SSSrain_satellite_2010_2022_TClocs.mat');
SSS1_ano=double(SSS1_ano1);
SSS2_ano=double(SSS2_ano1);
save(outfile,'-v7.3','SSS1_ano','SSS2_ano');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%}

outfile=char('/phodnet/share/asaha/data/SSSrain_satellite_2010_2022_TClocs.mat');
load(outfile);
SSS1_ano(SSS1_ano==0)=NaN;
SSS2_ano(SSS2_ano==0)=NaN;

load('/phodnet/share/asaha/data/mm_loc_TC33_2000_2022_atlantic.mat','lat_TC','lon_TC');

lat_TC(lat_TC==0)=NaN;
lon_TC(lon_TC==0)=NaN;

lat_TC=lat_TC(121:end,:);
lon_TC=lon_TC(121:end,:);

[indi,indj]=find(lat_TC>=5 & lat_TC<=27 & lon_TC>=-70 & lon_TC<=-50); %Amazon 


for k=1:length(indi)
if indj(k) > size(SSS1_ano,2)
continue
end %if
SSS1_indij(k,:)= SSS1_ano(indi(k),indj(k),:);
end %k
SSS1_indij(SSS1_indij==0)=NaN;
SSS1_all=squeeze(nanmean(SSS1_indij,1));

before_S=nanmean(SSS1_all(2:9));

SSS1_wake=SSS1_all-before_S;

%SSS1_anof_wake(SSS1_anof_wake==0)=NaN;
mean_SSS1_wake=SSS1_wake;  %squeeze(nanmean(SSS1_wake,1));
%clear SSS1_anof SSS1_indij k l before_S SSS1_wake

for k=1:length(indi)
if indj(k) > size(SSS2_ano,2)
continue
end %if
SSS2_indij(k,:)= SSS2_ano(indi(k),indj(k),:);
end %k
SSS2_indij(SSS2_indij==0)=NaN;
SSS2_all=squeeze(nanmean(SSS2_indij,1));

before_S2=nanmean(SSS2_all(2:9));

SSS2_wake=SSS2_all-before_S2;

%SSS1_anof_wake(SSS1_anof_wake==0)=NaN;%squeeze(nanmean(SSS1_wake,1));
mean_SSS2_wake=SSS2_wake;  
%clear SSS1_anof SSS1_indij k l before_S SSS1_wake

SSS1_std=std(SSS1_indij,0,1,"omitnan"); %std(A,w,dim)
SSS2_std=std(SSS2_indij,0,1,"omitnan"); %std(A,w,dim)


%correlation
[R,P]=corrcoef(SSS1_all,SSS2_all)
%corsig(i,1)=R(1,2);corsig(i,2)=P(1,2);
[R,P]=corrcoef(mean_SSS1_wake,mean_SSS2_wake)
%smoothing SSS2
SSS2_all_sm=movmean(SSS2_all,5);
mean_SSS2_wake_sm=movmean(mean_SSS2_wake,5);
[R,P]=corrcoef(SSS1_all,SSS2_all_sm)
[R,P]=corrcoef(mean_SSS1_wake,mean_SSS2_wake_sm)
SSS2_all_sm7=movmean(SSS2_all,7);
mean_SSS2_wake_sm7=movmean(mean_SSS2_wake,7);
[R,P]=corrcoef(SSS1_all,SSS2_all_sm7)
[R,P]=corrcoef(mean_SSS1_wake,mean_SSS2_wake_sm7)
zero=zeros(1,101);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%plot timeseries
%time=[1:376]'; %check from poster plo time=[-10:365]';
%time=[-10:100]';
time=[-10:90]';

figure('units','centimeters','position',[0 0 36 12]);%set(h, 'Visible', 'off');
errorbar(time,SSS1_all,SSS1_std,'r','linewidth',2); %plot(time,SSS1_all,'r','linewidth',2); %'color',[.6 .6 .6]
hold on; errorbar(time,SSS2_all,SSS2_std,'k','linewidth',2); %hold on; plot(time,SSS2_all,'k','linewidth',2);
%hold on; plot(time,SSS2_all_sm,'b','linewidth',2);
hold on; plot(time,SSS2_all_sm7,'c','linewidth',2);
hold on; plot(time,zero,'color',[.6 .6 .6],'LineWidth',1,'HandleVisibility','off');
xlim([-10 90]);
ylabel('SSS anomalies (psu)');
%xlabel('10 days before and 100 days after TC (2002-2021)');
xlabel('10 days before and 90 days after TC, 2010-2022');
%legend({'within 3x3 deg from TC','at ARGO locations within 3x3 deg from TC, R=0.7413,P=0','5 days moving mean, ARGO, R=0.8453,P=0','7 days moving mean, ARGO, R=0.8509,P=0'},'Orientation','vertical','Location','southeast');
legend({'within 3x3 deg from TC','at Argo locations within 3x3{\circ} from TC, R=0.129,P=0.1974','7 days moving mean at Argo locations, R=0.19,P=0.049'},'Orientation','vertical','Location','southeast');
set(gca,'linewidth',2);set(gca,'FontSize',14);
set(gca, 'TickDir', 'out');
filename=char(strcat('/phoddat/share/asaha/plots/plots_timeseries_SSS/satSSS_days90_afterTCall_b2_9_AmazonNatlantic_2010_2022crain'));
%filename=char(strcat('/phoddat/share/asaha/plots_timeseries_SSS/satSSS_days365_afterTC_NGoM_2002_2021'));
%filename=char(strcat('/phoddat/share/asaha/plots_timeseries_SSS/satSSS_days365_afterTC_SGoM_2002_2021'));
print(filename,'-dpng');
close all

figure('units','centimeters','position',[0 0 36 12]);%set(h, 'Visible', 'off');
plot(time,SSS1_all,'r','linewidth',2); %'color',[.6 .6 .6]
hold on; plot(time,SSS2_all,'k','linewidth',2);
%hold on; plot(time,SSS2_all_sm,'b','linewidth',2);
hold on; plot(time,SSS2_all_sm7,'c','linewidth',2);
hold on; plot(time,zero,'color',[.6 .6 .6],'LineWidth',1,'HandleVisibility','off');
xlim([-10 90]);
ylabel('SSS anomalies (psu)');
%xlabel('10 days before and 100 days after TC (2002-2021)');
xlabel('10 days before and 90 days after TC, 2010-2022');
%legend({'within 3x3 deg from TC','at ARGO locations within 3x3 deg from TC, R=0.7413,P=0','5 days moving mean, ARGO, R=0.8453,P=0','7 days moving mean, ARGO, R=0.8509,P=0'},'Orientation','vertical','Location','southeast');
legend({'within 3x3 deg from TC','at Argo locations within 3x3{\circ} from TC, R=0.129,P=0.1974','7 days moving mean at Argo locations, R=0.19,P=0.049'},'Orientation','vertical','Location','southeast');
set(gca,'linewidth',2);set(gca,'FontSize',14);
set(gca, 'TickDir', 'out');
filename=char(strcat('/phoddat/share/asaha/plots/plots_timeseries_SSS/satSSS_days90_afterTCallf_b2_9_AmazonNatlantic_2010_2022crain'));
%filename=char(strcat('/phoddat/share/asaha/plots_timeseries_SSS/satSSS_days365_afterTC_NGoM_2002_2021'));
%filename=char(strcat('/phoddat/share/asaha/plots_timeseries_SSS/satSSS_days365_afterTC_SGoM_2002_2021'));
print(filename,'-dpng');
close all

figure('units','centimeters','position',[0 0 36 12]);%set(h, 'Visible', 'off');
plot(time,mean_SSS1_wake,'r','linewidth',2);
hold on; plot(time,mean_SSS2_wake,'k','linewidth',2);
%hold on; plot(time,mean_SSS2_wake_sm,'b','linewidth',2);
hold on; plot(time,mean_SSS2_wake_sm7,'c','linewidth',2);
hold on; plot(time,zero,'color',[.6 .6 .6],'LineWidth',1,'HandleVisibility','off');
xlim([-10 90]);
ylabel('SSS anomalies wake (psu)');
%xlabel('10 days before and 100 days after TC (2002-2021)');
xlabel('10 days before and 90 days after TC, 2010-2022');
legend({'within 3x3 deg from TC','at Argo locations within 3x3{\circ} from TC, R=0.129,P=0.1974','7 days moving mean at Argo locations, R=0.19,P=0.049'},'Orientation','vertical','Location','southeast');
set(gca,'linewidth',2);set(gca,'FontSize',14);
set(gca, 'TickDir', 'out');
filename=char(strcat('/phoddat/share/asaha/plots/plots_timeseries_SSS/satSSSwake_days90_afterTCall_b2_9_AmazonNatlantic_2010_2022crain'));
%filename=char(strcat('/phoddat/share/asaha/plots/satSSS_days365_afterTC_NGoM_2002_2021'));
%filename=char(strcat('/phoddat/share/asaha/plots/satSSS_days365_afterTC_SGoM_2002_2021'));
print(filename,'-dpng');
close all



