def test_metric_conversion(metric_name, species, conversion): base_str_formats = ["{}", "kg {} / yr", "kg {}", "{} / yr"] for base_str_format in base_str_formats: base = _unit_registry(base_str_format.format(species)) dest = _unit_registry(base_str_format.format("CO2")) with _unit_registry.context(metric_name): np.testing.assert_allclose(base.to(dest).magnitude, conversion) np.testing.assert_allclose(dest.to(base).magnitude, 1 / conversion)
def test_context_compound_unit(): CO2 = 1 * _unit_registry("kg CO2 / yr") N = 1 * _unit_registry("kg N / yr") with _unit_registry.context("AR4GWP100"): np.testing.assert_allclose( CO2.to("kg N / yr").magnitude, 14 / (44 * 298)) np.testing.assert_allclose( N.to("kg CO2 / yr").magnitude, 44 * 298 / 14)
def test_nox(): NOx = _unit_registry("NOx") with pytest.raises(DimensionalityError): NOx.to("N") N = _unit_registry("N") with _unit_registry.context("NOx_conversions"): np.testing.assert_allclose(NOx.to("N").magnitude, 14 / 46) np.testing.assert_allclose(N.to("NOx").magnitude, 46 / 14) # this also becomes allowed, unfortunately... np.testing.assert_allclose(NOx.to("N2O").magnitude, 44 / 46)
def test_methane(): CH4 = _unit_registry("CH4") with pytest.raises(DimensionalityError): CH4.to("C") C = _unit_registry("C") with _unit_registry.context("CH4_conversions"): np.testing.assert_allclose(CH4.to("C").magnitude, 12 / 16) np.testing.assert_allclose(C.to("CH4").magnitude, 16 / 12) # this also becomes allowed, unfortunately... np.testing.assert_allclose(CH4.to("CO2").magnitude, 44 / 16)
def guess_parameter_type(variable_name: str, unit: Optional[str]) -> ParameterType: """ Attempt to guess the parameter of timeseries from a variable name and unit. This ``ParameterType`` is required when interpolating. We only use this function if the user has not already specified which ``ParameterType`` to use, hence forcing us to guess. If the units are available and the units include a `time` dimension, then ``ParameterType.AVERAGE_TIMESERIES`` is always returned, otherwise ``ParameterType.POINT_TIMESERIES`` is returned. If the units are not available, we will guess based on the ``variable_name``. If we don't recognise the name, ``ParameterType.POINT_TIMESERIES`` is returned. Parameters ---------- variable_name The full name of the variable of interest, including level separators. unit Unit corresponding to the variable. Returns ------- :obj:`ParameterType` Our guess of the parameter type which should be used for this ``variable_name`` and ``unit`` """ if unit: # try and determine if the unit contains a time dimension try: pint_unit = _unit_registry(unit).units if "[time]" in str(pint_unit.dimensionality): return ParameterType.AVERAGE_TIMESERIES return ParameterType.POINT_TIMESERIES except UndefinedUnitError: # default to trying to parse from variable name pass for r, t in parameter_matches: if r.match(variable_name): return t # Default to Point time series if unknown return ParameterType.POINT_TIMESERIES
def test_h(): h = _unit_registry("h") np.testing.assert_allclose(h.to("min").magnitude, 60)
def test_emissions_flux(): tOC = _unit_registry("tOC/day") np.testing.assert_allclose(tOC.to("tOC/hr").magnitude, 1 / 24)
def test_kt(): kt = _unit_registry("kt") np.testing.assert_allclose(kt.to("t").magnitude, 1000)
def test_short_definition(): tC = _unit_registry("tC") np.testing.assert_allclose(tC.to("tCO2").magnitude, 44 / 12) np.testing.assert_allclose(tC.to("gC").magnitude, 10**6)
def test_uppercase(): tC = _unit_registry("HFC4310MEE") np.testing.assert_allclose(tC.to("HFC4310mee").magnitude, 1)
def test_ppt(): ppt = _unit_registry("ppt") np.testing.assert_allclose(ppt.to("ppb").magnitude, 1 / 1000)
def test_unit_registry(): CO2 = _unit_registry("CO2") np.testing.assert_allclose(CO2.to("C").magnitude, 12 / 44)
def test_alias(): CO2 = _unit_registry("carbon_dioxide") np.testing.assert_allclose(CO2.to("C").magnitude, 12 / 44)
def test_a(): a = _unit_registry("a") np.testing.assert_allclose(a.to("day").magnitude, 365.24219878)
def test_context_dimensionality_error(): CO2 = _unit_registry("CO2") with pytest.raises(DimensionalityError): CO2.to("N")
def test_nitrogen(): N = _unit_registry("N") np.testing.assert_allclose(N.to("N2ON").magnitude, 28 / 14)
def test_base_unit(): assert _unit_registry("carbon") == _unit_registry("C")
def test_context(): CO2 = _unit_registry("CO2") N = _unit_registry("N") with _unit_registry.context("AR4GWP100"): np.testing.assert_allclose(CO2.to("N").magnitude, 14 / (44 * 298)) np.testing.assert_allclose(N.to("CO2").magnitude, 44 * 298 / 14)
def test_ppm(): ppm = _unit_registry("ppm") np.testing.assert_allclose(ppm.to("ppb").magnitude, 1000)