Skip to content

Commit

Permalink
fix issue #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed May 28, 2024
1 parent 6ac9dcb commit 529bbe3
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/land_sea_mask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@ is_land(mask::LandSeaMask, lat::Real, lon::Real) = nearest_point(mask, lat, lon)
is_land(mask::LandSeaMask, (lat, lon)::Tuple{Real, Real}) = is_land(mask, lat, lon)

function nearest_point(mask::LandSeaMask, lat::Real, lon::Real)
return mask.data[nearest_point_idx(mask.lon, lon), nearest_point_idx(mask.lat, lat)]
i = nearest_point_idx_periodic(mask.lon, lon)
j = nearest_point_idx(mask.lat, lat)
return mask.data[i,j]
end

function nearest_point_idx(xs::StepRangeLen, x::Real)
return convert(Int, floor((x - first(xs)) / (last(xs) - first(xs)) * length(xs)))
function nearest_point_idx(xs::AbstractRange, x::Real)
idx = 1 + round(Int,(x - first(xs)) / step(xs))
return idx
end

function nearest_point_idx_periodic(xs::AbstractRange, x::Real)
idx = nearest_point_idx(xs, x)
return mod1(idx,length(xs))
end

0 comments on commit 529bbe3

Please sign in to comment.