function read_files_basecase(PAROTS_flag, raw_path, rop_path, inl_path, con_path)
    if (PAROTS_flag) println("Reading input DATA: "); end
    contingency=zeros(Int32,0);
    (network, bus_shift)=process_raw(PAROTS_flag, raw_path);
    network=process_inl(PAROTS_flag, inl_path, network);
    network=process_rop(PAROTS_flag, rop_path, network, bus_shift);
    #contingency=process_con(PAROTS_flag, con_path, network, bus_shift);
    #network=add_LODFs(network, contingency)
    return(network, contingency);
end

function read_files_contingency(PAROTS_flag, raw_path, rop_path, inl_path, con_path)
    if (PAROTS_flag) println("Reading input DATA: "); end
    (network, bus_shift)=process_raw(PAROTS_flag, raw_path);
    network=process_inl(PAROTS_flag, inl_path, network);
    #network=process_rop(rop_path, network, bus_shift);
    contingency=process_con(PAROTS_flag, con_path, network, bus_shift);
    return(network, contingency);
end

"--------------------------------------------------
      Readiang 'raw' file (Network Data File)
---------------------------------------------------"
function add_buses(net_bus, RawString, start, size)
    bus_index=Dict{String,Any}();
    for i=1:size
        net_bus=merge(net_bus, Dict(string(i)=>Dict{String,Any}()));
        bus=split(RawString[i+start-1], ",");
        I=parse(Int64, split(bus[1]," "; limit=0, keepempty=false)[1]);
        VM=parse(Float64, split(bus[8]," "; limit=0, keepempty=false)[1]);
        VA=parse(Float64, split(bus[9]," "; limit=0, keepempty=false)[1]);
        NVHI=parse(Float64, split(bus[10]," "; limit=0, keepempty=false)[1]);
        NVLO=parse(Float64, split(bus[11]," "; limit=0, keepempty=false)[1]);
        EVHI=parse(Float64, split(bus[12]," "; limit=0, keepempty=false)[1]);
        EVLO=parse(Float64, split(bus[13]," "; limit=0, keepempty=false)[1]);
        bus_index=merge(bus_index, Dict(string(I)=>i));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("index"=>I));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("gen"=>false));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("vm"=>VM));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("va"=>deg2rad(VA)));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("vmax"=>NVHI));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("vmin"=>NVLO));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("evmax"=>EVHI));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("evmin"=>EVLO));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("bus_type"=>1));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("Pd"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("Qd"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("GL"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("BL"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("sh_max"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("sh_min"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("bcs"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("gfM"=>0.0));
        net_bus[string(i)]=merge(net_bus[string(i)], Dict("bfM"=>0.0));
    end
    return(net_bus, bus_index)
end
function add_loads(net_bus, bus_index, BaseMVA, RawString, start, size)
    for i=1:size
        load=split(RawString[i+start-1], ",");
        I_index=parse(Int64, split(load[1]," "; limit=0, keepempty=false)[1]);
        I=bus_index[string(I_index)];
        ST=parse(Int64, split(load[3]," "; limit=0, keepempty=false)[1]);
        PL=ST*parse(Float64, split(load[6]," "; limit=0, keepempty=false)[1])/BaseMVA+net_bus[string(I)]["Pd"];
        QL=ST*parse(Float64, split(load[7]," "; limit=0, keepempty=false)[1])/BaseMVA+net_bus[string(I)]["Qd"];
        net_bus[string(I)]=merge(net_bus[string(I)], Dict("Pd"=>PL));
        net_bus[string(I)]=merge(net_bus[string(I)], Dict("Qd"=>QL));
    end
    return(net_bus)
end

function add_shunts(net_bus, bus_index, BaseMVA, RawString, start, size)
    for i=1:size
        shunt=split(RawString[i+start-1], ",");
        I_index=parse(Int64, split(shunt[1]," "; limit=0, keepempty=false)[1]);
        I=bus_index[string(I_index)];
        ST=parse(Int64, split(shunt[3]," "; limit=0, keepempty=false)[1]);
        GL=ST*parse(Float64, split(shunt[4]," "; limit=0, keepempty=false)[1])/BaseMVA+net_bus[string(I)]["GL"];
        BL=ST*parse(Float64, split(shunt[5]," "; limit=0, keepempty=false)[1])/BaseMVA+net_bus[string(I)]["BL"];
        net_bus[string(I)]=merge(net_bus[string(I)], Dict("GL"=>GL));
        net_bus[string(I)]=merge(net_bus[string(I)], Dict("BL"=>BL));
    end
    return(net_bus)
end
function add_gens(net_bus, net_gen, bus_index, BaseMVA, RawString, start, size)
    temp_pamx_id=1; gen_buses=zeros(Int32,0);
    for i=1:size
        net_gen=merge(net_gen, Dict(string(i)=>Dict{String,Any}()));
        gen=split(RawString[i+start-1], ",");
        I_index=parse(Int64, split(gen[1]," "; limit=0, keepempty=false)[1]);
        I=bus_index[string(I_index)];
        ID=split(gen[2],r"\W"; limit=0, keepempty=false);
        if (length(ID)>0) ID=split(ID[1]," "; limit=0, keepempty=false)[1]; end
        St=parse(Int64, split(gen[15]," "; limit=0, keepempty=false)[1]);
        PG=parse(Float64, split(gen[3]," "; limit=0, keepempty=false)[1])/BaseMVA;
        QG=parse(Float64, split(gen[4]," "; limit=0, keepempty=false)[1])/BaseMVA;
        QT=parse(Float64, split(gen[5]," "; limit=0, keepempty=false)[1])/BaseMVA;
        QB=parse(Float64, split(gen[6]," "; limit=0, keepempty=false)[1])/BaseMVA;
        PT=parse(Float64, split(gen[17]," "; limit=0, keepempty=false)[1])/BaseMVA;
        PB=parse(Float64, split(gen[18]," "; limit=0, keepempty=false)[1])/BaseMVA;

        net_gen[string(i)]=merge(net_gen[string(i)], Dict("gen_bus_index"=>I));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("gen_bus"=>I_index));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("id"=>ID));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("gen_status"=>St));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("pg"=>PG));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("qg"=>QG));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("pmax"=>PT));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("qmax"=>QT));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("pmin"=>PB));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("qmin"=>QB));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("index"=>i));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("gov_droop"=>0.0));
        net_gen[string(i)]=merge(net_gen[string(i)], Dict("total_apf"=>0.0));

        if (St==1) net_bus[string(I)]["bus_type"]=2; net_bus[string(I)]["gen"]=true; append!(gen_buses,I); end
        if (St*PT>net_gen[string(temp_pamx_id)]["pmax"]) temp_pamx_id=i; end
    end
    net_bus[string(net_gen[string(temp_pamx_id)]["gen_bus_index"])]["bus_type"]=3;
    return(net_bus, net_gen, net_gen[string(temp_pamx_id)]["gen_bus_index"], gen_buses);
end

function add_Lbranches(net_br, bus_index, BaseMVA, RawString, start, size)
    i=0;
    for ind=1:size
        Lbr=split(RawString[ind+start-1], ",");
        St=parse(Int64, split(Lbr[14]," "; limit=0, keepempty=false)[1]);
        if (St==1) i+=1;
            net_br=merge(net_br, Dict(string(i)=>Dict{String,Any}()));
            # println("string_i: ", string(i))
            I_index=parse(Int64, split(Lbr[1]," "; limit=0, keepempty=false)[1]);
            I=bus_index[string(I_index)];
            J_index=parse(Int64, split(Lbr[2]," "; limit=0, keepempty=false)[1]);
            J=bus_index[string(J_index)];
            CKT=split(Lbr[3],r"\W"; limit=0, keepempty=false);
            if (length(CKT)>0) CKT=split(CKT[1]," "; limit=0, keepempty=false)[1]; end
            R=parse(Float64, split(Lbr[4]," "; limit=0, keepempty=false)[1]);
            X=parse(Float64, split(Lbr[5]," "; limit=0, keepempty=false)[1]);
            B=parse(Float64, split(Lbr[6]," "; limit=0, keepempty=false)[1]);
            RATEA=parse(Float64, split(Lbr[7]," "; limit=0, keepempty=false)[1])/BaseMVA;
            RATEC=parse(Float64, split(Lbr[9]," "; limit=0, keepempty=false)[1])/BaseMVA;

            net_br[string(i)]=merge(net_br[string(i)], Dict("br_r"=>R));
            net_br[string(i)]=merge(net_br[string(i)], Dict("ckt"=>CKT));
            net_br[string(i)]=merge(net_br[string(i)], Dict("br_status"=>St));
            net_br[string(i)]=merge(net_br[string(i)], Dict("br_x"=>X));
            net_br[string(i)]=merge(net_br[string(i)], Dict("b_fr"=>B/2));
            net_br[string(i)]=merge(net_br[string(i)], Dict("b_to"=>B/2));
            net_br[string(i)]=merge(net_br[string(i)], Dict("rate_a"=>RATEA));
            net_br[string(i)]=merge(net_br[string(i)], Dict("rate_c"=>RATEC));
            net_br[string(i)]=merge(net_br[string(i)], Dict("f_bus"=>I_index));
            net_br[string(i)]=merge(net_br[string(i)], Dict("t_bus"=>J_index));
            net_br[string(i)]=merge(net_br[string(i)], Dict("f_bus_index"=>I));
            net_br[string(i)]=merge(net_br[string(i)], Dict("t_bus_index"=>J));
            net_br[string(i)]=merge(net_br[string(i)], Dict("transformer"=>false));
            net_br[string(i)]=merge(net_br[string(i)], Dict("tap"=>1.0));
            net_br[string(i)]=merge(net_br[string(i)], Dict("shift"=>0.0));
            net_br[string(i)]=merge(net_br[string(i)], Dict("index"=>i));
            net_br[string(i)]=merge(net_br[string(i)], Dict("gfM"=>0.0));
            net_br[string(i)]=merge(net_br[string(i)], Dict("bfM"=>0.0));
            net_br[string(i)]=merge(net_br[string(i)], Dict("g_fr"=>0.0));
            net_br[string(i)]=merge(net_br[string(i)], Dict("g_to"=>0.0));
        end
    end
    return(net_br)
