Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Oct 24, 2024
1 parent d7caa88 commit df2c95e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/uniquecoords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ UniqueCoords(pairs::Pair{C,<:Function}...) where {C<:Column} =

isrevertible(::Type{<:UniqueCoords}) = false

apply(transform::UniqueCoords, geotable::AbstractGeoTable) = _unique(typeof(domain(geotable)), transform, geotable)
apply(transform::UniqueCoords, geotable::AbstractGeoTable) = _unique(transform, domain(geotable), values(geotable)), nothing

function _unique(::Type{<:Domain}, transform::UniqueCoords, geotable::AbstractGeoTable)
gtb = _adjustunits(geotable)
dom = domain(gtb)
tab = values(gtb)
function _unique(transform::UniqueCoords, domain::Domain, table)
dom = domain
tab = _adjustunits(table)
cols = Tables.columns(tab)
vars = Tables.columnnames(cols)

Expand Down Expand Up @@ -78,12 +77,10 @@ function _unique(::Type{<:Domain}, transform::UniqueCoords, geotable::AbstractGe
newdom = view(dom, ginds)

# new spatial data
newgtb = georef(newtab, newdom)

newgtb, nothing
georef(newtab, newdom)
end

_unique(::Type{<:Grid}, ::UniqueCoords, geotable::AbstractGeoTable) = geotable, nothing
_unique(::UniqueCoords, domain::Grid, table) = georef(table, domain)

# ---------------------------------------------------------------
# The code below was copied/modified provisorily from Base.unique
Expand Down
12 changes: 5 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ const Len{T} = Quantity{T,u"𝐋"}
_addunit(x::Number, u) = x * u
_addunit(::Quantity, _) = throw(ArgumentError("invalid units, please check the documentation"))

function _adjustunits(geotable::AbstractGeoTable)
dom = domain(geotable)
tab = values(geotable)
cols = Tables.columns(tab)
_adjustunits(geotable::AbstractGeoTable) = georef(_adjustunits(values(geotable)), domain(geotable))

function _adjustunits(table)
cols = Tables.columns(table)
vars = Tables.columnnames(cols)

pairs = (var => _absunit(Tables.getcolumn(cols, var)) for var in vars)
newtab = (; pairs...) |> Tables.materializer(tab)

georef(newtab, dom)
(; pairs...) |> Tables.materializer(table)
end

_absunit(x) = _absunit(nonmissingtype(eltype(x)), x)
Expand Down
3 changes: 2 additions & 1 deletion test/uniquecoords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,6 @@
grid = CartesianGrid(10, 10)
sdata = georef((; Z=rand(100)), grid)
ndata = sdata |> UniqueCoords()
@test sdata === ndata
@test domain(ndata) === domain(sdata)
@test values(ndata) === values(sdata)
end

0 comments on commit df2c95e

Please sign in to comment.