Skip to content

Commit

Permalink
Add grdcontour to the list of callable nested modules. (#1659)
Browse files Browse the repository at this point in the history
* Add grdcontour to the list of callable nested modules.

* Annotate all methods of arg2str, but Cthulhu stil f insists it may return Anys.

* Fix the limits determination when input is a grid.

* Accept the options 'contour=true' and 'cmap=:auto'

* Fix a grdimage test.
  • Loading branch information
joa-quim authored Feb 13, 2025
1 parent 53d9f2b commit 2e175a6
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/GMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const global DEF_FIG_AXES = [DEF_FIG_AXES_BAK] # This one may be be changed
const global DEF_FIG_AXES3 = [DEF_FIG_AXES3_BAK] # ""
const global FIG_MARGIN = [1] # Figure margin in points after convertion by 'psconvert'. Accessible 'margin' common option
const global CTRL = CTRLstruct(zeros(13), zeros(6), [true], [false],
[:arrows, :bubblechart, :basemap, :band, :clip, :coast, :colorbar, :hband, :hlines, :inset, :logo, :lines, :grdvector, :plot, :plot3, :quiver, :scatter, :scatter3, :stairs, :text, :vlines, :vband], fill(nothing, 6), ["","",""], ["","", "", " "], ["",""], ["",""], [false,true], [C_NULL], [Dict()])
[:arrows, :bubblechart, :basemap, :band, :clip, :coast, :colorbar, :grdcontour, :hband, :hlines, :inset, :logo, :lines, :grdvector, :plot, :plot3, :quiver, :scatter, :scatter3, :stairs, :text, :vlines, :vband], fill(nothing, 6), ["","",""], ["","", "", " "], ["",""], ["",""], [false,true], [C_NULL], [Dict()])
const global CTRLshapes = CTRLstruct2([true], [true], [""]) # Used in sub-module Drawing
const prj4WGS84 = "+proj=longlat +datum=WGS84 +units=m +no_defs" # This is used in many places
const CPTaliases = [:C :color :cmap :colormap :colorscale]
Expand Down
14 changes: 9 additions & 5 deletions src/common_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,7 @@ function line_decorated_with_string(str::AbstractString; dist=0)::String
end

