%Look at 10-min fluxes from CO2 instruments during HiWinGS2013
datasetv='_v1';
decorr=0;
dcname=[];
if decorr==1
    dcnames='_vCO2decorr1';  %means CO2 is decorrelated WRT motion
    dcname='_vdecorr';  %means W wsa decorrelated as well
%     dcname='_vCO2decorr_noWebbon7500mast';
end;
% CO2view=load(['C:\Data\CRUISES\HiWinGS_2013\7200\flux\HiWinGS_2013_CO2flux' datasetv dcname dcnames '.prn']);
CO2view=load(['C:\Data\CRUISES\HiWinGS_2013\7200\flux\HiWinGS_2013_CO2flux_UH_v1_vCO2decorr1.prn']);
% CO2caloffset=4.5;
CO2_doy=CO2view(:,1);
wCO2_7200PSD=CO2view(:,2);
wH2O_7200PSD=CO2view(:,3);
wCO2_7500PSD=CO2view(:,4);
wH2O_7500PSD=CO2view(:,5);
slopeCO2_7200PSD=CO2view(:,6:7)*600; %converts to ppb/10min slope
stdCO2_7200PSD=CO2view(:,8);
meanCO2_7200PSD=CO2view(:,9);
slopeH2O_7200PSD=CO2view(:,10:11)*600; %converts to ppb/10min slope
stdH2O_7200PSD=CO2view(:,12);
meanH2O_7200PSD=CO2view(:,13);
slopeCO2_7500PSD=CO2view(:,14:15)*600; %converts to ppb/10min slope
stdCO2_7500PSD=CO2view(:,16);
meanCO2_7500PSD=CO2view(:,17);
slopeH2O_7500PSD=CO2view(:,18:19)*600; %converts to ppb/10min slope
stdH2O_7500PSD=CO2view(:,20);
meanH2O_7500PSD=CO2view(:,21);
LI7200_AGC_PSD_10m=CO2view(:,22);
LI7200_AGC_PSD_10s=CO2view(:,23);
LI7500_AGC_PSD_10m=CO2view(:,24);
LI7500_AGC_PSD_10s=CO2view(:,25);
lLicor_CO2=CO2view(:,26);
lLI7200_CO2mf=CO2view(:,27);

wCO2PKT_7500PSD=CO2view(:,28); 
rhoc7500PSDPKT=CO2view(:,29); 
wCO2PKT_7200PSD=CO2view(:,30); 
rhoc7200PSDPKT=CO2view(:,31); 
wCO2JE0_7500PSD=CO2view(:,32); 
wCO2JE1_7500PSD=CO2view(:,33); 
gamPSDJE_7500PSD=CO2view(:,34); 
gamPKTJE_7500PSD=CO2view(:,35); 
wCO2JE0_7200PSD=CO2view(:,36); 
wCO2JE1_7200PSD=CO2view(:,37); 
gamPSDJE_7200PSD=CO2view(:,38); 
gamPKTJE_7200PSD=CO2view(:,39); 

slopeCO2d_7200PSD=CO2view(:,40:41); 
stdCO2d_7200PSD=CO2view(:,42); 
meanCO2d_7200PSD=CO2view(:,43); 
wCO2d_7200PSD=CO2view(:,44); 
wCO2PKTlb_7500PSD=CO2view(:,45); 
rhoc7500PSDPKTlb=CO2view(:,46); 
wCO2PKTlb_7200PSD=CO2view(:,47); 
rhoc7200PSDPKTlb=CO2view(:,48); 

uCO2d_7200PSD=CO2view(:,49); 
vCO2d_7200PSD=CO2view(:,50); 
uCO2_7500PSD=CO2view(:,51); 
vCO2_7500PSD=CO2view(:,52); 
slopeCO2_7200PSDrecomp=CO2view(:,53:54)*600; 
stdCO2_7200PSDrecomp=CO2view(:,55); 
meanCO2_7200PSDrecomp=CO2view(:,56); 
slopeH2O_7200PSDrecomp=CO2view(:,57:58); 
stdH2O_7200PSDrecomp=CO2view(:,59); 
meanH2O_7200PSDrecomp=CO2view(:,60); 
wCO2_7200PSDrecomp=CO2view(:,61); 
wH2O_7200PSDrecomp=CO2view(:,62); 

