Skip to content

Commit 87559ed

Browse files
authored
Use the new wrapDatasets() to try limit the number of potential recompilations. (#1923)
1 parent 965b005 commit 87559ed

8 files changed

Lines changed: 68 additions & 25 deletions

File tree

src/GMT.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,7 @@ using .Laszip
374374
@compile_workload begin
375375
G_API[] = GMT_Create_Session("GMT", 2, GMT_SESSION_BITFLAGS)
376376
#GMT.parse_B(Dict{Symbol, Any}(:frame => (annot=10, title="Ai Ai"), :grid => (pen=2, x=10, y=20)), "", " -Baf -BWSen");
377-
#GMT.parse_R(Dict{Symbol, Any}(:xlim => (1,2), :ylim => (3,4), :zlim => (5,6)), "");
378-
#GMT.parse_J(Dict{Symbol, Any}(:J => "X", :scale => "1:10"), "");
379377
#GMT.parse_opt_S(Dict{String, Any}(), mat2ds(rand(4,2)));
380-
#GMT.build_opt_J(:X5);
381378
GMT.theme("dark")
382379
GMT.theme_modern()
383380
mat2ds([9 8; 9 8], x=[0 7], pen=["5p,black", "4p,white,20p_20p"], multi=true)
@@ -389,6 +386,7 @@ using .Laszip
389386
gmtwrite(t, [0.0 0; 1 1])
390387
gmtread(t)
391388
gmtread(TESTSDIR * "assets/burro_cenora.jpg")
389+
coast(R=:g, proj=:guess, W=(level=1, pen=(2, :green)), savefig=tempname()*".ps")
392390
rm(t)
393391
D = mat2ds(rand(3, 3), colnames=["Time", "b", "c"])
394392
D.attrib = Dict("Timecol" => "1")
@@ -413,7 +411,6 @@ using .Laszip
413411
grdsample(Glix, inc=0.5)
414412
grdtrend(Glix, model=3)
415413
grdtrack(Glix, [1 1])
416-
coast(R=:g, proj=:guess, W=(level=1, pen=(2, :green)), savefig=tempname()*".ps")
417414
rescale(mat2img(rand(UInt16, 16,16,3)))
418415
plot(rand(5, 2))
419416
bar(1:5, (20, 35, 30, 35, 27), width=0.5, color=:lightblue, limits=(0.5,5.5,0,40))
@@ -426,13 +423,13 @@ using .Laszip
426423
#ecdfplot!(randn(50), fmt=:ps);
427424
#cornerplot(randn(50,3), scatter=true, fmt=:ps);
428425
#marginalhist(randn(1000,2), par=(PS_MEDIA="A2",), fmt=:ps); rm("GMTplot.ps")
429-
#feather([0.0 0 2.0; 0.0 30 2; 0.0 60 2], rtheta=true, aspect="1:1", arrow=(len=0.5, shape=0.5,), fmt=:ps);
426+
feather([0.0 0 2.0; 0.0 30 2; 0.0 60 2], rtheta=true, aspect="1:1", arrow=(len=0.5, shape=0.5,), fmt=:ps);
430427
#orbits(mat2ds(rand(10,3)));
431428
#pca(rand(Float32, 24, 4));
432429
#pca(mat2img(rand(UInt8, 64,64,4)));
433430
#kmeans(rand(100,3), 3, maxiter=10);
434431
#rm(joinpath(tempdir(), "GMTjl_custom_p_x.txt")) # This one gets created before username is set.
435-
#arrows([0 8.2 0 6], limits=(-2,4,0,9), arrow=(len=2,stop=1,shape=0.5,fill=:red), axis=:a, pen="6p");
432+
arrows([0 8.2 0 6], limits=(-2,4,0,9), arrow=(len=2,stop=1,shape=0.5,fill=:red), axis=:a, pen="6p");
436433
GMT.doc_source_links("psbasemap"; silent=true)
437434
theme()
438435

src/gmt2kml.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ gmt2kml(arg1; kw...) = gmt2kml_helper("", arg1; kw...)
7373
# ---------------------------------------------------------------------------------------------------
7474
function gmt2kml_helper(cmd0::String, arg1; kw...)
7575
d = init_module(false, kw...)[1]
76-
gmt2kml_helper(cmd0, arg1, d)
76+
gmt2kml_helper(wrapDatasets(cmd0, arg1), d)
7777
end
78-
function gmt2kml_helper(cmd0::String, arg1, d::Dict{Symbol, Any})
78+
function gmt2kml_helper(w::wrapDatasets, d::Dict{Symbol, Any})
79+
cmd0, arg1 = unwrapDatasets(w::wrapDatasets)
7980

8081
cmd, = parse_common_opts(d, "", [:R :V_params :bi :di :e :f :h :i :yx])
8182
cmd = parse_these_opts(cmd, d, [[:A :altitude_mode], [:D :descript], [:E :extrude], [:F :feature_type], [:G :fill],

src/gmt_types.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,30 @@ function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{GMTfv}}, ::
336336
end
337337
find4similar(FV::GMTfv, rest) = FV
338338

339+
# ------------------------------------------------------------------------------------
340+
mutable struct wrapDatasets#{T<:Real, N} <: AbstractArray{T,N}
341+
fname::String
342+
ds::GMTdataset#{T,N}
343+
vds::Vector{GMTdataset}
344+
fv::GMTfv#{T}
345+
cpt::GMTcpt
346+
function wrapDatasets(arg1, arg2)
347+
td, tvd, tcpt, tfv = GMTdataset(), Vector{GMTdataset}(), GMTcpt(), GMTfv()
348+
if (arg1 !== "") new(arg1, td, tvd, tfv, tcpt)
349+
elseif (isa(arg2, Matrix{<:Real})) new("", mat2ds(arg2), tvd, tfv, tcpt)
350+
elseif (isa(arg2, GMTdataset)) new("", arg2, tvd, tfv, tcpt)
351+
elseif (isa(arg2, Vector{<:GMTdataset})) new("", td, arg2, tfv, tcpt)
352+
elseif (isa(arg2, GMTfv)) new("", td, tvd, arg2, tcpt)
353+
elseif (isa(arg2, GMTcpt)) new("", td, tvd, tfv, arg2)
354+
elseif (arg1 === "" && arg2 === nothing) new("", td, tvd, tfv, tcpt) # Less usual case in plot where inly kwargs are given
355+
else error("Unknown types ($(typeof(arg1)), $(typeof(arg2))) in wrapDatasets")
356+
end
357+
end
358+
end
359+
function unwrapDatasets(w::wrapDatasets)
360+
return w.fname, !isempty(w.ds) ? w.ds : !isempty(w.vds) ? w.vds : !isempty(w.fv) ? w.fv : !isempty(w.cpt) ? w.cpt : nothing
361+
end
362+
339363
#=
340364
Base.@kwdef struct GMTtypes
341365
stored::String = ""

src/gmtconvert.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ gmtconvert(arg1; kwargs...) = gmtconvert_helper("", arg1; kwargs...)
5757

5858
function gmtconvert_helper(cmd0::String, arg1; kwargs...)
5959
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
60-
gmtconvert_helper(cmd0, arg1, d)
60+
gmtconvert_helper(wrapDatasets(cmd0, arg1), d)
6161
end
6262
# ---------------------------------------------------------------------------------------------------
63-
function gmtconvert_helper(cmd0::String, arg1, d::Dict{Symbol,Any})
63+
function gmtconvert_helper(w::wrapDatasets, d::Dict{Symbol,Any})
64+
cmd0, arg1 = unwrapDatasets(w::wrapDatasets)
6465

6566
cmd, = parse_common_opts(d, "", [:V_params :append :a :b :bo :d :e :g :h :i :o :q :s :w :yx])
66-
cmd, opt_f = parse_f(d, cmd) # Must have easier acces to opt_f
67+
cmd, opt_f = parse_f(d, cmd) # Must have easier acces to opt_f
6768

6869
# GMT is not able to return a converted time table, so we create a temporary file if time conversions are involved
6970
used_tmp_file = false
@@ -80,7 +81,6 @@ function gmtconvert_helper(cmd0::String, arg1, d::Dict{Symbol,Any})
8081
cmd = parse_these_opts(cmd, d, [[:A :hcat], [:C :n_records], [:D :dump], [:E :first_last], [:F :conn_method],
8182
[:I :invert :reverse], [:L :list_only], [:N :sort], [:Q :segments], [:S :select_hdr], [:T :suppress :skip], [:W :word2num], [:Z :transpose]])
8283

83-
8484
out = common_grd(d, cmd0, cmd, "gmtconvert ", arg1) # Finish build cmd and run it
8585
if (used_tmp_file) # Read the conversion results and clean up temporary file.
8686
out = gmtread(fname)

src/gmtinfo.jl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ end
5252
# ---------------------------------------------------------------------------------------------------
5353
function gmtinfo_helper(cmd0::String, arg1, d::Dict{Symbol,Any})::Union{String, GMTdataset}
5454

55+
#=
5556
#cmd, = parse_common_opts(d, "", [:V_params :e :f :i :o :r :w :yx])
5657
cmd = parse_V_params(d, "")
5758
cmd = parse_e(d, cmd)[1]
@@ -67,10 +68,31 @@ function gmtinfo_helper(cmd0::String, arg1, d::Dict{Symbol,Any})::Union{String,
6768
cmd = add_opt(d, cmd, "I", [:I :inc :increment :spacing],
6869
(exact=("e", nothing, 1), polyg=("b", nothing, 1), surface=("s", nothing, 1), fft=("d", nothing, 1), inc=("", arg2str, 2)); del=false, expand=true)
6970
cmd = add_opt(d, cmd, "T", [:T :nearest_multiple], (dz="", col="+c", column="+c"))
71+
=#
7072

71-
# If file name sent in, read it.
72-
if (cmd0 != "") cmd, arg1, = read_data(d, cmd0, cmd, arg1, " ") end
73+
cmd = gmtinfo_helper_helper(d) # Parse only options that do not depend on the (fcking Any) 'arg1'
74+
75+
if (cmd0 != "") cmd = cmd * " " * cmd0
76+
elseif (eltype(arg1) == String) cmd = cmd * " " * join(arg1, ' '); arg1 = nothing # Accept also tuples/vecs of file names
77+
end
7378
cmd = "gmtinfo " * cmd
7479
if (dbg_print_cmd(d, cmd) !== nothing) return cmd end
7580
isa(arg1, Tuple) ? gmt(cmd, arg1...) : gmt(cmd, arg1)
7681
end
82+
83+
function gmtinfo_helper_helper(d::Dict{Symbol,Any})::String
84+
cmd = parse_V_params(d, "")
85+
cmd = parse_e(d, cmd)[1]
86+
cmd = parse_f(d, cmd)[1]
87+
cmd = parse_i(d, cmd)[1]
88+
cmd = parse_o(d, cmd)[1]
89+
cmd = parse_r(d, cmd)[1]
90+
cmd = parse_w(d, cmd)[1]
91+
cmd = parse_swap_xy(d, cmd)[1]
92+
(endswith(cmd, "-:")) && (cmd *= "i") # Need to be -:i not -: to not swap output too
93+
cmd = parse_these_opts(cmd, d, [[:A :ranges], [:C :numeric :per_column], [:D :center], [:E :get_record], [:F :counts],
94+
[:L :common_limits], [:S :for_error_bars]])
95+
cmd = add_opt(d, cmd, "I", [:I :inc :increment :spacing],
96+
(exact=("e", nothing, 1), polyg=("b", nothing, 1), surface=("s", nothing, 1), fft=("d", nothing, 1), inc=("", arg2str, 2)); del=false, expand=true)
97+
add_opt(d, cmd, "T", [:T :nearest_multiple], (dz="", col="+c", column="+c"))
98+
end

src/psscale.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ To see the full documentation type: ``@? colorbar``
4949
"""
5050
function colorbar(arg1::Union{Nothing, GMTcpt}=nothing; first=true, kwargs...)
5151
d, K, O = init_module(first==1, kwargs...) # Also checks if the user wants ONLY the HELP mode
52-
dbg_cmd, d, cmd, arg1 = colorbar_parser(arg1, O, d)
52+
dbg_cmd, d, cmd, arg1 = colorbar_parser(isa(arg1, Nothing) ? GMTcpt() : arg1, O, d)
5353
(dbg_cmd !== nothing) && return dbg_cmd
5454
r = prep_and_call_finish_PS_module(d, cmd, "", K, O, true, arg1)
5555
(!isa(r,String)) && gmt("destroy") # Probably because of the rasters in cpt
@@ -73,7 +73,7 @@ function colorbar_parser(arg1::Union{Nothing, GMTcpt}, O::Bool, d::Dict{Symbol,
7373

7474
(!isempty(opt_D)) && (!contains(opt_D, "DJ") && !contains(opt_D, "Dj") && !contains(opt_D, "Dg")) && (cmd = replace(cmd, "-R " => ""))
7575
cmd *= opt_D
76-
cmd, arg1, = add_opt_cpt(d, cmd, CPTaliases, 'C', 0, arg1)
76+
cmd, arg1, = add_opt_cpt(d, cmd, CPTaliases, 'C', 0, isempty(arg1) ? nothing : arg1)
7777
if (opt_D === "" && ((val = hlp_desnany_str(d, [:triangles])) !== "")) # User asked for triangles but did not set pos
7878
(val == "true") && (val = "tri") # Means just triangles
7979
anc_tris = colorbar_triangles(val) # Returns anchor+triangles string

src/psxy.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ function common_plot_xyz(cmd0::String, arg1, caller::String, first::Bool, is3D::
1212
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
1313
(cmd0 != "" && arg1 === nothing && is_in_dict(d, [:groupvar :hue]) !== nothing) && (arg1 = gmtread(cmd0); cmd0 = "")
1414
_common_plot_xyz(cmd0, arg1, caller, O, K, is3D, d)
15-
#invokelatest(_common_plot_xyz, cmd0, arg1, caller, O, K, is3D, d)
1615
end
1716
function common_plot_xyz(cmd0::String, arg1, caller::String, first::Bool, is3D::Bool, d::Dict{Symbol, Any})
1817
(cmd0 != "" && arg1 === nothing && is_in_dict(d, [:groupvar :hue]) !== nothing) && (arg1 = gmtread(cmd0); cmd0 = "")
1918
_common_plot_xyz(cmd0, arg1, caller, !first, true, is3D, d)
20-
#invokelatest(_common_plot_xyz, cmd0, arg1, caller, !first, true, is3D, d)
2119
end
2220
function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool, is3D::Bool, d::Dict{Symbol, Any})
2321
first = !O
@@ -411,8 +409,8 @@ function parse_plot_callers(d::Dict{Symbol, Any}, gmt_proggy::String, caller::St
411409
end
412410

413411
# ---------------------------------------------------------------------------------------------------
414-
plt_txt_attrib!(D::GMTdataset{T,N}, d::Dict{Symbol, Any}, _cmd::Vector{String}) where {T,N} = plt_txt_attrib!([D], d, _cmd)
415-
function plt_txt_attrib!(D::Vector{<:GMTdataset{T,N}}, d::Dict{Symbol, Any}, _cmd::Vector{String}) where {T,N}
412+
plt_txt_attrib!(D::GMTdataset, d::Dict{Symbol, Any}, _cmd::Vector{String}) = plt_txt_attrib!([D], d, _cmd)
413+
function plt_txt_attrib!(D::Vector{<:GMTdataset}, d::Dict{Symbol, Any}, _cmd::Vector{String})
416414
# Plot TEXT attributed labels and serve as function barrier agains the f Any's (not sure if succeeds)
417415
((s_val = hlp_desnany_str(d, [:labels])) === "") && return nothing
418416

@@ -561,7 +559,7 @@ function check_grouping!(d, arg1)
561559
end
562560

563561
# ---------------------------------------------------------------------------------------------------
564-
function check_ribbon(d::Dict{Symbol, Any}, arg1::GMTdataset{T,N}, cmd::String, opt_W::String) where {T,N}
562+
function check_ribbon(d::Dict{Symbol, Any}, arg1::GMTdataset, cmd::String, opt_W::String)
565563
((val = find_in_dict(d, [:ribbon :band])[1]) === nothing) && return arg1, cmd
566564
ec1, ec2, add_2 = helper_check_ribbon(val) # Function barrier agains Anys
567565
(add_2) ? add2ds!(arg1, ec2; names=["Zbnd1","Zbnd2"]) : add2ds!(arg1, ec1; name="Zbnd")
@@ -570,7 +568,7 @@ function check_ribbon(d::Dict{Symbol, Any}, arg1::GMTdataset{T,N}, cmd::String,
570568
return arg1, cmd
571569
end
572570

573-
function check_ribbon(d::Dict{Symbol, Any}, arg1::Vector{<:GMTdataset{T,N}}, cmd::String, opt_W::String) where {T,N}
571+
function check_ribbon(d::Dict{Symbol, Any}, arg1::Vector{<:GMTdataset}, cmd::String, opt_W::String)
574572
((val = find_in_dict(d, [:ribbon :band])[1]) === nothing) && return arg1, cmd
575573
ec1, ec2, add_2 = helper_check_ribbon(val) # Function barrier agains Anys
576574
if (add_2)

src/sample1d.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ function sample1d_helper(cmd0::String, arg1; kwargs...)
6060
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
6161
isa(arg1, Matrix{<:Real}) && (arg1 = mat2ds(Float64.(arg1))) # One less type to deal with
6262
fill_nans = is_in_kwargs(d, [:fill_nans :interp_nans])
63-
(cmd0 != "") && fill_nans && (arg1 = gmtread(cmd0))
63+
(cmd0 != "") && fill_nans && (arg1 = gmtread(cmd0); cmd0 = "")
6464
fill_nans && isa(arg1, Vector) && (@warn "Filling NaNs only works with GMTdatasets. For vectors you must run one by one"; return nothing)
65-
sample1d_helper(cmd0, arg1, d)
65+
sample1d_helper(wrapDatasets(cmd0, arg1), d)
6666
end
6767

6868
# ---------------------------------------------------------------------------------------------------
69-
function sample1d_helper(cmd0::String, arg1, d::Dict{Symbol,Any})
69+
function sample1d_helper(w::wrapDatasets, d::Dict{Symbol,Any})
70+
cmd0, arg1 = unwrapDatasets(w::wrapDatasets)
7071

7172
cmd = parse_common_opts(d, "", [:V_params :b :d :e :f :h :i :o :s :w :yx])[1]
7273
_, opt_g = parse_g(d, "")

0 commit comments

Comments
 (0)