# Astropy units: automatic conversion to the closest human readable unit

**URL:** <https://community.openastronomy.org/t/astropy-units-automatic-conversion-to-the-closest-human-readable-unit/285>\
**Category:** Astropy\
**Created:** [February 17, 2022, 1:56pm UTC](https://community.openastronomy.org/t/astropy-units-automatic-conversion-to-the-closest-human-readable-unit/285 "2022-02-17T13:56:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![facero](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/facero/32/188_2.png) [@facero](https://community.openastronomy.org/u/facero)\
**Post date:** [February 17, 2022, 1:56pm UTC](https://community.openastronomy.org/t/astropy-units-automatic-conversion-to-the-closest-human-readable-unit/285/1 "2022-02-17T13:56:05Z")

</div>

Hi,

I’m a contributor of the astropy-affiliated package `gammapy`, an analysis package for gamma-ray astronomy.  
The gamma-ray energy range is open-ended and we use energy units ranging from MeV to PeV energies spanning 9 decades !

In gammapy we use astropy units but can sometime end up with values as E=1.5e6 MeV which is hard to understand from a user point of view. E=1.5 TeV is more readable.

In this [gammapy PR](https://github.com/gammapy/gammapy/pull/3752), I proposed a helper function to transform :  
E = [1.54e2 \*u.GeV, 4300 _u.keV, 300.6e12_ u.eV] (astropy unit list)  
=\> [‘154 GeV’, ‘4.30 MeV’, ‘301 TeV’] (string representation for label, legends, titles, etc)

Right now, I’m just doing an if/elif test to find the most appropriate unit.  
See the full code description [here](https://github.com/gammapy/gammapy/blob/c208e1bd5e5c4144bc3c13bde3892e0cab6eab05/gammapy/utils/units.py#L69).

Is there already way to do this with astropy ?  
Same issue exists with distance units (nm,cm,m, lyr, Mpc, etc) or time.  
Would you see a more astropy/pythonic way to do this ?

Thanks for any suggestions you might have,  
Fabio

---

<div class="post-metadata">

**Author:** ![hamogu](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/hamogu/32/93_2.png) [@hamogu](https://community.openastronomy.org/u/hamogu)\
**Post date:** [March 22, 2022, 3:50pm UTC](https://community.openastronomy.org/t/astropy-units-automatic-conversion-to-the-closest-human-readable-unit/285/2 "2022-03-22T15:50:28Z")

</div>

I don’t have a better solution; I think this is not bad. It might be useful to have that more general (e.g. length → mirometer, mm, m, km) function in astropy, but I don’t think it exists. Part of that is to figure out what a general algorithm should do, e.g. I use cm, but not ceV.

---

<div class="post-metadata">

**Author:** ![hamogu](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/hamogu/32/93_2.png) [@hamogu](https://community.openastronomy.org/u/hamogu)\
**Post date:** [March 22, 2022, 4:15pm UTC](https://community.openastronomy.org/t/astropy-units-automatic-conversion-to-the-closest-human-readable-unit/285/3 "2022-03-22T16:15:56Z")

</div>

I’ll open a feature request on the issue tracker for that because I think it’s a good feature that astropy should have - but right now I only get errors from github, so I’ll have to try again later.

---

<div class="post-metadata">

**Author:** ![facero](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/facero/32/188_2.png) [@facero](https://community.openastronomy.org/u/facero)\
**Post date:** [March 25, 2022, 1:38pm UTC](https://community.openastronomy.org/t/astropy-units-automatic-conversion-to-the-closest-human-readable-unit/285/4 "2022-03-25T13:38:06Z")

</div>

A similar feature request was apparently opened in 2017 concerning time and bytes.

> <https://github.com/astropy/astropy/issues/6913>
>
> (from discussion with @adrn at PyData2017 NYC)
> 
> Would like to automatically de…termine the units for a quantity so it's more human readable. For example, Googling "100000000000 bytes" (https://www.google.com/search?q=100000000000%20bytes) returns 100 GB automatically.
> 
> Currently, this functionality only exists if the desired units are specified, i.e., \`(100000000000 \* u.m).to(u.Gm)\`, but it would be convenient to have a function that automatically converted \`(100000000000 m).human\_readable()\` to 100 Gm.

I don’t know if it’s worth opening a new one.  
Maybe yes as a more general implementation would require more discussion about what unit would be concerned, how to apply this to arrays, and several other aspects.
