disp(['epicz_diurnal: ' cruise]);
%************************  diurnal averages  ***********
clear gd raind rsd rld rnld hnd ud hsd hld tsd tsgd jdd latd lond tad rlclrd rsclrd taud uxd uyd qsd qad orgd

jd0=floor(jdq);%integerized time series
jdf=jdq-jd0;%UTC fraction of day

nhours=24;
dt=1; % was 2 for 2-hourly diurnal composite bins
dd=0:dt:nhours;
dd=dd/nhours;  %Convert hr to decimal
p=jdq>=stdt & jdq<endt; % selects time interval
jj=find(p); 

rsnew=rs;
rlnew=rl;
rlnetnew=rlnet;
hsbnew=hsb;
hlbnew=hlb;
hnetnew=hnet;
% p=jdq<124.6; % never on VOCALS 2008
% rsct=sum(p);
% rsnew(p,1)=0;
% rlnew(p,1)=0;
% rlnetnew(p,1)=0;
% hsbnew(p,1)=0;
% hlbnew(p,1)=0;
% hnetnew(p,1)=0;

for i=1:nhours
    ii=find(jdf(jj)>=dd(i) & jdf(jj)<dd(i+1) & isfinite(hsb(jj)));
    nn(i)=length(ii);
    jdd(i)=nanmean(jdf(jj(ii)));
    uxd(i)=nanmean(U(jj(ii)).*cos(pi/180*dir(jj(ii))));
    uyd(i)=nanmean(U(jj(ii)).*sin(pi/180*dir(jj(ii))));
    ud(i)=nanmean(U(jj(ii)));
    ugd(i)=nanmean(sqrt(U(jj(ii)).^2+wgust(jj(ii)).^2));
    sigud(i)=nanvar(U(jj(ii)));
    raind(i)=nanmean(org(jj(ii)));
    % DEW 5/20/2007  see loop above
    rsd(i)=nanmean(rsnew(jj(ii)));
    rld(i)=nanmean(rlnew(jj(ii)));
    rnld(i)=nanmean(rlnet(jj(ii)));
    hnd(i)=nanmean(hnetnew(jj(ii)));
    hsd(i)=nanmean(hsbnew(jj(ii)));
    hld(i)=nanmean(hlbnew(jj(ii)));
    %%%%%%%%%%%%%%
    ud(i)=nanmean(U(jj(ii)));
    taud(i)=nanmean(taub(jj(ii)));
    tsd(i)=nanmean(tsnk(jj(ii)));
    tad(i)=nanmean(ta(jj(ii)));
     kk=find(isfinite(tsg(jj(ii))));
    tsgd(i)=nanmean(tsg(jj(ii(kk))));
    qsd(i)=nanmean(qse(jj(ii)));
    qad(i)=nanmean(qa(jj(ii)));
    latd(i)=nanmean(lat(jj(ii)));
    lond(i)=nanmean(lon(jj(ii)));
    rsclrd(i)=nanmean(Rscl(jj(ii)));
    rlclrd(i)=nanmean(rlclr(jj(ii)));
    gd(i)=nanmean(wgust(jj(ii)).^2);
    orgd(i)=nanmean(org1(jj(ii)));
end;
uv=sqrt(uxd.^2+uyd.^2);

jdd=jdd*24;

figure;set(gca,'fontsize',14);
plot(jdd,uxd,'-x',jdd,uyd,'-o');
xlabel('Time of Day (hr UTC)');
ylabel('Mean Wind Components (ms^-^1)');
%title('Northerly -x; Easternly - o');
grid on;%grid minor
title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
axis([0 24 -10 10])
% set(gca,'fontsize',8);
legend('N','E','Location','Best')
annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_wndcomponents_' leg(1:end-1) graphformat]); end;


figure;set(gca,'fontsize',14);
plot(jdd,rld-rlclrd,'-x');
xlabel('Time of Day (hr UTC)');
ylabel('IR Cloud Forcing (Wm^-^2)');xlim([0 24])
%figure;plot(jdd,raind,'-x');set(gca,'fontsize',14);xlabel('Time of Day (hr)');ylabel('Rain Rate (mm/h)');
grid on;%grid minor
title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_IRforcing_' leg(1:end-1) graphformat]); end;

