disp('AMMA_da_id2')


zm=-100:.1:10;
cuxx=Psi_fu(zm);
ctxx=Psi_ft(zm);
%semilogy(zm,cuxx,zm,ctxx);



zuu=zu-.7;
zqq=zq;


zetx=zuu./dt(:,17);
usb=dt(:,18);
tsb=dt(:,19);
qsb=dt(:,20);
hsbb=dt(:,11);
hlbb=dt(:,12);
taubb=dt(:,13);
cdn10bb=dt(:,29);
Lb=dt(:,17);
tsbs=tsb+.51*(ta+273.16).*qsb;

wtv=-usb.*(tsb+.61.*(ta+273.16).*qsb);

ws=(9.83./(ta+273.16).*abs(wtv)*600).^.333;%convective velocity
ug=sqrt(usb.^2+ws.^2);
ugw=sqrt(1.5*usb.^2+ws.^2);
ugu=sqrt(8*usb.^2+1.*ws.^2);
gg=sqrt(1+1.6*ws.^2./U.^2);
%jj=find((reldir<90 | reldir>270) & sig_h<5 & org<5 & sqrt(ww)./ug<.55+.003*U.^2 & sqrt(vv)./ug<1.1+0.05*U );

% isledist=sqrt((lat+.527).^2+(lon-166.935).^2)*100;%dist to nauru in km

jj=find((reldir<90 | reldir>270) & sig_h<8 & sig_u<2 &sp2<2  & org<5 & sqrt(ww)./ugw<.7+.0015*U.^2 & sqrt(vv)./ugu<1.2); % & isledist>3.5 );
%figure;semilogy(zetx(jj),cu(jj)./usb(jj).^2*zuu^.667,'.',zm,cuxx);axis([-50 1 1 1000]);
%figure;semilogy(zetx(jj),.75*cw(jj)./usb(jj).^2*zuu^.667,'.',zm,cuxx);axis([-50 1 1 1000]);
%figure;semilogy(zetx(jj),cs(jj)./usb(jj).^2*zuu^.667,'.',zm,cuxx);axis([-50 1 1 1000]);
jplume=ones(length(jdy),1);
ii=find(sqrt(ww)./ugw<.65+.002*U.^2 & sqrt(vv)./ugu<1.1);
jplume(ii)=0;
jmanuv=3*ones(length(jdy),1);
ij=find(sig_h<8 & sig_u<2 & sp2<2);
jmanuv(ij)=sig_u(ij);


zzl=[-60 -30 -20 -8 -5 -4 -3 -2 -1 -.7 -.5 -.3 -.2 -.1 -.05 0 .05 .1 1];
nn=length(zzl)-1;
clear zetxm csm cnt;
for i=1:nn
   ii=find(zetx(jj)>zzl(i) & zetx(jj)<zzl(i+1) & isfinite(cs(jj)));
   if isfinite(jj(ii));
   	zetxm(i)=median(zetx(jj(ii)));
      csm(i)=median(cs(jj(ii))./usb(jj(ii)).^2*zu^.667);
      cnt(i)=length(jj(ii));
	else;
      zetxm(i)=NaN;
      csm(i)=NaN;
      cnt(i)=NaN;
   end;
end;
figure;semilogy(zetxm,csm,'O',zm,cuxx);axis([-15 1 1 100]);%axis([-1 1 1 10]);
figure;plot(zetxm,csm./Psi_fu(zetxm),'.');%axis([-30 1 .8 1.2]);

u10n=U-usb/.4.*(log(zuu/10)-psiu_30(zuu./Lb));
usib=sqrt(cs.*zuu^.667./psi_fu(zetx));
tsibs=-sqrt(ct.*zuu^.667./psi_ft(zetx)).*sign(wtv);
%qsib_oph=sqrt(cq.*zqq^.667./psi_ft(zetx))*1e-3.*sign(qsb);%convert to g/g
qsib_lic=sqrt(cql.*zqq^.667./psi_ft(zetx))*1e-3.*sign(qsb);%convert to g/g

%ii=find(qsib_oph<-1e-3 | qsib_oph>1e-3);qsib_oph(ii)=NaN;
ii=find(qsib_lic<-1e-3 | qsib_lic>1e-3);qsib_lic(ii)=NaN;
tsib=tsibs-.51*(ta+273.16).*qsb;

