% Run single scattering approximation model for radiative transfer problem
clear
close all
lambda=[400 500 675 870 1020];
year=2016;
month=4;
day=19;
time=10;
[az,el]=sunpos(year,day2jd(year,month,day),time,52,21);
zen=90-el;
%az=180;
% Aerosol optical depth, see MieOptics.m for definition of aerosol properties
AOT=0.21;

for i=1:length(lambda)
    disp(i)
   [X,Y,I(:,:,i),Idir(i),x0,y0,F,T,I0(i)]=singlescattering(lambda(i),zen,az,AOT);
end    
r=squeeze(I(:,:,1));
teta=T(1,:)*180/pi;
fi=F(:,1)*180/pi;
[a,indT]=min(abs(teta-zen));
[a,indF]=min(abs(fi-az));

%h=plot(x0,y0,'oy')
%set(h,'MarkerFaceColor','y') 
pcolor(X,Y,log(r)); shading interp; 
%colormap(map);
axis square
set(gca,'color',[0.8 0.8 0.8])
set(gca,'Visible','off')

figure
Isky=squeeze(I(:,indT,:));
Isky(indF,:)=Isky(indF,:)+Idir;
semilogy(fi-az,Isky);

FI=fi-az;
ind=find(FI>=0);
Idif=squeeze(I(ind,indT,:));
FI=FI(ind);
ind=[1:5:10,15:10:length(FI)];
FI=FI(ind);
Idif=Idif(ind,:);
DATA.FOV=1;
domega=pi*(DATA.FOV/2*pi/180)^2;
Idif=Idif*domega;
Idif(1,:)=Idif(1,:)+Idir;
RelAz=[0 2.0300     3.0400     4.0600     5.0700     7.1000    10.1400    15.2100...
   20.2800    25.3500    30.4200    40.5700    50.7400    60.9100    71.1100...
   81.3300    91.5900   101.9000   112.2800   122.7900   133.5000   144.5800   156.6000   173.4300];
Idif=interp1(FI,Idif,RelAz);

DATA.site='ALOMAR';
DATA.year=year;
DATA.month=month;
DATA.day=day;
DATA.time=time;
DATA.lon=21;
DATA.lat=50;
DATA.alt=0;
DATA.psurf=1013;
DATA.DU=300;
DATA.ALB=0.1*ones(size(lambda));
DATA.Ref=complex(1.5,0.002)*ones(size(lambda));
DATA.scantype='A';
DATA.lambda=lambda;
DATA.zen=zen;
DATA.az=az;
DATA.Rad=Idif;
DATA.RelAz=RelAz;   %   FI;
COS=cos(pi/180*zen)*cos(pi/180*zen)+sin(pi/180*zen)*sin(pi/180*zen)*cos(pi/180*FI);
DATA.teta=180/pi*acos(COS);
DATA.ScatMax=max(DATA.teta);

CALIB.year=DATA.year;
CALIB.month=DATA.month;
CALIB.day=DATA.day;
CALIB.time=DATA.time;
CALIB.lambda=DATA.lambda;
CALIB.F0=I0;
save TESTscm DATA CALIB