Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/GMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ export

add2PSfile, append2fig, isoutlier, linearfitxy, regiongeog, streamlines, peaks, polygonlevels, randinpolygon, polyfit, polyval,

ablines, ablines!, biplot, biplot!, density, density!, boxplot, boxplot!, cornerplot, cornerplot!, cubeplot, cubeplot!, ecdfplot, ecdfplot!,
fill_between, fill_between!, marginalhist, marginalhist!, parallelplot, parallelplot!, piechart, piechart!, plotlinefit, plotlinefit!,
qqplot, qqplot!, qqnorm, qqnorm!, remotegrid, sealand, squeeze, terramar, violin, violin!, viz, vizpdf, windbarbs, whereami,
ablines, ablines!, biplot, biplot!, density, density!, boxplot, boxplot!, cornerplot, cornerplot!, cubeplot, cubeplot!,
ecdfplot, ecdfplot!, fill_between, fill_between!, funcurve, marginalhist, marginalhist!, parallelplot, parallelplot!,
piechart, piechart!, plotlinefit, plotlinefit!, qqplot, qqplot!, qqnorm, qqnorm!, remotegrid, sealand, squeeze, terramar,
violin, violin!, viz, vizpdf, windbarbs, whereami,

maregrams, pastplates, seismicity, ecmwf, era5time, era5vars, listecmwfvars, meteostat, weather, wmsinfo, wmstest, wmsread,

