1
|
|
|
""" Tests on utils module |
2
|
|
|
""" |
3
|
|
|
|
4
|
|
|
import numpy as np |
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from ne2001 import utils |
8
|
|
|
|
9
|
|
|
from astropy.coordinates import Angle |
10
|
|
|
from astropy import units as u |
11
|
|
|
from astropy.units.core import UnitConversionError |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def test_parse_lbd(): |
15
|
|
|
""" Parse lbd """ |
16
|
|
|
# Simple floats |
17
|
|
|
in_l,in_b,in_d = 1., 1., 50. |
18
|
|
|
l,b,d = utils.parse_lbd(in_l, in_b, in_d) |
19
|
|
|
# Test |
20
|
|
|
for xx in [l,b,d]: |
21
|
|
|
assert isinstance(xx,float) |
22
|
|
|
# Angles |
23
|
|
|
in_l,in_b,in_d = Angle(1.*u.deg), Angle(1.*u.deg), 50. |
24
|
|
|
l,b,d = utils.parse_lbd(in_l, in_b, in_d) |
25
|
|
|
assert np.isclose(in_l.value, l) |
26
|
|
|
# Distance |
27
|
|
|
in_l,in_b,in_d = Angle(1.*u.deg), Angle(1.*u.deg), 50.*u.kpc |
28
|
|
|
l,b,d = utils.parse_lbd(in_l, in_b, in_d) |
29
|
|
|
assert np.isclose(in_d.value, d) |
30
|
|
|
# Again |
31
|
|
|
in_l,in_b,in_d = Angle(1.*u.deg), Angle(1.*u.deg), 500.*u.pc |
32
|
|
|
l,b,d = utils.parse_lbd(in_l, in_b, in_d) |
33
|
|
|
assert np.isclose(d, 0.5) |
34
|
|
|
# Bad input |
35
|
|
|
in_l,in_b,in_d = Angle(1.*u.deg), Angle(1.*u.deg), 500.*u.s |
36
|
|
|
with pytest.raises(UnitConversionError): |
37
|
|
|
l,b,d = utils.parse_lbd(in_l, in_b, in_d) |
38
|
|
|
in_l,in_b,in_d = Angle(1.*u.deg), Angle(1.*u.deg), [500] |
39
|
|
|
with pytest.raises(IOError): |
40
|
|
|
l,b,d = utils.parse_lbd(in_l, in_b, in_d) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def test_parse_DM(): |
45
|
|
|
""" Parse lbd """ |
46
|
|
|
# Simple floats |
47
|
|
|
in_DM = 20. |
48
|
|
|
DM = utils.parse_DM(in_DM) |
49
|
|
|
assert isinstance(DM, float) |
50
|
|
|
# Quantity |
51
|
|
|
in_DM = 20. * u.pc / u.cm**3 |
52
|
|
|
DM = utils.parse_DM(in_DM) |
53
|
|
|
assert np.isclose(DM, in_DM.value) |
54
|
|
|
|