-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_skycoords.py
287 lines (224 loc) · 9.01 KB
/
plot_skycoords.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# plot ra, dec on various frames/systems
def plot_skycoords(system='Galactic', axis='latitude',
aitoff=False,
units='degree',
wrap_ra24hr=False,
overplot=False,
showplot=False,
label=True):
"""
plot galactic limits and ecliptic limits on an radec plot
values are currently hardwired in
could be generalised for ecliptic and lat/long
based on IDL plot_galcoords.pro
handles 24hr wrap
"""
import time
from time import strftime
from time import gmtime
from matplotlib import pyplot as plt
import numpy as np
from astropy.coordinates import SkyCoord
import astropy.units as u
#global appname
print('Plotting ' + system + ' Coordinates')
print('plot_skycoords; label:', label)
if not overplot:
plt.figure(figsize=(8.0, 6.0))
if system == 'Galactic':
color = 'red'
latitudes = [-30.0, -5.0, 0.0, 5.0, 30.0]
linestyles = ['--', '--', '-', '--', '--']
# latitudes= [-30.0, -20.0,-10.0, -5.0, 0.0, 5.0, 10.0, 20.0, 30.0]
# linestyles = [ '--', '--', '--', '--', '-', '--','--', '--','--']
if system == 'Ecliptic':
color = 'blue'
latitudes = [0.0]
linestyles = ['-.']
# latitudes= [-30.0, -20.0,-10.0, -5.0, 0.0, 5.0, 10.0, 20.0, 30.0]
# linestyles = [ '--', '--', '--', '--', '-', '--','--', '--','--']
npoints = 3600
npoints = npoints + 1
ilabel = 0
ilat = -1
# could be generalised for longitude and ecliptic coordinates
for lat in latitudes:
ilat = ilat + 1
print('latitude:', lat, latitudes[ilat])
latitude = np.full((npoints,), lat)
longitude = np.linspace(0.0, 360.0, num=npoints)
if system == 'Galactic':
Galactic = SkyCoord(
longitude * u.degree,
latitude * u.degree, frame='galactic')
Galactic_icrs = Galactic.icrs
if wrap_ra24hr:
ra = Galactic_icrs.ra.wrap_at(180 * u.deg).degree
if not wrap_ra24hr:
ra = Galactic_icrs.ra.degree
dec = Galactic_icrs.dec.degree
if system == 'Ecliptic':
Ecliptic = SkyCoord(
longitude * u.degree,
latitude * u.degree, frame='geocentrictrueecliptic')
Ecliptic_icrs = Ecliptic.icrs
if wrap_ra24hr:
ra = Ecliptic_icrs.ra.wrap_at(180 * u.deg).degree
if not wrap_ra24hr:
ra = Ecliptic_icrs.ra.degree
dec = Ecliptic_icrs.dec.degree
print('0,0: ', ra[0], dec[0])
# determine if where there are 24hrs wraps
if units == 'degree':
wrap_range = 240.0
if units == 'hour':
wrap_range = 18.0
ra = ra / 15.0
i = -1
nwrap = 0
iwrap = 0
wrap = []
ndata = len(ra)
for i in range(0, ndata):
if i <= ndata - 2:
if abs(ra[i] - ra[i + 1]) >= wrap_range:
wrap.append(i)
iwrap = iwrap + 1
print('wrap:', iwrap, i, ra[i], ra[i + 1])
nwrap = iwrap
print('plot_skycoords; label:', label, ilabel)
if label is not None:
label = system + ' Coordinates'
print('plot_skycoords; label:', label, ilabel)
linestyle = linestyles[ilat]
if nwrap == 0:
print('No wrapping: ', latitudes[ilat])
xdata = ra
ydata = dec
# commented out on 20181102
ilabel = plot_skycoords_plot(
xdata, ydata, linestyle=linestyle,
label=label, ilabel=ilabel, color=color,
wrap_ra24hr=wrap_ra24hr, units=units)
if nwrap == 1:
iwrap = 0
print()
print('wrapping:', latitudes[ilat], nwrap)
print('wrap at: ', wrap)
print(iwrap, wrap[iwrap], ra[wrap[iwrap]], ra[wrap[iwrap] + 1])
print(iwrap, wrap[iwrap], dec[wrap[iwrap]], dec[wrap[iwrap] + 1])
ndata = len(ra)
xdata = ra[0:wrap[iwrap]]
ydata = dec[0:wrap[iwrap]]
# commented out on 20181102
ilabel = plot_skycoords_plot(
xdata, ydata, linestyle=linestyle,
label=label, ilabel=ilabel, color=color,
wrap_ra24hr=wrap_ra24hr, units=units)
xdata = ra[wrap[iwrap] + 1:-1]
ydata = dec[wrap[iwrap] + 1:-1]
# commented out on 20181102
ilabel = plot_skycoords_plot(
xdata, ydata, linestyle=linestyle,
label=label, ilabel=ilabel, color=color,
wrap_ra24hr=wrap_ra24hr, units=units)
if nwrap >= 2:
print()
print('wrapping: ', latitudes[ilat], nwrap)
print('wrap at: ', wrap)
isegment = 0
for iwrap in range(0, nwrap):
print()
print(iwrap, nwrap, wrap[iwrap],
ra[wrap[iwrap] - 1], ra[wrap[iwrap]],
ra[wrap[iwrap] + 1])
print(iwrap, nwrap, wrap[iwrap],
dec[wrap[iwrap] - 1], dec[wrap[iwrap]],
dec[wrap[iwrap] + 1])
if iwrap == 0:
isegment = isegment + 1
print()
print('segment: ', isegment)
print(iwrap, nwrap, 0, '-', wrap[iwrap])
print('segment (RA): ', ra[0], '-', ra[wrap[iwrap]])
print('segment (Dec): ', dec[0], '-', dec[wrap[iwrap]])
xdata = ra[0:wrap[iwrap]]
ydata = dec[0:wrap[iwrap]]
#commented out on 20181102
#ilabel = plot_skycoords_plot(
# xdata, ydata,
# linestyle=linestyle, color=color,
# label=label, ilabel=ilabel,
# wrap_ra24hr=wrap_ra24hr, units=units)
if iwrap != 0:
isegment = isegment + 1
print()
print('segment: ', isegment)
print(iwrap, nwrap, wrap[iwrap - 1] + 1, '-', wrap[iwrap])
print('segment (RA):',
ra[wrap[iwrap - 1] + 1], '-', ra[wrap[iwrap]])
print('segment (Dec):',
dec[wrap[iwrap - 1] + 1], '-', dec[wrap[iwrap]])
xdata = ra[wrap[iwrap - 1] + 1:wrap[iwrap]]
ydata = dec[wrap[iwrap - 1] + 1:wrap[iwrap]]
#commented out on 20181102
#ilabel = plot_skycoords_plot(
# xdata, ydata,
# linestyle=linestyle, color=color,
# label=label, ilabel=ilabel,
# wrap_ra24hr=wrap_ra24hr, units=units)
if iwrap == nwrap - 1:
isegment = isegment + 1
print()
print('segment: ', isegment)
print(iwrap, nwrap, wrap[iwrap], npoints - 1)
print('segment (RA): ', ra[wrap[iwrap] + 1], '-', ra[-1])
print('segment (Dec):', dec[wrap[iwrap] + 1], '-', dec[-1])
xdata = ra[wrap[iwrap] + 1:-1]
ydata = dec[wrap[iwrap] + 1:-1]
#commented out on 20181102
#ilabel = plot_skycoords_plot(
# xdata, ydata,
# linestyle=linestyle, color=color,
# label=label, ilabel=ilabel,
# wrap_ra24hr=wrap_ra24hr, units=units)
plt.legend(fontsize='small')
plt.grid()
appname = 'ob_progress'
datestamp = time.strftime("%Y%m%d", time.gmtime())
figname = appname + '_galactic_' + datestamp + '.png'
print('Saving:' + figname)
plt.savefig('./' + figname)
return
def plot_skycoords_plot(xdata, ydata,
label=None, ilabel=0,
aitoff=False,
color='red', linestyle=None,
wrap_ra24hr=False, units='degrees'):
"""
"""
from matplotlib import pyplot as plt
print('plot_skycoords_plot; label:', label, ilabel)
# add plot legend label to first segment in series
if ilabel == 0:
plt.plot(xdata, ydata,
linestyle=linestyle,
color=color, label=label)
ilabel = 1
if ilabel != 0:
plt.plot(xdata, ydata,
linestyle=linestyle,
color=color)
if units == 'degree':
if wrap_ra24hr:
plt.xlim([-180.0, 180.0])
else:
plt.xlim([0.0, 360.0])
if not aitoff and units == 'hour':
if wrap_ra24hr:
plt.xlim([-12.0, 12.0])
else:
plt.xlim([0.0, 24.0])
if not aitoff:
plt.ylim([-90.0, +90.0])
return ilabel