figure;
set(gca,'fontsize',14);
plot(jdd,hnd,'-x');xlim([0 24])
xlabel('Time of Day (hr UTC)');
ylabel('Net Heat Flux (Wm^-^2)');
grid on;%grid minor
title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_Net_' leg(1:end-1) graphformat]); end;


figure;set(gca,'fontsize',14);
plot(jdd,hsd,'-',jdd,hld,'o',jdd,rnld,'-x');
xlim([0 24])
xlabel('Time of Day (hr UTC)');
ylabel('Heat Flux Components (Wm^-^2)');
legend('Sensible','Latent','NetIR','location','Best')
grid on;grid minor
title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_SenLatNet_' leg(1:end-1) graphformat]); end;

figure;set(gca,'fontsize',14);
plot(jdd,rsd,'-o',jdd,rsclrd,'-');
xlabel('Time of Day (hr UTC)');
ylabel('Solar Heat Flux(Wm^-^2)');
axis([0 24 0 1200])
grid on;grid minor
title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
legend('Measured','Modeled','location','Best')
annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_Solarflux_' leg(1:end-1) graphformat]); end;

% figure;
% set(gca,'fontsize',14);
% plot(jdd,uv,'-o',jdd,ud);
% xlim([0 24])
% xlabel('Time of Day (hr UTC)');
% ylabel('Wind Speed (ms^-^1)');
% %figure;plot(jdd,tsd,'-o',jdd,tsgd);set(gca,'fontsize',14);xlabel('Time of Day (hr)');ylabel('Sea Temperature (C)');
% grid on;grid minor
% title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
% legend('Vector','Scalar','location','Best')
% if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_vectorscalarwnd_' leg(1:end-1) graphformat]); end;


figure;set(gca,'fontsize',14);
plot(jdd,tad,'-x',jdd,tsd,'-o',jdd,tsgd,'-d');
xlim([0 24])
xlabel('Time of Day (hr UTC)');ylabel('Temperature (degC)');
grid on;grid minor
title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
legend('PSD Tair','PSD SST','SCS SST','location','Best')
annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_SSTTair_' leg(1:end-1) graphformat]); end;


figure;set(gca,'fontsize',14);
plot(jdd,tsd-tad,'-o');
xlim([0 24])
xlabel('Time of Day (hr UTC)');
ylabel('Ts-Ta (degC)');
grid on;grid minor
title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_TsTadiff_' leg(1:end-1) graphformat]); end;



% figure;set(gca,'fontsize',14);
% plot(jdd,orgd,'-o');
% xlim([0 24])
% xlabel('Time of Day (hr UTC)');
% ylabel('rainrate (mm/hr)');
% grid on;grid minor
% title(sprintf('%s -%s',cruise,leg(1:end-1)),'FontWeight','Bold','Interpreter','none')
% annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
% if prtit; print(graphdevice,[way_proc_images_flux cruise '_diurnal_rainrate_' leg(1:end-1) graphformat]); end;


%figure;ii=find(latd<100 & raind<.1);plot(uv(ii),sqrt(ugd(ii).^2-(uxd(ii).^2+uyd(ii).^2+gd(ii))),'.');axis([0 12 0j 5]);
%median(sqrt(ugd(ii).^2-(uxd(ii).^2+uyd(ii).^2+gd(ii))))
%figure;ii=find(raind<.1);jj=find(raind>.1 & raind<=1);kk=find(raind>1 );plot(ugd(ii),sqrt(uxd(ii).^2+uyd(ii).^2+gd(ii).^2+1.1^2),'.',ugd(jj),sqrt(uxd(jj).^2+uyd(jj).^2+gd(jj).^2+1.7^2),'x',ugd(kk),sqrt(uxd(kk).^2+uyd(kk).^2+gd(kk).^2+2.9^2),'o',[0 20],[0 20])