function [Phase,cf_,cf2_,cf3_]=FitPhase(fS,FS,FW)

%makes a linear fit to the phase diffence between stations
%input
Lon=[25.2, -105.2, -123.2, -150.1]; %lon of staion
T=[14.5349,14.7601,14.76601,16.0000]; %senter of period that is interesting
LEG=[0,1.5,3,7.5]; % phase differnce in days of the cross correlation
start='01,12,01'; %start of datasets that are crosscorrelated
ende='15,02,02'; %end of datasets that are crosscorrelated


Phase.Lon=Lon;
dLEG=1;
Phase.PhaseRadar=LEG./mean(T)*360;
Phase.dPhaseRadar=1/mean(T)^2*sqrt((LEG.*(T-mean(T))).^2+(mean(T)*dLEG)^2)*360;


% Data from data set "phase":
%     X = lonind:
%     Y = phase:
%     Weights = dPhase:

% --- Plot data that was originally in data set "phase"
lonind = Phase.Lon(:);
phase = Phase.PhaseRadar(:);
dPhase = Phase.dPhaseRadar(:);

% --- Create fit "fit 1"
ok_ = isfinite(lonind) & isfinite(phase) & isfinite(dPhase);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[-5    -180],'Upper',[5  180]);
st_ = [1 3];
set(fo_,'Startpoint',st_);
set(fo_,'Weight',1./(dPhase(ok_)));
ft_ = fittype('p1*x +p2');
% Fit this model using new data
cf_ = fit(lonind(ok_),phase(ok_),ft_,fo_);

% Plot this fit
cd C:\Users\kleinkne\Documents\PHD\MatLabFunc
fh=get_figure(fS,FS,FW);

pi95=confint(cf_);
Phase.Fit1=cf_.p1;
Phase.Fiterror1=abs((pi95(1,1)-cf_.p1)/2);
hold on
h0=errorbar(Phase.Lon,Phase.PhaseRadar,Phase.dPhaseRadar,'bo','LineWidth',4);
%%
title(['phase shift ',start,'-',ende])

xlabel('Longitude west of Hankasalmi [degree]')
ylabel('Phase shift from Hankasalmi [degree]')
set(gca,'xtick',[-154.8:20:25.2])
set(gca,'xticklabel',[180:-20:0])
xlim([-155,25.4]);
set(gca,'ytick',[-20:20:200])

ylim([-20,200])
%%
l1=plot([25.2-90,-154.8],[-45,135],'k','linewidth',3);
l2=plot([25.2-90,-154.8],[-65,115],'k--','linewidth',2);
l3=plot([25.2-90,-154.8],[-20,160],'k--','linewidth',2);

l4=plot([25.2,-154.8],[0,0.5*360],'k','linewidth',3);
l5=plot([25.2,-154.8],[20,200],'k--','linewidth',2);
l6=plot([25.2,-154.8],[-20,160],'k--','linewidth',2);


%%
% Create textbox
annotation(fh,'textbox',...
    [0.24 0.19 0.08 0.05],...
    'String',{'Saskatoon'},...
    'FontWeight',FW,...
    'FontSize',FS,...
    'FitBoxToText','on',...
    'LineStyle','none',...
    'Color',[0 0 1]);

% Create textbox
annotation(fh,'textbox',...
    [0.12 0.29 0.05 0.08],...
    'String',{'Prince George'},...
    'FontWeight',FW,...
    'FontSize',FS,...
    'FitBoxToText','on',...
    'LineStyle','none',...
    'Color',[0 0 1]);

% Create textbox
annotation(fh,'textbox',...
    [0.48 0.6 0.2 0.07],...
    'String',{'theoretical S_1-phase shift'},...
    'LineStyle','none',...
    'FontWeight',FW,...
    'FontSize',FS,...
    'FitBoxToText','on');

% Create textbox
annotation(fh,'textbox',...
    [0.46 0.13 0.2 0.07],...
    'String',{'theoretical S_2-phase shift'},...
    'LineStyle','none',...
    'FontWeight',FW,...
    'FontSize',FS,...
    'FitBoxToText','on');

% Create textbox
annotation(fh,'textbox',...
    [0.15 0.84 0.055 0.045],...
    'String',{'Kodiak'},...
    'FontWeight',FW,...
    'FontSize',FS,...
    'FitBoxToText','on',...
    'LineStyle','none',...
    'Color',[0 0 1]);

% Create textbox
annotation(fh,'textbox',...
    [0.72 0.33 0.07 0.044],...
    'String',{'Hankasalmi'},...
    'FontWeight',FW,...
    'FontSize',FS,...
    'FitBoxToText','on',...
    'LineStyle','none',...
    'Color',[0 0 1]);


%%
h1_ = plot([-180,180],cf_([-180,180]));
set(h1_(1),'Color',[1 0 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
st_ = [1 25];
set(fo_,'Startpoint',st_);
set(fo_,'Weight',1./[dPhase(1);dPhase(4)]);

cf2_ = fit([lonind(1);lonind(4)],[phase(1); phase(4)],ft_,fo_);
h2_ = plot([-180,180], cf2_([-180,180]));
set(h2_(1),'Color',[1 0 0],...
    'LineStyle','- -', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);hold on
Phase.Fit2=cf2_.p1;

st_ = [2 100];
set(fo_,'Startpoint',st_);
set(fo_,'Weight',1./(dPhase(2:3)));
cf3_ = fit(lonind(2:3),phase(2:3),ft_,fo_);
h3_ = plot([-180,180], cf3_([-180,180]));
set(h3_(1),'Color',[1 0 0],...
    'LineStyle',':', 'LineWidth',4,...
    'Marker','none', 'MarkerSize',6);
Phase.Fit3=cf3_.p1;

legend([h0(1),h1_(1),h2_(1),h3_(1)],'phase difference','fit all four stations', 'fit Hankasalmi, Kodiak', 'fit Prince George Saskatoon')

cd C:\Users\kleinkne\Documents\PHD\Paper1\Figures
saveas(gca,'Fig5','fig')
saveas(gca,'Fig5.eps','epsc')
cd C:\Users\kleinkne\Documents\PHD\MatLabFunc
makePDF(gca,fS,'Fig5','C:\Users\kleinkne\Documents\PHD\Paper1\Figures')
 cd C:\Users\kleinkne\Documents\PHD\Paper1\Figures
end
