Skip to content

Commit ed563bd

Browse files
committed
Make many of the datasets functions parametric. Other annotations changes.
1 parent adedf24 commit ed563bd

9 files changed

Lines changed: 131 additions & 63 deletions

File tree

src/extras/weather.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ function _meteostat(ID::String, granularity::String, startdate::String, enddate:
929929
n_years = (year2 - year1 + 1)
930930
D = load_meteostat(ID, granul, year1, verbose)
931931
if (n_years > 1) # If more than one year, read multiple files
932-
Dv = Vector{GMTdataset}(undef, n_years-1)
932+
Dv = Vector{GMTdataset{Float64,2}}(undef, n_years-1)
933933
for k = year1+1:year2
934934
Dv[k-year1] = load_meteostat(ID, granul, k, verbose)
935935
end

src/gdal_utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ function lonlat2xy(xy::Vector{<:Real}, t_srs_=nothing; t_srs=nothing, s_srs=prj4
990990
end
991991
lonlat2xy(x::Real, y::Real, t_srs_=nothing; t_srs=nothing, s_srs=prj4WGS84) = lonlat2xy([x y], t_srs_; t_srs=t_srs, s_srs=s_srs)
992992

993-
function lonlat2xy(xy::Matrix{<:Real}, t_srs_=nothing; t_srs=nothing, s_srs=prj4WGS84)
993+
function lonlat2xy(xy::Matrix{T}, t_srs_=nothing; t_srs=nothing, s_srs=prj4WGS84) where {T <: Real}
994994
(t_srs_ !== nothing) && (t_srs = t_srs_)
995995
isa(s_srs, Int) && (s_srs = epsg2wkt(s_srs))
996996
isa(t_srs, Int) && (t_srs = epsg2wkt(t_srs))
@@ -1002,7 +1002,7 @@ function lonlat2xy(xy::Matrix{<:Real}, t_srs_=nothing; t_srs=nothing, s_srs=prj4
10021002
end
10031003

10041004
lonlat2xy(D::GMTdataset, t_srs_=nothing; t_srs=nothing, s_srs=prj4WGS84) = lonlat2xy([D], t_srs_; t_srs=t_srs, s_srs=s_srs)
1005-
function lonlat2xy(D::Vector{<:GMTdataset}, t_srs_=nothing; t_srs=nothing, s_srs=prj4WGS84)
1005+
function lonlat2xy(D::Vector{<:GMTdataset{T,2}}, t_srs_=nothing; t_srs=nothing, s_srs=prj4WGS84) where {T<:Real}
10061006
(t_srs_ !== nothing) && (t_srs = t_srs_)
10071007
isa(t_srs, Int) && (t_srs = epsg2wkt(t_srs))
10081008
isa(s_srs, Int) && (s_srs = epsg2wkt(s_srs))
@@ -1129,7 +1129,7 @@ Return a vector of GMTdatasets from a vector of WKT strings.
11291129
"""
11301130
function readgeom(wkt::Vector{String})
11311131
n_ds = length(wkt)
1132-
D = Vector{GMTdataset}(undef, n_ds)
1132+
D = Vector{GMTdataset{Float64,2}}(undef, n_ds)
11331133
for k = 1:n_ds
11341134
D[k] = gd2gmt(fromWKT(wkt[k]))
11351135
end

src/gmt_main.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ function ogr2GMTdataset(in::Ptr{OGR_FEATURES}, drop_islands=false)::Union{GMTdat
14051405

14061406
if (OGR_F.n_islands == 0)
14071407
geom_type = unsafe_string(OGR_F.type)
1408-
geom = (geom_type == "Polygon") ? wkbPolygon : ((geom_type == "LineString") ? wkbLineString : wkbPoint)
1408+
geom::Int = (geom_type == "Polygon") ? wkbPolygon : ((geom_type == "LineString") ? wkbLineString : wkbPoint)
14091409
(is3D && (geom == wkbPolygon || geom == wkbPoint)) && (geom == wkbPolygon ? wkbPointZ : wkbPointZM) # Convert 2D to 3D
14101410
data = is3D ? [unsafe_wrap(Array, OGR_F.x, OGR_F.np) unsafe_wrap(Array, OGR_F.y, OGR_F.np) unsafe_wrap(Array, OGR_F.z, OGR_F.np)] : [unsafe_wrap(Array, OGR_F.x, OGR_F.np) unsafe_wrap(Array, OGR_F.y, OGR_F.np)]
14111411
D[n] = GMTdataset(data, Float64[], Float64[], attrib, coln, String[], hdr, String[], proj4, wkt, 0, Int(geom))

src/gmtspatial.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,23 @@ Parameters
5050
end points in the directions implied by the line ends.
5151
5252
"""
53-
function gmtspatial(cmd0::String="", arg1=nothing, arg2 = nothing; kwargs...)
53+
gmtspatial(cmd0::String, arg1=nothing; kw...) = gmtspatial_helper(cmd0, arg1, nothing; kw...)
54+
gmtspatial(arg1; kw...) = gmtspatial_helper("", arg1, nothing; kw...)
55+
gmtspatial(arg1, arg2; kw...) = gmtspatial_helper("", arg1, arg2; kw...)
5456

57+
# ---------------------------------------------------------------------------------------------------
58+
function gmtspatial_helper(cmd0::String, arg1, arg2; kw...)
5559
(cmd0 == "" && arg1 === nothing && arg2 === nothing && length(kwargs) == 0) && return gmt("gmtspatial")
56-
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
60+
d = init_module(false, kw...)[1] # Also checks if the user wants ONLY the HELP mode
61+
isa(arg1, Matrix) && (arg1 = mat2ds(arg1))
62+
_gmtspatial_helper(cmd0, arg1, arg2, d)
63+
end
64+
65+
#function gmtspatial(cmd0::String="", arg1=nothing, arg2 = nothing; kwargs...)
66+
function _gmtspatial_helper(cmd0::String, arg1, arg2, d::Dict)::Union{GMTdataset{Float64,2}, Vector{<:GMTdataset{Float64,2}}}
67+
68+
#(cmd0 == "" && arg1 === nothing && arg2 === nothing && length(kwargs) == 0) && return gmt("gmtspatial")
69+
#d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
5770
arg3 = nothing; arg4 = nothing
5871

5972
cmd, = parse_common_opts(d, "", [:R :V_params :b :d :e :f :g :h :i :o :yx])
@@ -88,6 +101,7 @@ function gmtspatial(cmd0::String="", arg1=nothing, arg2 = nothing; kwargs...)
88101
D.data = D.data[ind, :]
89102
!isempty(D.text) && (D.text = D.text[ind])
90103
end
104+
91105
if contains(cmd, "-N+i")
92106
D.colnames = ["x","y","polID"]
93107
sz = hasID ? 0 : size(D,1)
@@ -105,4 +119,4 @@ function gmtspatial(cmd0::String="", arg1=nothing, arg2 = nothing; kwargs...)
105119
end
106120

107121
# ---------------------------------------------------------------------------------------------------
108-
gmtspatial(arg1, arg2=nothing; kw...) = gmtspatial("", arg1, arg2; kw...)
122+
#gmtspatial(arg1, arg2=nothing; kw...) = gmtspatial("", arg1, arg2; kw...)

src/laszip/lazread.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Base.@kwdef struct lasout_types
22
stored::String = ""
33
grd::GMTgrid = GMTgrid()
44
ds::GMTdataset = GMTdataset()
5-
dsv::Vector{GMTdataset{Float64}} = [GMTdataset()]
5+
dsv::Vector{GMTdataset{Float64,2}} = [GMTdataset()]
66
function lasout_types(stored, grd, ds, dsv)
77
stored = !isempty(grd) ? "grd" : (!isempty(ds) ? "ds" : (!isempty(dsv) ? "dsv" : ""))
88
new(stored, grd, ds, dsv)

src/psscale.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ function colorbar(arg1::Union{Nothing, GMTcpt}=nothing; first=true, kwargs...)
5454

5555
parse_paper(d) # See if user asked to temporarily pass into paper mode coordinates
5656

57+
isBnone = (get(d, :B, nothing) == :none)
5758
cmd = parse_BJR(d, "", "", O, "")[1]
58-
opt_B = (!contains(cmd, " -B") && !IamModern[1]) ? DEF_FIG_AXES[1] : ""
59+
opt_B = (!contains(cmd, " -B") && !IamModern[1] && !isBnone) ? DEF_FIG_AXES[1] : ""
5960
cmd = parse_JZ(d, cmd; O=O, is3D=(CTRL.pocket_J[3] != ""))[1] # We can't use parse_J(d)[1]
6061
cmd = parse_common_opts(d, cmd, [:F :UVXY :params :margin :c :p :t]; first=first)[1]
6162
cmd = parse_these_opts(cmd, d, [[:G :truncate], [:I :shade], [:M :monochrome], [:N :dpi],

src/psxy.jl

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool,
156156
opt_W::String = add_opt_pen(d, [:W :pen], opt="W")
157157
arg1, opt_W, got_color_line_grad, made_it_vector = helper_psxy_line(d, cmd, opt_W, is3D, arg1, arg2, arg3)
158158

159-
arg1, cmd = check_ribbon(d, arg1, cmd, opt_W) # Do this check here, after -W is known and before parsing -G & -L
159+
isa(arg1, GDtype) && (arg1, cmd = check_ribbon(d, arg1, cmd, opt_W)) # Do this check here, after -W is known and before parsing -G & -L
160160

161161
mcc, bar_ok = false, (sub_module == "bar" && !check_bar_group(arg1))
162162
if (!got_color_line_grad && !is_gridtri && (arg1 !== nothing && !isa(arg1, GMTcpt)) && ((!got_Zvars && !is_ternary) || bar_ok))
@@ -259,7 +259,7 @@ end
259259

260260
# ---------------------------------------------------------------------------------------------------
261261
# If the input is a GMTdataset and one of its columns is a Time column, automatically set the -fT
262-
function set_fT(D::GMTdataset, cmd::String, opt_f::String)
262+
function set_fT(D::GMTdataset{T,N}, cmd::String, opt_f::String)::String where {T,N}
263263
if ((Tc = get(D.attrib, "Timecol", "")) != "")
264264
tc::Int = parse(Int, Tc) - 1
265265
_opt_f = (opt_f == "") ? " -f$(tc)T" : opt_f * ",$(tc)T"
@@ -284,7 +284,7 @@ function parse_grid2tri_case(d, cmd, caller, is3D, isFV, O, arg1)
284284
(opt_p == "" && !is3D && !O) && (CURRENT_VIEW[1] = "") # Make sure it empty under these conditions
285285
(opt_p == "") ? (opt_p = CURRENT_VIEW[1]; cmd *= opt_p) : (CURRENT_VIEW[1] = opt_p) # Save for eventual use in other modules.
286286

287-
if (is3D && isFV) # case of 3D faces
287+
if (is3D && isFV) # case of 3D faces
288288
arg1 = (is_in_dict(d, [:replicate]) !== nothing) ? replicant(arg1, d) : deal_faceverts(arg1, d; del=find_in_dict(d, [:nocull])[1] === nothing)
289289
(!O && !haskey(d, :aspect3) && is_in_dict(d, [:JZ :Jz :zsize :zscale]) === nothing && !isgeog(arg1)) && (d[:aspect3] = "equal")
290290
(!O && !haskey(d, :aspect3) && isgeog(arg1)) && (d[:aspect] = "equal")
@@ -296,12 +296,12 @@ function parse_grid2tri_case(d, cmd, caller, is3D, isFV, O, arg1)
296296
end
297297

298298
# ---------------------------------------------------------------------------------------------------
299-
function parse_Ebars(d, cmd, arg1)
299+
function parse_Ebars(d::Dict{Symbol, Any}, cmd::String, arg1)
300300
got_Ebars = false
301301
val, symb = find_in_dict(d, [:E :error :errorbars :error_bars], false)
302302
if (val !== nothing)
303303
if isa(val, String)
304-
cmd *= " -E" * val
304+
cmd::String *= " -E" * val
305305
else
306306
_mat = (arg1 === nothing) ? arg1 : isa(arg1, GMTdataset) ? arg1.data : arg1[1].data
307307
cmd, mat_t = add_opt(add_opt, (d, cmd, "E", [symb]),
@@ -316,7 +316,7 @@ function parse_Ebars(d, cmd, arg1)
316316
end
317317

318318
# ---------------------------------------------------------------------------------------------------
319-
function parse_color_request(d, cmd, N_args, arg1, arg2)
319+
function parse_color_request(d::Dict{Symbol, Any}, cmd::String, N_args::Int, arg1, arg2)
320320
opt_Z, args, n, got_Zvars = add_opt(d, "", "Z", [:Z :level :levels], :data, Any[arg1, arg2], (outline="_o", nofill="_f"))
321321
(opt_Z == " -Z" && n == 0) && error("The 'level' option (Z) must be set a single value, a file name or a vector os reals.")
322322
if (isa(arg1, Vector{<:GMTdataset}) && ((ind_att = findfirst('=', opt_Z)) !== nothing))
@@ -353,7 +353,7 @@ function set_avatar_defaults(d, cmd, mcc, caller, got_usr_R, opt_B, opt_Gsymb, o
353353
end
354354

355355
# ---------------------------------------------------------------------------------------------------
356-
function parse_plot_G_L(d, cmd, g_bar_fill, is_ternary)
356+
function parse_plot_G_L(d::Dict{Symbol, Any}, cmd::String, g_bar_fill::Vector{String}, is_ternary::Bool)
357357
opt_G::String = ""
358358
if (isempty(g_bar_fill)) # Otherwise bar fill colors are dealt with somewhere else
359359
((opt_G = add_opt_fill("", d, [:G :fill], 'G')) != "") && (cmd *= opt_G) # Also keep track if -G was set
@@ -374,7 +374,7 @@ function parse_plot_G_L(d, cmd, g_bar_fill, is_ternary)
374374
end
375375

376376
# ---------------------------------------------------------------------------------------------------
377-
function parse_plot_callers(d, gmt_proggy, caller, is3D, O, arg1)
377+
function parse_plot_callers(d::Dict{Symbol, Any}, gmt_proggy::String, caller::String, is3D::Bool, O::Bool, arg1)
378378
isFV = (isa(arg1, GMTfv) || isa(arg1, Vector{GMTfv}))
379379
(arg1 !== nothing && !isa(arg1, GDtype) && !isa(arg1, Matrix{<:Real}) && !isFV) &&
380380
(arg1 = tabletypes2ds(arg1, ((val = find_in_dict(d, [:interp])[1]) !== nothing) ? interp=val : interp=0))
@@ -407,9 +407,9 @@ function parse_plot_callers(d, gmt_proggy, caller, is3D, O, arg1)
407407
end
408408

409409
# ---------------------------------------------------------------------------------------------------
410-
plt_txt_attrib!(D::GMTdataset, d::Dict, _cmd::Vector{String}) = plt_txt_attrib!([D], d, _cmd)
411-
function plt_txt_attrib!(D::Vector{<:GMTdataset}, d::Dict, _cmd::Vector{String})
412-
# Plot TEXT attributed labels and serve as function barrier agains to f Anys
410+
plt_txt_attrib!(D::GMTdataset{T,N}, d::Dict{Symbol, Any}, _cmd::Vector{String}) where {T,N} = plt_txt_attrib!([D], d, _cmd)
411+
function plt_txt_attrib!(D::Vector{<:GMTdataset{T,N}}, d::Dict{Symbol, Any}, _cmd::Vector{String}) where {T,N}
412+
# Plot TEXT attributed labels and serve as function barrier agains the f Any's (not sure if succeeds)
413413
((val = find_in_dict(d, [:labels])[1]) === nothing) && return nothing
414414

415415
s_val::String = string(val)
@@ -422,13 +422,13 @@ function plt_txt_attrib!(D::Vector{<:GMTdataset}, d::Dict, _cmd::Vector{String})
422422
if ((fnt = add_opt(d, "", "", [:font], (angle="+a", font=("+f", font)); del=false)) != "")
423423
(fnt[1] != '+') && (fnt = "+f" * fnt)
424424
delete!(d, :font)
425-
ct.text = make_attrtbl(D, att=ts)[1]
425+
ct.text = vec(make_attrtbl(D, att=ts)[1])
426426
else
427427
nc::Int = round(Int, sqrt(length(D))) # A crude guess of the number of columns
428428
fnt = (nc < 5) ? "7p" : (nc < 9 ? "5p" : "4p") # A simple heuristic
429429
outline = fnt * ",black=~0.75p,white " # Apply the outline trick
430430
fnt = "+f"
431-
ct.text = outline .* make_attrtbl(D, att=ts)[1]
431+
ct.text = outline .* vec(make_attrtbl(D, att=ts)[1])
432432
end
433433
(CTRL.pocket_call[1] === nothing) ? (CTRL.pocket_call[1] = ct) : (CTRL.pocket_call[2] = ct)
434434
append!(_cmd, ["pstext -R -J -F" * fnt * "+jMC"])
@@ -510,7 +510,7 @@ function if_multicols(d, arg1, is3D::Bool)
510510
MULTI_COL[1] && (d2[:multi] = true) # MULTI_COL was set in cat_2_arg2() when 2nd arg had 2 or more cols.
511511
arg1 = ds2ds(arg1; is3D=is3D, d2...) # Pass a 'd' copy and remove possible kw that are also parsed in psxy
512512
delete!(d, [[:multi, :multicol, :multicols], [:lt, :linethick], [:ls, :linestyle], [:fill], [:fillalpha], [:color]])
513-
MULTI_COL[1] = false # If it was true, its jobe is done.
513+
MULTI_COL[1] = false # If it was true, its jobe is done.
514514
return arg1
515515
end
516516

@@ -545,6 +545,7 @@ function check_grouping!(d, arg1)
545545
end
546546

547547
# ---------------------------------------------------------------------------------------------------
548+
#=
548549
function check_ribbon(d, arg1, cmd::String, opt_W::String)
549550
((val = find_in_dict(d, [:ribbon :band])[1]) === nothing) && return arg1, cmd
550551
add_2 = true
@@ -561,6 +562,7 @@ function check_ribbon(d, arg1, cmd::String, opt_W::String)
561562
else
562563
error("Wrong data type for ribbon/band $(typeof(val))")
563564
end
565+
ec1, ec2, add_2 = helper_check_ribbon(val)
564566
if (isa(arg1, Vector{<:GMTdataset}))
565567
if (add_2)
566568
for k = 1:numel(arg1) add2ds!(arg1[k], ec2; names=["Zbnd1","Zbnd2"]) end
@@ -574,6 +576,49 @@ function check_ribbon(d, arg1, cmd::String, opt_W::String)
574576
(!occursin(cmd, "-W") && opt_W == "") && (cmd *= " -W0.5p") # Do not leave without a line specification
575577
return arg1, cmd
576578
end
579+
=#
580+
581+
function check_ribbon(d, arg1::GMTdataset{T,N}, cmd::String, opt_W::String) where {T,N}
582+
((val = find_in_dict(d, [:ribbon :band])[1]) === nothing) && return arg1, cmd
583+
ec1, ec2, add_2 = helper_check_ribbon(val) # Function barrier agains Anys
584+
(add_2) ? add2ds!(arg1, ec2; names=["Zbnd1","Zbnd2"]) : add2ds!(arg1, ec1; name="Zbnd")
585+
d[:L] = (add_2) ? "+D" : "+d"
586+
(!occursin(cmd, "-W") && opt_W == "") && (cmd *= " -W0.5p") # Do not leave without a line specification
587+
return arg1, cmd
588+
end
589+
590+
function check_ribbon(d, arg1::Vector{<:GMTdataset{T,N}}, cmd::String, opt_W::String) where {T,N}
591+
((val = find_in_dict(d, [:ribbon :band])[1]) === nothing) && return arg1, cmd
592+
ec1, ec2, add_2 = helper_check_ribbon(val) # Function barrier agains Anys
593+
if (add_2)
594+
for k = 1:numel(arg1) add2ds!(arg1[k], ec2; names=["Zbnd1","Zbnd2"]) end
595+
else
596+
for k = 1:numel(arg1) add2ds!(arg1[k], ec1; name="Zbnd") end
597+
end
598+
d[:L] = (add_2) ? "+D" : "+d"
599+
(!occursin(cmd, "-W") && opt_W == "") && (cmd *= " -W0.5p") # Do not leave without a line specification
600+
return arg1, cmd
601+
end
602+
603+
function helper_check_ribbon(val)::Tuple{Vector{Float64}, Matrix{Float64}, Bool}
604+
# Isolate here the fact that 'val' is a Any
605+
add_2 = true
606+
ec1, ec2 = Float64[], Matrix{Float64}[]
607+
if isa(val, Real)
608+
ec1 = repeat([float(val)::Float64], size(arg1,1)::Int)
609+
add_2 = false
610+
elseif (isa(val, VecOrMat{<:Real}) || isa(val, Tuple{<:Real, <:Real}))
611+
(length(val)::Int == 2) ? (ec2 = repeat([float(val[1])::Float64 float(val[2])::Float64], size(arg1,1)::Int)) :
612+
ec2 = [Float64.(vec(val)) Float64.(vec(val))]
613+
elseif isa(val, Tuple{Vector{<:Real}, Vector{<:Real}})
614+
ec2 = [Float64.(val[1]) Float64.(val[2])]
615+
elseif (isa(val, Matrix{<:Real}) && size(val,2) == 2)
616+
ec2 = val
617+
else
618+
error("Wrong data type for ribbon/band $(typeof(val))")
619+
end
620+
ec1, ec2, add_2
621+
end
577622

578623
# ---------------------------------------------------------------------------------------------------
579624
function with_xyvar(d::Dict, arg1::GMTdataset, no_x::Bool=false)::Union{GMTdataset, Vector{<:GMTdataset}}

src/utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ function delrows(D::GMTdataset; rows::Vector{<:Integer}=Int[], nodata=nothing, c
628628
return size(mat,1) != size(D,1) ? mat2ds(mat, D) : D # If nothing found, just return the original dataset
629629
end
630630
function delrows(D::Vector{<:GMTdataset}; rows::Vector{<:Integer}=Int[], nodata=nothing, col=0)
631-
Dv = Vector{GMTdataset{typeof(D[1])}}(undef, length(D))
631+
Dv = Vector{GMTdataset{typeof(D[1]),2}}(undef, length(D))
632632
for k = 1:length(D)
633633
Dv[k] = delrows(D[k], rows=rows, nodata=nodata, col=col)
634634
end
@@ -1011,7 +1011,7 @@ Returns the shorter vector `y` and indices of picked points in `ind`
10111011
10121012
See https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf
10131013
"""
1014-
function lttb(D::GMTdataset, decfactor::Int=10)::GMTdataset
1014+
function lttb(D::GMTdataset{T,2}, decfactor::Int=10)::GMTdataset{T,2} where T
10151015
y, ind = lttb(view(D.data, :, 2), decfactor)
10161016
(size(D,2) == 2) ? mat2ds([D.data[ind,1] y], ref=D) : mat2ds([D.data[ind,1] y D.data[ind,3:end]], ref=D)
10171017
end

0 commit comments

Comments
 (0)