Skip to content

Commit 7eb0ab8

Browse files
Merge pull request #34 from ConnectedSystems/doc-changes
Standardizing docstrings and code format
2 parents a179e48 + a7d3495 commit 7eb0ab8

File tree

11 files changed

+350
-261
lines changed

11 files changed

+350
-261
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,6 @@ julia>] test
6262

6363
## Usage
6464

65-
Streamfall is currently unregistered but can be added to a Julia environment directly from
66-
the Package manager:
67-
68-
```bash
69-
julia>] add https://github.com/ConnectedSystems/Streamfall.jl#main
70-
```
71-
7265
The examples below use data from the CAMEL-AUS dataset, available here:
7366

7467
> Fowler, K. J. A., Acharya, S. C., Addor, N., Chou, C., and Peel, M. C.: CAMELS-AUS: hydrometeorological time series and landscape attributes for 222 catchments in Australia, Earth Syst. Sci. Data, 13, 3847–3867, https://doi.org/10.5194/essd-13-3847-2021, 2021.

src/Climate.jl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function Climate(file_path::String, p_id, et_id; t_id="_T")
1919
end
2020

2121
"""
22+
extract_flow(
23+
data::DataFrame, gauge_id::String; suffix::String="_Q"
24+
)::DataFrame
25+
2226
Extract streamflow data from file.
2327
2428
Streamflow (Q) column is identified the Gauge ID.
@@ -35,13 +39,13 @@ e.g., ("000001_Q")
3539
DataFrame of observations for selected gauge.
3640
"""
3741
@inline function extract_flow(
38-
data::DataFrame, gauge_id::String, suffix::String="_Q"
42+
data::DataFrame, gauge_id::String; suffix::String="_Q"
3943
)::DataFrame
4044
target = data[:, ["Date", gauge_id * suffix]]
4145
try
42-
target[!, gauge_id * suffix] = convert.(Float64, target[!, gauge_id * suffix])
46+
target[!, gauge_id*suffix] = convert.(Float64, target[!, gauge_id*suffix])
4347
catch
44-
target[!, gauge_id * suffix] = convert.(Union{Float64,Missing}, target[!, gauge_id * suffix])
48+
target[!, gauge_id*suffix] = convert.(Union{Float64,Missing}, target[!, gauge_id*suffix])
4549
end
4650

4751
rename!(target, gauge_id * suffix => gauge_id)
@@ -78,8 +82,9 @@ Extract rainfall data for a given node.
7882
function rainfall_data(node::NetworkNode, climate::Climate)::DataFrame
7983
data = climate.climate_data
8084
rain_col = filter(x -> occursin(node.name, x)
81-
& occursin(climate.rainfall_id, x),
82-
names(data))[1]
85+
&
86+
occursin(climate.rainfall_id, x),
87+
names(data))[1]
8388

8489
return data[:, rain_col]
8590
end
@@ -107,15 +112,18 @@ function climate_values(node::NetworkNode, climate::Climate, timestep::Int)
107112

108113
# TODO : Catch instances where data is not found (raises BoundsError)
109114
rain_col = filter(x -> occursin(node_name, x)
110-
& occursin(climate.rainfall_id, x),
111-
names(data))[1]
115+
&
116+
occursin(climate.rainfall_id, x),
117+
names(data))[1]
112118
et_col = filter(x -> occursin(node_name, x)
113-
& occursin(climate.et_id, x),
114-
names(data))[1]
119+
&
120+
occursin(climate.et_id, x),
121+
names(data))[1]
115122
t_col = try
116123
filter(x -> occursin(node_name, x)
117-
& occursin(climate.t_id, x),
118-
names(data))[1]
124+
&
125+
occursin(climate.t_id, x),
126+
names(data))[1]
119127
catch err
120128
if !(err isa BoundsError)
121129
rethrow(err)
@@ -148,11 +156,13 @@ function climate_values(node::NetworkNode, climate::Climate)
148156

149157
# TODO : Catch instances where data is not found (raises BoundsError)
150158
rain_col = filter(x -> occursin(node.name, x)
151-
& occursin(climate.rainfall_id, x),
152-
names(data))[1]
159+
&
160+
occursin(climate.rainfall_id, x),
161+
names(data))[1]
153162
et_col = filter(x -> occursin(node.name, x)
154-
& occursin(climate.et_id, x),
155-
names(data))[1]
163+
&
164+
occursin(climate.et_id, x),
165+
names(data))[1]
156166

157167
if isempty(rain_col) | isempty(et_col)
158168
throw(ArgumentError("No climate data found for $(node.name) at time step: $(timestep)"))

src/Network.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ function get_prop(sn::StreamfallNetwork, nid::Int64, prop::Symbol)::Any
4141
end
4242

4343

