disp('HiWinGS2013_hr_ave');
dta=60;%average time in minutes
clear jda y1 yvar1 nn1 y2 yvar2 nn2 y3;
%**************   straight average of all data
[jda,y1,yvar1,nn1]=timave_1(jdy,dta,x);
[jda,y3,yvar3,nn1]=timave_1(jdy,dta,yxx);
[jda,y4,yvar4,nn1]=timave_1(jdy,dta,yco);

%**************   selected turbulence averages
% jj=find((reldir<60 | reldir>300) & ushp_im<5 & sig_h<4 & sig_u<0.2 & sp2<0.8  & sqrt(ww)./ugw<.7+.0015*U.^2 & sqrt(vv)./ugu<1.4 & isfinite(usib));% & isledist>3.5 
jj=find((reldir<90 | reldir>270) & sig_h<5 & sig_u<1.4 & org<5 & sp2<1.2  & sqrt(wwj)./ugw<2+.0015*U.^2 & sqrt(vvj)./ugu<2 & isfinite(usib));% & isledist>3.5 
jplume=ones(length(jdy),1);
ii=find(sqrt(wwj)./ugw<2+.0015*U.^2 & sqrt(vvj)./ugu<2);
jplume(ii)=0;
jmanuv=3*ones(length(jdy),1);
ij=find(sig_h<5 & sig_u<1.4 & sp2<1.2);
jmanuv(ij)=sig_u(ij);
clear y2 yj jym
[jdax,y2,yvar2,nn2]=timave_1(jdy(jj),dta,x(jj,:));%selected values, all variables
[jdax,yjx,yjvar,nn2]=timave_1(jdy(jj),dta,jplume(jj));
[jdax,yjmx,yjmvar,nn2]=timave_1(jdy(jj),dta,jmanuv(jj));
[jdax,yco2,yco2var2,nn2]=timave_1(jdy(jj),dta,yco(jj,:));
[jdax,ycr,ycrvar,nn2]=timave_1(jdy(jj),dta,yxx(jj,:));

%**************   identify turbulence variables  ********
yj=zeros(1,length(jda));
yjm=zeros(1,length(jda));


ii=find(jda==jdax(1));
y1(ii:length(jdax),116:128)=y2(:,116:128);%cov and var
% y1(ii:length(jda),116:128)=y2(:,116:128);%cov and var
% y1(ii:length(jda),134:141)=y2(:,134:141);%cx^2
% y4(ii:length(jda),243)=yco2(:,243);%Cq^2 from Licor
% y3(ii:length(jda),2:16)=ycr(:,2:16);%.cr files
y1(ii:length(jda),44)=y2(:,44);%tilt
yj(ii:length(jda))=yjx(:);%plume
kk=find(isnan(yj));yj(kk)=1;
yjm(ii:length(jda))=yjmx(:);%maneuver
y1(ii:length(jdax),44)=y2(:,44);%tilt
yj(ii:length(jdax))=yjx(:);%plume
kk=find(isnan(yj));yj(kk)=1;
yjm(ii:length(jdax))=yjmx(:);%maneuver
kk=find(isnan(yjm));yjm(kk)=3;
if length(ii)>1
   y1(1:(ii-1),116:128)=NaN;
   y1(1:(ii-1),134:141)=NaN;
   y4(1:(ii-1),243)=NaN;
   y3(1:(ii-1),2:16)=NaN;
   y1(1:(ii-1),44)=NaN;
   yj(1:(ii-1))=NaN;
   yjm(1:(ii-1))=NaN;
end;

%*****************  humidity spectrum
clear ycq;
% jj=find((reldir<60 | reldir>300) & ushp_im<5 & sig_h<4 & sig_u<0.2 & sp2<0.8  & sqrt(ww)./ugw<.7+.002*U.^2 & sqrt(vv)./ugu<1.4 & isfinite(usib));% & isledist>3.5 
jj=find((reldir<90 | reldir>270) & sig_h<5 & sig_u<1.4 & sp2<1.2 & org<5 & sqrt(wwj)./ugw<2+.0015*U.^2 & sqrt(vvj)./ugu<2 & isfinite(usib));% & isledist>3.5 
[jdar,ycql,ycqvarl,nnql]=timave_1(jdy(jj),dta,yco(jj,243));

