-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastro.py
192 lines (148 loc) · 4.48 KB
/
astro.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
Routines for astronomical related calculation.
"""
from __future__ import print_function, division
import datetime
import numpy as np
import astropy.units as u
def beam_area(*args):
"""
Calculate the Gaussian beam area.
Parameters
----------
args: float
FWHM of the beam.
If args is a single argument, a symmetrical beam is assumed.
If args has two arguments, the two arguments are bmaj and bmin,
the width of the major and minor axes of the beam in that order.
Return
------
out: float
Beam area. No unit conversion is performed, i.e. the unit will depend
on the input arguments. For example, beam width in degree wil return
the beam area in square degree. Likewise, beam width in pixel will
return the beam area in pixel.
"""
if len(args) > 2:
raise ValueError('Input argument must be a single beam width for a '
'symmetrical beam, or widths of the major and minor '
'axes of the beam.')
if len(args) == 2:
bmaj, bmin = args
else:
bmaj = args[0]
bmin = bmaj
return np.pi * bmaj * bmin / (4 * np.log(2))
def h2hms24(h):
"""
Convert decimal hours to hh:mm:ss, rounding value to +0h to +24h.
"""
hm, seconds = divmod((h % 24) * 3600, 60)
hours, minutes = divmod(hm, 60)
return '{:02.0f}:{:02.0f}:{:02.3f}'.format(hours, minutes, seconds)
def h2hms_signed(h):
"""
Convert decimal hours to hh:mm:ss, rounding value to -24h to +24h.
"""
inverse = ''
if h < 0:
inverse = '-'
hms = str(datetime.timedelta(hours=abs(h))).rsplit(', ')[-1]
return inverse + hms
def d2dms(d, delimiter=':', precision=6):
"""
Convert decimal degrees to dd:mm:ss
"""
inverse = ''
if d < 0:
inverse = '-'
minutes, seconds = divmod(abs(d) * 3600, 60)
degrees, minutes = divmod(minutes, 60)
return '{0:s}{1:.0f}{4}{2:02.0f}{4}{3:0{5:d}.{6:d}f}'\
.format(inverse, degrees, minutes, seconds, delimiter, 3 + precision,
precision)
def lst2gha(lst, site_long=116.670456):
"""
Convert LST in decimal degree to GHA in decimal hours.
Longitude of the observer can be specify with site_long = +/- decimal
degree, where + is east and - is west of the prime meridian. Else assume
MWA 128T location, site_long = 116.670456.
"""
gha = lst/15. - site_long/15.
if gha > 24.0:
gha -= 24.0
elif gha < 0.0:
gha += 24.0
return gha
def jysr2k(intensity, freq):
"""
Convert Jy/sr to K.
Parameters
----------
intensity: array-like
Intensity (brightness) in Jy/sr
freq: float
Frequency of the map in MHz
Return
------
out: array-like or float
Brightness temperature in Kelvin
"""
ba = 1 * u.sr
equiv = u.brightness_temperature(ba, freq * u.MHz)
return (intensity * u.Jy).to(u.K, equivalencies=equiv).value
def k2jysr(temp, freq):
"""
Convert K to Jy/sr.
Parameters
----------
temp: array-like
Brightness temperature in Kelvin
freq: float
Frequency of the map in MHz
Return
------
out: array-like or float
Intensity (brightness) in Jy/sr
"""
ba = 1 * u.sr
equiv = u.brightness_temperature(ba, freq * u.MHz)
return (temp * u.K).to(u.Jy, equivalencies=equiv).value
def jybeam2k(intensity, freq, beam_width):
"""
Convert Jy/beam to K.
Parameters
----------
intensity: array-like
Intensity (brightness) in Jy/beam
freq: float
Frequency of the map in MHz
beam_width: float
The Gaussian FWHM width in degree
Return
------
out: array-like or float
Brightness temperature in Kelvin
"""
ba = beam_area(beam_width) * u.Unit('deg2')
equiv = u.brightness_temperature(ba, freq * u.MHz)
return (intensity * u.Jy).to(u.K, equivalencies=equiv).value
def k2jybeam(temp, freq, beam_width):
"""
Convert K to Jy/beam.
Parameters
----------
temp: array-like
Brightness temperature in Kelvin
freq: float
Frequency of the map in MHz
beam_width: float
The Gaussian FWHM width in degree
Return
------
out: array-like or float
Intensity (brightness) in Jy/beam
"""
ba = beam_area(beam_width) * u.Unit('deg2')
equiv = u.brightness_temperature(ba, freq * u.MHz)
return (temp * u.K).to(u.Jy, equivalencies=equiv).value