Skip to content

Commit

Permalink
Merge pull request #286 from GenericMappingTools/finishmodern
Browse files Browse the repository at this point in the history
Finish the modern mode group (add gmtfig)
  • Loading branch information
joa-quim authored Jul 12, 2019
2 parents 77dc3ce + 85e0a2d commit dc234cb
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 50 deletions.
5 changes: 3 additions & 2 deletions src/GMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export
arrows, arrows!, bar, bar!, bar3, bar3!, lines, lines!, legend, legend!,
basemap, basemap!, blockmean, blockmedian, blockmode, clip, clip!, coast, coast!, colorbar, colorbar!,
contour, contour!, filter1d, fitcircle, gmt2kml, gmtconnect, gmtconvert, gmtinfo, gmtregress,
gmtbegin, gmtend, gmtread, gmtselect, gmtset, gmtsimplify, gmtspatial, gmtvector, gmtwrite, gmtwhich,
gmtread, gmtselect, gmtset, gmtsimplify, gmtspatial, gmtvector, gmtwrite, gmtwhich,
grd2cpt, grd2kml, grd2xyz, grdblend, grdclip, grdcontour, grdcontour!, grdcut, grdedit, grdfft,
grdfilter, grdgradient, grdhisteq, grdimage, grdimage!, grdinfo, grdlandmask, grdpaste, grdproject,
grdsample, grdtrack, grdtrend, grdvector, grdvector!, grdview, grdview!, grdvolume, greenspline,
Expand All @@ -44,8 +44,9 @@ export
pssolar, pssolar!, psternary, psternary!, pstext, pstext!, pswiggle, pswiggle!, psxy, psxy!, psxyz,
psxyz!, regress, rose, rose!, sample1d, scatter, scatter!, scatter3, scatter3!, solar, solar!, spectrum1d,
sphdistance, sphinterpolate, sphtriangulate, surface, ternary, ternary!,
text, text!, text_record, trend1d, trend2d, triangulate, splitxyz, subplot,
text, text!, text_record, trend1d, trend2d, triangulate, splitxyz,
decorated, vector_attrib, wiggle, wiggle!, xyz2grd,
gmtbegin, gmtend, subplot, gmtfig, inset,
linspace, logspace, contains, fields, tic, toc

include("common_docs.jl")
Expand Down
23 changes: 14 additions & 9 deletions src/common_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ end

function parse_R(cmd::String, d::Dict, O=false, del=false)
# Build the option -R string. Make it simply -R if overlay mode (-O) and no new -R is fished here
global IamModern
opt_R = ""
val, symb = find_in_dict(d, [:R :region :limits])
if (val !== nothing)
opt_R = build_opt_R(val)
if (del) delete!(d, symb) end
elseif (IamModern)
return cmd, ""
end
if (isempty(opt_R)) # See if we got the region as tuples of xlim, ylim [zlim]

if (opt_R == "") # See if we got the region as tuples of xlim, ylim [zlim]
R = ""; c = 0
if (haskey(d, :xlim) && isa(d[:xlim], Tuple) && length(d[:xlim]) == 2)
R = @sprintf(" -R%.15g/%.15g", d[:xlim][1], d[:xlim][2])
Expand Down Expand Up @@ -140,11 +144,11 @@ function parse_J(cmd::String, d::Dict, default="", map=true, O=false, del=false)
# Build the option -J string. Make it simply -J if overlay mode (-O) and no new -J is fished here
# Default to 12c if no size is provided.
# If MAP == false, do not try to append a fig size
global IamSubplot
global IamModern
opt_J = ""; mnemo = false
if ((val = find_in_dict(d, [:J :proj :projection], del)[1]) !== nothing)
opt_J, mnemo = build_opt_J(val)
elseif (IamSubplot) # Subplots do not rely is the classic default mechanism
elseif (IamModern) # Subplots do not rely is the classic default mechanism
return cmd, ""
end
if (!map && opt_J != "")
Expand Down Expand Up @@ -362,8 +366,8 @@ end
# ---------------------------------------------------------------------------------------------------
function parse_B(cmd::String, d::Dict, opt_B::String="", del=false)

