-
Notifications
You must be signed in to change notification settings - Fork 3
/
podi_guidestars.py
executable file
·327 lines (253 loc) · 9.79 KB
/
podi_guidestars.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import astropy.io.fits as pyfits
import os
import sys
import numpy
import itertools
import matplotlib
import matplotlib.pyplot as plot
matplotlib.rcParams['font.family'] = 'DejaVu Sans'
qr_dir = "/work/podi_prep56"
sys.path.append(qr_dir)
import podi_swarpstack
from podi_commandline import *
from podi_definitions import *
import podi_logging
import podi_sitesetup as sitesetup
from scipy.stats import gaussian_kde
limit_fwhm_max = 3.5
def get_guidephotom_filelist(directory, filebase):
filelist = []
for l,n in itertools.product(['A', 'B', 'C', 'F', 'G', 'H'], range(1,5)):
filename = "%s/expVideo/%s.odi%s%d.photom.fits" % (
directory, filebase, l, n)
#print filename
if (os.path.isfile(filename)):
filelist.append(filename)
return filelist
def draw_guidestarplot(filelist, title=None, plot_filename=None):
#
# Start plot
#
fig = plot.figure()
fig.set_facecolor('#e0e0e0')
fig.canvas.set_window_title(
title if title is not None else "Guide Star details")
fig.suptitle(
title if title is not None else "Guide Star details")
#
# Set all plot dimensions
#
plot_width = 0.73
ax_flux = plot.axes([0.10, 0.57, 0.73, 0.36])
ax_fwhm = plot.axes([0.10, 0.20, 0.73, 0.36])
hist_flux = plot.axes([0.84, 0.57, 0.14, 0.36])
hist_fwhm = plot.axes([0.84, 0.20, 0.14, 0.36])
#
# Set all other plot-specific properties
#
null_fmt = plot.NullFormatter()
# Top panel
ax_flux.grid(True)
#ax_flux.set_xlabel("time [seconds]")
ax_flux.set_ylabel("normalized flux")
ax_flux.xaxis.set_major_formatter(null_fmt)
ax_fwhm.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%.1f"))
# Bottom panel
ax_fwhm.grid(True)
ax_fwhm.set_xlabel("time [seconds]")
ax_fwhm.set_ylabel("FWHM [arcsec]")
ax_fwhm.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%.1f"))
hist_fwhm.xaxis.set_major_formatter(null_fmt)
hist_fwhm.yaxis.set_major_formatter(null_fmt)
hist_flux.xaxis.set_major_formatter(null_fmt)
hist_flux.yaxis.set_major_formatter(null_fmt)
# from matplotlib.patches import Rectangle
# currentAxis = fig.canvas
# currentAxis.add_patch(Rectangle((0, 0), 1, 0.10, facecolor="white", fill=True))
global_min_flux = 1e9
shutter_delay = 1.25
pixelscale = 0.118
max_time = -1.
all_flux = []
all_fwhm = []
colors = ['blue',
'red',
'green',
'purple',
'orange',
'dimgrey',
'sienna', ]
text_positions = numpy.array([
[0.01, 0.05],
[0.01, 0.01],
[0.50, 0.05],
[0.50, 0.01],
[0.99, 0.05],
[0.99, 0.01],
])
text_align = ['left', 'left', 'center', 'center', 'right', 'right']
guidestats = {}
if (len(filelist) <= 0):
return guidestats
for idx, infile in enumerate(filelist):
hdulist = pyfits.open(infile)
data = hdulist['VideoPhotometry'].data
timestamp = data['TimeStamp']
flux = data["ApertureBasedFluxDN"]
fwhm_x = data["FWHMX"]
fwhm_y = data["FWHMY"]
fwhm = numpy.hypot(fwhm_x, fwhm_y)
# correct timestamp to seconds since exposure start
timestamp = (timestamp - numpy.min(timestamp)) / 1000.
shutter_open = (timestamp > shutter_delay) & \
(timestamp < (numpy.max(timestamp) - shutter_delay))
flux = flux[shutter_open]
fwhm = fwhm[shutter_open] * pixelscale
timestamp = timestamp[shutter_open]
min_flux, max_flux = numpy.min(flux), numpy.max(flux)
global_min_flux = numpy.min([global_min_flux, min_flux/max_flux])
max_time = numpy.max([max_time, numpy.max(timestamp)])
min_fwhm, max_fwhm = numpy.min(fwhm), numpy.max(fwhm)
#ax_fwhm.plot(timestamp, fwhm)
ax_fwhm.scatter(timestamp, fwhm,
linewidth=0, alpha=0.3,
c=colors[idx])
#ax_flux.plot(timestamp, flux/max_flux)
ax_flux.scatter(timestamp, flux/max_flux,
linewidth=0, alpha=0.3,
c=colors[idx])
#
# Plot some smooth curves through the data points
#
spline_t = numpy.linspace(timestamp[1], timestamp[-2], timestamp[-1]/5.)
# smooth_flux = scipy.interpolate.UnivariateSpline(
# x=timestamp, y=flux/max_flux, w=None, bbox=[None, None], k=3,
# s=0.3)
smooth_flux = scipy.interpolate.LSQUnivariateSpline(
x=timestamp, y=flux/max_flux, t=spline_t, k=3)
ax_flux.plot(timestamp, smooth_flux(timestamp),
color=colors[idx])
smooth_fwhm = scipy.interpolate.UnivariateSpline(
x=timestamp, y=fwhm, k=3) #, w=None, bbox=[None, None], s=0.8*fwhm.shape[0])
smooth_fwhm = scipy.interpolate.LSQUnivariateSpline(
x=timestamp, y=fwhm,
t=spline_t,
)
#ax_fwhm.scatter(spline_t, smooth_fwhm(spline_t))
#ax_fwhm.plot(spline_t, smooth_fwhm(spline_t), linewidth=5)
ax_fwhm.plot(timestamp, smooth_fwhm(timestamp),
#linewidth=4,
color=colors[idx])
#print fwhm
#
# Make histograms
#
# h_flux_count, h_flux = numpy.histogram(flux/max_flux, bins=15, range=[min_flux/max_flux, 1.0])
# h_flux_pos = 0.5*(h_flux[:-1] + h_flux[1:])
# print h_flux
# hist_flux.plot(h_flux_count, h_flux_pos,
# color=colors[idx])
# h_fwhm_count, h_fwhm = numpy.histogram(fwhm, bins=15, range=[min_fwhm, max_fwhm])
# h_fwhm_pos = 0.5*(h_fwhm[:-1] + h_fwhm[1:])
# print h_fwhm
#hist_fwhm.plot(h_fwhm_count, h_fwhm_pos,
# color=colors[idx])
flux_range = (max_flux - min_flux)/max_flux
x_flux = numpy.linspace(min_flux/max_flux-0.05*flux_range, 1.+0.05*flux_range, 100)
density_flux = gaussian_kde(flux/max_flux)
#density_flux.covariance_factor = lambda : .1
density_flux._compute_covariance()
peak_flux = 1. #numpy.max(density_flux(x_flux))
hist_flux.plot(density_flux(x_flux)/peak_flux, x_flux, "-", color=colors[idx])
fwhm_range = max_fwhm - min_fwhm
x_fwhm = numpy.linspace(min_fwhm-0.05*fwhm_range, max_fwhm+0.05*fwhm_range, 100)
density_fwhm = gaussian_kde(fwhm)
#density_fwhm.covariance_factor = lambda : .1
density_fwhm._compute_covariance()
peak_fwhm = 1. #numpy.max(density_fwhm(x_fwhm))
hist_fwhm.plot(density_fwhm(x_fwhm)/peak_fwhm, x_fwhm, "-", color=colors[idx])
#
#
#
all_flux.append(flux/max_flux)
all_fwhm.append(fwhm)
s2p = lambda x : 0.5*(1+scipy.special.erf(x/numpy.sqrt(2)))*100
#print s2p([-3,-1,1,3])
try:
sigmas = scipy.stats.scoreatpercentile(flux/max_flux, s2p([-3,-1,1,3]))
sigma1 = sigmas[2] - sigmas[1]
sigma3 = sigmas[3] - sigmas[0]
except:
sigma1, sigma3 = -1., -1.
pass
txt = u'GS %d: 1\u03C3=%.3f - 3\u03C3=%.3f' % (
idx+1, sigma1, sigma3)
fig.text(text_positions[idx,0], text_positions[idx,1],
txt,
horizontalalignment=text_align[idx])
#hist_fwhm.hist(flux/max_flux, bins=15, orientation='horizontal')
#axHisty.hist(y, bins=bins, orientation='horizontal')
one_return = {
'fwhm_min': min_fwhm,
'fwhm_max': max_fwhm,
'flux_min': min_flux,
'flux_max': max_flux,
'flux_1sigma': sigma1,
'flux_3sigma': sigma3,
'n_guide_samples': fwhm.shape[0]
}
guidestats[infile] = one_return
#
# Determine and set all axis limits
#
all_flux = numpy.array(all_flux)
all_fwhm = numpy.array(all_fwhm)
#print all_fwhm.shape
#numpy.savetxt("fwhm", all_fwhm.reshape((-1,1)))
try:
_min_flux, _max_flux = numpy.min(all_flux), 1.0 # normalized !
except:
_min_flux, _max_flux = 0.0, 1.0
ax_flux.set_ylim((_min_flux-0.05, 1.05))
hist_flux.set_ylim((_min_flux-0.05, 1.05))
try:
_min_fwhm, _max_fwhm = numpy.min(all_fwhm), numpy.max(all_fwhm)
except:
_min_fwhm, _max_fwhm = 0.0, limit_fwhm_max
#print _min_fwhm, _max_fwhm
if (_max_fwhm > limit_fwhm_max): _max_fwhm = limit_fwhm_max
_range_fwhm = _max_fwhm - _min_fwhm
ax_fwhm.set_ylim((_min_fwhm-0.05*_range_fwhm, _max_fwhm+0.05*_range_fwhm))
hist_fwhm.set_ylim((_min_fwhm-0.05*_range_fwhm, _max_fwhm+0.05*_range_fwhm))
d_time = numpy.max([2, 0.03*max_time])
ax_flux.set_xlim((0-d_time, max_time+d_time))
ax_fwhm.set_xlim((0-d_time, max_time+d_time))
hist_flux.set_ylim((global_min_flux-0.05, 1.05))
if (plot_filename is not None):
fig.set_size_inches(8,6)
fig.savefig(plot_filename, dpi=100,
facecolor=fig.get_facecolor(), edgecolor='none')
else:
fig.show()
plot.show()
pass
# catfile = infile[:-5]+".cat"
# sex_cmd = """
# %(sex)s -c %(qr_base)s/config/wcsfix.sex
# -PARAMETERS_NAME %(qr_base)s/config/wcsfix.sexparam
# -CATALOG_NAME %(catfile)s
# %(infile)s """ % {
# 'sex': sitesetup.sextractor,
# 'qr_base': qr_dir,
# 'catfile': catfile,
# 'infile': infile,
# }
# print " ".join(sex_cmd.split())
# os.system(" ".join(sex_cmd.split()))
return guidestats
if __name__ == "__main__":
filelist = sys.argv[1:]
guidestats = draw_guidestarplot(filelist, title="some title", plot_filename="guide.png")
print(guidestats)