function [F,G,L] = LonLatDir(Fmodel,Gmodel,Lmodel, V0,Beta0,Alpha0,ROM,LonLD,DIM,SBA)
%	Longitudinal and Lateral-Directional Partitions
%       Body or Hybrid/Stability Axes
%       12th, 6th, or 4th Order
%   July 5, 2023

%	Original 12th-order Body/Inertial State and Control Vectors
%		delx	=	del[u v w x y z p q r Phi Theta Psi]'
%		delu	=	del[dE dA dR dT dAS dF dS]'
%       delw    -   del(u_w v_w w_w)' [wind perturbation]

%   Re-Ordered in Longitudinal and Lateral-Directional Partitions
%       delx	=	del[u w x z q Theta]', del[ v y p r Phi Psi]'
%       delu	=	del[dE dT dF dS]', del[ dA dR dAS]'
%       delw    =   del(u_w w_w)', del(v_w)

%	Transformations for Longitudinal and Lateral-Directional Sets
%   =============================================================
	ReOrderState	=	[1 0 0 0 0 0 0 0 0 0 0 0
                         0 0 1 0 0 0 0 0 0 0 0 0
                         0 0 0 1 0 0 0 0 0 0 0 0
                         0 0 0 0 0 1 0 0 0 0 0 0
                         0 0 0 0 0 0 0 1 0 0 0 0
                         0 0 0 0 0 0 0 0 0 0 1 0
                         0 1 0 0 0 0 0 0 0 0 0 0
                         0 0 0 0 1 0 0 0 0 0 0 0
                         0 0 0 0 0 0 1 0 0 0 0 0
                         0 0 0 0 0 0 0 0 1 0 0 0
                         0 0 0 0 0 0 0 0 0 1 0 0
                         0 0 0 0 0 0 0 0 0 0 0 1];
	ReOrderControl	=	[1 0 0 0 0 0 0
                         0 0 0 1 0 0 0
                         0 0 0 0 0 1 0
                         0 0 0 0 0 0 1
                         0 1 0 0 0 0 0
                         0 0 1 0 0 0 0
                         0 0 0 0 1 0 0];
    ReOrderDist     =   [1 0 0
                         0 0 1
                         0 1 0];
                     
%   12th-Order RE-ORDERED Body/Inertial Model
%   ========================================
    disp('      12th-Order Model')
    disp('      RE-ORDERED in Longitudinal and Lateral-Directional Partitions')
    disp('      =============================================================')
    disp('      delx    =   del[u w x z q Theta]') 
    disp('                  del[v y p r Phi Psi]')
    disp('      delu    =   del[dE dT dF dS]') 
    disp('                  del[dA dR dAS]')
    disp('      delw    =   del[u_w w_w]') 
    disp('                  del[v_w]')

    disp('  ')
    disp('      F, G, & L for RE-ORDERED State Vector')
    disp('      =====================================')
    
    F12 =   ReOrderState * Fmodel * inv(ReOrderState)
	G12	=	ReOrderState * Gmodel * inv(ReOrderControl)
    L12 =   ReOrderState * Lmodel * inv(ReOrderDist)
    F   =   F12;
    G   =   G12;
    L   =   L12;
    disp('=======================================')
    disp('EIGENVALUES for RE-ORDERED State Vector')
    E	=	eig(F12)
    disp('=======================================')

    u0 	=	V0 * cos(Alpha0) * cos(Beta0);
	v0 	=	V0 * sin(Beta0);
	w0 	=	V0 * sin(Alpha0) * cos(Beta0);
           
%   REDUCED-ORDER MODELS
%   ====================
    if DIM ~= 12
        if LonLD == 'Lon'
%           LONGITUDINAL MODELS [6th- and 4th-Order]
%           ========================================
%           6th-Order Longitudinal Body-Axis Model
            disp(' ')
			disp('      6th-Order Longitudinal BODY-AXIS Model')
            disp('      --------------------------------------')
            disp('      xLon = del[u w q Theta x z]')
            disp('      uLon = del[dE dT dF dS]')
            disp('      wLon = del[u_w w_w]')
            T6Lon   =   [1 0 0 0 0 0
                         0 1 0 0 0 0
                         0 0 0 0 1 0
                         0 0 0 0 0 1
                         0 0 1 0 0 0
                         0 0 0 1 0 0];
            FLon6	=	T6Lon*F12(1:6,1:6)*inv(T6Lon);
			GLon6	=	T6Lon*G12(1:6,1:4);
            LLon6   =   T6Lon*L12(1:6,1:2);
 
