Skip to content

Commit

Permalink
loadcase() now accepts file paths to .mat files
Browse files Browse the repository at this point in the history
  • Loading branch information
kersulis committed Aug 31, 2016
1 parent 48d2b63 commit fce6bea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Dict{ASCIIString,Any} with 8 entries:
"gencost" => [2.0 0.0 0.0 3.0 0.01 40.0 0.0…
```
You may load data for any network MATPOWER 5.1 supports, plus the RTS-96 (`case96`) and RTS-79 (`case79`) networks.
You may load data for any network MATPOWER 5.1 supports, plus the RTS-96 (`case96`) and RTS-79 (`case79`) networks. You may also provide a path to any `.mat` file containing MATPOWER-format data to `loadcase()`.
## Detailed network information
Expand Down
31 changes: 21 additions & 10 deletions src/loadcase.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
using MAT

""" Return a Dict containing power system data
in MATPOWER's format.
Return the list of valid case names with casenames().
"""
mpc = loadcase(caseName)
mpc = loadcase(fpath)
Return a Dict containing power system data in MATPOWER's format.
Argument may be any of the names returned by casenames(), or a
path to a .mat file containing MATPOWER-format data.
Set keyword argument `describe` to `false` for silent operation.
Set `describe` to `false` for silent operation.
"""
function loadcase(caseName::ASCIIString; describe=true)
s = joinpath(Pkg.dir(),"MatpowerCases","data")
p = joinpath(s,"$(caseName).mat")
if !isfile(p)
error("No data for network \"$(caseName)\".\nUse casenames() to list all valid names.")
function loadcase(caseName::AbstractString; describe=true)
if caseName[end-3:end] == ".mat"
p = caseName
!isfile(p) && error("File not found.")
else
s = joinpath(Pkg.dir(),"MatpowerCases","data")
p = joinpath(s,"$(caseName).mat")
!isfile(p) && error("No data for network \"$(caseName)\".\nUse casenames() to list all valid names.")
end

mpc = matread(p)["mpc"]
ds = mpc["docstring"]
if describe
Expand All @@ -20,7 +28,10 @@ function loadcase(caseName::ASCIIString; describe=true)
return mpc
end

""" Return an array containing all valid
"""
casenames()
Return an array containing all valid
MatpowerCases case names.
"""
function casenames()
Expand Down
3 changes: 3 additions & 0 deletions test/case.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ for c in casenames()
@assert isa(extract_case(c), MatpowerCases.Case)
end

c = joinpath(Pkg.dir(),"MatpowerCases","data","case118.mat")
loadcase(c; describe=false)
@assert isa(extract_case(c), MatpowerCases.Case)
2 changes: 1 addition & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ end

println("- graceful failure test")
@test_throws ErrorException loadcase("invalidcasename")

@test_throws ErrorException loadcase("doesnotexist.mat")

0 comments on commit fce6bea

Please sign in to comment.