aboutsummaryrefslogtreecommitdiffstats
path: root/python/radec2deg.py
blob: 99660953c86cb80af4632c2db4c42c3cf8ca7628 (plain)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Aaron LI
# Created: 2015-04-17
# Updated: 2016-06-30
#

"""
Convert the coordinates data in format (??h??m??s, ??d??m??s)
to format (degree, degree).
"""

import os
import sys
import re
import getopt
import math


USAGE = """Usage:
    %(prog)s [ -h ] -i coords_file

Required arguments:
    -i, --infile
        infile containing the coordinates

Optional arguments:
    -h, --help
""" % {'prog': os.path.basename(sys.argv[0])}


def usage():
    print(USAGE)


def ra2deg(h, m, s):
    return h * 15.0 + m * 15.0/60.0 + s * 15.0/3600.0


def dec2deg(d, m, s):
    if (d >= 0):
        sign = 1.0
    else:
        sign = -1.0
    return sign * (math.fabs(d) + m/60.0 + s/3600.0)


def s_ra2deg(hms):
    h, m, s = map(float, re.sub('[hms]', ' ', hms).split())
    return h * 15.0 + m * 15.0/60.0 + s * 15.0/3600.0


def s_dec2deg(dms):
    d, m, s = map(float, re.sub('[dms]', ' ', dms).split())
    if (d >= 0):
        sign = 1.0
    else:
        sign = -1.0
    return sign * (math.fabs(d) + m/60.0 + s/3600.0)


def calc_offset(coord1, coord2):
    ra1, dec1 = coord1
    ra2, dec2 = coord2
    return math.sqrt((ra1-ra2)**2 + (dec1-dec2)**2)


def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:",
                                   ["help", "infile="])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(1)
        elif opt in ("-i", "--infile"):
            infile = arg
        else:
            assert False, "unhandled option"

    for line in open(infile):
        if re.match(r"^\s*#", line) or re.match(r"^\s*$", line):
            continue
        ra, dec = line.split()
        ra_deg = s_ra2deg(ra)
        dec_deg = s_dec2deg(dec)
        print("%.8f %.8f" % (ra_deg, dec_deg))


if __name__ == "__main__":
    main()