Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_temperature_conversions():
"""
Test conversions between various supported temperatue scales.
Also ensure we only allow compound units with temperature
scales that have a proper zero point.
"""
from yt.units.unit_object import InvalidUnitOperation
km = YTQuantity(1, 'km')
balmy = YTQuantity(300, 'K')
balmy_F = YTQuantity(80.33, 'degF')
balmy_C = YTQuantity(26.85, 'degC')
balmy_R = YTQuantity(540, 'R')
assert_array_almost_equal(balmy.in_units('degF'), balmy_F)
assert_array_almost_equal(balmy.in_units('degC'), balmy_C)
assert_array_almost_equal(balmy.in_units('R'), balmy_R)
balmy_view = balmy.ndarray_view()
balmy.convert_to_units('degF')
assert_true(balmy_view.base is balmy.base)
assert_array_almost_equal(np.array(balmy), np.array(balmy_F))
balmy.convert_to_units('degC')
assert_true(balmy_view.base is balmy.base)
assert_array_almost_equal(np.array(balmy), np.array(balmy_C))
def _sanitize_min_max_units(amin, amax, finfo, registry):
# returns a copy of amin and amax, converted to finfo's output units
umin = getattr(amin, 'units', None)
umax = getattr(amax, 'units', None)
if umin is None:
umin = Unit(finfo.output_units, registry=registry)
rmin = YTQuantity(amin, umin)
else:
rmin = amin.in_units(finfo.output_units)
if umax is None:
umax = Unit(finfo.output_units, registry=registry)
rmax = YTQuantity(amax, umax)
else:
rmax = amax.in_units(finfo.output_units)
return rmin, rmax
yc = None
if isinstance(origin, string_types):
origin = tuple(origin.split('-'))[:3]
if 1 == len(origin):
origin = ('lower', 'left') + origin
elif 2 == len(origin) and origin[0] in set(['left','right','center']):
o0map = {'left': 'lower', 'right': 'upper', 'center': 'center'}
origin = (o0map[origin[0]],) + origin
elif 2 == len(origin) and origin[0] in set(['lower','upper','center']):
origin = (origin[0], 'center', origin[-1])
elif 3 == len(origin) and isinstance(origin[0], (int, float)):
xc = self.ds.quan(origin[0], 'code_length')
yc = self.ds.quan(origin[1], 'code_length')
elif 3 == len(origin) and isinstance(origin[0], tuple):
xc = YTQuantity(origin[0][0], origin[0][1])
yc = YTQuantity(origin[1][0], origin[0][1])
assert origin[-1] in ['window', 'domain', 'native']
if origin[2] == 'window':
xllim, xrlim = self.xlim
yllim, yrlim = self.ylim
elif origin[2] == 'domain':
xax = self.ds.coordinates.x_axis[axis_index]
yax = self.ds.coordinates.y_axis[axis_index]
xllim = self.ds.domain_left_edge[xax]
xrlim = self.ds.domain_right_edge[xax]
yllim = self.ds.domain_left_edge[yax]
yrlim = self.ds.domain_right_edge[yax]
elif origin[2] == 'native':
return (self.ds.quan(0.0, 'code_length'),
def _set_center(self, center):
if center is None:
self.center = None
return
elif isinstance(center, YTArray):
self.center = self.ds.arr(center.copy())
self.center.convert_to_units('code_length')
elif isinstance(center, (list, tuple, np.ndarray)):
if isinstance(center[0], YTQuantity):
self.center = self.ds.arr([c.copy() for c in center])
self.center.convert_to_units('code_length')
else:
self.center = self.ds.arr(center, 'code_length')
elif isinstance(center, string_types):
if center.lower() in ("c", "center"):
self.center = self.ds.domain_center
# is this dangerous for race conditions?
elif center.lower() in ("max", "m"):
self.center = self.ds.find_max(("gas", "density"))[1]
elif center.startswith("max_"):
self.center = self.ds.find_max(center[4:])[1]
elif center.lower() == "min":
self.center = self.ds.find_min(("gas", "density"))[1]
elif center.startswith("min_"):
self.center = self.ds.find_min(center[4:])[1]
#-----------------------------------------------------------------------------
from yt.units.yt_array import YTQuantity as quan
#
# meter
#
fm = femtometer = quan(1.0, "fm")
pm = picometer = quan(1.0, "pm")
nm = nanometer = quan(1.0, "nm")
um = micrometer = quan(1.0, "um")
mm = millimeter = quan(1.0, "mm")
cm = centimeter = quan(1.0, "cm")
m = meter = quan(1.0, "m")
km = kilometer = quan(1.0, "km")
Mm = Megameter = megameter = quan(1.0, "Mm")
#
# parsec
#
pc = parsec = quan(1.0, "pc")
kpc = kiloparsec = quan(1.0, "kpc")
Mpc = mpc = megaparsec = quan(1.0, "Mpc")
Gpc = gpc = Gigaparsec = quan(1.0, "Gpc")
#
# gram
#
mg = milligram = quan(1.0, "mg")
g = gram = quan(1.0, "g")
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
from yt.units.yt_array import YTQuantity as quan
#
# meter
#
fm = femtometer = quan(1.0, "fm")
pm = picometer = quan(1.0, "pm")
nm = nanometer = quan(1.0, "nm")
um = micrometer = quan(1.0, "um")
mm = millimeter = quan(1.0, "mm")
cm = centimeter = quan(1.0, "cm")
m = meter = quan(1.0, "m")
km = kilometer = quan(1.0, "km")
Mm = Megameter = megameter = quan(1.0, "Mm")
#
# parsec
#
pc = parsec = quan(1.0, "pc")
kpc = kiloparsec = quan(1.0, "kpc")
Mpc = mpc = megaparsec = quan(1.0, "Mpc")
Gpc = gpc = Gigaparsec = quan(1.0, "Gpc")
#
# gram
#
mg = milligram = quan(1.0, "mg")
# Physical Constants
boltzmann_constant_cgs = YTQuantity(boltzmann_constant_erg_per_K, 'erg/K')
boltzmann_constant = boltzmann_constant_cgs
kboltz = boltzmann_constant_cgs
kb = kboltz
gravitational_constant_cgs = YTQuantity(6.67384e-8, 'cm**3/g/s**2')
gravitational_constant = gravitational_constant_cgs
G = gravitational_constant_cgs
planck_constant_cgs = YTQuantity(6.62606957e-27, 'erg*s')
planck_constant = planck_constant_cgs
hcgs = planck_constant_cgs
hbar = 0.5*hcgs/pi
stefan_boltzmann_constant_cgs = YTQuantity(5.670373e-5, 'erg/cm**2/s**1/K**4')
stefan_boltzmann_constant = stefan_boltzmann_constant_cgs
Tcmb = YTQuantity(2.726, 'K') # Current CMB temperature
CMB_temperature = Tcmb
# Solar System
mass_sun_cgs = YTQuantity(mass_sun_grams, 'g')
mass_sun = mass_sun_cgs
solar_mass = mass_sun_cgs
msun = mass_sun_cgs
# Standish, E.M. (1995) "Report of the IAU WGAS Sub-Group on Numerical Standards",
# in Highlights of Astronomy (I. Appenzeller, ed.), Table 1,
# Kluwer Academic Publishers, Dordrecht.
# REMARK: following masses include whole systems (planet + moons)
mass_jupiter_cgs = YTQuantity(mass_jupiter_grams, 'g')
mass_jupiter = mass_jupiter_cgs
keV = kilo_electron_volt = quan(1.0, "keV")
MeV = mega_electron_volt = quan(1.0, "MeV")
GeV = giga_electron_volt = quan(1.0, "GeV")
amu = atomic_mass_unit = quan(1.0, "amu")
angstrom = quan(1.0, "angstrom")
me = electron_mass = quan(1.0, "me")
#
# Angle units
#
deg = degree = quan(1.0, "degree")
rad = radian = quan(1.0, "radian")
arcsec = arcsecond = quan(1.0, "arcsec")
arcmin = arcminute = quan(1.0, "arcmin")
mas = milliarcsecond = quan(1.0, "mas")
sr = steradian = quan(1.0, "steradian")
#
# CGS electromagnetic units
#
electrostatic_unit = esu = quan(1.0, "esu")
gauss = G = quan(1.0, "gauss")
statampere = statA = quan(1.0, "statA")
statvolt = statV = quan(1.0, "statV")
statohm = quan(1.0, "statohm")
#
# SI electromagnetic units
#
jupiter_mas = mass_jupiter_cgs
mass_mercury_cgs = YTQuantity(mass_mercury_grams, 'g')
mass_mercury = mass_mercury_cgs
mercury_mass = mass_mercury_cgs
mass_venus_cgs = YTQuantity(mass_venus_grams, 'g')
mass_venus = mass_venus_cgs
venus_mass = mass_venus_cgs
mass_earth_cgs = YTQuantity(mass_earth_grams, 'g')
mass_earth = mass_earth_cgs
earth_mass = mass_earth_cgs
mearth = mass_earth_cgs
mass_mars_cgs = YTQuantity(mass_mars_grams, 'g')
mass_mars = mass_mars_cgs
mars_mass = mass_mars_cgs
mass_saturn_cgs = YTQuantity(mass_saturn_grams, 'g')
mass_saturn = mass_saturn_cgs
saturn_mass = mass_saturn_cgs
mass_uranus_cgs = YTQuantity(mass_uranus_grams, 'g')
mass_uranus = mass_uranus_cgs
uranus_mass = mass_uranus_cgs
mass_neptune_cgs = YTQuantity(mass_neptune_grams, 'g')
mass_neptune = mass_neptune_cgs
neptune_mass = mass_neptune_cgs
# Planck units
def get_interpolator(self, data_type, e_min, e_max, energy=True):
data = getattr(self, "emissivity_%s" % data_type)
if not energy:
data = data[..., :] / self.emid.v
e_min = YTQuantity(e_min, "keV")*(1.0+self.redshift)
e_max = YTQuantity(e_max, "keV")*(1.0+self.redshift)
if (e_min - self.ebin[0]) / e_min < -1e-3 or \
(e_max - self.ebin[-1]) / e_max > 1e-3:
raise EnergyBoundsException(self.ebin[0], self.ebin[-1])
e_is, e_ie = np.digitize([e_min, e_max], self.ebin)
e_is = np.clip(e_is - 1, 0, self.ebin.size - 1)
e_ie = np.clip(e_ie, 0, self.ebin.size - 1)
my_dE = self.dE[e_is: e_ie].copy()
# clip edge bins if the requested range is smaller
my_dE[0] -= e_min - self.ebin[e_is]
my_dE[-1] -= self.ebin[e_ie] - e_max
interp_data = (data[..., e_is:e_ie]*my_dE).sum(axis=-1)
if data.ndim == 2:
emiss = UnilinearFieldInterpolator(np.log10(interp_data),