load e:\ascii\lidar_fr ;%jd=julian day, fr=1-hr cloud fracion, htt= 1st layer cloud base/top hts
jdx=jd;
jdl=jdx-1;
ff=find(jdl>547);
jdl(ff)=jdl(ff)-1;

load e:\ascii\ceilo_fr ;%jd=julian day, fr=1-hr cloud fracion, htt= 1st layer cloud base,vert vis
jd(1:5336)=jdl(1:5336);
jd(5337:5337+2116-1)=jdfc;
htl=htt;
clear htt;
htt(:,1:5336)=htl(:,1:5336);
htt(:,5337:5337+2116-1)=htc';
frl=fr;
clear fr;
fr(1:5336)=frl(1:5336);
fr(5337:5337+2116-1)=frcc;




read_main_file3;%%%%%%%%%%%%%%%%%%%% read in flux data


%Rsdm=Rsdm*.975;
%Rsum=Rsum*1.025;
ii=find(Rldm>500);Rldm(ii)=NaN;
ii=find(Rlum>500);Rlum(ii)=NaN;
ii=find(Rsdm>1500);Rsdm(ii)=NaN;
ii=find(Rldm>1500);Rsum(ii)=NaN;

ii=find(hsm>1000);hsm(ii)=NaN;
hlbm=hlb2m;
ii=find(hlbm>1000);hlbm(ii)=NaN;
ij=find(hlb10m>1000);hlb10m(ij)=NaN;

jj=find(isfinite(hlb10m(ii)));
hlbm(ii(jj))=hlb10m(ii(jj));

get_Rsclear;

%get Rlclear
ii=find(Rldm>500);
Rldm(ii)=NaN;
ii=find(Tgem>10);
Tgem(ii)=NaN;
ii=find(Th10m>500);
Th10m(ii)=NaN;
ii=find(q10m>500);
q10m(ii)=NaN;
rlcler=5.67e-8*(Th10m+273.16).^4.*(.70+.03*sqrt(q10m));%model clear sky
rucler=5.67e-8*(Th10m+273.16).^4.*(.93+.03*sqrt(q10m));%model clear sky
i=find(isnan(rlcler));
[a1 a2]=size(i);


frm=interp1(jd,fr,jdm,'linear');
hb=interp1(jd,htt(1,:),jdm,'linear');
ht=interp1(jd,htt(2,:),jdm,'linear');

ii=find(frm==0 & isfinite(frm) & isfinite(Rldm));
jj=find(frm==1 & isfinite(frm)  & isfinite(Rldm));

figure;plot(jdm(ii),Rldm(ii),'.',jdm(jj),Rldm(jj),'x',jdm,frm*100);set(gca,'fontsize',13);axis([306 350 0 300]);
xlabel('Year day (1997)');ylabel('Downward IR Flux (W/m^2) ; Cloud Fraction (%)');
%title('Red line=cloud fraction  Blue dots=Rld clear sky');

j=1;clear jj1;clear n1;clear n0;clear j1m;clear j0m;clear Rl0dm;clear Rl1dm;clear hbm;clear fm;
clear Rl0um;clear Rl1dm;clear Rlalld;clear Rlallu;
du=20; 
for i=300:du:640
 jj=find(jdm>i & jdm<i+du & isfinite(Rldm)& frm==0 );
 [a n0(j)]=size(jj);
 Rl0dm(j)=mean(Rldm(jj));
 Rl0um(j)=mean(Rlum(jj));
 kk=find(isfinite(rlcler(jj)));
 Rl0dcm(j)=mean(rlcler(jj(kk)));
 Rl0ucm(j)=mean(rucler(jj(kk)));

 j0m(j)=mean(jdm(jj));
 
   
 jj=find(jdm>i & jdm<i+du & isfinite(Rldm)& isfinite(Rlum) & frm==1 );
 [a n1(j)]=size(jj);
 Rl1dm(j)=mean(Rldm(jj));
 Rl1um(j)=mean(Rlum(jj));
 kk=find(isfinite(rlcler(jj)));
 Rl1dcm(j)=mean(rlcler(jj(kk)));
 Rl1ucm(j)=mean(rucler(jj(kk)));

 j1m(j)=mean(jdm(jj));
 
 
 jj=find(jdm>i & jdm<i+du & isfinite(Rldm) & isfinite(Rlum) & isfinite(frm) );
 %jj=find(utn>i & utn<i+du & isfinite(us1)& isfinite(un1) & isfinite(hs1));
  
 [a nf(j)]=size(jj);
 fm(j)=mean(frm(jj));
 Rlalld(j)=mean(Rldm(jj));
 Rlallu(j)=mean(Rlum(jj));
 kk=find(isfinite(rlcler(jj)));
 Rlalldc(j)=mean(rlcler(jj(kk)));
 Rlalluc(j)=mean(rucler(jj(kk)));


 ij=find(frm(jj)>0 & isfinite(hb(jj)));
   hbm(j)=mean(hb(jj(ij)));
   htm(j)=mean(ht(jj(ij)));

 j=j+1;