end
function add_Tbranches(net_bus, net_br, bus_index, BaseMVA, RawString, pre, start, size)
    # println("pre: ", pre);
    ind=0
    for i in 1:4:size
        Tran1=split(RawString[i+start-1], ",");
        Tran2=split(RawString[i+start], ",");
        Tran3=split(RawString[i+start+1], ",");
        Tran4=split(RawString[i+start+2], ",");
        St=parse(Int64, split(Tran1[12]," "; limit=0, keepempty=false)[1]);
        if (St==1)  ind+=1;
            net_br=merge(net_br, Dict(string(ind+pre)=>Dict{String,Any}()));
            I_index=parse(Int64, split(Tran1[1]," "; limit=0, keepempty=false)[1]);
            I=bus_index[string(I_index)];
            J_index=parse(Int64, split(Tran1[2]," "; limit=0, keepempty=false)[1]);
            J=bus_index[string(J_index)];
            CKT=split(Tran1[4],r"\W"; limit=0, keepempty=false);
            if (length(CKT)>0) CKT=split(CKT[1]," "; limit=0, keepempty=false)[1]; end
            MAG1=parse(Float64, split(Tran1[8]," "; limit=0, keepempty=false)[1]);
            MAG2=parse(Float64, split(Tran1[9]," "; limit=0, keepempty=false)[1]);

            R12=parse(Float64, split(Tran2[1]," "; limit=0, keepempty=false)[1]);
            X12=parse(Float64, split(Tran2[2]," "; limit=0, keepempty=false)[1]);
            WINDV1=parse(Float64, split(Tran3[1]," "; limit=0, keepempty=false)[1]);
            ANG1=parse(Float64, split(Tran3[3]," "; limit=0, keepempty=false)[1]);
            RATEA1=parse(Float64, split(Tran3[4]," "; limit=0, keepempty=false)[1])/BaseMVA;
            RATEC1=parse(Float64, split(Tran3[6]," "; limit=0, keepempty=false)[1])/BaseMVA;
            WINDV2=parse(Float64, split(Tran4[1]," "; limit=0, keepempty=false)[1]);

            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("br_r"=>R12));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("ckt"=>CKT));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("br_status"=>St));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("br_x"=>X12));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("b_fr"=>0.0));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("b_to"=>0.0));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("rate_a"=>RATEA1));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("rate_c"=>RATEC1));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("f_bus"=>I_index));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("t_bus"=>J_index));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("f_bus_index"=>I));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("t_bus_index"=>J));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("transformer"=>true));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("tap"=>WINDV1/WINDV2));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("shift"=>ANG1*pi/180));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("index"=>ind+pre)); #index=i
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("g_fr"=>0.0));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("g_to"=>0.0));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("gfM"=>MAG1));
            net_br[string(ind+pre)]=merge(net_br[string(ind+pre)], Dict("bfM"=>MAG2));

            gfM_total=net_bus[string(I)]["gfM"]+MAG1*St;
            bfM_total=net_bus[string(I)]["bfM"]+MAG2*St;
            net_bus[string(I)]=merge(net_bus[string(I)], Dict("gfM"=>gfM_total));
            net_bus[string(I)]=merge(net_bus[string(I)], Dict("bfM"=>bfM_total));
        end
    end
    return(net_bus,net_br)
end

function add_switch_shunts(net_bus, net_sshunt, bus_index, BaseMVA, RawString, start, size)
    net_sshunt=merge(net_sshunt, Dict("bus_index"=>zeros(Int32,0)));
    for i=1:size
        net_sshunt=merge(net_sshunt, Dict(string(i)=>Dict{String,Any}()));
        shunt=split(RawString[i+start-1], ",");
        I_index=parse(Int64, split(shunt[1]," "; limit=0, keepempty=false)[1]);
        I=bus_index[string(I_index)];
        STATUS=parse(Int64, split(shunt[4]," "; limit=0, keepempty=false)[1]);

        N=zeros(8); B=zeros(8); BL=zeros(8); Shmin=0; Shmax=0;
        for i=1:8
            N[i]=parse(Int64, split(shunt[2*(4+i)+1]," "; limit=0, keepempty=false)[1]);
            B[i]=parse(Float64, split(shunt[2*(5+i)]," "; limit=0, keepempty=false)[1]);
            BL[i]=N[i]*B[i]/BaseMVA; if (BL[i]>0) Shmax+=BL[i]*STATUS; elseif (BL[i]<0) Shmin+=BL[i]*STATUS; end
        end

        net_sshunt[string(i)]=merge(net_sshunt[string(i)], Dict("status"=>STATUS), Dict("shunt_bus"=>I_index), Dict("shunt_bus_index"=>I));
        net_sshunt[string(i)]=merge(net_sshunt[string(i)], Dict("shmax"=>Shmax), Dict("shmin"=>Shmin));

        Shmax+=net_bus[string(I)]["sh_max"]*STATUS;
        Shmin+=net_bus[string(I)]["sh_min"]*STATUS;
        net_bus[string(I)]=merge(net_bus[string(I)], Dict("sh_max"=>Shmax), Dict("sh_min"=>Shmin));
        push!(net_sshunt["bus_index"],I);
    end
    return(net_bus, net_sshunt);
end

function process_raw(PAROTS_flag, path_raw)
    network1=Dict{String,Any}();
    raw_string = readlines(open(path_raw));
    temp=split(raw_string[1], ","); baseMVA=parse(Float64, split(temp[2]," "; limit=0, keepempty=false)[1]);
    network1=merge(network1, Dict("baseMVA"=>baseMVA), Dict("bus"=>Dict{String,Any}()), Dict("sshunt"=>Dict{String,Any}()), Dict("gen"=>Dict{String,Any}()));
    network1=merge(network1, Dict("branch"=>Dict{String,Any}()), Dict("solution"=>Dict()));

    bus_start=4; bus_end=0; bus_check=true; bus_col=13;
    load_start=4; load_end=0; load_check=true; load_col=14;
    shunt_start=4; shunt_end=0; shunt_check=true; shunt_col=5;
    gen_start=4; gen_end=0; gen_check=true; gen_col=28;
    branch_start=4; branch_end=0; branch_check=true; branch_col=24;
    Sshunt_start=4; Sshunt_end=0; Sshunt_check=true; Sshunt_col=26;
    trans_start=4; trans_end=0; trans_check=true; Tcol1=21; Tcol2=3; Tcol3=17; Tcol4=2;
    for i=4:length(raw_string)
        temp=split(raw_string[i],","); temp_pre=split(raw_string[i-1],","); temp1=0; temp2=0; temp3=0; temp_pre_sh=0; temp_sh=0;
        if (length(temp)>=2) temp_sh=split(temp[2]," "; limit=0, keepempty=false)[1]; temp_sh=split(temp[2],""; limit=0, keepempty=false)[1]; end
        if (length(temp_pre)>=2) temp_pre_sh=split(temp_pre[2]," "; limit=0, keepempty=false)[1]; temp_pre_sh=split(temp_pre[2],""; limit=0, keepempty=false)[1]; end
        if (i<length(raw_string)-3) temp1=split(raw_string[i+1],","); temp2=split(raw_string[i+2],","); temp3=split(raw_string[i+3],","); end

        if (length(temp)==bus_col && bus_check) bus_start=i; bus_check=false; end
        if (length(temp_pre)==bus_col && length(temp)!=bus_col) bus_end=i-1; end

        if (length(temp)==load_col && load_check) load_start=i; load_check=false; end
        if (length(temp_pre)==load_col && length(temp)!=load_col) load_end=i-1; end

        if (length(temp)==shunt_col && temp_sh=="'" && shunt_check) shunt_start=i; shunt_check=false; end
        if (length(temp_pre)==shunt_col && (length(temp)!=shunt_col || temp_sh!="'") && temp_pre_sh=="'") shunt_end=i-1; end

        if (length(temp)==gen_col && gen_check) gen_start=i; gen_check=false; end
        if (length(temp_pre)==gen_col && length(temp)!=gen_col) gen_end=i-1; end

        if (length(temp)==branch_col && branch_check) branch_start=i; branch_check=false; end
        if (length(temp_pre)==branch_col && length(temp)!=branch_col) branch_end=i-1; end

        if (length(temp)==Tcol1 && length(temp1)==Tcol2 && length(temp2)==Tcol3 && length(temp3)==Tcol4 && trans_check) trans_start=i; trans_check=false; end
        if (length(temp)==Tcol4 && length(temp_pre)==Tcol3 && length(temp1)!=Tcol1) trans_end=i; end

        if (length(temp)==Sshunt_col && Sshunt_check) Sshunt_start=i; Sshunt_check=false; end
        if (length(temp_pre)==Sshunt_col && length(temp)!=Sshunt_col) Sshunt_end=i-1; end
    end

    (network1["bus"], bus_shift)=add_buses(network1["bus"], raw_string, bus_start, bus_end-bus_start+1);
    network1["bus"]=add_loads(network1["bus"], bus_shift, network1["baseMVA"], raw_string, load_start, load_end-load_start+1);
    network1["bus"]=add_shunts(network1["bus"], bus_shift, network1["baseMVA"], raw_string, shunt_start, shunt_end-shunt_start+1);
    (network1["bus"], network1["gen"], slack, gen_buses) =add_gens(network1["bus"], network1["gen"], bus_shift, network1["baseMVA"], raw_string, gen_start, gen_end-gen_start+1);
    network1=merge(network1, Dict("gen_buses"=>gen_buses, "slack"=>slack));
    network1["branch"]=add_Lbranches(network1["branch"], bus_shift, network1["baseMVA"], raw_string, branch_start, branch_end-branch_start+1);
    (network1["bus"], network1["branch"])=add_Tbranches(network1["bus"], network1["branch"], bus_shift, network1["baseMVA"], raw_string, length(network1["branch"]), trans_start, trans_end-trans_start+1);
    (network1["bus"], network1["sshunt"])=add_switch_shunts(network1["bus"], network1["sshunt"], bus_shift, network1["baseMVA"], raw_string, Sshunt_start, Sshunt_end-Sshunt_start+1);
    network1=merge(network1, Dict("nbal"=>Dict{String,Any}(), "source_type"=>"arpae"));
    for i=1:length(network1["bus"])
        network1["nbal"]=merge(network1["nbal"], Dict(string(i)=>Dict()));
        network1["nbal"][string(i)]=merge(network1["nbal"][string(i)], Dict("gen"=>zeros(Int32,0)), Dict("Sij"=>zeros(Int32,0)), Dict("Sji"=>zeros(Int32,0)), Dict("shunt"=>zeros(Int32,0)));
    end

    for g=1:length(network1["gen"]) gen=network1["gen"][string(g)];
        if (gen["gen_bus_index"]*gen["gen_status"]>0) push!(network1["nbal"][string(gen["gen_bus_index"])]["gen"],g); end
    end
    # println("nbr: ", length(network1["branch"]));
    for b=1:length(network1["branch"]) br=network1["branch"][string(b)];
        if (br["f_bus_index"]*br["br_status"]>0) push!(network1["nbal"][string(br["f_bus_index"])]["Sij"], b); end
        if (br["t_bus_index"]*br["br_status"]>0) push!(network1["nbal"][string(br["t_bus_index"])]["Sji"], b); end
    end
    for sh=1:length(network1["sshunt"])-1 shunt=network1["sshunt"][string(sh)];
        if (shunt["shunt_bus_index"]*shunt["status"]>0) push!(network1["nbal"][string(shunt["shunt_bus_index"])]["shunt"],sh); end
    end
    # if (PAROTS_flag>0) println("The RAW file was read successfully ...") end
    return(network1, bus_shift)
