% 120426: modified from getModisSwath.m
% 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. 


function plot_userAer_swathMODIS()
   
   numOth = 3; %lat, lon, elv  %v3.0 has elv as one more output.
   numParm = 5; %Flxall(5,3)
   numIntv = 3; %Flxall(5,3)
   MISSVAL = -9999.;

    
fullname =['../results/2015/aqua/',...
    'modis.terra.2015243.1845.dat'];
   %! * Flxall(Ip,Iv) : Ip=1 to Nfpar, Iv=1 to Nintv, all-sky flux;
   %!      Ip=1, top of atmosphere down-flux
   %!      Ip=2, top of atmosphere up-flux
   %!      Ip=3, surface down-flux
   %!      Ip=4, surface up-flux
   %!      Ip=5, diffuse surface down-flux
   %
   %!      Iv=1, shortwave
   %!      Iv=2, visible (PAR)
   %!      Iv=3, near infrared
   %!   Flxclr(Ip,Iv) : as Flxall but clear-sky flux
   %
   fid = fopen(fullname, 'r');
   NX = fread(fid,1,'int32');
   NY = fread(fid,1,'int32');
   dM = fread(fid,(numOth+numParm*numIntv*2)*NX*NY,'float32'); %new data has "elv" at pos 3
   fclose(fid);
   dM(dM<-900)=NaN;
   
   dM = reshape(dM, [(numOth+numParm*numIntv*2),NX,NY]);
   latM = squeeze(dM(1,:,:));
   lonM = squeeze(dM(2,:,:));
   elvM = squeeze(dM(3,:,:)); %new output for v3.0 
   dM = dM(numOth+1:end,:,:);
   dM = reshape(dM, [numParm,numIntv,2,NX,NY]);
   
   fdnM = squeeze(dM(3,1,1,:,:));
   fupM = squeeze(dM(4,1,1,:,:));

   m1.lat = latM;
   m1.lon = lonM;
   m1.fdn = fdnM;
   m1.fup = fupM;
   imagesc(fdnM)

keyboard
end