figure;plot(u10n(jj),(usib(jj)./u10n(jj)).^2,'.',u10n(jj),cdn10bb(jj),'.');axis([0 20 0 3e-3]);
xlabel('U_{10n}');ylabel('ID C_{10n}');title([jdollars ' 10-min']);
hsib=-rhoair*cpa.*(tsib).*usib;
bfbb=-rhoair*cpa.*(tsb+0.51*(ta+tdk).*qsb).*usb;
bfib=-rhoair*cpa.*(tsibs).*usib;
figure;plot(hsbb(jj),hsib(jj),'.',[-10 100],[-10 100]);axis([-10 100 -10 100]);
xlabel('H_s bulk (W/m^2))');ylabel('H_s ID (W/m^2))');title([jdollars ' 10-min']);

hsc=cpa*rhoair.*wt;

%hlib_oph=-rhoair*Le.*(qsib_oph).*usib;
hlib_lic=-rhoair*Le.*(qsib_lic).*usib;

%ii=find(sgq_lic<.05 | sgq_lic>.9 | hlib_lic<-200);hlib_lic(ii)=NaN;
%kk=find(siglic(jj)<50 & ophm(jj)>1000 & isfinite(hlib_oph(jj)));

%figure;plot(hlbb(jj(kk)),hlib_oph(jj(kk))+hl_webb(jj(kk)),'.',[-20 300],[-20 300]);axis([-20 300 -20 300]);
%xlabel('Bulk Hl (W/m^2)');ylabel('Turb Hlib (W/m^2)');
%hlc=Le*rhoair.*wq/1000*1.07*1.045;%7% ophir mean cal, 4.5% displacement
figure;plot(hlbb(jj),hlc(jj)+hl_webb(jj),'.',[-20 300],[-20 300]);axis([-20 300 -20 300]);
xlabel('Bulk Hl (W/m^2)');ylabel('Turb Hlc (W/m^2)');
hlm=(hlc+hlib_lic)*.5;
figure;plot(hlbb(jj),hlm(jj)+hl_webb(jj),'.',[-20 300],[-20 300]);axis([-20 300 -20 300]);
xlabel('Bulk Hl (W/m^2)');ylabel('Turb Hl (W/m^2)');


clear hlmm hlbm nn dqm uu
uu=0:20;
for i=1:21
   ii=find(floor(U)==uu(i));
   jj=find(ushp(ii)<7 & (reldir(ii) <90 | reldir (ii) >270)& isfinite(hlm(ii)) & sig_h(ii)<5 & org(ii)<5 & sqrt(ww(ii))./ug(ii)<.5+.003*U(ii).^2 & sqrt(vv(ii))./ug(ii)<1.1+0.05*U(ii));
   if isempty(jj)
      hlmm(i)=NaN;
      hlbmb(i)=NaN;
      hlbm(i)=NaN;
      nn(i)=0;
      dqm(i)=NaN;
   else
   	hlmm(i)=median(hlm(ii(jj))+hl_webb(ii(jj)));
   	hlbmb(i)=median(Hlb(ii(jj))+hl_webb(ii(jj)));
   	hlbm(i)=median(hlbb(ii(jj)));
   	nn(i)=length(jj);
   	dqm(i)=median(qse(ii(jj))-qa(ii(jj)))-1e3*mean(dt(ii(jj),22));%
   end;
end;

figure;plot(uu,hlbm,'g',uu,hlmm,'b');xlabel('U (m/s)');ylabel('Hl (W/m^2)');title('hlb median in green, [(hlc+hlib)*.5 + hl_ webb] median in blue');
figure;plot(hlbm,hlmm,'o',[0 200],[0 200]);xlabel('Bulk Hl (W/m^2)');ylabel('Turb Hl (W/m^2)');axis([0 200 0 200]);
jj=find((reldir<90 | reldir>270) & sig_h<8 &sig_u<2 &sp2<2  & org<5 & sqrt(ww)./ugw<.55+.002*U.^2 & sqrt(vv)./ugu<1.1 & isfinite(usib)); % & isledist>3.5 );
kk=find(abs(-wu(jj)-usib(jj).^2)<.02+.006*U(jj));
figure;plot(jdy(jj),-wu(jj),'.',jdy(jj),usib(jj).^2,[stdt endt],[0 0]);axis([stdt endt -.05 .35]);
xlabel('Julian Day(2008)');ylabel('u_*^2(m^2/s^2)');title([jdollars 'filtered pass 1: ID and Cov']);