end

"--------------------------------------------------
   Readiang 'rop' file (Generator Cost Data File)
---------------------------------------------------"
function Generator_Dispatch_data(rop_string, gen_index, bus_index, GDD_in_start, size, generator)
    for i=1:size
        temp = split(rop_string[GDD_in_start+i-1],",");
        temp[2]=split(temp[2],r"\W"; limit=0, keepempty=false)[1];
        ID=split(temp[2]," "; limit=0, keepempty=false)[1];
        bus_no=parse(Int64,temp[1]);
        bus_no_index=bus_index[string(bus_no)];
        DSPTBL=parse(Int64,temp[4]);
        generator[string(gen_index[string(DSPTBL)])]=merge(generator[string(gen_index[string(DSPTBL)])], Dict("GenID"=>ID), Dict("bus"=>bus_no), Dict("bus_index"=>bus_no_index));
    end
    return(generator)
end

function process_rop(PAROTS_flag, path_rop, network, bus_index)
    rop_string = readlines(open(path_rop));
    generator=Dict{String,Any}();
    "Generator Dispatch Data"
    GDU_start=0; GDU_check=true; GDU_col=4; GDU_end=0;
    APDT_start=0; APDT_check=true; APDT_col=7; #APDT_end=0;
    PLCT_start=0; PLCT_check=true; PLCT_col=3; #PLCT_end=0;

    for i=1:length(rop_string)
        temp=split(rop_string[i],",");
        temp_pre= (i>1)  ? split(rop_string[i-1],",") : ["0 "," 0"] ;
	       temp_1=split(temp[1]," "; limit=0, keepempty=false)[1];
	          temp_1_pre=split(temp_pre[1]," "; limit=0, keepempty=false)[1];
        if (length(temp)==GDU_col && GDU_check && temp_1!="0") GDU_start=i; GDU_check=false; end
        if (length(temp_pre)==GDU_col && length(temp)!=GDU_col && temp_1_pre!="0") GDU_end=i-1; end
        if (length(temp)==APDT_col && APDT_check && temp_1!="0") APDT_start=i; APDT_check=false; end
        if (length(temp)==PLCT_col && PLCT_check && temp_1!="0") PLCT_start=i; PLCT_check=false; end
    end
    size=GDU_end-GDU_start+1;
    NPAIRSit=PLCT_start;
    NPAIRS=0;
    gen_index=Dict{String,Any}();
    for i=1:size
        temp = split(rop_string[NPAIRSit],","); NPAIRS = parse(Int64, temp[3]); nTable=parse(Int64, temp[1]);
        temp=split(rop_string[NPAIRSit+1],","); X=parse(Float64, temp[1]); Y=parse(Float64, temp[2]);
        generator=merge(generator, Dict(string(nTable)=>Dict("LinPoints"=>NPAIRS, "LinCost"=>Dict())));
        baseMVA=network["baseMVA"];
        generator[string(nTable)]["LinCost"]=merge(generator[string(nTable)]["LinCost"], Dict("1"=>[X/baseMVA,Y/baseMVA]));
        for c=2:NPAIRS
            temp=split(rop_string[NPAIRSit+c],","); X=parse(Float64, temp[1]); Y=parse(Float64, temp[2]);
            generator[string(nTable)]["LinCost"]=merge(generator[string(nTable)]["LinCost"], Dict(string(c)=>[X/baseMVA,Y/baseMVA]));
        end
        NPAIRSit+=NPAIRS+1;
    end
    for i=1:size
        temp1 = split(rop_string[APDT_start+i-1],",");
        index = parse(Int64, temp1[7]); TBL = parse(Int64, temp1[1]); gen_index=merge(gen_index, Dict(string(TBL)=>index));
        generator[string(index)]=merge(generator[string(index)], Dict("gov_droop"=>0.0));
    end
    for i=1:size
        temp2 = split(rop_string[GDU_start+i-1],",");
        temp2[2]=split(temp2[2],r"\W"; limit=0, keepempty=false)[1];
        ID=split(temp2[2]," "; limit=0, keepempty=false)[1];
        bus_no=parse(Int64,temp2[1]);
        bus_no_index=bus_index[string(bus_no)];
        DSPTBL=parse(Int64,temp2[4]);
        generator[string(gen_index[string(DSPTBL)])]=merge(generator[string(gen_index[string(DSPTBL)])], Dict("GenID"=>ID), Dict("bus"=>bus_no), Dict("bus_index"=>bus_no_index));
    end
    for i=1:length(generator) if (!haskey(generator[string(i)], "LinPoints") && (PAROTS_flag)) println("generator missing info at ", i) end end
    if (size==length(network["gen"])) Gen_Dict=deepcopy(network["gen"]);
        for i=1:size
            for j in Gen_Dict
                bg=j[2]["gen_bus"]; Ug=j[2]["id"];
                if (generator[string(i)]["bus"]==bg && generator[string(i)]["GenID"]==Ug)
                    LC=generator[string(i)]["LinCost"]; LP=generator[string(i)]["LinPoints"];
                    network["gen"][string(j[1])]=merge(network["gen"][string(j[1])], Dict("LinCost"=>LC), Dict("LinPoints"=>LP));
                    delete!(Gen_Dict, j[1]);
                end
            end
        end
        # if (PAROTS_flag>0) println("The ROP file was read successfully ...") end
    end
    # elseif (PAROTS_flag) println("ERROR | The number of generators in RAW and ROP files don't match") end
    return(network)
end

"--------------------------------------------------
Readiang 'inl' file (Governor Response Data File)
---------------------------------------------------"
function process_inl(PAROTS_flag, path_inl, network)
    inl_string = readlines(open(path_inl));
    Gen_Dict=deepcopy(network["gen"]);
    for i=1:length(inl_string)
        temp=split(inl_string[i],",");
        if (length(temp)==7)
            bus=parse(Int64, split(temp[1]," "; limit=0, keepempty=false)[1]);
            ID_temp=split(temp[2],r"\W"; limit=0, keepempty=false)[1]; genID=split(ID_temp," "; limit=0, keepempty=false)[1];
            for j in Gen_Dict
                bg=j[2]["gen_bus"]; Ug=j[2]["id"];
                if (bus==bg && genID==Ug)
                    gov_droop=parse(Float64, temp[6]);
                    network["gen"][string(j[1])]=merge(network["gen"][string(j[1])], Dict("gov_droop"=>gov_droop));
                    delete!(Gen_Dict, j[1]);
                end
            end
        end
    end
    sum_apf=0
    for i=1:length(network["gen"]) gen=network["gen"][string(i)]; st=gen["gen_status"]
        sum_apf+=st*gen["gov_droop"];
    end
    for i=1:length(network["gen"]) gen=network["gen"][string(i)]; st=gen["gen_status"]
        total_apf=sum_apf-st*gen["gov_droop"];
        network["gen"][string(i)]=merge(network["gen"][string(i)], Dict("total_apf"=>total_apf));
    end
    # if (PAROTS_flag>0) println("The INL file was read successfully ..."); end
    return(network);
end

"-------------------------------------------------------
Readiang 'con' file (Contingency Description Data File)
--------------------------------------------------------"
function adding_con_line_info(contingency, counts, Branch_Dict)
    fc=contingency[string(counts)]["f_bus"];
    tc=contingency[string(counts)]["t_bus"];
    CTc=contingency[string(counts)]["CT"];
    for j in Branch_Dict
        fb=j[2]["f_bus"];
        tb=j[2]["t_bus"];
        CTb=j[2]["ckt"];
        if (fb==fc && tb==tc && CTb==CTc)
            index=j[2]["index"];
            contingency[string(counts)]=merge(contingency[string(counts)], Dict("branch"=>index));
        end
    end
    return(contingency)
end

function adding_con_gen_info(contingency, counts, contingency_G, Gcounts, Gen_Dict)
    bc=contingency[string(counts)]["bus"]; Uc=contingency[string(counts)]["unit"];
    for j in Gen_Dict
        bg=j[2]["gen_bus"]; Ug=j[2]["id"];
        if (bc==bg && Uc==Ug)
            contingency[string(counts)]=merge(contingency[string(counts)], Dict("GenNum"=>j[2]["index"]));
            contingency_G[string(Gcounts)]=merge(contingency_G[string(Gcounts)], Dict("GenNum"=>j[2]["index"]));
        end
    end
    return(contingency, contingency_G);
