disp('metz_med_hl_plt7');
%ysigoph=y1(:,133)';
clear hlmm hlbm nn dqm hlmmx hlbmx dum uu gfm gfmx dqmx flx jam jamx sig taumm tauim hs0 dthm hl_webarm hl_nulmm
uu(1:13)=0:12;
uu(14:16)=14:2:18;uu(17)=24;

for i=1:16
   ii=find(v1>=uu(i) & v1<uu(i+1));
   jj=find(isfinite(hll(ii)) & (reldir(ii)>300 | reldir(ii)<60)  & isfinite(dq10n(ii)) & abs(dq10n(ii))>1 & org(ii)<1 & sog(ii)<5 & xco2(ii)>340);
   %jj=find(isfinite(taucx(ii)) & (reldir(ii)>300 | reldir(ii)<60) );%velocity data

   if isempty(jj)
      hlmm(i)=NaN;
      hlbm(i)=NaN;
      hlmmx(i)=NaN;
      hlbmx(i)=NaN;
      nn(i)=0;
      dqm(i)=NaN;
      adqm(i)=NaN;
      dqmx(i)=NaN;
      dum(i)=NaN;
      gfm(i)=NaN;
      gfmx(i)=NaN;
      jam(i)=NaN;
      hlnrm(i)=NaN;
      cen10m(i)=NaN;
      hlnrmx(i)=NaN;
      cen10mx(i)=NaN;
      taumm(i)=NaN;
      tauim(i)=NaN;
      gravm(i)=NaN;
      sig(i)=NaN;
      sigu(i)=NaN;
      hs0(i)=NaN;
      ns0(i)=0;
      dthm(i)=NaN;
        hl_webarmm(i)=NaN;
            hl_nullmm(i)=NaN;
   else
   	  hlmm(i)=nanmedian(hll(ii(jj))+hl_webar(ii(jj))'-hlcn(ii(jj)));
      hlbm(i)=nanmedian(yhlb(ii(jj)));
      nn(i)=length(jj);
      dthm(i)=median(dth10n(ii(jj)));%
      dqm(i)=median(dq10n(ii(jj)).*rhoair(ii(jj)));%
      adqm(i)=median(abs(dq10n(ii(jj)).*rhoair(ii(jj))));%
      dqmx(i)=mean(dq10n(ii(jj)).*rhoair(ii(jj)));%
      hlmmx(i)=mean(hll(ii(jj))+hl_webar(ii(jj))'-hlcn(ii(jj)));
      hl_webarmm(i)=median(hl_webar(ii(jj))');
      hl_nullmm(i)=nanmedian(hlcn(ii(jj)));
   	  hlbmx(i)=mean(yhlb(ii(jj)));
      dum(i)=mean(v1(ii(jj)));
      gfm(i)=median(gf(ii(jj)));
      gfmx(i)=mean(gf(ii(jj)));
      clear flx;
      flx=[(hll(ii(jj))+hl_webar(ii(jj))')' ];
      flxx=sortrows(flx');
      nx=length(flxx);
      jlft=floor(max(.15*nx,1));jrgt=floor(max(.85*nx,1));sig(i)=(flxx(jrgt)-flxx(jlft))/2;
      jam(i)=median(flx);
      jamx(i)=mean(flx);
      hlnrm(i)=median((hll(ii(jj))+hl_webar(ii(jj))')./dq10n(ii(jj))./rhoair(ii(jj))./u10n(ii(jj))./gf(ii(jj)));
      cen10m(i)=median(cen10(ii(jj)));
      hlnrmx(i)=mean((hll(ii(jj))+hl_webar(ii(jj))')./dq10n(ii(jj))./rhoair(ii(jj))./u10n(ii(jj))./gf(ii(jj)));
      gravm(i)=mean(grav(ii(jj)));
      cen10mx(i)=mean(cen10(ii(jj)));
      taumm(i)=median(taucx(ii(jj))./rhoair(ii(jj)));
      tauim(i)=median(tauib(ii(jj))./rhoair(ii(jj)));
      imx=[];imx=find((dts(ii(jj)))>-1);
      if isempty(imx)
          hs0(i)=NaN;
          ns0(i)=0;
      else
          hs0(i)=.5*nanmedian(hsc(ii(jj(imx)))+hsib(ii(jj(imx))));
          ns0(i)=length(imx);
      end;
      %figure;hist(flx);title(num2str(dum(i)));
   end;
end;
dqm=dqm;%account for cool skin
dqmx=dqmx;%account for cool skin


figure;h=plot(dum,hlbm,'--',dum,hlmm,'-',dum,hlbmx,'x',dum,hlmmx,'o');set(gca,'fontsize',13);set(h,'LineWidth',2);xlabel('U_{10n} (ms^{-1})');ylabel('Hl (Wm^{-2})');%title([jdollars  '  line-median  x-bulk  o-turb']);
legend('H_{lb} median','H_{lc} median','H_{lb} mean','H_{lc} mean');
figure;h=plot(hlbm,hlmm,'o',hlbmx,hlmmx,'x',[0 200],[0 200]);set(gca,'fontsize',13);set(h,'LineWidth',2);xlabel('Bulk Hl (Wm^{-2})');ylabel('Turbulent Hl (Wm^{-2})');%title([jdollars  '  x-mean  o-median'  ]);
figure;h=plot(dum,hlbm./dqm/Le./dum*1e3./gfm,'-x',dum,hlmm./dqm/Le./dum*1e3./gfm,'-o',dum,hlbmx./dqmx/Le./dum*1e3./gfmx,'x',dum,hlmmx./dqmx/Le./dum*1e3./gfmx,'o');set(gca,'fontsize',13);figure;h=plot(dum,hlbm,'--',dum,hlmm,'-',dum,hlbmx,'x',dum,hlmmx,'o');set(gca,'fontsize',13);set(h,'LineWidth',2);xlabel('u_{10n} (ms^{-1})');ylabel('Hl (Wm^{-2})');%title([jdollars  '  line-median  x-bulk  o-turb']);xlabel('U_{10n} (ms^{-1})');ylabel('<wq>/Dq_{10n}/U_{10n}/Gf');%title([jdollars  '  line-median  x-bulk  o-turb']);
figure;plot(dum,sig./adqm/Le./dum*1e3./gfm,'-o',dum,sig./adqm/Le./dum*1e3./gfm./sqrt(nn),'-x');set(gca,'fontsize',13);xlabel('U_{10n} (ms^{-1})');ylabel('\sigma_{C_e}');title([ '  x-mean estimate  o- 1-hr estimate']);

figure;plot(dum,jam./dqm/Le./dum*1e3./gfm,'-ok',dum,(jam+sig./sqrt(nn))./dqm/Le./dum*1e3./gfm,'+k',dum,(jam-sig./sqrt(nn))./dqm/Le./dum*1e3./gfm,'+k');set(gca,'fontsize',13);xlabel('U_{10n} (ms^{-1})');ylabel('{C_{e10n}}');title([ 'o- median']);
%figure;plot(dum,jamx./dqmx/Le./dum*1e3./gfmx,'-ok',dum,(jamx+sig./sqrt(nn))./dqmx/Le./dum*1e3./gfmx,'+k',dum,(jamx-sig./sqrt(nn))./dqmx/Le./dum*1e3./gfmx,'+k');set(gca,'fontsize',13);xlabel('U_{10n} (m/s)');ylabel('{C_{E10n}}');title([ 'o- mean']);
axis([0 20 1e-3 1.4e-3]);
hold on;
clear yyz yyu usm zom zoqm;
for i=1:length(nn)
   plot([dum(i) dum(i)],[(jam(i)+sig(i)./sqrt(nn(i)))./dqm(i)/Le./dum(i)*1e3./gfm(i) (jam(i)-sig(i)./sqrt(nn(i)))./dqm(i)/Le./dum(i)*1e3./gfm(i)])
   %plot([dum(i) dum(i)],[(jamx(i)+sig(i)./sqrt(nn(i)))./dqmx(i)/Le./dum(i)*1e3./gfmx(i) (jamx(i)-sig(i)./sqrt(nn(i)))./dqmx(i)/Le./dum(i)*1e3./gfmx(i)])

   xx=[dumx(i) 0 28.7 28.2 24.2 22.5 141 419 0 600 1010 10 10 10 0 1 0 1 1];
   y=cor30a(xx);
   yyu(i)=y(19);
   yyz(i)=y(21);
   usm(i)=y(8);
   zom(i)=y(4);
   zoqm(i)=y(6);
   zlm(i)=y(7);
   
end;
h=plot(dum,yyz,'r');set(h,'LineWidth',2);
%hh=plot(dum,hlbm./dqm/Le./dum*1e3./gfmx,'d');set(hh,'LineWidth',2);
hh=plot(dum,hlnrm./Le*1e3,'d');set(hh,'LineWidth',2);
hold off


figure;plot(dum,hlmm./hlbm.*yyz,'-ok',dum,(hlmm./hlbm.*yyz)+sig./sqrt(nn)./dqm/Le./dum*1e3./gfm,'+k',dum,(hlmm./hlbm.*yyz)-sig./sqrt(nn)./dqm/Le./dum*1e3./gfm,'+k');set(gca,'fontsize',13);xlabel('U_{10n} (ms^{-1})');ylabel('{C_{E10n}}');title([ 'median: o=<Ft>/<Fb>C, diamond=<Ce>']);
axis([0 20 5e-4 2.0e-3]);
hold on;
for i=1:length(nn)
   plot([dum(i) dum(i)],[hlmm(i)./hlbm(i).*yyz(i)+(sig(i)./sqrt(nn(i)))./dqm(i)/Le./dum(i)*1e3./gfm(i) hlmm(i)./hlbm(i).*yyz(i)-(sig(i)./sqrt(nn(i)))./dqm(i)/Le./dum(i)*1e3./gfm(i)])
end;
h=plot(dum,yyz,'r');set(h,'LineWidth',2);
%hh=plot(dum,hlbm./dqm/Le./dum*1e3./gfmx,'d');set(hh,'LineWidth',2);
hh=plot(dum,hlnrm./Le*1e3,'d');set(hh,'LineWidth',2);
hold off

hlgm=(hlmm./hlbm+hlmmx./hlbmx)/2;%hlmm./hlbm;%

hlgm=hlmm./hlbm;%
figure;plot(dum,hlgm.*yyz,'-ok',dum,(hlgm.*yyz)+sig./sqrt(nn)./dqm/Le./dum*1e3./gfm,'+k',dum,(hlgm.*yyz)-sig./sqrt(nn)./dqm/Le./dum*1e3./gfm,'+k');set(gca,'fontsize',13);xlabel('U_{10n} (ms^{-1})');ylabel('{C_{E10n}}');%title([ 'median & mean: o=<Ft>/<Fb>C']);
%axis([0 20 1e-3 1.4e-3]);
hold on;
for i=1:length(nn)
   plot([dum(i) dum(i)],[hlgm(i).*yyz(i)+(sig(i)./sqrt(nn(i)))./dqm(i)/Le./dum(i)*1e3./gfm(i) hlgm(i).*yyz(i)-(sig(i)./sqrt(nn(i)))./dqm(i)/Le./dum(i)*1e3./gfm(i)]);%combine mean and median
end;
h=plot(dum,yyz,'r',dum,hlnrm./Le*1e3,'d');set(h,'LineWidth',2);
jj=find((reldir>270 | reldir<90) & org<1  & abs(dq10n)>1 & sog<5 );plot(u10n(jj),(hll(jj)+hl_webar(jj)')./dq10n(jj)./rhoair(jj)./u10n(jj)./gf(jj)/Le*1e3,'o');%axis([0 10 0 3.5e-3]);
axis([0 22 -1e-3 3e-3]);
hold off




num=mean(visa);
rr=usm.*zom/num;

usid=(tauibmm./taubmm).*usm;
usc=(taucxmm./taubmm).*usm;
ii=find(dum>0);
usc(1:2)=.95*(usid(1:2));
ust=usid;
ust(ii)=(usc(ii)+usid(ii))/2;

jamu(1:2)=.95*tauibmm(1:2);
jamux(1:2)=.95*tauibmx(1:2);

cdn10mt_ib=tauibmm.*yyu./taubmm;%(ust./gfm./dum).^2;
cdn10mt_cx=taucxmm.*yyu./taubmm;%(ust./gfm./dum).^2;
cdn10mt=jamu.*yyu./taubmm;%(ust./gfm./dum).^2;

cdn10mt_ibx=tauibmx.*yyu./taubmx;%(ust./gfm./dum).^2;
cdn10mt_cxx=taucxmx.*yyu./taubmx;%(ust./gfm./dum).^2;
cdn10mtx=jamux.*yyu./taubmx;%(ust./gfm./dum).^2;
cdn10_mnmd=(cdn10mt+cdn10mtx)/2;
cdn10_mnmd_c=(cdn10mt_cx+cdn10mt_cxx)/2;
cdn10_mnmd_i=(cdn10mt_ib+cdn10mt_ibx)/2;


%zomt=10./exp(.4*dum./ust.*gfm);
chft=sqrt(cdn10mt);%ust./dum./gfm;
zomt=10./exp(.4./chft);
rrt=zomt.*ust/num;

chfb=.4./log(10./zom);

ce=hlgm.*yyz;
cehf=ce./chft;
cehfb=ce./chfb;

zoqf=10./exp(.4./cehf);
zoqfb=10./exp(.4./cehfb);
rrqf=zoqf.*ust/num;
rrqfb=zoqfb.*ust/num;

figure;
h=loglog(rr,zoqm,rrt,zoqf,'-o',rr,zoqfb,'-x');set(h,'LineWidth',2);axis([.1 1e3 1e-7 1e-3]);set(gca,'fontsize',13);xlabel('Rr ');ylabel('{z_{oq}} (m)');title([ '']);
figure;
h=loglog(rr(2:16),(rrqfb(2:16)+rrqf(2:16))/2,'o-',rr,min(1.15e-4,5.5e-5./rr.^.6).*ust/num);set(h,'LineWidth',2);axis([.1 1e2 2e-2 2e0]);set(gca,'fontsize',13);xlabel('Rr ');ylabel('{R_{q}}');title([ '']);
hold on;
loglog(rr(2:16),(rrqfb(2:16)+rrqf(2:16))/2,'o-',rr,rr./exp(2.28*rr.^.25-2),'--',rr,rr./exp(3.5*rr.^.25-3.4));
hold off;
%figure;
%h=loglog(rr(2:16),(rrqfb(2:16)+rrqf(2:16))/2,'o-',rr,rr./exp(2.28*rr.^.25-2),'--',rr,rr./exp(3.5*rr.^.25-3.4));set(h,'LineWidth',2);axis([.1 1e2 2e-2 2e0]);set(gca,'fontsize',13);xlabel('Rr ');ylabel('{R_{q}}');title([ '']);

alp=(zomt-.11*num./ust).*gravm./ust.^2;
ut=0:20;
   charn=0.011*ones(1,length(ut));
   ii=find(ut>10);
      charn(ii)=0.011+(ut(ii)-10)/(18-10)*(0.018-0.011);
   ii=find( ut>18)
      charn(ii)=0.018;
alp(1:2)=NaN;
figure;h=plot(ut,charn,'--',dumx,alp,'-o',[0 20],[1.0e-2 1.0e-2]);set(h,'LineWidth',2);set(gca,'fontsize',13);xlabel('U_{10n} (ms^{-1})');ylabel('\alpha');title([ '']);


figure;plot(dumx,yyu,dumx,cdn10mt,'-ok',dumx,cdn10mt+(sigu./sqrt(nm)).*yyu./taubmm,'+k',dumx,cdn10mt-(sigu./sqrt(nm)).*yyz./taubmm,'+k');set(gca,'fontsize',13);xlabel('U_{10n} (ms^{-1})');ylabel('{C_{d10n}}');title([ 'median: o=<Ft>/<Fb>C']);ylim([.5e-3 2e-3]);
%axis([0 10 .0e-3 2.5e-3]);
hold on;
for i=1:length(nn)
   plot([dumx(i) dumx(i)],[cdn10mt(i)+(sigu(i)./sqrt(nm(i))).*yyu(i)./taubmm(i) cdn10mt(i)-sigu(i)./sqrt(nm(i)).*yyz(i)./taubmm(i)])
end;
h=plot(dumx,yyu,'r');set(h,'LineWidth',2);
plot(dumx,cdn10mt_ib,'x',dumx,cdn10mt_cx,'d');
hold off

% jj=find(abs(taucx-tauib)<.95*taub & (reldir>270 | reldir<90) );plot(u10n(jj),(taucx(jj)+tauib(jj))/2./gf(jj)./u10n(jj).^2./rhoair(jj),'.');axis([0 22 0 3e-3]);

figure;plot(dumx,yyu,dumx,cdn10mt,'-ok',dumx,cdn10mt+(sigu).*yyu./taubmm./sqrt(nm),'+k',dumx,cdn10mt-(sigu).*yyz./taubmm./sqrt(nn),'+k');set(gca,'fontsize',13);xlabel('U_{10n} (m/s)');ylabel('{C_{d10n}}');%title([ 'median & mean: o=<Ft>/<Fb>C']);
%axis([0 10 .0e-3 2.5e-3]);
hold on;
for i=1:length(nm)
   plot([dumx(i) dumx(i)],[cdn10mt(i)+(sigu(i)).*yyu(i)./taubmm(i)/sqrt(nm(i)) cdn10mt(i)-(sigu(i)).*yyz(i)./taubmm(i)/sqrt(nm(i))])
end;
h=plot(dumx,yyu,'r');set(h,'LineWidth',2);
jj=find(abs(taucx-tauib)<2*taub & (reldir>270 | reldir<90) );plot(u10n(jj),(taucx(jj)+tauib(jj))/2./gf(jj)./u10n(jj).^2./rhoair(jj),'.');%axis([0 10 0 3.5e-3]);
hold off
axis([0 22 -1e-3 3e-3]);


hlbm_cor=hlbm;
hlmm_cor=hlmm;
dum_cor=dum;
dqm_cor=dqm;

cdn10s=cdn10mt;


Tint=18./dum./(min(1,max(.2,.35+.22*18./zlm)));
a=1.0;figure;plot(dum,a*1.25*(1+3*abs(18./zlm)).^.33.*phi_tt_nq(18./zlm).*sqrt(Tint/10/60),dum,sig./hlmm,'-o',dum,a*1.25*(1+3*abs(18./zlm)).^.33.*sqrt(5+.6*abs(1000./zlm).^.667).*sqrt(Tint/10/60),'--',dum,sqrt(sigux.^2)./taubmm,'-x');xlabel('U_{10n}');ylabel('\delta[<wx>]/<wx>');axis([0 15 0 1.5]);title('Normalized flux uncertainty, StratusClim');
%a=1.0;figure;plot(18./zlm,a*(1+3*abs(18./zlm)).^.33.*phi_tt_nq(18./zlm)/3.*sqrt(Tint/2.8))
legend('Theory Hl','Measured Hl','Theory \tau','Measured \tau')