Expand Down
4 changes: 2 additions & 2 deletions src/extras/anaglyph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function anaglyph(G::GMTgrid; vscale=1, sscale=2, view3d=false, zsize=4, azim=19
m_scale = -vscale / 50 # Amp factor

function gra(G, scale)
azim,elev = 0.0, 30.0
s = [sind(azim) * cosd(elev); cosd(azim) * cosd(elev); sind(elev)]
_azim,elev = 0.0, 30.0
s = [sind(_azim) * cosd(elev); cosd(_azim) * cosd(elev); sind(elev)]
data = zeros(Float32, size(G))
@inbounds Threads.@threads for c = 2:size(G,1)-1
@inbounds for r = 2:size(G,2)-1
Expand Down
2 changes: 1 addition & 1 deletion src/extras/weather.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function ecmwf_fc(; filename="", levlist="", kw...)
function which_name(d::Dict, thisvar::String, name::String, filename::AbstractString, EXT::String)::String
# Select which name to use for saving the file. NAME is the composed (long) name. The others are user choices.
(filename != "") && return filename # This one takes priority
fname_prefix::String = ((val = find_in_dict(d, [:prefix], false)[1]) !== nothing) ? string(val)::String * "_$(thisvar)$(EXT)" : ""
fname_prefix::String = ((_val = find_in_dict(d, [:prefix], false)[1]) !== nothing) ? string(_val)::String * "_$(thisvar)$(EXT)" : ""
return (fname_prefix != "") ? fname_prefix : name # 'fname_prefix' takes priority if exists.
end

Expand Down
4 changes: 3 additions & 1 deletion src/filter1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ function filter1d_helper(cmd0::String, arg1; kwargs...)
r = common_grd(d, cmd0, cmd, "filter1d ", arg1) # Finish build cmd and run it
if (isa(arg1, GDtype)) # Keep the attribs if they existed in arg1
isa(arg1, GMTdataset) && !isempty(arg1.attrib) && (r.attrib = arg1.attrib)
isa(arg1, Vector) && !isempty(arg1[1].attrib) && [r[k].attrib = arg1[1].attrib for k = 1:length(arg1)]
if (isa(arg1, Vector) && !isempty(arg1[1].attrib))
for k = 1:length(arg1) r[k].attrib = arg1[1].attrib end
end
end
return r
end
6 changes: 3 additions & 3 deletions src/gadm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ function gadm(country, subregions...; children::Bool=false, names::Bool=false, c

function _getlayer(plevel)
# Get layer of the desired `level` from the `data`.
nlayers = Gdal.nlayer(data)
for l = 0:nlayers - 1
_nlayers = Gdal.nlayer(data)
for l = 0:_nlayers - 1
layer = Gdal.getlayer(data, l)
lname = Gdal.getname(layer)
llevel = last(split(lname, "_"))
string(plevel) == llevel && return layer, lname
end
error("Asked data for a level ($(plevel+1)) that is lower than lowest data level ($(nlayers))")
error("Asked data for a level ($(plevel+1)) that is lower than lowest data level ($(_nlayers))")
end

function _get_attrib(feature)
Expand Down
4 changes: 2 additions & 2 deletions src/linefit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ function _plotlinefit(D::GMTdataset, first::Bool, grp::Bool, d::Dict{Symbol,Any}

function do_ribs(d, symbs, def_color::String)
do_rib, rib_cor = false, ""
if ((val = find_in_dict(d, symbs)[1]) !== nothing) # Either 'true' or ribbon color
rib_cor = (val == 1) ? def_color : string(val)::String
if ((_val = find_in_dict(d, symbs)[1]) !== nothing) # Either 'true' or ribbon color
rib_cor = (_val == 1) ? def_color : string(_val)::String
do_rib = (rib_cor != "")
end
return do_rib, rib_cor
Expand Down
9 changes: 7 additions & 2 deletions src/maregrams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ function maregrams(; list=false, code="", name="", days=2, starttime::String="",
(n_lines <= 0) && (println("This station has no data or a file tranfer error occured."); return GMTdataset())

mat = Matrix{Float64}(undef, n_lines, 2)
n = -2
var_in_mareg = ["prs(m)"]
open(file, "r") do io
n = -2
local ind
for line in eachline(io)
if ((n += 1) <= 0) # To jump the first two (header) lines
(n == -1) && continue # Header line with station name
Expand Down Expand Up @@ -123,14 +124,18 @@ function read_maregrams(fname=TESTSDIR * "/assets/maregs_online.csv")
names = Vector{String}(undef, 378)
codes = Vector{String}(undef, 378)
countries = Vector{String}(undef, 378)
n = -1
#fid = open(fname)
#iter = eachline(fid)
#for line in iter
open(fname, "r") do io
n = -1
for line in eachline(io)
x, y, name, code, country = split(line, ",")
((n += 1) == 0) && continue # The header line
mat[n, 1], mat[n, 2] = parse(Float64, x), parse(Float64, y)
names[n], codes[n], countries[n] = name, code, country
end
end
#close(fid)
Dict(:pos => mat, :name => names, :code => codes, :country => countries)
end
8 changes: 4 additions & 4 deletions src/potential/gravprisms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ function gravprisms(cmd0::String="", arg1::GDtype=GMTdataset(); kwargs...)
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode

function fish_grids(d, Gs, cmd, symbs, opt, N)
if ((val = find_in_dict(d, symbs)[1]) !== nothing)
if (isa(val, Real) || isa(val, String))
cmd *= string(" -", opt, val)::String
if ((_val = find_in_dict(d, symbs)[1]) !== nothing)
if (isa(_val, Real) || isa(_val, String))
cmd *= string(" -", opt, _val)::String
else
isa(val, GMTgrid) ? (Gs[N+=1] = val; cmd *= " -D") : error("Invalid type $(typeof(val)) for option $opt")
isa(_val, GMTgrid) ? (Gs[N+=1] = _val; cmd *= " -D") : error("Invalid type $(typeof(_val)) for option $opt")
end
end
return cmd, N
Expand Down
12 changes: 8 additions & 4 deletions src/proj_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ function invgeod(lonlat1::Matrix{<:Real}, lonlat2::Matrix{<:Real}; proj::String=
az2 += 180; (az2 > 180) && (az2 -= 360)
else
az1 .+= 180; az2 .+= 180
[(az1[k] > 180) && (az1[k] -= 360) for k = 1:lastindex(az1)]
[(az2[k] > 180) && (az2[k] -= 360) for k = 1:lastindex(az2)]
@inbounds for k = 1:lastindex(az1)
(az1[k] > 180) && (az1[k] -= 360)
end
@inbounds for k = 1:lastindex(az2)
(az2[k] > 180) && (az2[k] -= 360)
end
end
end
return size(d,1) == 1 ? (d[1], az1[1], az2[1]) : (d, az1, az2)
Expand Down Expand Up @@ -493,8 +497,8 @@ function geodesic_long(lonlat1::VMr, lonlat2::VMr; step=0.0, np=180, proj::Strin
(step != 0) && (np = round(Int, dtot / step) + 1) # Takes precedence over 'np' because that one has a default value.

function get_mindist(az)
D = geod(lonlat1, az, linspace(0,dtot,360), proj=proj, epsg=epsg)[1]
gmtwrite(_name, D);
_D = geod(lonlat1, az, linspace(0,dtot,360), proj=proj, epsg=epsg)[1]
gmtwrite(_name, _D);
gmt("mapproject -L$_name", [lonlat2[1] lonlat2[2]]) # Find closest point to B in the passing by geodesic.
end

Expand Down
2 changes: 1 addition & 1 deletion src/psxy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ function parse_opt_S(d::Dict, arg1, is3D::Bool=false)
elseif (isa(val, Tuple) && isa(val[1], Function) && isa(val[2], VMr)) # ~useless size=(fun, [2,20]) but no col size
val2::Tuple = val
sc = _scale(eltype(val2[2]) <: Integer)
arg1 = hcat(arg1, funcurve(val2[1], vec(Float64.(val2[2] .* sc)), size(arg1,1)))
arg1 = hcat(arg1, funcurve(val2[1], vec(Float64.(val2[2] .* sc)), size(arg1,1))[2])
end
elseif (string(val)::String != "indata") # WTF is "indata"?
marca *= arg2str(val)::String
Expand Down
16 changes: 12 additions & 4 deletions src/spatial_funs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,10 @@ function spatialjoin(D1::GMTdataset, D2::Vector{<:GMTdataset}; pred::Function=in
atts_vec = Vector{Vector{String}}(undef, length(att_names))

function fill_atts_vec(D2, ind, atts_vec, att_names, isleft=true)
[atts_vec[l] = Vector{String}(undef, length(ind)) for l = 1:length(att_names)]
#[atts_vec[l] = Vector{String}(undef, length(ind)) for l = 1:length(att_names)]
for l = 1:length(att_names)
atts_vec[l] = Vector{String}(undef, length(ind))
end
for k = 1:numel(ind) # Loop over number of points in D1
if (isleft && ind[k] == 0) # Skip if not joined
for l = 1:length(att_names) atts_vec[l][k] = "" end
Expand All @@ -705,16 +708,21 @@ function spatialjoin(D1::GMTdataset, D2::Vector{<:GMTdataset}; pred::Function=in
end

if (kind == :left)
atts_vec = fill_atts_vec(D2, ind, atts_vec, att_names)
[D1.attrib[att_names[l]] = atts_vec[l] for l = 1:numel(att_names)] # Add the new attributes to D1.attrib
#[D1.attrib[att_names[l]] = atts_vec[l] for l = 1:numel(att_names)] # Add the new attributes to D1.attrib
for l = 1:numel(att_names) # Add the new attributes to D1.attrib
D1.attrib[att_names[l]] = atts_vec[l]
end
return D1
else
(kind != :inner) && error("Only :left or :inner joins are supported")
ind_retain = findall(ind .!= 0) # Indices of points to retain
deleteat!(ind, findall(ind .== 0)) # Remove points that are not joined
atts_vec = fill_atts_vec(D2, ind, atts_vec, att_names, false)
D = mat2ds(D1, (ind_retain,:)) # TODO: Make this case a view
[D.attrib[att_names[l]] = atts_vec[l] for l = 1:numel(att_names)] # Add the new attributes to D.attrib
#[D.attrib[att_names[l]] = atts_vec[l] for l = 1:numel(att_names)] # Add the new attributes to D.attrib
for l = 1:numel(att_names) # Add the new attributes to D.attrib
D.attrib[att_names[l]] = atts_vec[l]
end
return D
end
end
Expand Down
49 changes: 29 additions & 20 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,43 @@ getdecimal(x::Number) = x - trunc(Int, x)
""" Return an ierator over data skipping non-finite values"""
skipnan(itr) = Iterators.filter(el->isfinite(el), itr)

square(x) = x ^ 2
pow(b,e) = b ^ e

"""
y = funcurve(f::Function, lims::VMr, n=100) -> Vector{Float64}

Geneate a curve between lims[1] and lims[2] having the form of function 'f'
funcurve(f::Function, lims, n=100)::Tuple{Vector{Float64}, Vector{Float64}}

### Args
- `f`: A function among: `exp`, `log`, `log10`, `exp10`, `sqrt`, `square`
- `lims`: limits of `x` in `y = f(x)`
- `n`: number of points in the generated curve.
Evaluate a function over a range and return the computed points.

### Example
# Arguments
- `f::Function`: The one dimensional function to be evaluated. This can be the named of a built-in function
(_e.g._, `exp`), a custom or an anonymous function.
- `lims`: The limits defining the range over which to evaluate the function.
That is, the limits of `x` in `y = f(x)`. This can be a tuple or a vector with two elements.
- `n::Int`: Number of points to evaluate (default: 100)

# Returns
- `Tuple{Vector{Float64}, Vector{Float64}}`: A tuple containing two vectors of Float64 values
representing the x-coordinates and corresponding function values

# Example
```julia
x = funcurve(exp, [1, 10], 10)
x, y = funcurve(exp, [0, 10])
```

Another example with a custom function (an anonymous function in this case):
```julia
x, y = funcurve(x->x^2, (0, 10))
```

Visualize the result with:
```julia
viz(x,y)
```
"""
function funcurve(f::Function, lims::VMr, n=100)::Vector{Float64}
if (f == exp) x::Vector{Float64} = vec(log.(Float64.(lims)))
elseif (f == log) x = vec(exp.(Float64.(lims)))
elseif (f == log10) x = vec(exp10.(Float64.(lims)))
elseif (f == exp10) x = vec(log10.(Float64.(lims)))
elseif (f == sqrt) x = vec(square.(Float64.(lims)))
elseif (f == square) x = vec(sqrt.(Float64.(lims)))
else error("Function $f not implemented in funcurve().")
end
f.(linspace(x[1], x[2], n))
function funcurve(f::Function, lims, n::Integer=100)::Tuple{Vector{Float64}, Vector{Float64}}
x = collect(linspace(lims[1], lims[2], n))
y = f.(x)
return x, y
end

#=
Expand Down
3 changes: 0 additions & 3 deletions test/test_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,6 @@
add2PSfile("Bla")
add2PSfile(["Bla", "Bla"])

GMT.funcurve(GMT.square, [1 10])
@test_throws ErrorException("Function tan not implemented in funcurve().") GMT.funcurve(tan, [1 10]);

println(" DRAWING")
circle(0,0,1,first=true,Vd=dbg2);
cross(0,0,1,Vd=dbg2);
Expand Down
Loading