Completed
Push — master ( d03f0e...fca813 )
by Ben
01:04
created

parse_lbd()   B

Complexity

Conditions 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 27
rs 8.8571
1
"Some utility methods"
2
from __future__ import division
3
4
import numpy as np
5
from numpy import cos
6
from numpy import pi
7
from numpy import sin
8
9
10
def parse_units(val, units, name, dtype=float):
11
    """
12
    Convert, as necessary, `val` to `units` as `dtype`
13
    """
14
15
    try:
16
        return val.to(units).value
17
    except AttributeError:
18
        pass
19
    except Exception as inst:
20
        raise IOError("Bad format for input {}. ({})".
21
                      format(name, inst))
22
    try:
23
        parsed_val = np.array(val).astype(dtype)
24
        if parsed_val.size == 1:
25
            return parsed_val.flatten()[0]
26
        return parsed_val
27
    except Exception as inst:
28
        raise IOError("Bad format for input {}. ({})".
29
                      format(name, inst))
30
31
32
def parse_DM(DM):
33
    """
34
    Convert, as necessary, DM into float
35
    """
36
    return parse_units(DM, 'pc/cm**3', 'DM')
37
38
39
def parse_lbd(gal_l, gal_b, distance):
40
    """
41
    Convert, as necessary, l,b,d into floats
42
43
    Parameters
44
    ----------
45
    in_l : float or Angle
46
      Galactic longitude; assumed deg if unitless
47
    in_b : float
48
      Galactic latitude; assumed deg if unitless
49
    in_d : float or Quantity
50
      Distance to source; assumed kpc if unitless
51
52
    Returns
53
    -------
54
    gal_l : float
55
      Galactic longitude in deg
56
    gal_b : float
57
      Galactic latitude in deg
58
    distance : float
59
      Distance in kpc
60
61
    """
62
    l = parse_units(gal_l, 'deg', 'Galactic longitude')
63
    b = parse_units(gal_b, 'deg', 'Galactic latitude')
64
    d = parse_units(distance, 'kpc', 'distance')
65
    return l, b, d
66
67
68
def galactic_to_galactocentric(l, b, distance, xyz_sun):
69
    slc = sin(l/180*pi)
70
    clc = cos(l/180*pi)
71
    sbc = sin(b/180*pi)
72
    cbc = cos(b/180*pi)
73
    rgalc = distance*cbc
74
    xc = xyz_sun[0] + rgalc*slc
75
    yc = xyz_sun[1] - rgalc*clc
76
    zc = xyz_sun[-1] + distance*sbc
77
    return np.array([xc, yc, zc])
78
79
80
def lzproperty(attribute):
81
    """
82
    Lazy property: evaluate property only once
83
    """
84
    save_att = '_' + attribute.__name__
85
86
    @property
87
    def _get(self):
88
        try:
89
            return getattr(self, save_att)
90
        except AttributeError:
91
            setattr(self, save_att, attribute(self))
92
        return getattr(self, save_att)
93
    return _get
94
95
96
def rotation(theta, axis=-1):
97
    """
98
    Return a rotation matrix around axis
99
    0:x, 1:y, 2:z
100
    """
101
    ct = cos(theta)
102
    st = sin(theta)
103
104
    if axis in (0, -3):
105
        return np.array([[1, 0, 0],
106
                         [0, ct, st],
107
                         [0, -st, ct]])
108
109
    if axis in (1, -2):
110
        return np.array([[ct, 0, st],
111
                         [0, 1, 0],
112
                         [-st, 0, ct]])
113
114
    if axis in (2, -1):
115
        return np.array([[ct, st, 0],
116
                         [-st, ct, 0],
117
                         [0, 0, 1]])
118
119
120
def rad3d2(xyz):
121
    return xyz[0]**2 + xyz[1]**2 + xyz[-1]**2
122
123
124
def rad2d2(xyz):
125
    return xyz[0]**2 + xyz[1]**2
126
127
128
def matmul(a, b):
129
    try:
130
        return a.__matmul__(b)
131
    except AttributeError:
132
        return np.matmul(a, b)
133