Skip to content

Commit 216b769

Browse files
authored
Add wrap function to help prevent multiple compilation to fun operating on grids (#1927)
1 parent 8565ad5 commit 216b769

13 files changed

Lines changed: 55 additions & 28 deletions

src/contourf.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ function contourf(cmd0::String, arg1, arg2, first::Bool, d::Dict{Symbol,Any})
174174
if (opt_W !== nothing) d[:W] = opt_W end
175175
d[:W] = "c"; done = true
176176
end
177-
grdview_helper(cmd0, arg1; first=first, d...)
177+
#grdview_helper(cmd0, arg1; first=first, d...)
178+
grdview_helper(wrapGrids(cmd0, arg1), !first, true, d)
178179
delete!(d, [[:C, :z], [:Q], [:W]])
179180
if (done) return end
180181

src/gmt_types.jl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,11 @@ end
337337
find4similar(FV::GMTfv, rest) = FV
338338

339339
# ------------------------------------------------------------------------------------
340-
mutable struct wrapDatasets#{T<:Real, N} <: AbstractArray{T,N}
340+
mutable struct wrapDatasets
341341
fname::String
342-
ds::GMTdataset#{T,N}
342+
ds::GMTdataset
343343
vds::Vector{GMTdataset}
344-
fv::GMTfv#{T}
344+
fv::GMTfv
345345
cpt::GMTcpt
346346
function wrapDatasets(arg1, arg2)
347347
td, tvd, tcpt, tfv = GMTdataset(), Vector{GMTdataset}(), GMTcpt(), GMTfv()
@@ -351,7 +351,7 @@ mutable struct wrapDatasets#{T<:Real, N} <: AbstractArray{T,N}
351351
elseif (isa(arg2, Vector{<:GMTdataset})) new("", td, arg2, tfv, tcpt)
352352
elseif (isa(arg2, GMTfv)) new("", td, tvd, arg2, tcpt)
353353
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
354+
elseif (arg1 === "" && arg2 === nothing) new("", td, tvd, tfv, tcpt) # Less usual case in plot where only kwargs are given
355355
else error("Unknown types ($(typeof(arg1)), $(typeof(arg2))) in wrapDatasets")
356356
end
357357
end
@@ -360,6 +360,23 @@ function unwrapDatasets(w::wrapDatasets)
360360
return w.fname, !isempty(w.ds) ? w.ds : !isempty(w.vds) ? w.vds : !isempty(w.fv) ? w.fv : !isempty(w.cpt) ? w.cpt : nothing
361361
end
362362

363+
# ------------------------------------------------------------------------------------
364+
mutable struct wrapGrids
365+
fname::String
366+
grd::GMTgrid
367+
function wrapGrids(arg1, arg2)
368+
if (arg1 !== "") new(arg1, GMTgrid())
369+
elseif (isa(arg2, GMTgrid)) new("", arg2)
370+
elseif (isa(arg2, Matrix{<:Real})) new("", mat2grid(arg2))
371+
elseif (arg1 === "" && arg2 === nothing) new("", GMTgrid()) # Less usual case in grdlandmask where only kwargs are given
372+
else error("Unknown types ($(typeof(arg1)), $(typeof(arg2))) in wrapGrids")
373+
end
374+
end
375+
end
376+
function unwrapGrids(w::wrapGrids)
377+
return w.fname, !isempty(w.grd) ? w.grd : nothing
378+
end
379+
363380
#=
364381
Base.@kwdef struct GMTtypes
365382
stored::String = ""

src/grd2cpt.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ grd2cpt(cmd0::String; kwargs...) = grd2cpt_helper(cmd0, nothing; kwargs...)
6767
grd2cpt(arg1; kwargs...) = grd2cpt_helper("", arg1; kwargs...)
6868
function grd2cpt_helper(cmd0::String, arg1; kwargs...)
6969
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
70-
grd2cpt_helper(cmd0, arg1, d)
70+
grd2cpt_helper(wrapGrids(cmd0, arg1), d)
7171
end
7272

7373
# ---------------------------------------------------------------------------------------------------
74-
function grd2cpt_helper(cmd0::String, arg1, d::Dict{Symbol, Any})
74+
function grd2cpt_helper(w::wrapGrids, d::Dict{Symbol, Any})
75+
cmd0, arg1 = unwrapGrids(w)
7576

7677
cmd, = parse_common_opts(d, "", [:R :V_params :b :h :t])
7778
cmd, Tvec = helper_cpt(d, cmd)

src/grd2xyz.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ grd2xyz(cmd0::String; kwargs...) = grd2xyz_helper(cmd0, nothing; kwargs...)
4040
grd2xyz(arg1; kwargs...) = grd2xyz_helper("", arg1; kwargs...)
4141
function grd2xyz_helper(cmd0::String, arg1; kwargs...)
4242
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
43-
grd2xyz_helper(cmd0, arg1, d)
43+
grd2xyz_helper(wrapGrids(cmd0, arg1), d)
4444
end
4545

4646
# ---------------------------------------------------------------------------------------------------
47-
function grd2xyz_helper(cmd0::String, arg1, d::Dict{Symbol, Any})
47+
function grd2xyz_helper(w::wrapGrids, d::Dict{Symbol, Any})
48+
cmd0, arg1 = unwrapGrids(w)
4849

4950
cmd, = parse_common_opts(d, "", [:R :V_params :bo :d :f :h :o :s])
5051
cmd = parse_these_opts(cmd, d, [[:C :rcnumbers :row_col :rowcol], [:L :hvline], [:T :stl :STL], [:W :weight], [:Z :onecol :one_col]])

src/grdcontour.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ grdcontour!(arg1; kw...) = grdcontour_helper("", arg1; first=false, kw...)
7171
# ---------------------------------------------------------------------------------------------------
7272
function grdcontour_helper(cmd0::String, arg1; first=true, kw...)
7373
d, K, O = init_module(first, kw...) # Also checks if the user wants ONLY the HELP mode
74-
_grdcontour_helper(cmd0, arg1, O, K, d)
74+
_grdcontour_helper(wrapGrids(cmd0, arg1), O, K, d)
7575
end
76-
function _grdcontour_helper(cmd0::String, arg1, O::Bool, K::Bool, d::Dict{Symbol, Any})
76+
function _grdcontour_helper(w::wrapGrids, O::Bool, K::Bool, d::Dict{Symbol, Any})
77+
cmd0, arg1 = unwrapGrids(w)
78+
7779
arg2, arg3 = nothing, nothing
7880
dict_auto_add!(d) # The ternary module may send options via another channel
7981

src/grdcut.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ grdcut(arg1; kwargs...) = grdcut_helper("", arg1; kwargs...)
4545
grdcut(; kwargs...) = grdcut_helper("", nothing; kwargs...) # To allow grdcut(data=..., ...)
4646
function grdcut_helper(cmd0::String, arg1; kwargs...)
4747
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
48-
grdcut_helper(cmd0, arg1, d)
48+
grdcut_helper(wrapGrids(cmd0, arg1), d)
4949
end
5050

5151
# ---------------------------------------------------------------------------------------------------
52-
function grdcut_helper(cmd0::String, arg1, d::Dict{Symbol, Any})::Union{Nothing, GMTgrid, GMTimage, String}
52+
function grdcut_helper(w::wrapGrids, d::Dict{Symbol, Any})::Union{Nothing, GMTgrid, GMTimage, String}
53+
cmd0, arg1 = unwrapGrids(w)
5354

5455
arg2 = nothing
5556
cmd::String, opt_R::String = parse_R(d, "")

src/grdinfo.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,19 @@ grdinfo(cmd0::String; kwargs...) = grdinfo_helper(cmd0, nothing; kwargs...)
4949
grdinfo(arg1; kwargs...) = grdinfo_helper("", arg1; kwargs...)
5050
function grdinfo_helper(cmd0::String, arg1; kwargs...)
5151
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
52-
grdinfo_helper(cmd0, arg1, d)
52+
grdinfo_helper(wrapGrids(cmd0, arg1), d)
5353
end
5454

5555
# ---------------------------------------------------------------------------------------------------
56-
function grdinfo_helper(cmd0::String, arg1, d::Dict{Symbol, Any})::Union{GMTdataset, String}
56+
function grdinfo_helper(w::wrapGrids, d::Dict{Symbol, Any})::Union{GMTdataset, String}
57+
cmd0, arg1 = unwrapGrids(w)
5758

5859
cmd, = parse_common_opts(d, "", [:R :V_params :f :o])
5960
(is_in_dict(d, [:numeric], del=true) !== nothing) && (cmd *= " -Cn")
6061
cmd = parse_these_opts(cmd, d, [[:C :oneliner], [:D :tiles], [:E :extrema :extreme], [:F :report_ingeog],
6162
[:G :download :force], [:I :nearest], [:L :force_scan], [:Q :cube], [:T :minmax :zmin_max]])
62-
opt_M = add_opt(d, "", "M", [:M :minmax_pos]); (opt_M != "") && (cmd *= opt_M)
63-
opt_L = add_opt(d, "", "L", [:L :force_scan]); (opt_L != "") && (cmd *= opt_L)
63+
opt_M = add_opt(d, "", "M", [:M :minmax_pos]); (opt_M !== "") && (cmd *= opt_M)
64+
opt_L = add_opt(d, "", "L", [:L :force_scan]); (opt_L !== "") && (cmd *= opt_L)
6465

6566
(isa(arg1, GMTgrid) && size(arg1,3) > 1 && !occursin("-Q", cmd)) && (cmd *= " -Q") # arg1 is a CUBE
6667
R = common_grd(d, cmd0, cmd, "grdinfo ", arg1) # Finish build cmd and run it
@@ -71,12 +72,12 @@ function grdinfo_helper(cmd0::String, arg1, d::Dict{Symbol, Any})::Union{GMTdata
7172
append!(hdims, ["z_min","z_max","dx","dy","n_cols","n_rows","reg","isgeog"]) :
7273
append!(hdims, ["b(?)","t(?)","z_min","z_max","dx","dy","dz","n_cols","n_rows","n_layers","reg","isgeog"])
7374
R.colnames = hdims
74-
elseif (opt_M != "" && opt_L == "")
75+
elseif (opt_M !== "" && opt_L == "")
7576
(length(R.data) == 17) && # It can be 19 for cubes (not implemented yet)
7677
(R.colnames = append!(hdims, ["z_min","z_max","dx","dy","n_cols","n_rows","xmin_pos","ymin_pos","xmax_pos","ymax_pos","n_NaNs","reg","isgeog"]))
7778
end
7879
# -o changes it all so we parse just the simplest case. If it fails we remove colnames
79-
if ((t = scan_opt(cmd, "-o")) != "")
80+
if ((t = scan_opt(cmd, "-o")) !== "")
8081
cn = tryparse(Int, t) !== nothing
8182
(cn !== nothing) ? (R.colnames = [R.colnames[cn+1]]) : (R.colnames = String[])
8283
end

src/grdlandmask.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ grdlandmask(arg1::GItype; kwargs...) = grdlandmask_helper("", arg1; kwargs...)
4444
grdlandmask(; kwargs...) = grdlandmask_helper("", nothing; kwargs...)
4545
function grdlandmask_helper(cmd0::String, arg1; kwargs...)
4646
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
47-
grdlandmask_helper(cmd0, arg1, d)
47+
grdlandmask_helper(wrapGrids(cmd0, arg1), d)
4848
end
4949

5050
# ---------------------------------------------------------------------------------------------------
51-
function grdlandmask_helper(cmd0::String, arg1, d::Dict{Symbol, Any})
51+
function grdlandmask_helper(w::wrapGrids, d::Dict{Symbol, Any})
52+
cmd0, arg1 = unwrapGrids(w)
5253

5354
cmd::String, prj::String = "", ""
5455
if (arg1 !== nothing)

src/grdsample.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ grdsample(cmd0::String; kwargs...) = grdsample_helper(cmd0, nothing; kwargs...)
3131
grdsample(arg1; kwargs...) = grdsample_helper("", arg1; kwargs...)
3232
function grdsample_helper(cmd0::String, arg1; kwargs...)
3333
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
34-
grdsample_helper(cmd0, arg1, d)
34+
grdsample_helper(wrapGrids(cmd0, arg1), d)
3535
end
3636

3737
# ---------------------------------------------------------------------------------------------------
38-
function grdsample_helper(cmd0::String, arg1, d::Dict{Symbol, Any})
38+
function grdsample_helper(w::wrapGrids, d::Dict{Symbol, Any})
39+
cmd0, arg1 = unwrapGrids(w)
3940

4041
cmd, = parse_common_opts(d, "", [:G :RIr :V_params :f :n :x])
4142
cmd = parse_these_opts(cmd, d, [[:T :toggle]])

src/grdview.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ grdview!(arg1; kwargs...) = grdview_helper("", arg1; first=false, kwargs
5151
# ---------------------------------------------------------------------------------------------------
5252
function grdview_helper(cmd0::String, arg1; first=true, kwargs...)
5353
d, K, O = init_module(first, kwargs...) # Also checks if the user wants ONLY the HELP mode
54-
invokelatest(grdview_helper, cmd0, arg1, O, K, d)
54+
grdview_helper(wrapGrids(cmd0, arg1), O, K, d)#invokelatest(grdview_helper, cmd0, arg1, O, K, d)
5555
end
56-
function grdview_helper(cmd0::String, arg1, O::Bool, K::Bool, d::Dict{Symbol, Any})
56+
function grdview_helper(w::wrapGrids, O::Bool, K::Bool, d::Dict{Symbol, Any})
57+
cmd0, arg1 = unwrapGrids(w)
5758

5859
arg2 = nothing; arg3 = nothing; arg4 = nothing; arg5 = nothing;
5960
first = !O

0 commit comments

Comments
 (0)