disp('epicz_weller_diurnal');
%************************  daily averages  ***********
clear gd raind rsd rld rnld hnd ud hsd hld tsd tsgd jdd latd lond tad rlclrd rsclrd taud uxd uyd qsd qad


jj0=floor(jdq(1));%integer first day
jjm=ceil(jdq(length(jdq)));%integer last day
mx=jjm-jj0;jj=0;%number of days

jd0=floor(jdq);%integerized time series
jdf=jdq-jd0;%local time


dd=0:2:24;
dd=dd/24;  %Convert hr to decimal
jj=find(jd0>=stdt & jd0<=endt);

%
% For AMMA 2007 JD 123 abd 1/2 or JD 124, the PSP and PIR are bad
%Loop through and put in "0" for these periods DEW 5/20/2007
lj = length(jdq);
for p = 1:lj
    rsnew(p,1) = rs(p,1);
    rlnew(p,1) = rl(p,1);
    rlnetnew(p,1) = rlnet(p,1);
    hsbnew(p,1) = hsb(p,1);
    hlbnew(p,1) = hlb(p,1);
    hnetnew(p,1) = hnet(p,1);
        if jdq(p,1) < 124.6
                rsct = 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;
        end
end

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

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

jdd=jdd*24;
tit = ('GOMECC 2007')

figure;
plot(jdd,uxd,'-x',jdd,uyd,'-o',[0 25],[0 0],'--');
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');
ylabel('Mean Wind Components (ms^-^1)');
%title('Northerly -x; Easternly - o');
title(tit)
ylim([-10,10])
set(gca,'fontsize',8);
legend('N','E')
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_wndcomponents.jpg'];
print('-djpeg90 ', print_buffer);

figure;
plot(jdd,rld-rlclrd,'-x');
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');
ylabel('IR Cloud Forcing (Wm^-^2))');
%figure;plot(jdd,raind,'-x');set(gca,'fontsize',14);xlabel('Local Time of Day (hr)');ylabel('Rain Rate (mm/h)');
title(tit)
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_IRforcing.jpg'];
print('-djpeg90 ', print_buffer);

figure;plot(jdd,hnd,'-x',[0 24],[0 0 ]);
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');
ylabel('Net Heat Flux (Wm^-^2)');
title(tit)
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_Net.jpg'];
print('-djpeg90 ', print_buffer);

figure;
plot(jdd,hsd,'-',jdd,hld,'o',jdd,rnld,'-x',[0 24],[0 0 ]);
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');
ylabel('Heat Flux Components (Wm^-^2)');
set(gca,'fontsize',8);
legend('Sensible','Latent','Net','location','Southeast')
title(tit)
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_SenLatNet.jpg'];
print('-djpeg90 ', print_buffer);

figure;
plot(jdd,rsd,'-o',jdd,rsclrd,'-',[0 24],[0 0 ]);
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');
ylabel('Solar Heat Flux(W/m^-^2)');
ylim([0 1200])
title(tit)
set(gca,'fontsize',8);
legend('Measured','Modeled','location','Northeast')
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_Solarflux.jpg'];
print('-djpeg90 ', print_buffer);

figure;plot(jdd,uv,'-o',jdd,ud);
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');
ylabel('Wind Speed (ms^-^1)');
%figure;plot(jdd,tsd,'-o',jdd,tsgd);set(gca,'fontsize',14);xlabel('Local Time of Day (hr)');ylabel('Sea Temperature (C)');
title(tit)
set(gca,'fontsize',8);
legend('Vector','Scalar','location','Southeast')
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_vectorscalarwnd.jpg'];
print('-djpeg90 ', print_buffer);


figure;
plot(jdd,tad,'-x',jdd,tsd,'-o',jdd,tsgd,'-d');
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');ylabel('Temperature (C)');
title(tit)
set(gca,'fontsize',8);
legend('PSD Tair','PSD SST','SCS SST','location','Northwest')
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_SSTTair.jpg'];
print('-djpeg90 ', print_buffer);


figure;
plot(jdd,tsd-tad,'-o');
set(gca,'fontsize',14);
xlabel('Local Time of Day (hr)');
ylabel('Ts-Ta (C)');
title(tit)
print_buffer = ['S:\GOMECC_2007\RHB\flux\Processed_Images\GOMECC07_FLUX_diurnal_TsTadiff.jpg'];
print('-djpeg90 ', print_buffer);



%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])