Skip to content

Commit

Permalink
Rename slice1d/2d to interp1d/2d; add slice1d
Browse files Browse the repository at this point in the history
  • Loading branch information
henry2004y committed Sep 10, 2024
1 parent c86e779 commit 1f85103
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/src/man/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ d = interp1d(bd, "rho", loc)
```julia
point1 = Float32[-10.0, -1.0]
point2 = Float32[10.0, 1.0]
w = slice1d(bd, "rho", point1, point2)
w = interp1d(bd, "rho", point1, point2)
```

Here is a full list of predefined derived quantities:
Expand Down
2 changes: 1 addition & 1 deletion ext/BatsrusMakieExt/BatsrusMakieExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module BatsrusMakieExt

using Batsrus, Printf
import Batsrus: findindex, hasunit, getunit, slice2d
import Batsrus: findindex, hasunit, getunit, interp2d
import Batsrus.UnitfulBatsrus
import Makie

Expand Down
2 changes: 1 addition & 1 deletion ext/BatsrusMakieExt/typerecipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
"Conversion for 2D plots."
function Makie.convert_arguments(P::Makie.GridBased, bd::BATLData, var::String;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1)
x, y, w = slice2d(bd, var, plotrange, plotinterval)
x, y, w = interp2d(bd, var, plotrange, plotinterval)

unitx = getunit(bd, bd.head.variables[1])
unity = getunit(bd, bd.head.variables[2])
Expand Down
2 changes: 1 addition & 1 deletion src/Batsrus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export BATLData,
load, readlogdata, readtecdata, showhead, # io
getvars, getvar, cutdata, subvolume, subsurface, # select
Batl, convertTECtoVTU, convertIDLtoVTK, readhead, readtree, getConnectivity, # vtk
interp1d, slice1d, slice2d, get_var_range # plot/utility
interp1d, interp2d, slice1d, get_var_range # plot/utility

"Type for the file information."
struct FileList
Expand Down
2 changes: 1 addition & 1 deletion src/plot/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1) where {T}
hasunits = hasunit(bd)

x, y, w = slice2d(bd, var, plotrange, plotinterval)
x, y, w = interp2d(bd, var, plotrange, plotinterval)
if hasunits
unitx = getunit(bd, bd.head.variables[1])
unity = getunit(bd, bd.head.variables[2])
Expand Down
10 changes: 5 additions & 5 deletions src/plot/pyplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ Wrapper over `contour` in matplotlib.
"""
function PyPlot.contour(bd::BATLData{2, T}, var::AbstractString, ax=nothing; levels=0,
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...) where T
Xi, Yi, Wi = slice2d(bd, var, plotrange, plotinterval; innermask)
Xi, Yi, Wi = interp2d(bd, var, plotrange, plotinterval; innermask)
if isnothing(ax) ax = plt.gca() end

if levels != 0
Expand All @@ -499,11 +499,11 @@ end
contourf(data, var, ax=nothing; levels=0, plotrange=[-Inf,Inf,-Inf,Inf],
plotinterval=0.1, innermask=false, kwargs...)
Wrapper over `contourf` in matplotlib. See [`slice2d`](@ref) for some related keywords.
Wrapper over `contourf` in matplotlib. See [`interp2d`](@ref) for some related keywords.
"""
function PyPlot.contourf(bd::BATLData{2, T}, var::AbstractString, ax=nothing; levels::Int=0,
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...) where T
Xi, Yi, Wi = slice2d(bd, var, plotrange, plotinterval; innermask)
Xi, Yi, Wi = interp2d(bd, var, plotrange, plotinterval; innermask)
if isnothing(ax) ax = plt.gca() end

if levels != 0
Expand Down Expand Up @@ -577,7 +577,7 @@ Wrapper over `plot_surface` in matplotlib.
"""
function PyPlot.plot_surface(bd::BATLData{2, T}, var::AbstractString;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...) where T
xi, yi, Wi = slice2d(bd, var, plotrange, plotinterval; innermask)
xi, yi, Wi = interp2d(bd, var, plotrange, plotinterval; innermask)
Xi, Yi = meshgrid(xi, yi)

plot_surface(Xi, Yi, Wi; kwargs...)
Expand All @@ -592,7 +592,7 @@ Wrapper over `pcolormesh` in matplotlib.
"""
function PyPlot.pcolormesh(bd::BATLData{2, T}, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...) where T
xi, yi, Wi = slice2d(bd, var, plotrange, plotinterval; innermask)
xi, yi, Wi = interp2d(bd, var, plotrange, plotinterval; innermask)