muco2h2o_7500PSD=CO2view(:,63); 
muco2h2o_7200PSD=CO2view(:,64); 
wCO2H2O_7500PSD=CO2view(:,65); 
wCO2H2O_7200PSD=CO2view(:,66); 
rqc_7200PSD=CO2view(:,67); 
rqc_7500PSD=CO2view(:,68); 
 wCO2H2O_7200PSD2=CO2view(:,69); 

wCO2d_7200PSDi=CO2view(:,70); 
wCO2d_7200PSDichop=CO2view(:,71); 


figure;plot(CO2_doy,wCO2d_7200PSD,CO2_doy,wCO2_7500PSD,'k.');grid; legend('7200 PSD','7500 PSD');ylim([-.2 .2])

figure;plot(CO2_doy,wCO2d_7200PSD,CO2_doy,wCO2_7500PSD,'r.--',CO2_doy,wCO2PKT_7500PSD,'m.--',CO2_doy,wCO2JE1_7500PSD,'g.--',CO2_doy,wCO2PKTlb_7500PSD,'c.--');grid; legend('PSD','7500PSD','7500PSD PKT','7500PSD JEdecorr','7500PSD PKTlb');ylim([-.2 .2])

figure;plot(CO2_doy,meanCO2_7500PSD,'r',CO2_doy,meanCO2d_7200PSD,'g',CO2_doy,rhoc7200PSDPKT,'m.');grid;ylim([300 500])


% wCO2d_7200PSD=wCO2d_7200PSDichop;

% %12/06/2012 - look at optical cell transfer function of 7200
% Fnotch=nanmedian(Flow7200_UC_10m)/60000/1.609e-5; % first 0 notch of 7200 cell transfer function
% Fnotchf=Fnotch./pi; %scale sinc to reflect notch
% x =0:0.1:20;figure;plot(x, abs(sin(x./Fnotchf) ./ (x./Fnotchf)),'+--' );grid
% hold on;plot([Fnotch Fnotch],[0 1],'r')


[c, ia, ib] = intersect(jdy,CO2_doy);  %indexes common to both AB and AC
%QC criteria
jj=find((reldir(ia)>270 | reldir(ia)<90) & sig_h(ia)<5 & sig_u(ia)<1.4 &sp2(ia)<1.2 & org(ia)<5 & sqrt(ww(ia))./ugw(ia)<2+.0015*U(ia).^2 & sqrt(vv(ia))./ugu(ia)<2);
SteadyStateCO2criteria;
% correct for attenuation in tube %%
[wCO2_7200PSDbuz,r_buz]=BuzoriusCorrection(wCO2d_7200PSD(ib),urel(ia),0.4);
% wCO2_7200UCbuz=wCO2_7200UC./r_buz;
% wCO2_7200PSDbuz(wCO2_7200PSDbuz>0.15)=NaN;

figure(1);plot(CO2_doy(jj((ii7200CO2PSD))),wCO2d_7200PSD(jj((ii7200CO2PSD))),CO2_doy(jj((ii7200CO2PSD))),wCO2_7200PSDbuz(jj((ii7200CO2PSD))),'r');grid; legend('wco27200','wco27200 after buz');ylim([-.05 .05])

% compare various instruments
% figure(2);plot(CO2_doy,wCO2_7200PSDbuz,'g',CO2_doy,wCO2_7200UC,'r',CO2_doy,wCO2_7200UCwebb,'m',CO2_doy,wCO2_7200UCcov,'b');grid; legend('7200 PSD','7200 UC dry','7200 UC webb','7200 UC dry cov');ylim([-.2 .2])
figure(2);plot(CO2_doy(jj),wCO2_7500PSD(jj),'b.',CO2_doy(jj),wCO2_7200PSDbuz(jj),'g');grid; legend('7500 PSD','7200 PSD dry');ylim([-.05 .05])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %loas pCO2 - 1min dataset
% % filecat('D:\Desktop\Data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\HiWinGS_2013_proc_dpco2_*.txt','HiWinGS_2013_proc_dpco2_all.txt')
% pcodat=load(['C:\Data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\HiWinGS_2013_proc_dpco2_all.txt']);
% jdypco=pcodat(:,1);
% pCO2a=pcodat(:,2); 
% pCO2w=pcodat(:,3); 
% dpCO2=-pcodat(:,4); dpCO2(dpCO2<-80 | dpCO2>0)=NaN;
% figure;plotyy(CO2_doy(jj(ii7200CO2PSD)),wCO2_7200PSDbuz(jj(ii7200CO2PSD)),jdypco,dpCO2)
% %clean pCO2 data
% oo=find(jdypco>284+18/24 & jdypco<284+22/24); dpCO2(oo)=NaN; pCO2a(oo)=NaN; pCO2w(oo)=NaN;
% oo=find(dpCO2<-70); dpCO2(oo)=NaN; pCO2a(oo)=NaN; pCO2w(oo)=NaN;
% pCO2w(pCO2w>380 | pCO2w<314)=NaN;

