Skip to content

Commit

Permalink
fix accessing Coord(Lat) with inverse order latitudes (#87)
Browse files Browse the repository at this point in the history
* fix accessing Coord(Lat) with inverse order latitudes

* put lat sorting in innermost function

* reversing indices is no longer needed

* fix tests
  • Loading branch information
Datseris committed Jan 28, 2022
1 parent d14283c commit 70199ca
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ClimateBase"
uuid = "35604d93-0fb8-4872-9436-495b01d137e2"
authors = ["Datseris <[email protected]>", "Philippe Roy <[email protected]>"]
version = "0.14.2"
version = "0.14.3"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
5 changes: 3 additions & 2 deletions src/physical_dimensions/spatial_equalarea.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,15 @@ end
# The code here is exclusively a performance optimization that relies
# on the fact that we sort coordinates by latitude
function coord_latitudes_between(c, l1, l2)
l1, l2 = min(l1, l2), max(l1, l2)
idxs, lats = uniquelats(c)
i1 = searchsortedfirst(lats, l1)
i2 = searchsortedlast(lats, l2)
i1 > i2 && ((i1, i2) = (i2, i1)) # in case bounds are given in reverse order
return idxs[i1][1]:idxs[i2][end]
end

# This modifies what happens on A[Coord(Lat(Between(x,y)))]
# Notice that `Between` is deprecated from DimensionalData.jl.
function DimensionalData.selectindices(c::Coord, sel::Tuple{<:Lat{ <: Between}})
l1, l2 = sel[1].val.val
return coord_latitudes_between(gnv(c), l1, l2) # this is Vector{Int}
Expand All @@ -204,6 +205,6 @@ end
# This modifies what happens on A[Coord(Lat(x..y))]
function DimensionalData.selectindices(c::Coord,
sel::Tuple{<:Lat{ <: DimensionalData.LookupArrays.IntervalSets.AbstractInterval}})
l1 = sel[1].val.left; l2 = sel.val.right
l1 = sel[1].val.left; l2 = sel[1].val.right
return coord_latitudes_between(gnv(c), l1, l2) # this is Vector{Int}
end
3 changes: 3 additions & 0 deletions test/space_coord_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
Atran = transform_to_coord(A)
@test hasdim(Atran, Coord)
@test lons == sort!(unique!([c[1] for c in gnv(dims(Atran, Coord))]))

# subsel2 = C[Coord(Lat(8..0))] # DimensionalData.jl is not allowing this to work
# @test subsel2 == subsel
end

@testset "Zonal mean" begin
Expand Down

0 comments on commit 70199ca

Please sign in to comment.