global IamSubplot
def_fig_axes_ = (IamSubplot) ? "" : def_fig_axes # def_fig_axes is a global const
global IamModern
def_fig_axes_ = (IamModern) ? "" : def_fig_axes # def_fig_axes is a global const

# These four are aliases
extra_parse = true
Expand Down Expand Up @@ -451,12 +455,12 @@ function parse_BJR(d::Dict, cmd::String, caller, O, defaultJ="", del=false)
cmd, opt_R = parse_R(cmd, d, O, del)
cmd, opt_J = parse_J(cmd, d, defaultJ, true, O, del)

global IamSubplot
def_fig_axes_ = (IamSubplot) ? "" : def_fig_axes # def_fig_axes is a global const
global IamModern
def_fig_axes_ = (IamModern) ? "" : def_fig_axes # def_fig_axes is a global const

if (caller != "" && occursin("-JX", opt_J)) # e.g. plot() sets 'caller'
if (caller == "plot3d" || caller == "bar3" || caller == "scatter3")
def_fig_axes3_ = (IamSubplot) ? "" : def_fig_axes3
def_fig_axes3_ = (IamModern) ? "" : def_fig_axes3
cmd, opt_B = parse_B(cmd, d, (O ? "" : def_fig_axes3_), del)
else
cmd, opt_B = parse_B(cmd, d, (O ? "" : def_fig_axes_), del) # For overlays, default is no axes
Expand Down Expand Up @@ -1890,6 +1894,7 @@ end
function read_data(d::Dict, fname::String, cmd, arg, opt_R="", opt_i="", is3D=false)
# In case DATA holds a file name, read that data and put it in ARG
# Also compute a tight -R if this was not provided
global IamModern
data_kw = nothing
if (haskey(d, :data)) data_kw = d[:data] end
if (fname != "") data_kw = fname end
Expand Down Expand Up @@ -1918,7 +1923,7 @@ function read_data(d::Dict, fname::String, cmd, arg, opt_R="", opt_i="", is3D=fa

if (data_kw !== nothing) arg = data_kw end # Finaly move the data into ARG

if (opt_R == "" || opt_R[1] == '/')
if (!IamModern && (opt_R == "" || opt_R[1] == '/'))
info = gmt("gmtinfo -C" * opt_i * opt_di * opt_h, arg) # Here we are reading from an original GMTdataset or Array
if (opt_R != "" && opt_R[1] == '/') # Modify what will be reported as a -R string
# Example "/-0.1/0.1/0//" will extend x axis +/- 0.1, set y_min=0 and no change to y_max
Expand Down
3 changes: 2 additions & 1 deletion src/gmt_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1578,8 +1578,9 @@ D = mat2ds(mat; x=nothing, hdr=nothing, color=nothing)
`color` optional array with color names. Its length can be smaller than n_rows, case in which colors will be
cycled.
"""
function mat2ds(mat; x=nothing, hdr=nothing, color=nothing, ls=nothing, text=nothing)
function mat2ds(mat, txt=nothing; x=nothing, hdr=nothing, color=nothing, ls=nothing, text=nothing)

if (txt !== nothing) return text_record(mat, txt, hdr) end
if (text !== nothing) return text_record(mat, text, hdr) end

if (x === nothing)
Expand Down
106 changes: 75 additions & 31 deletions src/gmtbegin.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
gmtbegin(name::String=""; fmt)
gmtbegin(name::String=""; fmt)
Start a GMT session in modern mode (GMT >= 6).
'name' contains the figure name with or without extension. If an extension is used
Expand All @@ -10,43 +10,87 @@ As an alternative use 'fmt' as a string or symbol containing the format (ps, pdf
By default name="GMTplot" and fmt="ps"
"""
function gmtbegin(name::String=""; fmt=nothing, verbose=nothing)
global IamModern = true

cmd = "begin" # Default name (GMTplot.ps) is set in gmt_main()
if (name != "") cmd *= " " * get_format(name, fmt) end
if (verbose !== nothing) cmd *= " -V" * string(verbose) end
gmt(cmd)
return nothing
cmd = "begin" # Default name (GMTplot.ps) is set in gmt_main()
if (name != "") cmd *= " " * get_format(name, fmt) end
if (verbose !== nothing) cmd *= " -V" * string(verbose) end
gmt(cmd)
return nothing
end

"""
gmtend(show=false, verbose=nothing)
gmtend(show=false, verbose=nothing)
Ends a GMT session in modern mode (GMT >= 6) and optionaly shows the figure
"""
function gmtend(; show::Bool=false, verbose=nothing)
global IamModern = false
cmd = "end"
if (show) cmd *= " show" end
if (verbose !== nothing) cmd *= " -V" * string(verbose) end
gmt(cmd)
return nothing
function gmtend(show=nothing; verbose=nothing)
cmd = "end"
if (show !== nothing) cmd *= " show" end
if (verbose !== nothing) cmd *= " -V" * string(verbose) end
gmt(cmd)
return nothing
end

"""
function gmtfig(name::String; fmt=nothing, opts="")
Set attributes for the current modern mode session figure.
'name' name of the new (or resumed) figure. It may contain an extension.
'fmt' figures graphics format (or formats, e.g. fmt="eps,pdf"). Not needed if 'name' has extension
'opts' Sets one or more comma-separated options (and possibly arguments) that can be passed to psconvert when preparing this figure.
"""
function gmtfig(name::String; fmt=nothing, opts="")
global IamModern
if (!IamModern) error("Not in modern mode. Must run 'gmtbegin' first") end

cmd = "figure" # Default name (GMTplot.ps) is set in gmt_main()
if (name == "") error("figure name cannot be empty") end
cmd *= " " * get_format(name, fmt)
if (opts != "") cmd *= " " * opts end
gmt(cmd)
return nothing
end

function inset(fim=nothing; stop=false, kwargs...)

global IamModern
if (!IamModern) error("Not in modern mode. Must run 'gmtbegin' first") end

d = KW(kwargs)
cmd = parse_common_opts(d, "", [:c :F :V_params], true)
cmd = parse_these_opts(cmd, d, [[:M :margins]])
cmd = parse_type_anchor(d, cmd, [[:D :inset :inset_box]])

do_show = false
if (fim !== nothing)
t = lowercase(string(fim))
if (t == "end" || t == "stop") stop = true
elseif (t == "show") stop = true; do_show = true
end
end

if (!stop)
if (dbg_print_cmd(d, cmd) !== nothing) return cmd end # Vd=2 cause this return
gmt("inset begin " * cmd);
else
gmt("inset end");
if (do_show || haskey(d, :show)) gmt("end" * show); end
end
end

function get_format(name, fmt=nothing, d=nothing)
# Get the fig name and format. If format not specified, default to FMT (ps)
# NAME is supposed to always exist (otherwise, errors)
fname, ext = splitext(string(name))
if (ext != "")
fname *= " " * ext[2:end]
elseif (fmt !== nothing)
fname *= " " * string(fmt) # No checking
elseif (d !== nothing)
if (haskey(d, :fmt)) fname *= " " * string(d[:fmt])
else fname *= " " * FMT # Then use default format
end
else
fname *= " " * FMT
end
return fname
# Get the fig name and format. If format not specified, default to FMT (ps)
# NAME is supposed to always exist (otherwise, errors)
fname, ext = splitext(string(name))
if (ext != "")
fname *= " " * ext[2:end]
elseif (fmt !== nothing)
fname *= " " * string(fmt) # No checking
elseif (d !== nothing)
if (haskey(d, :fmt)) fname *= " " * string(d[:fmt])
else fname *= " " * FMT # Then use default format
end
else
fname *= " " * FMT
end
return fname
end
6 changes: 1 addition & 5 deletions src/subplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ function subplot(fim=nothing; stop=false, kwargs...)
end
gmt("begin " * fname)
end
try
gmt("subplot begin " * cmd);
catch
gmt("end"); return nothing
end
gmt("subplot begin " * cmd);
IamSubplot = true
elseif (do_set)
if (!IamSubplot) error("Cannot call subplot(set, ...) before setting dimensions") end
Expand Down
18 changes: 16 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ if (got_it) # Otherwise go straight to end
bar(T, color=:rainbow, figsize=(14,8), title="Colored bars", Vd=2)
T = mat2ds([1.0 0.446143 0; 2.0 0.581746 0; 3.0 0.268978 0], text=[" "; " "; " "]);
bar(T, color=:rainbow, figsize=(14,8), mz=[3 2 1], Vd=2)
mat2ds([0 0],["aa"]);

# BAR3
G = gmt("grdmath -R-15/15/-15/15 -I1 X Y HYPOT DUP 2 MUL PI MUL 8 DIV COS EXCH NEG 10 DIV EXP MUL =");
Expand Down Expand Up @@ -841,7 +842,7 @@ if (got_it) # Otherwise go straight to end
G = sphdistance(R="0/10/0/10", I=0.1, Q=D, L=:k, Vd=2); # But works with data from sph_3.sh test
@test sphdistance(nothing, R="0/10/0/10", I=0.1, Q="D", L=:k, Vd=2) == "sphdistance -I0.1 -R0/10/0/10 -Lk -QD"

# SUBPLOT
# MODERN
if (GMTver >= 6)
@test GMT.helper_sub_F("1/2") == "1/2"
#@test GMT.helper_sub_F((size=(1,2), frac=((2,3),(3,4,5))) ) == "1/2+f2,3/3,4,5"
Expand All @@ -857,9 +858,22 @@ if (got_it) # Otherwise go straight to end
@test_throws ErrorException("SUBPLOT: garbage in DIMS option") GMT.helper_sub_F([1 2 3])
@test_throws ErrorException("SUBPLOT: 'grid' keyword is mandatory") subplot(F=("1i"), Vd=2)
@test_throws ErrorException("Cannot call subplot(set, ...) before setting dimensions") subplot(:set, F=("1i"), Vd=2)
subplot(grid="1x1", limits="0/5/0/5", frame="west", F="s7/7", title="VERY VERY");subplot(:set, panel=(1,1));plot([0 0; 1 1]);subplot(:end)
subplot(name="lixo", grid="1x1", limits="0/5/0/5", frame="west", F="s7/7", title="VERY VERY");subplot(:set, panel=(1,1));plot([0 0; 1 1]);subplot(:end)
gmtbegin("lixo.ps"); gmtend()
gmtbegin("lixo", fmt=:ps); gmtend()
gmtbegin("lixo"); gmtend()
gmtbegin(); gmtend()

gmtbegin("inset.ps")
basemap(region=(0,40,20,60), proj=:merc, figsize=16, frame=(annot=:afg, fill=:lightgreen))
inset(D="jTR+w2.5i+o0.2i", F="+gpink+p0.5p", margins=0.6)
basemap(region=:global360, J="A20/20/2i", frame=:afg)
text(text_record([1 1],["INSET"]), font=18, region_justify=:TR, D="j-0.15i", noclip=true)
inset(:end)
text(text_record([0 0],[" "]), text="MAP", font=18, region_justify=:BL, D="j0.2i")
gmtend()

gmtbegin(); gmtfig("lixo.ps"); gmtend()
end

# SURFACE
Expand Down

0 comments on commit dc234cb

Please sign in to comment.