pcodat=importdata(['C:\data\CRUISES\HiWinGS_2013\7200\flux\KN213-03_HiWinGS_uwpco2_final.csv']);
jdypco= pcodat.data(:,18);
dpCO2=pcodat.data(:,13);
pCO2a=pcodat.data(:,12);
pCO2w=pcodat.data(:,11);

figure;plot(CO2_doy(jj(ii7200CO2PSD)),meanCO2d_7200PSD(jj(ii7200CO2PSD)))
hold on;plot(jdypco,pCO2a,'r',jdypco,pCO2w,'g.')


% deltapco2=dpCO2(~isnan(dpCO2));
% jdeltapco2=jdypco(~isnan(dpCO2));
% deltapco2f=intavg(jdeltapco2,deltapco2,jdy);
deltapco2f=intavg(jdypco,dpCO2,jdy);

% %compares with BB
% flist=fopen('D:\Desktop\Data\CRUISES\HiWinGS_2013\Knorr\pCO2 Data\2013 11 02 fCO2 Data.txt');
% temp=textscan(flist,'%2f/%f/%f %2f:%2f:%2f %f %f %f %*[^\n]','delimiter', ' ', 'headerlines', 1, 'emptyvalue', NaN);
% dpCO2BB=cell2mat(temp);
% jdy_dpCO2BB=datenum(2000+dpCO2BB(:,3),dpCO2BB(:,1),dpCO2BB(:,2),dpCO2BB(:,4),dpCO2BB(:,5),dpCO2BB(:,6))-datenum(2013,1,0);
% 
% figure;plot(jdy,deltapco2f,jdy_dpCO2BB,dpCO2BB(:,9),'g')
% figure;plot(jdypco,pCO2a,jdy_dpCO2BB,dpCO2BB(:,8),'g')
% figure;plot(jdypco,pCO2w,jdy_dpCO2BB,dpCO2BB(:,7),'g')
% 
% deltapco2f=intavg(jdy_dpCO2BB,dpCO2BB(:,9),jdy);



% 
% figure;plot(CO2_doy(jj),(wCO2_7200PSDbuz(jj)*1e-6).*(1.25e3/28.97).*365.24.*24.*60.*60);ylabel('molC/m2/yr')
% figure;plot(CO2_doy(jj),(wCO2_7200UC(jj)*1e-6).*(1.25e3*44/28.97).*365.24.*24.*60.*60*1e-3,'r',CO2_doy(jj),(wCO2_7200PSDbuz(jj)*1e-6).*(1.25e3*44/28.97).*365.24.*24.*60.*60*1e-3);ylabel('g/m2/yr')
% disp('mol/m2/yr'); [nanmedian((wCO2_7200PSDbuz(jj)*1e-6).*(1.25e3/28.97).*365.24.*24.*60.*60) nanmedian((wCO2_7200UC(jj)*1e-6).*(1.25e3/28.97).*365.24.*24.*60.*60)]

co2taka=(wCO2_7200PSDbuz(jj)*1e-6).*(1.25e3*44/28.97).*365.24.*24.*60.*60*1e-3;
co2taka(co2taka<-2)=NaN;
[nanmedian(co2taka) nanstd(co2taka)]
% figure;plot(CO2_doy(jj),wCO2_7200UC(jj).*(1.25/28.97).*365.24.*24.*60.*60.*1e-3);
% .*28.96.*365.24.*24.*60.*60.*1e-6;  %units are mol/m2/year
%%%%add COARE
soludo=solco2(Ts); %y=[sol al sc];
ug=dt(:,32);von  = 0.4; zu=18; %zu=zu_etl(1);
usb=dt(:,18);L=dt(:,17);
U10n=U- (usb./(von*(sqrt((1+ug./U).^2)))).*(log(zu./10)-psiu_26(zu./L));

