Package: physunits
physunitsSimple package with common physical unit definitions.InstallationAdd this package to your existing python environment with pippip install physunits, or alternatively follow your distribution specific instructions.UsageExample 1An example usage is:>>fromphysunits.lengthimport*>>print(f"{1000*cm/um:.2f}is how many microns there are in a thousand centimeters")10000000.00ishowmanymicronsthereareinathousandcentimetersNote that in the above, only thephysunits.lengthmodule is imported (new feature in version 1.0.0), so no other untits like time units, or pressure units will be available. It is possible to import all available units, as shown in the following examples. If only a subset of the supported units is desired, manually importing them asfromphysunits.lengthimportmm,nm,umExample 2physunitsincludes the most widely used unit prefixes, but you can generate other units, for example:>>fromphysunits.time_unitsimports>>minute=60*s>>hour=60*minute>>day=24*hour>>print(f'There are approximately{365*day/hour}hours in a year.')Thereareapproximately8760.0hoursinayear.Units are simplefloatobjects meant to make physics code more readable, and physical units more tracktable.
is good enough.Another example where physunits may be useful is illustrated by the following script dealing with a voltage trace simulation:importnumpyasnpimportmatplotlib.pyplotaspltfromphysunitsimport*f=2.12*kHzomega=2*np.pi*ft=np.linspace(-670*us,410*us,2**10)my_voltage=(1.5*mV)*np.sin(omega*t)# Now plot voltage in mV vs time in msplt.plot(t/ms,my_voltage/mV)plt.xlabel(r't (ms)')plt.ylabel(r'V (mV)')plt.show()which outputs the following plot:DescriptionAll units are referenced to the SI. This means that base, derived, freedom (imperial), and other units including prefixes are always relative to theSI base units. An example where this is manifest is that when imported, the unitkgtakes on the numerical value1.0, regardless of the prefix indicating the numerical factor of1000.UnitSupportedlengthfm, pm, nm, um, mm, cm, m, km, inch, ft, ydtimeps, ns, us, ms, s, minute, hour, day, weekmassng, ug, mg, g, kg, lb, oztemperaturenK, uK, mK, Kangle*deg, rad, mradfrequencymHz, Hz, kHz, MHz, GHz, THzvoltagepV, nV, uV, mV, V, kVchargeC, mC, uC, nCcurrentnA, uA, mA, AresistanceOhm, mOhm, kOhm, MOhm, GOhmcapacitanceF, mF, uF, nF, pFinductanceH, mH, uH, nHmagnetic field strengthT, mT, uT, nT, pTenergy**mJ, J, kJ, meV, eV, keV, MeV, GeV, TeVpowernW, uW, mW, W, kW, MWpressuremPa, Pa, kPa, MPa, atm, psi, Torr, mBar, Barrel concentrationppm, ppbIf you want your favorite unit to be added, please open an issue, or fork and submit a pull request.physunitsis always thriving to improve.Weallknow that if there was an SI unit for angles, it would be the radian, so inphysunits, it takes the valuerad = 1.0, and degrees are defined relative to it. This works nicely in all trig functions. Special thanks to chrisjbillington for pointing this out.** While the electronvolt (eV) is not a part of the SI, the SIrecognizes its use. Furthermore, thelatest SI redefinitionfixed the value of the electron charge constant, effectively fixing the conversion betweeneVandJ.Contact and supportPlease report, fork, test, contribute, or create an issue directly on the project'srepository.