See the O$_2$sclpy documentation at https://neutronstars.utk.edu/code/o2sclpy for more information.
import o2sclpy
import matplotlib.pyplot as plot
import numpy
import sys
plots=True
if 'pytest' in sys.modules:
plots=False
Link the O$_2$scl library:
link=o2sclpy.linker()
link.link_o2scl()
Get a copy (a pointer to) the O$_2$scl unit conversion object:
cu=link.o2scl_settings.get_convert_units()
By default, conversions are allowed to presume that $\hbar=c=k_B=1$. This code converts 2 $\mathrm{MeV}$ to $1/\mathrm{fm}$:
val=cu.convert('MeV','1/fm',2.0)
print('Conversion from MeV to 1/fm: %7.6e' % val)
1.013546e-02
Add a new unit, a Bethe, defined to be $ 10^{51}~\mathrm{erg} $
cu.add_unit(b'Bethe',cu.convert('erg','kg*m^2/s^2',1.0e51),
b'fifty one ergs',2,1,-2)
Now use the new unit conversion
print(cu.convert('erg','Bethe',3.0e53))
Unicode is supported. Set a unit named α to refer to 3 Newtons per Kelvin
cu.add_unit('α',3.0,'alpha unit',1,1,-2,-1)
9.999999999999999e+43 b'fifty one ergs' b'Bethe'
Print current unit table
cu.print_units_cout()
SI-like: label m kg s K A mol cd value --------------- -- -- -- -- -- --- -- ------------ rad 0 0 0 0 0 0 0 1.000000e+00 sr 0 0 0 0 0 0 0 1.000000e+00 J 2 1 -2 0 0 0 0 1.000000e+00 N 1 1 -2 0 0 0 0 1.000000e+00 Pa -1 1 -2 0 0 0 0 1.000000e+00 W 2 1 -3 0 0 0 0 1.000000e+00 C 0 1 0 0 1 0 0 1.000000e+00 V 2 1 -3 0 -1 0 0 1.000000e+00 ohm 2 1 -3 0 -2 0 0 1.000000e+00 S -2 -1 3 0 2 0 0 1.000000e+00 F -2 -1 4 0 2 0 0 1.000000e+00 Wb 2 1 -2 0 -1 0 0 1.000000e+00 H 2 1 -2 0 -2 0 0 1.000000e+00 T 0 1 -2 0 -1 0 0 1.000000e+00 Hz 0 0 -1 0 0 0 0 1.000000e+00 lm 0 0 0 0 0 0 1 1.000000e+00 lx -2 0 0 0 0 0 1 1.000000e+00 Bq 0 0 -1 0 0 0 0 1.000000e+00 Gy 2 0 -2 0 0 0 0 1.000000e+00 Sv 2 0 -2 0 0 0 0 1.000000e+00 kat 0 0 -1 0 0 1 0 1.000000e+00 l 3 0 0 0 0 0 0 1.000000e-03 L 3 0 0 0 0 0 0 1.000000e-03 Da 0 1 0 0 0 0 0 1.660539e-27 eV 2 1 -2 0 0 0 0 1.602177e-19 SI prefixes: prefix value ------------------- ------------ Y 1.000000e+24 Z 1.000000e+21 E 1.000000e+18 P 1.000000e+15 T 1.000000e+12 G 1.000000e+09 M 1.000000e+06 k 1.000000e+03 h 1.000000e+02 da 1.000000e+01 d 1.000000e-01 c 1.000000e-02 m 1.000000e-03 mu 1.000000e-06 μ 1.000000e-06 n 1.000000e-09 p 1.000000e-12 f 1.000000e-15 a 1.000000e-18 z 1.000000e-21 y 1.000000e-24 Other: label m kg s K A mol cd value full name --------------- -- -- -- -- -- --- -- ------------ --------------------- ft 1 0 0 0 0 0 0 3.048000e-01 foot foot 1 0 0 0 0 0 0 3.048000e-01 foot in 1 0 0 0 0 0 0 2.540000e-02 inch yd 1 0 0 0 0 0 0 9.144000e-01 yard mi 1 0 0 0 0 0 0 1.609344e+03 mile nmi 1 0 0 0 0 0 0 1.852000e+03 nautical mile fathom 1 0 0 0 0 0 0 1.828800e+00 fathom angstrom 1 0 0 0 0 0 0 1.000000e-10 angstrom mil 1 0 0 0 0 0 0 2.540000e-05 mil point 1 0 0 0 0 0 0 3.527778e-04 point texpoint 1 0 0 0 0 0 0 3.514598e-04 texpoint micron 1 0 0 0 0 0 0 1.000000e-06 micron AU 1 0 0 0 0 0 0 1.495979e+11 astronomical unit au 1 0 0 0 0 0 0 1.495979e+11 astronomical unit ly 1 0 0 0 0 0 0 9.460730e+15 light year lyr 1 0 0 0 0 0 0 9.460730e+15 light year Gpc 1 0 0 0 0 0 0 3.085678e+25 gigaparsec Mpc 1 0 0 0 0 0 0 3.085678e+22 megaparsec kpc 1 0 0 0 0 0 0 3.085678e+19 kiloparsec pc 1 0 0 0 0 0 0 3.085678e+16 parsec fermi 1 0 0 0 0 0 0 1.000000e-15 fermi hectare 2 0 0 0 0 0 0 1.000000e+04 hectare ha 2 0 0 0 0 0 0 1.000000e+04 hectare acre 2 0 0 0 0 0 0 4.046856e+03 acre barn 2 0 0 0 0 0 0 1.000000e-28 barn us_gallon 3 0 0 0 0 0 0 3.785412e-03 gallon quart 3 0 0 0 0 0 0 9.463529e-04 quart pint 3 0 0 0 0 0 0 4.731765e-04 pint cup 3 0 0 0 0 0 0 2.365882e-04 cup tbsp 3 0 0 0 0 0 0 1.478676e-05 tablespoon tsp 3 0 0 0 0 0 0 4.928922e-06 teaspoon ca_gallon 3 0 0 0 0 0 0 4.546090e-03 canadian gallon uk_gallon 3 0 0 0 0 0 0 4.546092e-03 uk gallon Msun 0 1 0 0 0 0 0 1.988410e+30 solar mass Msolar 0 1 0 0 0 0 0 1.988410e+30 solar mass pound 0 1 0 0 0 0 0 4.535924e-01 pound ounce 0 1 0 0 0 0 0 2.834952e-02 ounce tonne 0 1 0 0 0 0 0 1.000000e+03 (metric) tonne uk_ton 0 1 0 0 0 0 0 1.016047e+03 uk ton troy_ounce 0 1 0 0 0 0 0 3.110347e-02 troy ounce carat 0 1 0 0 0 0 0 2.000000e-04 carat knot 1 0 -1 0 0 0 0 5.144444e-01 knot c 1 0 -1 0 0 0 0 2.997925e+08 speed of light cal 2 1 -2 0 0 0 0 4.186800e+00 calorie btu 2 1 -2 0 0 0 0 1.055056e+03 btu erg 2 1 -2 0 0 0 0 1.000000e-07 erg therm 2 1 -3 0 0 0 0 1.055060e+08 therm horsepower 2 1 -3 0 0 0 0 7.457000e+02 horsepower hp 2 1 -3 0 0 0 0 7.457000e+02 horsepower atm -1 2 -2 0 0 0 0 1.013250e+05 atmosphere bar -1 1 -2 0 0 0 0 1.000000e+05 bar torr -1 1 -2 0 0 0 0 1.333224e+02 torr psi -1 1 -2 0 0 0 0 6.894757e+03 psi yr 0 0 1 0 0 0 0 3.155693e+07 year (tropical) wk 0 0 1 0 0 0 0 6.048000e+05 week d 0 0 1 0 0 0 0 8.640000e+04 day deg 0 0 0 0 0 0 0 1.745329e-02 degree ° 0 0 0 0 0 0 0 1.745329e-02 degree ′ 0 0 0 0 0 0 0 2.908882e-04 minute (fraction of a degree) ″ 0 0 0 0 0 0 0 4.848137e-06 second (fraction of a degree) hr 0 0 1 0 0 0 0 3.600000e+03 hour min 0 0 1 0 0 0 0 6.000000e+01 minute curie 0 0 -1 0 0 0 0 3.700000e+10 curie dyne 1 1 -2 0 0 0 0 1.000000e-05 dyne poise -1 1 -1 0 0 0 0 1.000000e-01 poise kB 2 1 -2 -1 0 0 0 1.380649e-23 Boltzmann's constant hbar 2 1 -1 0 0 0 0 1.054572e-34 reduced Planck constant Planck 2 1 -1 0 0 0 0 6.626070e-34 Planck constant GNewton 3 -1 -2 0 0 0 0 6.674300e-11 gravitational constant G 0 1 -2 0 -1 0 0 1.000000e-04 gauss NA 0 0 0 0 0 -1 0 6.022141e+23 Avogadro's number 2 1 -2 0 0 0 0 1.000000e+44
Show that our unit named α works
print(cu.convert('N/K','α',27))
Now, remove the use of natural units
cu.set_natural_units(False,False,False)