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

Duncan Macleod duncan.macleod at ligo.org
Wed Feb 18 10:33:13 EST 2015


Hi Erik,

This looks easy enough, and isn't too hacky.


Thanks
Duncan

On 18 February 2015 at 04:59, Erik Tollerud <erik.tollerud at gmail.com> wrote:

> 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
>



-- 
Duncan Macleod
duncan.macleod at ligo.org
LIGO Data Grid systems development
Louisiana State University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20150218/1a017c00/attachment.html>


More information about the AstroPy mailing list