end

function process_con(PAROTS_flag, path_con, network, bus_index)
    con_string = readlines(open(path_con));
    contingency=Dict{String,Any}(); contingency_G=Dict{String,Any}();
    counts=0; Gcounts=0
    for i=1:length(con_string)-1
        temp1 = split(con_string[i]," "; limit=0, keepempty=false); temp2 = split(con_string[i+1]," "; limit=0, keepempty=false);

        if (length(temp1)==2 && length(temp2)<6)
            label=temp1[2]; counts+=1;
            contingency[string(counts)]=Dict("status"=>false);
            contingency[string(counts)]=merge(contingency[string(counts)], Dict("label"=>label), Dict("type"=>NaN));
        elseif (length(temp1)==2 && length(temp2)==10)
            label=temp1[2]; type = split(temp1[2],""; limit=0, keepempty=false)[1]; counts+=1; f_bus_index=parse(Int64,temp2[5]); f_bus=bus_index[string(f_bus_index)];
            contingency[string(counts)]=Dict("type"=>type, "status"=>true, "label"=>label, "f_bus"=>f_bus_index, "f_bus_index"=>f_bus);
            t_bus_index=parse(Int64,temp2[8]); t_bus=bus_index[string(t_bus_index)]; CT_bus=temp2[10];
            contingency[string(counts)]=merge(contingency[string(counts)], Dict("t_bus"=>t_bus_index, "t_bus_index"=>t_bus, "CT"=>CT_bus));
            contingency=adding_con_line_info(contingency, counts, network["branch"]);
        elseif (length(temp1)==2 && length(temp2)==6)
            label=temp1[2];
            counts+=1;
            Gcounts+=1;
            unit=temp2[3];
            f_bus_index=parse(Int64,temp2[6]);
            f_bus=bus_index[string(f_bus_index)];
            contingency[string(counts)]=Dict("type"=>"G", "status"=>true, "label"=>label, "unit"=>unit, "bus"=>f_bus_index, "bus_index"=>f_bus);
            contingency_G[string(Gcounts)]=Dict("type"=>"G", "label"=>label, "unit"=>unit, "bus"=>f_bus_index, "bus_index"=>f_bus);
            (contingency, contingency_G)=adding_con_gen_info(contingency, counts, contingency_G, Gcounts, network["gen"]);
        end
    end

    # if (PAROTS_flag>0) println("The CON file was read successfully ..."); end
    return(contingency);
end

"--------------------------------------------------
   Readiang 'raw' file for LODF calculation
---------------------------------------------------"
function process_net_LODF(PAROTS_flag, path_raw)
    network1=Dict{String,Any}();
    raw_string = readlines(open(path_raw));
    temp=split(raw_string[1], ","); BaseMVA=parse(Float64, split(temp[2]," "; limit=0, keepempty=false)[1]);
    network1=merge(network1, Dict("baseMVA"=>BaseMVA), Dict("bus"=>Dict{String,Any}()), Dict("sshunt"=>Dict{String,Any}()), Dict("gen"=>Dict{String,Any}()));
    network1=merge(network1, Dict("branch"=>Dict{String,Any}()), Dict("solution"=>Dict()));

    bus_start=4; bus_end=0; bus_check=true; bus_col=13;
    gen_start=4; gen_end=0; gen_check=true; gen_col=28;
    branch_start=4; branch_end=0; branch_check=true; branch_col=24;
    trans_start=4; trans_end=0; trans_check=true; Tcol1=21; Tcol2=3; Tcol3=17; Tcol4=2;
    for i=4:length(raw_string)
        temp=split(raw_string[i],","); temp_pre=split(raw_string[i-1],","); temp1=0; temp2=0; temp3=0; temp_pre_sh=0; temp_sh=0;
        if (length(temp)>=2) temp_sh=split(temp[2]," "; limit=0, keepempty=false)[1]; temp_sh=split(temp[2],""; limit=0, keepempty=false)[1]; end
        if (length(temp_pre)>=2) temp_pre_sh=split(temp_pre[2]," "; limit=0, keepempty=false)[1]; temp_pre_sh=split(temp_pre[2],""; limit=0, keepempty=false)[1]; end
        if (i<length(raw_string)-3) temp1=split(raw_string[i+1],","); temp2=split(raw_string[i+2],","); temp3=split(raw_string[i+3],","); end

        if (length(temp)==bus_col && bus_check) bus_start=i; bus_check=false; end
        if (length(temp_pre)==bus_col && length(temp)!=bus_col) bus_end=i-1; end

        if (length(temp)==gen_col && gen_check) gen_start=i; gen_check=false; end
        if (length(temp_pre)==gen_col && length(temp)!=gen_col) gen_end=i-1; end

        if (length(temp)==branch_col && branch_check) branch_start=i; branch_check=false; end
        if (length(temp_pre)==branch_col && length(temp)!=branch_col) branch_end=i-1; end

        if (length(temp)==Tcol1 && length(temp1)==Tcol2 && length(temp2)==Tcol3 && length(temp3)==Tcol4 && trans_check) trans_start=i; trans_check=false; end
        if (length(temp)==Tcol4 && length(temp_pre)==Tcol3 && length(temp1)!=Tcol1) trans_end=i; end
    end
    bus_size=bus_end-bus_start+1; bus_shift=Dict{String,Any}();
    for i=1:bus_size
        bus=split(raw_string[i+bus_start-1], ",");
        I=parse(Int64, split(bus[1]," "; limit=0, keepempty=false)[1]);
        bus_shift=merge(bus_shift, Dict(string(I)=>i));
    end
    gen_size=gen_end-gen_start+1;
    temp_pamx_id=1; max_gen=0.0;
    for i=1:gen_size
        gen=split(raw_string[i+gen_start-1], ",");
        I_index=parse(Int64, split(gen[1]," "; limit=0, keepempty=false)[1]);
        I=bus_shift[string(I_index)];
        St=parse(Int64, split(gen[15]," "; limit=0, keepempty=false)[1]);
        PT=parse(Float64, split(gen[17]," "; limit=0, keepempty=false)[1])/BaseMVA;
        if (max_gen<St*PT) max_gen=St*PT; temp_pamx_id=i; end
    end
    network1=merge(network1, Dict("slack"=>temp_pamx_id));
    network1=merge(network1, Dict("nbus"=>bus_size));
    branch_size=branch_end-branch_start+1; i=0;
    for ind=1:branch_size
        Lbr=split(raw_string[ind+branch_start-1], ",");
        St=parse(Int64, split(Lbr[14]," "; limit=0, keepempty=false)[1]);
        if (St==1) i+=1;
            network1["branch"]=merge(network1["branch"], Dict(string(i)=>Dict{String,Any}()));
            I_index=parse(Int64, split(Lbr[1]," "; limit=0, keepempty=false)[1]);
            I=bus_shift[string(I_index)];
            J_index=parse(Int64, split(Lbr[2]," "; limit=0, keepempty=false)[1]);
            J=bus_shift[string(J_index)];
            CKT=split(Lbr[3],r"\W"; limit=0, keepempty=false);
            if (length(CKT)>0) CKT=split(CKT[1]," "; limit=0, keepempty=false)[1]; end

            R=parse(Float64, split(Lbr[4]," "; limit=0, keepempty=false)[1]);
            X=parse(Float64, split(Lbr[5]," "; limit=0, keepempty=false)[1]);
            B=parse(Float64, split(Lbr[6]," "; limit=0, keepempty=false)[1]);
            RATEC=parse(Float64, split(Lbr[9]," "; limit=0, keepempty=false)[1])/BaseMVA;

            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("br_r"=>R));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("br_status"=>St, "index"=>i));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("br_x"=>X,"ratec"=>RATEC));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("b_fr"=>B/2));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("b_to"=>B/2));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("f_bus"=>I_index, "t_bus"=>J_index, "ckt"=>CKT));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("f_bus_index"=>I));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("t_bus_index"=>J));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("tap"=>1.0));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("shift"=>0.0));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("gfM"=>0.0));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("bfM"=>0.0));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("g_fr"=>0.0));
            network1["branch"][string(i)]=merge(network1["branch"][string(i)], Dict("g_to"=>0.0));
        end
    end
    branch_size=length(network1["branch"]);
    tbranch_size=trans_end-trans_start+1; ind=0;
    for i in 1:4:tbranch_size
        Tran1=split(raw_string[i+trans_start-1], ",");
        Tran2=split(raw_string[i+trans_start], ",");
        Tran3=split(raw_string[i+trans_start+1], ",");
        Tran4=split(raw_string[i+trans_start+2], ",");
        St=parse(Int64, split(Tran1[12]," "; limit=0, keepempty=false)[1]);
        if (St==1) ind+=1;
            network1["branch"]=merge(network1["branch"], Dict(string(ind+branch_size)=>Dict{String,Any}()));
            I_index=parse(Int64, split(Tran1[1]," "; limit=0, keepempty=false)[1]);
            I=bus_shift[string(I_index)];
            J_index=parse(Int64, split(Tran1[2]," "; limit=0, keepempty=false)[1]);
            J=bus_shift[string(J_index)];
            CKT=split(Tran1[4],r"\W"; limit=0, keepempty=false);
            if (length(CKT)>0) CKT=split(CKT[1]," "; limit=0, keepempty=false)[1]; end
            MAG1=parse(Float64, split(Tran1[8]," "; limit=0, keepempty=false)[1]);
            MAG2=parse(Float64, split(Tran1[9]," "; limit=0, keepempty=false)[1]);
            R12=parse(Float64, split(Tran2[1]," "; limit=0, keepempty=false)[1]);
            X12=parse(Float64, split(Tran2[2]," "; limit=0, keepempty=false)[1]);
            WINDV1=parse(Float64, split(Tran3[1]," "; limit=0, keepempty=false)[1]);
            ANG1=parse(Float64, split(Tran3[3]," "; limit=0, keepempty=false)[1]);
            WINDV2=parse(Float64, split(Tran4[1]," "; limit=0, keepempty=false)[1]);
            RATEC1=parse(Float64, split(Tran3[6]," "; limit=0, keepempty=false)[1])/BaseMVA;

            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("br_r"=>R12));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("br_status"=>St, "index"=>ind+branch_size));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("br_x"=>X12));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("b_fr"=>0.0,"ratec"=>RATEC1));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("b_to"=>0.0));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("f_bus"=>I_index, "t_bus"=>J_index, "ckt"=>CKT));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("f_bus_index"=>I));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("t_bus_index"=>J));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("transformer"=>true));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("tap"=>WINDV1/WINDV2));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("shift"=>deg2rad(ANG1)));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("g_fr"=>0.0));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("g_to"=>0.0));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("gfM"=>MAG1));
            network1["branch"][string(ind+branch_size)]=merge(network1["branch"][string(ind+branch_size)], Dict("bfM"=>MAG2));
        end
    end
    return(network1, bus_shift)