44-
"""Determine a node's connection"""
44+
"""
45+
in_or_out(g, v)
46+
47+
Determine a node's connection
48+
"""
4549
function in_or_out(g, v)
4650
ins = length(inneighbors(g, v))
4751
outs = length(outneighbors(g, v))
@@ -58,7 +62,11 @@ function in_or_out(g, v)
5862
end
5963

6064

61-
"""Find all inlets and outlets in a network."""
65+
"""
66+
find_inlets_and_outlets(sn::StreamfallNetwork)::Tuple
67+
68+
Find all inlets and outlets in a network.
69+
"""
6270
function find_inlets_and_outlets(sn::StreamfallNetwork)::Tuple
6371
g = sn.mg
6472
vs = vertices(g)
@@ -161,9 +169,14 @@ function create_node(mg::MetaDiGraph, node_name::String, details::AbstractDict,
161169
func = run_node!
162170
end
163171

164-
set_props!(mg, nid, Dict(:name=>node_name,
165-
:node=>n,
166-
:nfunc=>func))
172+
set_props!(
173+
mg, nid,
174+
Dict(
175+
:name => node_name,
176+
:node => n,
177+
:nfunc => func
178+
)
179+
)
167180

168181
this_id = nid
169182
else
@@ -307,7 +320,7 @@ function Base.show(io::IO, sn::StreamfallNetwork)
307320

308321
show_verts = vs
309322
if length(vs) > 4
310-
show_verts = [1, 2, nothing, length(vs)-1, length(vs)]
323+
show_verts = [1, 2, nothing, length(vs) - 1, length(vs)]
311324
end
312325

313326
for nid in show_verts
@@ -328,7 +341,7 @@ end
328341
Simple plot of stream network.
329342
"""
330343
function plot_network(sn::StreamfallNetwork; as_html=false)
331-
node_labels = ["$(sn[i].name)\n"*string(nameof(typeof(sn[i]))) for i in vertices(sn.mg)]
344+
node_labels = ["$(sn[i].name)\n" * string(nameof(typeof(sn[i]))) for i in vertices(sn.mg)]
332345

333346
if as_html
334347
plot_func = gplothtml
@@ -355,7 +368,7 @@ function Base.iterate(sn::StreamfallNetwork, state=1)
355368
return nothing
356369
end
357370

358-
return (sn[state], state+1)
371+
return (sn[state], state + 1)
359372
end
360373

361374

src/Nodes/DamNode.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function c_dam_outflow(discharge, irrigation_extraction)
2626
end
2727

2828

29-
Base.@kwdef mutable struct DamNode{P, A<:AbstractFloat} <: NetworkNode
29+
Base.@kwdef mutable struct DamNode{P,A<:AbstractFloat} <: NetworkNode
3030
name::String
3131
area::A
3232

@@ -74,12 +74,12 @@ function DamNode(
7474
calc_dam_outflow::Function
7575
) where {F<:Float64}
7676
return DamNode(name, area, max_storage, storage_coef,
77-
calc_dam_level, calc_dam_area, calc_dam_discharge, calc_dam_outflow,
78-
F[initial_storage], F[], F[], F[], F[], F[], F[], F[])
77+
calc_dam_level, calc_dam_area, calc_dam_discharge, calc_dam_outflow,
78+
F[initial_storage], F[], F[], F[], F[], F[], F[], F[])
7979
end
8080

8181
"""
82-
DamNode(name::String, spec::Dict)
82+
DamNode(name::String, spec::AbstractDict)
8383
8484
Create DamNode from a given specification.
8585
"""
@@ -136,7 +136,7 @@ function storage(node::DamNode)
136136
end
137137

138138
function prep_state!(node::DamNode, timesteps::Int64)
139-
resize!(node.storage, timesteps+1)
139+
resize!(node.storage, timesteps + 1)
140140
node.storage[2:end] .= 0.0
141141

142142
node.effective_rainfall = zeros(timesteps)
@@ -204,13 +204,13 @@ end
204204

205205

206206
function run_node!(node::DamNode, climate::Climate;
207-
inflow=nothing, extraction=nothing, exchange=nothing)
207+
inflow=nothing, extraction=nothing, exchange=nothing)
208208
timesteps = sim_length(climate)
209209
prep_state!(node, timesteps)
210210

211211
for ts in 1:timesteps
212212
run_node!(node, climate, ts;
213-
inflow=inflow, extraction=extraction, exchange=exchange)
213+
inflow=inflow, extraction=extraction, exchange=exchange)
214214
end
215215

216216
return nothing
@@ -289,7 +289,7 @@ function run_node!(
289289
discharge = node.calc_dam_discharge(volume, node.max_storage)
290290

291291
updated_store = update_volume(volume, inflow, gw_flux, rain, et,
292-
dam_area, extractions, discharge, node.max_storage)
292+
dam_area, extractions, discharge, node.max_storage)
293293
outflow = node.calc_dam_outflow(discharge, extractions)
294294

295295
update_state!(node, ts, updated_store, rain, et, dam_area, discharge, outflow)

0 commit comments

Comments
 (0)