if isnothing(ax) ax = plt.gca() end

Expand Down
22 changes: 16 additions & 6 deletions src/plot/utility.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Utility functions for plotting.

"""
slice2d(bd::BATLData, var::AbstractString, plotrange=[-Inf, Inf, -Inf, Inf],
interp2d(bd::BATLData, var::AbstractString, plotrange=[-Inf, Inf, -Inf, Inf],
plotinterval=Inf; kwargs...)
Return 2D slices of data `var` from `bd`. If `plotrange` is not set, output data resolution
is the same as the original.
Return 2D interpolated slices of data `var` from `bd`. If `plotrange` is not set, output
data resolution is the same as the original.
# Keyword Arguments
- `innermask=false`: Whether to mask the inner boundary with NaN.
- `rbody=1.0`: Radius of the inner mask. Used when the rbody parameter is not found in the header.
- `useMatplotlib=true`: Whether to Matplotlib (somehow faster) or NaturalNeighbours for scattered interpolation.
"""
function slice2d(bd::BATLData{2, T}, var::AbstractString,
function interp2d(bd::BATLData{2, T}, var::AbstractString,
plotrange::Vector=[-Inf, Inf, -Inf, Inf], plotinterval::Real=Inf;
innermask::Bool=false, rbody::Real=1.0, useMatplotlib::Bool=true) where {T}
x, w = bd.x, bd.w
Expand Down Expand Up @@ -146,11 +146,11 @@ function interp1d(bd::BATLData{2, T}, var::AbstractString, loc::AbstractVector{<
end

"""
slice1d(bd::BATLData, var::AbstractString, point1::Vector, point2::Vecto)
interp1d(bd::BATLData, var::AbstractString, point1::Vector, point2::Vecto)
Interpolate `var` along a line from `point1` to `point2` in `bd`.
"""
function slice1d(bd::BATLData{2, T}, var::AbstractString, point1::Vector, point2::Vector) where {T}
function interp1d(bd::BATLData{2, T}, var::AbstractString, point1::Vector, point2::Vector) where {T}
@assert !bd.head.gencoord "Only accept structured grids!"

x = bd.x
Expand All @@ -168,6 +168,16 @@ function slice1d(bd::BATLData{2, T}, var::AbstractString, point1::Vector, point2
Wi = [itp(loc...) for loc in points]
end

"""
slice1d(bd, var, icut::Int=1, dir::Int=2)
Return view of variable `var` in `bd` along 1D slice. `icut` is the index along axis `dir`.
`dir == 1` means align with the 2nd (y) axis, `dir == 2` means align with the 1st (x) axis.
"""
function slice1d(bd, var, icut::Int=1, dir::Int=2)
selectdim(bd[var], dir, icut)
end

"Return view of variable `var` in `bd`."
function getview(bd::BATLData{1, T}, var) where T
varIndex_ = findindex(bd, var)
Expand Down
9 changes: 5 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end
@test extrema(bd.x) == (-127.5f0, 127.5f0)
@test extrema(bd.w) == (-0.79985905f0, 1.9399388f0)
plotrange = [-10.0, 10.0, -Inf, Inf]
x, y, w = slice2d(bd, "rho", plotrange)
x, y, w = interp2d(bd, "rho", plotrange)
@test w[1,end] == 0.6848635077476501
@test bd["B"][:,end,end] == Float32[1.118034, -0.559017, 0.0]
# Linear interpolation at a given point
Expand All @@ -59,17 +59,18 @@ end
# Linear interpolation along a line
point1 = Float32[-10.0, -1.0]
point2 = Float32[10.0, 1.0]
w = slice1d(bd, "rho", point1, point2)
w = interp1d(bd, "rho", point1, point2)
@test sum(w) == 10.676028f0

w = slice1d(bd, "rho", 1, 1)
@test sum(w) == 4.0f0
@test get_var_range(bd, "rho") == (0.11626893f0, 1.0f0)
end

@testset "Reading 2D unstructured" begin
file = "bx0_mhd_6_t00000100_n00000352.out"
bd = load(joinpath(datapath, file))
plotrange = [-Inf, Inf, -Inf, Inf]
x, y, w = slice2d(bd, "rho", plotrange, useMatplotlib=false)
x, y, w = interp2d(bd, "rho", plotrange, useMatplotlib=false)
@test w[1,2] == 5.000018304080387
end

Expand Down

0 comments on commit 1f85103

Please sign in to comment.