Example #1
0
    def _set_units(self, ds, base_units):
        if ds is not None:
            if getattr(ds, "cosmological_simulation", False):
                self.hubble_constant = ds.hubble_constant
                self.current_redshift = ds.current_redshift
        attrs = (
            "length_unit",
            "mass_unit",
            "time_unit",
            "velocity_unit",
            "magnetic_unit",
        )
        cgs_units = ("cm", "g", "s", "cm/s", "gauss")
        for unit, attr, cgs_unit in zip(base_units, attrs, cgs_units):
            if unit is None:
                if ds is not None:
                    u = getattr(ds, attr, None)
                elif attr == "velocity_unit":
                    u = self.length_unit / self.time_unit
                elif attr == "magnetic_unit":
                    u = np.sqrt(4.0 * np.pi * self.mass_unit /
                                (self.time_unit**2 * self.length_unit))
                else:
                    u = cgs_unit
            else:
                u = unit

            if isinstance(u, str):
                uq = YTQuantity(1.0, u)
            elif isinstance(u, numeric_type):
                uq = YTQuantity(u, cgs_unit)
            elif isinstance(u, YTQuantity):
                uq = u.copy()
            elif isinstance(u, tuple):
                uq = YTQuantity(u[0], u[1])
            else:
                uq = None

            if uq is not None and hasattr(self, "hubble_constant"):
                # Don't store cosmology units
                atoms = {str(a) for a in uq.units.expr.atoms()}
                if str(uq.units).startswith(
                        "cm") or "h" in atoms or "a" in atoms:
                    uq.convert_to_cgs()

            if uq is not None and uq.units.is_code_unit:
                mylog.warning(
                    "Cannot use code units of '%s' "
                    "when creating a FITSImageData instance! "
                    "Converting to a cgs equivalent.",
                    uq.units,
                )
                uq.convert_to_cgs()

            if attr == "length_unit" and uq.value != 1.0:
                mylog.warning("Converting length units from %s to %s.", uq,
                              uq.units)
                uq = YTQuantity(1.0, uq.units)

            setattr(self, attr, uq)
Example #2
0
    def _set_units(self, ds, base_units):
        attrs = (
            "length_unit",
            "mass_unit",
            "time_unit",
            "velocity_unit",
            "magnetic_unit",
        )
        cgs_units = ("cm", "g", "s", "cm/s", "gauss")
        for unit, attr, cgs_unit in zip(base_units, attrs, cgs_units):
            if unit is None:
                if ds is not None:
                    u = getattr(ds, attr, None)
                elif attr == "velocity_unit":
                    u = self.length_unit / self.time_unit
                elif attr == "magnetic_unit":
                    u = np.sqrt(
                        4.0
                        * np.pi
                        * self.mass_unit
                        / (self.time_unit ** 2 * self.length_unit)
                    )
                else:
                    u = cgs_unit
            else:
                u = unit

            if isinstance(u, str):
                uq = YTQuantity(1.0, u)
            elif isinstance(u, numeric_type):
                uq = YTQuantity(u, cgs_unit)
            elif isinstance(u, YTQuantity):
                uq = u.copy()
            elif isinstance(u, tuple):
                uq = YTQuantity(u[0], u[1])
            else:
                uq = None

            if uq is not None and uq.units.is_code_unit:
                mylog.warning(
                    "Cannot use code units of '%s' " % uq.units
                    + "when creating a FITSImageData instance! "
                    "Converting to a cgs equivalent."
                )
                uq.convert_to_cgs()

            if attr == "length_unit" and uq.value != 1.0:
                mylog.warning(
                    "Converting length units " "from %s to %s." % (uq, uq.units)
                )
                uq = YTQuantity(1.0, uq.units)

            setattr(self, attr, uq)