end;
Rl0um(13)=310;

figure;plot(j1m,Rl1dm,j0m,Rl0dm,'-o',j1m,Rlalldc,'--',j0m,Rl0dcm,'-kx');set(gca,'fontsize',13);axis([300 640 100 350]);xlabel('Year day (1997)');ylabel('Downward IR Flux (W/m^2)')
figure;plot(j1m,Rl1um,j0m,Rl0um,'-o',j1m,Rlalluc,'--',j0m,Rl0ucm,'-kx');set(gca,'fontsize',13);axis([300 640 100 350]);xlabel('Year day (1997)');ylabel('Upward IR Flux (W/m^2) ')

figure;plot(j1m,fm.*(Rl1dm-Rl1um-Rl0dm+Rl0um),'-o',j1m,Rlalld-Rlallu-Rl0dm+Rl0um,'-x',j1m,fm*100,[300 700],[0 0]);set(gca,'fontsize',13);axis([300 640 -00 100]);xlabel('Year day (1997)');ylabel(' Net IR Flux SCF (W/m^2); Cloud Fraction (%) ')
figure;plot(j1m,fm.*(Rl1dm-Rl1um-Rl0dcm+Rl0ucm),'-o',j1m,+Rlalld-Rlallu-Rl0dcm+Rl0ucm,'-x',j1m,fm*100,[300 700],[0 0]);set(gca,'fontsize',13);axis([300 640 -00 100]);xlabel('Year day (1997)');ylabel(' Net IR Flux SCF (W/m^2) ; Cloud Fraction (%)')

figure;plot(j1m,(Rl1dm-Rl1um-Rl0dcm+Rl0ucm),'-x',j1m,Rl1dm-Rl1um-Rl0dm+Rl0um,'-o');set(gca,'fontsize',13);axis([300 640 -00 100]);xlabel('Year day (1997)');ylabel(' Net IR Flux MSCF (W/m^2)')

%Title('Circles=f*(Clear-Cloudy)');
figure;plot(j1m,hbm,'-o',j1m,htm,'-x');set(gca,'fontsize',13);axis([300 640 0 3000]);xlabel('Year day (1997)');ylabel('Cloud Base/Top Height (m)')

j=1;clear n1;clear n0;clear j1m;clear j0m;clear Rs0dm;clear Rs1dm;
clear Rs0um;clear Rs1um;clear Rsalld;clear Rsallu;

for i=300:du:640
 jj=find(jdm>i & jdm<i+du & isfinite(Rsdm) & isfinite(Rsum) & frm==0 );
 [a n0(j)]=size(jj);
 Rs0dm(j)=mean(Rsdm(jj));
 Rs0um(j)=mean(Rsum(jj));
 Rs0cm(j)=mean(Rscl(jj));
 j0m(j)=mean(jdm(jj));
 
   
 jj=find(jdm>i & jdm<i+du & isfinite(Rsdm) & isfinite(Rsum) & frm==1 );
 [a n1(j)]=size(jj);
 Rs1dm(j)=mean(Rsdm(jj));
 Rs1um(j)=mean(Rsum(jj));
 Rs1cm(j)=mean(Rscl(jj));
 j1m(j)=mean(jdm(jj));
 
 
 jj=find(jdm>i & jdm<i+du & isfinite(Rsdm) & isfinite(Rsum) & isfinite(frm) );
 %jj=find(utn>i & utn<i+du & isfinite(us1)& isfinite(un1) & isfinite(hs1));
  
 [a nf(j)]=size(jj);
 Rsalld(j)=mean(Rsdm(jj));
 Rsallu(j)=mean(Rsum(jj));
 Rsallc(j)=mean(Rscl(jj));

 j=j+1;
end;
[a nn]=size(Rsalld);
albedo=Rsallu./Rsalld;
ii=find(isnan(albedo));
albedo(ii)=0;

j=1;clear n1;clear n0;clear j1hm;clear j0hm;clear hsm0m;clear hlm0m;
clear hsm1m;clear hsm1m;clear hlallm

