Skip to content

Commit

Permalink
Merge pull request #1330 from GenericMappingTools/improve-hasnans
Browse files Browse the repository at this point in the history
Fix the description/usage(future) of 'hasnans'
  • Loading branch information
joa-quim authored Dec 19, 2023
2 parents 963f474 + e23b613 commit 40e6f71
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/gmt_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ function get_grid(API::Ptr{Nothing}, object, cube::Bool)::GMTgrid

# Return grids via a float matrix in a struct
rng, inc = (gmt_hdr.n_bands > 1) ? (fill(NaN,8), fill(NaN,3)) : (fill(NaN,6), fill(NaN,2))
out = GMTgrid("", "", 0, -1, rng, inc, 0, NaN, "", "", "", "", String[], X, Y, V, z, "", "", "", "", layout, 1f0, 0f0, 0, 0)
hasnans = any(!isfinite, z) ? 2 : 1
out = GMTgrid("", "", 0, -1, rng, inc, 0, NaN, "", "", "", "", String[], X, Y, V, z, "", "", "", "", layout, 1f0, 0f0, 0, hasnans)

if (gmt_hdr.ProjRefPROJ4 != C_NULL) out.proj4 = unsafe_string(gmt_hdr.ProjRefPROJ4) end
if (gmt_hdr.ProjRefWKT != C_NULL) out.wkt = unsafe_string(gmt_hdr.ProjRefWKT) end
Expand Down
2 changes: 1 addition & 1 deletion src/gmt_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The fields of this struct are:
- `scale::Union{Float64, Float32}=1f0`: When saving in file apply `z = z * scale + offset`
- `offset::Union{Float64, Float32}=0f0`
- `pad::Int=0`: When != 0 means that the array is placed in a padded array of PAD rows/cols
- `hasnans::Int=2`: 2 -> "don't know"; 1 -> confirmed, "have NaNs"; 0 -> confirmed, "have no NaNs"
- `hasnans::Int=0`: 0 -> "don't know"; 1 -> confirmed, "have no NaNs"; 2 -> confirmed, "have NaNs"
"""
Base.@kwdef mutable struct GMTgrid{T<:Real,N} <: AbstractArray{T,N}
proj4::String
Expand Down
2 changes: 1 addition & 1 deletion src/gmtreadwrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function gmtread(_fname::String; kwargs...)
(opt_bi == "" && isa(o, GDtype)) && file_has_time!(fname, o, corder)

if (isa(o, GMTgrid))
o.hasnans = any(!isfinite, o) ? 2 : 1
o.hasnans = any(!isfinite, o.z) ? 2 : 1
# Check if we should assign a default CPT to this grid
((fname[1] == '@') && (cptname = check_remote_cpt(fname)) != "") && (o.cpt = cptname)
end
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function peaks(; N=49, grid::Bool=true, pixreg::Bool=false)
z = Float32.(z)
reg = (pixreg) ? 1 : 0
G = GMTgrid("", "", 0, 0, [_x[1], _x[end], _y[1], _y[end], minimum(z), maximum(z)], [inc, inc],
reg, NaN, "", "", "", "", String[], _x, _y, Vector{Float64}(), z, "x", "y", "", "z", "BCB", 1f0, 0f0, 0, 0)
reg, NaN, "", "", "", "", String[], _x, _y, Vector{Float64}(), z, "x", "y", "", "z", "BCB", 1f0, 0f0, 0, 2)
return G
else
return x,y,z
Expand Down
6 changes: 3 additions & 3 deletions src/utils_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ function mat2img(mat::Union{GMTgrid,Matrix{<:AbstractFloat}}; x=Float64[], y=Flo
proj4::String="", wkt::String="", GI::Union{GItype,Nothing}=nothing, clim=[0,255], cmap=nothing, kw...)
# This is the same as Matlab's imagesc() ... plus some extras.
mi, ma = (isa(mat,GMTgrid)) ? mat.range[5:6] : extrema(mat)
(isa(mat,GMTgrid) && mat.hasnans > 1) && (mi = NaN) # Don't know yet so force checking
(isa(mat,GMTgrid) && mat.hasnans == 2) && (mi = NaN) # Don't know yet so force checking
if (isnan(mi)) # Shit, such a memory waste we need to do.
mi, ma = extrema_nan(mat)
t = isa(mat, GMTgrid) ? Float32.((mat.z .- mi) ./ (ma - mi) .* 255) : Float32.((mat .- mi) ./ (ma - mi) .* 255)
Expand Down Expand Up @@ -1661,8 +1661,8 @@ istransposed(mat) = !isempty(fields(mat)) && (fields(mat)[1] == :parent)

mat2grid(mat, xx, yy, zz=Float64[];
reg=nothing, hdr=Float64[], proj4::String="", proj::String="", wkt::String="", epsg::Int=0, geog::Int=-1, title::String="", tit::String="", rem::String="", cmd::String="", names::Vector{String}=String[], scale::Real=1f0, offset::Real=0f0, layout::String="", is_transposed::Bool=false, z_units::String="") =
mat2grid(mat; x=xx, y=yy, v=zz, reg=reg, hdr=hdr, proj4=proj4, proj=proj, wkt=wkt, epsg=epsg, geog=geog,
title=title, tit=tit, rem=rem, cmd=cmd, names=names, scale=scale, offset=offset, layout=layout, is_transposed=is_transposed, z_units=z_units)
mat2grid(mat; x=xx, y=yy, v=zz, reg=reg, hdr=hdr, proj4=proj4, proj=proj, wkt=wkt, epsg=epsg, geog=geog,
title=title, tit=tit, rem=rem, cmd=cmd, names=names, scale=scale, offset=offset, layout=layout, is_transposed=is_transposed, z_units=z_units)

#function mat2grid(mat, xx=Float64[], yy=Float64[], zz=Float64[]; reg=nothing,
function mat2grid(mat; reg=nothing, x=Float64[], y=Float64[], v=Float64[], hdr=Float64[], proj4::String="",
Expand Down
10 changes: 5 additions & 5 deletions test/test_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@
GMT.color_gradient_line(mat2ds(rand(3,2)));
GMT.color_gradient_line([mat2ds(rand(3,2)), mat2ds(rand(4,2))]);
@info "line2multiseg"
GMT.line2multiseg(mat2ds(rand(5,2)), lt=[1,2], auto_color=true);
GMT.line2multiseg(mat2ds(rand(5,2)), lt=[1,2,4], auto_color=true);
GMT.line2multiseg(mat2ds(rand(3,2)), lt=[1,2,4]);
GMT.line2multiseg([mat2ds(rand(3,2)), mat2ds(rand(4,2))], lt=[1,2], auto_color=true);
GMT.resetGMT()
#GMT.line2multiseg(mat2ds(rand(5,2)), lt=[1,2], auto_color=true);
#GMT.line2multiseg(mat2ds(rand(5,2)), lt=[1,2,4], auto_color=true);
#GMT.line2multiseg(mat2ds(rand(3,2)), lt=[1,2,4]);
#GMT.line2multiseg([mat2ds(rand(3,2)), mat2ds(rand(4,2))], lt=[1,2], auto_color=true);
#GMT.resetGMT()

@info "mat2grid"
GMT.mat2grid(rand(Float32, 10,10), reg=1);
Expand Down

0 comments on commit 40e6f71

Please sign in to comment.