[yt-svn] commit/yt: 19 new changesets
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Mon Sep 14 11:06:22 PDT 2015
19 new commits in yt:
https://bitbucket.org/yt_analysis/yt/commits/124187c5b9fd/
Changeset: 124187c5b9fd
Branch: yt
User: jzuhone
Date: 2015-09-05 18:41:36+00:00
Summary: Roundtrip conversions between Pint and yt units
Affected #: 1 file
diff -r 4be612c5693787623b448e6976fee7a40eacbb82 -r 124187c5b9fd3e5f04f17d2ef855b6466d271811 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -617,6 +617,73 @@
"an AstroPy quantity.")
return self.value*_astropy.units.Unit(str(self.units), **kwargs)
+ @classmethod
+ def from_pint(cls, arr, unit_registry=None):
+ """
+ Convert a Pint "Quantity" to a YTArray or YTQuantity.
+
+ Unfortunately, currently there is no straightforward way to convert
+ from pint Quantities to something in yt in the exact same units.
+ Therefore, most quantities will be returned in some combination of
+ pint's default base units of [meter,second,gram,radian,ampere,kelvin].
+ From there, the units can be converted in yt to whatever units one
+ desires. In the case where the unit is simply degrees Celsius or
+ Fahrenheit, the units will be passed over directly.
+
+ Parameters
+ ----------
+ arr : Pint Quantity
+ The Quantity to convert from.
+ unit_registry : Pint UnitRegistry, optional
+ The Pint UnitRegistry to use in the conversion. If one is not
+ supplied, the default one will be used.
+ """
+ from pint import UnitRegistry
+ if unit_registry is None:
+ unit_registry = UnitRegistry()
+ if str(arr.units) in ["degC","degF"]:
+ factor = 1.0
+ p_units = str(arr.units)
+ else:
+ pint_to_yt = {"meter":"m",
+ "ampere":"A",
+ "second":"s",
+ "radian":"rad",
+ "gram":"g",
+ "kelvin":"K"}
+ base_units = unit_registry.get_base_units(arr.units)
+ factor = base_units[0]
+ base_units = base_units[1]
+ p_units = []
+ for base, power in base_units.items():
+ unit_str = pint_to_yt.get(base, None)
+ if unit_str is None:
+ raise RuntimeError("Cannot parse base unit \"%s\" in Pint "
+ "Quantity with units of %s!" % (base, arr.units))
+ p_units.append("%s**(%s)" % (unit_str, Rational(power)))
+ p_units = "*".join(p_units)
+ if isinstance(arr.magnitude, np.ndarray):
+ return factor*YTArray(arr.magnitude, p_units)
+ else:
+ return factor*YTQuantity(arr.magnitude, p_units)
+
+ def to_pint(self, unit_registry=None):
+ """
+ Convert a YTArray or YTQuantity to a Pint Quantity.
+
+ Parameters
+ ----------
+ arr : YTArray or YTQuantity
+ The unitful quantity to convert from.
+ unit_registry : Pint UnitRegistry, optional
+ The Pint UnitRegistry to use in the conversion. If one is not
+ supplied, the default one will be used.
+ """
+ from pint import UnitRegistry
+ if unit_registry is None:
+ unit_registry = UnitRegistry()
+ return unit_registry.Quantity(self.value, str(self.units))
+
#
# End unit conversion methods
#
https://bitbucket.org/yt_analysis/yt/commits/a99392276fa5/
Changeset: a99392276fa5
Branch: yt
User: jzuhone
Date: 2015-09-05 19:56:39+00:00
Summary: Add "mole" unit
Affected #: 2 files
diff -r 124187c5b9fd3e5f04f17d2ef855b6466d271811 -r a99392276fa579da86c54eb58d02e7635ea151f7 yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -110,6 +110,7 @@
"counts": (1.0, dimensions.dimensionless),
"photons": (1.0, dimensions.dimensionless),
"me": (mass_electron_grams, dimensions.mass),
+ "mol": (1.0/amu_grams, dimensions.dimensionless),
# for AstroPy compatibility
"solMass": (mass_sun_grams, dimensions.mass),
diff -r 124187c5b9fd3e5f04f17d2ef855b6466d271811 -r a99392276fa579da86c54eb58d02e7635ea151f7 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -623,12 +623,12 @@
Convert a Pint "Quantity" to a YTArray or YTQuantity.
Unfortunately, currently there is no straightforward way to convert
- from pint Quantities to something in yt in the exact same units.
- Therefore, most quantities will be returned in some combination of
- pint's default base units of [meter,second,gram,radian,ampere,kelvin].
- From there, the units can be converted in yt to whatever units one
- desires. In the case where the unit is simply degrees Celsius or
- Fahrenheit, the units will be passed over directly.
+ from Pint Quantities to a YTArray in the same units. Therefore, most
+ quantities will be returned in some equivalent combination of Pint's
+ default base units of [meter,second,gram,radian,ampere,kelvin,mole].
+ From there, the units can be converted in yt to whatever equivalent
+ units one desires. In the case where the unit is simply degrees
+ Celsius or Fahrenheit, the units will be passed over directly.
Parameters
----------
@@ -637,6 +637,15 @@
unit_registry : Pint UnitRegistry, optional
The Pint UnitRegistry to use in the conversion. If one is not
supplied, the default one will be used.
+
+ Examples
+ --------
+ >>> from pint import UnitRegistry
+ >>> import numpy as np
+ >>> ureg = UnitRegistry()
+ >>> a = np.random.random(10)
+ >>> b = ureg.Quantity(a, "erg/cm**3")
+ >>> c = yt.YTArray.from_pint(b, unit_registry=ureg)
"""
from pint import UnitRegistry
if unit_registry is None:
@@ -650,7 +659,8 @@
"second":"s",
"radian":"rad",
"gram":"g",
- "kelvin":"K"}
+ "kelvin":"K",
+ "mole":"mol"}
base_units = unit_registry.get_base_units(arr.units)
factor = base_units[0]
base_units = base_units[1]
@@ -678,6 +688,11 @@
unit_registry : Pint UnitRegistry, optional
The Pint UnitRegistry to use in the conversion. If one is not
supplied, the default one will be used.
+
+ Examples
+ --------
+ >>> a = YTQuantity(4.0, "cm**2/s")
+ >>> b = a.to_pint()
"""
from pint import UnitRegistry
if unit_registry is None:
https://bitbucket.org/yt_analysis/yt/commits/c2183df1daf9/
Changeset: c2183df1daf9
Branch: yt
User: jzuhone
Date: 2015-09-05 19:56:50+00:00
Summary: Pint tests--don't work yet
Affected #: 1 file
diff -r a99392276fa579da86c54eb58d02e7635ea151f7 -r c2183df1daf9042230ec168fbc6b144cacde9d61 yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -829,6 +829,31 @@
yield assert_array_equal, yt_arr, YTArray.from_astropy(yt_arr.to_astropy())
yield assert_equal, yt_quan, YTQuantity.from_astropy(yt_quan.to_astropy())
+ at requires_module("pint")
+def test_pint():
+ from pint import UnitRegistry
+
+ ureg = UnitRegistry()
+
+ p_arr = np.arange(10)*ureg.km/ureg.hr
+ yt_arr = YTArray(np.arange(10), "km/hr")
+ yt_arr2 = YTArray.from_pint(p_arr)
+
+ p_quan = 10.*ureg.megayear**0.5/(ureg.kpc**3)
+ yt_quan = YTQuantity(10., "sqrt(Myr)/kpc**3")
+ yt_quan2 = YTQuantity.from_pint(p_quan)
+
+ yield assert_array_equal, p_arr, yt_arr.to_pint()
+ yield assert_array_equal, yt_arr.in_cgs(), YTArray.from_pint(p_arr).in_cgs()
+ yield assert_array_equal, yt_arr.in_cgs(), yt_arr2.in_cgs()
+
+ yield assert_equal, p_quan, yt_quan.to_pint()
+ yield assert_equal, yt_quan.in_cgs(), YTQuantity.from_pint(p_quan).in_cgs()
+ yield assert_equal, yt_quan.in_cgs(), yt_quan2.in_cgs()
+
+ yield assert_array_equal, yt_arr.in_cgs(), YTArray.from_astropy(yt_arr.to_astropy()).in_cgs()
+ yield assert_equal, yt_quan.in_cgs(), YTQuantity.from_astropy(yt_quan.to_astropy()).in_cgs()
+
def test_subclass():
class YTASubclass(YTArray):
https://bitbucket.org/yt_analysis/yt/commits/3b57f43b7512/
Changeset: 3b57f43b7512
Branch: yt
User: jzuhone
Date: 2015-09-05 21:16:14+00:00
Summary: Fixing tests
Affected #: 2 files
diff -r c2183df1daf9042230ec168fbc6b144cacde9d61 -r 3b57f43b7512bb09e71b84461adcd9204d78984d yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -839,15 +839,17 @@
yt_arr = YTArray(np.arange(10), "km/hr")
yt_arr2 = YTArray.from_pint(p_arr)
- p_quan = 10.*ureg.megayear**0.5/(ureg.kpc**3)
- yt_quan = YTQuantity(10., "sqrt(Myr)/kpc**3")
+ p_quan = 10.*ureg.g**0.5/(ureg.mm**3)
+ yt_quan = YTQuantity(10., "sqrt(g)/mm**3")
yt_quan2 = YTQuantity.from_pint(p_quan)
yield assert_array_equal, p_arr, yt_arr.to_pint()
+ assert p_quan.units == yt_quan.to_pint().units
yield assert_array_equal, yt_arr.in_cgs(), YTArray.from_pint(p_arr).in_cgs()
yield assert_array_equal, yt_arr.in_cgs(), yt_arr2.in_cgs()
- yield assert_equal, p_quan, yt_quan.to_pint()
+ yield assert_equal, p_quan.magnitude, yt_quan.to_pint().magnitude
+ assert p_quan.units == yt_quan.to_pint().units
yield assert_equal, yt_quan.in_cgs(), YTQuantity.from_pint(p_quan).in_cgs()
yield assert_equal, yt_quan.in_cgs(), yt_quan2.in_cgs()
diff -r c2183df1daf9042230ec168fbc6b144cacde9d61 -r 3b57f43b7512bb09e71b84461adcd9204d78984d yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -697,7 +697,16 @@
from pint import UnitRegistry
if unit_registry is None:
unit_registry = UnitRegistry()
- return unit_registry.Quantity(self.value, str(self.units))
+ powers_dict = self.units.expr.as_powers_dict()
+ units = []
+ for unit, pow in powers_dict.items():
+ # we have to do this because Pint doesn't recognize
+ # "yr" as "year"
+ if str(unit).endswith("yr") and len(str(unit)) in [2,3]:
+ unit = str(unit).replace("yr","year")
+ units.append("%s**(%s)" % (unit, Rational(pow)))
+ units = "*".join(units)
+ return unit_registry.Quantity(self.value, units)
#
# End unit conversion methods
https://bitbucket.org/yt_analysis/yt/commits/1a1dddcd449f/
Changeset: 1a1dddcd449f
Branch: yt
User: jzuhone
Date: 2015-09-08 02:03:25+00:00
Summary: Add unit aliases
Affected #: 2 files
diff -r 3b57f43b7512bb09e71b84461adcd9204d78984d -r 1a1dddcd449fbcbd63047813aaa07be786fce835 yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -98,7 +98,7 @@
"degree": (np.pi/180., dimensions.angle), # degrees
"arcmin": (np.pi/10800., dimensions.angle), # arcminutes
"arcsec": (np.pi/648000., dimensions.angle), # arcseconds
- "mas": (np.pi/648000000., dimensions.angle), # millarcseconds
+ "mas": (np.pi/648000000., dimensions.angle), # milliarcseconds
"hourangle": (np.pi/12., dimensions.angle), # hour angle
"steradian": (1.0, dimensions.solid_angle),
@@ -112,20 +112,6 @@
"me": (mass_electron_grams, dimensions.mass),
"mol": (1.0/amu_grams, dimensions.dimensionless),
- # for AstroPy compatibility
- "solMass": (mass_sun_grams, dimensions.mass),
- "solRad": (cm_per_rsun, dimensions.length),
- "solLum": (luminosity_sun_ergs_per_sec, dimensions.power),
- "dyn": (1.0, dimensions.force),
- "sr": (1.0, dimensions.solid_angle),
- "rad": (1.0, dimensions.solid_angle),
- "deg": (np.pi/180., dimensions.angle),
- "Fr": (1.0, dimensions.charge_cgs),
- "G": (1.0, dimensions.magnetic_field_cgs),
- "d": (1.0, dimensions.time),
- "Angstrom": (cm_per_ang, dimensions.length),
- "statC": (1.0, dimensions.charge_cgs),
-
# Planck units
"m_pl": (planck_mass_grams, dimensions.mass),
"l_pl": (planck_length_cm, dimensions.length),
@@ -136,6 +122,49 @@
}
+unit_aliases = {
+ "meter":"m",
+ "second":"s",
+ "gram":"g",
+ "rad":"radian",
+ "deg":"degree",
+ "joule":"J",
+ "franklin":"esu",
+ "statC":"esu",
+ "dyn":"dyne",
+ "sr":"steradian",
+ "parsec":"pc",
+ "mole":"mol",
+ "d":"day",
+ "rankine":"R",
+ "solMass":"Msun",
+ "solRad":"Rsun",
+ "solLum":"Lsun",
+ "G":"gauss",
+ "watt":"W",
+ "pascal":"Pa",
+ "tesla":"T",
+ "kelvin":"K",
+ "year":"yr",
+ "minute":"min",
+ "Fr":"esu",
+ "Angstrom":"angstrom",
+ "hour":"hr",
+ "light_year":"ly",
+ "volt":"V",
+ "ampere":"A",
+ "astronomical_unit":"au",
+ "foot":"ft",
+ "atomic_mass_unit":"amu",
+ "esu_per_second":"statA",
+ "coulomb":"C",
+ "newton":"N",
+ "hertz":"Hz",
+ "speed_of_light":"c",
+ "jansky":"Jy",
+ "electron_mass":"me",
+}
+
# Add LaTeX representations for units with trivial representations.
latex_symbol_lut = {
"unitary" : r"",
@@ -181,6 +210,27 @@
'y': 1e-24, # yocto
}
+prefix_aliases = {
+ 'yotta':'Y',
+ 'zetta':'Z',
+ 'exa':'E',
+ 'peta':'P',
+ 'tera':'T',
+ 'giga':'G',
+ 'mega':'M',
+ 'kilo':'k',
+ 'deci':'d',
+ 'centi':'c',
+ 'milli':'m',
+ 'micro':'u',
+ 'nano':'n',
+ 'pico':'p',
+ 'femto':'f',
+ 'atto':'a',
+ 'zepto':'z',
+ 'yocto':'y',
+}
+
latex_prefixes = {
"u" : "\\mu",
}
diff -r 3b57f43b7512bb09e71b84461adcd9204d78984d -r 1a1dddcd449fbcbd63047813aaa07be786fce835 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -28,7 +28,8 @@
from yt.units.unit_lookup_table import \
latex_symbol_lut, unit_prefixes, \
prefixable_units, cgs_base_units, \
- mks_base_units, latex_prefixes, yt_base_units
+ mks_base_units, latex_prefixes, yt_base_units, \
+ unit_aliases, prefix_aliases
from yt.units.unit_registry import UnitRegistry
from yt.utilities.exceptions import YTUnitsNotReducible
@@ -520,12 +521,25 @@
# lookup successful, return the tuple directly
return unit_symbol_lut[symbol_str]
+ if symbol_str in unit_aliases:
+ # this is an alias for another unit, return its associated tuple
+ return unit_symbol_lut[unit_aliases[symbol_str]]
+
# could still be a known symbol with a prefix
- possible_prefix = symbol_str[0]
+ possible_prefix = None
+ for prefix in prefix_aliases:
+ if symbol_str.startswith(prefix):
+ possible_prefix = prefix_aliases[prefix]
+ symbol_start = len(prefix)
+ break
+ if possible_prefix is None:
+ possible_prefix = symbol_str[0]
+ symbol_start = 1
if possible_prefix in unit_prefixes:
# the first character could be a prefix, check the rest of the symbol
- symbol_wo_prefix = symbol_str[1:]
-
+ symbol_wo_prefix = symbol_str[symbol_start:]
+ if symbol_wo_prefix in unit_aliases:
+ symbol_wo_prefix = unit_aliases[symbol_wo_prefix]
if symbol_wo_prefix in unit_symbol_lut and symbol_wo_prefix in prefixable_units:
# lookup successful, it's a symbol with a prefix
unit_data = unit_symbol_lut[symbol_wo_prefix]
https://bitbucket.org/yt_analysis/yt/commits/6bdfc00e2ae6/
Changeset: 6bdfc00e2ae6
Branch: yt
User: jzuhone
Date: 2015-09-08 02:04:03+00:00
Summary: with the addition of unit aliases, we don't need this anymore
Affected #: 1 file
diff -r 1a1dddcd449fbcbd63047813aaa07be786fce835 -r 6bdfc00e2ae6eaae063b72953e64d91489c75cb7 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -618,26 +618,15 @@
return self.value*_astropy.units.Unit(str(self.units), **kwargs)
@classmethod
- def from_pint(cls, arr, unit_registry=None):
+ def from_pint(cls, arr):
"""
Convert a Pint "Quantity" to a YTArray or YTQuantity.
- Unfortunately, currently there is no straightforward way to convert
- from Pint Quantities to a YTArray in the same units. Therefore, most
- quantities will be returned in some equivalent combination of Pint's
- default base units of [meter,second,gram,radian,ampere,kelvin,mole].
- From there, the units can be converted in yt to whatever equivalent
- units one desires. In the case where the unit is simply degrees
- Celsius or Fahrenheit, the units will be passed over directly.
-
Parameters
----------
arr : Pint Quantity
The Quantity to convert from.
- unit_registry : Pint UnitRegistry, optional
- The Pint UnitRegistry to use in the conversion. If one is not
- supplied, the default one will be used.
-
+
Examples
--------
>>> from pint import UnitRegistry
@@ -645,37 +634,16 @@
>>> ureg = UnitRegistry()
>>> a = np.random.random(10)
>>> b = ureg.Quantity(a, "erg/cm**3")
- >>> c = yt.YTArray.from_pint(b, unit_registry=ureg)
+ >>> c = yt.YTArray.from_pint(b)
"""
- from pint import UnitRegistry
- if unit_registry is None:
- unit_registry = UnitRegistry()
- if str(arr.units) in ["degC","degF"]:
- factor = 1.0
- p_units = str(arr.units)
+ p_units = []
+ for base, power in arr.units.items():
+ p_units.append("%s**(%s)" % (base, Rational(power)))
+ p_units = "*".join(p_units)
+ if isinstance(arr.magnitude, np.ndarray):
+ return YTArray(arr.magnitude, p_units)
else:
- pint_to_yt = {"meter":"m",
- "ampere":"A",
- "second":"s",
- "radian":"rad",
- "gram":"g",
- "kelvin":"K",
- "mole":"mol"}
- base_units = unit_registry.get_base_units(arr.units)
- factor = base_units[0]
- base_units = base_units[1]
- p_units = []
- for base, power in base_units.items():
- unit_str = pint_to_yt.get(base, None)
- if unit_str is None:
- raise RuntimeError("Cannot parse base unit \"%s\" in Pint "
- "Quantity with units of %s!" % (base, arr.units))
- p_units.append("%s**(%s)" % (unit_str, Rational(power)))
- p_units = "*".join(p_units)
- if isinstance(arr.magnitude, np.ndarray):
- return factor*YTArray(arr.magnitude, p_units)
- else:
- return factor*YTQuantity(arr.magnitude, p_units)
+ return YTQuantity(arr.magnitude, p_units)
def to_pint(self, unit_registry=None):
"""
https://bitbucket.org/yt_analysis/yt/commits/e9117ff27b3b/
Changeset: e9117ff27b3b
Branch: yt
User: jzuhone
Date: 2015-09-08 02:06:52+00:00
Summary: Update tests
Affected #: 1 file
diff -r 6bdfc00e2ae6eaae063b72953e64d91489c75cb7 -r e9117ff27b3b615a1c49c57376d82c0223c5b16c yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -845,16 +845,16 @@
yield assert_array_equal, p_arr, yt_arr.to_pint()
assert p_quan.units == yt_quan.to_pint().units
- yield assert_array_equal, yt_arr.in_cgs(), YTArray.from_pint(p_arr).in_cgs()
- yield assert_array_equal, yt_arr.in_cgs(), yt_arr2.in_cgs()
+ yield assert_array_equal, yt_arr, YTArray.from_pint(p_arr)
+ yield assert_array_equal, yt_arr, yt_arr2
yield assert_equal, p_quan.magnitude, yt_quan.to_pint().magnitude
assert p_quan.units == yt_quan.to_pint().units
- yield assert_equal, yt_quan.in_cgs(), YTQuantity.from_pint(p_quan).in_cgs()
- yield assert_equal, yt_quan.in_cgs(), yt_quan2.in_cgs()
+ yield assert_equal, yt_quan, YTQuantity.from_pint(p_quan)
+ yield assert_equal, yt_quan, yt_quan2
- yield assert_array_equal, yt_arr.in_cgs(), YTArray.from_astropy(yt_arr.to_astropy()).in_cgs()
- yield assert_equal, yt_quan.in_cgs(), YTQuantity.from_astropy(yt_quan.to_astropy()).in_cgs()
+ yield assert_array_equal, yt_arr, YTArray.from_pint(yt_arr.to_pint())
+ yield assert_equal, yt_quan, YTQuantity.from_pint(yt_quan.to_pint())
def test_subclass():
https://bitbucket.org/yt_analysis/yt/commits/64dc5197bd78/
Changeset: 64dc5197bd78
Branch: yt
User: jzuhone
Date: 2015-09-08 12:28:39+00:00
Summary: Putting the hydrogen mass in physical_ratios.
Affected #: 2 files
diff -r e9117ff27b3b615a1c49c57376d82c0223c5b16c -r 64dc5197bd785bd5140b4733846092697809b3c1 yt/utilities/physical_constants.py
--- a/yt/utilities/physical_constants.py
+++ b/yt/utilities/physical_constants.py
@@ -4,7 +4,7 @@
mass_electron_cgs = YTQuantity(mass_electron_grams, 'g')
amu_cgs = YTQuantity(amu_grams, 'g')
-mass_hydrogen_cgs = 1.007947*amu_cgs
+mass_hydrogen_cgs = YTQuantity(mass_hydrogen_grams, 'g')
# Velocities
speed_of_light_cgs = YTQuantity(speed_of_light_cm_per_s, 'cm/s')
diff -r e9117ff27b3b615a1c49c57376d82c0223c5b16c -r 64dc5197bd785bd5140b4733846092697809b3c1 yt/utilities/physical_ratios.py
--- a/yt/utilities/physical_ratios.py
+++ b/yt/utilities/physical_ratios.py
@@ -10,6 +10,7 @@
# Elementary masses
mass_electron_grams = 9.10938291e-28
amu_grams = 1.660538921e-24
+mass_hydrogen_grams = 1.007947*amu_grams
# Solar values (see Mamajek 2012)
# https://sites.google.com/site/mamajeksstarnotes/bc-scale
https://bitbucket.org/yt_analysis/yt/commits/04a29201b278/
Changeset: 04a29201b278
Branch: yt
User: jzuhone
Date: 2015-09-08 12:28:59+00:00
Summary: re-arranging units so that the abbreviated versions are the defaults
Affected #: 1 file
diff -r 64dc5197bd785bd5140b4733846092697809b3c1 -r 04a29201b278d01ff55239f42ea9270ce9b638bb yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -19,9 +19,9 @@
sec_per_min, temp_sun_kelvin, luminosity_sun_ergs_per_sec, \
metallicity_sun, erg_per_eV, amu_grams, mass_electron_grams, \
cm_per_ang, jansky_cgs, mass_jupiter_grams, mass_earth_grams, \
- boltzmann_constant_erg_per_K, kelvin_per_rankine, \
+ mass_hydrogen_grams, kelvin_per_rankine, planck_time_s, \
speed_of_light_cm_per_s, planck_length_cm, planck_charge_esu, \
- planck_energy_erg, planck_mass_grams, planck_temperature_K, planck_time_s
+ planck_energy_erg, planck_mass_grams, planck_temperature_K
import numpy as np
# Lookup a unit symbol with the symbol string, and provide a tuple with the
@@ -33,13 +33,13 @@
#"cm": (1.0, length, r"\rm{cm}"), # duplicate with meter below...
"s": (1.0, dimensions.time),
"K": (1.0, dimensions.temperature),
- "radian": (1.0, dimensions.angle),
+ "rad": (1.0, dimensions.angle),
# other cgs
"dyne": (1.0, dimensions.force),
"erg": (1.0, dimensions.energy),
"esu": (1.0, dimensions.charge_cgs),
- "gauss": (1.0, dimensions.magnetic_field_cgs),
+ "G": (1.0, dimensions.magnetic_field_cgs),
"degC": (1.0, dimensions.temperature, -273.15),
"statA": (1.0, dimensions.current_cgs),
"statV": (1.0, dimensions.electric_potential_cgs),
@@ -95,12 +95,12 @@
"pc": (cm_per_pc, dimensions.length),
# angles
- "degree": (np.pi/180., dimensions.angle), # degrees
+ "deg": (np.pi/180., dimensions.angle), # degrees
"arcmin": (np.pi/10800., dimensions.angle), # arcminutes
"arcsec": (np.pi/648000., dimensions.angle), # arcseconds
"mas": (np.pi/648000000., dimensions.angle), # milliarcseconds
"hourangle": (np.pi/12., dimensions.angle), # hour angle
- "steradian": (1.0, dimensions.solid_angle),
+ "sr": (1.0, dimensions.solid_angle),
# misc
"eV": (erg_per_eV, dimensions.energy),
@@ -110,6 +110,7 @@
"counts": (1.0, dimensions.dimensionless),
"photons": (1.0, dimensions.dimensionless),
"me": (mass_electron_grams, dimensions.mass),
+ "mp": (mass_hydrogen_grams, dimensions.mass),
"mol": (1.0/amu_grams, dimensions.dimensionless),
# Planck units
@@ -126,13 +127,13 @@
"meter":"m",
"second":"s",
"gram":"g",
- "rad":"radian",
- "deg":"degree",
+ "radian":"rad",
+ "degree":"deg",
"joule":"J",
"franklin":"esu",
"statC":"esu",
"dyn":"dyne",
- "sr":"steradian",
+ "steradian":"sr",
"parsec":"pc",
"mole":"mol",
"d":"day",
@@ -140,7 +141,7 @@
"solMass":"Msun",
"solRad":"Rsun",
"solLum":"Lsun",
- "G":"gauss",
+ "gauss":"G",
"watt":"W",
"pascal":"Pa",
"tesla":"T",
@@ -163,6 +164,9 @@
"speed_of_light":"c",
"jansky":"Jy",
"electron_mass":"me",
+ "proton_mass":"mp",
+ "arcsecond":"arcsec",
+ "arcminute":"arcmin",
}
# Add LaTeX representations for units with trivial representations.
@@ -264,6 +268,7 @@
"statV",
"ohm",
"statohm",
+ "arcsec",
)
yt_base_units = {
@@ -271,7 +276,7 @@
dimensions.length:'cm',
dimensions.time:'s',
dimensions.temperature:'K',
- dimensions.angle:'radian',
+ dimensions.angle:'rad',
dimensions.current_mks:'A',
}
@@ -283,6 +288,6 @@
dimensions.length:'m',
dimensions.time:'s',
dimensions.temperature:'K',
- dimensions.angle:'radian',
+ dimensions.angle:'rad',
dimensions.current_mks:'A',
}
https://bitbucket.org/yt_analysis/yt/commits/2b362ea0150b/
Changeset: 2b362ea0150b
Branch: yt
User: jzuhone
Date: 2015-09-08 21:36:47+00:00
Summary: Adding info about pint conversions to the docs
Affected #: 1 file
diff -r 04a29201b278d01ff55239f42ea9270ce9b638bb -r 2b362ea0150b887b3a7c4e88b180fcad20f27753 doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
--- a/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
+++ b/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
- "signature": "sha256:4d19ee42177c60fb4b39550b5acd7a0f7e97f59f5c2da3565ff42cdd580454b0"
+ "signature": "sha256:6a06d5720eb6316ac0d322ef0898ec20f33d65ea3eeeacef35ae1d869af12607"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -352,7 +352,7 @@
"level": 3,
"metadata": {},
"source": [
- "Round-Trip Conversions to and from AstroPy's Units System"
+ "Round-Trip Conversions to and from Other Unit Systems"
]
},
{
@@ -503,6 +503,58 @@
"language": "python",
"metadata": {},
"outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We can also do the same thing with unitful quantities from the [Pint package](http://pint.readthedocs.org), using essentially the same procedure:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from pint import UnitRegistry\n",
+ "ureg = UnitRegistry()\n",
+ "v = 1000.*ureg.km/ureg.s\n",
+ "w = yt.YTQuantity.from_pint(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print v, type(v)\n",
+ "print w, type(w)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ptemp = temp.to_pint()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print temp, type(temp)\n",
+ "print ptemp, type(ptemp)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
}
],
"metadata": {}
https://bitbucket.org/yt_analysis/yt/commits/193a64ea0b5a/
Changeset: 193a64ea0b5a
Branch: yt
User: jzuhone
Date: 2015-09-10 00:45:50+00:00
Summary: Merge
Affected #: 7 files
diff -r 47da0c1a0142b30d6ff793f84090761fca977f67 -r 193a64ea0b5af80fb9209acac83b596292857887 doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
--- a/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
+++ b/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
- "signature": "sha256:4d19ee42177c60fb4b39550b5acd7a0f7e97f59f5c2da3565ff42cdd580454b0"
+ "signature": "sha256:6a06d5720eb6316ac0d322ef0898ec20f33d65ea3eeeacef35ae1d869af12607"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -352,7 +352,7 @@
"level": 3,
"metadata": {},
"source": [
- "Round-Trip Conversions to and from AstroPy's Units System"
+ "Round-Trip Conversions to and from Other Unit Systems"
]
},
{
@@ -503,6 +503,58 @@
"language": "python",
"metadata": {},
"outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We can also do the same thing with unitful quantities from the [Pint package](http://pint.readthedocs.org), using essentially the same procedure:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from pint import UnitRegistry\n",
+ "ureg = UnitRegistry()\n",
+ "v = 1000.*ureg.km/ureg.s\n",
+ "w = yt.YTQuantity.from_pint(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print v, type(v)\n",
+ "print w, type(w)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ptemp = temp.to_pint()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print temp, type(temp)\n",
+ "print ptemp, type(ptemp)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
}
],
"metadata": {}
diff -r 47da0c1a0142b30d6ff793f84090761fca977f67 -r 193a64ea0b5af80fb9209acac83b596292857887 yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -837,6 +837,33 @@
yield assert_array_equal, yt_arr, YTArray.from_astropy(yt_arr.to_astropy())
yield assert_equal, yt_quan, YTQuantity.from_astropy(yt_quan.to_astropy())
+ at requires_module("pint")
+def test_pint():
+ from pint import UnitRegistry
+
+ ureg = UnitRegistry()
+
+ p_arr = np.arange(10)*ureg.km/ureg.hr
+ yt_arr = YTArray(np.arange(10), "km/hr")
+ yt_arr2 = YTArray.from_pint(p_arr)
+
+ p_quan = 10.*ureg.g**0.5/(ureg.mm**3)
+ yt_quan = YTQuantity(10., "sqrt(g)/mm**3")
+ yt_quan2 = YTQuantity.from_pint(p_quan)
+
+ yield assert_array_equal, p_arr, yt_arr.to_pint()
+ assert p_quan.units == yt_quan.to_pint().units
+ yield assert_array_equal, yt_arr, YTArray.from_pint(p_arr)
+ yield assert_array_equal, yt_arr, yt_arr2
+
+ yield assert_equal, p_quan.magnitude, yt_quan.to_pint().magnitude
+ assert p_quan.units == yt_quan.to_pint().units
+ yield assert_equal, yt_quan, YTQuantity.from_pint(p_quan)
+ yield assert_equal, yt_quan, yt_quan2
+
+ yield assert_array_equal, yt_arr, YTArray.from_pint(yt_arr.to_pint())
+ yield assert_equal, yt_quan, YTQuantity.from_pint(yt_quan.to_pint())
+
def test_subclass():
class YTASubclass(YTArray):
diff -r 47da0c1a0142b30d6ff793f84090761fca977f67 -r 193a64ea0b5af80fb9209acac83b596292857887 yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -21,7 +21,7 @@
cm_per_ang, jansky_cgs, mass_jupiter_grams, mass_earth_grams, \
kelvin_per_rankine, speed_of_light_cm_per_s, planck_length_cm, \
planck_charge_esu, planck_energy_erg, planck_mass_grams, \
- planck_temperature_K, planck_time_s
+ planck_temperature_K, planck_time_s, mass_hydrogen_grams
import numpy as np
# Lookup a unit symbol with the symbol string, and provide a tuple with the
@@ -32,13 +32,13 @@
"g": (1.0, dimensions.mass, 0.0, r"\rm{g}"),
"s": (1.0, dimensions.time, 0.0, r"\rm{s}"),
"K": (1.0, dimensions.temperature, 0.0, r"\rm{K}"),
- "radian": (1.0, dimensions.angle, 0.0, r"\rm{radian}"),
+ "rad": (1.0, dimensions.angle, 0.0, r"\rm{radian}"),
# other cgs
- "dyne": (1.0, dimensions.force, 0.0, r"\rm{dyn}"),
+ "dyn": (1.0, dimensions.force, 0.0, r"\rm{dyn}"),
"erg": (1.0, dimensions.energy, 0.0, r"\rm{erg}"),
"esu": (1.0, dimensions.charge_cgs, 0.0, r"\rm{esu}"),
- "gauss": (1.0, dimensions.magnetic_field_cgs, 0.0, r"\rm{G}"),
+ "G": (1.0, dimensions.magnetic_field_cgs, 0.0, r"\rm{G}"),
"degC": (1.0, dimensions.temperature, -273.15, r"^\circ\rm{C}"),
"statA": (1.0, dimensions.current_cgs, 0.0, r"\rm{statA}"),
"statV": (1.0, dimensions.electric_potential_cgs, 0.0, r"\rm{statV}"),
@@ -95,15 +95,15 @@
"pc": (cm_per_pc, dimensions.length, 0.0, r"\rm{pc}"),
# angles
- "degree": (np.pi/180., dimensions.angle, 0.0, r"\rm{deg}"), # degrees
+ "deg": (np.pi/180., dimensions.angle, 0.0, r"\rm{deg}"), # degrees
"arcmin": (np.pi/10800., dimensions.angle, 0.0,
r"\rm{arcmin}"), # arcminutes
"arcsec": (np.pi/648000., dimensions.angle, 0.0,
r"\rm{arcsec}"), # arcseconds
"mas": (np.pi/648000000., dimensions.angle, 0.0,
- r"\rm{mas}"), # millarcseconds
- "hourangle": (np.pi/12., dimensions.angle, 0.0, r"\rm{HA}"), # hour angle
- "steradian": (1.0, dimensions.solid_angle, 0.0, r"\rm{sr}"),
+ r"\rm{mas}"), # milliarcseconds
+ "HA": (np.pi/12., dimensions.angle, 0.0, r"\rm{HA}"), # hour angle
+ "sr": (1.0, dimensions.solid_angle, 0.0, r"\rm{sr}"),
# misc
"eV": (erg_per_eV, dimensions.energy, 0.0, r"\rm{eV}"),
@@ -113,20 +113,8 @@
"counts": (1.0, dimensions.dimensionless, 0.0, r"\rm{counts}"),
"photons": (1.0, dimensions.dimensionless, 0.0, r"\rm{photons}"),
"me": (mass_electron_grams, dimensions.mass, 0.0, r"m_e"),
-
- # for AstroPy compatibility
- "solMass": (mass_sun_grams, dimensions.mass, 0.0, r"M_\odot"),
- "solRad": (cm_per_rsun, dimensions.length, 0.0, r"R_\odot"),
- "solLum": (luminosity_sun_ergs_per_sec, dimensions.power, 0.0, r"L_\odot"),
- "dyn": (1.0, dimensions.force, 0.0, r"\rm{dyn}"),
- "sr": (1.0, dimensions.solid_angle, 0.0, r"\rm{sr}"),
- "rad": (1.0, dimensions.solid_angle, 0.0, r"\rm{rad}"),
- "deg": (np.pi/180., dimensions.angle, 0.0, r"\rm{deg}"),
- "Fr": (1.0, dimensions.charge_cgs, 0.0, r"\rm{Fr}"),
- "G": (1.0, dimensions.magnetic_field_cgs, 0.0, r"\rm{G}"),
- "d": (1.0, dimensions.time, 0.0, r"\rm{d}"),
- "Angstrom": (cm_per_ang, dimensions.length, 0.0, r"\AA"),
- "statC": (1.0, dimensions.charge_cgs, 0.0, r"\rm{statC}"),
+ "mp": (mass_hydrogen_grams, dimensions.mass, 0.0, r"m_p"),
+ "mol": (1.0/amu_grams, dimensions.dimensionless, r"\rm{mol}"),
# Planck units
"m_pl": (planck_mass_grams, dimensions.mass, 0.0, r"m_{\rm{P}}"),
@@ -138,6 +126,53 @@
}
+unit_aliases = {
+ "meter":"m",
+ "second":"s",
+ "gram":"g",
+ "radian":"rad",
+ "degree":"deg",
+ "joule":"J",
+ "franklin":"esu",
+ "statC":"esu",
+ "dyne":"dyn",
+ "steradian":"sr",
+ "parsec":"pc",
+ "mole":"mol",
+ "d":"day",
+ "rankine":"R",
+ "solMass":"Msun",
+ "solRad":"Rsun",
+ "solLum":"Lsun",
+ "gauss":"G",
+ "watt":"W",
+ "pascal":"Pa",
+ "tesla":"T",
+ "kelvin":"K",
+ "year":"yr",
+ "minute":"min",
+ "Fr":"esu",
+ "Angstrom":"angstrom",
+ "hour":"hr",
+ "light_year":"ly",
+ "volt":"V",
+ "ampere":"A",
+ "astronomical_unit":"au",
+ "foot":"ft",
+ "atomic_mass_unit":"amu",
+ "esu_per_second":"statA",
+ "coulomb":"C",
+ "newton":"N",
+ "hertz":"Hz",
+ "speed_of_light":"c",
+ "jansky":"Jy",
+ "electron_mass":"me",
+ "proton_mass":"mp",
+ "arcsecond":"arcsec",
+ "arcminute":"arcmin",
+ "hourangle":"HA",
+}
+
# This dictionary formatting from magnitude package, credit to Juan Reyero.
unit_prefixes = {
'Y': 1e24, # yotta
@@ -160,6 +195,27 @@
'y': 1e-24, # yocto
}
+prefix_aliases = {
+ 'yotta':'Y',
+ 'zetta':'Z',
+ 'exa':'E',
+ 'peta':'P',
+ 'tera':'T',
+ 'giga':'G',
+ 'mega':'M',
+ 'kilo':'k',
+ 'deci':'d',
+ 'centi':'c',
+ 'milli':'m',
+ 'micro':'u',
+ 'nano':'n',
+ 'pico':'p',
+ 'femto':'f',
+ 'atto':'a',
+ 'zepto':'z',
+ 'yocto':'y',
+}
+
latex_prefixes = {
"u": r"\mu",
}
@@ -174,13 +230,12 @@
"s",
"yr",
"K",
- "dyne",
+ "dyn",
"erg",
"esu",
"J",
"Hz",
"W",
- "gauss",
"G",
"Jy",
"N",
@@ -193,6 +248,7 @@
"statV",
"ohm",
"statohm",
+ "arcsec",
)
yt_base_units = {
@@ -200,7 +256,7 @@
dimensions.length: 'cm',
dimensions.time: 's',
dimensions.temperature: 'K',
- dimensions.angle: 'radian',
+ dimensions.angle: 'rad',
dimensions.current_mks: 'A',
}
@@ -212,6 +268,6 @@
dimensions.length: 'm',
dimensions.time: 's',
dimensions.temperature: 'K',
- dimensions.angle: 'radian',
+ dimensions.angle: 'rad',
dimensions.current_mks: 'A',
}
diff -r 47da0c1a0142b30d6ff793f84090761fca977f67 -r 193a64ea0b5af80fb9209acac83b596292857887 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -27,7 +27,8 @@
dimensionless, current_mks
from yt.units.unit_lookup_table import \
unit_prefixes, prefixable_units, cgs_base_units, \
- mks_base_units, latex_prefixes, yt_base_units
+ mks_base_units, latex_prefixes, yt_base_units, \
+ unit_aliases, prefix_aliases
from yt.units.unit_registry import UnitRegistry
from yt.utilities.exceptions import YTUnitsNotReducible
@@ -538,11 +539,25 @@
# lookup successful, return the tuple directly
return unit_symbol_lut[symbol_str]
+ if symbol_str in unit_aliases:
+ # this is an alias for another unit, return its associated tuple
+ return unit_symbol_lut[unit_aliases[symbol_str]]
+
# could still be a known symbol with a prefix
- possible_prefix = symbol_str[0]
+ possible_prefix = None
+ for prefix in prefix_aliases:
+ if symbol_str.startswith(prefix):
+ possible_prefix = prefix_aliases[prefix]
+ symbol_start = len(prefix)
+ break
+ if possible_prefix is None:
+ possible_prefix = symbol_str[0]
+ symbol_start = 1
if possible_prefix in unit_prefixes:
# the first character could be a prefix, check the rest of the symbol
- symbol_wo_prefix = symbol_str[1:]
+ symbol_wo_prefix = symbol_str[symbol_start:]
+ if symbol_wo_prefix in unit_aliases:
+ symbol_wo_prefix = unit_aliases[symbol_wo_prefix]
unit_is_si_prefixable = (symbol_wo_prefix in unit_symbol_lut and
symbol_wo_prefix in prefixable_units)
diff -r 47da0c1a0142b30d6ff793f84090761fca977f67 -r 193a64ea0b5af80fb9209acac83b596292857887 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -644,6 +644,65 @@
"an AstroPy quantity.")
return self.value*_astropy.units.Unit(str(self.units), **kwargs)
+ @classmethod
+ def from_pint(cls, arr):
+ """
+ Convert a Pint "Quantity" to a YTArray or YTQuantity.
+
+ Parameters
+ ----------
+ arr : Pint Quantity
+ The Quantity to convert from.
+
+ Examples
+ --------
+ >>> from pint import UnitRegistry
+ >>> import numpy as np
+ >>> ureg = UnitRegistry()
+ >>> a = np.random.random(10)
+ >>> b = ureg.Quantity(a, "erg/cm**3")
+ >>> c = yt.YTArray.from_pint(b)
+ """
+ p_units = []
+ for base, power in arr.units.items():
+ p_units.append("%s**(%s)" % (base, Rational(power)))
+ p_units = "*".join(p_units)
+ if isinstance(arr.magnitude, np.ndarray):
+ return YTArray(arr.magnitude, p_units)
+ else:
+ return YTQuantity(arr.magnitude, p_units)
+
+ def to_pint(self, unit_registry=None):
+ """
+ Convert a YTArray or YTQuantity to a Pint Quantity.
+
+ Parameters
+ ----------
+ arr : YTArray or YTQuantity
+ The unitful quantity to convert from.
+ unit_registry : Pint UnitRegistry, optional
+ The Pint UnitRegistry to use in the conversion. If one is not
+ supplied, the default one will be used.
+
+ Examples
+ --------
+ >>> a = YTQuantity(4.0, "cm**2/s")
+ >>> b = a.to_pint()
+ """
+ from pint import UnitRegistry
+ if unit_registry is None:
+ unit_registry = UnitRegistry()
+ powers_dict = self.units.expr.as_powers_dict()
+ units = []
+ for unit, pow in powers_dict.items():
+ # we have to do this because Pint doesn't recognize
+ # "yr" as "year"
+ if str(unit).endswith("yr") and len(str(unit)) in [2,3]:
+ unit = str(unit).replace("yr","year")
+ units.append("%s**(%s)" % (unit, Rational(pow)))
+ units = "*".join(units)
+ return unit_registry.Quantity(self.value, units)
+
#
# End unit conversion methods
#
diff -r 47da0c1a0142b30d6ff793f84090761fca977f67 -r 193a64ea0b5af80fb9209acac83b596292857887 yt/utilities/physical_constants.py
--- a/yt/utilities/physical_constants.py
+++ b/yt/utilities/physical_constants.py
@@ -10,7 +10,7 @@
amu = amu_cgs
Na = 1 / amu_cgs
-mass_hydrogen_cgs = 1.007947*amu_cgs
+mass_hydrogen_cgs = YTQuantity(mass_hydrogen_grams, 'g')
mass_hydrogen = mass_hydrogen_cgs
mp = mass_hydrogen_cgs
mh = mp
diff -r 47da0c1a0142b30d6ff793f84090761fca977f67 -r 193a64ea0b5af80fb9209acac83b596292857887 yt/utilities/physical_ratios.py
--- a/yt/utilities/physical_ratios.py
+++ b/yt/utilities/physical_ratios.py
@@ -10,6 +10,7 @@
# Elementary masses
mass_electron_grams = 9.10938291e-28
amu_grams = 1.660538921e-24
+mass_hydrogen_grams = 1.007947*amu_grams
# Solar values (see Mamajek 2012)
# https://sites.google.com/site/mamajeksstarnotes/bc-scale
https://bitbucket.org/yt_analysis/yt/commits/4059a8a82b90/
Changeset: 4059a8a82b90
Branch: yt
User: jzuhone
Date: 2015-09-10 01:37:29+00:00
Summary: Handle this in __new__ instead
Affected #: 1 file
diff -r 193a64ea0b5af80fb9209acac83b596292857887 -r 4059a8a82b9017153e60cbdd039405b1d1c5cd8c yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -127,50 +127,52 @@
}
unit_aliases = {
- "meter":"m",
- "second":"s",
- "gram":"g",
- "radian":"rad",
- "degree":"deg",
- "joule":"J",
- "franklin":"esu",
- "statC":"esu",
- "dyne":"dyn",
- "steradian":"sr",
- "parsec":"pc",
- "mole":"mol",
- "d":"day",
- "rankine":"R",
- "solMass":"Msun",
- "solRad":"Rsun",
- "solLum":"Lsun",
- "gauss":"G",
- "watt":"W",
- "pascal":"Pa",
- "tesla":"T",
- "kelvin":"K",
- "year":"yr",
- "minute":"min",
- "Fr":"esu",
- "Angstrom":"angstrom",
- "hour":"hr",
- "light_year":"ly",
- "volt":"V",
- "ampere":"A",
- "astronomical_unit":"au",
- "foot":"ft",
- "atomic_mass_unit":"amu",
- "esu_per_second":"statA",
- "coulomb":"C",
- "newton":"N",
- "hertz":"Hz",
- "speed_of_light":"c",
- "jansky":"Jy",
- "electron_mass":"me",
- "proton_mass":"mp",
- "arcsecond":"arcsec",
- "arcminute":"arcmin",
- "hourangle":"HA",
+ # full name aliases
+ "meter": "m",
+ "second": "s",
+ "gram": "g",
+ "radian": "rad",
+ "degree": "deg",
+ "joule": "J",
+ "franklin": "esu",
+ "dyne": "dyn",
+ "steradian": "sr",
+ "parsec": "pc",
+ "mole": "mol",
+ "rankine": "R",
+ "gauss": "G",
+ "watt": "W",
+ "pascal": "Pa",
+ "tesla": "T",
+ "kelvin": "K",
+ "year": "yr",
+ "minute": "min",
+ "hour": "hr",
+ "volt": "V",
+ "ampere": "A",
+ "foot": "ft",
+ "coulomb": "C",
+ "newton": "N",
+ "hertz": "Hz",
+ "jansky": "Jy",
+ "arcsecond": "arcsec",
+ "arcminute": "arcmin",
+ "hourangle": "HA",
+ # other aliases
+ "statC": "esu",
+ "solMass": "Msun",
+ "solRad": "Rsun",
+ "solLum": "Lsun",
+ "d": "day",
+ "speed_of_light": "c",
+ "esu_per_second": "statA",
+ "atomic_mass_unit": "amu",
+ "astronomical_unit": "au",
+ "Fr": "esu",
+ "Angstrom": "angstrom",
+ "light_year": "ly",
+ "electron_mass": "me",
+ "proton_mass": "mp",
}
# This dictionary formatting from magnitude package, credit to Juan Reyero.
https://bitbucket.org/yt_analysis/yt/commits/dadb36eb0725/
Changeset: dadb36eb0725
Branch: yt
User: jzuhone
Date: 2015-09-10 04:16:20+00:00
Summary: Refactoring this--only pint needs to know about the aliases.
Affected #: 4 files
diff -r 4059a8a82b9017153e60cbdd039405b1d1c5cd8c -r dadb36eb0725b5dd9db456c15e3b8b1141ac3760 yt/units/misc.py
--- /dev/null
+++ b/yt/units/misc.py
@@ -0,0 +1,88 @@
+"""
+Miscellaneous units-related stuff
+
+
+"""
+from __future__ import print_function
+#-----------------------------------------------------------------------------
+# Copyright (c) 2015, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+pint_aliases = {
+ "meter": "m",
+ "second": "s",
+ "gram": "g",
+ "joule": "J",
+ "franklin": "esu",
+ "dyne": "dyn",
+ "parsec": "pc",
+ "mole": "mol",
+ "rankine": "R",
+ "gauss": "G",
+ "watt": "W",
+ "pascal": "Pa",
+ "tesla": "T",
+ "kelvin": "K",
+ "year": "yr",
+ "minute": "min",
+ "hour": "hr",
+ "volt": "V",
+ "ampere": "A",
+ "foot": "ft",
+ "coulomb": "C",
+ "newton": "N",
+ "hertz": "Hz",
+ "jansky": "Jy",
+ "arcsecond": "arcsec",
+ "arcminute": "arcmin",
+ "speed_of_light": "c",
+ "esu_per_second": "statA",
+ "atomic_mass_unit": "amu",
+ "astronomical_unit": "au",
+ "light_year": "ly",
+ "electron_mass": "me",
+ "proton_mass": "mp",
+}
+
+pint_prefixes = {
+ 'yotta':'Y',
+ 'zetta':'Z',
+ 'exa':'E',
+ 'peta':'P',
+ 'tera':'T',
+ 'giga':'G',
+ 'mega':'M',
+ 'kilo':'k',
+ 'deci':'d',
+ 'centi':'c',
+ 'milli':'m',
+ 'micro':'u',
+ 'nano':'n',
+ 'pico':'p',
+ 'femto':'f',
+ 'atto':'a',
+ 'zepto':'z',
+ 'yocto':'y',
+}
+
+def convert_pint_units(unit_expr):
+ uexpr = unit_expr
+ pfx = ''
+ for prefix in pint_prefixes:
+ if unit_expr.startswith(prefix):
+ pfx = pint_prefixes[prefix]
+ uexpr = uexpr[len(prefix):]
+ break
+ if uexpr in pint_aliases:
+ uexpr = pint_aliases[uexpr]
+ if pfx == '':
+ return uexpr
+ else:
+ return pfx+uexpr
+ # If we can't figure it out just pass it and see
+ # what happens
+ return unit_expr
diff -r 4059a8a82b9017153e60cbdd039405b1d1c5cd8c -r dadb36eb0725b5dd9db456c15e3b8b1141ac3760 yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -32,13 +32,13 @@
"g": (1.0, dimensions.mass, 0.0, r"\rm{g}"),
"s": (1.0, dimensions.time, 0.0, r"\rm{s}"),
"K": (1.0, dimensions.temperature, 0.0, r"\rm{K}"),
- "rad": (1.0, dimensions.angle, 0.0, r"\rm{radian}"),
+ "radian": (1.0, dimensions.angle, 0.0, r"\rm{radian}"),
# other cgs
- "dyn": (1.0, dimensions.force, 0.0, r"\rm{dyn}"),
+ "dyne": (1.0, dimensions.force, 0.0, r"\rm{dyn}"),
"erg": (1.0, dimensions.energy, 0.0, r"\rm{erg}"),
"esu": (1.0, dimensions.charge_cgs, 0.0, r"\rm{esu}"),
- "G": (1.0, dimensions.magnetic_field_cgs, 0.0, r"\rm{G}"),
+ "gauss": (1.0, dimensions.magnetic_field_cgs, 0.0, r"\rm{G}"),
"degC": (1.0, dimensions.temperature, -273.15, r"^\circ\rm{C}"),
"statA": (1.0, dimensions.current_cgs, 0.0, r"\rm{statA}"),
"statV": (1.0, dimensions.electric_potential_cgs, 0.0, r"\rm{statV}"),
@@ -95,15 +95,15 @@
"pc": (cm_per_pc, dimensions.length, 0.0, r"\rm{pc}"),
# angles
- "deg": (np.pi/180., dimensions.angle, 0.0, r"\rm{deg}"), # degrees
+ "degree": (np.pi/180., dimensions.angle, 0.0, r"\rm{deg}"), # degrees
"arcmin": (np.pi/10800., dimensions.angle, 0.0,
r"\rm{arcmin}"), # arcminutes
"arcsec": (np.pi/648000., dimensions.angle, 0.0,
r"\rm{arcsec}"), # arcseconds
"mas": (np.pi/648000000., dimensions.angle, 0.0,
r"\rm{mas}"), # milliarcseconds
- "HA": (np.pi/12., dimensions.angle, 0.0, r"\rm{HA}"), # hour angle
- "sr": (1.0, dimensions.solid_angle, 0.0, r"\rm{sr}"),
+ "hourangle": (np.pi/12., dimensions.angle, 0.0, r"\rm{HA}"), # hour angle
+ "steradian": (1.0, dimensions.solid_angle, 0.0, r"\rm{sr}"),
# misc
"eV": (erg_per_eV, dimensions.energy, 0.0, r"\rm{eV}"),
@@ -116,6 +116,20 @@
"mp": (mass_hydrogen_grams, dimensions.mass, 0.0, r"m_p"),
"mol": (1.0/amu_grams, dimensions.dimensionless, r"\rm{mol}"),
+ # for AstroPy compatibility
+ "solMass": (mass_sun_grams, dimensions.mass, 0.0, r"M_\odot"),
+ "solRad": (cm_per_rsun, dimensions.length, 0.0, r"R_\odot"),
+ "solLum": (luminosity_sun_ergs_per_sec, dimensions.power, 0.0, r"L_\odot"),
+ "dyn": (1.0, dimensions.force, 0.0, r"\rm{dyn}"),
+ "sr": (1.0, dimensions.solid_angle, 0.0, r"\rm{sr}"),
+ "rad": (1.0, dimensions.solid_angle, 0.0, r"\rm{rad}"),
+ "deg": (np.pi/180., dimensions.angle, 0.0, r"\rm{deg}"),
+ "Fr": (1.0, dimensions.charge_cgs, 0.0, r"\rm{Fr}"),
+ "G": (1.0, dimensions.magnetic_field_cgs, 0.0, r"\rm{G}"),
+ "d": (1.0, dimensions.time, 0.0, r"\rm{d}"),
+ "Angstrom": (cm_per_ang, dimensions.length, 0.0, r"\AA"),
+ "statC": (1.0, dimensions.charge_cgs, 0.0, r"\rm{statC}"),
+
# Planck units
"m_pl": (planck_mass_grams, dimensions.mass, 0.0, r"m_{\rm{P}}"),
"l_pl": (planck_length_cm, dimensions.length, 0.0, r"\ell_\rm{P}"),
@@ -126,55 +140,6 @@
}
-unit_aliases = {
- # full name aliases
- "meter": "m",
- "second": "s",
- "gram": "g",
- "radian": "rad",
- "degree": "deg",
- "joule": "J",
- "franklin": "esu",
- "dyne": "dyn",
- "steradian": "sr",
- "parsec": "pc",
- "mole": "mol",
- "rankine": "R",
- "gauss": "G",
- "watt": "W",
- "pascal": "Pa",
- "tesla": "T",
- "kelvin": "K",
- "year": "yr",
- "minute": "min",
- "hour": "hr",
- "volt": "V",
- "ampere": "A",
- "foot": "ft",
- "coulomb": "C",
- "newton": "N",
- "hertz": "Hz",
- "jansky": "Jy",
- "arcsecond": "arcsec",
- "arcminute": "arcmin",
- "hourangle": "HA",
- # other aliases
- "statC": "esu",
- "solMass": "Msun",
- "solRad": "Rsun",
- "solLum": "Lsun",
- "d": "day",
- "speed_of_light": "c",
- "esu_per_second": "statA",
- "atomic_mass_unit": "amu",
- "astronomical_unit": "au",
- "Fr": "esu",
- "Angstrom": "angstrom",
- "light_year": "ly",
- "electron_mass": "me",
- "proton_mass": "mp",
-}
-
# This dictionary formatting from magnitude package, credit to Juan Reyero.
unit_prefixes = {
'Y': 1e24, # yotta
@@ -197,27 +162,6 @@
'y': 1e-24, # yocto
}
-prefix_aliases = {
- 'yotta':'Y',
- 'zetta':'Z',
- 'exa':'E',
- 'peta':'P',
- 'tera':'T',
- 'giga':'G',
- 'mega':'M',
- 'kilo':'k',
- 'deci':'d',
- 'centi':'c',
- 'milli':'m',
- 'micro':'u',
- 'nano':'n',
- 'pico':'p',
- 'femto':'f',
- 'atto':'a',
- 'zepto':'z',
- 'yocto':'y',
-}
-
latex_prefixes = {
"u": r"\mu",
}
@@ -232,12 +176,13 @@
"s",
"yr",
"K",
- "dyn",
+ "dyne",
"erg",
"esu",
"J",
"Hz",
"W",
+ "gauss",
"G",
"Jy",
"N",
@@ -258,7 +203,7 @@
dimensions.length: 'cm',
dimensions.time: 's',
dimensions.temperature: 'K',
- dimensions.angle: 'rad',
+ dimensions.angle: 'radian',
dimensions.current_mks: 'A',
}
@@ -270,6 +215,6 @@
dimensions.length: 'm',
dimensions.time: 's',
dimensions.temperature: 'K',
- dimensions.angle: 'rad',
+ dimensions.angle: 'radian',
dimensions.current_mks: 'A',
}
diff -r 4059a8a82b9017153e60cbdd039405b1d1c5cd8c -r dadb36eb0725b5dd9db456c15e3b8b1141ac3760 yt/units/unit_object.py
--- a/yt/units/unit_object.py
+++ b/yt/units/unit_object.py
@@ -27,8 +27,7 @@
dimensionless, current_mks
from yt.units.unit_lookup_table import \
unit_prefixes, prefixable_units, cgs_base_units, \
- mks_base_units, latex_prefixes, yt_base_units, \
- unit_aliases, prefix_aliases
+ mks_base_units, latex_prefixes, yt_base_units
from yt.units.unit_registry import UnitRegistry
from yt.utilities.exceptions import YTUnitsNotReducible
@@ -539,25 +538,11 @@
# lookup successful, return the tuple directly
return unit_symbol_lut[symbol_str]
- if symbol_str in unit_aliases:
- # this is an alias for another unit, return its associated tuple
- return unit_symbol_lut[unit_aliases[symbol_str]]
-
# could still be a known symbol with a prefix
- possible_prefix = None
- for prefix in prefix_aliases:
- if symbol_str.startswith(prefix):
- possible_prefix = prefix_aliases[prefix]
- symbol_start = len(prefix)
- break
- if possible_prefix is None:
- possible_prefix = symbol_str[0]
- symbol_start = 1
+ possible_prefix = symbol_str[0]
if possible_prefix in unit_prefixes:
# the first character could be a prefix, check the rest of the symbol
- symbol_wo_prefix = symbol_str[symbol_start:]
- if symbol_wo_prefix in unit_aliases:
- symbol_wo_prefix = unit_aliases[symbol_wo_prefix]
+ symbol_wo_prefix = symbol_str[1:]
unit_is_si_prefixable = (symbol_wo_prefix in unit_symbol_lut and
symbol_wo_prefix in prefixable_units)
diff -r 4059a8a82b9017153e60cbdd039405b1d1c5cd8c -r dadb36eb0725b5dd9db456c15e3b8b1141ac3760 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -44,6 +44,7 @@
default_unit_symbol_lut
from yt.units.equivalencies import equivalence_registry
from yt.utilities.logger import ytLogger as mylog
+from .misc import convert_pint_units
NULL_UNIT = Unit()
@@ -665,7 +666,8 @@
"""
p_units = []
for base, power in arr.units.items():
- p_units.append("%s**(%s)" % (base, Rational(power)))
+ bs = convert_pint_units(base)
+ p_units.append("%s**(%s)" % (bs, Rational(power)))
p_units = "*".join(p_units)
if isinstance(arr.magnitude, np.ndarray):
return YTArray(arr.magnitude, p_units)
https://bitbucket.org/yt_analysis/yt/commits/06a4d7541129/
Changeset: 06a4d7541129
Branch: yt
User: jzuhone
Date: 2015-09-10 04:22:57+00:00
Summary: Don't need these here
Affected #: 1 file
diff -r dadb36eb0725b5dd9db456c15e3b8b1141ac3760 -r 06a4d7541129e362335c3c89018c276b98fb4bbb yt/units/misc.py
--- a/yt/units/misc.py
+++ b/yt/units/misc.py
@@ -22,7 +22,6 @@
"parsec": "pc",
"mole": "mol",
"rankine": "R",
- "gauss": "G",
"watt": "W",
"pascal": "Pa",
"tesla": "T",
@@ -36,7 +35,6 @@
"coulomb": "C",
"newton": "N",
"hertz": "Hz",
- "jansky": "Jy",
"arcsecond": "arcsec",
"arcminute": "arcmin",
"speed_of_light": "c",
https://bitbucket.org/yt_analysis/yt/commits/0cc99c47aa70/
Changeset: 0cc99c47aa70
Branch: yt
User: jzuhone
Date: 2015-09-14 14:21:02+00:00
Summary: Missed this
Affected #: 1 file
diff -r 06a4d7541129e362335c3c89018c276b98fb4bbb -r 0cc99c47aa70238aa542e6bf37bbd5d9ede92f30 yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -114,7 +114,7 @@
"photons": (1.0, dimensions.dimensionless, 0.0, r"\rm{photons}"),
"me": (mass_electron_grams, dimensions.mass, 0.0, r"m_e"),
"mp": (mass_hydrogen_grams, dimensions.mass, 0.0, r"m_p"),
- "mol": (1.0/amu_grams, dimensions.dimensionless, r"\rm{mol}"),
+ "mol": (1.0/amu_grams, dimensions.dimensionless, 0.0, r"\rm{mol}"),
# for AstroPy compatibility
"solMass": (mass_sun_grams, dimensions.mass, 0.0, r"M_\odot"),
https://bitbucket.org/yt_analysis/yt/commits/e9a89327650a/
Changeset: e9a89327650a
Branch: yt
User: jzuhone
Date: 2015-09-14 14:41:50+00:00
Summary: Don't need this line
Affected #: 1 file
diff -r 0cc99c47aa70238aa542e6bf37bbd5d9ede92f30 -r e9a89327650a8db96bf3e203d768544f8e05d70e yt/units/misc.py
--- a/yt/units/misc.py
+++ b/yt/units/misc.py
@@ -3,7 +3,6 @@
"""
-from __future__ import print_function
#-----------------------------------------------------------------------------
# Copyright (c) 2015, yt Development Team.
#
https://bitbucket.org/yt_analysis/yt/commits/2e17d3026526/
Changeset: 2e17d3026526
Branch: yt
User: jzuhone
Date: 2015-09-14 15:15:46+00:00
Summary: Removing arcsec from prefixable_units. Renaming misc.py to something more descriptive.
Affected #: 4 files
diff -r e9a89327650a8db96bf3e203d768544f8e05d70e -r 2e17d302652659bc57845f18e4e904b041a6ad9f yt/units/misc.py
--- a/yt/units/misc.py
+++ /dev/null
@@ -1,85 +0,0 @@
-"""
-Miscellaneous units-related stuff
-
-
-"""
-#-----------------------------------------------------------------------------
-# Copyright (c) 2015, yt Development Team.
-#
-# Distributed under the terms of the Modified BSD License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#-----------------------------------------------------------------------------
-
-pint_aliases = {
- "meter": "m",
- "second": "s",
- "gram": "g",
- "joule": "J",
- "franklin": "esu",
- "dyne": "dyn",
- "parsec": "pc",
- "mole": "mol",
- "rankine": "R",
- "watt": "W",
- "pascal": "Pa",
- "tesla": "T",
- "kelvin": "K",
- "year": "yr",
- "minute": "min",
- "hour": "hr",
- "volt": "V",
- "ampere": "A",
- "foot": "ft",
- "coulomb": "C",
- "newton": "N",
- "hertz": "Hz",
- "arcsecond": "arcsec",
- "arcminute": "arcmin",
- "speed_of_light": "c",
- "esu_per_second": "statA",
- "atomic_mass_unit": "amu",
- "astronomical_unit": "au",
- "light_year": "ly",
- "electron_mass": "me",
- "proton_mass": "mp",
-}
-
-pint_prefixes = {
- 'yotta':'Y',
- 'zetta':'Z',
- 'exa':'E',
- 'peta':'P',
- 'tera':'T',
- 'giga':'G',
- 'mega':'M',
- 'kilo':'k',
- 'deci':'d',
- 'centi':'c',
- 'milli':'m',
- 'micro':'u',
- 'nano':'n',
- 'pico':'p',
- 'femto':'f',
- 'atto':'a',
- 'zepto':'z',
- 'yocto':'y',
-}
-
-def convert_pint_units(unit_expr):
- uexpr = unit_expr
- pfx = ''
- for prefix in pint_prefixes:
- if unit_expr.startswith(prefix):
- pfx = pint_prefixes[prefix]
- uexpr = uexpr[len(prefix):]
- break
- if uexpr in pint_aliases:
- uexpr = pint_aliases[uexpr]
- if pfx == '':
- return uexpr
- else:
- return pfx+uexpr
- # If we can't figure it out just pass it and see
- # what happens
- return unit_expr
diff -r e9a89327650a8db96bf3e203d768544f8e05d70e -r 2e17d302652659bc57845f18e4e904b041a6ad9f yt/units/pint_conversions.py
--- /dev/null
+++ b/yt/units/pint_conversions.py
@@ -0,0 +1,84 @@
+"""
+Stuff for pint conversions
+
+"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2015, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+pint_aliases = {
+ "meter": "m",
+ "second": "s",
+ "gram": "g",
+ "joule": "J",
+ "franklin": "esu",
+ "dyne": "dyn",
+ "parsec": "pc",
+ "mole": "mol",
+ "rankine": "R",
+ "watt": "W",
+ "pascal": "Pa",
+ "tesla": "T",
+ "kelvin": "K",
+ "year": "yr",
+ "minute": "min",
+ "hour": "hr",
+ "volt": "V",
+ "ampere": "A",
+ "foot": "ft",
+ "coulomb": "C",
+ "newton": "N",
+ "hertz": "Hz",
+ "arcsecond": "arcsec",
+ "arcminute": "arcmin",
+ "speed_of_light": "c",
+ "esu_per_second": "statA",
+ "atomic_mass_unit": "amu",
+ "astronomical_unit": "au",
+ "light_year": "ly",
+ "electron_mass": "me",
+ "proton_mass": "mp",
+}
+
+pint_prefixes = {
+ 'yotta':'Y',
+ 'zetta':'Z',
+ 'exa':'E',
+ 'peta':'P',
+ 'tera':'T',
+ 'giga':'G',
+ 'mega':'M',
+ 'kilo':'k',
+ 'deci':'d',
+ 'centi':'c',
+ 'milli':'m',
+ 'micro':'u',
+ 'nano':'n',
+ 'pico':'p',
+ 'femto':'f',
+ 'atto':'a',
+ 'zepto':'z',
+ 'yocto':'y',
+}
+
+def convert_pint_units(unit_expr):
+ uexpr = unit_expr
+ pfx = ''
+ for prefix in pint_prefixes:
+ if unit_expr.startswith(prefix):
+ pfx = pint_prefixes[prefix]
+ uexpr = uexpr[len(prefix):]
+ break
+ if uexpr in pint_aliases:
+ uexpr = pint_aliases[uexpr]
+ if pfx == '':
+ return uexpr
+ else:
+ return pfx+uexpr
+ # If we can't figure it out just pass it and see
+ # what happens
+ return unit_expr
diff -r e9a89327650a8db96bf3e203d768544f8e05d70e -r 2e17d302652659bc57845f18e4e904b041a6ad9f yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -195,7 +195,6 @@
"statV",
"ohm",
"statohm",
- "arcsec",
)
yt_base_units = {
diff -r e9a89327650a8db96bf3e203d768544f8e05d70e -r 2e17d302652659bc57845f18e4e904b041a6ad9f yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -44,7 +44,7 @@
default_unit_symbol_lut
from yt.units.equivalencies import equivalence_registry
from yt.utilities.logger import ytLogger as mylog
-from .misc import convert_pint_units
+from .pint_conversions import convert_pint_units
NULL_UNIT = Unit()
https://bitbucket.org/yt_analysis/yt/commits/73b20f6ecddb/
Changeset: 73b20f6ecddb
Branch: yt
User: jzuhone
Date: 2015-09-14 17:32:56+00:00
Summary: Optionally pass in yt unit registry objects when converting from either AstroPy or Pint.
Affected #: 1 file
diff -r 2e17d302652659bc57845f18e4e904b041a6ad9f -r 73b20f6ecddba9ad8e041c258b4e9aad546eb481 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -615,10 +615,17 @@
return np.array(self)
@classmethod
- def from_astropy(cls, arr):
+ def from_astropy(cls, arr, unit_registry=None):
"""
- Creates a new YTArray with the same unit information from an
- AstroPy quantity *arr*.
+ Convert an AstroPy "Quantity" to a YTArray or YTQuantity.
+
+ Parameters
+ ----------
+ arr : AstroPy Quantity
+ The Quantity to convert from.
+ unit_registry : yt UnitRegistry, optional
+ A yt unit registry to use in the conversion. If one is not
+ supplied, the default one will be used.
"""
# Converting from AstroPy Quantity
u = arr.unit
@@ -631,9 +638,9 @@
ap_units.append("%s**(%s)" % (unit_str, Rational(power)))
ap_units = "*".join(ap_units)
if isinstance(arr.value, np.ndarray):
- return YTArray(arr.value, ap_units)
+ return YTArray(arr.value, ap_units, registry=unit_registry)
else:
- return YTQuantity(arr.value, ap_units)
+ return YTQuantity(arr.value, ap_units, registry=unit_registry)
def to_astropy(self, **kwargs):
@@ -646,7 +653,7 @@
return self.value*_astropy.units.Unit(str(self.units), **kwargs)
@classmethod
- def from_pint(cls, arr):
+ def from_pint(cls, arr, unit_registry=None):
"""
Convert a Pint "Quantity" to a YTArray or YTQuantity.
@@ -654,6 +661,9 @@
----------
arr : Pint Quantity
The Quantity to convert from.
+ unit_registry : yt UnitRegistry, optional
+ A yt unit registry to use in the conversion. If one is not
+ supplied, the default one will be used.
Examples
--------
@@ -670,9 +680,9 @@
p_units.append("%s**(%s)" % (bs, Rational(power)))
p_units = "*".join(p_units)
if isinstance(arr.magnitude, np.ndarray):
- return YTArray(arr.magnitude, p_units)
+ return YTArray(arr.magnitude, p_units, registry=unit_registry)
else:
- return YTQuantity(arr.magnitude, p_units)
+ return YTQuantity(arr.magnitude, p_units, registry=unit_registry)
def to_pint(self, unit_registry=None):
"""
@@ -684,7 +694,8 @@
The unitful quantity to convert from.
unit_registry : Pint UnitRegistry, optional
The Pint UnitRegistry to use in the conversion. If one is not
- supplied, the default one will be used.
+ supplied, the default one will be used. NOTE: This is not
+ the same as a yt UnitRegistry object.
Examples
--------
https://bitbucket.org/yt_analysis/yt/commits/98fd66d8cfb2/
Changeset: 98fd66d8cfb2
Branch: yt
User: MatthewTurk
Date: 2015-09-14 18:06:09+00:00
Summary: Merged in jzuhone/yt (pull request #1748)
Roundtrip conversions between Pint and yt units
Affected #: 8 files
diff -r ad96b8c8f330750d9457710381e859419bb89a5b -r 98fd66d8cfb26fd6ab648b5ca2982bc177970f26 doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
--- a/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
+++ b/doc/source/analyzing/units/2)_Fields_and_unit_conversion.ipynb
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
- "signature": "sha256:4d19ee42177c60fb4b39550b5acd7a0f7e97f59f5c2da3565ff42cdd580454b0"
+ "signature": "sha256:6a06d5720eb6316ac0d322ef0898ec20f33d65ea3eeeacef35ae1d869af12607"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -352,7 +352,7 @@
"level": 3,
"metadata": {},
"source": [
- "Round-Trip Conversions to and from AstroPy's Units System"
+ "Round-Trip Conversions to and from Other Unit Systems"
]
},
{
@@ -503,6 +503,58 @@
"language": "python",
"metadata": {},
"outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We can also do the same thing with unitful quantities from the [Pint package](http://pint.readthedocs.org), using essentially the same procedure:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from pint import UnitRegistry\n",
+ "ureg = UnitRegistry()\n",
+ "v = 1000.*ureg.km/ureg.s\n",
+ "w = yt.YTQuantity.from_pint(v)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print v, type(v)\n",
+ "print w, type(w)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ptemp = temp.to_pint()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print temp, type(temp)\n",
+ "print ptemp, type(ptemp)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
}
],
"metadata": {}
diff -r ad96b8c8f330750d9457710381e859419bb89a5b -r 98fd66d8cfb26fd6ab648b5ca2982bc177970f26 yt/units/pint_conversions.py
--- /dev/null
+++ b/yt/units/pint_conversions.py
@@ -0,0 +1,84 @@
+"""
+Stuff for pint conversions
+
+"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2015, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+pint_aliases = {
+ "meter": "m",
+ "second": "s",
+ "gram": "g",
+ "joule": "J",
+ "franklin": "esu",
+ "dyne": "dyn",
+ "parsec": "pc",
+ "mole": "mol",
+ "rankine": "R",
+ "watt": "W",
+ "pascal": "Pa",
+ "tesla": "T",
+ "kelvin": "K",
+ "year": "yr",
+ "minute": "min",
+ "hour": "hr",
+ "volt": "V",
+ "ampere": "A",
+ "foot": "ft",
+ "coulomb": "C",
+ "newton": "N",
+ "hertz": "Hz",
+ "arcsecond": "arcsec",
+ "arcminute": "arcmin",
+ "speed_of_light": "c",
+ "esu_per_second": "statA",
+ "atomic_mass_unit": "amu",
+ "astronomical_unit": "au",
+ "light_year": "ly",
+ "electron_mass": "me",
+ "proton_mass": "mp",
+}
+
+pint_prefixes = {
+ 'yotta':'Y',
+ 'zetta':'Z',
+ 'exa':'E',
+ 'peta':'P',
+ 'tera':'T',
+ 'giga':'G',
+ 'mega':'M',
+ 'kilo':'k',
+ 'deci':'d',
+ 'centi':'c',
+ 'milli':'m',
+ 'micro':'u',
+ 'nano':'n',
+ 'pico':'p',
+ 'femto':'f',
+ 'atto':'a',
+ 'zepto':'z',
+ 'yocto':'y',
+}
+
+def convert_pint_units(unit_expr):
+ uexpr = unit_expr
+ pfx = ''
+ for prefix in pint_prefixes:
+ if unit_expr.startswith(prefix):
+ pfx = pint_prefixes[prefix]
+ uexpr = uexpr[len(prefix):]
+ break
+ if uexpr in pint_aliases:
+ uexpr = pint_aliases[uexpr]
+ if pfx == '':
+ return uexpr
+ else:
+ return pfx+uexpr
+ # If we can't figure it out just pass it and see
+ # what happens
+ return unit_expr
diff -r ad96b8c8f330750d9457710381e859419bb89a5b -r 98fd66d8cfb26fd6ab648b5ca2982bc177970f26 yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -837,6 +837,33 @@
yield assert_array_equal, yt_arr, YTArray.from_astropy(yt_arr.to_astropy())
yield assert_equal, yt_quan, YTQuantity.from_astropy(yt_quan.to_astropy())
+ at requires_module("pint")
+def test_pint():
+ from pint import UnitRegistry
+
+ ureg = UnitRegistry()
+
+ p_arr = np.arange(10)*ureg.km/ureg.hr
+ yt_arr = YTArray(np.arange(10), "km/hr")
+ yt_arr2 = YTArray.from_pint(p_arr)
+
+ p_quan = 10.*ureg.g**0.5/(ureg.mm**3)
+ yt_quan = YTQuantity(10., "sqrt(g)/mm**3")
+ yt_quan2 = YTQuantity.from_pint(p_quan)
+
+ yield assert_array_equal, p_arr, yt_arr.to_pint()
+ assert p_quan.units == yt_quan.to_pint().units
+ yield assert_array_equal, yt_arr, YTArray.from_pint(p_arr)
+ yield assert_array_equal, yt_arr, yt_arr2
+
+ yield assert_equal, p_quan.magnitude, yt_quan.to_pint().magnitude
+ assert p_quan.units == yt_quan.to_pint().units
+ yield assert_equal, yt_quan, YTQuantity.from_pint(p_quan)
+ yield assert_equal, yt_quan, yt_quan2
+
+ yield assert_array_equal, yt_arr, YTArray.from_pint(yt_arr.to_pint())
+ yield assert_equal, yt_quan, YTQuantity.from_pint(yt_quan.to_pint())
+
def test_subclass():
class YTASubclass(YTArray):
diff -r ad96b8c8f330750d9457710381e859419bb89a5b -r 98fd66d8cfb26fd6ab648b5ca2982bc177970f26 yt/units/unit_lookup_table.py
--- a/yt/units/unit_lookup_table.py
+++ b/yt/units/unit_lookup_table.py
@@ -21,7 +21,7 @@
cm_per_ang, jansky_cgs, mass_jupiter_grams, mass_earth_grams, \
kelvin_per_rankine, speed_of_light_cm_per_s, planck_length_cm, \
planck_charge_esu, planck_energy_erg, planck_mass_grams, \
- planck_temperature_K, planck_time_s
+ planck_temperature_K, planck_time_s, mass_hydrogen_grams
import numpy as np
# Lookup a unit symbol with the symbol string, and provide a tuple with the
@@ -101,7 +101,7 @@
"arcsec": (np.pi/648000., dimensions.angle, 0.0,
r"\rm{arcsec}"), # arcseconds
"mas": (np.pi/648000000., dimensions.angle, 0.0,
- r"\rm{mas}"), # millarcseconds
+ r"\rm{mas}"), # milliarcseconds
"hourangle": (np.pi/12., dimensions.angle, 0.0, r"\rm{HA}"), # hour angle
"steradian": (1.0, dimensions.solid_angle, 0.0, r"\rm{sr}"),
@@ -113,6 +113,8 @@
"counts": (1.0, dimensions.dimensionless, 0.0, r"\rm{counts}"),
"photons": (1.0, dimensions.dimensionless, 0.0, r"\rm{photons}"),
"me": (mass_electron_grams, dimensions.mass, 0.0, r"m_e"),
+ "mp": (mass_hydrogen_grams, dimensions.mass, 0.0, r"m_p"),
+ "mol": (1.0/amu_grams, dimensions.dimensionless, 0.0, r"\rm{mol}"),
# for AstroPy compatibility
"solMass": (mass_sun_grams, dimensions.mass, 0.0, r"M_\odot"),
diff -r ad96b8c8f330750d9457710381e859419bb89a5b -r 98fd66d8cfb26fd6ab648b5ca2982bc177970f26 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -44,6 +44,7 @@
default_unit_symbol_lut
from yt.units.equivalencies import equivalence_registry
from yt.utilities.logger import ytLogger as mylog
+from .pint_conversions import convert_pint_units
NULL_UNIT = Unit()
@@ -614,10 +615,17 @@
return np.array(self)
@classmethod
- def from_astropy(cls, arr):
+ def from_astropy(cls, arr, unit_registry=None):
"""
- Creates a new YTArray with the same unit information from an
- AstroPy quantity *arr*.
+ Convert an AstroPy "Quantity" to a YTArray or YTQuantity.
+
+ Parameters
+ ----------
+ arr : AstroPy Quantity
+ The Quantity to convert from.
+ unit_registry : yt UnitRegistry, optional
+ A yt unit registry to use in the conversion. If one is not
+ supplied, the default one will be used.
"""
# Converting from AstroPy Quantity
u = arr.unit
@@ -630,9 +638,9 @@
ap_units.append("%s**(%s)" % (unit_str, Rational(power)))
ap_units = "*".join(ap_units)
if isinstance(arr.value, np.ndarray):
- return YTArray(arr.value, ap_units)
+ return YTArray(arr.value, ap_units, registry=unit_registry)
else:
- return YTQuantity(arr.value, ap_units)
+ return YTQuantity(arr.value, ap_units, registry=unit_registry)
def to_astropy(self, **kwargs):
@@ -644,6 +652,70 @@
"an AstroPy quantity.")
return self.value*_astropy.units.Unit(str(self.units), **kwargs)
+ @classmethod
+ def from_pint(cls, arr, unit_registry=None):
+ """
+ Convert a Pint "Quantity" to a YTArray or YTQuantity.
+
+ Parameters
+ ----------
+ arr : Pint Quantity
+ The Quantity to convert from.
+ unit_registry : yt UnitRegistry, optional
+ A yt unit registry to use in the conversion. If one is not
+ supplied, the default one will be used.
+
+ Examples
+ --------
+ >>> from pint import UnitRegistry
+ >>> import numpy as np
+ >>> ureg = UnitRegistry()
+ >>> a = np.random.random(10)
+ >>> b = ureg.Quantity(a, "erg/cm**3")
+ >>> c = yt.YTArray.from_pint(b)
+ """
+ p_units = []
+ for base, power in arr.units.items():
+ bs = convert_pint_units(base)
+ p_units.append("%s**(%s)" % (bs, Rational(power)))
+ p_units = "*".join(p_units)
+ if isinstance(arr.magnitude, np.ndarray):
+ return YTArray(arr.magnitude, p_units, registry=unit_registry)
+ else:
+ return YTQuantity(arr.magnitude, p_units, registry=unit_registry)
+
+ def to_pint(self, unit_registry=None):
+ """
+ Convert a YTArray or YTQuantity to a Pint Quantity.
+
+ Parameters
+ ----------
+ arr : YTArray or YTQuantity
+ The unitful quantity to convert from.
+ unit_registry : Pint UnitRegistry, optional
+ The Pint UnitRegistry to use in the conversion. If one is not
+ supplied, the default one will be used. NOTE: This is not
+ the same as a yt UnitRegistry object.
+
+ Examples
+ --------
+ >>> a = YTQuantity(4.0, "cm**2/s")
+ >>> b = a.to_pint()
+ """
+ from pint import UnitRegistry
+ if unit_registry is None:
+ unit_registry = UnitRegistry()
+ powers_dict = self.units.expr.as_powers_dict()
+ units = []
+ for unit, pow in powers_dict.items():
+ # we have to do this because Pint doesn't recognize
+ # "yr" as "year"
+ if str(unit).endswith("yr") and len(str(unit)) in [2,3]:
+ unit = str(unit).replace("yr","year")
+ units.append("%s**(%s)" % (unit, Rational(pow)))
+ units = "*".join(units)
+ return unit_registry.Quantity(self.value, units)
+
#
# End unit conversion methods
#
diff -r ad96b8c8f330750d9457710381e859419bb89a5b -r 98fd66d8cfb26fd6ab648b5ca2982bc177970f26 yt/utilities/physical_constants.py
--- a/yt/utilities/physical_constants.py
+++ b/yt/utilities/physical_constants.py
@@ -10,7 +10,7 @@
amu = amu_cgs
Na = 1 / amu_cgs
-mass_hydrogen_cgs = 1.007947*amu_cgs
+mass_hydrogen_cgs = YTQuantity(mass_hydrogen_grams, 'g')
mass_hydrogen = mass_hydrogen_cgs
mp = mass_hydrogen_cgs
mh = mp
diff -r ad96b8c8f330750d9457710381e859419bb89a5b -r 98fd66d8cfb26fd6ab648b5ca2982bc177970f26 yt/utilities/physical_ratios.py
--- a/yt/utilities/physical_ratios.py
+++ b/yt/utilities/physical_ratios.py
@@ -10,6 +10,7 @@
# Elementary masses
mass_electron_grams = 9.10938291e-28
amu_grams = 1.660538921e-24
+mass_hydrogen_grams = 1.007947*amu_grams
# Solar values (see Mamajek 2012)
# https://sites.google.com/site/mamajeksstarnotes/bc-scale
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list