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

"""
Calculate the angles between the given center point to other points
on the sphere.

The following input formats are supported:
    longitude  latitude     => FMT
    ------------------------------------
    ??h??m??s  ??d??m??s    => "radec"
    ?? ?? ??   ?? ?? ??     => "ra3dec3"
    deg        deg          => "degdeg"
"""

import os
import sys
import re
import getopt
import math


USAGE = """Usage:
    %(prog)s [ -b -h ] -r RA -d DEC -i infile -f FMT -u UNIT

Required arguments:
    -r, --ra
        RA (??h??m??s) of the center
    -d, --dec
        DEC (??d??m??s) of the center
    -i, --infile
        input file containing the coordinates data
    -f, --format
        value: radec | ra3dec3 | degdeg
        coordinates format of the input data file
    -u, --unit
        value: deg | arcmin | arcsec
        unit of the output data

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


def usage():
    print(USAGE, file=sys.stderr)


def ra2deg(h, m, s):
    """
    Convert RA (hour, minute, second) to degree.
    """
    return h * 15.0 + m * 15.0/60.0 + s * 15.0/3600.0


def dec2deg(d, m, s):
    """
    Convert DEC (deg, arcmin, arcsec) to degree.
    """
    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):
    """
    Convert RA string ("??h??m??s") to degree.
    """
    h, m, s = map(float, re.sub('[hms]', ' ', hms).split())
    return ra2deg(h, m, s)


def s_dec2deg(dms):
    """
    Convert DEC string ("??d??m??s") to degree.
    """
    d, m, s = map(float, re.sub('[dms]', ' ', dms).split())
    return dec2deg(d, m, s)


def deg2rad(deg):
    """
    Convert unit from deg to rad.
    """
    return deg * math.pi / 180.0


def rad2deg(rad):
    """
    Convert unit from rad to deg.
    """
    return rad * 180.0 / math.pi


def central_angle(p1, p2, unit="deg"):
    """
    Calculate the central angle between the two points on the sphere.

    Input parameters:
        p1, p2: (longitude, latitude), coorindates of the two points

    Algorithm:
        (radial, azimuthal, polar): (r, theta, phi)
        central_angle: alpha
        longitude: lambda = theta
        latitude: delta = 90 - phi
        colatitude: phi

        Unit vector:
        \hat{r}_1 = (cos(theta1) sin(phi1), sin(theta1) sin(phi1), cos(phi1))
            = (cos(lambda1) cos(delta1), sin(lambda1) cos(delta1), sin(delta1))
        \hat{r}_2 = (cos(theta2) sin(phi2), sin(theta2) sin(phi2), cos(phi2))
            = (cos(lambda2) cos(delta2), sin(lambda2) cos(delta2), sin(delta2))

        Therefore the angle (alpha) between \hat{r}_1 and \hat{r}_2:
        cos(alpha) = \hat{r}_1 \cdot \hat{r}_2
            = cos(delta1) cos(delta2) cos(lambda1-lambda2)
              + sin(delta1) sin(delta2)

    References:
    [1] Spherical Coordinates - Wolfram MathWorld
        http://mathworld.wolfram.com/SphericalCoordinates.html
        Equation (19)
    [2] Great Circle - Wolfram MathWorld
        http://mathworld.wolfram.com/GreatCircle.html
        Equation (1), (2), (4)
    """
    lbd1, delta1 = map(deg2rad, p1)
    lbd2, delta2 = map(deg2rad, p2)
    dotvalue = (math.cos(delta1) * math.cos(delta2) * math.cos(lbd1-lbd2) +
                math.sin(delta1) * math.sin(delta2))
    alpha = math.acos(dotvalue)
    if unit == "rad":
        return alpha
    elif unit == "arcmin":
        return rad2deg(alpha) * 60.0
    elif unit == "arcsec":
        return rad2deg(alpha) * 60.0*60.0
    else:
        # default: degree
        return rad2deg(alpha)


def main():
    # Mandatory arguments
    center_ra = None
    center_dec = None
    infile = None

    # Default parameters
    unit = "arcmin"
    fmt = "radec"  # default format: "??h??m??s  ??d??m??s"

    # Process command line arguments
    try:
        opts, args = getopt.getopt(sys.argv[1:], "bd:f:hi:r:u:",
                                   ["brief", "dec=", "format=", "help",
                                    "infile=", "ra=", "unit="])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit(2)
    brief = False  # brief mode
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(1)
        elif opt in ("-b", "--brief"):
            brief = True
        elif opt in ("-d", "--dec"):
            center_dec = arg
        elif opt in ("-r", "--ra"):
            center_ra = arg
        elif opt in ("-i", "--infile"):
            infile = arg
        elif opt in ("-f", "--format"):
            fmt = arg
        elif opt in ("-u", "--unit"):
            unit = arg
        else:
            assert False, "unhandled option"

    # Check mandatory arguments
    if not center_ra:
        print("Error: --ra argument required!", file=sys.stderr)
    if not center_dec:
        print("Error: --dec argument required!", file=sys.stderr)
    if not infile:
        print("Error: --infile argument required!", file=sys.stderr)

    if fmt == "radec":
        center_ra_deg = s_ra2deg(center_ra)
        center_dec_deg = s_dec2deg(center_dec)
    elif fmt == "ra3dec3":
        ra_h, ra_m, ra_s = map(float, center_ra.split())
        dec_d, dec_m, dec_s = map(float, center_dec.split())
        center_ra_deg = ra2deg(ra_h, ra_m, ra_s)
        center_dec_deg = dec2deg(dec_d, dec_m, dec_s)
    elif fmt == "degdeg":
        center_ra_deg = float(center_ra)
        center_dec_deg = float(center_dec)
    else:
        print("Error: unknown format type: %s" % fmt, file=sys.stderr)
        sys.exit(2)

    if not brief:
        print("# Central_angle (unit: %s)" % unit)

    datafile = open(infile, "r")
    for line in datafile:
        if re.match(r"^\s*#", line):
            # skip comments
            continue
        elif re.match(r"^\s*$", line):
            # skip blank line
            continue
        # coordinate format
        if fmt == "radec":
            ra, dec = line.split()
            ra_deg = s_ra2deg(ra)
            dec_deg = s_dec2deg(dec)
        elif fmt == "ra3dec3":
            ra_h, ra_m, ra_s, dec_d, dec_m, dec_s = map(float, line.split())
            ra_deg = ra2deg(ra_h, ra_m, ra_s)
            dec_deg = dec2deg(dec_d, dec_m, dec_s)
        elif fmt == "degdeg":
            ra_deg, dec_deg = map(float, line.split())
        else:
            print("Error: unknown format type: %s" % fmt, file=sys.stderr)
            sys.exit(2)
        # calculate angle
        angle = central_angle((center_ra_deg, center_dec_deg),
                              (ra_deg, dec_deg), unit=unit)
        print("%.10f" % angle)
    datafile.close()


if __name__ == "__main__":
    main()