% function [wc0,wc1,gamPSD,gamPKT,Cqc,Scc,muw,CwCO2c]=SpectralCrosstalk(CCO2H2O,SH2O,CwCO2,CwH2O,wCO2PSD,wCO2PKT,SCO2)
function [wc0,wc1,gamPSD,gamPKT,Cqc,Scc,muw,CwCO2c]=SpectralCrosstalk(CCO2H2O,SH2O,CwCO2,CwH2O,wCO2PSD,wCO2PKT,SCO2,gam0)
% function [wc0,wc1,gamPSD,gamPKT]=SpectralCrosstalk(CCO2H2O,SH2O,CwCO2,CwH2O,CwCO2PSD,wCO2PKT)
% CCO2H2O=CCO2H2O_7200UC_i;SH2O=SH2O_7200UC_i;CwCO2=CwCO2_7200UC_i;CwH2O=CwH2O_7200UC_i;wCO2PSD=wCO2_7200PSD(tt+1);wCO2PKT=wCO2PKT_7200UC(tt+1); SCO2=SCO2_7200UC_i;
% CCO2H2O=CCO2H2O_7500PSD_i;SH2O=SH2O_7500PSD_i;CwCO2=CwCO2_7500PSD_i;CwH2O=CwH2O_7500PSD_i;wCO2PSD=wCO2_7200PSD(tt+1);wCO2PKT=wCO2PKT_7500PSD(tt+1); SCO2=SCO2_7500PSD_i;gam0=1.15;

[Cc,Fc0,dfc]=specsmooludot([CwCO2],10);
wc0=real(nansum(Cc.*dfc));


plotit=0;
% gam0=0.88;
muw=gam0*(CCO2H2O)./(SH2O);
[CwCO2c,Fc1,dfc]=specsmooludot([CwCO2-muw.*CwH2O],10);
wc1=real(nansum(CwCO2c.*dfc));
[CwCO2c,Fc1]=specsmoo([CwCO2-muw.*CwH2O],10);

if plotit
    [Cmuw,Fmuw]=specsmoo(muw.*CwH2O,10);
    figure(1);semilogx(Fpic(:,tt+1),Fpic(:,tt+1).*CwCO2_7500PSD(:,tt+1),'r',Fmuw,Fmuw.*Cmuw,'b');
    hold on; semilogx(Fc0,Fc0.*Cc,'g',Fpic(:,tt+1),Fpic(:,tt+1).*CwCO2_7200PSD(:,tt+1),'k');
    legend('7500UCwr_{cm}','7500PSDmu*wr_{v}','7500PSDwr_{c}','7200PSDwr_{cm}')
    % legend('7500UCwr_{cm}','7500UCwr_{c}','7200PSDwr_{cm}')
    grid
end

% gamw=-5:.01:5;wCO2_mu=ones(1,length(gamw))+NaN;
% gamw=-1:.01:2;wCO2_mu=ones(1,length(gamw))+NaN;
gamw=0:.01:2;wCO2_mu=ones(1,length(gamw))+NaN;
for ll=1:length(gamw);
    [Cc,Fc,dfc]=specsmooludot([CwCO2-[gamw(ll)*(CCO2H2O)./(SH2O)].*CwH2O],10);       wCO2_mu(ll)=real(nansum(Cc.*dfc));
end;
if plotit
    figure(2);plot(gamw,wCO2_mu,'b+--',[gamw(1) gamw(end)],[wCO2_7200PSD(tt+1) wCO2_7200PSD(tt+1)],'g--',[gamw(1) gamw(end)],[wCO2PKT_7500UC(tt+1) wCO2PKT_7500UC(tt+1)],'r--');grid
end    
if ~isnan(wCO2PSD)
    gamPSD=gamw(abs(wCO2_mu-wCO2PSD)==min(abs(wCO2_mu-wCO2PSD)));
else   
       gamPSD=-999;
end
if ~isnan(wCO2PKT)
    gamPKT=gamw(abs(wCO2_mu-wCO2PKT)==min(abs(wCO2_mu-wCO2PKT)));
else
   gamPKT=-999;
end

if length(gamPSD)>1
   gamPSD=-999;
end
if length(gamPKT)>1
   gamPKT=-999;
end
% [Cc,Fc,dfc]=specsmooludot([CwCO2PSD],10);
% wCO2PSD=real(nansum(Cc.*dfc));
% gamPSD=gamw(abs(wCO2_mu-wCO2PSD)==min(abs(wCO2_mu-wCO2PSD)));

% % %uses gamPSD to match it to PSD fluxes
% % muw=gamPSD*(CCO2H2O)./(SH2O);
% % [CwCO2c,Fc,dfc]=specsmooludot([CwCO2-muw.*CwH2O],10);
% % wc1=real(nansum(CwCO2c.*dfc));
% % [CwCO2c,Fc]=specsmoo([CwCO2-muw.*CwH2O],10);




% [Cc,Fc,dfc]=specsmooludot([CwCO2-muw.*CwH2O],10);
% wc7500UC1=real(nansum(Cc.*dfc));
% [wc0 wc7500UC1]
% 
if plotit
    figure(1);hold on; semilogx(Fc,Fc.*Cc,'m');
    grid
end



%%%adds the computation of Cqc and Scc
Cqc=CCO2H2O-muw.*SH2O;
Scc=SCO2-muw.*[CCO2H2O+Cqc];

[Cqc,Fc]=specsmoo(Cqc,10);Cqc=Cqc';
[Scc,Fc]=specsmoo(Scc,10);Scc=Scc';
[muw,Fc]=specsmoo(muw,10);muw=muw';
%     figure;semilogx(Fpics_i,Fpics_i.*CCO2H2O_7200UC(:,tt+1)',Fc,Fc.*Cqc,'r')
%  figure;loglog(Fpics_i,SCO2_7200UC(:,tt+1)',Fc,(Scc),'r')

% [Cc,Fc,dfc]=specsmooludot(Scc,10);
% C1=real(nansum(Cc.*dfc));