%           6th-Order Longitudinal Hybrid/Stability-Axis Model
            disp('  ')
            disp('      6th-Order Hybrid/Stability Axes')
            disp('      -------------------------------')
            disp('      xLon = del[V Alpha q Theta x z]')
            disp('      uLon = del[dE dT dF dS]')
            disp('      wLon = del[u_w w_w]')
            T6int   =   [u0/V0 w0/V0 0 0 0 0
                         -w0/(u0^2+w0^2) u0/(u0^2+w0^2) 0 0 0 0
                         0 0 1 0 0 0
                         0 0 0 1 0 0
                         0 0 0 0 1 0
                         0 0 0 0 0 1];
            disp('      ... [Replace Theta by Gamma and re-order]')
            Fint6   =   T6int*FLon6*inv(T6int);
            Gint6   =   T6int*GLon6;
            Lint6   =   T6int*LLon6;
                        
            disp('      xLon = del[V Gamma q Alpha x z]')
            disp('      uLon = del[dE dT dF dS]')
            disp('      wLon = del[V_w Alpha_w]')
            T6hyb   =   [1 0 0 0 0 0
                         0 -1 0 1 0 0
                         0 0 1 0 0 0
                         0 1 0 0 0 0
                         0 0 0 0 1 0
                         0 0 0 0 0 1];
            TF      =   T6hyb*Fint6;         
            Tinv    =   inv(T6hyb);         
            Fhyb6   =   T6hyb*Fint6*inv(T6hyb);
            Ghyb6   =   T6hyb*Gint6;
            Lhyb6   =   T6hyb*Lint6;
            
            if DIM == 6
%               6th-Order Models [Body or Hybrid/Stability Axes]
%               ================================================
                if SBA == 'B'
%                   6th-Order Body-Axis Model
                    F   =   FLon6;
                    G   =   GLon6;
                    L   =   LLon6;
                end
                if SBA == 'H'
%                   6th-Order Hybrid/Stability-Axis Model
                    F   =   Fhyb6;
                    G   =   Ghyb6;
                    L   =   Lhyb6;
                end
            end        
            if DIM == 4
%               4th-Order Models [Body or Hybrid/Stability Axes]
%               ======================================
                if SBA == 'B'
%                   4th-Order Body-Axis Model
                    disp('  ')
                    disp('      4th-Order Body Axes from 6th-Order Body Axes')
                    disp('  	--------------------------------------------')
                    disp('      xLon = del[u w q Theta]')
                    disp('      uLon = del[dE dT dF dS]')
                    disp('      wLon = del[u_w w_w]')
                    F   =   FLon6(1:4,1:4);
                    G   =   GLon6(1:4,1:4);
                    L   =   LLon6(1:4,1:2);
                end
                if SBA == 'H'
%                   4th-Order Hybrid-Axis Model
                    disp('  ')
                    disp('      4th-Order Hybrid Axes from 6th-Order Hybrid Axes')
                    disp('      ------------------------------------------------')
                    disp('      xLon = TO:   del[V Gamma q Alpha]')
                    disp('             FROM: del[u w q Theta]')
                    disp('      uLon =       del[dE dT dF dS]')
                    disp('      wLon =       del[V_w Alpha_w]')
                    Fhyb4   =   Fhyb6(1:4,1:4);
                    Ghyb4   =   Ghyb6(1:4,1:4);
                    Lhyb4   =   Lhyb6(1:4,1:2);
                    
                    F       =   Fhyb4;
                    G       =   Ghyb4;
                    L       =   Lhyb4;
                end
            end
        end
