Skip to content

Commit

Permalink
fix(layers): update the mask! function to deal with 0
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisot committed Oct 15, 2023
1 parent 3d71644 commit 75c4327
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions SimpleSDMLayers/src/lib/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function cellsize(layer::T; R = 6371.0) where {T <: SimpleSDMLayer}
lonstride, latstride = 2.0 .* stride(layer)
cells_per_ribbon = 360.0 / lonstride
latitudes_ranges = (layer.bottom):latstride:(layer.top)
# We need to express the latitudes in gradients for the top and bottom of each row of
# We need to express the latitudes in radians for the top and bottom of each row of
# cell
ϕ1 = deg2rad.(latitudes_ranges[1:(end - 1)])
ϕ2 = deg2rad.(latitudes_ranges[2:end])
Expand All @@ -87,5 +87,6 @@ function cellsize(layer::T; R = 6371.0) where {T <: SimpleSDMLayer}
surface_grid = reshape(repeat(cell_surface, size(layer, 2)), size(layer))
# And we return based on the actual type of the input
RT = T <: SimpleSDMResponse ? SimpleSDMResponse : SimpleSDMPredictor
return mask(layer, RT(surface_grid; SimpleSDMLayers.boundingbox(layer)...))
surface_layer = RT(surface_grid; SimpleSDMLayers.boundingbox(layer)...)
return mask(layer, surface_layer)
end
9 changes: 6 additions & 3 deletions SimpleSDMLayers/src/operations/mask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ _inner_type(::SimpleSDMPredictor{T}) where {T <: Any} = T
"""
mask!(l1::T1, l2::T2) where {T1 <: SimpleSDMLayer, T2 <: SimpleSDMLayer}
Changes the second layer so that the positions for which the first layer is zero
(of the appropriate type) or `nothing` are set to `nothing`. This is mostly
Changes the second layer so that the positions for which the first layer is `false`
(for Boolean layers) or `nothing` are set to `nothing`. This is mostly
useful in cases where you have a `Bool` layer.
"""
function mask!(l1::T1, l2::T2) where {T1 <: SimpleSDMLayer, T2 <: SimpleSDMLayer}
_itype = _inner_type(l1)
dropfunc = (x) -> isnothing(x) || (x == zero(_itype))
dropfunc = (x) -> isnothing(x)
if _itype == Bool
dropfunc = (x) -> (isnothing(x) || (x == false))
end
todrop = findall(dropfunc, l1.grid)
l2.grid[todrop] .= nothing
end
Expand Down

0 comments on commit 75c4327

Please sign in to comment.