Skip to content

Commit 22f2aed

Browse files
committed
Using extentions instead of requires.jl
1 parent 0bd8ec2 commit 22f2aed

File tree

7 files changed

+80
-39
lines changed

7 files changed

+80
-39
lines changed

Project.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
name = "WhereTheWaterFlows"
22
uuid = "ea906314-1493-4d22-a0af-f886a20c9fba"
33
authors = ["Mauro Werder <[email protected]>"]
4-
version = "0.8.1"
4+
version = "0.9.1"
55

66
[deps]
7-
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
87
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
98

9+
[weakdeps]
10+
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
11+
12+
[extensions]
13+
PyPlotExt = "PyPlot"
14+
1015
[compat]
11-
Requires = "0.5.2, 1.0"
1216
StaticArrays = "0.11.1, 0.12, 1.0"
13-
julia = "1.1"
17+
PyPlot = "2"
18+
julia = "1.9"
1419

1520
[extras]
1621
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,29 @@ x,y,dem = peaks2(200)
3636
area, slen, dir, nout, nin, pits, c, bnds = waterflows(dem)
3737

3838
# log-upslope area as well as pits (sinks)
39-
WWF.plotarea(x, y, area, pits)
39+
WWF.plt.plotarea(x, y, area, pits)
4040

4141
# log-upslope area over contours of the dem
42-
WWF.plotarea_dem(x, y, dem, area, pits)
42+
WWF.plt.plotarea_dem(x, y, dem, area, pits)
4343

4444
# catchments
4545
figure()
46-
WWF.heatmap(x,y,c)
46+
WWF.plt.heatmap(x,y,c)
4747

4848
# A single catchment of some point. Choose one with large catchment:
4949
i, j = 50, findmax(area[50,:])[2]
5050
cc = catchment(dir, CartesianIndex(i,j))
51-
WWF.heatmap(x,y,cc)
51+
WWF.plt.heatmap(x,y,cc)
5252
plot(x[i], y[j], "<r", ms=10)
5353

5454
# stream length
5555
figure()
56-
WWF.heatmap(x,y,slen)
56+
WWF.plt.heatmap(x,y,slen)
5757

