[AstroPy] unit equivalency testing

Adrian Price-Whelan adrianmpw at gmail.com
Thu Feb 23 14:57:48 EST 2017


I think what you want is "is_equivalent()":

>>> import astropy.units as u
>>> a = 15*u.kpc
>>> a.unit.is_equivalent(u.km)
True
>>> a = 15*u.Hz
>>> a.unit.is_equivalent(u.Angstrom, equivalencies=u.spectral())
True

Right now, it's just a method on UnitBase objects, but it might make
sense to add a convenience method on Quantity as well...

On Thu, Feb 23, 2017 at 2:44 PM, John K. Parejko <parejkoj at uw.edu> wrote:
> Hello astropy,
>
> I’m looking for a way to check whether astropy units are “convertible” to each other. For now, asking if the two units have the same physical type is a good start for me (which might be a handy utility method on a quantity: `quantity1.convertible_to(quantity2)`):
>
> u.get_physical_type(quantity.unit) == u.get_physical_type(self.unit)
>
> but I see that there’s a rich unit equivalency system available. What I’d like is something like `quantity1.equivalent_to(quantity2)`, that returns True/False whether those units are equivalent, based on the defined equivalencies of that unit. Is there such a feature available?
>
> Thank you,
> John
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy



-- 
Adrian M. Price-Whelan
Lyman Spitzer, Jr. Postdoctoral Fellow
Princeton University
http://adrian.pw



More information about the AstroPy mailing list