function [Climatology] = ClimatologyWind(DataFolder,location,smoothing,p)
%calculates a climatology and smoothed climatology of the meridional mean wind at the location using all existing years of the dataset
if p==1 ||p==2 || p==3

if p==1
load(fullfile(DataFolder,location,[location,'_Fit']));
Climatology.Name=['Climatology_',location];
elseif p==2
    load(fullfile(DataFolder,location,[location,'_Fit2']));
Climatology.Name=['Climatology2_',location]
Climatology.WeightedName=['WeightedClimatology2_',location]
elseif p==3
    load(fullfile(DataFolder,location,[location,'_Fit3']));
Climatology.Name=['Climatology3_',location]
Climatology.WeightedName=['WeightedClimatology3_',location]
end

Climatology.Climatology=zeros(365,1);%climatology
Climatology.WeightedClimatology=zeros(365,1);%Weigthed climatology
Climatology.WeightedClimatology_err=zeros(365,1);%error Weigthed climatology
Climatology.WeightedClimatology_STD=zeros(365,1);%std Weigthed climatology
Climatology.DayofYear=zeros(365,1);%day of year
Climatology.N=zeros(365,1);%number off years per day of year exiting
Climatology.MeanWindMinusC=zeros(size(Data.Fit.DaysWithFit));%anomalyWind (mean wind - climatology)
Climatology.MeanWindMinusWC=zeros(size(Data.Fit.DaysWithFit));%anomalyWind (mean wind - Weighted climatology)
Climatology.MeanWindMinusSC=zeros(size(Data.Fit.DaysWithFit)); %anomalyWind (mean wind - smoothed climatology)
Climatology.MeanWindMinusSWC=zeros(size(Data.Fit.DaysWithFit));%anomalyWind (mean wind - smoothed weighted climatology)
Climatology.MeanWindMinusSC_95=zeros(size(Data.Fit.DaysWithFit));% error smoothed climatology
Climatology.MeanWindMinusSWC_err=zeros(size(Data.Fit.DaysWithFit));%error smootehd weighted climatology

DV=datevec(Data.Fit.DaysWithFit); %Datevec of all days that exist
DayofYear=Data.Fit.DaysWithFit-datenum(DV(:,1),1,1)'+1;%Day of year of all existing days
Climatology.MeanWindDate=Data.Fit.DaysWithFit;% existing days
%Climatology.Smooth %smoothed climatology (defined later)
%Climatology.WeightedSmooth %smoothed weighted climatology (defined later)
for i=1:365
    ind=find(DayofYear==i);%& Climatology.DaysWithModes>732173 &Climatology.DaysWithModes<733999);
    if isempty(ind)
    %pause
    disp(['missing day in climatology:',int2str(i)])
    end
        Climatology.Climatology(i)=mean(Data.Fit.a0(ind));
        Climatology.WeightedClimatology(i)=sum(Data.Fit.a0(ind)./Data.Fit.a0_95(ind).^2)/sum(1./Data.Fit.a0_95(ind).^2);
        Climatology.WeightedClimatology_err(i)=(1/sum(1./(Data.Fit.a0_95(ind)./2).^2));
        Climatology.WeightedClimatology_STD(i)=std(Data.Fit.a0(ind));
        Climatology.DayofYear(i)=i;
        Climatology.MeanWindMinusC(ind)=Data.Fit.a0(ind)-Climatology.Climatology(i);
        Climatology.MeanWindMinusWC(ind)=Data.Fit.a0(ind)-Climatology.WeightedClimatology(i);
        Climatology.N(i)=size(ind,2);
end


Climatology.Smooth=SmoothClimatology(Climatology.Climatology,smoothing);
Climatology.WeightedSmooth=SmoothClimatology(Climatology.WeightedClimatology,smoothing);


for i=1:365
    ind=find(DayofYear==i);
        Climatology.MeanWindMinusSC(ind)=Data.Fit.a0(ind)-Climatology.Smooth(i);
        Climatology.MeanWindMinusSWC(ind)=Data.Fit.a0(ind)-Climatology.WeightedSmooth(i);
        
        
        Climatology.MeanWindMinusSC_95(ind)=Data.Fit.a0_95(ind)./Data.Fit.a0(ind); %procetage error
        Climatology.MeanWindMinusSWC_err(ind)=(Climatology.WeightedClimatology_err(i)+(Data.Fit.a0_95(ind)./2).^2).^(1/2);
end


title1=[Climatology.Name,'_',int2str(DV(1,1)),'/',int2str(DV(1,2)),'-',int2str(DV(end,1)),'/',int2str(DV(end,2))];
title2='MeanWind-Climatology';
title3=['SmoothClimatology; Smoothing=+/-',int2str((smoothing-mod(smoothing,2))/2),' days','_',int2str(DV(1,1)),'/',int2str(DV(1,2)),'-',int2str(DV(end,1)),'/',int2str(DV(end,2))];
title4='MeanWind-SmoothClimatology';


figure1=plot4sub(title1,'Day of Year','',Climatology.DayofYear,Climatology.Climatology,...
         title2,'Date[yymmdd]','',Climatology.MeanWindDate,Climatology.MeanWindMinusC,...
         title3,'Day of Year','',Climatology.DayofYear,Climatology.Smooth,...
         title4,'Date[yymmdd]','',Climatology.MeanWindDate,Climatology.MeanWindMinusSC);

save(fullfile(DataFolder,location,[Climatology.Name,'_Smoothing_',int2str(smoothing)]),'Climatology')

if p==1
saveas(figure1,fullfile(DataFolder,location,['SmoothClimatology_',int2str(smoothing),'_',location]),'fig')
elseif p==2
saveas(figure1,fullfile(DataFolder,location,['SmoothClimatology2_',int2str(smoothing),'_',location]),'fig')
elseif p==3
saveas(figure1,fullfile(DataFolder,location,['SmoothClimatology3_',int2str(smoothing),'_',location]),'fig')
end

title1=[Climatology.WeightedName,'_',int2str(DV(1,1)),'/',int2str(DV(1,2)),'-',int2str(DV(end,1)),'/',int2str(DV(end,2))];
title2='MeanWind-WeightedClimatology';
title3=['SmoothWeightedClimatology; Smoothing=+/-',int2str((smoothing-mod(smoothing,2))/2),' days','_',int2str(DV(1,1)),'/',int2str(DV(1,2)),'-',int2str(DV(end,1)),'/',int2str(DV(end,2))];
title4='MeanWind-SmoothWeightedClimatology';


figure2=plot4sub(title1,'Day of Year','',Climatology.DayofYear,Climatology.WeightedClimatology,...
         title2,'Date[yymmdd]','',Climatology.MeanWindDate,Climatology.MeanWindMinusWC,...
         title3,'Day of Year','',Climatology.DayofYear,Climatology.WeightedSmooth,...
         title4,'Date[yymmdd]','',Climatology.MeanWindDate,Climatology.MeanWindMinusSWC);

if p==1
saveas(figure2,fullfile(DataFolder,location,['SmoothWeightedClimatology_',int2str(smoothing),'_',location]),'fig')
elseif p==2
saveas(figure2,fullfile(DataFolder,location,['SmoothWeightedClimatology2_',int2str(smoothing),'_',location]),'fig')
elseif p==3
saveas(figure2,fullfile(DataFolder,location,['SmoothWeightedClimatology3_',int2str(smoothing),'_',location]),'fig')
end


end
end