figure;plot(jdy(jj(kk)),-wu(jj(kk)),'.',jdy(jj),usib(jj).^2,[stdt endt],[0 0]);axis([stdt endt -.05 .35]);
xlabel('Julian Day(2008)');ylabel('u_*^2(m^2/s^2)');title([jdollars ' filtered pass 2: ID and Cov']);

%figure;plot(jdy(jj(kk)),sign(-wu(jj(kk))).*sqrt(abs(wu(jj(kk)))),'.',jdy(jj),usib(jj),[stdt endt],[0 0]);
%xlabel('Julian Day(1999)');ylabel('u_*(m/s)');title([jdollars ' filtered pass 2: ID and Cov']);

%kk=find(sigoph(jj)<50 & ophm(jj)>1000 );

figure;semilogy(zetx(jj),cql(jj)./(1e3*qsb(jj)).^2.*zuu^.67,'.',zetx(jj),psi_ft(zetx(jj)),'.');axis([-10 2 0.01 5e2])
xlabel('z/L');ylabel('Dimensionless C_q^2');
figure;semilogy(zetx(jj),ct(jj)./(tsbs(jj)).^2.*zuu^.67,'.',zetx(jj),psi_ft(zetx(jj)),'.');axis([-10 2 0.01 5e2])
xlabel('z/L');ylabel('Dimensionless C_t^2');

%figure;semilogy(zetx(jj),sqrt(qq(jj))./abs(1e3*qsb(jj)),'.',zetx(jj),phi_tt(zetx(jj)),'.');axis([-10 2 0.1 5e1])
%xlabel('z/L');ylabel('Dimensionless \sigma_q');
%figure;semilogy(zetx(jj),sqrt(tt(jj))./abs(tsbs(jj)),'.',zetx(jj),phi_tt(zetx(jj)),'.');axis([-10 2 0.1 5e1])
%xlabel('z/L');ylabel('Dimensionless \sigma_t');

%figure;semilogy(zetx(jj(kk)),sqrt(qq(jj(kk)))./abs(1e3*qsb(jj(kk)))./(sqrt(tt(jj(kk)))./abs(tsbs(jj(kk)))),'.',[-10 2],[.85 .85]);axis([-10 2 0.1 1e1]);
%xlabel('z/L');ylabel('Dimensionless \sigma_q/ \sigma_t');

% ####test ludo######
figure;plot(usb(jj),usib(jj),'.',[0 0.7],[0 0.7]);axis([0 0.7 0 0.7]);xlabel('usb');ylabel('usib')
figure;plot(-wu(jj),usb(jj).^2,'.',[0 0.4],[0 0.4]);xlabel('-wu Hare');ylabel('usb^2');title([' filtered pass 1']);axis([-0.05 0.4 0 0.4]);
figure;plot(-wu(jj(kk)),usb(jj(kk)).^2,'.',[0 0.4],[0 0.4]);xlabel('-wu Hare');ylabel('usb^2');title([' filtered pass 2']);axis([-0.05 0.4 0 0.4]);
figure;plot(-wuj(jj),usb(jj).^2,'.',[0 0.4],[0 0.4]);xlabel('-wu Edson');ylabel('usb^2');title([' filtered pass 1']);axis([-0.05 0.4 0 0.4]);
figure;plot(-wuj(jj(kk)),usb(jj(kk)).^2,'.',[0 0.4],[0 0.4]);xlabel('-wu Edson');ylabel('usb^2');title([' filtered pass 2']);axis([-0.05 0.4 0 0.4]);

figure;plot(U(jj),usib(jj),'.',U(jj),usb(jj),'r.');xlabel('U');title([' usb in red, usib in blue']);
figure;plot(usb(jj),sqrt(abs(wu(jj))),'.',[0 0.7],[0 0.7]);axis([0 0.7 0 0.7]);xlabel('usb');ylabel('sqrt(-wu) Hare')
figure;plot(usb(jj),sqrt(abs(wuj(jj))),'.',[0 0.7],[0 0.7]);axis([0 0.7 0 0.7]);xlabel('usb');ylabel('sqrt(-wu) Edson')