kpsd=wCO2_7200PSDbuz./(soludo(ia,2).*deltapco2f(ia));  %k in m/s
% figure;plot(CO2_doy(jj(ii7200CO2PSD)),kpsd(jj(ii7200CO2PSD))*360000,'g')
k660psd=kpsd.*(660./soludo(ia,3)).^(-.5);  %k in m/s
co2_xfer_plt4a
figure;plot(u,100*vto*3600','-o',u,100*vt*3600,'-d',u,0.31*u.^2);
ylabel('k (cm/hr)');xlabel('U_{10} (m/s)');
legend('ocean xfer','total xfer','0.31*u.^2')
hold on;plot(U10n(ia(jj(ii7200CO2PSD))),100*kpsd(jj(ii7200CO2PSD))*3600,'g.');grid



% figure;plot(CO2_doy,wCO2_7200PSD./(usb(ia).*deltapco2f(ia)),'.--')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%averaged by wind spd bins
X=U10n(ia(jj(ii7200CO2PSD)));
kpsd1=100*kpsd(jj(ii7200CO2PSD))*3600;
k660psd1=100*k660psd(jj(ii7200CO2PSD))*3600;
% Hsleg31=Hsleg3(jj(iiCO2PSD));
clear kpsdm kpsdmx nm dum k660psdm k660psdmx 
uu=0:28;
for i=1:(length(uu)-1)
  ii=find(X>=uu(i) & X<uu(i+1));
%   Hsleg3m(i)=nanmedian(Hsleg31(ii));
  kpsdm(i)=nanmedian(kpsd1(ii)); kpsdstd(i)=nanstd(kpsd1(ii));kpsdmx(i)=nanmean(kpsd1(ii)); 
  k660psdm(i)=nanmedian(k660psd1(ii)); k660psdstd(i)=nanstd(k660psd1(ii));k660psdmx(i)=nanmean(k660psd1(ii)); 
%   kucm(i)=nanmedian(kuc1(ii));   kucstd(i)=nanstd(kuc1(ii));
  nm(i)=length(ii);
  dum(i)=nanmedian(X(ii));%
end;
ii=find(nm>5);
figure;errorbar(dum(ii),kpsdm(ii),kpsdstd(ii)./sqrt(nm(ii)),'b^','MarkerSize',10,'LineWidth',2);
hold on;plot(dum(ii),kpsdmx(ii),'bo');
hold on;plot(u,0.093*u.^3-1.3*u.^2+5.8*u-2.9,'b--','MarkerSize',5,'LineWidth',1.5);
ylabel('k (cm/hr)');xlabel('U_{10N} (m/s)');
% legend('ocean xfer','total xfer','0.31*u.^2')
% hold on;errorbar(dum,kpsdm,kpsdstd./nm,'g^-','MarkerSize',10,'LineWidth',2);
% hold on;plot(u,100*vt*3600,'-rd',u,0.312*u.^2,'k--',u,100*vto*3600','-go',u,4.3+0.0315*u.^3,'m--','MarkerSize',5,'LineWidth',1.5);
hold on;plot(u,100*vt*3600,'-rd',u,0.312*u.^2,'k--',u,100*vto*3600','-go','MarkerSize',5,'LineWidth',1.5);
% hold on;errorbar(dum,kucm,kucstd,'y^-','MarkerSize',10,'LineWidth',2);
grid;axis([0 26 -20 200])
% legend('measured (median)','measured (mean)','interfacial and breaking wave processes','0.31*u.^2','whitecap/bubble algorithm turned off (kb=0)')
hold on;errorbar(dum(ii),k660psdm(ii),k660psdstd(ii)./sqrt(nm(ii)),'c^');
KWnM=0.312*U.^2;
KMetal=4.3+0.0315*U.^3;
legend('measured (median +/- std error)','measured (mean)','cubic fit (0.093*u^3-1.3*u^2+5.8*u-2.9)','cor30_-kx_-co2a','0.31*u.^2','whitecap/bubble algorithm turned off (kb=0)','k_{660}')




