function [J_Ireal,J_Iimag,err_Ireal,err_Iimag] = form_J_PMUI(line_pi,VV,t_IRI)
% Syntax:[J_Ireal,J_Iimag,err_Ireal,err_Iimag]=form_J_PMUI(line_pi,VV,t_IRI)
%            
% Purpose:   To form the measurement jacobian as well as error corresponding
%            to PMU line current measurement in rectangular coordinate
%            
% Input:    line_pi  - pi-model line information
%           VV       - Voltage vector in complex form
%           t_IRI    - PMU current meas. information in rectangular form
%                      (col1: meas_index col2:I_real_meas col3:I_imag_meas 
%                      col4:R_Ireal col5:R_Iimag)
%
% Output:   J_Ireal    - Jacobian corresponding to PMU current real part 
%           J_Iimag    - Jacobian corresponding to PMU current reactive part 
%           err_Ireal  - error corresponding to PMU current real part
%           err_Iimag  - error corresponding to PMU current reactive part
%
err_Ireal=[];err_Iimag=[];
VM=abs(VV);VA=angle(VV);
t_len=length(t_IRI(:,1));
nob=length(VV);
J71=zeros(t_len,nob);%wrt angle_voltage
J72=zeros(t_len,nob);%wrt voltage magnitude
J81=zeros(t_len,nob);
J82=zeros(t_len,nob);

if isempty(t_IRI)==0    
    for i=1:length(t_IRI(:,1)) 
        k=t_IRI(i,1);
        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);
        bi_t=line_pi(k,4);
            
        %Calculate the value of real and imaginary current thru the branch
        Imnreal=VM(m)*((g_t+gi_t)*cos(VA(m))-(b_t+bi_t)*sin(VA(m)))-VM(n)*(g_t*cos(VA(n))-b_t*sin(VA(n)));
        Imnimag=VM(m)*((g_t+gi_t)*sin(VA(m))+(b_t+bi_t)*cos(VA(m)))-VM(n)*(g_t*sin(VA(n))+b_t*cos(VA(n)));
            
        J71(i,m)=-VM(m)*((g_t+gi_t)*sin(VA(m))+(b_t+bi_t)*cos(VA(m)));
        J71(i,n)=VM(n)*(g_t*sin(VA(n))+b_t*cos(VA(n)));            
            
        J72(i,m)=(g_t+gi_t)*cos(VA(m))-(b_t+bi_t)*sin(VA(m));
        J72(i,n)=-g_t*cos(VA(n))+b_t*sin(VA(n));            
            
        J81(i,m)=VM(m)*((g_t+gi_t)*cos(VA(m))-(b_t+bi_t)*sin(VA(m)));
        J81(i,n)=VM(n)*(-g_t*cos(VA(n))+b_t*sin(VA(n)));                
            
        J82(i,m)=(g_t+gi_t)*sin(VA(m))+(b_t+bi_t)*cos(VA(m));
        J82(i,n)=-g_t*sin(VA(n))-b_t*cos(VA(n));            
            
        [err_Ireal]=[err_Ireal;t_IRI(i,2)-Imnreal];
        [err_Iimag]=[err_Iimag;t_IRI(i,3)-Imnimag];
    end
end
J_Ireal=[J71 J72];
J_Iimag=[J81 J82];
    