ycqfl=zeros(1,length(jda));
ii=find(jda==jdar(1));

ycqfl(ii:length(jda))=ycql;
if ii>1
   ycql(1:ii-1)=NaN;
end;


%***************    10-m neutral wind  *******
clear yu yts yqs yq
[jdaa,yu,yvaru,nnu]=timave_1(jdy,dta,u10n);
[jdaa,yts,tsvar,nnts]=timave_1(jdy,dta,dt(:,4));
[jdaa,yqs,qsvar,nnts]=timave_1(jdy,dta,dt(:,6));  %average qs
% yqs=qsea_p([yts y1(:,157)]);
yq=y1(:,21);
%*******************  vector average winds  ****
clear v1 theta1 v2_scs thet1_scs v1rel thet1rel yushp yhead v1x thet1x jdam
[jda,v1,thet1]=timave_vec1(jdy,dta,x(:,16),x(:,41));%etl true
[jda,v1_scs,thet1_scs]=timave_vec1(jdy,dta,x(:,2),x(:,34));%scs true 
[jda,v1rel,thet1rel]=timave_vec1(jdy,dta,x(:,38),x(:,42));%relative
[jdam,v1x,thet1x]=timave_vec1(jdy(jj),dta,x(jj,16),x(jj,41));%etl turb select
[jdax,yushp,yhead]=timave_vec1(jdy,dta,ushp,head);%ship dopler log, RL gyro
[jdax,ysog,ycog]=timave_vec1(jdy,dta,ushp_im,x(:,101));%scs sog and cog

%August 4, 2008 - minor corrections to direction averaging.
i=find(thet1<0);thet1(i)=thet1(i)+360;
i=find(thet1_scs<0);thet1_scs(i)=thet1_scs(i)+360;
i=find(thet1rel<0);thet1rel(i)=thet1rel(i)+360;
i=find(ycog<0);ycog(i)=ycog(i)+360;
i=find(yhead<0);yhead(i)=yhead(i)+360;


% %10/31/2012 some weirdness in heading dur to 0 ship's speed... really need
% %to rewrite those crappy codes one day!
% switch leg(1:end-1)
%     case {'leg2','leg3'}
%         [jojo,yhead2,yvar2,nn2]=timave_1(jdy,dta,head);%selected values, all variables
%         yhead(yhead==0)=yhead2(yhead==0);
%         [jojo,ycog2,yvar2,nn2]=timave_1(jdy,dta,x(:,101));%selected values, all variables
%         ycog(ycog==0)=ycog2(ycog==0);
% end


v1f=zeros(1,length(jda));
ii=find(jda==jdam(1));
v1f(ii:length(jda))=v1x;
if ii>1
   v1f(1:ii-1)=v1(1:ii-1);
end;



%**************  ave 10-min bulk fluxes

[jda,hsbar,hsvar,nnhs]=timave_1(jdy,dta,hsbb);
[jda,hlbar,hlvar,nnhl]=timave_1(jdy,dta,hlbb);
[jda,taubar,tauvar,nntau]=timave_1(jdy,dta,taubb);
[jda,dtwrmbar,dtwrmvar,nndtwrm]=timave_1(jdy,dta,dt(:,34));
[jda,hl_webar,hl_webvar,nnhl_webb]=timave_1(jdy,dta,hl_webb);
[jda,rhbar,rhvar,nnhs]=timave_1(jdy,dta,rh);

%************  licor
hlc_licbar=zeros(1,length(jda));
if isempty(hlc_lic)
   hlc_licbar(:)=NaN;
else
  	[jdax,hlx,hlc_licvar,nnlic]=timave_1(jdy(jj),dta,hlc_lic(jj,:));
	ii=find(jda==jdax(1));
	hlc_licbar(ii:length(jda))=hlx;%cov and var

	if ii>1
   	hlc_licbar(1:(ii-1))=NaN;
   end;
end;

wco2_licbar=zeros(1,length(jda));
if isempty(wco2_lic)
   wco2_licbar(:)=NaN;
else
  	[jdax,hlx,wco2_licvar,nnlic]=timave_1(jdy(jj),dta,wco2_lic(jj,:));
	ii=find(jda==jdax(1));
	wco2_licbar(ii:length(jda))=hlx;%cov and var

	if ii>1
   	wco2_licbar(1:(ii-1))=NaN;
   end;
end;
