Пример #1
0
    def get_base_equivalent(self, unit_system=None):
        """Create and return dimensionally-equivalent units in a specified base.

        >>> from unyt import g, cm
        >>> (g/cm**3).get_base_equivalent('mks')
        kg/m**3
        >>> (g/cm**3).get_base_equivalent('solar')
        Mearth/AU**3
        """
        from unyt.unit_registry import _sanitize_unit_system

        unit_system = _sanitize_unit_system(unit_system, self)
        try:
            conv_data = _check_em_conversion(
                self.units, registry=self.registry, unit_system=unit_system
            )
            um = unit_system.units_map
            if self.dimensions in um and self.expr == um[self.dimensions]:
                return self.copy()
        except MKSCGSConversionError:
            raise UnitsNotReducible(self.units, unit_system)
        if any(conv_data):
            new_units, _ = _em_conversion(self, conv_data, unit_system=unit_system)
        else:
            try:
                new_units = unit_system[self.dimensions]
            except MissingMKSCurrent:
                raise UnitsNotReducible(self.units, unit_system)
        return Unit(new_units, registry=self.registry)
Пример #2
0
    def get_base_equivalent(self, unit_system="mks"):
        """Create and return dimensionally-equivalent units in a specified base.

        >>> from unyt import g, cm
        >>> (g/cm**3).get_base_equivalent('mks')
        kg/m**3
        >>> (g/cm**3).get_base_equivalent('solar')
        Mearth/AU**3
        """
        from unyt.unit_systems import unit_system_registry
        if hasattr(unit_system, "unit_registry"):
            unit_system = unit_system.unit_registry.unit_system_id
        elif unit_system == "code":
            unit_system = self.registry.unit_system_id
        unit_system = unit_system_registry[str(unit_system)]
        try:
            conv_data = _check_em_conversion(self.units.expr)
        except MKSCGSConversionError:
            raise UnitsNotReducible(self.units, unit_system)
        if any(conv_data):
            new_units, _ = _em_conversion(
                self, conv_data, unit_system=unit_system)
        else:
            new_units = unit_system[self.dimensions]
        return Unit(new_units, registry=self.registry)
Пример #3
0
def _Unit_get_base_equivalent_no_em(self, unit_system=None):
    """Create and return dimensionally-equivalent units in a specified base.

    **Assumes that no electromagnetic units are being used.**

    >>> from unyt import g, cm
    >>> (g/cm**3).get_base_equivalent('mks')
    kg/m**3
    >>> (g/cm**3).get_base_equivalent('solar')
    Mearth/AU**3
    """

    unit_system = _sanitize_unit_system(unit_system, self)

    try:
        new_units = unit_system[self.dimensions]
    except MissingMKSCurrent:
        raise UnitsNotReducible(self.units, unit_system)
    return unyt.Unit(new_units, registry=self.registry)
Пример #4
0
    def get_base_equivalent(self, unit_system="mks"):
        """Create and return dimensionally-equivalent units in a specified base.

        >>> from unyt import g, cm
        >>> (g/cm**3).get_base_equivalent('mks')
        kg/m**3
        >>> (g/cm**3).get_base_equivalent('solar')
        Mearth/AU**3
        """
        unit_system = _sanitize_unit_system(unit_system, self)
        try:
            conv_data = _check_em_conversion(
                self.units, registry=self.registry, unit_system=unit_system)
        except MKSCGSConversionError:
            raise UnitsNotReducible(self.units, unit_system)
        if any(conv_data):
            new_units, _ = _em_conversion(
                self, conv_data, unit_system=unit_system)
        else:
            new_units = unit_system[self.dimensions]
        return Unit(new_units, registry=self.registry)