Skip to content

Commit

Permalink
Merge pull request #219 from GenericMappingTools/imgview
Browse files Browse the repository at this point in the history
Introduce get_cpt_set_R() and common_shade() functions
  • Loading branch information
joa-quim authored Apr 15, 2019
2 parents ae96b0e + 8b12743 commit 8770dad
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 51 deletions.
4 changes: 2 additions & 2 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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!(pos=(anchor=:TC,length=(12.5,0.6), horizontal=true, offset=(0,-2.5)),
colorbar!(pos=(anchor=:TC,length=(12.5,0.6), horizontal=true, offset=(0,1.0)),
color=topo, frame=(ylabel=:m,), fmt=:jpg, show=true)
```

Expand All @@ -108,7 +108,7 @@ We will make a perspective, color-coded view of the US Rockies from the southeas
```julia
topo = makecpt(color=:rainbow, range=(1000,5000,500), continuous=true);
grdview("@tut_relief.nc", proj=:Mercator, JZ="1c", shade="+ne0.8+a100", view=(135,30),
frame=:a, fmt=:jpg, color=topo, Q="i100", show=true)
frame=:a, fmt=:jpg, Q="i100", show=true)
```

```@raw html
Expand Down
1 change: 0 additions & 1 deletion docs/src/grdcontour.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ Optional Arguments
label strings with a comma (*e.g.*, *labels="lo,hi"*). If a file is given by **cont**, and **ticks** is set,
then only contours marked with upper case C or A will have tick marks [and annotations].


- **W** or *pen* : *pen=(annot=true, contour=true, pen=pen)*\
*annot=true*, if present, means to annotate contours or *contour=true* for regular contours [Default].
The *pen* sets the attributes for the particular line. Default pen for annotated contours: *pen=(0.75,:black)*.
Expand Down
36 changes: 36 additions & 0 deletions src/common_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,42 @@ function add_opt_fill(cmd::String, d::Dict, symbs, opt="")
return cmd
end

# ---------------------------------------------------------------------------------------------------
function get_cpt_set_R(d, cmd0, cmd, opt_R, got_fname, arg1, arg2=nothing, arg3=nothing, prog="")
# Get CPT either from keyword input of from current_cpt.
# Also puts -R in cmd when accessing grids from grdimage|view|contour, etc... (due to a GMT bug that doesn't do it)
cpt_opt_T = ""
if (isa(arg1, GMTgrid)) # GMT bug, -R will not be stored in gmt.history
cpt_opt_T = @sprintf(" -T%f/%f/128+n", arg1.range[5], arg1.range[6])
if (opt_R == "")
cmd *= @sprintf(" -R%f/%f/%f/%f", arg1.range[1], arg1.range[2], arg1.range[3], arg1.range[4])
end
elseif (cmd0 != "")
info = grdinfo(cmd0 * " -C"); range = info[1].data
cpt_opt_T = @sprintf(" -T%.14g/%.14g/128+n", range[5], range[6])
if (opt_R == "")
cmd *= @sprintf(" -R%.14g/%.14g/%.14g/%.14g", range[1], range[2], range[3], range[4])
end
end

N_used = got_fname == 0 ? 1 : 0 # To know whether a cpt will go to arg1 or arg2
get_cpt = false
if (prog == "grdview")
if ((val = find_in_dict(d, [:G :drapefile])[1]) !== nothing)
if (isa(val, Tuple) && length(val) == 3) cpt_opt_T = "" end
end
get_cpt = true
elseif (prog == "grdimage" && (isempty_(arg3) && !occursin("-D", cmd)))
get_cpt = true # This still lieve out the case when the r,g,b were sent as a text.
#elseif (prog == "")
#get_cpt = true
end
if (get_cpt)
cmd, arg1, arg2, = add_opt_cpt(d, cmd, [:C :color :cmap], 'C', N_used, arg1, arg2, true, true, cpt_opt_T)
end
return cmd, N_used, arg1, arg2, arg3
end

# ---------------------------------------------------------------------------------------------------
function add_opt_module(d::Dict, symbs)
# SYMBS should contain a module name 'coast' or 'colorbar', and if present in D,
Expand Down
1 change: 1 addition & 0 deletions src/grdcontour.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function grdcontour(cmd0::String="", arg1=nothing; first=true, kwargs...)

