-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoords.py
More file actions
113 lines (106 loc) · 3.46 KB
/
coords.py
File metadata and controls
113 lines (106 loc) · 3.46 KB
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
# H. Ferguson 7/12/05
def degdms(ideg):
if (ideg < 0):
s = -1
else:
s = 1
ideg = abs(ideg)
deg = int(ideg)+0.
m = 60.*(ideg-deg)
minutes = int(m)+0.
seconds = 60.*(m-minutes)
if s < 0:
dms = "-%02d:%02d:%06.3f" % (deg,minutes,seconds)
else:
dms = "%02d:%02d:%06.3f" % (deg,minutes,seconds)
return dms
def deghms(ideg):
ihours = ideg/15.
hours = int(ihours)+0.
m = 60.*(ihours-hours)
minutes = int(m)+0.
seconds = 60.*(m-minutes)
hms = "%02d:%02d:%06.3f" % (hours,minutes,seconds)
return hms
def to_sexagesimal(ra,dec):
hms = deghms(ra)
dms = degdms(dec)
str = "%s %s" % (hms,dms)
return(str)
def parsecoo(coostring,inhours=False):
""" Convert an coordinate string to decimal degrees. Accept
the input in any one of the following formats:
hhh:mm:ss.s -- where hhh is either hours or degrees
hhh:mm.m -- can include whitespace
hhh mm ss.s
hhh mm.m
ddd.ddddd -- assumed to be in degrees, not hours
###h##m##.###s -- with or without whitespace
###d##m##.###s -- with or without whitespace
If inhours is True, multiply result by 15. (hours->deg)
"""
if inhours:
factor = 15.
else:
factor = 1.0
s = coostring.lstrip() # Strip leading whitespace
s = s.rstrip() # Strip trailing whitespace
s = s.replace(':',' ') # Change colons to spaces
s = s.replace('d',' ')
s = s.replace('D',' ')
s = s.replace('h',' ')
s = s.replace('H',' ')
s = s.replace('m',' ')
s = s.replace('M',' ')
s = s.replace('s',' ')
s = s.replace('S',' ')
s = s.replace('\'',' ')
s = s.replace('\"',' ')
a = s.split() # Split the sting
# print "coostring ", a
if len(a) == 1: # Assume degrees if just one value
coo = float(a[0])
if a[0][0] == '-':
sgn = -1
degorhours = -float(a[0])
else:
sgn = 1
degorhours = float(a[0])
if len(a) == 2: # Assume hours, minutes if two values given
coo = degorhours + float(a[1])/60.
coo = sgn*factor*coo
if len(a) == 3: # Assume hours, minutes, seconds
coo = degorhours + float(a[1])/60. + float(a[2])/3600.
coo = sgn*factor*coo
return coo
def parsera(rastring):
return parsecoo(rastring,inhours=True)
def parsedec(decstring):
return parsecoo(decstring)
def parsecoords(coostring):
""" Convert coordinate string to decimal degrees. Accept
the input in any one of the following formats:
hhh:mm:ss.s ddd:mm:ss.s -- whitespace allowed, as long as consistent
hhh:mm:ss.s ddd:mm:ss.s
hhh mm ss.s ddd mm ss.s
hhh:mm.m ddd:mm.m
hhh mm.m ddd mm.m
ddd.ddddd ddd.ddddd -- ra,dec both assumed to be decimal deg
also converts:
12h25m32.52s 25d32m25.2s -- with or without whitespace
"""
a = coostring.split()
l = len(a)
if l<2 or l%2 != 0:
print "parsecoords: cannot convert %s" % coostring
return
if (len(a)) == 2:
ra = parsera(a[0])
dec = parsedec(a[1])
if (len(a)) == 4:
ra = parsera(a[0]+' '+a[1])
dec = parsedec(a[2]+' '+a[3])
if (len(a)) == 6:
ra = parsera(a[0]+' '+a[1]+' '+a[2])
dec = parsedec(a[3]+' '+a[4]+' '+a[5])
return ra,dec