Skip to content

Instantly share code, notes, and snippets.

@krantikal
Last active March 31, 2021 04:30
Show Gist options
  • Save krantikal/e3e7b197b8f2211a2c1430df9187ecdd to your computer and use it in GitHub Desktop.
Save krantikal/e3e7b197b8f2211a2c1430df9187ecdd to your computer and use it in GitHub Desktop.
[Declared Constants and Unit Conversion] in #astropy and other #scicmp dialects. Not just for #astrophysics
from astropy.constants import c, m_e
m = m_e
E = m * c**2
E_MeV=E.to('MeV')
print 'E in MeV=',E_MeV
from astropy import constants as const
print const.c.Value
print const.c.to('pc/yr')
print const.c.cgs
from astropy import units as u
F = (const.G * 3. * const.M_sun * 100 * u.kg) / (2.2 * u.au) ** 2
print F.to(u.N)
-> 0.367669392028 N
Mpc = 1000.0 * u.parsec # Define new constants
>>> u.m / u.m
Unit(dimensionless)
(1.0 * u.Pa).cgs
(1000 * u.nm).to(u.Hz, equivalencies=u.spectral())
Astropy constants
==================
G
L_sun
M_earth
M_jup
M_sun
N_A
R
R_earth
R_jup
R_sun
Ryd
a0
alpha
atmosphere
au
b_wien
c
e
eps0
g0
h
hbar
k_B
kpc
m_e
m_n
m_p
mu0
muB
pc
sigma_s // Stefan-Boltzmann constant
u // Atomic mass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment