Skip to content
Snippets Groups Projects
Data.jl 2.13 KiB
#* Functions for handling the Network Data Dictionary (NDD) of PowerModels.jl
#*------------------------------------------------------------------------------

#=
Reads the geographic bus locations (longitude and latitude coordinates in degrees) from a CSV file and adds them to the NDD. Returns the resulting NDD, in which each bus has a "bus_lat" and "bus_lon" entry. The columns containg the longitude and latitude coordinates in the CSV file should be called "SubLongitude" and "SubLatitude", respectively.
=#
function addlocs!(
        network_data::Dict{String,<:Any}, 
        csvfile::String
    )

    pos = Dict{String,Any}(
        "baseMVA"  => 100.0,
        "bus" => Dict{String,Any}(), 
        "per_unit" => true
    )

    BusData::DataFrame = CSV.File(csvfile) |> DataFrame!
    N = size(BusData, 1) # number of buses
    sizehint!(pos["bus"], N)

    for bus in eachrow(BusData)
        pos["bus"][string(bus[:Number])] = Dict(
            "bus_lon" => bus[:SubLongitude], 
            "bus_lat" => bus[:SubLatitude]
        )
    end
    update_data!(network_data, pos) # add positions to network_data
    
    return network_data
end

#=
Returns a dictionary containing geographic bus locations that are included in the NDD. The dicitonary structure is bus-index => (lon, lat). If getlims=true, the minmimum and maximum longitude and latitude coordinates are returned as well. Can be useful for plotting the grid.
=#
function getlocs(network_data::Dict{String,<:Any}; getlims=false)
    pos = Dict(
        parse(Int64, i) => (b["bus_lon"], b["bus_lat"]) 
        for (i, b) in network_data["bus"]
    )

    ### Check whether to return lon- and lat-limits or not
    if getlims == true
        N = length(network_data["bus"]) # number of buses
        lats = [
            network_data["bus"]["$i"]["bus_lat"] 
            for i in keys(network_data["bus"])
        ]
        lons = [
            network_data["bus"]["$i"]["bus_lon"] 
            for i in keys(network_data["bus"])
        ]
        xlims = (minimum(lons), maximum(lons))
        ylims = (minimum(lats), maximum(lats))
        return pos, xlims, ylims
    else
        return pos
    end
end