Last active
October 31, 2017 01:51
-
-
Save Sunmish/08df2cb5ed7cd34ef786218ac727d86c to your computer and use it in GitHub Desktop.
Convert sky coordinates from hh:mm:ss and dd:mm:ss to decimal degrees.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hms_dms_dd(ra, dec, delimiter=" "): | |
"""Convert from HMS; DMS to DD. | |
Examples: | |
>>> ra, dec = hms_dms_dd("00h59m59.3s", "-00d00m01.01s") | |
>>> ra, dec | |
(14.997083333333332, -0.00028055555555555554) | |
>>> ra, dec = hms_dms_dd("23 59 59", "+56 00 00") | |
>>> ra, dec | |
(359.99583333333334, 56.0) | |
>>> ra, dec = hms_dms_dd("24:30:00", "+90:00:00") | |
>>> ra, dec | |
(7.5, 90.0) | |
""" | |
try: | |
ra_dd, dec_dd = float(ra), float(dec) | |
except ValueError: | |
if ":" in ra: | |
delimiter = ":" | |
elif "h" in ra: | |
ra = ra.replace("h", " ").replace("m", " ").replace("s", " ") | |
dec = dec.replace("d", " ").replace("m", " ").replace("s", " ") | |
ra, dec = ra.split(delimiter), dec.split(delimiter) | |
# RA: | |
ra_hours_dd = float(ra[0]) * 15. | |
ra_minutes_dd = float(ra[1]) * 15. / 60. | |
ra_seconds_dd = float(ra[2]) * 15. / 3600. | |
ra_dd = ra_hours_dd + ra_minutes_dd + ra_seconds_dd | |
if ra_dd >= 360.: | |
ra_dd = abs(ra_dd - 360.) | |
# DEC: | |
if "-" in dec[0]: | |
dec_dd = float(dec[0]) - (float(dec[1]) / 60.) - (float(dec[2]) / 3600.) | |
else: | |
dec_dd = float(dec[0]) + (float(dec[1]) / 60.) + (float(dec[2]) / 3600.) | |
return ra_dd, dec_dd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RA conversion could be more similar to DEC but the separate variables for hours, minutes, and seconds is clearer.