def test_cgs_equivalent(): """ Get cgs equivalent to some unit. """ from dimensionful.dimensions import mass_density from dimensionful.units import get_conversion_factor Msun_cgs = 1.98892e33 Mpc_cgs = 3.08568e24 u1 = Unit("Msun * Mpc**-3") u2 = Unit("g * cm**-3") u3 = u1.get_cgs_equivalent() assert u2.expr == u3.expr assert u2 == u3 assert equal_sigfigs(u1.cgs_value, Msun_cgs / Mpc_cgs ** 3, 8) assert u2.cgs_value == 1 assert u3.cgs_value == 1 assert u1.dimensions == mass_density assert u2.dimensions == mass_density assert u3.dimensions == mass_density assert equal_sigfigs(get_conversion_factor(u1, u3), Msun_cgs / Mpc_cgs ** 3, 8)
def convert_to(self, units): """ Convert the data and units to given unit. This overwrites the ``data`` and ``units`` attributes, making no copies, and returns None. Parameters ---------- units : Unit object or string The units you want the data in. """ new_units = self._unit_repr_check_same(units) conversion_factor = get_conversion_factor(self.units, new_units) self.data *= conversion_factor self.units = new_units return self
def get_in(self, units): """ Creates a new Quantity with the data in the supplied units, and returns it. Does not modify this object. Parameters ---------- units : Unit object or string The units you want to get a new quantity in. Returns ------- Quantity object with converted data and supplied units. """ new_units = self._unit_repr_check_same(units) conversion_factor = get_conversion_factor(self.units, new_units) return Quantity(self.data * conversion_factor, new_units)
def get_data_in(self, units): """ Returns the data, converted to the supplied units. Parameters ---------- units : Unit object or string The units you want the data in. Returns ------- ``data`` attribute, multiplied by the conversion factor to the supplied units. """ new_units = self._unit_repr_check_same(units) # don't operate on data if given the same units if self.units == new_units: return self.data conversion_factor = get_conversion_factor(self.units, new_units) return self.data * conversion_factor