Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for unitful cellarea #799

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

asinghvi17
Copy link
Collaborator

@asinghvi17 asinghvi17 commented Oct 11, 2024

Does what it says on the tin. Unfortunately it looks like Unitful can't convert to Float64 (see PainterQubits/Unitful.jl#742) so going to Proj is where this fails.

It also occurs to me that we could try to promote and rebuild e.g. bignumbers or Unitful quantities when going into reproject. That would allow unitful axes to pass through reproject unchanged. But that's a different PR.

@rafaqz
Copy link
Owner

rafaqz commented Oct 12, 2024

Hmm with units, probably we need to check they are the same units as the crs and otherwise convert. You can Unitful.ustrip to a specific unit, like ustrip(u"m", x)

(We will need a Unitful.jl extension)

I think if the crs units are "US survey foot" and the lookup has meter units we can just error. I guess a dumb first round check is is it in "meters" and then do the ustrip above, or just error. Maybe "kilometers" too. I don't know where to find the spec for the allowed units...

@asinghvi17
Copy link
Collaborator Author

asinghvi17 commented Oct 12, 2024

That sounds like a way bigger effort :D, but doable at some point via Proj. It might be nice for reproject, actually, since we could discover the CRS's units (maybe stash that in lookup metadata) and this would all optimize away at compile-time. Then indexing with arbitrary units could just work TM via Unitful's automatic conversion.

Most of the unit finding work would happen in Proj.

Comment on lines +90 to +94
# The tests below fail because Unitful apparently can't convert to Float64...
# (see https://github.com/PainterQubits/Unitful.jl/issues/742)
# But we also have to re-convert back to the original unit type, which cellarea currently
# doesn't do.

Copy link
Owner

@rafaqz rafaqz Oct 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# The tests below fail because Unitful apparently can't convert to Float64...
# (see https://github.com/PainterQubits/Unitful.jl/issues/742)
# But we also have to re-convert back to the original unit type, which cellarea currently
# doesn't do.
# This needs fixes for unit handling, probably using `ustrip(u"m", x)` in a Unitful.jl extension. That will also require checking the units of the crs.

Unitful is like a free test suite for physical models, not converting is by design!

If we are going to make units work here we need to do ustrip(u"m", x) to ensure we are actually getting metres coming in. If people pass in kilometers and we convert back and forth with Proj.jl treating them as meters in the middle, we are really breaking the contract.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Maybe we have a maybe_ustrip(unit, x) defined in Rasters, its definition for Unitful unit in RastersUnitfulExt, and its definition for Proj units in RastersProjUnitfulExt?

(This is getting to be a mouthful..)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah those names! But makes sense. Somehow we need some mapping from Proj units to Unitful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does conversion from Proj to Unitful have to live in Rasters.jl? Shouldn't this be in a ProjUnitfulExt?

Copy link
Collaborator Author

@asinghvi17 asinghvi17 Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we could introduce a nice interface to batch transform an array, but do it out of place in Proj.jl. Then the Unitful stuff could also live there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially going to put that here because this calls the C-API directly, but if it doesn't have to then there's no reason to stop this from calling Proj.

Comment on lines +83 to +85
ux_3857 = rebuild(x_3857; val = rebuild(val(x_3857); data = val(x_3857) .* Unitful.u"m", span = Regular(val(x_3857).span.step * Unitful.u"m")))
uy_3857 = rebuild(y_3857; val = rebuild(val(y_3857); data = val(y_3857) .* Unitful.u"m", span = Regular(val(y_3857).span.step * Unitful.u"m")))
unitful_dimz_3857 = (ux_3857, uy_3857)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also super complicated, should we have DD overloads to make constructing lookups with unitful easier?

Copy link
Owner

@rafaqz rafaqz Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think set will just work this out for us, no need to rebuild manually?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants