forked from e-koch/ewky_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_velocity.py
60 lines (36 loc) · 1.2 KB
/
convert_velocity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from astropy import units as u
from astropy.constants import c
import numpy as np
def optical_to_radio(vel, freq, rest):
return freq * vel / rest
def radio_to_optical(vel, freq, rest):
return rest * vel / freq
def test_opt_to_rad():
opt_vel = -2126.1876453900204 * (u.km/u.s)
rad_vel = -2141.374699999949 * (u.km/u.s)
rest = 1.4 * u.GHz
freq = 1.41 * u.GHz
np.testing.assert_almost_equal(opt_vel.value,
radio_to_optical(rad_vel, freq, rest).value)
np.testing.assert_almost_equal(rad_vel.value,
optical_to_radio(opt_vel, freq, rest).value)
def direct_radio_to_optical(velocity):
try:
velocity.units
unit_flag = True
except AttributeError:
unit_flag = False
if unit_flag:
return (c * velocity) / (c - velocity)
else:
return (c.value * velocity) / (c.value - velocity)
def direct_optical_to_radio(velocity):
try:
velocity.units
unit_flag = True
except AttributeError:
unit_flag = False
if unit_flag:
return (c * velocity) / (c + velocity)
else:
return (c.value * velocity) / (c.value + velocity)