for i=300:du:640
 jj=find(jdm>i & jdm<i+du & isfinite(hsm) & isfinite(hlbm) & frm==0 );
 [a n0(j)]=size(jj);
 hsm0m(j)=mean(hsm(jj));
 hlm0m(j)=mean(hlbm(jj));
 j0hm(j)=mean(jdm(jj));
 
   
 jj=find(jdm>i & jdm<i+du & isfinite(hsm) & isfinite(hlbm) & frm==1 );
 [a n1(j)]=size(jj);
 hsm1m(j)=mean(hsm(jj));
 hlm1m(j)=mean(hlbm(jj));
 j1hm(j)=mean(jdm(jj));
 
 
 jj=find(jdm>i & jdm<i+du & isfinite(hsm) & isfinite(hlbm) & isfinite(frm) );
 %jj=find(utn>i & utn<i+du & isfinite(us1)& isfinite(un1) & isfinite(hs1));
  
 [a nf(j)]=size(jj);
 hsallm(j)=mean(hsm(jj));
 hlallm(j)=mean(hlbm(jj));

 j=j+1;
end;
albedop=albedo;
bb=find(albedo==0);albedop(bb)=NaN;
figure;plot(j1m,fm*100,'-o',j1m,albedop*100,'-x');set(gca,'fontsize',13);axis([300 640 40 100]);xlabel('Year day (1997)');ylabel('Albedo ; Cloud Fraction (%)')

%figure;plot(j1m,Rs1dm,j0m,Rs0dm,'-o',j0m,Rsallc,'v');set(gca,'fontsize',13);axis([300 640 0 500]);xlabel('Year day (1997)');ylabel('Downward Sol Flux (W/m^2)')
%figure;plot(j1m,Rs1um,j0m,Rs0um,'-o',j0m,Rsallc.*albedo,'v');set(gca,'fontsize',13);axis([300 640 0 350]);xlabel('Year day (1997)');ylabel('Upward Sol Flux (W/m^2) ')

%figure;plot(j1m,fm.*(Rs1dm-Rs1um-Rs0dm+Rs0um),'-o',j1m,Rsalld-Rsallu-Rs0dm+Rs0um,'-x',j1m,fm*100,[300 650],[0 0],[300 650],[100 100]);set(gca,'fontsize',13);axis([300 640 -120 120]);xlabel('Year day (1997)');ylabel(' Net Sol Flux SCF (W/m^2) ; Cloud Fraction (%)')
%Title('Circle=f*(Clear-Cloudy');
%figure;plot(j1m,fm.*(Rs1dm-Rs1um-Rsallc+Rsallc.*albedo),'-o',j1m,Rsalld-Rsallu-Rsallc+Rsallc.*albedo,'-x',j1m,fm*100,[300 650],[0 0],[300 650],[100 100]);set(gca,'fontsize',13);axis([300 640 -120 120]);xlabel('Year day (1997)');ylabel(' Net Sol Flux SCF (W/m^2) ; Cloud Fraction (%)')
%Title('Circle=f*(Clear-Cloudy');


%Bogus in better clear sky estimates
Rs0dm(10)=270;Rs0um(10)=197;%192/270=.71
Rs0dm(11)=370;Rs0um(11)=270;%263/370=.71
%Rs0dm(13)=332;Rs0um(13)=182;%236/332=.71
%Rs0dm(14)=270;Rs0um(14)=167;

figure;plot(j1m,Rs1dm,j0m,Rs0dm,'-o',j0m,Rsallc,'v');set(gca,'fontsize',13);axis([300 640 0 500]);xlabel('Year day (1997)');ylabel('Downward Sol Flux (W/m^2)')
figure;plot(j1m,Rs1um,j0m,Rs0um,'-o',j0m,Rsallc.*albedo,'v');set(gca,'fontsize',13);axis([300 640 0 350]);xlabel('Year day (1997)');ylabel('Upward Sol Flux (W/m^2) ')