end
"--------------------------------------------------
        find slack bus from 'raw' file
---------------------------------------------------"
function find_slack(path_raw)
    raw_string = readlines(open(path_raw));
    temp=split(raw_string[1], ","); BaseMVA=parse(Float64, split(temp[2]," "; limit=0, keepempty=false)[1]);
    bus_start=4; bus_end=0; bus_check=true; bus_col=13;
    gen_start=4; gen_end=0; gen_check=true; gen_col=28;
    for i=4:length(raw_string)
        temp=split(raw_string[i],","); temp_pre=split(raw_string[i-1],",");
        if (length(temp)==bus_col && bus_check) bus_start=i; bus_check=false; end
        if (length(temp_pre)==bus_col && length(temp)!=bus_col) bus_end=i-1; end
        if (length(temp)==gen_col && gen_check) gen_start=i; gen_check=false; end
        if (length(temp_pre)==gen_col && length(temp)!=gen_col) gen_end=i-1; end
    end
    bus_size=bus_end-bus_start+1; bus_shift=Dict{String,Any}();
    for i=1:bus_size
        bus=split(raw_string[i+bus_start-1], ",");
        I=parse(Int64, split(bus[1]," "; limit=0, keepempty=false)[1]);
        bus_shift=merge(bus_shift, Dict(string(I)=>i));
    end
    gen_size=gen_end-gen_start+1; index=0;
    for i=1:gen_size
        gen=split(raw_string[i+gen_start-1], ",");
        I_index=parse(Int64, split(gen[1]," "; limit=0, keepempty=false)[1]);
        I=bus_shift[string(I_index)];
        St=parse(Int64, split(gen[15]," "; limit=0, keepempty=false)[1]);
        if (St==1) index=I; break; end
    end
    return(index)
end

"--------------------------------------------------
 Readiang 'raw' file for node4 (Network Data File)
---------------------------------------------------"
function process_raw_node4(PAROTS_flag, path_raw)
    network1=Dict{String,Any}();
    raw_string = readlines(open(path_raw));
    temp=split(raw_string[1], ","); baseMVA=parse(Float64, split(temp[2]," "; limit=0, keepempty=false)[1]);
    network1=merge(network1, Dict("baseMVA"=>baseMVA), Dict("bus"=>Dict{String,Any}()), Dict("sshunt"=>Dict{String,Any}()), Dict("gen"=>Dict{String,Any}()));
    network1=merge(network1, Dict("branch"=>Dict{String,Any}()), Dict("solution"=>Dict()));

    bus_start=4; bus_end=0; bus_check=true; bus_col=13;
    load_start=4; load_end=0; load_check=true; load_col=14;
    shunt_start=4; shunt_end=0; shunt_check=true; shunt_col=5;
    gen_start=4; gen_end=0; gen_check=true; gen_col=28;
    branch_start=4; branch_end=0; branch_check=true; branch_col=24;
    Sshunt_start=4; Sshunt_end=0; Sshunt_check=true; Sshunt_col=26;
    trans_start=4; trans_end=0; trans_check=true; Tcol1=21; Tcol2=3; Tcol3=17; Tcol4=2;
    for i=4:length(raw_string)
        temp=split(raw_string[i],","); temp_pre=split(raw_string[i-1],","); temp1=0; temp2=0; temp3=0; temp_pre_sh=0; temp_sh=0;
        if (length(temp)>=2) temp_sh=split(temp[2]," "; limit=0, keepempty=false)[1]; temp_sh=split(temp[2],""; limit=0, keepempty=false)[1]; end
        if (length(temp_pre)>=2) temp_pre_sh=split(temp_pre[2]," "; limit=0, keepempty=false)[1]; temp_pre_sh=split(temp_pre[2],""; limit=0, keepempty=false)[1]; end
        if (i<length(raw_string)-3) temp1=split(raw_string[i+1],","); temp2=split(raw_string[i+2],","); temp3=split(raw_string[i+3],","); end

        if (length(temp)==bus_col && bus_check) bus_start=i; bus_check=false; end
        if (length(temp_pre)==bus_col && length(temp)!=bus_col) bus_end=i-1; end

        if (length(temp)==load_col && load_check) load_start=i; load_check=false; end
        if (length(temp_pre)==load_col && length(temp)!=load_col) load_end=i-1; end

        if (length(temp)==shunt_col && temp_sh=="'" && shunt_check) shunt_start=i; shunt_check=false; end
        if (length(temp_pre)==shunt_col && (length(temp)!=shunt_col || temp_sh!="'") && temp_pre_sh=="'") shunt_end=i-1; end

        if (length(temp)==gen_col && gen_check) gen_start=i; gen_check=false; end
        if (length(temp_pre)==gen_col && length(temp)!=gen_col) gen_end=i-1; end

        if (length(temp)==branch_col && branch_check) branch_start=i; branch_check=false; end
        if (length(temp_pre)==branch_col && length(temp)!=branch_col) branch_end=i-1; end

        if (length(temp)==Tcol1 && length(temp1)==Tcol2 && length(temp2)==Tcol3 && length(temp3)==Tcol4 && trans_check) trans_start=i; trans_check=false; end
        if (length(temp)==Tcol4 && length(temp_pre)==Tcol3 && length(temp1)!=Tcol1) trans_end=i; end

        if (length(temp)==Sshunt_col && Sshunt_check) Sshunt_start=i; Sshunt_check=false; end
        if (length(temp_pre)==Sshunt_col && length(temp)!=Sshunt_col) Sshunt_end=i-1; end
    end

    (network1["bus"], bus_shift)=add_buses(network1["bus"], raw_string, bus_start, bus_end-bus_start+1);
    (network1["bus"], network1["gen"], slack) =add_gens(network1["bus"], network1["gen"], bus_shift, network1["baseMVA"], raw_string, gen_start, gen_end-gen_start+1);
    # @save "slack.jld2" slack
    network1["bus"]=add_loads(network1["bus"], bus_shift, network1["baseMVA"], raw_string, load_start, load_end-load_start+1);
    network1["bus"]=add_shunts(network1["bus"], bus_shift, network1["baseMVA"], raw_string, shunt_start, shunt_end-shunt_start+1);
    network1=merge(network1, Dict("slack"=>slack));
    network1["branch"]=add_Lbranches(network1["branch"], bus_shift, network1["baseMVA"], raw_string, branch_start, branch_end-branch_start+1);
    (network1["bus"], network1["branch"])=add_Tbranches(network1["bus"], network1["branch"], bus_shift, network1["baseMVA"], raw_string, branch_end-branch_start+1, trans_start, trans_end-trans_start+1);
    (network1["bus"], network1["sshunt"])=add_switch_shunts(network1["bus"], network1["sshunt"], bus_shift, network1["baseMVA"], raw_string, Sshunt_start, Sshunt_end-Sshunt_start+1);
    network1=merge(network1, Dict("nbal"=>Dict{String,Any}()));
    for i=1:length(network1["bus"])
        network1["nbal"]=merge(network1["nbal"], Dict(string(i)=>Dict()));
        network1["nbal"][string(i)]=merge(network1["nbal"][string(i)], Dict("gen"=>zeros(Int32,0)), Dict("Sij"=>zeros(Int32,0)), Dict("Sji"=>zeros(Int32,0)), Dict("shunt"=>zeros(Int32,0)));
    end

    for g=1:length(network1["gen"]) gen=network1["gen"][string(g)];
        if (gen["gen_bus_index"]*gen["gen_status"]>0) push!(network1["nbal"][string(gen["gen_bus_index"])]["gen"],g); end
    end
    for b=1:length(network1["branch"]) br=network1["branch"][string(b)];
        if (br["f_bus_index"]*br["br_status"]>0) push!(network1["nbal"][string(br["f_bus_index"])]["Sij"], b); end
        if (br["t_bus_index"]*br["br_status"]>0) push!(network1["nbal"][string(br["t_bus_index"])]["Sji"], b); end
    end
    for sh=1:length(network1["sshunt"])-1 shunt=network1["sshunt"][string(sh)];
        if (shunt["shunt_bus_index"]*shunt["status"]>0) push!(network1["nbal"][string(shunt["shunt_bus_index"])]["shunt"],sh); end
    end
    return(network1, bus_shift)
end

"--------------------------------------------------
 Readiang 'con' file for node5 (Network Data File)
---------------------------------------------------"
function adding_con_line_info_node5(contingency, Branch_Dict, LconList)
    fc=contingency["f_bus"];
    tc=contingency["t_bus"];
    CTc=contingency["CT"];
    for j in Branch_Dict
        fb=j[2]["f_bus"];
        tb=j[2]["t_bus"];
        CTb=j[2]["ckt"];
        if (fb==fc && tb==tc && CTb==CTc)
            index=j[2]["index"];
            append!(LconList,index);
        end
    end
    return(LconList)