%   ==================================================================
%   ==================================================================                            
%       LATERAL-DIRECTIONAL Model [6th-Order from 12th-Order]
        if LonLD == 'LaD'
            FLD6	=	F12(7:12,7:12);
            GLD6	=	G12(7:12,5:7);
            LLD6    =   F12(7:12,7);
            disp('  ')
			disp('      6th-Order Lateral-Directional Body-Axis Model')
            disp('      =============================================')
            disp('      xLD = TO:   del[v p r Phi Psi y]')
            disp('            FROM: del[v y p r Phi Psi]')
            disp('      uLD =       del[dA dR dAS]')
            disp('      wLD =       del[v_w]')
            T6Lat   =   [1 0 0 0 0 0
                         0 0 1 0 0 0
                         0 0 0 1 0 0
                         0 0 0 0 1 0
                         0 0 0 0 0 1
                         0 1 0 0 0 0];
            FLat6   =   T6Lat*FLD6*inv(T6Lat);
            GLat6   =   T6Lat*GLD6;
            LLat6   =   T6Lat*LLD6;
       
            if DIM <= 6
%               6th-Order Models [Body or Hybrid/Stability Axes]
%               ================================================
                if SBA == 'B'
%                   6th-Order Body-Axis Model
                    disp(' ')
                    disp('      6th-Order Body Axes')
                    disp('  	-------------------')
                    disp('      xLD = del[v p r Phi Psi y]')
                    disp('      uLD = del[dA dR dAS]')
                    disp('      wLD = del[v_w]')
                    F   =   FLat6;
                    G   =   GLat6;
                    L   =   LLat6;
                end                
                if SBA == 'H'
%                   Hybrid/Stability Axes
                    disp('  ')
                    disp('      6th-Order Hybrid/Stability Axes')
                    disp('      ------------------------')
                    disp('      xLD = TO:   del[Beta pS rS Phi Psi y]')
                    disp('            FROM: del[v p r Phi Psi y]')                    
                    disp('      uLD =       del[dA dR dAS]')
                    disp('      wLD =       del[Beta_w]')
                    T6int   =   [1/sqrt(V0^2-v0^2) 0 0 0 0 0
                                 0 cos(Alpha0) sin(Alpha0) 0 0 0
                                 0 -sin(Alpha0) cos(Alpha0) 0 0 0
                                 0 0 0 1 0 0
                                 0 0 0 0 1 0
                                 0 0 0 0 0 1];
                    Fint6   =   T6int*FLat6*inv(T6int);
                    disp('      xLD = TO:    del[rS Beta pS Phi Psi y]')
                    disp('            FROM:  del[Beta pS rS Phi Psi y]')
                    disp('      uLD =        del[dA dR dAS]')
                    disp('      wLD =        del[Beta_w]')
                    T6Stab  =   [0 0 1 0 0 0
                                 1 0 0 0 0 0
                                 0 1 0 0 0 0
                                 0 0 0 1 0 0
                                 0 0 0 0 1 0
                                0 0 0 0 0 1];                
                    FStab6  =   T6Stab*Fint6*inv(T6Stab);
                    GStab6  =   T6Stab*GLat6;
                    LStab6  =   T6Stab*LLat6;
                    F       =   FStab6;
                    G       =   GStab6;
                    L       =   LStab6;
                end
            end
                
            if DIM == 4
%               4th-Order Models [Body or Hybrid/Stability Axes]
%               ================================================
                if SBA == 'B'
%                   4th-Order Body-Axis Model
                    disp('  ')
                    disp('      4th-Order Body Axes from 6th-Order')
                    disp('      ----------------------------------')
                    disp('      xLD = del[v p r Phi]')
                    disp('      uLD = del[dA dR dAS]')
                    disp('      wLD = del[v_w]')
                    F   =   FLat6(1:4,1:4);
                    G   =   GLat6(1:4,1:3);
                    L   =   LLat6(1:4,1);
                end
                if SBA == 'H'
%                   4th-Order Hybrid/Stability-Axis Model
                    disp('  ')
                    disp('      4th-Order Stability/Hybrid Axes from 6th-Order')
                    disp('      ----------------------------------------------')
                    disp('      xLD = TO:   del[rS Beta pS Phi]')
                    disp('            FROM: del[rS Beta pS Phi Psi y]')
                    disp('      uLD = del[dA dR dAS]')
                    disp('      wLD = del[Beta_w]')
                    FStab4  =   FStab6(1:4,1:4);
                    GStab4  =   GStab6(1:4,1:3);
                    LStab4  =   LStab6(1:4,1);
                    F       =   FStab4;
                    G       =   GStab4;
                    L       =   LStab4;
                end
            end
        end
    end
