Example #1
0
def test_convert_units2():
    xarr = units.SpectroscopicAxis(np.linspace(1, 10, 10), unit='Hz')

    velocity_arr = xarr.as_unit('km/s',
                                velocity_convention='optical',
                                refX=5 * u.GHz)

    xarr2 = units.SpectroscopicAxis(np.linspace(1, 10, 10),
                                    unit='Hz',
                                    velocity_convention='optical',
                                    refX=5 * u.GHz)
    velocity_arr2 = xarr2.to(u.km / u.s)

    assert np.all(velocity_arr.value == velocity_arr2.value)
Example #2
0
def test_convert_units(unit_from, unit_to, convention, ref_unit):
    xarr = units.SpectroscopicAxis(np.linspace(1, 10, 10),
                                   unit=unit_from,
                                   refX=5,
                                   refX_unit=ref_unit)
    xarr.convert_to_unit(unit_to, convention=convention)
    assert xarr.unit == unit_to
def test_convert_back(unit_from, unit_to, convention, ref_unit):
    if unit_from in ('cms', 'cm/s') or unit_to in ('cms', 'cm/s'):
        xvals = np.linspace(1000, 10000, 10)
        threshold = 1e-6
    else:
        xvals = np.linspace(1, 10, 10)
        threshold = 1e-15
    # all conversions include a * or / by speedoflight_ms
    threshold = np.spacing(units.speedoflight_ms) * 100
    if 'megameter' in unit_from or 'Mm' in unit_from:
        threshold *= 10
    xarr = units.SpectroscopicAxis(xvals,
                                   unit=unit_from,
                                   refX=5,
                                   refX_units=ref_unit,
                                   xtype=units.unit_type_dict[unit_from])
    xarr.convert_to_unit(unit_to, convention=convention)
    xarr.convert_to_unit(unit_from, convention=convention)
    assert all(np.abs((xarr - xvals) / xvals) < threshold)
    assert xarr.units == unit_from
    if unit_from in ('cms', 'cm/s') or unit_to in ('cms', 'cm/s'):
        xvals = np.linspace(1000, 10000, 10)
        threshold = 1e-6
    else:
        xvals = np.linspace(1, 10, 10)
        threshold = 1e-15
    # all conversions include a * or / by speedoflight_ms
    threshold = np.spacing(units.speedoflight_ms) * 100
    if 'megameter' in unit_from or 'Mm' in unit_from:
        threshold *= 10
    xarr = units.SpectroscopicAxis(xvals,
                                   unit=unit_from,
                                   refX=5,
                                   refX_units=ref_unit,
                                   xtype=units.unit_type_dict[unit_from])
    xarr.convert_to_unit(unit_to, convention=convention)
    xarr.convert_to_unit(unit_from, convention=convention)
    assert all(np.abs((xarr - xvals) / xvals) < threshold)
    assert xarr.units == unit_from


if __name__ == "__main__":
    unit_from = 'GHz'
    xarr = units.SpectroscopicAxis(np.linspace(1, 10, 10),
                                   unit=unit_from,
                                   refX=5,
                                   refX_units='GHz',
                                   xtype=units.unit_type_dict[unit_from])
    xarr2 = xarr.as_unit('km/s', quiet=False, debug=True)
    xarr3 = xarr2.as_unit('GHz', quiet=False, debug=True)
Example #5
0
def test_equivalencies_1():
    x = units.SpectroscopicAxis(np.arange(5),
                                unit=u.angstrom,
                                velocity_convention='optical',
                                equivalencies=u.doppler_optical(3 * u.AA))
    assert x.equivalencies is not None  # == u.doppler_optical(3*u.AA)
Example #6
0
def test_convert_to(unit_from, unit_to, convention, ref_unit):
    x = units.SpectroscopicAxis(np.arange(5),
                                unit=u.angstrom,
                                velocity_convention='optical',
                                equivalencies=u.doppler_optical(3 * u.AA))
    x.convert_to_unit(unit_to, convention=convention)
Example #7
0
def test_initialize_units():
    xarr = units.SpectroscopicAxis(np.linspace(1, 10, 10),
                                   unit=u.dimensionless_unscaled)
    assert xarr.unit == u.dimensionless_unscaled
    xarr = units.SpectroscopicAxis(np.linspace(1, 10, 10), unit=u.m)
    assert xarr.unit == u.m
Example #8
0
def test_convert_back(unit_from, unit_to, convention, ref_unit):
    if unit_from in ('cms', 'cm/s') or unit_to in ('cms', 'cm/s'):
        xvals = np.linspace(1000, 10000, 10)
        threshold = 1e-6
    else:
        xvals = np.linspace(1, 10, 10)
        threshold = 1e-15
    # all conversions include a * or / by speedoflight_ms
    threshold = np.spacing(units.speedoflight_ms) * 100
    if 'megameter' in unit_from or 'Mm' in unit_from:
        threshold *= 10
    xarr = units.SpectroscopicAxis(xvals,
                                   unit=unit_from,
                                   refX=5,
                                   refX_unit=ref_unit,
                                   velocity_convention=convention)
    xarr.convert_to_unit(unit_to, convention=convention)
    xarr.convert_to_unit(unit_from, convention=convention)
    assert all(np.abs((xarr.value - xvals) / xvals) < threshold)
    assert xarr.unit == unit_from


if __name__ == "__main__":
    unit_from = 'GHz'
    xarr = units.SpectroscopicAxis(np.linspace(1, 10, 10),
                                   unit=unit_from,
                                   refX=5,
                                   refX_unit='GHz')
    xarr2 = xarr.as_unit('km/s', quiet=False, debug=True)
    xarr3 = xarr2.as_unit('GHz', quiet=False, debug=True)