Debian Codegraph @

asdf_astropy/converters/unit/tests/test_equivalency.py

raw
1 import asdf
2 import astropy
3 import pytest
4 from astropy import units as u
5 from astropy.cosmology import Planck15
6 from astropy.cosmology.units import with_H0
7 from astropy.units import equivalencies as eq
8 from packaging.version import Version
9
10
11 def create_equivalencies():
12 result = [
13 eq.plate_scale(0.3 * u.deg / u.mm),
14 eq.pixel_scale(0.5 * u.deg / u.pix),
15 eq.spectral_density(350 * u.nm),
16 eq.spectral(),
17 eq.brightness_temperature(500 * u.GHz),
18 eq.brightness_temperature(500 * u.GHz, beam_area=23 * u.sr),
19 with_H0(),
20 eq.temperature_energy(),
21 eq.temperature(),
22 eq.thermodynamic_temperature(300 * u.Hz),
23 eq.thermodynamic_temperature(140 * u.GHz, Planck15.Tcmb0),
24 eq.beam_angular_area(3 * u.sr),
25 eq.mass_energy(),
26 eq.molar_mass_amu(),
27 eq.doppler_relativistic(2 * u.m),
28 eq.doppler_optical(2 * u.nm),
29 eq.doppler_radio(2 * u.Hz),
30 eq.parallax(),
31 eq.logarithmic(),
32 eq.dimensionless_angles(),
33 eq.spectral() + eq.temperature(),
34 (eq.spectral_density(35 * u.nm) + eq.brightness_temperature(5 * u.Hz, beam_area=2 * u.sr)),
35 (eq.spectral() + eq.spectral_density(35 * u.nm) + eq.brightness_temperature(5 * u.Hz, beam_area=2 * u.sr)),
36 ]
37
38 if Version(astropy.__version__) >= Version("4.1"):
39 result.append(eq.pixel_scale(100.0 * u.pix / u.cm))
40
41 # the factor argument to spectral density is deprecated in astropy 7
42 # skip this test to avoid test failures due to the deprecation warning
43 if Version(astropy.__version__) < Version("7.0.0.dev"):
44 result.append(eq.spectral_density(350 * u.nm, factor=2))
45 return result
46
47
48 @pytest.mark.parametrize("equivalency", create_equivalencies())
49 def test_serialization(equivalency, tmp_path):
50 file_path = tmp_path / "test.asdf"
51 with asdf.AsdfFile() as af:
52 af["equivalency"] = equivalency
53 af.write_to(file_path)
54
55 with asdf.open(file_path) as af:
56 assert af["equivalency"] == equivalency