Completed
Push — master ( 954152...dfa698 )
by Ben
53s
created

rotation()   B

Complexity

Conditions 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
c 2
b 0
f 0
dl 0
loc 22
rs 8.9197
1
"Some utility methods"
2
from __future__ import division
3
import numpy as np
4
from numpy import cos
5
from numpy import pi
6
from numpy import sin
7
8
9
def lzproperty(attribute):
10
    """
11
    Lazy property: evaluate property only once
12
    """
13
    save_att = '_' + attribute.__name__
14
15
    @property
16
    def _get(self):
17
        try:
18
            return getattr(self, save_att)
19
        except AttributeError:
20
            setattr(self, save_att, attribute(self))
21
        return getattr(self, save_att)
22
    return _get
23
24
25
def rotation(theta, axis=-1):
26
    """
27
    Return a rotation matrix around axis
28
    0:x, 1:y, 2:z
29
    """
30
    ct = cos(theta)
31
    st = sin(theta)
32
33
    if axis in (0, -3):
34
        return np.array([[1, 0, 0],
35
                         [0, ct, st],
36
                         [0, -st, ct]])
37
38
    if axis in (1, -2):
39
        return np.array([[ct, 0, st],
40
                         [0, 1, 0],
41
                         [-st, 0, ct]])
42
43
    if axis in (2, -1):
44
        return np.array([[ct, st, 0],
45
                         [-st, ct, 0],
46
                         [0, 0, 1]])
47
48
49
def galactic_to_galactocentric(l, b, distance, xyz_sun=[0, 8.5, 0]):
50
    slc = sin(l/180*pi)
51
    clc = cos(l/180*pi)
52
    sbc = sin(b/180*pi)
53
    cbc = cos(b/180*pi)
54
    rgalc = distance*cbc
55
    xc = xyz_sun[0] + rgalc*slc
56
    yc = xyz_sun[1] - rgalc*clc
57
    zc = xyz_sun[-1] + distance*sbc
58
    return np.array([xc, yc, zc])
59