-
Notifications
You must be signed in to change notification settings - Fork 2
/
astroCoords.py
210 lines (154 loc) · 4.46 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
# 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, ephem
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(m)/60+float(s)/3600) * 15
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) * 15
radians = np.deg2rad(degrees)
return degrees
def main():
# A quick example
b = (167.374, 38.612)
print "b RA: ", deg2rahms(b[0])
print "b DEC: ", deg2decdms(b[1])
if __name__ == '__main__':
main()