end
function process_con_node5(PAROTS_flag, path_con, branch, bus_index)
    con_string = readlines(open(path_con));
    LconList=zeros(Int32,0);
    for i=1:length(con_string)-1
        temp1 = split(con_string[i]," "; limit=0, keepempty=false); temp2 = split(con_string[i+1]," "; limit=0, keepempty=false);
        if (length(temp1)==2 && length(temp2)==10)
            f_bus_index=parse(Int64,temp2[5]); f_bus=bus_index[string(f_bus_index)];
            t_bus_index=parse(Int64,temp2[8]); t_bus=bus_index[string(t_bus_index)]; CT_bus=temp2[10];
            contingency=Dict("f_bus"=>f_bus_index, "t_bus"=>t_bus_index, "CT"=>CT_bus);
            LconList=adding_con_line_info_node5(contingency, branch, LconList);
        end
    end
    # data=Dict("data"=>LconList);
    return(LconList)
    # @save "LconList.bson" data
    # @save "LconList.bson" Dict("data"=>LconList)
end


immutable VLineGeometry <: Gadfly.GeometryElement
    color::Union(ColorValue, Nothing)
    size::Union(Measure, Nothing)

    function VLineGeometry(; color=nothing,
                           size::Union(Measure, Nothing)=nothing)
        new(color === nothing ? nothing : Color.color(color), size)
    end
end

const vline = VLineGeometry

    function element_aesthetics(::VLineGeometry)
    [:xintercept]
end

# Generate a form for the vline geometry
function render(geom::VLineGeometry, theme::Gadfly.Theme,
                aes::Gadfly.Aesthetics, data::Gadfly.Data,
                scales::Dict{Symbol, ScaleElement})
    Gadfly.assert_aesthetics_defined("Geom.vline", aes, :xintercept)

    color = geom.color === nothing ? theme.default_color : geom.color
    size = geom.size === nothing ? theme.line_width : geom.size

    return compose!(
        context(),
        Compose.line([{(x, 0h), (x, 1h)} for x in aes.xintercept]),
        stroke(color),
        linewidth(size),
        svgclass("yfixed"))
end


"""データ可視化"""

using Plots
#using LaTeXStrings

"""基本的なものだけplot"""
function plot_simulation_data(data, savepath)
    println("基本グラフを作成中...")
    q1, q2, q3, q4, q5, q6, q7 = split_vec_of_arrays(data.q)
    fig_q = plot(data.t, q1, ylabel="q", label="_q1", legend=:outerright)
    plot!(fig_q, data.t, q2, label="_q2")
    plot!(fig_q, data.t, q3, label="_q3")
    plot!(fig_q, data.t, q4, label="_q4")
    plot!(fig_q, data.t, q5, label="_q5")
    plot!(fig_q, data.t, q6, label="_q6")
    plot!(fig_q, data.t, q7, label="_q7")

    q1, q2, q3, q4, q5, q6, q7 = split_vec_of_arrays(data.dq)
    fig_dq = plot(data.t, q1, ylabel="dq", label="_w1", legend=:outerright)
    plot!(fig_dq, data.t, q2, label="_w2")
    plot!(fig_dq, data.t, q3, label="_w3")
    plot!(fig_dq, data.t, q4, label="_w4")
    plot!(fig_dq, data.t, q5, label="_w5")
    plot!(fig_dq, data.t, q6, label="_w6")
    plot!(fig_dq, data.t, q7, label="_w7")

    q1, q2, q3, q4, q5, q6, q7 = split_vec_of_arrays(data.ddq)
    fig_ddq = plot(data.t, q1, ylabel="ddq", label="_a1", legend=:outerright)
    plot!(fig_ddq, data.t, q2, label="_a2")
    plot!(fig_ddq, data.t, q3, label="_a3")
    plot!(fig_ddq, data.t, q4, label="_a4")
    plot!(fig_ddq, data.t, q5, label="_a5")
    plot!(fig_ddq, data.t, q6, label="_a6")
    plot!(fig_ddq, data.t, q7, label="_a7")

    q1, q2, q3, q4, q5, q6, q7 = split_vec_of_arrays(data.desired_ddq)
    fig_desired_ddq = plot(data.t, q1, ylabel="desired ddq", label="a*1", legend=:outerright)
    plot!(fig_desired_ddq, data.t, q2, label="a*2")
    plot!(fig_desired_ddq, data.t, q3, label="a*3")
    plot!(fig_desired_ddq, data.t, q4, label="a*4")
    plot!(fig_desired_ddq, data.t, q5, label="a*5")
    plot!(fig_desired_ddq, data.t, q6, label="a*6")
    plot!(fig_desired_ddq, data.t, q7, label="a*7")

    q1, q2, q3, q4, q5, q6, q7 = split_vec_of_arrays(data.u)
    fig_u = plot(data.t, q1, ylabel="u", label="u*1", legend=:outerright)
    plot!(fig_u, data.t, q2, label="u*2")
    plot!(fig_u, data.t, q3, label="u*3")
    plot!(fig_u, data.t, q4, label="u*4")
    plot!(fig_u, data.t, q5, label="u*5")
    plot!(fig_u, data.t, q6, label="u*6")
    plot!(fig_u, data.t, q7, label="u*7")

    fig_error = plot(
        data.t, data.error,
        label="err", ylabel="error [m]", legend=:outerright,
        ylim=(0, maximum(data.error))
    )
    fig_dis_to_obs = plot(
        data.t, data.dis_to_obs,
        label="obs", ylabel="min distance to obs [m]", ylims=(0.0,), legend=:outerright
    )

    fig_integrate = plot(
        fig_q, fig_dq, fig_ddq, fig_desired_ddq, fig_u, fig_error, fig_dis_to_obs,
        layout=(7,1),
        size=(500,1400)
    )

    fname = savepath * "basic_his.png"
    savefig(fig_integrate, fname)

    println("基本グラフ作成完了")
end

"""rmpのグラフに使用"""
function styling(i::Int64, j::Int64)
    if i == 1
        n = "ro_"
        ls = :dash
        lw = 5
    elseif i == 9
        n = "ee_"
        ls = :dashdot
        lw = 5
    else
        n = "c" * string(i-1) * "_"
        ls = :solid
        lw = :auto
    end
    n *= string(j)

    return (label=n, linestyle=ls, linewidth=lw)
end

"""rmpの力fをグラフ化"""
function plot_rmp_f(data)
    f_norms_list = get_f_norms_from_nodes_list(
        length(data.q), data.nodes
    )

    fig = plot()
    for (i, fs) in enumerate(f_norms_list)
        for (j, f) in enumerate(fs)
            n = styling(i,j)
            plot!(
                fig,
                data.t, f, label = n.label,
                legend=:outerright, #linestyle=n.linestyle,
                linewidth=n.linewidth
            )
        end
    end

    plot!(
        fig,
        xlabel="time [sec]", ylabel="norm(f)",
        xlims=(0, data.t[end]), ylims=(0,)
    )

    return fig
end

"""rmp関連だけplot"""
function plot_rmp(data, savepath)
    println("rmpグラフを作成中...")

    q1, q2, q3, q4, q5, q6, q7 = split_vec_of_arrays(data.q)
    fig_q = plot(data.t, q1, ylabel="q", label="_q1_", legend=:outerright)
    plot!(fig_q, data.t, q2, label="_q2_")
    plot!(fig_q, data.t, q3, label="_q3_")
    plot!(fig_q, data.t, q4, label="_q4_")
    plot!(fig_q, data.t, q5, label="_q5_")
    plot!(fig_q, data.t, q6, label="_q6_")
    plot!(fig_q, data.t, q7, label="_q7_")
    plot!(xlims=(0, data.t[end]),)

    fig_error = plot(
        data.t, data.error,
        label="err_", ylabel="error [m]",
        xlims=(0.0, data.t[end]), legend=:outerright,
        ylim=(0, maximum(data.error))
    )

    fig_force = plot(
        data.t, norm.(data.Fc),
        label="extF", ylabel="eforce [N]",
        xlims=(0.0, data.t[end]), legend=:outerright,
        #ylim=(0, maximum(data.error))
    )

    fig_dis_to_obs = plot(
        data.t, data.dis_to_obs,
        label="obs_", ylabel="min distance to obs [m]",
        xlims=(0, data.t[end]), legend=:outerright
    )

    fig_f = plot_rmp_f(data)

    q1, q2, q3, q4, q5, q6, q7 = split_vec_of_arrays(data.desired_ddq)
    fig_desired_ddq = plot(data.t, q1, ylabel="desired ddq", label="_a*1", legend=:outerright)
    plot!(fig_desired_ddq, data.t, q2, label="_a*2")
    plot!(fig_desired_ddq, data.t, q3, label="_a*3")
    plot!(fig_desired_ddq, data.t, q4, label="_a*4")
    plot!(fig_desired_ddq, data.t, q5, label="_a*5")
    plot!(fig_desired_ddq, data.t, q6, label="_a*6")
    plot!(fig_desired_ddq, data.t, q7, label="_a*7")

    fig_integrate = plot(
        fig_q, fig_error, fig_force, fig_dis_to_obs, fig_f, fig_desired_ddq,
        layout=(6,1),
        size=(700,1900)
    )

    fname = savepath * "rmp_his.png"
    savefig(fig_integrate, fname)

    println("rmpグラフを作成完了")
end

