[AstroPy] How can I add another name to a registered unit?

Erik Tollerud erik.tollerud at gmail.com
Wed Feb 18 05:59:30 EST 2015


Hello Duncan,

This isn't precisely supported behavior (because it uses a private
variable), so I'm not sure I can promise it will work long-term, but at
least in 0.4 and 1.0 it should do the trick:

>>> from astropy import units as u
>>> from astropy.units import imperial
>>> imperial.enable()
>>> imperial.deg_F
Unit("deg_F")
>>> u.Unit('deg_F')
Unit("deg_F")
>>> u.Unit('Degrees_F')
ValueError: 'Degrees_F' did not parse as unit: At col 0, Degrees_F is not a
valid unit. Did you mean degree?
>>> imperial.deg_F._names.append('Degrees_F')  # this is the tricky part
that involves a private variable
>>> u.add_enabled_units([imperial.deg_F])  # this registers the new names
>>> u.Unit('Degrees_F')
Unit("deg_F")


Hope that helps!


On Tue, Feb 3, 2015 at 11:09 AM, Duncan Macleod <duncan.macleod at ligo.org>
wrote:

> Hi all,
>
> I’m wondering how to add another name to an already registered unit. The
> example I have is a data file containing the unit string ‘Degrees_F’ -
> meaning Fahrenheit. I understand I can enable a new unit called ‘Degrees_F’
> that represents the original ‘deg_F/Fahrenheit' unit from units.imperial,
> but what I’d really like to do is just register the string ‘Degrees_F’ as
> another name for ‘deg_F’, rather than an equivalent unit.
>
> In the end I’d like to see the following:
>
> >>> units.Unit(‘Degrees_F’)
> Unit(‘deg_F’)
>
> Is this possible?
>
>
> Thanks
> Duncan
> --
> Duncan Macleod
> duncan.macleod at ligo.org
> LIGO Data Grid systems development
> Louisiana State University
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
>
>


-- 
Erik T
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20150218/f04140f3/attachment.html>


More information about the AstroPy mailing list