% computes clear sky solar for ETL data
%jdy is julian day (decimal) at the start of average interval
%lat and lon are decimal latitude and longitude
%iv is column water vapor in cm
%   if no data is available for iv, put in NaN and select a ratio of iv to
%   near-surface water vapor mixing ratio, qrat.  Typical tropical value is
%   4.5
%calls SolarRadiance
%
close all
clear

way_raw_scs = 'y:\knorr\scs\';

% sounding PWV in ICEALOT__PWV_sonde.mat c:\data\ICEALOT\data\sonde\
ss = 'c:\data\ICEALOT\data\comparison\';


ll =145;

Rh(1:ll,1) = 70;
T(1:ll,1) = -11;
p(1:ll,1) = 1013;
q=Rh./100.*qsea(T);
iv(1:ll,1) = 1;


jjx = 1;
qrat=4.;
lond(1:ll,1) = 10;
lat = 80.1;
ct = 0;
jd = 80;

for la = 40:40:80
for i = jd:10:jd+35
min(:,1)=0:144;				  % 10-min parts of an day
jdys = i + (min/6/24);			  % 10-min fraction of day
    ct = ct + 1;
latd(1:ll,1) = la;
    
k1 = .05;                  %aerosol optical depth, band 1
k2 = .05;                  %aerosol optical depth, band 2
oz = 0.2;                  %column ozone
%%%%%% DEW 12262007


%%%%
%function y=qsea(x)
%es=6.112.*exp(17.502.*x./(x+241.0))*.98*(1.0007+3.46e-6*1010);
%y=es*622./(1010.0-.378*es);
%%%%
%QA_mean=[QA_mean qvais];
%qa=QA_mean(1:jjx);


watvap(1:length(jdys),1)=iv;
%ii=find(isnan(iv));
%watvap(ii)=qa(ii)/qrat;%total column water vapor

jdx=floor(jdys+.5/60/24); %julian day in bin centers for 10-min aves


tutc=(jdys+5/60/24-jdx)*24;%0:1:23;%hour of the day
[n m]=size(tutc);
clear sw sz saz dirs sky;
[sw,sz,saz,dirs,sky] = SolarRadiancex(latd,lond,jdx,tutc,watvap,p,k1,k2,oz);
Rscl(:,ct)=sw;
%plot(jdy,Rscl,jdy,rs,'o');xlabel('Julian Day (1999)');ylabel('Downward Solar Flux (W/m^2)');
%ss=stdt;axis([ss endt 0 1200]);

%sw = downward shortwave (W/m^2)
%sz=zenith angle
%saz = azimuth angle
%dirs is direct solar
%sky is diffuse solar
end
end % loop through lat

hr = (jdys-fix(jdys(1)))*24;
figure
subplot(2,1,1),plot(hr,Rscl(:,1:4))
%title('Clear')
ylabel('SW (W/m2')
xlabel('Hour')
%legend('40','45','50','55','60','65','70')
%legend('80','85','90','95','100','105','110','115')
ylim([-10 1000]);
xlim([0 24]);

subplot(2,1,2),plot(hr,Rscl(:,5:8))
%title('Clear')
ylabel('SW (W/m2')
xlabel('Hour')
%legend('40','45','50','55','60','65','70')
%legend('80','85','90','95','100','105','110','115')
ylim([-10 500]);
xlim([0 24]);



