function [J_Pflow,J_Qflow,err_Pflow,err_Qflow] = form_J_flow_polar(line_pi,VV,m_Pflow,m_Qflow)
% Syntax:    [J_Pflow,J_Qflow,err_Pflow,err_Qflow] = 
% form_J_flow_polar(line_pi,VV,m_Pflow,m_Qflow)
% 
% Purpose:   To form the measurement jacobian as well as error corresponding
%            to conventional real and reactive power flow measurement 
%            w.r.t. voltage angle and magnitude in order
%
% Input:    line_pi       - System line data
%           VV            - Voltage vector in complex form
%           m_Pflow       - Pflow measurement information matrix
%           m_Qflow       - Qflow measurement information matrix
%                      
% Output:   J_Pflow    - Jacobian corresponding to conventional real power 
%                        flow measurement
%           J_Qflow    - Jacobian corresponding to conventional reactive
%                        power flow measurement
%           err_Pflow  - error corresponding to conventional real power 
%           err_Qflow  - error corresponding to conventional reactive power
%
% Calls:     
% Called by SE_WLS
%
nob=length(VV(:,1));
Pflowindex=m_Pflow(:,1);%Real power flow measurement index vector
Qflowindex=m_Qflow(:,1);%Reactive power flow measurement index vector
VM=abs(VV);VA=angle(VV);

err_Pflow=[];
J21=zeros(length(Pflowindex),nob);%wrt voltage angle
J22=zeros(length(Pflowindex),nob);%wrt voltage magnitude 
if isempty(Pflowindex)==0    
    for i=1:length(Pflowindex)
        k=Pflowindex(i);%Line number  
        m=line_pi(k,1);
        n=line_pi(k,2);
        g_t=line_pi(k,5);
        b_t=line_pi(k,6);
        gi_t=line_pi(k,3);
               
        Pmn=VM(m)^2*(g_t+gi_t)-VM(m)*VM(n)*(g_t*cos(VA(m)-VA(n))+b_t*sin(VA(m)-VA(n)));
            
        J21(i,m)=VM(m)*VM(n)*(g_t*sin(VA(m)-VA(n))-b_t*cos(VA(m)-VA(n)));                                
        J21(i,n)=-VM(m)*VM(n)*(g_t*sin(VA(m)-VA(n))-b_t*cos(VA(m)-VA(n)));
        
        J22(i,m)=2*VM(m)*(g_t+gi_t)-VM(n)*(g_t*cos(VA(m)-VA(n))+b_t*sin(VA(m)-VA(n)));
        J22(i,n)=-VM(m)*(g_t*cos(VA(m)-VA(n))+b_t*sin(VA(m)-VA(n)));                    
            
        [err_Pflow]=[err_Pflow;m_Pflow(i,2)-Pmn];
    end
end
J_Pflow=[J21 J22];
%===================================================================

err_Qflow=[];
J41=zeros(length(Qflowindex),nob);
J42=zeros(length(Qflowindex),nob);
if isempty(Qflowindex)==0    
    for i=1:length(Qflowindex)
        k=Qflowindex(i);%Line number
        m=line_pi(k,1);
        n=line_pi(k,2);
        g_t=line_pi(k,5);
        b_t=line_pi(k,6);
        bi_t=line_pi(k,4);
        
        Qmn=-VM(m)^2*(b_t+bi_t)-VM(m)*VM(n)*(g_t*sin(VA(m)-VA(n))-b_t*cos(VA(m)-VA(n)));
            
        J41(i,m)=-VM(m)*VM(n)*(g_t*cos(VA(m)-VA(n))+b_t*sin(VA(m)-VA(n)));
        J41(i,n)=VM(m)*VM(n)*(g_t*cos(VA(m)-VA(n))+b_t*sin(VA(m)-VA(n)));
        
        J42(i,m)=-VM(m)*2*(b_t+bi_t)-VM(n)*(g_t*sin(VA(m)-VA(n))-b_t*cos(VA(m)-VA(n)));
        J42(i,n)=-VM(m)*(g_t*sin(VA(m)-VA(n))-b_t*cos(VA(m)-VA(n)));
            
        [err_Qflow]=[err_Qflow;m_Qflow(i,2)-Qmn];%error vector for reactive power flow 
    end
end
J_Qflow=[J41 J42];