Skip to content

Commit

Permalink
Create front end tiny functions to deal with the kwargs and create Di…
Browse files Browse the repository at this point in the history
…cts out of them. (#1609)

This highly reduces the amount of code that has to be compiled in every invalidation involving these functions.
  • Loading branch information
joa-quim authored Dec 8, 2024
1 parent cf9b272 commit d5a9b2a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/GMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ using GMT.Laszip
grdimage(rand(Float32,32,32), R="0/32/0/32");
I = mat2img(rand(UInt8, 32, 32, 3), clim=:zscale);
grdimage(I, V=:q);
#grdview(rand(Float32,32,32), Vd=2);
grdview(rand(Float32,32,32), Vd=2);
#grdinfo(mat2grid(rand(Float32,4,4)));
#Glix=gmt("grdmath", "-R0/10/0/10 -I2 X");
#grdcontour(Glix);
Expand Down
10 changes: 7 additions & 3 deletions src/grdimage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ Parameters
To see the full documentation type: ``@? grdimage``
"""
function grdimage(cmd0::String="", arg1=nothing, arg2=nothing, arg3=nothing; first=true, kwargs...)
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
_grdimage(cmd0, arg1, arg2, arg3, O, K, d)
end
function _grdimage(cmd0::String, arg1, arg2, arg3, O::Bool, K::Bool, d::Dict)

arg4 = nothing # For the r,g,b + intensity case
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
first = !O
(cmd0 != "" && arg1 === nothing && haskey(d, :inset)) && (arg1 = gmtread(cmd0); cmd0 = "")
common_insert_R!(d, O, cmd0, arg1) # Set -R in 'd' out of grid/images (with coords) if limits was not used

Expand Down Expand Up @@ -106,8 +110,8 @@ function grdimage(cmd0::String="", arg1=nothing, arg2=nothing, arg3=nothing; fir
(isa(arg3, Matrix{<:Real})) && (arg3 = mat2grid(arg3))
end
elseif (isa(arg1, GMTimage) && size(arg1, 3) <= 3 && eltype(arg1.image) <: UInt16)
arg1 = mat2img(arg1; kwargs...)
(haskey(kwargs, :stretch) || haskey(kwargs, :histo_bounds)) && delete!(d, [:histo_bounds, :stretch])
arg1 = mat2img(arg1; d...)
(haskey(d, :stretch) || haskey(d, :histo_bounds)) && delete!(d, [:histo_bounds, :stretch])
end

set_defcpt!(d, cmd0, arg1) # When dealing with a remote grid assign it a default CPT
Expand Down
6 changes: 5 additions & 1 deletion src/grdview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ grdview!(arg1; kwargs...) = grdview_helper("", arg1; first=false, kwargs

# ---------------------------------------------------------------------------------------------------
function grdview_helper(cmd0::String, arg1; first=true, kwargs...)
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
grdview_helper(cmd0, arg1, O, K, d)
end
function grdview_helper(cmd0::String, arg1, O::Bool, K::Bool, d::Dict)

arg2 = nothing; arg3 = nothing; arg4 = nothing; arg5 = nothing;
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
first = !O

haskey(d, :outline) && delete!(d, :outline) # May come through `pcolor` where it was valid, but not here.
have_opt_JZ = (is_in_dict(d, [:JZ :Jz :zsize :zscale]) !== nothing)
Expand Down
12 changes: 8 additions & 4 deletions src/pscoast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ Parameters
To see the full documentation type: ``@? coast``
"""
function coast(cmd0::String=""; clip=nothing, first=true, kwargs...)
function coast(cmd0::String=""; clip::StrSymb="", first=true, kwargs...)
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
_coast(cmd0, O, K, clip, d)
end
function _coast(cmd0::String, O::Bool, K::Bool, clip::StrSymb, d::Dict)

gmt_proggy = (IamModern[1]) ? "coast " : "pscoast "
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
first = !O

if ((val = find_in_dict(d, [:getR :getregion :get_region], false)[1]) !== nothing)
t::String = string(gmt_proggy, " -E", val)
Expand Down Expand Up @@ -128,7 +132,7 @@ function coast(cmd0::String=""; clip=nothing, first=true, kwargs...)
cmd = add_opt_fill(cmd, d, [:G :land], 'G')
cmd = add_opt_fill(cmd, d, [:S :water :ocean], 'S')

if (clip !== nothing)
if (clip !== "")
_clip::String = string(clip)
if (_clip == "land") cmd *= " -Gc"
elseif (_clip == "water" || _clip == "ocean") cmd *= " -Sc"
Expand Down Expand Up @@ -274,7 +278,7 @@ function parse_dcw(val::Tuple)::String
end

# ---------------------------------------------------------------------------------------------------
coast!(cmd0::String=""; clip=nothing, kw...) = coast(cmd0; clip=clip, first=false, kw...)
coast!(cmd0::String=""; clip::StrSymb="", kw...) = coast(cmd0; clip=clip, first=false, kw...)

const pscoast = coast # Alias
const pscoast! = coast!
Expand Down
8 changes: 6 additions & 2 deletions src/pstext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ Parameters
To see the full documentation type: ``@? pstext``
"""
function text(cmd0::String="", arg1=nothing; first=true, kwargs...)
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
_text(cmd0, arg1, O, K, d)
end
function _text(cmd0::String, arg1, O::Bool, K::Bool, d::Dict)

(find_in_kwargs(kwargs, [:L :list])[1] !== nothing) && return gmt("pstext -L")
(is_in_dict(d, [:L :list]) !== nothing) && return gmt("pstext -L")

gmt_proggy = (IamModern[1]) ? "text " : "pstext "

N_args = (arg1 === nothing) ? 0 : 1
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
first = !O

function parse_xy(d, arg)
# Deal with cases (txt="Bla", x=0.5, y=0.5) or (data="Bla", x=0.5, y=0.5)
Expand Down
16 changes: 9 additions & 7 deletions src/utils_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,11 @@ the georeference info as well as `attrib` and `colnames`.
mat2ds(mat::Array{T,N}, ref::GMTdataset) where {T,N} = mat2ds(mat; ref=ref)

# ---------------------------------------------------------------------------------------------------
function mat2ds(mat::Array{T,N}, txt::Union{String,Vector{String}}=String[]; hdr=String[], geom=0, kwargs...)::GDtype where {T,N}
function mat2ds(mat::Array{T,N}, txt::Union{String,Vector{String}}=String[]; hdr::Union{String,VecOrMat{String}}=String[], geom=0, kwargs...)::GDtype where {T,N}
d = KW(kwargs)
_mat2ds(mat, txt, isa(hdr, String) ? [hdr] : vec(hdr), Int(geom), d)
end
function _mat2ds(mat::Array{T,N}, txt::Union{String,Vector{String}}, hdr::Vector{String}, geom::Int, d::Dict)::GDtype where {T,N}

(!isempty(txt)) && return text_record(mat, txt, hdr)
((text = find_in_dict(d, [:text])[1]) !== nothing) && return text_record(mat, text, hdr)
Expand All @@ -218,14 +221,12 @@ function mat2ds(mat::Array{T,N}, txt::Union{String,Vector{String}}=String[]; hdr
xx = Vector{Float64}()
end

if (!isempty(hdr) && isa(hdr, String)) # Accept one only but expand to n_ds with the remaining as blanks
_hdr::Vector{String} = Base.fill("", n_ds); _hdr[1] = hdr
if (!isempty(hdr) && length(hdr) == 1) # Accept one only but expand to n_ds with the remaining as blanks
_hdr::Vector{String} = Base.fill("", n_ds); _hdr[1] = hdr[1]
elseif (!isempty(hdr) && length(hdr) != n_ds)
error("The header vector can only have length = 1 or same number of MAT Y columns")
elseif (!isempty(hdr))
_hdr = vec(hdr)
else
_hdr = String[]
_hdr = hdr
end

color_cycle = false
Expand Down Expand Up @@ -407,7 +408,8 @@ function mat2ds(mat::Array{T,N}, txt::Union{String,Vector{String}}=String[]; hdr
(length(ref_coln) >= size(D[1].data,2)) && (D[1].colnames = ref_coln[1:size(D[1].data,2)]) # This still loses Text colname
CTRL.pocket_d[1] = d # Store d that may be not empty with members to use in other functions
set_dsBB!(D) # Compute and set the global BoundingBox for this dataset
return (find_in_kwargs(kwargs, [:letsingleton])[1] !== nothing) ? D : (length(D) == 1 && !multi) ? D[1] : D
#return (find_in_kwargs(kwargs, [:letsingleton])[1] !== nothing) ? D : (length(D) == 1 && !multi) ? D[1] : D
return (find_in_dict(d, [:letsingleton])[1] !== nothing) ? D : (length(D) == 1 && !multi) ? D[1] : D
end

# ---------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit d5a9b2a

Please sign in to comment.