figure;plot(j1m,fm.*(Rs1dm-Rs1um-Rs0dm+Rs0um),'-o',j1m,Rsalld-Rsallu-Rs0dm+Rs0um,'-x',j1m,fm.*(Rl1dm-Rl1um-Rl0dm+Rl0um),'-o',j1m,Rlalld-Rlallu-Rl0dm+Rl0um,'-x',j1m,-(hsallm+hlallm-hsm0m-hlm0m),'-d',[300 640],[0 0]);set(gca,'fontsize',13);axis([300 640 -150 100]);xlabel('Year day (1997)');ylabel(' Net Flux Components SCF (W/m^2)')
figure;plot(j1m,fm.*(Rs1dm-Rs1um-Rsallc+Rsallc.*albedo),'-o',j1m,Rsalld-Rsallu-Rsallc+Rsallc.*albedo,'-x',j1m,fm.*(Rl1dm-Rl1um-Rl0dcm+Rl0ucm),'-o',j1m,Rlalld-Rlallu-Rl0dcm+Rl0ucm,'-x',j1m,-(hsallm+hlallm-hsm0m-hlm0m),'-d',[300 640],[0 0]);set(gca,'fontsize',13);axis([300 640 -150 100]);xlabel('Year day (1997)');ylabel(' Net Flux Compents SCF (W/m^2)')

figure;plot(j1m,(Rs1dm-Rs1um-Rsallc+albedo.*Rsallc),'-x',j1m,Rs1dm-Rs1um-Rs0dm+Rs0um,'-o');set(gca,'fontsize',13);axis([300 640 -200 00]);xlabel('Year day (1997)');ylabel(' Net Solar Flux MSCF (W/m^2)')
figure;plot(j1m,Rsalld-Rsallu-Rs0dm+Rs0um,'-o',j1m,+Rsalld-Rsallu-Rsallc+albedo.*Rsallc,'-x',j1m,fm*100,[300 700],[0 0]);set(gca,'fontsize',13);axis([300 640 -200 100]);xlabel('Year day (1997)');ylabel(' Net Solar Flux SCF (W/m^2) ; Cloud Fraction (%)')


figure;plot(j1m,Rsalld-Rsallu-Rs0dm+Rs0um+Rlalld-Rlallu-Rl0dm+Rl0um,'-o',j1m,Rsalld-Rsallu-Rsallc+Rsallc.*albedo+Rlalld-Rlallu-Rl0dcm+Rl0ucm,'-x',j1m,fm*100,[300 650],[0 0]);set(gca,'fontsize',13);axis([300 640 -100 100]);xlabel('Year day (1997)');ylabel(' Net Rad Flux SCF (W/m^2) ; Cloud Fraction (%)')

figure;plot(j1m,hsm1m,j0m,hsm0m,'-o',[300 700],[0 0]);set(gca,'fontsize',13);axis([300 640 -15 10]);xlabel('Year day (1997)');ylabel('Sensible Heat Flux (W/m^2)')
figure;plot(j1m,hlm1m,j0m,hlm0m,'-o',[300 700],[0 0]);set(gca,'fontsize',13);axis([300 640 -15 10]);xlabel('Year day (1997)');ylabel('Latent Heat Flux (W/m^2) ')

%figure;plot(j1m,-Rsalld+Rsallu+Rs0dm-Rs0um-Rlalld+Rlallu+Rl0dm-Rl0um+hsallm+hlallm-hsm0m-hlm0m,'-x',j1m,fm*100,[300 640],[0 0]);axis([300 640 -100 120]);xlabel('Year day (1997)');ylabel(' Net Total Flux SCF (W/m^2) ; Cloud Fraction (%)')
%figure;plot(j1m,-Rsalld+Rsallu+Rsallc-Rsallc.*albedo-Rlalld+Rlallu+Rl0dm-Rl0um+hsallm+hlallm-hsm0m-hlm0m,'-x',j1m,fm*100,[300 640],[0 0]);axis([300 640 -100 120]);xlabel('Year day (1997)');ylabel(' Net Total Flux SCF (W/m^2) ; Cloud Fraction (%)')
figure;plot(j1m,Rsalld-Rsallu-Rs0dm+Rs0um+Rlalld-Rlallu-Rl0dm+Rl0um-(hsallm+hlallm-hsm0m-hlm0m),'-o',j1m,Rsalld-Rsallu-Rsallc+Rsallc.*albedo+Rlalld-Rlallu-Rl0dcm+Rl0ucm-(hsallm+hlallm-hsm0m-hlm0m),'-x',j1m,fm*100,[300 640],[0 0]);set(gca,'fontsize',13);axis([300 640 -100 100]);xlabel('Year day (1997)');ylabel(' Net Total Flux SCF (W/m^2) ; Cloud Fraction (%)')

ii=4170:4270;figure;plot(jdm(ii)-.7/24,Rscl(ii),'.',jdm(ii),Rsdm(ii));set(gca,'fontsize',13);xlabel('Julian Day(1997)');ylabel('Solar Flux (W/m^2)');