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)=nanmean1(jdf(jj(ii)));
    uxd(i)=nanmean1(U(jj(ii)).*cos(pi/180*dir(jj(ii))));
    uyd(i)=nanmean1(U(jj(ii)).*sin(pi/180*dir(jj(ii))));
    ud(i)=nanmean1(U(jj(ii)));
    ugd(i)=nanmean1(sqrt(U(jj(ii)).^2+wgust(jj(ii)).^2));
    sigud(i)=nanvar1(U(jj(ii)));
    raind(i)=nanmean1(org(jj(ii)));
    rsd(i)=nanmean1(rsnew(jj(ii)));
    rld(i)=nanmean1(rlnew(jj(ii)));
    rnld(i)=nanmean1(rlnet(jj(ii)));
    hnd(i)=nanmean1(hnetnew(jj(ii)));
    hsd(i)=nanmean1(hsbnew(jj(ii)));
    hld(i)=nanmean1(hlbnew(jj(ii)));
    taud(i)=nanmean1(taub(jj(ii)));
    tsd(i)=nanmean1(tsnk(jj(ii)));
    tad(i)=nanmean1(ta(jj(ii)));
     kk=find(isfinite(tsg(jj(ii))));
    tsgd(i)=nanmean1(tsg(jj(ii(kk))));
    qsd(i)=nanmean1(qse(jj(ii)));
    qad(i)=nanmean1(qa(jj(ii)));
    latd(i)=nanmean1(lat(jj(ii)));
    lond(i)=nanmean1(lon(jj(ii)));
    rsclrd(i)=nanmean1(Rscl(jj(ii)));
    rlclrd(i)=nanmean1(rlclr(jj(ii)));
    gd(i)=nanmean1(wgust(jj(ii)).^2);
    orgd(i)=nanmean1(org1(jj(ii)));
end;

uv=sqrt(uxd.^2+uyd.^2);

jdd=jdd*24;

figure; set(gca,'fontsize',12);
plot(jdd,uxd,'-x',jdd,uyd,'-o');
xlabel('Time of Day (hr UTC)');
ylabel('Mean Wind Components (ms^-^1)');
title(sprintf('%s',cruise),'FontWeight','Bold','Interpreter','none')
axis([0,24,-10,10]); set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_wndcomponents_',graphformat]);
    print(graphdevice,ppath);
end;

figure;set(gca,'fontsize',12);
plot(jdd,rld-rlclrd,'-x');
xlabel('Time of Day (hr UTC)');
ylabel('IR Cloud Forcing (Wm^-^2)');
xlim([0,24]); set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
title(sprintf('%s',cruise),'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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_IRforcing_',graphformat]);
    print(graphdevice,ppath);
end;

figure;
set(gca,'fontsize',12);
plot(jdd,hnd,'-x');
xlim([0,24]); set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
xlabel('Time of Day (hr UTC)');
ylabel('Net Heat Flux (Wm^-^2)');
title(sprintf('%s',cruise),'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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_Net_',graphformat]);
    print(graphdevice,ppath);
end;

figure;set(gca,'fontsize',12);
plot(jdd,hsd,'-',jdd,hld,'o',jdd,rnld,'-x');
xlim([0,24]); set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
xlabel('Time of Day (hr UTC)');
ylabel('Heat Flux Components (Wm^-^2)');
legend('Sensible','Latent','NetIR','location','Best');
title(sprintf('%s',cruise),'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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_SenLatNet_',graphformat]);
    print(graphdevice,ppath);
end;

figure;set(gca,'fontsize',12);
plot(jdd,rsd,'-o',jdd,rsclrd,'-');
xlabel('Time of Day (hr UTC)');
ylabel('Solar Heat Flux(Wm^-^2)');
axis([0,24,0,800]); set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
title(sprintf('%s',cruise),'FontWeight','Bold','Interpreter','none');
legend('Observed','Clearsky','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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_Solarflux_',graphformat]);
    print(graphdevice,ppath);
end;

% figure;
% set(gca,'fontsize',12);
% plot(jdd,uv,'-o',jdd,ud);
% xlim([0,24]);  set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
% xlabel('Time of Day (hr UTC)');
% ylabel('Wind Speed (ms^-^1)');
% title(sprintf('%s',cruise),'FontWeight','Bold','Interpreter','none')
% legend('Vector','Scalar','location','Best')
% if prtit;
%     ppath = fullfile(path_raw_images,[cruise,'_diurnal_vectorscalarwnd_',graphformat]);
%     print(graphdevice,ppath);
% end;

figure;set(gca,'fontsize',12);
plot(jdd,tad,'-x',jdd,tsd,'-o',jdd,tsgd,'-d');
xlim([0,24]);  set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
xlabel('Time of Day (hr UTC)');ylabel('Temperature (degC)');
title(sprintf('%s',cruise),'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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_SSTTair_',graphformat]);
    print(graphdevice,ppath);
end;

figure;set(gca,'fontsize',12);
plot(jdd,tsd-tad,'-o');
xlim([0,24]);  set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
xlabel('Time of Day (hr UTC)');
ylabel('Ts-Ta (degC)');
title(sprintf('%s',cruise),'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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_TsTadiff_',graphformat]);
    print(graphdevice,ppath);
end;

figure;set(gca,'fontsize',12);
plot(jdd,orgd,'-o');
xlim([0,24]);  set(gca(gcf),'XTick',0:6:24); grid on; grid minor;
xlabel('Time of Day (hr UTC)');
ylabel('rainrate (mm/hr)');
title(sprintf('%s',cruise),'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;
    ppath = fullfile(path_raw_images,[cruise,'_diurnal_rainrate_',graphformat]);
    print(graphdevice,ppath);
end;
