%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PURPOSE: Cuts scan boxes through TSDIS 2A_25 data. It first call a file
%   (TClocation) containing Lat/Lon positions of TC. Then opens
%   filename.HDF and using info from TClocation, cut a box of PR reflectivity
%   data out of it. Then copies this box to a new file called filename.cut
%INPUT: TClocation text file; filename.HDF
%
%OUTPUT: reflectivity data window around the TC center saved
%   filename.cut in binary format
%
%
%NOTE: MatLab version of cutprocess.cc written by ML
% Created: March 20, 2007 KMV
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
% Global Variables
NAME_MAX=80; ORBIT_MAX=5000; ORBITS=1;

% Reading a TC location text file.
[yr,mn,dy,tym,lat,lon,dist,orb,name]=textread(...
    '/home/meteo/abh5000/non_RI_Atlantic.txt',...
    '%q %q %q %q %q %q %q %q %q');

ORBLST=struct('Year',yr,'Month',mn,'Day',dy,'Time',tym,'Lat',lat,...
    'Lon',lon,'Orbit',orb,'Storm',name);

% Local variables
boxsize=2.0;
npixel=49;
count=(1:44);
for m=15
    scancenter=0;
% Creates the .HDF filename
    if (str2num(char(ORBLST(m).Year))<10)
        if (str2num(char(ORBLST(m).Month))<10)
            if (str2num(char(ORBLST(m).Day))<10)
                bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.0',...
                    num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),'0',...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
            else
                bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.0',...
                    num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
            end
        else if (str2num(char(ORBLST(m).Day))<10)
                 bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.0',...
                    num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),'0',...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
            else
                 bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.0',...
                    num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
            end
        end
    else
        if (str2num(char(ORBLST(m).Month))<10)
                if (str2num(char(ORBLST(m).Day))<10)
                    bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.',...
                    num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),'0',...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
                else
                    bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.',...
                    num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
                 end
        else if    (str2num(char(ORBLST(m).Day))<10)
                    bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.',...
                    num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),'0',...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
               else
                    bufferA=strcat('/home/meteo/abh5000/HDFfiles/2A25.',...
                    num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),...
                    num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),...
                    '.6.HDF');
            end
        end
    end
    
    % Determination of eye scan by creating a box around the TClocation
    % lon/lat positions
    latmin=str2num(ORBLST(m).Lat)-boxsize;
    latmax=str2num(ORBLST(m).Lat)+boxsize;
    lonmin=str2num(ORBLST(m).Lon)-boxsize;
    lonmax=str2num(ORBLST(m).Lon)+boxsize;

% Determination of the scan line limits
    nscan=findorbsz(bufferA);
    datascan=sdsread(bufferA);
    
    for scan=1:nscan
        data=squeeze(datascan.geolocation(scan,:,:));
        if ((str2num(ORBLST(m).Lon))>(180-boxsize)) || ((str2num(ORBLST(m).Lon))<(-180+boxsize))
            lonmin=180-boxsize; lonmax=179.9999;
            for pixel=1:npixel
                if ((latmin<data(pixel,1)) && (data(pixel,1)<latmax) && ...
                        (lonmin<data(pixel,2)) || (data(pixel,2)<lonmax))
                    scancenter=scan;
                    break;
                end
            end
            break;
