%State estimation measurement generation in simulation mode

%=========================================================================
cd simulation_meas;
excelfilename= input('Enter the xcel filename: ','s') ;
if isempty(excelfilename)
    excelfilename='IEEE_14.xlsx';
end
[bus,line,bus_sol,line_flow,nob,nol,slack,lf_flag]=get_data(excelfilename);
if lf_flag==0
    return;
end

%All true value quantity from load flow
[true_bus,true_line]=get_meas_true(bus_sol,line_flow,nob,nol);

%All possible measurements
[all_bus,all_line]=get_meas_total(true_bus,true_line,err_spec,flag_error,nob,nol);
    
[z_Pinj,z_Qinj,z_Pflow,z_Qflow]=get_zeromeasurement(bus_sol,line_flow);
[Pindex,Qindex,Pflowindex,Qflowindex]=get_convindex(bus,line,z_Pinj,z_Qinj,z_Pflow,z_Qflow);
[PMUindex,PMUAngleindex,PMUcurrentindex] = get_PMUindex(bus,line);
cd ..;
%=========================================================================

fprintf('\n\t%s\n','1   PMU CURRENT MEASUREMENT IN POLAR FORM (By default)');
fprintf('\t%s\n','2   PMU CURRENT MEASUREMENT IN RECTANGULAR FORM');
flag_current=input('Enter PMU current measurement form: ');
if isempty(flag_current)==0
    if flag_current~=1 && flag_current~=2
        flag_current=1;
    end
else
    flag_current=1;
end

%Generate bus measurements
meas_bus=[];
for i=1:length(Pindex)
    k=Pindex(i);
    [meas_bus]=[meas_bus;all_bus(k,:)];
end
for i=1:length(Qindex)
    k=Qindex(i);
    [meas_bus]=[meas_bus;all_bus(k+nob,:)];
end
for i=1:length(PMUindex)
    k=PMUindex(i);
    [meas_bus]=[meas_bus;all_bus(k+2*nob,:)];
end
for i=1:length(PMUAngleindex)
    k=PMUAngleindex(i);
    [meas_bus]=[meas_bus;all_bus(k+3*nob,:)];
end

%Generate line measurements
meas_line=[];
for i=1:length(Pflowindex)
    k=Pflowindex(i);
    [meas_line]=[meas_line;all_line(k,1:5)];
end
for i=1:length(Qflowindex)
    k=Qflowindex(i);
    [meas_line]=[meas_line;all_line(k+2*nol,1:5)];
end
if flag_current==2  %PMU current in rectangular form
    for i=1:length(PMUcurrentindex)
        k=PMUcurrentindex(i);
        [meas_line]=[meas_line;all_line(k+8*nol,1:5)];
    end
    for i=1:length(PMUcurrentindex)
        k=PMUcurrentindex(i);
        [meas_line]=[meas_line;all_line(k+10*nol,1:5)];
    end
else
    for i=1:length(PMUcurrentindex)
        k=PMUcurrentindex(i);
        [meas_line]=[meas_line;all_line(k+4*nol,1:5)];
    end
    for i=1:length(PMUcurrentindex)
        k=PMUcurrentindex(i);
        [meas_line]=[meas_line;all_line(k+6*nol,1:5)];
    end
end

delete 'meas_true.xlsx';
excelfilename='meas_true.xlsx';
xlswrite(excelfilename,true_bus,'true_bus_meas');
xlswrite(excelfilename,true_line,'true_line_meas');    
%===================================================================    
delete 'meas_total.xlsx';
excelfilename='meas_total.xlsx';
xlswrite(excelfilename,all_bus,'total_bus_meas');
xlswrite(excelfilename,all_line,'total_line_meas');
%===================================================================
delete 'meas_data.xlsx';
excelfilename='meas_data.xlsx';
temp=[nob;nol;slack];
xlswrite(excelfilename,temp,'sys_param');
%--------------------------------------------------------------
xlswrite(excelfilename,line(:,1:7),'linedata');
%--------------------------------------------------------------
clear temp;
bus_shunt=[bus(:,1) bus(:,8:9)];
xlswrite(excelfilename,bus_shunt,'bus_shunt');

%--------------------------------------------------------------
xlswrite(excelfilename,meas_bus,'bus_meas');
xlswrite(excelfilename,meas_line,'line_meas');
%--------------------------------------------------------------
clear temp;
temp=[z_Pinj ones(length(z_Pinj),1);z_Qinj 2*ones(length(z_Qinj),1)];
if isempty(temp)==0
    xlswrite(excelfilename,temp,'zero_inj');
else
    xlswrite(excelfilename,'N','zero_inj');
end
%--------------------------------------------------------------
temp=[];
for i=1:length(z_Pflow)
    k=z_Pflow(i);
    [temp]=[temp;1 line_flow(k,1:3)];
end
for i=1:length(z_Qflow)
    k=z_Qflow(i);
    [temp]=[temp;2 line_flow(k,1:3)];
end
if isempty(temp)==0
    xlswrite(excelfilename,temp,'zero_flow');
else
    xlswrite(excelfilename,'N','zero_flow');
end
%======================================================================
%===       MEASUREMENT GENERATION OVER     ============================
%======================================================================
clear k_load bus bus_sol line_flow flag_current Pindex Qindex Pflowindex;
clear Qflowindex PMUindex PMUAngleindex PMUcurrentindex;
clear i k temp excelfilename nob nol slack;
%OUTPUT  excel sheet named 'meas_data.xlsx' 'meas_true.xlsx' & 
%                          'meas_total.xlsx'