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