Skip to content

Commit

Permalink
Merge pull request #208 from GenericMappingTools/moreembed
Browse files Browse the repository at this point in the history
Let embedding module calls in grdimage accept more than one
  • Loading branch information
joa-quim authored Apr 4, 2019
2 parents bd61e79 + cec0043 commit 713a956
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ for more details about what the arguments mean.
```julia
topo = makecpt(color=:rainbow, range=(1000,5000,500), continuous=true);
grdimage("@tut_relief.nc", shade="+ne0.8+a100", proj=:Mercator, frame=:a, color=topo)
colorbar!(position="jTC+w5i/0.25i+h+o0/-1i", region=[-108 -103 35 40], color=topo,
proj=[], frame="y+lm", fmt=:jpg, show=true)
colorbar!(pos=(anchor=:TC,length=(12.5,0.6), horizontal=true, offset=(0,-2.5)),
color=topo, frame=(ylabel=:m,), fmt=:jpg, show=true)
```

```@raw html
Expand Down
27 changes: 18 additions & 9 deletions src/common_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1037,17 +1037,26 @@ end

# ---------------------------------------------------------------------------------------------------
function add_opt_module(d::Dict, symbs)
# SYMBS should contain a module name 'coast' or 'plot', and if present in D,
# SYMBS should contain a module name 'coast' or 'colorbar', and if present in D,
# 'val' must be a NamedTuple with the module's arguments.
val, symb = find_in_dict(d, symbs)
if (val !== nothing && isa(val, NamedTuple))
nt = (val..., Vd=:cmd)
if (symb == :coast) return coast!(; nt...)
elseif (symb == :plot) return plot!(; nt...)
out = Array{String,1}()
for k = 1:length(symbs)
r = nothing
if (haskey(d, symbs[k]))
val = d[symbs[k]]
if (isa(val, NamedTuple))
nt = (val..., Vd=:cmd)
if (symbs[k] == :coast) r = coast!(; nt...)
elseif (symbs[k] == :colorbar) r = colorbar!(; nt...)
end
elseif (isa(val, Number) && (val != 0) && symbs[k] == :coast) # Allow setting just ``coast=true``
r = coast!(W=0.5, Vd=:cmd)
end
end
return nothing
else
return nothing
if (r != nothing) append!(out, [r]) end
end
if (out == []) return nothing
else return out
end
end

Expand Down
21 changes: 14 additions & 7 deletions src/grdimage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function grdimage(cmd0::String="", arg1=[], arg2=[], arg3=[]; first=true, kwargs
output, opt_T, fname_ext = fname_out(d) # OUTPUT may have been an extension only

K, O = set_KO(first) # Set the K O dance
cmd, opt_B, = parse_BJR(d, "", "", O, " -JX12c/0")
cmd, opt_B, opt_J, opt_R = parse_BJR(d, "", "", O, " -JX12c/0")
cmd = parse_common_opts(d, cmd, [:UVXY :params :f :n :p :t])
cmd = parse_these_opts(cmd, d, [[:A :img_out :image_out], [:D :img_in :image_in], [:E :dpi], [:G],
[:M :monochrome], [:N :noclip], [:Q :nan_t :nan_alpha], ["," :mem :mem_layout]])
Expand All @@ -87,8 +87,9 @@ function grdimage(cmd0::String="", arg1=[], arg2=[], arg3=[]; first=true, kwargs
if ((val = find_in_dict(d, [:I :shade :intensity :intensfile])[1]) !== nothing)
if (!isa(val, GMTgrid)) # Uff, simple. Either a file name or a -A type modifier
if (isa(val, String) || isa(val, Symbol)) cmd *= " -I" * arg2str(val)
else cmd = add_opt(cmd, 'I', d, [:I :shade :intensity],
(azimuth="+a", norm="+n", default="_+d+a-45+nt1"))
else
cmd = add_opt(cmd, 'I', d, [:I :shade :intensity],
(azimuth="+a", norm="+n", default="_+d+a-45+nt1"))
end
else
cmd, N = put_in_slot(cmd, val, 'I', [arg1, arg2, arg3, arg4])
Expand All @@ -100,10 +101,16 @@ function grdimage(cmd0::String="", arg1=[], arg2=[], arg3=[]; first=true, kwargs
end
end

if (isa(arg1, GMTimage) && !occursin("-D", cmd)) cmd *= " -D" end # GMT bug. It says not necessary but it is.
cmd = "grdimage " * cmd # In any case we need this
if (!occursin("-A", cmd)) # -A means that we are requesting the image directly
if ((cmd2 = add_opt_module(d, [:coast])) !== nothing) K = true end
if (isa(arg1, GMTimage) && !occursin("-D", cmd)) cmd *= " -D" end # GMT bug. It says not need but it is.
if (opt_R == "" && isa(arg1, GMTgrid)) # GMT bug. -R is not stored in gmt.history
cmd *= @sprintf(" -R%f/%f/%f/%f", arg1.range[1], arg1.range[2], arg1.range[3], arg1.range[4])
elseif (opt_R == "" && cmd0 != "")
info = grdinfo(cmd0 * " -I0.000000001") # 0.000...1 because it doesn't accept -I0
cmd *= " " * info[1].text[1] # The -R.... string
end
cmd = "grdimage " * cmd # In any case we need this
if (!occursin("-A", cmd)) # -A means that we are requesting the image directly
if ((cmd2 = add_opt_module(d, [:coast :colorbar])) !== nothing) K = true end
cmd = finish_PS(d, cmd, output, K, O)
if (cmd2 !== nothing) cmd = [cmd; cmd2] end # Plot the coastlines as well
end
Expand Down
12 changes: 6 additions & 6 deletions src/psscale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ Full option list at [`psscale`](http://gmt.soest.hawaii.edu/doc/latest/psscale.h
"""
function colorbar(cmd0::String="", arg1=[]; first=true, kwargs...)

length(kwargs) == 0 && isempty(data) && return monolitic("psscale", cmd0, arg1)
length(kwargs) == 0 && isempty(data) && return monolitic("psscale", cmd0, arg1)

d = KW(kwargs)
output, opt_T, fname_ext = fname_out(d) # OUTPUT may have been an extension only

K, O = set_KO(first) # Set the K O dance
cmd, opt_B, opt_J, opt_R = parse_BJR(d, "", "", O, "")
cmd, opt_B, = parse_BJR(d, "", "", O, "")
cmd = parse_common_opts(d, cmd, [:UVXY :params :p :t])
cmd = parse_these_opts(cmd, d, [[:G :truncate], [:I :shade], [:M :monochrome], [:N :dpi],
[:Q :log], [:S :nolines], [:W :zscale], [:Z :zfile]])
cmd = parse_these_opts(cmd, d, [[:G :truncate], [:I :shade], [:M :monochrome], [:N :dpi],
[:Q :log], [:S :nolines], [:W :zscale], [:Z :zfile]])
cmd = add_opt(cmd, "D", d, [:D :pos :position],
(map=("g", nothing, 1), mirror=("J", nothing, 1), anchor=("", arg2str, 2), length="+w", triangles="+e",
justify="+j", offset="+o", horizontal="_+h", move_annot="+m", neon="_+mc", nan="+n"))
Expand All @@ -76,10 +76,10 @@ function colorbar(cmd0::String="", arg1=[]; first=true, kwargs...)

cmd = add_opt(cmd, 'F', d, [:F :box], (clearance="+c", fill=("+g", add_opt_fill), inner="+i",
pen=("+p", add_opt_pen), rounded="+r", shade="+s"))
cmd = add_opt(cmd, 'L', d, [:L :equal :equal_size], (range="i", gap=""))
cmd = add_opt(cmd, 'L', d, [:L :equal :equal_size], (range="i", gap=""))

cmd = finish_PS(d, cmd, output, K, O)
return finish_PS_module(d, "psscale " * cmd, "", output, fname_ext, opt_T, K, arg1)
return finish_PS_module(d, "psscale " * cmd, "", output, fname_ext, opt_T, K, arg1)
end

# ---------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/psxy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function common_plot_xyz(cmd0, arg1, caller, first, is3D, kwargs...)
else gmt_proggy = "psxy "
end

((cmd0 == "" && isempty_(arg1)) || occursin(" -", cmd0)) && return monolitic(gmt_proggy, cmd0, arg1)
(occursin(" -", cmd0)) && return monolitic(gmt_proggy, cmd0, arg1)

cmd = ""; sub_module = "" # Will change to "scatter", etc... if called by sub-modules
if (caller != "")
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ if (got_it) # Otherwise go straight to end
@test_throws ErrorException("No, no, no. Nothing useful in the region named tuple arguments") GMT.build_opt_R((zz=:x,))
@test_throws ErrorException("Unknown continent name") GMT.build_opt_R((continent='a',extend=4))
@test_throws ErrorException("Increments for limits must be a String, a Number, Array or Tuple") GMT.build_opt_R((iso="PT",extend='8'))
@test_throws ErrorException("The only valid case to provide a number to the 'proj' option is when that number is an EPSG code, but this (1500) is clearly an invalid EPSG") GMT.build_opt_J(1500)
@test GMT.build_opt_J(:X5)[1] == " -JX5"
@test GMT.build_opt_J(2500)[1] == " -J2500"
@test GMT.build_opt_J([])[1] == " -J"
Expand Down Expand Up @@ -76,6 +77,7 @@ if (got_it) # Otherwise go straight to end
@test GMT.get_color((0.1,0.2,0.3)) == "26/51/77"
@test GMT.get_color([1 2 3]) == "1/2/3"
@test GMT.get_color([0.4 0.5 0.8; 0.1 0.2 0.7]) == "102/26/128,26/51/179"
@test_throws ErrorException("GOT_COLOR, got and unsupported data type: Tuple{Int64,Int64}") GMT.get_color((1,2))
@test GMT.parse_unit_unit("data") == "u"
@test GMT.parse_units((2,:p)) == "2p"
@test GMT.add_opt((a=(1,0.5),b=2), (a="+a",b="-b")) == "+a1/0.5-b2"
Expand Down Expand Up @@ -380,6 +382,8 @@ if (got_it) # Otherwise go straight to end
grdimage(rand(Float32, 128, 128)*255, rand(Float32, 128, 128)*255, rand(Float32, 128, 128)*255, J="X10")
grdimage(data=(Gr,Gg,Gb), J=:X10, I=mat2grid(rand(Float32,128,128)), Vd=:cmd)
grdimage(rand(Float32, 128, 128), shade=(default=30,), coast=(W=1,), Vd=:cmd)
grdimage(rand(Float32, 128, 128), colorbar=(color=:rainbow, pos=(anchor=:RM,length=8)), Vd=:cmd)
grdimage("lixo.grd", coast=true, Vd=:cmd)
#grdimage("@earth_relief_05m", J="S21/90/15c", R="10/68/50/80r", B=:afg, X=:c, I="+")
PS = grdview(G, J="X6i", JZ=5, I=45, Q="s", C="topo", R="-15/15/-15/15/-1/1", view="120/30", ps=1);
gmt("destroy")
Expand Down

0 comments on commit 713a956

Please sign in to comment.