%         else if ((str2num(ORBLST(m).Lon))<(-180+boxsize))
%                 lonmin=-179.9999; lonmax=-180+boxsize;
%                 for pixel=1:npixel
%                     if ((latmin<data(pixel,1)) &&amp! ; (data( pixel,1)<latmax) && ...
%                         (lonmin<data(pixel,2)) && (data(pixel,2)<lonmax))
%                             scancenter=scan;
%                             break;
%                     end
%                 end
%                 break;
            else
                for pixel=1:npixel
                    if ((latmin<data(pixel,1)) && (data(pixel,1)<latmax) && ...
                        (lonmin<data(pixel,2)) && (data(pixel,2)<lonmax))
                            scancenter=scan;
                            count(43)=scancenter;
                    end
                end
            %end
        end
        if scancenter~=0
            break;
        end
    end
    
    if scancenter~=0;
        scancenter=scancenter-1;
        
        if scancenter<200
            scanmin=1;
        else if scancenter >(nscan-200)
                scanmin=nscan-401;
            else
                scanmin=scancenter-200;
                scanmax=scancenter+200;
            end
        end
    
        for i=1:80
            for scan=1:401
                dataZ=squeeze(datascan.correctZFactor((scan+(scanmin)),:,:));
                dataL=squeeze(datascan.geolocation((scan+(scanmin)),:,:));
                dataZ=double(dataZ);
                dataL=double(dataL);
                for pixel=1:npixel
                    Zvalue(pixel,scan,i)=(dataZ(pixel,i))./100;
                end
                for pixel=(npixel+1):98
                    Zvalue(pixel,scan,i)=dataL((pixel-49),1);
                end
                for pixel=99:147
                    Zvalue(pixel,scan,i)=dataL((pixel-98),2);
                end
            end
        end
        
         if (str2num(char(ORBLST(m).Year))<10)
            if (str2num(char(ORBLST(m).Month))<10)
                if (str2num(char(ORBLST(m).Day))<10)
                    bufferB=strcat(...
                        '/home/meteo/abh5000/HDFfiles/2A25.0',...
                        num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),'0',...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                else
                    bufferB=strcat(...
                        '/home/meteo/abh5000/HDFfiles/2A25.0',...
                        num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                end
            else if (str2num(char(ORBLST(m).Day))<10)
                     bufferB=strcat(...
                       '/home/meteo/abh5000/HDFfiles/2A25.0',...
                        num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),'0',...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                else
                     bufferB=strcat(...
                        '/home/meteo/abh5000/HDFfiles/2A25.0',...
                        num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                end
            end
        else if (str2num(char(ORBLST(m).Month))<10)
                    if (str2num(char(ORBLST(m).Day))<10)
                        bufferB=strcat(...
                        '/home/meteo/abh5000/HDFfiles/2A25.',...
                        num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),'0',...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                    else
                        bufferB=strcat(...
                        '/hone/meteo/abh5000/HDFfiles/2A25.',...
                        num2str(char(ORBLST(m).Year)),'0',num2str(char(ORBLST(m).Month)),...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                    end
    
             else  if (str2num(char(ORBLST(m).Day))<10)
                        bufferB=strcat(...
                        '/home/meteo/abh5000/HDFfiles/2A25.',...
                        num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),'0',...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                   else
                        bufferB=strcat(...
                        '/home/meteo/abh5000/HDFfiles/2A25.',...
                        num2str(char(ORBLST(m).Year)),num2str(char(ORBLST(m).Month)),...
                        num2str(char(ORBLST(m).Day)),'.',num2str(char(ORBLST(m).Orbit)),'.',char(ORBLST(m).Storm),...
                        '.6Z80levels.txt');
                   end
            end
         end
         
         fid=fopen(bufferB,'w');
         
         hgt=80;
         for pixel=1:49
             fprintf(fid,'%10d',-20);
             for scan=1:401
                 fprintf(fid,'%10.2f',Zvalue((pixel+98),scan,hgt));
             end
             fprintf(fid,'\n');
         end
         
         for pixel=1:49
             fprintf(fid,'%10d',-10);
             for scan=1:401
                 fprintf(fid,'%10.2f',Zvalue((pixel+49),scan,hgt));
             end
             fprintf(fid,'\n');        
         end
         
         while hgt>0
            for pixel=1:49
           fprintf(fid,'%10d',(81-hgt));
                for scan=1:401
                    fprintf(fid,'%10.2f',Zvalue((pixel),scan,hgt));
                end
            fprintf(fid,'\n');
            end
            hgt=hgt-1;
         end
     else
         disp('DOES NOT MATCH BOX CONDITIONS')
         break;
     end      
end