# ---------------------------------------------------------------------------------------------------
function arg2str(d::Dict, symbs)
function arg2str(d::Dict, symbs)::String
# Version that allow calls from add_opt()
arg2str(find_in_dict(d, symbs)[1])
end
Expand Down Expand Up @@ -2734,7 +2734,8 @@ function get_cpt_set_R(d::Dict, cmd0::String, cmd::String, opt_R::String, got_fn
end

if (isa(arg1, GItype) || (cmd0 != "" && cmd0[1] != '@'))
if (isempty(CURRENT_CPT[1]) && (val = find_in_dict(d, CPTaliases, false)[1]) === nothing)
val, symb = find_in_dict(d, CPTaliases, false)
if (isempty(CURRENT_CPT[1]) && val === nothing)
# If no cpt name sent in, then compute (later) a default cpt
if (isa(arg1, GMTgrid) && ((val = find_in_dict(d, [:percent :pct])[1])) !== nothing)
lh = quantile(any(!isfinite, arg1) ? skipnan(vec(arg1)) : vec(arg1), [(100 - val)/200, (1 - (100 - val)/200)])
Expand All @@ -2745,12 +2746,15 @@ function get_cpt_set_R(d::Dict, cmd0::String, cmd::String, opt_R::String, got_fn
elseif (cpt_opt_T == "")
drange = range[6] - range[5]
(drange > 1e6) && @warn("The z range expands to more then 6 orders of magnitude. Missed to replace the nodatavalues?\n\n")
loc_eps = (drange > 1e-8) ? 1e-8 : 1e-15 # Totally ad hoc condition
cpt_opt_T = @sprintf(" -T%.12g/%.12g/256+n", range[5] - loc_eps, range[6] + loc_eps)
loc_eps = (drange > 1e-6) ? 1e-6 : 1e-12 # Totally ad hoc condition
cpt_opt_T = @sprintf(" -T%.12g/%.12g/256+n", range[5] - loc_eps*abs(range[5]), range[6] + loc_eps*abs(range[6]))
end
(range[5] > 1e100) && (cpt_opt_T = "") # cmd0 is an image name and now grdinfo does not compute its min/max
elseif (val == :auto || val == "auto") # Force computing the cpt from input grid. Usefull for multi-grdimages in one figure
cpt_opt_T = @sprintf(" -T%.12g/%.12g/256+n", range[5] - 1e-6*abs(range[5]), range[6] + 1e-6*abs(range[6]))
delete!(d, symb)
end
if (opt_R == "" && (!IamModern[1] || (IamModern[1] && FirstModern[1])) ) # No -R ovewrite by accident
if (opt_R == "" && (!IamModern[1] || (IamModern[1] && FirstModern[1]))) # No -R ovewrite by accident
cmd *= @sprintf(" -R%.14g/%.14g/%.14g/%.14g", range[1], range[2], range[3], range[4])
end
elseif (cmd0 != "" && cmd0[1] == '@') # No reason not to let @grids use clim=[...]
Expand Down
4 changes: 4 additions & 0 deletions src/finish_PS_nested.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ function add_opt_module(d::Dict)::Vector{String}
(CTRL.pocket_call[1] === nothing) ? (CTRL.pocket_call[1] = val) : (CTRL.pocket_call[2] = val)
r = "clip"
end
elseif (symb == :grdcontour)
(CTRL.pocket_call[1] === nothing) ? (CTRL.pocket_call[1] = val) : (CTRL.pocket_call[2] = val)
r = "grdcontour -J -R"
end
delete!(d, symb)

Expand All @@ -109,6 +112,7 @@ function add_opt_module_barr1(nt, symb::Symbol)::Union{String, Vector{String}}
r::Union{String, Vector{String}} = ""
if (symb == :coast) r = coast!(; Vd=2, nt...)
elseif (symb == :basemap) r = basemap!(; Vd=2, nt...)
elseif (symb == :grdcontour) r = grdcontour!(; Vd=2, nt...) # Not working
elseif (symb == :logo) r = logo!(; Vd=2, nt...)
elseif (symb == :colorbar)
r = colorbar!(; Vd=2, nt...)
Expand Down
1 change: 1 addition & 0 deletions src/grdimage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function _grdimage(cmd0::String, arg1, arg2, arg3, O::Bool, K::Bool, d::Dict)
_cmd = frame_opaque(_cmd, opt_B, opt_R, opt_J; bot=false) # No -t in frame
if (!occursin("-A", cmd)) # -A means that we are requesting the image directly
(haskey(d, :inset)) && (CTRL.pocket_call[4] = arg1) # If 'inset', it may be needed from next call
(isa(arg1, GMTgrid) && find_in_dict(d, [:contour :contours])[1] !== nothing) && (d[:grdcontour] = arg1) # Still very rudimentar
_cmd = finish_PS_nested(d, _cmd)
if ((ind = findfirst(startswith.(_cmd, "inset_"))) !== nothing) # inset commands must be the last ones
ins = popat!(_cmd, ind) # Remove the 'inset' command
Expand Down
8 changes: 5 additions & 3 deletions src/makecpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ function makecpt(G::GMTgrid; equalize=false, kw...) # A version that works on g
end
if (equalize == 0)
t = isempty(d) ? kw : d
makecpt(; T=@sprintf("%.12g/%.12g/256+n", G.range[5]*(1-0.001), G.range[6]*(1+0.001)), C=cpt, t...)
range::Vector{Float64} = G.range
loc_eps = 0.0004
makecpt(; T=@sprintf("%.12g/%.12g/256+n", range[5] - loc_eps*abs(range[5]), range[6] + loc_eps*abs(range[6])), C=cpt, t...)
else
(equalize == 1) ? grd2cpt(G, C=cpt, kw...) : grd2cpt(G, T="$equalize", C=cpt, kw...)
end
Expand Down Expand Up @@ -136,8 +138,8 @@ function parse_opt_range(d::Dict, cmd::String, opt::String="")::Tuple{String, Ve
Tvec::Vector{Float64} = Float64[]
if ((val = find_in_dict(d, symbs)[1]) !== nothing)
if (isa(val, Tuple))
n = length(val)
out = isa(val[1], Symbol) ? arg2str(val[1:min(n,3)], ',') : arg2str(val[1:min(n,3)])
n::Int = length(val)
out::String = isa(val[1], Symbol) ? arg2str(val[1:min(n,3)], ',') : arg2str(val[1:min(n,3)])
if (n > 3)
for k = 4:n # N should be at most = 5 (e.g. +n+b)
_opt::String = string(val[k])
Expand Down
1 change: 0 additions & 1 deletion src/potential/gravfft.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ Parameters
gravfft(arg1, arg2=nothing; kw...) = gravfft("", arg1, arg2; kw...)
function gravfft(cmd0::String="", arg1=nothing, arg2=nothing; kwargs...)

(GMTver < v"6.4.0") && (@warn("Sorry but you need at least GMT 6.4 to use this module. Previous version had a bug that prevented Julia wrapper to work."); return nothing)
arg3 = nothing
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode

Expand Down
1 change: 1 addition & 0 deletions test/test_views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ grdimage(rand(Float32, 128, 128)*255, rand(Float32, 128, 128)*255, rand(Float32,
grdimage(data=(Gr,Gg,Gb), J=:X10, I=mat2grid(rand(Float32,128,128)), Vd=dbg2)
grdimage(rand(Float32, 128, 128), shade=(default=30,), coast=(W=1,), Vd=dbg2)
grdimage(rand(Float32, 128, 128), colorbar=(color=:rainbow, pos=(anchor=:RM,length=8)), Vd=dbg2)
GMT.CURRENT_CPT[1] = GMTcpt()
grdimage(rand(Float32, 128, 128), percent=90, Vd=dbg2)
grdimage(rand(Float32, 128, 128), clim=[0.1, 0.9], Vd=dbg2)
grdimage("@earth_relief_01d_g", percent=98, Vd=dbg2)
Expand Down

0 comments on commit 2e175a6

Please sign in to comment.