
% read MODIS (5-km) swath outputs
% The file name is designated by the start time of 5-minutes swath. 
% The file is in binary format. 
% The geo-location information and flux for every pixel within a 5-minutes swath 
% are stored in the order of latitude (270*406), longitude (270*406) 
% and radiative flux (270*406). 
% Usually, the output grid size is 270*406 pixels. 
% However, there are swaths that are of 270*408 pixels or 270*410 pixels.
% Those cases are denoted by the additional suffix "_408" or "_410" to
% the file names. 
addpath('/data/srb1/wchen/tools/m_fx//','-end')
import matlab.io.hdf4.* 
clear all;clc; 
   modis = 'aqua';%'terra';% 
   if strcmp(modis,'terra') satStr='MOD';
   elseif strcmp(modis,'aqua')  satStr='MYD';
   end
   plttype = "hist";
   year  = 2015;
   yyyy = num2str(year, '%04d');
   inpath0 = ['/data/srb1/wchen/WORK/MODIS/SRB_SW/case_5km_cp_run/input/'];
   dataPath1 = ['/data/srb1/wchen/WORK/MODIS/SRB_SW/case_5km_cp_run/results/'];
   filepath1 = [dataPath1,'/',yyyy,'/',modis,'/'];

   
   dataPath2 = ['/data/srb9/5kmMODIS/Results/'];
   filepath2 = [dataPath2,'/',yyyy,'/'];
   
   fluxpath=['/data/srb9/MODIS/C7/cloud/',yyyy,'/'];
   cnt = 0
   for doi=182:212
       doy   = num2str(doi, '%03d');
       datev = datevec(doy2date(doi,year))
       fptn  = [filepath1,'modis.',modis,'.',yyyy,doy,'.*.dat'];
       fList = dir(fptn);
       nFl   = length(fList);
   
      if nFl==0 
         disp('---read_MODIS_swath_new(): Empty directory!')       
      else
         for i=1:nFl
            cnt = cnt+1;
            hhmm  = fList(i).name(end-7:end-4); 
            hrmin = str2num(hhmm);
            hor=str2num(hhmm(1:2));mnt=str2num(hhmm(3:4));
						listF = dir([fluxpath,'/',satStr,'06_L2.A',yyyy,doy,'.',hhmm,'.*.hdf']);
    				nFiles = length(listF);
    			if nFiles~=1 then
    				disp('nFiles ~= 1');keyboard;
    			else
               fileName = listF.name;
               disp(['nasa flux... ',fileName])      
               %-- read the MODIS swath  data
               %
               fullName = [fluxpath, fileName];
               hdfID = sd.start(fullName,'read');

               varMcld = obtain_hdf_datafield(...
                              hdfID,'Downwelling_SW_Cloudy_Flux_At_Surface', 'doScl','doNaN');
               varMcld(varMcld<-9000)=0.;
               varMclc =   obtain_hdf_datafield(...
                              hdfID,'Downwelling_SW_Flux_At_Surface', 'doScl','doNaN');
               varMflx = varMcld+varMclc;
               sd.close(hdfID); %need for WRITTING. 
               end %if nFiles
               %keyboard
            [ds2] = read_MODIS_swath_v40(...
                     modis, filepath1, fList(i).name);
            data2 = ds2.varM;
            data2c = ds2.varC;
            [ds1] = read_MODIS_swath_v40(...
                     modis, filepath2, fList(i).name);
            data1 = ds1.varM;
            data1c = ds1.varC;
            [cldLat,cldLon,...
                     ctp, wfrc,wcod,wcre, ifrc,icod,icre, tfrc ] =...
                    get_cloud(...
                              year, datev(2), datev(3), hrmin, doi, modis, inpath0); %0:clear; >0: cloud
            if doi==194 && hrmin==1800 
                keyboard
            end
            if i==1 && doi==182
               c7flx = data2(:);
               c7flxc = data2c(:);

               c6flx = data1(:);
               c6flxc = data1c(:);

               flxcld = varMcld(:);
               flxclc = varMclc(:);

               flx = varMflx(:);

               clds = tfrc(:);
            else              
               c7flx = [c7flx;data2(:)];
               c7flxc = [c7flxc;data2c(:)];

               c6flx = [c6flx;data1(:)];
               c6flxc = [c6flxc;data1c(:)];
            
               flxcld = [flxcld;varMcld(:)];
               flxclc = [flxclc;varMclc(:)];

               flx = [flx;varMflx(:)];
               clds = [clds;tfrc(:)];
            end

            % figure()
            % imagesc(ds2.varM)
            % figure()
            % imagesc(ds1.varM)
            % figure()
            % imagesc(tfrc)
%             keyboard
            clear data1 data1c data2 data2c varMcld varMclc varMflx tfrc;
         end%nFL
      end %if nFL==0
	end % doi
  
   
   keyboard