% Delivered to the Government in Accordance with Contract FA8650-10-C-7080
% 
%  (c) 2011 United Technologies Corp.
% Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
%  
% Permission is hereby granted, free of charge, to any person obtaining a copy of this data, including any software or models in source or binary form, as well as any drawings, specifications, and documentation (collectively "the Data"), to deal in the Data without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data is furnished to do so, subject to the following conditions:
%  
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Data.
%  
% THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE USE OR OTHER DEALINGS IN THE DATA.
% 

% This script reads a turbine map and bisects it into choked and unchoked
% sections.  It does so by finding coordinates at which the speed lines
% become vertical

Res = max(size(Mass_LPT));
tolerance = 0.01;
c1 = 0;
c2 = 0;
uc1 = 0;
uc2 = 0;

for s = 1:Res
    for b = 2:Res
    
        if abs((Mass_LPT(s,b)-Mass_LPT(s,b-1))/(Beta_LPT(b)-Beta_LPT(b-1)))>tolerance
           choke_beta_LPT(s) = Beta_LPT(b);  % Beta coordinate of choke
           choke_speed_LPT(s) = Speed_LPT(s); % Speed coordinat of choke
        end      
        
    end
end

for s = 1:Res
    for b = 1:Res
        
        
        if Speed_LPT(s)<= (max(Speed_LPT) + min(Speed_LPT))/2 && Beta_LPT(b) >= 0.5 
            
            c1= c1+1;
            Beta_LPT_c1(c1) = Beta_LPT(b);
            Speed_LPT_c1(c1) = Speed_LPT(s);
            Mass_LPT_c1(c1) = Mass_LPT(s,b);      % Choked Section
            PR_LPT_c1(c1) = PR_LPT(s,b);
            Efficiency_LPT_c1(c1) = Efficiency_LPT(s,b);
       
        
        elseif Speed_LPT(s) >= (max(Speed_LPT) + min(Speed_LPT))/2 && Beta_LPT(b) >= 0.5
            
            c2= c2+1;
            Beta_LPT_c2(c2) = Beta_LPT(b);
            Speed_LPT_c2(c2) = Speed_LPT(s);
            Mass_LPT_c2(c2) = Mass_LPT(s,b);      % Choked Section
            PR_LPT_c2(c2) = PR_LPT(s,b);
            Efficiency_LPT_c2(c2) = Efficiency_LPT(s,b);            
            
        elseif Speed_LPT(s)<= (max(Speed_LPT) + min(Speed_LPT))/2 && Beta_LPT(b) <= 0.5 
        
            uc1= uc1+1;
            Beta_LPT_uc1(uc1) = Beta_LPT(b);
            Speed_LPT_uc1(uc1) = Speed_LPT(s);
            Mass_LPT_uc1(uc1) = Mass_LPT(s,b);    % Unchoked Section
            PR_LPT_uc1(uc1) = PR_LPT(s,b);
            Efficiency_LPT_uc1(uc1) = Efficiency_LPT(s,b);
        
        elseif  Speed_LPT(s) >= (max(Speed_LPT) + min(Speed_LPT))/2 && Beta_LPT(b) <= 0.5
        
            uc2= uc2+1;
            Beta_LPT_uc2(uc2) = Beta_LPT(b);
            Speed_LPT_uc2(uc2) = Speed_LPT(s);
            Mass_LPT_uc2(uc2) = Mass_LPT(s,b);    % Unchoked Section
            PR_LPT_uc2(uc2) = PR_LPT(s,b);
            Efficiency_LPT_uc2(uc2) = Efficiency_LPT(s,b);
      
        end
        
        
    end
end