N_used = got_fname == 0 ? 1 : 0 # To know whether a cpt will go to arg1 or arg2
cmd, arg1, arg2, = add_opt_cpt(d, cmd, [:C :color :cmap], 'C', N_used, arg1, arg2)
#cmd, N_used, arg1, arg2, = get_cpt_set_R(d, cmd0, cmd, opt_R, got_fname, arg1, arg2)

if (!occursin(" -C", cmd)) # Otherwise ignore an eventual :cont because we already have it
cmd = add_opt(cmd, 'C', d, [:cont :contours :levels])
Expand Down
43 changes: 19 additions & 24 deletions src/grdimage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,20 @@ function grdimage(cmd0::String="", arg1=nothing, arg2=nothing, arg3=nothing; fir
if (!isempty_(arg3) && isa(arg3, Array{<:Number})) arg3 = mat2grid(arg3) end
end

cpt_opt_T = ""
if (opt_R == "" && isa(arg1, GMTgrid)) # GMT bug, -R will not be stored in gmt.history
cmd *= @sprintf(" -R%f/%f/%f/%f", arg1.range[1], arg1.range[2], arg1.range[3], arg1.range[4])
cpt_opt_T = @sprintf(" -T%f/%f/128+n", arg1.range[5], arg1.range[6])
elseif (opt_R == "" && cmd0 != "")
info = grdinfo(cmd0 * " -C"); range = info[1].data
cmd *= @sprintf(" -R%.14g/%.14g/%.14g/%.14g", range[1], range[2], range[3], range[4])
cpt_opt_T = @sprintf(" -T%.14g/%.14g/128+n", range[5], range[6])
end
cmd, N_used, arg1, arg2, arg3 = get_cpt_set_R(d, cmd0, cmd, opt_R, got_fname, arg1, arg2, arg3, "grdimage")
cmd, arg1, arg2, arg3, arg4 = common_shade(d, cmd, arg1, arg2, arg3, arg4, "grdimage")

N_used = got_fname == 0 ? 1 : 0 # To know whether a cpt will go to arg1 or arg2
if (isempty_(arg3) && !occursin("-D", cmd)) # But this lieves out the case when the r,g,b were sent as a text.
cmd, arg1, arg2, = add_opt_cpt(d, cmd, [:C :color :cmap], 'C', N_used, arg1, arg2, true, true, cpt_opt_T)
if (isa(arg1, GMTimage) && !occursin("-D", cmd)) cmd *= " -D" end # GMT bug. It says not need 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
cmd, K = finish_PS_nested(d, cmd, output, K, O, [:coast :colorbar :basemap])
end
return finish_PS_module(d, cmd, "", output, fname_ext, opt_T, K, arg1, arg2, arg3, arg4)
end

# ---------------------------------------------------------------------------------------------------
function common_shade(d, cmd, arg1, arg2, arg3, arg4, prog)
# Used both by grdimage and grdview
if ((val = find_in_dict(d, [:I :shade :intensity])[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)
Expand All @@ -101,21 +100,17 @@ function grdimage(cmd0::String="", arg1=nothing, arg2=nothing, arg3=nothing; fir
(auto="_+", azimuth="+a", norm="+n", default="_+d+a-45+nt1"))
end
else
cmd, N = put_in_slot(cmd, val, 'I', [arg1, arg2, arg3, arg4])
if (N == 1) arg1 = val
elseif (N == 2) arg2 = val
elseif (N == 3) arg3 = val
elseif (N == 4) arg4 = val
if (prog == "grdimage") cmd, N_used = put_in_slot(cmd, val, 'I', [arg1, arg2, arg3, arg4])
else cmd, N_used = put_in_slot(cmd, val, 'I', [arg1, arg2, arg3])
end
if (N_used == 1) arg1 = val
elseif (N_used == 2) arg2 = val
elseif (N_used == 3) arg3 = val
elseif (N_used == 4) arg4 = val # grdview doesn't have this case but no harm to not test for that
end
end
end

if (isa(arg1, GMTimage) && !occursin("-D", cmd)) cmd *= " -D" end # GMT bug. It says not need 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
cmd, K = finish_PS_nested(d, cmd, output, K, O, [:coast :colorbar :basemap])
end
return finish_PS_module(d, cmd, "", output, fname_ext, opt_T, K, arg1, arg2, arg3, arg4)
return cmd, arg1, arg2, arg3, arg4
end

# ---------------------------------------------------------------------------------------------------
Expand Down
30 changes: 7 additions & 23 deletions src/grdview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,39 +77,23 @@ function grdview(cmd0::String="", arg1=nothing; first=true, kwargs...)

if (isa(arg1, Array{<:Number})) arg1 = mat2grid(arg1) end

N_used = got_fname == 0 ? 1 : 0 # To know whether a cpt will go to arg1 or arg2
cmd, arg1, arg2, = add_opt_cpt(d, cmd, [:C :color :cmap], 'C', N_used, arg1, arg2)

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],
(auto="_+", azimuth="+a", norm="+n", default="_+d+a-45+nt1"))
end
else
cmd, N_used = put_in_slot(cmd, val, 'I', [arg1, arg2, arg3])
if (N_used == 1) arg1 = val
elseif (N_used == 2) arg2 = val
elseif (N_used == 3) arg3 = val
end
end
end
cmd, N_used, arg1, arg2, arg3 = get_cpt_set_R(d, cmd0, cmd, opt_R, got_fname, arg1, arg2, arg3, "grdview")
cmd, arg1, arg2, arg3, arg4 = common_shade(d, cmd, arg1, arg2, arg3, arg4, "grdview")

if ((val = find_in_dict(d, [:G :drapefile])[1]) !== nothing)
if (isa(val, String)) # Uff, simple. Either a file name or a -A type modifier
cmd *= " -G" * val
elseif (isa(val, GMTgrid)) # A single drape grid (arg1-3 may be used already)
cmd, N_used = put_in_slot(cmd, val, 'G', [arg1, arg2, arg3, arg4])
if (N_used == 1) arg1 = val
elseif (N_used == 2) arg2 = val
elseif (N_used == 3) arg3 = val
elseif (N_used == 4) arg4 = val
if (N_used == 1) arg1 = val
elseif (N_used == 2) arg2 = val
elseif (N_used == 3) arg3 = val
elseif (N_used == 4) arg4 = val
end
elseif (isa(val, Tuple) && length(val) == 3)
cmd, N_used = put_in_slot(cmd, val[1], 'G', [arg1, arg2, arg3, arg4, arg5])
cmd *= " -G -G" # Because the above only set one -G and we need 3
if (N_used == 1) arg1 = val[1]; arg2 = val[2]; arg3 = val[3]
if (N_used == 1) arg1 = val[1]; arg2 = val[2]; arg3 = val[3]
elseif (N_used == 2) arg2 = val[1]; arg3 = val[2]; arg4 = val[3]
elseif (N_used == 3) arg3 = val[1]; arg4 = val[2]; arg5 = val[3]
end
Expand Down
2 changes: 1 addition & 1 deletion src/makecpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function makecpt(cmd0::String="", arg1=nothing; kwargs...)

if (haskey(d, :cptname)) cmd = cmd * " > " * d[:cptname] end
(haskey(d, :Vd)) && println(@sprintf("\tmakecpt %s", cmd))
C = gmt("makecpt " * cmd, arg1)
global current_cpt = gmt("makecpt " * cmd, arg1)
end

# ---------------------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ if (got_it) # Otherwise go straight to end
grdview!(G, G=G, J="X6i", JZ=5, I=45, Q="s", C="topo", R="-15/15/-15/15/-1/1", view="120/30", Vd=:cmd);
r = grdview!(G, plane=(-6,:lightgray), surftype=(surf=true,mesh=:red), Vd=:cmd);
@test startswith(r, "grdview -R -J -N-6+glightgray -Qsmred")
@test_throws ErrorException("Wrong way of setting the drape (G) option.") grdview(rand(16,16), G=(1,2))
if (GMTver >= 6) # Crashes GMT5
grdview(rand(128,128), G=(Gr,Gg,Gb), I=mat2grid(rand(Float32,128,128)), J=:X12, JZ=5, Q=:i, view="145/30")
end
Expand Down

0 comments on commit 8770dad

Please sign in to comment.