"""ロボットアームを描画（q, dqの時刻歴から）"""
function draw_arm(q=q_neutral, dq=zeros(Float64, 7), goal=nothing, obs=nothing, t=nothing, jl=nothing)
    _, _, _, _, _, _, _, _, cpoints_x_global, _, joints_x_global, _, = calc_all(q, dq)

    fig = plot()
    x, y, z = split_vec_of_arrays(joints_x_global)
    plot!(
        fig,
        x, y, z,
        aspect_ratio = 1,
        marker=:circle,
        markerα = 0.5,
        label = "joints",
        xlabel = "X[m]", ylabel = "Y[m]", zlabel = "Z[m]"
    )

    cname = ("1", "2", "3", "4", "5", "6", "7", "GL")
    for (i, cs) in enumerate(cpoints_x_global)
        x, y, z = split_vec_of_arrays(cs)
        scatter!(
            fig,
            x, y, z,
            label = cname[i],
            markersize=5,
        )
    end

    if !isnothing(goal)
        scatter!(
            [goal.x[1]], [goal.x[2]], [goal.x[3]],
            markershape=:star6,
            markersize=8,
        )
    end

    if !isnothing(obs)
        _o = get_x_from_State(obs)
        x, y, z = split_vec_of_arrays(_o)
        scatter!(
            x, y, z,
            markershape=:circle,
            markerα = 0.5,
            #markersize=2,
            #markercolor="black"
        )
    end

    x_max = 1.0
    x_min = -1.0
    y_max = 0.2
    y_min = -1.0
    z_max = 2.0
    z_min = 0.0
    max_range = max(x_max-x_min, y_max-y_min, z_max-z_min)*0.5
    x_mid = (x_max + x_min) / 2
    y_mid = (y_max + y_min) / 2
    z_mid = (z_max + z_min) / 2

    plot!(
        fig,
        xlims=(x_mid-max_range, x_mid+max_range),
        ylims=(y_mid-max_range, y_mid+max_range),
        zlims=(z_mid-max_range, z_mid+max_range),
        legend = false,
    )

    id = findall(x->x==true, jl)

    fig2 = plot(fig, camera=(90, 0))
    fig3 = plot(fig, camera=(0, 90))
    fig4 = plot(fig, camera=(0,0))
    fig_all = plot(
        fig, fig2, fig3, fig4, layout=(2, 2),
        size=(1000, 1000),
        title = string(round(t, digits=2)) * "[s]" * ", " * string(id)
    )
    return fig_all
end

"""アニメ制作"""
function make_animation(data, path)
    println("アニメ作成中...")
    # 枚数決める
    #println(data.t)
    epoch_max = 100
    epoch = length(data.t)
    if epoch < epoch_max
        step = 1
    else
        step = div(epoch, epoch_max)
    end

    #println(step)

    anim = Animation()
    @gif for i in 1:step:length(data.q)
        # _fig = draw_arm(
        #     data.q[i], data.dq[i], data.goal[i], data.obs[i], data.t[i], data.jl[i]
        # )

        _fig = draw_arm(
            data.nodes[i], data.goal[i], data.obs[i], data.t[i], data.jl[i]
        )

        frame(anim, _fig)
    end

    fname = path *  "animation.gif"

    gif(anim, fname, fps = 60)

    println("アニメ作成完了")
end

"""ロボットアームを描画（nodesから）"""
function draw_arm(nodes, goal=nothing, obs=nothing, t=nothing, jl=nothing)

    fig = plot()
    x, y, z = nodes |> get_joint_positions_from_nodes |> split_vec_of_arrays
    plot!(
        fig,
        x, y, z,
        aspect_ratio = 1,
        marker=:circle,
        markerα = 0.5,
        label = "joints",
        xlabel = "X[m]", ylabel = "Y[m]", zlabel = "Z[m]"
    )

    cname = ("1", "2", "3", "4", "5", "6", "7", "GL")

    cpoints_x_global = nodes |> get_cpoints_from_nodes
    for (i, cs) in enumerate(cpoints_x_global)
        x, y, z = split_vec_of_arrays(cs)
        scatter!(
            fig,
            x, y, z,
            label = cname[i],
            markersize=5,
        )
    end

    if !isnothing(goal)
        scatter!(
            [goal.x[1]], [goal.x[2]], [goal.x[3]],
            markershape=:star6,
            markersize=8,
        )
    end

    if !isnothing(obs)
        _o = get_x_from_State(obs)
        x, y, z = split_vec_of_arrays(_o)
        scatter!(
            x, y, z,
            markershape=:circle,
            markerα = 0.5,
            #markersize=2,
            #markercolor="black"
        )
    end

    x_max = 1.0
    x_min = -1.0
    y_max = 0.2
    y_min = -1.0
    z_max = 2.0
    z_min = 0.0
    max_range = max(x_max-x_min, y_max-y_min, z_max-z_min)*0.5
    x_mid = (x_max + x_min) / 2
    y_mid = (y_max + y_min) / 2
    z_mid = (z_max + z_min) / 2

    plot!(
        fig,
        xlims=(x_mid-max_range, x_mid+max_range),
        ylims=(y_mid-max_range, y_mid+max_range),
        zlims=(z_mid-max_range, z_mid+max_range),
        legend = false,
    )

    id = findall(x->x==true, jl)

    fig2 = plot(fig, camera=(90, 0))
    fig3 = plot(fig, camera=(0, 90))
    fig4 = plot(fig, camera=(0,0))
    fig_all = plot(
        fig, fig2, fig3, fig4, layout=(2, 2),
        size=(1000, 1000),
        title = string(round(t, digits=2)) * "[s]" * ", " * string(id)
    )
    return fig_all
end


using SuperStiff.PeriodizeSC
using NPZ
using Glob

# You have to run convert_converged_green() before running this program properly (produce both selfR and greenR)

filename_to_write = "PER_periodized_zz.dat"

t = 1.0; tpp = 0.0
pwd_ = pwd()
path_ = pwd_*"/"*filename_to_write
Grid_ = 100 # Resolution of k-space that is relevant ONLY for inplane calculations
OPT_ = "PER"  # Options are CUM, PER and TR
AXIS_ = "zz" # Options are xx (a axis), yy (b axis) and zz (c axis)

macro assertion(ex, text)
    :($ex ? nothing : error("Assertion failed: ", $(text)))
end

existing_mu = Array{Float64}(); existing_beta = Array{Float64}(); existing_U = Array{Float64}(); existing_tp = Array{Float64}()
if isfile(path_)
    existing_data = readdlm(path_, header=true)
    existing_tp = existing_data[1][:,2]
    existing_U = existing_data[1][:,3]
    existing_mu = existing_data[1][:,4]
    existing_beta = existing_data[1][:,5]
    #println("length(existing_mu): ", existing_mu, "\n", "length(existing_U): ", existing_U, "\n", "length(existing_beta): ", existing_beta)
    @assertion(length(existing_tp)==length(existing_mu)==length(existing_U)==length(existing_beta), "Length of existing_* differs from each other.")
else
    println("The file containing the values of the superfluid stiffness is not existing. Will be created!")
end
existing_data = collect(zip(existing_tp,existing_U,existing_mu,existing_beta))
println("existing_data array: ", existing_data)

function grasp_mu(mu_file_path::String)
    data_mu = readdlm(mu_file_path)[:,2][end]
    return data_mu
end

function load_iwn_list(w_file_path)
    data_iwn = readdlm(w_file_path)[:,1]
    return convert(Array{Complex{Float64},1},data_iwn)
end

pattern_SE = r"selfR_aver"
pattern_GE = r"greenR_aver"
list_of_npy_SE_files = Array{String,1}()
list_of_npy_GE_files = Array{String,1}()
for (root, dirs, files) in walkdir(".")
    #println("Files in $root")
    for file in files
        if ismatch(pattern_SE,file)
            push!(list_of_npy_SE_files,joinpath(root, file)) # path to files
        elseif ismatch(pattern_GE,file)
            push!(list_of_npy_GE_files,joinpath(root, file))
        end
    end
end
#println(list_of_npy_GE_files,"\n",list_of_npy_SE_files)

@assertion(length(list_of_npy_SE_files)==length(list_of_npy_GE_files), "Length of list_of_npy_SE_files differs from that of list_of_npy_GE_files.")

regex_exp_beta_n = r"(?<=beta)(\d+\.?\d*[n]\Z)"
regex_exp_beta = r"(?<=beta)(\d+\.?\d*[^n]\Z)"
regex_exp_tp = r"(?<=tp)([-+]\d*\.\d+|\d+)"
regex_exp_U = r"(?<=U)([-+]\d*\.\d+|\d+)"
regex_exp_mu = r"(?<=m)(\d*\.\d+|\d+)"

list_of_npy_GE_files_todo  = Array{String,1}()
list_of_npy_SE_files_todo = Array{String,1}()
list_list_iwn = Array{Array{Complex{Float64},1},1}()
list_N = Array{Float64,1}()
list_params = Array{Tuple{Float64,Float64,Float64,Float64},1}()
for l in 1:length(list_of_npy_SE_files)
    beta = 0.0
    dir_name = dirname(list_of_npy_SE_files[l])
    try
        beta = parse(Float64, match(regex_exp_beta,dir_name).match)
    catch e
        println("Caught the following exception: ", e)
    end
    U = parse(Float64, match(regex_exp_U,dir_name).match)
    mu = parse(Float64, match(regex_exp_mu,dir_name).match)
    tp = parse(Float64, match(regex_exp_tp,dir_name).match)
    tuple_data = ((tp,U,mu,beta),)
    if isempty(find(existing_data.==tuple_data)) && beta != 0.0
        println("tuple_data: ",tuple_data)
        N_files = glob(dir_name*"/*N.dat") # Contains the file containing the chemical potentials
        iwn_files = glob(dir_name*"/*w.dat") # Contains the file containing the Matsubara frequencies
        NN = grasp_mu(N_files...)
        iwn_list = load_iwn_list(iwn_files...)
        push!(list_N,NN)
        push!(list_list_iwn,1.0im*iwn_list) ### Added 1.0im
        push!(list_params,(tp,U,mu,beta))
        push!(list_of_npy_GE_files_todo,list_of_npy_GE_files[l])
        push!(list_of_npy_SE_files_todo,list_of_npy_SE_files[l])
    elseif beta == 0.0
        println("beta is set to: ",0.0)
    else
        println("This folder's data has already been treated!!")
        println("Set already done at index: ",find(existing_data.==tuple_data)...); println()
    end
end

println("list_params: ",list_params)

