-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
149 lines (120 loc) · 4.02 KB
/
utils.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
from astroquery.simbad import Simbad
def check_format(input):
"""
Check the format of a given input and extract components.
The expected format is 'x1:x2:x3' or 'x1 x2 x3',
where 'x1', 'x2', and 'x3' are separated by a colon (':') or a space (' ').
Parameters:
input (str): The input to be checked and parsed.
Returns:
False: If the input does not match the expected format.
list: A list containing the three components extracted from the input if the format is correct.
"""
separators = [':', ' ']
separator = None
for sep in separators:
if sep in input:
separator = sep
break
if separator is None:
return False
components = input.split(separator)
# Check for correct format
if len(components) != 3:
return False
else:
return components
def hms_to_hours(time_string):
"""
Converts Hours string to float
:param time_string: Hours String (hh:mm:ss.ss)
"""
# Verify separator
components = check_format(time_string)
if components:
hours = abs(int(components[0]))
minutes = int(components[1])
seconds = float(components[2])
total_hours = hours + minutes / 60 + seconds / 3600
sign = -1 if "-" in time_string else 1
return sign*total_hours
else:
return None
def is_numeric(input):
"""
Check if the input is a numeric value.
Parameters:
input (int or float): The value to be checked.
Returns:
bool: True if the input is numeric (int or float), False otherwise.
"""
if isinstance(input, (int, float)):
return True
else:
return False
def dms_to_degrees(degrees_string):
"""
Converts Degrees string to float
:param degrees_string: Degrees String (dd:mm:ss.ss)
"""
# Verify separator
components = check_format(degrees_string)
if components:
degrees_int = abs(int(components[0]))
minutes = int(components[1])
seconds = float(components[2])
degrees = degrees_int + minutes / 60 + seconds / 3600
sign = -1 if "-" in degrees_string else 1
return sign*degrees
else:
return None
def hours_to_hms(hours, decimal_digits=0):
"""
Converts Float Hour to string Hour, in format hh:mm:ss:cc
:param hours: Hours (float)
"""
if is_numeric(hours):
sign = "-" if hours < 0 else ""
hours = abs(hours)
whole_hours = int(hours)
fractional_hours = hours - whole_hours
minutes = int(fractional_hours * 60)
fractional_minutes = fractional_hours * 60 - minutes
seconds = int(fractional_minutes * 60)
fractional_seconds = fractional_minutes * 60 - seconds
seconds_str = f"{seconds:02}.{int(fractional_seconds * (10 ** decimal_digits)):02d}"
time_string = f"{sign}{whole_hours:02}:{minutes:02}:{seconds_str}"
return time_string
else:
return None
def degrees_to_dms(degrees):
"""
Converts Degrees to string, in format dd:mm:ss:cc
:param hours: Degrees (float)
"""
if is_numeric(degrees):
sign = "-" if degrees < 0 else "+"
degrees = abs(degrees)
degrees_int = int(degrees)
minutes = int((degrees - degrees_int) * 60)
seconds = int(((degrees - degrees_int) * 60 - minutes) * 60)
seconds_decimal = int((((degrees - degrees_int) * 60 - minutes) * 60 - seconds) * 100)
# Formated value
degrees_string = f'{sign}{degrees_int:02}:{minutes:02}:{seconds:02}.{seconds_decimal:02}'
return degrees_string
else:
return None
def simbad_radec(id):
"""Input: Simbad ID
Return ra (hours), dec (degrees)"""
identifier = id
try:
result_table = Simbad.query_object(identifier)
ra = result_table["RA"][0]
dec = result_table["DEC"][0]
# ra = hms_to_hours(ra)
# dec = dms_to_degrees(dec)
except:
ra = None
dec = None
return(ra, dec)