clear 
clc

% m = aerosol models 
% m1= Marine m2= Smoke
% Wind Speed 1
%**********************************************

B=2; 

cd('/ships19/hercules/mino/6SLUT/AHI_6sV2.1.2/6SV2.1_AHI_core/output/')



Spherical_albedo_Ray_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN; % 1
Spherical_albedo_Aer_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
Spherical_albedo_total_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

Scat_Angle_B2_ahi_v212(1:10,1:15,1:21)=NaN; % 4

Water_Refl_Foam_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%5
Water_Refl_Water_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
Water_Refl_Glint_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

BRDF_rodir_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%8
BRDF_robar_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
BRDF_ropbar_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
BRDF_robarbar_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
BRDF_Albedo_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

Apparent_Refl_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%13


Atm_intrin_refl_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%14
Background_refl_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
Pixel_refl_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

Ray_scat_trans_dn_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%17
Ray_scat_trans_up_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
Ray_scat_trans_total_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

Aer_scat_trans_dn_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%20
Aer_scat_trans_up_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
Aer_scat_trans_total_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

Total_scat_trans_dn_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%23
Total_scat_trans_up_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
Total_scat_trans_total_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

Rayle_OD_total_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%26
Aer_OD_total_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;
Total_OD_total_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;

Reflectance_rayleigh_I_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN;%29
Reflectance_aerosol_I_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN; 
Reflectance_total_I_B2_ahi_v212(1:10,1:15,1:21,1:4,1:8,1:9)=NaN; 


   % OB2M03A07_v212.txt 
for M=1:9

    for aod=1:8
        
    dataview=[ M aod]  
    
    if(M>1&&aod==1) % the same rayleigh reflectance for different aerosol model 
    fid=fopen(['OB',num2str(B),'M01','A',num2str(aod,'%02i'),...
                       '_v212.txt'],'r'); 
    
    else
     
     fid=fopen(['OB',num2str(B),'M',num2str(M,'%02i'),'A',num2str(aod,'%02i'),...
                       '_v212.txt'],'r');
        
    end
        for wsi=1:4
              for raz=1:21
                   for vza=1:15 
                       for sza=1:10  
        
               
               
               
                   
                   
                 OutputText=textscan(fid,'%s',161,'delimiter','\n');  % Read strings delimited
                                                  % by a carriage return
                                                  
                 textfile=OutputText{1};
                 
                 header1='******************************* 6SV version 2.1 *******************************';
                 
                 k=1;
                 
                 while(length(textfile{k})~=79)
                     
                 
                     k=k+1;
               
                 end
                 
%                  G=textfile{k};
%                  
%                  (G==header1)
%                 
                 
                 Temp=textfile{9+k};
                 Scat_Angle_B2_ahi_v212(sza,vza,raz)=str2double(Temp(24:32));

                 
                 Temp=textfile{45+k};
                 Water_Refl_Foam_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(20:29));
                 Water_Refl_Water_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(41:50));
                 Water_Refl_Glint_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(61:72));
                 
%                  
                 Temp=textfile{49+k};
                 
                 BRDF_rodir_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(19:25));
                 BRDF_robar_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(29:35));
                 BRDF_ropbar_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(39:45));
                 BRDF_robarbar_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(48:55));
                 BRDF_Albedo_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(57:66));
                 
                 
                 Temp=textfile{65+k};
             
                 Apparent_Refl_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(30:40));
                 
                 
                 Temp=textfile{92+k};
                 
                 Atm_intrin_refl_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(17:21));
                 Background_refl_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(37:42));
                 Pixel_refl_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(58:62));
                 
                 
                 Temp=textfile{129+k};
                 Ray_scat_trans_dn_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(30:40)); 
                 Ray_scat_trans_up_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(45:55)); 
                 Ray_scat_trans_total_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(61:71));
                 
                 Temp=textfile{130+k};
                 Aer_scat_trans_dn_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(30:40)); 
                 Aer_scat_trans_up_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(45:55)); 
                 Aer_scat_trans_total_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(61:71));
                 
                 
                 Temp=textfile{131+k};
                 Total_scat_trans_dn_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(30:40)); 
                 Total_scat_trans_up_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(45:55)); 
                 Total_scat_trans_total_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(61:71));
                 
                 Temp=textfile{137+k};
                 Spherical_albedo_Ray_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(30:40)); 
                 Spherical_albedo_Aer_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(45:55)); 
                 Spherical_albedo_total_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(61:71));
                 
                 Temp=textfile{138+k};
                 Rayle_OD_total_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(30:40)); 
                 Aer_OD_total_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(45:55)); 
                 Total_OD_total_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(61:71));
                 
                 Temp=textfile{140+k};
                 Reflectance_rayleigh_I_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(30:40));
                 Reflectance_aerosol_I_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(45:55)); 
                 Reflectance_total_I_B2_ahi_v212(sza,vza,raz,wsi,aod,M)=str2double(Temp(61:71));
        
                       end
                   end
              end
        end
       fclose(fid);
    end