list_Kintegral_stiff = Array{Tuple{Tuple{Float64,Float64,Float64,Float64},Array{Complex{Float64},2}},1}()
list_Kintegral_DOS = Array{Tuple{Tuple{Float64,Float64,Float64,Float64},Array{Complex{Float64},2}},1}()
list_mu = Array{Float64,1}(); list_U = Array{Float64,1}(); list_beta = Array{Float64,1}(); list_gap = Array{Float64,1}()
list_tp = Array{Float64,1}(); list_N = Array{Float64,1}()
@time for l in 1:1:length(list_of_npy_GE_files_todo)
    dir_name_l = dirname(list_of_npy_GE_files_todo[l])
    SEvec = npzread(list_of_npy_SE_files_todo[l])
    println("SEvec: ",size(SEvec),"\n")
    GEvec = npzread(list_of_npy_GE_files_todo[l])
    println("GEvec: ",size(GEvec),"\n")
    len_iwn_G = size(GEvec)[1]; len_iwn_SE = size(SEvec)[1]
    len_iwn_G != len_iwn_SE && throw(ErrorException("Must have same number of Matsubara frequencies for self-energy and Green's function!"))
    N_data = readdlm(dir_name_l*"/"*"N.dat")
    list_green_npy = glob(dir_name_l*"/greenR*.npy")
    list_green_npy = Array{String,1}([dir_name for dir_name in list_green_npy if !contains(dir_name,"greenR_aver")])
    println("list_green_npy: ",list_green_npy)
    len_N_aver = length(list_green_npy) #Important to know on how many N values one averages (comes down to number of Green's function used to average)
    N_aver = 0.0
    try
        N_aver = sum(N_data[:,2][end-len_N_aver:end])/(len_N_aver+1)
        println("N_aver: ",N_aver,"\t data_N: ",N_data[:,2][end-len_N_aver:end],"\t",len_N_aver,"\n")
    catch e
        if isa(e,BoundsError)
            println("Caught BoundsError! Not enough iterations to average over.")
        else
            println("Unidentified error occured at runtime.")
        end
    end
    params_tuple = list_params[l]
    tp = params_tuple[1]; beta = params_tuple[4]; mu = params_tuple[3]
    anomalous_GE = abs((1/beta)*sum(GEvec[:,1,6]))
    println("anomalous GE: ", anomalous_GE)
    push!(list_gap, anomalous_GE)
    k_array_prebuild = Array{Array{Float64,2},1}()
    tktilde_prebuild = Array{Array{Complex{Float64},2},2}(0,0)
    modelvec = PeriodizeSC.ModelVector(t,tp,tpp,mu,list_list_iwn[l],SEvec)
    cal = Matrix{Complex{Float64}}(0,0)
    if AXIS_ == "zz"
        println("zz")
        if OPT_ == "PER"
            println("PER")
            cal = PeriodizeSC.calcintegral_BZ(modelvec, PeriodizeSC.make_stiffness_kintegrand_SC, len_sEvec_c=len_iwn_G)  ## cal variable exits an array whose size is (length(iw_n),2). The second column holds the superfluid stiffness data.
            println("cal: ", cal)
        elseif OPT_ == "CUM"
            println("CUM")
            cal = PeriodizeSC.calcintegral_BZ(modelvec, PeriodizeSC.make_stiffness_kintegrand_cum_SC, len_sEvec_c=len_iwn_G) ## Idem cal
            println("cal: ", cal)
        elseif OPT_ == "TR"
            println("TR")
            cal = PeriodizeSC.calcintegral_BZ(modelvec, PeriodizeSC.make_stiffness_trace_G_kintegrand, len_sEvec_c=len_iwn_G) ## Idem cal
        end
    elseif AXIS_ == "yy"
        println("yy")
        fct_array = [PeriodizeSC.DyDyEpsilonbark]
        for f in fct_array
            push!(k_array_prebuild, PeriodizeSC.k_grid(PeriodizeSC.Model(modelvec,1),Grid_,f))
        end
        tktilde_prebuild = PeriodizeSC.k_grid(PeriodizeSC.Model(modelvec,1),Grid_,PeriodizeSC.tktilde)
        if OPT_ == "PER"
            println("PER")
            cal = PeriodizeSC.stiffness_NOCOEX_Per_Cum_ab_k_grid(modelvec,k_array_prebuild,tktilde_prebuild,Grid_,0,0,len_sEvec_c=len_iwn_G)
        elseif OPT_ == "CUM"
            println("CUM")
            cal = PeriodizeSC.stiffness_NOCOEX_Per_Cum_ab_k_grid(modelvec,k_array_prebuild,tktilde_prebuild,Grid_,1,0,len_sEvec_c=len_iwn_G)
        end
    elseif AXIS_ == "xx"
        println("xx")
        fct_array = [PeriodizeSC.DxDxEpsilonbark]
        for f in fct_array
            push!(k_array_prebuild, PeriodizeSC.k_grid(PeriodizeSC.Model(modelvec,1),Grid_,f))
        end
        tktilde_prebuild = PeriodizeSC.k_grid(PeriodizeSC.Model(modelvec,1),Grid_,PeriodizeSC.tktilde)
        if OPT_ == "PER"
            println("PER")
            cal = PeriodizeSC.stiffness_NOCOEX_Per_Cum_ab_k_grid(modelvec,k_array_prebuild,tktilde_prebuild,Grid_,0,0,len_sEvec_c=len_iwn_G)
        elseif OPT_ == "CUM"
            println("CUM")
            cal = PeriodizeSC.stiffness_NOCOEX_Per_Cum_ab_k_grid(modelvec,k_array_prebuild,tktilde_prebuild,Grid_,1,0,len_sEvec_c=len_iwn_G)
        end
    end
    push!(list_Kintegral_stiff,(params_tuple,cal))
    push!(list_mu,mu)
    push!(list_U,params_tuple[2])
    push!(list_beta,beta)
    push!(list_tp,tp)
    push!(list_N,N_aver)
end
println("list_mu: ", list_mu)
stiffness = Array{Float64,1}()
for j in 1:size(list_Kintegral_stiff)[1]
    stiffness_tmp = 0.0
    println(list_Kintegral_stiff[j][1])
    for ii in 1:size(list_Kintegral_stiff[j][2])[1]
        stiffness_tmp+=list_Kintegral_stiff[j][2][ii, 2]
    end
    beta = list_Kintegral_stiff[j][1][4]
    println("beta: ", beta)
    push!(stiffness,(2.0/beta)*real(stiffness_tmp)) #Factor two to sum over all the imaginary axis
    println((2.0/beta)*imag(stiffness_tmp))
end

@assertion(length(list_mu)==length(stiffness), "Length of list_mu differs from that of stiffness.")

params_stiff = hcat(list_N,list_tp,list_U,list_mu,list_beta,stiffness,list_gap)
map!(x->round.(x,8),params_stiff,params_stiff)
println("Stiffness: ", stiffness)
if isfile(path_)
    open(path_,"a") do io
        writedlm(io, params_stiff, "\t\t")
    end
else
    f = open(filename_to_write, "w")
    write(f, "n"*"\t\t"*"tp"*"\t\t"*"U"*"\t\t"*"mu"*"\t\t"*"beta"*"\t\t"*"Stiff"*"\t\t"*"SC_Gap"*"\n")
    writedlm(f, params_stiff, "\t\t")
    close(f)
end


module ANYmalRobot

using RigidBodyDynamics

packagepath() = joinpath(@__DIR__, "..")
urdfpath() = joinpath(packagepath(), "anymal_b_simple_description/urdf/anymal.urdf")

function mechanism(::Type{T} = Float64;
                   floating = true,
                   # contactmodel = default_contact_model(),
                   remove_fixed_tree_joints = true, add_flat_ground = false) where {T}
    mechanism = RigidBodyDynamics.parse_urdf(urdfpath(); scalar_type = T, floating = floating, remove_fixed_tree_joints = remove_fixed_tree_joints)

    remove_fixed_tree_joints && remove_fixed_tree_joints!(mechanism)

    return mechanism
end

end # module


abstract Evaluator

type ProblemEvaluator <: Evaluator
  problem::OptimizationProblem
  fitness_scheme::FitnessScheme
  archive::Archive
  num_evals::Int64
  last_fitness

  ProblemEvaluator(problem::OptimizationProblem; archive = false,
    fitness_scheme = FloatFitness()) = begin
    archive = archive || TopListArchive(numdims(problem))
    new(problem, fitness_scheme, archive, 0, nothing)
  end
end

worst_fitness(e::Evaluator) = worst_fitness(e.fitness_scheme)
num_evals(e::Evaluator) = e.num_evals
numdims(e::Evaluator) = numdims(e.problem)
search_space(e::Evaluator) = search_space(e.problem)
describe(e::Evaluator) = "Problem: $(name(e.problem)) (dimensions = $(numdims(e)))"
problem_summary(e::Evaluator) = "$(name(e.problem))_$(numdims(e))d"
problem(e::Evaluator) = e.problem

function evaluate(e::Evaluator, candidate)
  e.last_fitness = eval1(candidate, e.problem)
  e.num_evals += 1
  add_candidate!(e.archive, e.last_fitness, candidate, e.num_evals)
  e.last_fitness
end

# A way to get the fitness of the last evaluated candidate. Leads to nicer
# code if we can first test if better than or worse than existing candidates
# and only want the fitness itself if the criteria fulfilled.
last_fitness(e::Evaluator) = e.last_fitness

function is_better(e::Evaluator, f1::Fitness, f2::Fitness)
  is_better(f1, f2, e.fitness_scheme)
end

function is_better(e::Evaluator, candidate, fitness::Fitness)
  is_better(evaluate(e, candidate), fitness, e.fitness_scheme)
end

function best_of(e::Evaluator, candidate1, candidate2)
  f1 = evaluate(e, candidate1)
  f2 = evaluate(e, candidate2)
  if is_better(f1, f2, e.fitness_scheme)
    return candidate1, f1
  else
    return candidate2, f2
  end
end

function rank_by_fitness(e::Evaluator, candidates)
  # Note that we re-evaluate all candidates here. This might be wasteful and
  # we should cache if evaluations are costly.
  fitness = [(c[1], c[2], evaluate(e, c[1])) for c=candidates]
  sort(fitness; by = (t) -> t[3])
end

function fitness_is_within_ftol(e::Evaluator, ftolerance; index = 1)
  try
    fitness_is_within_ftol(e.problem, ftolerance, best_fitness(e.archive); index = index)
  catch
    false # In case we have no information yet about the best fitness
  end
end