Skip to content

Commit

Permalink
feat(plot): add 1D and 2D field plot recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Jan 5, 2025
1 parent 2870168 commit 54cee4c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
57 changes: 54 additions & 3 deletions ext/UnderwaterAcousticsPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using RecipesBase
using Colors
using UnderwaterAcoustics
import UnderwaterAcoustics: AbstractAcousticSource, AbstractAcousticReceiver, RayArrival, value
import UnderwaterAcoustics: SampledFieldZ, SampledFieldX, SampledFieldXZ, SampledFieldXY
import DSP: amp2db

@recipe function plot(env::UnderwaterEnvironment)
Expand All @@ -16,12 +17,12 @@ import DSP: amp2db
xguide --> "x (m)"
yguide --> "z (m)"
@series begin
seriestype := :line
seriestype := :path
linecolor := :royalblue
xrange, [value(z1, (x, 0.0)) for x xrange]
end
@series begin
seriestype := :line
seriestype := :path
linecolor := :brown
xrange, [-value(z2, (x, 0.0)) for x xrange]
end
Expand Down Expand Up @@ -107,11 +108,61 @@ end
for (j, r) enumerate(rays)
r = r.path
@series begin
seriestype := :line
seriestype := :path
linecolor := colors[cndx[j]]
[r[i][1] for i 1:length(r)], [r[i][3] for i 1:length(r)]
end
end
end

@recipe function plot(fld::SampledFieldZ; npts=1000)
zr = range(minimum(fld.zrange), maximum(fld.zrange); length=npts)
ticks --> :native
legend --> false
yguide --> "z (m)"
@series begin
seriestype := :path
[fld(z) for z zr], zr
end
end

@recipe function plot(fld::SampledFieldX; npts=1000)
xr = range(minimum(fld.xrange), maximum(fld.xrange); length=npts)
ticks --> :native
legend --> false
xguide --> "x (m)"
@series begin
seriestype := :path
xr, [fld(x) for x xr]
end
end

@recipe function plot(fld::SampledFieldXY; npts=1000)
xr = range(minimum(fld.xrange), maximum(fld.xrange); length=npts)
yr = range(minimum(fld.yrange), maximum(fld.yrange); length=npts)
ticks --> :native
legend --> false
xguide --> "x (m)"
yguide --> "y (m)"
colorbar --> true
@series begin
seriestype := :heatmap
xr, yr, [fld(x, y) for x xr, y yr]
end
end

@recipe function plot(fld::SampledFieldXZ; npts=1000)
xr = range(minimum(fld.xrange), maximum(fld.xrange); length=npts)
zr = range(minimum(fld.zrange), maximum(fld.zrange); length=npts)
ticks --> :native
legend --> false
xguide --> "x (m)"
yguide --> "z (m)"
colorbar --> true
@series begin
seriestype := :heatmap
xr, zr, [fld(x, z) for x xr, z zr]
end
end

end # module
6 changes: 4 additions & 2 deletions src/pm_stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,12 @@ sampled on a regular grid.
function SampledField(v; x=nothing, y=nothing, z=nothing, interp=:linear)
interp === :linear || error("Only linear interpolation supported")
if x === nothing && y === nothing && z !== nothing
f = linear_interpolation(z, v; extrapolation_bc=Flat())
ndx = sortperm(z)
f = linear_interpolation(z[ndx], v[ndx]; extrapolation_bc=Flat())
SampledFieldZ(f, z)
elseif x !== nothing && y === nothing && z === nothing
f = linear_interpolation(x, v; extrapolation_bc=Flat())
ndx = sortperm(x)
f = linear_interpolation(x[ndx], v[ndx]; extrapolation_bc=Flat())
SampledFieldX(f, x)
elseif x !== nothing && y === nothing && z !== nothing
f = extrapolate(interpolate((x, z), v, Gridded(Linear())), Flat())
Expand Down

0 comments on commit 54cee4c

Please sign in to comment.