5858
demf = fill_dem(dem, pits, dir)
5959
# "lake-depth"
6060
figure()
61-
WWF.heatmap(x,y,demf.-dem)
61+
WWF.plt.heatmap(x,y,demf.-dem)
6262
```
6363

6464
In the `example/` folder there are two more complicated examples. One

examples/example.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if !(@isdefined WWF)
99
const WWF = WhereTheWaterFlows
1010
end
1111

12-
"A artificial DEM"
12+
"An artificial DEM"
1313
function ele(x, y; withpit=false, randfac=0.0)
1414
out = - (x^2 - 1)^2 - (x^2*y - x - 1)^2 + 6 + 0.1*x + 3*y
1515
if withpit
@@ -22,15 +22,15 @@ dx = 0.01
2222
xs = -1.5:dx:1
2323
ys = -0.5:dx:3.0
2424
dem = ele.(xs, ys', randfac=0.1, withpit=true);
25-
plotyes && WWF.heatmap(xs, ys, dem)
25+
plotyes && WWF.plt.heatmap(xs, ys, dem)
2626

2727
area, slen, dir, nout, nin, pits, c, bnds = WWF.waterflows(dem, drain_pits=true);
2828

2929
@assert size(dem)==(length(xs), length(ys))
30-
plotyes && WWF.plotit(xs, ys, dem)
31-
plotyes && WWF.plotarea(xs, ys, area, pits)
30+
plotyes && WWF.plt.plotit(xs, ys, dem)
31+
plotyes && WWF.plt.plotarea(xs, ys, area, pits)
3232

33-
plotyes && WWF.heatmap(xs, ys, c)
33+
plotyes && WWF.plt.heatmap(xs, ys, c)
3434

3535
demf = WWF.fill_dem(dem, pits, dir) #, small=1e-6)
36-
plotyes && WWF.heatmap(xs, ys, demf.-dem)
36+
plotyes && WWF.plt.heatmap(xs, ys, demf.-dem)

examples/subglacial-routing-feedback.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ phi = rho_w*g*bed + flotation_fraction * g * rho_i * (surface - bed)
5454

5555
# To track the amount of melted ice, we'll use three cellareas as inputs,
5656
# which in turns leads to three outputs in `area`:
57-
# - water discharge including extra melt
57+
# - water discharge due to source and extra melt
5858
# - water discharge due to extra melt only
5959
# - melt rate in m/s at each cell
6060
cellarea = (fill!(similar(surface), 0.1/day*dx*dx), # summer conditions: 10cm melt per day over the whole glacier
61-
fill!(similar(surface), 0.0),
62-
fill!(similar(surface), 0.0))
61+
fill!(similar(surface), 0.0), # no source here
62+
fill!(similar(surface), 0.0)) # no source here
6363

6464
const phi_ = phi # make const for use in below function
6565
"""
@@ -99,12 +99,12 @@ area, slen, dir, nout, nin, pits, c, bnds = WWF.waterflows(phi, drain_pits=true
9999
feedback_fn=melting)
100100

101101
# Plot it
102-
plotyes && WWF.plotit(x, y, phi)
103-
plotyes && WWF.plotarea(x, y, area[1], pits)
104-
plotyes && WWF.plotarea(x, y, area[2], pits)
102+
plotyes && WWF.plt.plotit(x, y, phi)
103+
plotyes && WWF.plt.plotarea(x, y, area[1], pits)
104+
plotyes && WWF.plt.plotarea(x, y, area[2], pits)
105105
@show extrema(area[3])
106106

107-
plotyes && WWF.heatmap(x, y, c)
107+
plotyes && WWF.plt.heatmap(x, y, c)
108108

109109
phi_filled = WWF.fill_dem(phi, pits, dir) #, small=1e-6)
110-
plotyes && WWF.heatmap(x, y, phi_filled .- phi)
110+
plotyes && WWF.plt.heatmap(x, y, phi_filled .- phi)

examples/subglacial-routing.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ phi = bed + flotation_fraction * rho_i/rho_w * (surface - bed)
4949
area, slen, dir, nout, nin, pits, c, bnds = WWF.waterflows(phi, drain_pits=true)
5050

5151
# Plot it
52-
plotyes && WWF.plotit(x, y, phi)
53-
plotyes && WWF.plotarea(x, y, area, pits)
52+
plotyes && WWF.plt.plotit(x, y, phi)
53+
plotyes && WWF.plt.plotarea(x, y, area, pits)
5454

55-
plotyes && WWF.heatmap(x, y, c)
55+
plotyes && WWF.plt.heatmap(x, y, c)
5656

5757
phi_filled = WWF.fill_dem(phi, pits, dir) #, small=1e-6)
58-
plotyes && WWF.heatmap(x, y, phi_filled .- phi)
58+
plotyes && WWF.plt.heatmap(x, y, phi_filled .- phi)

src/plotting.jl renamed to ext/PyPlotExt.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
using .PyPlot
2-
export plotarea, plotarea_dem
1+
module PyPlotExt
2+
# Some extension specific threads for my information:
3+
# - https://www.youtube.com/live/vG6ZLhe9Hns?si=7bPLZWqC-EB3AFzr&t=24578
4+
# - https://discourse.julialang.org/t/are-extension-packages-importable/92527/18
5+
# - https://discourse.julialang.org/t/package-extensions-structs-inside-extensions/101849
6+
# - https://github.com/JuliaLang/Pkg.jl/pull/3552
7+
8+
9+
using WhereTheWaterFlows
10+
const WWF=WhereTheWaterFlows
11+
using PyPlot
12+
13+
export plotarea, plotarea_dem, plotit, plotdir, plotbnds, heatmap, plotlakedepth, plot_catchments
314

415
"""
516
plotit(x, y, dem)
@@ -22,7 +33,7 @@ function plotit(x, y, waterflows_output::Tuple, dem)
2233
end
2334

2435
function plotdir(x, y, dir)
25-
vecfield = dir2vec.(dir)
36+
vecfield = WWF.dir2vec.(dir)
2637
vecfieldx = [v[1] for v in vecfield]
2738
vecfieldy = [v[2] for v in vecfield]
2839
quiver(repeat(x,1, length(y)), repeat(y,length(x),1), vecfieldx, vecfieldy)
@@ -68,7 +79,7 @@ end
6879

6980
function plotlakedepth(x, y, dem)
7081
area, slen, dir, nout, nin, pits = waterflows(dem)
71-
demf = fill_dem(dem, pits, dir)
82+
demf = WWF.fill_dem(dem, pits, dir)
7283
heatmap(x, y, demf.-dem)
7384
end
7485

@@ -99,3 +110,5 @@ function plot_catchments(x, y, c, lim=10)
99110
end
100111
heatmap(x,y,c)
101112
end
113+
114+
end

src/WhereTheWaterFlows.jl

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module WhereTheWaterFlows
22

3-
using StaticArrays, Requires
3+
using StaticArrays
44

55
export waterflows
66

@@ -286,7 +286,7 @@ function _flowrouting_catchments!(area, len, c, dir, cellarea, feedback_fn, colo
286286
slen = max(slen, slen_+1)
287287
end
288288
end
289-
# feedback of areas with each other an onto themselves
289+
# feedback of areas with each other and onto themselves
290290
if feedback_fn!==nothing
291291
uparea = feedback_fn(uparea, ij, dir)
292292
end
@@ -552,12 +552,35 @@ function _flow_from_to!(P1, P2, dir, nin, nout, allow_reversion=false)
552552
end
553553

554554

555-
## Plotting
556-
function __init__()
557-
@require PyPlot="d330b81b-6aea-500a-939a-2ce795aea3ee" include("plotting.jl")
558-
end
559-
560555
## Post processing
561556
include("postproc.jl")
562557

558+
## Plotting via Plt
559+
struct Plt end
560+
561+
"""
562+
Plotting functions can be accessed via `plt` after loading PyPlot.
563+
564+
Example
565+
```
566+
WhereTheWaterFlows.plt.plotit(dem)
567+
```
568+
"""
569+
plt = Plt()
570+
function Base.getproperty(::Plt, name::Symbol)
571+
ext = Base.get_extension(@__MODULE__, :PyPlotExt)
572+
if isnothing(ext)
573+
error("Need to `using PyPlot` to make plotting available")
574+
end
575+
return getproperty(ext, name)
576+
end
577+
578+
function Base.propertynames(::Plt)
579+
ext = Base.get_extension(@__MODULE__, :PyPlotExt)
580+
if isnothing(ext)
581+
error("Need to `using PyPlot` to make plotting available")
582+
end
583+
return propertynames(ext)
584+
end
585+
563586
end # module

0 commit comments

Comments
 (0)