-
Notifications
You must be signed in to change notification settings - Fork 0
/
astroCoords.py
294 lines (228 loc) · 6.58 KB
/
astroCoords.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# encoding: utf-8
"""
astroCoords.py
==============
Some useful functions for converting in between Right Ascension (hours,mins,secs)
and Declination (degrees,mins,secs) to degrees and radians. These functions are
all named in the same format: a2b(), where a is the original data, and b is the new
data format.
All of this type of basic stuff can be done with pyEphem or similar, but I wanted
something super simple.
Created by Danny Price on 2011-05-01.
Copyright (c) 2011 The University of Oxford. All rights reserved.
Module listing
~~~~~~~~~~~~~~
"""
import sys, os
import numpy as np
#### FROM DEGREES #####
def deg2rahms(degrees):
""" Convert number in degrees to right ascension HH,MM,SS.SS
Returns in list (hours,mins,secs)
Parameters
----------
degrees: float
degrees to be converted (from 0 to 360)
"""
hours = degrees / 360 * 24
mins = (hours - int(hours)) * 60.0
secs = (mins - int(mins)) * 60
return [int(hours), int(mins), secs]
def deg2decdms(degrees):
""" Convert number in degrees to declination degs, arcmins, arcsecs
Returns in list (hours,mins,secs)
Parameters
----------
degrees: float
degrees to be converted (from 0 to 360)
"""
degs = int(degrees)
arcmins = (degrees - degs) * 60.0
arcsecs = (arcmins - int(arcmins)) * 60
return [int(degs), int(arcmins), arcsecs]
def deg2rad(degrees):
"""Converts from degrees to radians.
This is just a numpy call...
Parameters
----------
degrees: float
degrees to be converted (from 0 to 360)
"""
return np.deg2rad(degrees)
#### FROM RADIANS #####
def rad2rahms(radians):
""" Convert number in radians to right ascension H,M,SS.SS
Returns tuple (hours,mins,secs)
Parameters
----------
radians: float
radians to be converted (from 0 to 2 pi)
"""
degrees = radians * 180.0 / np.pi
hours = degrees / 360 * 24
mins = (hours - int(hours)) * 60.0
secs = (mins - int(mins)) * 60
return [int(hours), int(mins), secs]
def rad2decdms(radians):
""" Convert number in radians to declination degs, arcmins, arcsecs
Returns in list (hours,mins,secs)
Parameters
----------
radians: float
radians to be converted (from 0 to 2 pi)
"""
degrees = np.rad2deg(radians)
degs = int(degrees)
arcmins = (degrees - degs) * 60.0
arcsecs = (arcmins - int(arcmins)) * 60
return [int(degs), int(arcmins), arcsecs]
def rad2deg(radians):
"""Converts from radians to degrees.
This is just a numpy call...
Parameters
----------
radians: float
radians to be converted (from 0 to 2 pi)
"""
return np.rad2deg(radians)
#### FROM RIGHT ASCENSION (H,M,S) #####
def rahms2deg(h,m,s):
"""Converts list (hours,minutes,seconds) to degrees
Value returned in range (0,360)
Parameters
----------
h: int
hour angle to be converted (from 0 to 24)
h: int
minutes to be converted (from 0 to 60)
s: float
seconds to be converted (from 0.0 to 60.0)
"""
degrees = (float(h)+float(m)/60+float(s)/3600) * 15
return degrees
def rahms2rad(h,m,s):
"""Converts list (hours,minutes,seconds) to radians
Value returned in range (0,2pi)
Parameters
----------
h: int
hour angle to be converted (from 0 to 24)
h: int
minutes to be converted (from 0 to 60)
s: float
seconds to be converted (from 0.0 to 60.0)
"""
degrees = (float(h)+float(m)/60+float(s)/3600) * 15
radians = np.deg2rad(degrees)
return radians
#### FROM DECLINATION (D,M,S) #####
def decdms2deg(deg, min, sec):
"""Converts degrees, minutes seconds into degrees
i.e. a floating point number in range (0,360)
Parameters
----------
deg: int
degrees to be converted (from 0 to 360)
min: int
minutes to be converted (from 0 to 60)
sec: float
seconds to be converted (from 0.0 to 60.0)
"""
degrees = (float(deg)+float(min)/60+float(sec)/3600)
return degrees
def decdms2rad(deg, min, sec):
"""Converts degrees, minutes seconds into radians
i.e. a floating point number in range (0,2pi)
Parameters
----------
deg: int
degrees to be converted (from 0 to 360)
min: int
minutes to be converted (from 0 to 60)
sec: float
seconds to be converted (from 0.0 to 60.0)
"""
degrees = (float(deg)+float(m)/60+float(s)/3600)
radians = np.deg2rad(degrees)
return degrees
#### FROM STRINGS HH:MM:SS.ss #####
def rastring2deg(rastring):
"""Converts a string of right ascension to degrees
Value returned in range (0,360)
Todo: More sophisticated string parser
Parameters
----------
rastring: string
right ascension in 24 hour string: HH:MM:SS.ss
"""
try:
rastring = rastring.strip()
(h,m,s) = rastring.split(':')
(h,m,s) = (int(h), int(m), float(s))
deg = rahms2deg(h,m,s)
return deg
except:
print "coords: couldn't convert %s"%rastring
return 0
def rastring2rad(rastring):
"""Converts a string of right ascension to degrees
Value returned in range (0,2pi)
Todo: More sophisticated string parser
Parameters
----------
rastring: string
right ascension in 24 hour string: HH:MM:SS.ss
"""
try:
rastring = rastring.strip()
(h,m,s) = rastring.split(':')
(h,m,s) = (int(h), int(m), float(s))
rad = rahms2rad(h,m,s)
return rad
except:
print "coords: couldn't convert %s"%rastring
return 0
#### FROM STRINGS DD:MM:SS.ss #####
def decstring2deg(decstring):
"""Converts a string of declination to degrees
Value returned in range (0,360)
Todo: More sophisticated string parser
Parameters
----------
decstring: string
declination in DD:MM:SS.ss
"""
try:
decstring = decstring.strip()
(d,m,s) = decstring.split(':')
(d,m,s) = (int(d), int(m), float(s))
deg = decdms2deg(d,m,s)
return deg
except:
print "coords: couldn't convert %s"%decstring
return 0
def decstring2rad(decstring):
"""Converts a string of declination to radians
Value returned in range (0,360)
Todo: More sophisticated string parser
Parameters
----------
decstring: string
declination in DD:MM:SS.ss
"""
try:
decstring = decstring.strip()
(d,m,s) = decstring.split(':')
(d,m,s) = (int(d), int(m), float(s))
rad = decdms2rad(d,m,s)
return rad
except:
print "coords: couldn't convert %s"%decstring
return 0
def main():
# A quick example
ra_in_deg, dec_in_deg = (167.374, 38.612)
print "b RA: ", deg2rahms(ra_in_deg)
print "b DEC: ", deg2decdms(dec_in_deg)
if __name__ == '__main__':
main()