file={'./ECMWF/2018_pressure_1.nc','./ECMWF/2018_pressure_2.nc','./ECMWF/2018_pressure_3.nc','./ECMWF/2018_pressure_4.nc'};
wind.time = datenum('01-janv-2018'):1/24:datenum('31-dec-2018 23:00');
wind.latitude=flip(double(ncread(file{1},'latitude')));
wind.longitude=double(ncread(file{1},'longitude'));
wind.pressure=double([ncread(file{1},'level') ; ncread(file{2},'level') ; ncread(file{3},'level') ; ncread(file{4},'level') ]);
wind.alt = (1-(wind.pressure*100/101325).^(1/5.25588))/2.25577/10^(-5);
tmp_1 = permute(flip(ncread(file{1},'u'),2) , [2 1 4 3]);
tmp_2 = permute(flip(ncread(file{2},'u'),2) , [2 1 4 3]);
tmp_3 = permute(flip(ncread(file{3},'u'),2) , [2 1 4 3]);
tmp_4 = permute(flip(ncread(file{4},'u'),2) , [2 1 4 3]);
wind.u = cat(4,tmp_1,tmp_2,tmp_3,tmp_4); % m/s
tmp_1 = permute(flip(ncread(file{1},'v'),2) , [2 1 4 3]);
tmp_2 = permute(flip(ncread(file{2},'v'),2) , [2 1 4 3]);
tmp_3 = permute(flip(ncread(file{3},'v'),2) , [2 1 4 3]);
tmp_4 = permute(flip(ncread(file{4},'v'),2) , [2 1 4 3]);
wind.v = cat(4,tmp_1,tmp_2,tmp_3,tmp_4); % m/s
tmp_1 = permute(flip(ncread(file{1},'t'),2) , [2 1 4 3]);
tmp_2 = permute(flip(ncread(file{2},'t'),2) , [2 1 4 3]);
tmp_3 = permute(flip(ncread(file{3},'t'),2) , [2 1 4 3]);
tmp_4 = permute(flip(ncread(file{4},'t'),2) , [2 1 4 3]);
wind.t = cat(4,tmp_1,tmp_2,tmp_3,tmp_4); % K
Fu = griddedInterpolant({wind.latitude,wind.longitude,datenum(wind.time),wind.alt},wind.u,'linear','linear');
Fv = griddedInterpolant({wind.latitude,wind.longitude,datenum(wind.time),wind.alt},wind.v,'linear','linear');
Ft = griddedInterpolant({wind.latitude,wind.longitude,datenum(wind.time),wind.alt},wind.t,'linear','linear');
windu = permute(Fu({dc(i_d).lat,dc(i_d).lon,datenum(dc(i_d).time),dc(1).alt}),[3,4,1,2]);
windv = permute(Fv({dc(i_d).lat,dc(i_d).lon,datenum(dc(i_d).time),dc(1).alt}),[3,4,1,2]);
dc(i_d).wt = permute(Ft({dc(i_d).lat,dc(i_d).lon,datenum(dc(i_d).time),dc(1).alt}),[3,4,1,2]);
dc(i_d).ws = windu + 1i*windv;
figure('position',[0 0 300 300]); hold on; hold on
% plot(xi1(islocalmax(ft)&islocalmax(ft,2)), xi2(islocalmax(ft)&islocalmax(ft,2)),'.r')
surf(xi1, xi2, ft,'FaceAlpha',0.5)
[~,p1]=contour3(xi1, xi2, gmfit.ComponentProportion(1)*f_1,10,'Color',[162 29 49]/255,'linewidth',1.5);
[~,id_max] = max(f_1(:));
plot3(xi1(id_max),xi2(id_max),gmfit.ComponentProportion(1)*f_1(id_max),'+','Color',[162 29 49]/255,'LineWidth',2,'MarkerSize',6)
plot3([xi1(id_max) xi1(id_max)],[xi2(id_max) xi2(id_max)], [0 gmfit.ComponentProportion(1)*f_1(id_max)],'Color',[162 29 49]/255,'LineWidth',1.5 )
[~,p2]=contour3(xi1, xi2, gmfit.ComponentProportion(2)*f_2,10,'Color',[127 47 141]/255,'linewidth',1.5);
[~,id_max] = max(f_2(:));
plot3(xi1(id_max),xi2(id_max),gmfit.ComponentProportion(2)*f_2(id_max),'+','Color',[127 47 141]/255,'LineWidth',2,'MarkerSize',6)
plot3([xi1(id_max) xi1(id_max)],[xi2(id_max) xi2(id_max)], [0 gmfit.ComponentProportion(2)*f_2(id_max)],'Color',[127 47 141]/255,'LineWidth',1.5 )
axis tight;shading interp; view(37,31)
xlabel('airspeed [m/s]'); ylabel('\sigma_{vvp} [m/s]'); zlabel('PDF')
%legend([p1 p2], 'Multi-normal corresponding to bird','Multi-normal corresponding to insect')
Ax = gca; Ax.ZAxis.TickValues=[];
colormap(gca,viridis) %colormap(gca,1-(1-viridis)/1.2) %
figure('position',[0 0 800 600]); hold on; hold on
h(1)=pcolor(xi1(1,:), xi2(:,1), ft./max(ft(:)));
h(2)=plot([0 15],[2 2],'r','linewidth',2);
plot([5 5],[0 7],'r','linewidth',2);
[~,h(3)]=contour(xi1, xi2, f_1,6,'Color',[162 29 49]/255,'LineWidth',1.5);
[~,id_max] = max(f_1(:)); plot(xi1(id_max),xi2(id_max),'+','Color',[162 29 49]/255,'LineWidth',2,'MarkerSize',10)
[~,h(4)]=contour(xi1, xi2, f_2,6,'Color',[127 47 141]/255,'LineWidth',1.5);
[~,id_max] = max(f_2(:)); plot(xi1(id_max),xi2(id_max),'+','Color',[127 47 141]/255,'LineWidth',2,'MarkerSize',10)
[C,h(5)]=contour(xi1, xi2, f_2./(f_1+f_2),.1:.2:.9,'w','ShowText','on','LineWidth',2);
clabel(C,h(5),'FontSize',15,'Color','w','LabelSpacing',500,'FontWeight','bold')
axis tight;shading interp;
xlabel('airspeed [m/s]'); ylabel('\sigma_{vvp} [m/s]');
legend(h,{'Kernel density estimation','Traditional Threashold approach','Bird Gaussian Fit','Insect Gaussian Fit','New Proportion approach'})
colormap(gca,1-(1-viridis)/1.2) %colormap(gca,viridis)
ftime = nan(size(xi1,1), size(xi1,2),nmonth);
id = tmonth(i)<t & tmonth(i+1)>t;
X = [reshape(abs(v_a(id,:,:)),[],1),reshape(sdvvp(id,:,:),[],1)];
X = X(~any(isnan(X),2),:);
ftime(:,:,i) = reshape(ksdensity(X, [xi1(:), xi2(:)]),size(xi1));
ftime(:,:,i) = ftime(:,:,i)./sum(sum(ftime(:,:,i)));
mismatch = @(alpha) ( alpha.*f_1 + (1-alpha).*f_2);
amplit(i) = fminsearch(@(alpha) sum(sum((ftime(:,:,i)-mismatch(alpha)).^2)) , 0.5 );
figure('position',[0 0 1200 800]); hold on;
ax=subplot(3,4,i); hold on
id = tmonth(i)<t & tmonth(i+1)>t;
pcolor(xi1(1,:), xi2(:,1), ftime(:,:,i)./max(max(ftime(:,:,i))))
contour(xi1, xi2, amplit(i)*f_1,[0.0002 0.01],'Color',[162 29 49]/255,'LineWidth',1.5);
[~,id_max] = max(f_1(:));
plot(xi1(id_max),xi2(id_max),'+','Color',[162 29 49]/255,'LineWidth',2,'MarkerSize',amplit(i)*20)
contour(xi1, xi2, (1-amplit(i))*f_2,[0.0002 0.01],'Color',[127 47 141]/255,'LineWidth',1.5);
[~,id_max] = max(f_2(:));
plot(xi1(id_max),xi2(id_max),'+','Color',[127 47 141]/255,'LineWidth',2,'MarkerSize',(1-amplit(i))*20)
[C,h]=contour(xi1, xi2, (1-amplit(i))*f_2./(amplit(i)*f_1+(1-amplit(i))*f_2),[.1 .5 .9],'w','ShowText','on','LineWidth',2);
clabel(C,h,'Color','w','LabelSpacing',500,'FontWeight','bold')
axis tight;shading interp;
title(month(median(dc(1).time(id)),'name'))
%xlabel('airspeed [m/s]'); ylabel('\sigma_{vvp} [m/s]');
colormap(gca,1-(1-viridis)/1.2)
i_c = ceil(amplit(i)*size(c_map,1));
ax.XColor = c_map(i_c,:);
ax.YColor = c_map(i_c,:);
ax.XTick=[]; ax.YTick=[];
amplir = nan(1,numel(dc));
fradar = nan(size(xi1,1), size(xi1,2),numel(dc));
X = [reshape(abs(v_a(:,:,i_d)),[],1),reshape(sdvvp(:,:,i_d),[],1)];
X = X(~any(isnan(X),2),:);
fradar(:,:,i_d) = reshape(ksdensity(X, [xi1(:), xi2(:)]),size(xi1));
fradar(:,:,i_d) = fradar(:,:,i_d)./sum(sum(fradar(:,:,i_d)));
mismatch = @(alpha) ( alpha.*f_1 + (1-alpha).*f_2);
amplir(i_d) = fminsearch(@(alpha) sum(sum((fradar(:,:,i_d)-mismatch(alpha)).^2)) , 0.5 );
figure('position',[0 0 1000 1000]); hold on;
ax=subplot(6,6,i_d); hold on
pcolor(xi1(1,:), xi2(:,1), fradar(:,:,i_d)./max(max(fradar(:,:,i_d))))
contour(xi1, xi2, amplir(i_d)*f_1,5,'Color',[162 29 49]/255);
contour(xi1, xi2, (1-amplir(i_d))*f_2,5,'Color',[127 47 141]/255);
[C,h]=contour(xi1, xi2, (1-amplir(i_d))*f_2./(amplir(i_d)*f_1+(1-amplir(i_d))*f_2),[.1 .5 .9],'w','ShowText','on','LineWidth',2);
clabel(C,h,'Color','w','LabelSpacing',500,'FontWeight','bold')
axis tight;shading interp; title(dc(i_d).name)
ax.XTick=[]; ax.YTick=[];
%xlabel('airspeed [m/s]'); ylabel('\sigma_{vvp} [m/s]');
falt = nan(size(xi1,1), size(xi1,2), 25);
X = [reshape(abs(v_a(:,i,:)),[],1),reshape(sdvvp(:,i,:),[],1)];
X = X(~any(isnan(X),2),:);
falt(:,:,i) = reshape(ksdensity(X, [xi1(:), xi2(:)]),size(xi1));
falt(:,:,i) = falt(:,:,i)./sum(sum(falt(:,:,i)));
mismatch = @(alpha) ( alpha.*f_1 + (1-alpha).*f_2);
amplia(i) = fminsearch(@(alpha) sum(sum((falt(:,:,i)-mismatch(alpha)).^2)) , 0.5 );
figure('position',[0 0 1200 1000]); hold on; c_map = magma;
ax=subplot(4,5,i); hold on
pcolor(xi1(1,:), xi2(:,1), falt(:,:,i)./max(max(falt(:,:,i))))
contour(xi1, xi2, amplia(i)*f_1,5,'Color',[162 29 49]/255);
contour(xi1, xi2, (1-amplia(i))*f_2,5,'Color',[127 47 141]/255);
contour(xi1, xi2, (1-amplia(i))*f_2./(amplia(i)*f_1+(1-amplia(i))*f_2),[.1 .5 .9],'w','ShowText','on','LineWidth',2)
axis tight;shading interp; xticks([]); yticks([])
%xlabel('airspeed [m/s]'); ylabel('\sigma_{vvp} [m/s]');
title([num2str(dc(1).alt(i)-100) ' - ' num2str(dc(1).alt(i)+100) ' m'])
i_c = ceil(amplia(i)*size(c_map,1));
ax.XColor = c_map(i_c,:);
ax.YColor = c_map(i_c,:);
ax.XTick=[]; ax.YTick=[];
ampliall = nan(nmonth,numel(dc));
fall = nan(size(xi1,1), size(xi1,2), nweek, numel(dc));
nall = nan(nmonth,numel(dc));
id = tmonth(max(1,i-1))<t & tmonth(min(nweek,i+2))>t;
X = [reshape(abs(v_a(id,:,i_d)),[],1),reshape(sdvvp(id,:,i_d),[],1)];
X = X(~any(isnan(X),2),:);
fall(:,:,i,i_d) = reshape(ksdensity(X, [xi1(:), xi2(:)]),size(xi1));
fall(:,:,i,i_d) = fall(:,:,i,i_d)./sum(sum(fall(:,:,i,i_d)));
mismatch = @(alpha) ( alpha.*f_1 + (1-alpha).*f_2);
ampliall(i,i_d) = fminbnd(@(alpha) sum(sum((fall(:,:,i,i_d)-mismatch(alpha)).^2)) , 0, 1 );
ampliall(ampliall==0)=nan;
i_d = find(strcmp({dc.name}, 'dedrs'));
figure('position',[0 0 1200 800]); hold on;
ax=subplot(3,4,i); hold on
h = worldmap([min([dc.lat]) max([dc.lat])], [min([dc.lon]) max([dc.lon])]);
setm(h,'frame','on','grid','off'); set(findall(h,'Tag','MLabel'),'visible','off'); set(findall(h,'Tag','PLabel'),'visible','off')
geoshow('landareas.shp', 'FaceColor', [77 77 77]./255);
scatterm([dc.lat], [dc.lon], 60,'ok', 'filled');
scatterm(dc(i_d).lat, dc(i_d).lon, 120,'or','filled');
elseif all(all(isnan(fall(:,:,i,i_d))))
%title(month(median(dc(1).time(id)),'name'))
ax=subplot(3,4,i); hold on
id = tmonth(i)<t & tmonth(i+1)>t;
pcolor(xi1(1,:), xi2(:,1), fall(:,:,i,i_d)./max(max(fall(:,:,i,i_d))))
contour(xi1, xi2, ampliall(i,i_d)*f_1,[0.0002 0.01],'Color',[162 29 49]/255,'LineWidth',1.5);
[~,id_max] = max(f_1(:));
plot(xi1(id_max),xi2(id_max),'+','Color',[162 29 49]/255,'LineWidth',2,'MarkerSize',ampliall(i,i_d)*20)
contour(xi1, xi2, (1-ampliall(i,i_d))*f_2,[0.0002 0.01],'Color',[127 47 141]/255,'LineWidth',1.5);
[~,id_max] = max(f_2(:));
plot(xi1(id_max),xi2(id_max),'+','Color',[127 47 141]/255,'LineWidth',2,'MarkerSize',(1-ampliall(i,i_d))*20)
[C,h]=contour(xi1, xi2, (1-ampliall(i,i_d))*f_2./(ampliall(i,i_d)*f_1+(1-ampliall(i,i_d))*f_2),[.1 .5 .9],'w','ShowText','on','LineWidth',2);
clabel(C,h,'Color','w','LabelSpacing',500,'FontWeight','bold')
axis tight;shading interp;
title(month(median(dc(1).time(id)),'name'))
%xlabel('airspeed [m/s]'); ylabel('\sigma_{vvp} [m/s]');
colormap(gca,1-(1-viridis)/1.2)
i_c = ceil(ampliall(i,i_d)*size(c_map,1));
ax.XColor = c_map(i_c,:);
ax.YColor = c_map(i_c,:);
ax.XTick=[]; ax.YTick=[];
% saveas(gcf,['C:\Users\rnussba1\switchdrive\Paper\InsectWR\figure\PerRadar\' dc(i_d).name '.png']) % ,'-dpdf -painters epsFig'
figure('position',[0 0 1200 800]);
id = tmonth(i)<t & tmonth(i+1)>t;
subplot(3,4,i);title(month(median(dc(1).time(id)),'name'))
h = worldmap([min([dc.lat]) max([dc.lat])], [min([dc.lon]) max([dc.lon])]);
setm(h,'frame','on','grid','off'); set(findall(h,'Tag','MLabel'),'visible','off'); set(findall(h,'Tag','PLabel'),'visible','off')
geoshow('landareas.shp', 'FaceColor', [77 77 77]./255);
scatterm([dc(id_de).lat], [dc(id_de).lon], 180,ampliall(i,id_de),'s', 'filled');
scatterm([dc(id_fr).lat], [dc(id_fr).lon], 180, ampliall(i,id_fr),'o','filled');
scatterm([dc(~id_fr&~id_de).lat]', [dc(~id_fr&~id_de).lon]', 180, ampliall(i,~id_fr&~id_de)','d','filled');
figure('position',[0 0 1000 450]); hold on;
plot( t, ti,'Color',[.7 .7 .7],'LineWidth',2);
scatter(repmat(tmonth_mid',sum(id_de),1), reshape(ampliall(:,id_de),1,[])',[],reshape(ampliall(:,id_de),1,[])', 's','filled','MarkerEdgeColor','k');
scatter(repmat(tmonth_mid',sum(id_fr),1), reshape(ampliall(:,id_fr),1,[])',[],reshape(ampliall(:,id_fr),1,[])', 'o','filled','MarkerEdgeColor','k');
scatter(repmat(tmonth_mid',sum(~id_fr&~id_de),1), reshape(ampliall(:,~id_fr&~id_de),1,[])',[],reshape(ampliall(:,~id_fr&~id_de),1,[])', 'd','filled','MarkerEdgeColor','k');
ylim([0 1]); xlim([31 365]); datetick('x','keeplimits');
c=colorbar;c.Label.String='Ratio of amplitude';
gaussian = @(mu,sigma,x) exp(-((x-mu)/sigma).^2/2);
skewedgaussian = @(mu,sigma,alpha,x) 2*gaussian(mu,sigma,x).*normcdf(alpha*(x-mu)/sigma);
funall= @(A1,m1,s1,a1,A2,m2,s2,a2,x) (A1*skewedgaussian(m1,s1,a1,x) + A2*(skewedgaussian(m2,s2,a2,x)+skewedgaussian(m2+365,s2,a2,x)));
funratio = @(A1,m1,s1,a1,A2,m2,s2,a2,x) A1*skewedgaussian(m1,s1,a1,x)./(A1*skewedgaussian(m1,s1,a1,x) + A2*(skewedgaussian(m2,s2,a2,x)+skewedgaussian(m2+365,s2,a2,x)));
parmbird0=[0.9,365/2,365/15,0];
parminsect0=[0.1,0,365/15,0];
parmall0=[parmbird0 parminsect0];
[xData, yData] = prepareCurveData( tmonth_mid, 1-ampliall_fil(:,i_d) );
fitresult{i_d} = fit( xData, yData, funall,...
'StartPoint', parmall0, ...
'Lower', [.5, 50, 5, -5, .01, -15, 5, -5], ...
'Upper', [ 1, 150,100, 5, 1, 50, 50, 5]);
% A1, m1, s1, a1, A2, m2, s2, a2
figure('position',[0 0 800 450]);
i_c = ceil((dc(i_d).lat-43.5)/(54.2-43.5)*size(parula,1));
plot( 1:365, fitresult{i_d}.A2*skewedgaussian(fitresult{i_d}.m2,fitresult{i_d}.s2,fitresult{i_d}.a2,1:365),'Color',[.7 .7 .7]);
plot( 1:365, fitresult{i_d}.A1*skewedgaussian(fitresult{i_d}.m1,fitresult{i_d}.s1,fitresult{i_d}.a1,1:365),'Color',[.5 .5 .5]);
plot( 1:365, fitresult{i_d}.A2*skewedgaussian(fitresult{i_d}.m2+365,fitresult{i_d}.s2,fitresult{i_d}.a2,1:365),'Color',[.7 .7 .7]);
scatter(repmat(tmonth_mid',sum(id_de),1), 1-reshape(ampliall(:,id_de),1,[])',[],reshape(ampliall(:,id_de),1,[])', 's','filled','MarkerEdgeColor','k');
scatter(repmat(tmonth_mid',sum(id_fr),1), 1-reshape(ampliall(:,id_fr),1,[])',[],reshape(ampliall(:,id_fr),1,[])', 'o','filled','MarkerEdgeColor','k');
scatter(repmat(tmonth_mid',sum(~id_fr&~id_de),1), 1-reshape(ampliall(:,~id_fr&~id_de),1,[])',[],reshape(ampliall(:,~id_fr&~id_de),1,[])', 'd','filled','MarkerEdgeColor','k')
datetick('x','keeplimits');
ylim([0 1]); xlim([31 365]); datetick('x','keeplimits');
box on; grid on; legend('Insect','Weather')
ylabel('1 - Amplitude Ratio'); colormap(gca,magma)
i_c = ceil((dc(i_d).lat-43.5)/(54.2-43.5)*size(parula,1));
plot( 1:365, funratio(fitresult{i_d}.A1,fitresult{i_d}.m1,fitresult{i_d}.s1,fitresult{i_d}.a1,fitresult{i_d}.A2,fitresult{i_d}.m2,fitresult{i_d}.s2,fitresult{i_d}.a2,1:365),'Color',[.4 .4 .4],'LineWidth',2);
datetick('x','keeplimits');ylim([0 1]); xlim([31 365]); grid on; box on
XI = normrnd(gmfit.mu(2,1), sqrt(gmfit.Sigma(1,1,2)),100000,1);
XB = normrnd(gmfit.mu(1,1), sqrt(gmfit.Sigma(1,1,1)),100000,1);
%XI = normrnd(gmfit.mu(1,1), 2.5,1000,1);
%XB = normrnd(gmfit.mu(2,1), 2.5,1000,1);
X = alpha0*XI + (1-alpha0)*XB;
% fg = @(mu,sigma,x) exp(-((x-mu)/sigma).^2/2);
% fb = @(b,x,alpha) fg(gmfit.mu(1,1),sqrt(gmfit.Sigma(1,1,1)),b) .* fg((1-alpha)*b+alpha*gmfit.mu(2,1), sqrt(gmfit.Sigma(1,1,2)),x);
% cb = @(x,alpha,r) integral( @(b) fb(b,x,alpha),-Inf,r) ./ integral( @(b) fb(b,x,alpha),-Inf,Inf);
% fsample = @(x,alpha) fsolve( @(r) abs(cb(x,alpha,r)-rand()), 10);
% s1 = gmfit.Sigma(1,1,1);
% m2 = @(alpha,x) (x-alpha*gmfit.mu(2,1))/(1-alpha);
% s2 = @(alpha) gmfit.Sigma(1,1,2)/(1-alpha)^2;
% m12 = @(alpha,x) (m1*s2(alpha)+m2(alpha,x)*s1)/(s1*s2(alpha));
% s12 = @(alpha) (s1*s2(alpha))/(s1+s2(alpha));
% pdf12 = @(alpha,x) normpdf(as,m12(alpha,x),sqrt(s12(alpha)));
% rnd12 = @(alpha,x) normrnd(m12(alpha,x),sqrt(s12(alpha)),1);
pBsX = pdfB .* normpdf(X,alpha0.* gmfit.mu(2,1) + (1-alpha0)*as, sqrt(gmfit.Sigma(1,1,2)));
pBsX = pBsX./sum(pBsX,2);
[~,id] = min(abs(cumsum(pBsX,2) - rand(numel(X),1)),[],2);
% tmp = cumsum(pBsX'./sum(pBsX'));
% plot(as,cumsum(tmp)./sum(tmp))
% plot(as(i),cb(X,alpha0,as(i)),'.k')
%plot(normpdf(as,m2(alpha0,X),sqrt(s2(alpha0))))
%plot(normpdf(as,m2(alpha0,X),sqrt(s2(alpha0))))
%plot(normpdf(X,alpha0.* gmfit.mu(2,1) + (1-alpha0)*as, sqrt(gmfit.Sigma(1,1,2))))
Xcorr = as(id)+median(diff(as))*(rand(size(id'))-.5);
% Xcorr = X .* (1 - alpha0 + sqrt(gmfit.Sigma(1,1,2))/sqrt(gmfit.Sigma(1,1,1)).*alpha0) + (gmfit.mu(2,1) - gmfit.mu(1,1)*sqrt(gmfit.Sigma(1,1,2))/sqrt(gmfit.Sigma(1,1,1)) ).*alpha0;
%Xcorr = X*(1-alpha0+4.1/2.8*alpha0) + (8 - 2.6*4.1/2.8).*alpha0;
pIsX = pdfI .* normpdf(X,(1-alpha0).* gmfit.mu(1,1) + alpha0*as, sqrt(gmfit.Sigma(1,1,1)));
pIsX = pIsX./sum(pIsX,2);
[~,id] = min(abs(cumsum(pIsX,2) - rand(numel(X),1)),[],2);
XcorrI = as(id)+median(diff(as))*(rand(size(id'))-.5);
figure('position',[0 0 1000 400]); hold on;
plot(as,pdfB,'DisplayName','Bird','linewidth',2);
plot(as,pdfI,'DisplayName','Insect','linewidth',2);
histogram(X,'Normalization','pdf')
histogram(Xcorr,'Normalization','pdf')
histogram(XcorrI,'Normalization','pdf')
l=legend(); l.String{3}='Raw data (insect+bird)'; l.String{4}='Corrected for bird'; l.String{5}='Corrected for insect';
box on; grid on; title(['Synthetic case for \alpha=' num2str(alpha0)])
alpha = reshape([dc.bird],size(v_a));
subs=randi(numel(X),100000,1);
pBsX = pdfB .* normpdf(X(subs),(1-alpha0(subs)).* gmfit.mu(2,1) + alpha0(subs)*as, sqrt(gmfit.Sigma(1,1,2)));
pBsX = pBsX./sum(pBsX,2);
[~,id] = min(abs(cumsum(pBsX,2) - rand(numel(X(subs)),1)),[],2);
Xcorr = as(id)+median(diff(as))*(rand(size(id'))-.5);
figure('position',[0 0 1000 300]); hold on;
plot(as,pdfB,'DisplayName','Bird','linewidth',2);
plot(as,pdfI,'DisplayName','Insect','linewidth',2);
histogram(X(subs),100,'Normalization','pdf')
histogram(Xcorr,100,'Normalization','pdf')
% r = 200; % ratio of correlation between vertical and time: 1 => 200m = 5minutes
% [X,Y]=meshgrid(linspace(1,r*25,25),1:numel(dc(i_d).time));
% Compute insect proportion
tmp1 = repmat(ti(:,i_d),1,25).*reshape(pdf(gm1,[reshape(abs(v_a(:,:,i_d)),[],1) reshape(dc(i_d).sd_vvp2,[],1)]),size(v_a(:,:,i_d)));
tmp2 = (1-repmat(ti(:,i_d),1,25)).*reshape(pdf(gm2,[reshape(abs(v_a(:,:,i_d)),[],1) reshape(dc(i_d).sd_vvp2,[],1)]),size(v_a(:,:,i_d)));
dc(i_d).bird = tmp1 ./(tmp1+tmp2);
% Interpolate the insect/bird ratio when dens3 exist but not sd_vvp/u/v
idp = ~isnan(dc(i_d).dens3) & isnan(dc(i_d).bird);
% Fbird = scatteredInterpolant(X(~isnan(dc(i_d).bird)), Y(~isnan(dc(i_d).bird)), dc(i_d).bird(~isnan(dc(i_d).bird)),'nearest','nearest');
% dc(i_d).bird(idp) = Fbird(X(idp), Y(idp));
Fbird = fillmissing(dc(i_d).bird,'nearest',2);
Fbird = fillmissing(Fbird,'nearest',1);
dc(i_d).bird(idp) = Fbird(idp);
% Separate Insect from weather
dc(i_d).insect=(1-dc(i_d).bird).*funratio(fitresult{i_d}.A1,fitresult{i_d}.m1,fitresult{i_d}.s1,fitresult{i_d}.a1,fitresult{i_d}.A2,fitresult{i_d}.m2,fitresult{i_d}.s2,fitresult{i_d}.a2,datenum(dc(i_d).time-datetime(2018,1,1)))';
% imagesc(dc(i_d).insect','AlphaData',~isnan(dc(i_d).insect'))
% Correct density by removing the
dc(i_d).dens4 = dc(i_d).dens3 .* dc(i_d).bird;
% Compute airspeed component (after interpolation so can't use v_a)
airspeed_u = dc(i_d).u-real(dc(i_d).ws);
airspeed_v = dc(i_d).v-imag(dc(i_d).ws);
airspeed_l2 = sqrt(airspeed_u.^2 + airspeed_v.^2);
% Compute the correction factor
%cc = speed_corr(airspeed_l2, dc(i_d).insect)
% apply to each component
%dc(i_d).u2 = airspeed_u.*cc./airspeed_l2 + dc(i_d).windu;
%dc(i_d).v2 = airspeed_v.*cc./airspeed_l2 + dc(i_d).windv;
% find nan to reduce time
idnan = ~(isnan(airspeed_l2(:)) | isnan(dc(i_d).insect(:)));
% Build the pdf of the posteriori distribution of airspeed
pBsX = pdfB .* normpdf(airspeed_l2(idnan),(1-dc(i_d).bird(idnan)).* gmfit.mu(2,1) + dc(i_d).bird(idnan)*as, sqrt(gmfit.Sigma(1,1,2)));
pBsX = pBsX./sum(pBsX,2);
% Sample randomly in the cdf
[~,id] = min(abs(cumsum(pBsX,2) - rand(sum(idnan),1)),[],2);
airspeed_l2_corr=nan(size(airspeed_l2));
airspeed_l2_corr(idnan) = as(id)+median(diff(as))*(rand(size(id'))-.5);
% Recompute the groundspeed components
dc(i_d).ub = airspeed_u.*airspeed_l2_corr./airspeed_l2 + real(dc(i_d).ws);
dc(i_d).vb = airspeed_v.*airspeed_l2_corr./airspeed_l2 + imag(dc(i_d).ws);
pIsX = pdfI .* normpdf(airspeed_l2(idnan),dc(i_d).bird(idnan).* gmfit.mu(1,1) + (1-dc(i_d).bird(idnan))*as, sqrt(gmfit.Sigma(1,1,1)));
pIsX = pIsX./sum(pIsX,2);
[~,id] = min(abs(cumsum(pIsX,2) - rand(sum(idnan),1)),[],2);
airspeed_l2_corr=nan(size(airspeed_l2));
airspeed_l2_corr(idnan) = as(id)+median(diff(as))*(rand(size(id'))-.5);
% Recompute the groundspeed components
dc(i_d).ui = airspeed_u.*airspeed_l2_corr./airspeed_l2 + real(dc(i_d).ws);
dc(i_d).vi = airspeed_v.*airspeed_l2_corr./airspeed_l2 + imag(dc(i_d).ws);
figure('position',[0 0 800 450]); hold on;
histogram(sqrt( ([dc.ub] - real([dc.ws])).^2 + ([dc.vb] - imag([dc.ws])).^2),100,'EdgeColor','none','DisplayName','after correction')
histogram(sqrt( ([dc.ui] - real([dc.ws])).^2 + ([dc.vi] - imag([dc.ws])).^2),100,'EdgeColor','none','DisplayName','after correction')
histogram(sqrt( ([dc.u] - real([dc.ws])).^2 + ([dc.v] - imag([dc.ws])).^2),100,'EdgeColor','none','DisplayName','before correction')
f=ksdensity(abs(v_a(:)),as);
plot(as,pdfB./max(pdfB)*271200,'DisplayName','Bird','linewidth',2);
plot(as,pdfI./max(pdfI)*335700,'DisplayName','Insect','linewidth',2);
xlabel('Air speed [m/s]')
[G,day_num]=findgroups(datenum(dateshift(dc(1).time,'start','day','nearest')));
birdDENSmean=nan(numel(day_num),numel(dc));
birdDENSmean_OLD=birdDENSmean;
insectDENSmean = birdDENSmean;
birdMTRmean = birdDENSmean;
insectMTRmean = birdDENSmean;
weatherDENSmean = birdDENSmean;
birdMTRmean_OLD = birdDENSmean;
birdDIRmean=birdDENSmean;
insectDIRmean=birdDENSmean;
% propdenssum = splitapply(@nansum,dc(i_d).dens3(:).*dc(i_d).bird(:),G2(:));
% denssum = splitapply(@nansum,dc(i_d).dens3(:),G2(:));
% plot(day_num, propdenssum./denssum);
% datetick('x','keeplimits')
birdDENSmean(:,i_d) = splitapply(@nanmean,nansum(dc(i_d).dens3 .* dc(i_d).bird .* .2,2),G');
insectDENSmean(:,i_d) = splitapply(@nanmean,nansum(dc(i_d).dens3 .* dc(i_d).insect .* .2,2),G');
weatherDENSmean(:,i_d) = splitapply(@nanmean,nansum(dc(i_d).dens3 .* (1-dc(i_d).bird-dc(i_d).insect) .* .2, 2),G');
% /1000*60*60*5 :m/s -> km/hr + bird/km^3 -> bird/km^2
tmp = dc(i_d).dens3 .* dc(i_d).bird .* .2 .* sqrt(dc(i_d).ub.^2+dc(i_d).vb.^2);
birdMTRmean(:,i_d) = splitapply(@nanmean, nansum(tmp,2) /1000*60*60,G');
assert(0==nansum(tmp(isnan(dc(i_d).vb(:))&isnan(dc(i_d).ub(:)))))
V = splitapply(@nansum, tmp(:).*dc(i_d).vb(:),G2(:)) ./ splitapply(@nansum, tmp(:),G2(:));
U = splitapply(@nansum, tmp(:).*dc(i_d).ub(:),G2(:)) ./ splitapply(@nansum, tmp(:),G2(:));
birdDIRmean(:,i_d) = atan2d(V,U);
tmp = dc(i_d).dens3 .* dc(i_d).insect.* .2 .* sqrt(dc(i_d).ui.^2+dc(i_d).vi.^2);
insectMTRmean(:,i_d) = splitapply(@nanmean, nansum(tmp,2) /1000*60*60,G');
assert(0==nansum(tmp(isnan(dc(i_d).vi(:))&isnan(dc(i_d).ui(:)))))
V = splitapply(@nansum, tmp(:).*dc(i_d).vi(:),G2(:)) ./ splitapply(@nansum, tmp(:),G2(:));
U = splitapply(@nansum, tmp(:).*dc(i_d).ui(:),G2(:)) ./ splitapply(@nansum, tmp(:),G2(:));
insectDIRmean(:,i_d) = atan2d(V,U);
tmp = dc(i_d).dens3 .* dc(i_d).insect.* .2 .* sqrt(dc(i_d).ui.^2+dc(i_d).vi.^2);
weatherMTRmean(:,i_d) = splitapply(@nanmean, nansum(tmp,2) /1000*60*60,G');
tmp(sqrt(dc(i_d).u.^2 + dc(i_d).v.^2)<5)=0;
birdDENSmean_OLD(:,i_d) = splitapply(@nanmean,nansum(tmp .* .2,2),G');
birdMTRmean_OLD(:,i_d) = splitapply(@nanmean,nansum(tmp.*.2 .* sqrt(dc(i_d).u.^2+dc(i_d).v.^2),2)/1000*60*60 ,G');
figure('position',[0 0 1000 800]);
gr = repelem(1:1000,gr_day); gr=gr(1:length(day_num));
subplot(5,1,[1 3]);hold on;
b=bar(day_num(1:gr_day:end), splitapply(@nanmean,[nanmean(birdDENSmean,2) nanmean(insectDENSmean,2) nanmean(weatherDENSmean,2)],gr')./11,1,'stacked');
b(1).FaceColor = [162 29 49]/255; b(1).EdgeAlpha=0;
b(2).FaceColor = [127 47 141]/255; b(2).EdgeAlpha=0;
b(3).FaceColor = [0.9290 0.6940 0.1250]; b(3).EdgeAlpha=0;
b2=bar(day_num(1:gr_day:end),splitapply(@nanmean,nanmean(birdDENSmean_OLD,2),gr')./11,1,'k');
b2(1).FaceAlpha =0; b2(1).LineWidth=1.5;
% stairs(splitapply(@nanmean,day_num,gr)-gr_day/2, splitapply(@nanmean,nanmean(birdDENSmean_OLD,2),gr')./11,'k');
datetick('x','keeplimits'); xlim([737104 737426]); ylabel('Reflectivity [cm^2/km^3]');
legend('Birds','Insects','Weather','Bird old'); box on; grid on; %ylim([0 1])
b2=bar(day_num(1:gr_day:end),(splitapply(@nanmean,nanmean(birdDENSmean,2),gr')-splitapply(@nanmean,nanmean(birdDENSmean_OLD,2),gr'))./11,1,'k');
b2(1).FaceAlpha =0; b2(1).LineWidth=1.5;
box on; grid on;datetick('x','keeplimits'); xlim([737104 737426]);
tt = splitapply(@nanmean,[nanmean(birdDENSmean,2) nanmean(insectDENSmean,2) nanmean(weatherDENSmean,2)],gr')./11;
tt2 = splitapply(@nanmean, nanmean(birdDENSmean_OLD,2),gr')./11;
for i=[2 3 4 5 6 8 9 10 11 12]
p=pie(tt(i,:)/nansum(tt(i,:)),{'','',''});
p(1).FaceColor = [162 29 49]/255; p(1).EdgeAlpha=0;
p(3).FaceColor = [127 47 141]/255; p(3).EdgeAlpha=0;
p(5).FaceColor = [0.9290 0.6940 0.1250]; p(5).EdgeAlpha=0;
p2 = pie(tt2(i,:)./nansum(tt(i,:)),{''});
title(num2str(round(nansum(tt(i,:)),2)))
figure('position',[0 0 1000 800]);
gr = repelem(1:1000,gr_day); gr=gr(1:length(day_num));
subplot(5,1,[1 3]);hold on;
b=bar(day_num(1:gr_day:end), splitapply(@nanmean,[nanmean(birdMTRmean,2) nanmean(insectMTRmean,2)],gr')./11,1,'stacked');
b(1).FaceColor = [162 29 49]/255; b(1).EdgeAlpha=0;
b(2).FaceColor = [127 47 141]/255; b(2).EdgeAlpha=0;
%b(3).FaceColor = [0.9290 0.6940 0.1250]; b(3).EdgeAlpha=0;
b2=bar(day_num(1:gr_day:end),splitapply(@nanmean,nanmean(birdMTRmean_OLD,2),gr')./11,1,'k');
b2(1).FaceAlpha =0; b2(1).LineWidth=1.5;
% stairs(splitapply(@nanmean,day_num,gr)-gr_day/2, splitapply(@nanmean,nanmean(birdDENSmean_OLD,2),gr')./11,'k');
datetick('x','keeplimits'); xlim([737104 737426]); ylabel('Reflectivity Traffic Rate [cm^2/km^2/hr]');
legend('Birds','Insects','Bird old'); box on; grid on; %ylim([0 1])
b2=bar(day_num(1:gr_day:end),(splitapply(@nanmean,nanmean(birdMTRmean,2),gr')-splitapply(@nanmean,nanmean(birdMTRmean_OLD,2),gr'))./11,1,'k');
b2(1).FaceAlpha =0; b2(1).LineWidth=1.5;
box on; grid on;datetick('x','keeplimits'); xlim([737104 737426]);
tt = splitapply(@nanmean,[nanmean(birdMTRmean,2) nanmean(insectMTRmean,2)],gr')./11;
tt2 = splitapply(@nanmean, nanmean(birdMTRmean_OLD,2),gr')./11;
for i=[2 3 4 5 6 8 9 10 11 12]
p=pie(tt(i,:)/nansum(tt(i,:)),{'',''});
p(1).FaceColor = [162 29 49]/255; p(1).EdgeAlpha=0;
p(3).FaceColor = [127 47 141]/255; p(3).EdgeAlpha=0;
p2 = pie(tt2(i,:)./nansum(tt(i,:)),{''});
title(num2str(round(nansum(tt(i,:)),2)))