disp('AMMA_hr_ave');
dta=60;%average time in minutes
clear jda y1 yvar1 nn1 y2 yvar2 nn2 ;
data=importdata('C:\Data\AMMA\Final\AMMA087_filter_Final.txt');
total=data(:,18);
ii=find(isnan(total));
x(ii)=NaN;
 yxx(ii)=NaN;
yco(ii)=NaN;
yfl(ii)=NaN;
%**************   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);
[jda,y5,yvar4,nn1]=timave_1(jdy,dta,yfl);


%**************   selected turbulence averages
jj=find((reldir<90 | reldir>270) & sig_h<8 & sig_u<2 & sp2<2  & org<5 & sqrt(ww)./ugw<.65+.002*U.^2 & sqrt(vv)./ugu<1.1 & isfinite(usib));% & isledist>3.5 
jplume=ones(length(jdy),1);
ii=find(sqrt(ww)./ugw<.55+.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);
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(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),133)=y2(:,133);%sigoph
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
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;
   y3(1:(ii-1),2:16)=NaN;
   %y1(1:(ii-1),133)=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<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(sigoph(jj)<50 & ophm(jj)>2600);
%[jdar,ycq,ycqvar,nnq]=timave_1(jdy(jj(kk)),dta,x(jj(kk),148));
[jdar,ycq,ycqvar,nnq]=timave_1(jdy(jj),dta,x(jj,141));
[jdar,ycql,ycqlvar,nnql]=timave_1(jdy(jj),dta,yco(jj,243));
ycqf=zeros(1,length(jda));
ycqlf=zeros(1,length(jda));
ii=find(jda==jdar(1));
ycqf(ii:length(jda))=ycq;
ycqlf(ii:length(jda))=ycql;
if ii>1
   ycq(1:ii-1)=NaN;
   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));
yqs=qsea(yts);
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


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);

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

	if ii>1
   	hl_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;

%************  ophir
%hl_ophbar=zeros(1,length(jda));
%if isempty(hlc_oph)
%   hl_ophbar(:)=NaN;
%else
%  	[jdax,hlx,hl_ophvar,nnoph]=timave_1(jdy(jj),dta,hlc_oph(jj,:));
%	ii=find(jda==jdax(1));%
%	hl_ophbar(ii:length(jda))=hlx;%cov and var

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

%q_ophbar=zeros(1,length(jda));
%if isempty(q_oph)
%   q_ophbar(:)=NaN;
%else
 % 	[jdax,hlx,q_ophvar,nnoph]=timave_1(jdy(jj),dta,q_oph(jj,:));
%	ii=find(jda==jdax(1));
%	q_ophbar(ii:length(jda))=hlx;%cov and var
%
%	if ii>1
 %  	q_ophbar(1:(ii-1))=NaN;
%   end;
%end;




