[AstroPy] Units with time-dependent conversion

Thomas Robitaille thomas.robitaille at gmail.com
Wed Aug 17 17:45:42 EDT 2016


Hi Elke,

I think it might be possible to do this by defining a new unit
equivalency that can take a time as an argument:

http://docs.astropy.org/en/stable/units/equivalencies.html#writing-new-equivalencies

The code for this would look something like this:

def currency_conversion(time):
    # figure out the conversion rates based on time
    return [(u.EUR, u.USD, lambda x: x * EUR2USD, lambda x: x / EUR2USD), ...]

and you can return as many pairs of conversions as you like (see the
above link for more details). The you could use it like:

>>> (4 * u.EUR).to(u.USD, currency_conversion(Time.now()))

Cheers,
Tom



On 4 August 2016 at 11:33, Elke Schaper <elkewschaper at gmail.com> wrote:
> Dear Astropy-Team,
>
>
> thanks for a great tool first of all!
> Perhaps this question has been asked before:
>
> We would like to add “Currency” as a unit system to astropy, with units such
> as “USD”, “EUR”, …
> This would allow to use all of the great astropy functionality (e.g. the
> close integration with numpy) on monetary data also, combined with all
> already integrated units.
>
> In principle, new units are easy to add to Astropy.
> Now comes the challenge:
>
> Conversions between these units change with time. For example, there are
> other great Python packages (such as
> https://pypi.python.org/pypi/CurrencyConverter/0.5) to do a time dependent
> conversion.
>
> Is it possible to add “time” as an input parameter to unit conversion in
> Astropy?
> If yes, could you give me some hints how to best go ahead (I would probably
> need to overwrite some of the internal functionality?). Or is this a solved
> problem, just I overlooked it?
>
>
>
> Thanks a lot, all the best!
>
>
> Elke
>
>
>
>
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy
>



More information about the AstroPy mailing list