end

save Apparent_Refl_B2_ahi_v212 Apparent_Refl_B2_ahi_v212

save Spherical_albedo_Ray_B2_ahi_v212 Spherical_albedo_Ray_B2_ahi_v212 % 1
save Spherical_albedo_Aer_B2_ahi_v212 Spherical_albedo_Aer_B2_ahi_v212
save Spherical_albedo_total_B2_ahi_v212 Spherical_albedo_total_B2_ahi_v212

save Scat_Angle_B2_ahi_v212 Scat_Angle_B2_ahi_v212 %4

save Water_Refl_Foam_B2_ahi_v212 Water_Refl_Foam_B2_ahi_v212 % 5
save Water_Refl_Water_B2_ahi_v212 Water_Refl_Water_B2_ahi_v212
save Water_Refl_Glint_B2_ahi_v212 Water_Refl_Glint_B2_ahi_v212

save BRDF_rodir_B2_ahi_v212 BRDF_rodir_B2_ahi_v212%8
save BRDF_robar_B2_ahi_v212 BRDF_robar_B2_ahi_v212
save BRDF_ropbar_B2_ahi_v212 BRDF_ropbar_B2_ahi_v212
save BRDF_robarbar_B2_ahi_v212 BRDF_robarbar_B2_ahi_v212
save BRDF_Albedo_B2_ahi_v212 BRDF_Albedo_B2_ahi_v212

save Apparent_Refl_B2_ahi_v212 Apparent_Refl_B2_ahi_v212 %13

save Atm_intrin_refl_B2_ahi_v212 Atm_intrin_refl_B2_ahi_v212%14
save Background_refl_B2_ahi_v212 Background_refl_B2_ahi_v212
save Pixel_refl_B2_ahi_v212 Pixel_refl_B2_ahi_v212

save Ray_scat_trans_dn_B2_ahi_v212 Ray_scat_trans_dn_B2_ahi_v212 % 17
save Ray_scat_trans_up_B2_ahi_v212 Ray_scat_trans_up_B2_ahi_v212
save Ray_scat_trans_total_B2_ahi_v212 Ray_scat_trans_total_B2_ahi_v212

save Aer_scat_trans_dn_B2_ahi_v212 Aer_scat_trans_dn_B2_ahi_v212 %20
save Aer_scat_trans_up_B2_ahi_v212 Aer_scat_trans_up_B2_ahi_v212
save Aer_scat_trans_total_B2_ahi_v212 Aer_scat_trans_total_B2_ahi_v212

save Total_scat_trans_dn_B2_ahi_v212 Total_scat_trans_dn_B2_ahi_v212 %23
save Total_scat_trans_up_B2_ahi_v212 Total_scat_trans_up_B2_ahi_v212
save Total_scat_trans_total_B2_ahi_v212 Total_scat_trans_total_B2_ahi_v212

save Rayle_OD_total_B2_ahi_v212 Rayle_OD_total_B2_ahi_v212 % 26
save Aer_OD_total_B2_ahi_v212 Aer_OD_total_B2_ahi_v212
save Total_OD_total_B2_ahi_v212 Total_OD_total_B2_ahi_v212

save Reflectance_rayleigh_I_B2_ahi_v212 Reflectance_rayleigh_I_B2_ahi_v212 %29
save Reflectance_aerosol_I_B2_ahi_v212 Reflectance_aerosol_I_B2_ahi_v212
save Reflectance_total_I_B2_ahi_v212 Reflectance_total_I_B2_ahi_v212




