-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotcurve.py
625 lines (587 loc) · 19.8 KB
/
plotcurve.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import pickle
from pathlib import Path
from operator import itemgetter
from matplotlib import pyplot as plt
from glob import glob
from matplotlib import cm
from mergecat import convert_coord_str_float
import click
import numpy as np
@click.command()
@click.option("-f", "--file_name", type=str, help="Input pkl file name")
@click.option(
"-i", "--init_star_index", type=int, default=0, help="Index of star to plot"
)
@click.option(
"--coord",
type=str,
help="Coordinate of the query star. Example: '0:0:0.0 0:0:0.0' or '0 0 0.0 0 0 0.0' or '0.0 0.0'",
)
def cli(file_name, init_star_index, coord):
"""Plot light curves"""
dmatch = 1
if file_name is None:
candidate_file_list = glob("*gcat_cal.pkl")
if len(candidate_file_list) == 1:
file_name = candidate_file_list[0]
print("Find {0}".format(file_name))
elif len(candidate_file_list) == 0:
print(
"No *gcat_cal.pkl file is found! Please run command `correctphot` in advance!"
)
return
else:
print(
"More than one *gcat_cal.pkl is found! Please specify which file to use by `plotcurve -f FILE_NAME`"
)
return
if not Path(file_name).is_file():
print("WARNING: File not found!")
return
mergecat_dict = pickle.load(open(file_name, "rb"))
if coord is not None:
print("Searching coordinate ...")
c_split = coord.split()
try:
if len(c_split) == 2:
if ":" not in c_split[0] and ":" not in c_split[1]:
ra_q = float(c_split[0].strip())
dec_q = float(c_split[1].strip())
else:
ra_q, dec_q = convert_coord_str_float(
c_split[0].strip().split(":"), c_split[1].strip().split(":")
)
else:
ra_q, dec_q = convert_coord_str_float(c_split[0:3], c_split[3:6])
except Exception:
print("WARNING: Unable to parse the coordinate!")
return
else:
coord = mergecat_dict["coord"]
sep = np.sqrt((ra_q - coord[:, 0]) ** 2 + (dec_q - coord[:, 1]) ** 2)
if np.min(sep) < dmatch / 3600:
init_star_index = sep.argmin()
print("Find target star with index: {0}".format(init_star_index))
else:
print("WARNING: No matching stars within {} arcsec".format(dmatch))
return
if "magx" in mergecat_dict.keys():
plot_lc(file_name, init_star_index)
else:
print(
"This catalog does not have corrected photometry! Please run command `correctphot` in advance!"
)
return
def coord_to_str(ra, dec):
"""Transfer coord into str
Args:
ra (float): RA in deg
dec (float): DEC in deg
Returns:
ra_str (str): RA str
dec_str (str): DEC str
"""
rah = int(ra / 15)
ram = int((ra * 60 / 15 - rah * 60))
ras = ra * 240 - rah * 3600 - ram * 60
decd = int(dec)
decm = int(dec * 60 - decd * 60)
decs = dec * 3600 - decd * 3600 - decm * 60
ra_str = "{0:2d}:{1:0>2d}:{2:0>5.2f}".format(rah, ram, ras)
dec_str = "{0:3d}:{1:0>2d}:{2:0>5.2f}".format(decd, decm, decs)
return ra_str, dec_str
def save_single_phot(
file_name, magtype, star_index, amjd, coord, frame_info, magmatch, magx
):
"""Save single star's photometry into file
Args:
file_name (str): input catalog pkl file name
magtype (str): magtype input
star_index (int): index of the star
amjd (array): AMJD
coord (array): 2D coords array
magmatch (array): raw photometry array
magx (array): corrected photometry array
"""
ra_str, dec_str = coord_to_str(coord[star_index][0], coord[star_index][1])
orig_file_name = "{0}.{1}{3}S{2:0>4d}.orig".format(
file_name.split(".")[0], file_name.split(".")[1][0], star_index, magtype,
)
mag_file_name = "{0}.{1}{3}S{2:0>4d}.dat".format(
file_name.split(".")[0], file_name.split(".")[1][0], star_index, magtype,
)
print(
"Save photometry data of #{0:3d} to {1}, {2}".format(
star_index, orig_file_name, mag_file_name
)
)
with open(orig_file_name, "w") as f:
f.write(
"{0} {1} {2} (J2000) {3}\n".format(star_index, ra_str, dec_str, magtype)
)
for i in range(magmatch.shape[1]):
f.write(
"{0:5d} {1:7d} {2:10.5f} {3:10.5f} {4:10.5f} {5}\n".format(
i,
int(amjd[i]),
(amjd[i] - int(amjd[i]) + 0.5) * 24,
magmatch[star_index, i, 0],
magmatch[star_index, i, 1],
frame_info.iloc[i]["band"],
)
)
with open(mag_file_name, "w") as f:
f.write(
"{0} {1} {2} (J2000) {3}\n".format(star_index, ra_str, dec_str, magtype)
)
for i in range(magmatch.shape[1]):
f.write(
"{0:5d} {1:7d} {2:10.5f} {3:10.5f} {4:10.5f} {5}\n".format(
i,
int(amjd[i]),
(amjd[i] - int(amjd[i]) + 0.5) * 24,
magx[star_index, i, 0],
magx[star_index, i, 1],
frame_info.iloc[i]["band"],
)
)
def prepare_lc_df(star_index, frame_info, magmatch, magx):
"""Prepare cleaned light curve data
Add mag, mag_err, magx, and magx_err to info
Remove nan values or too bright values in magx
Args:
star_index (int): index of the star
frame_info (DataFrame): info data
magmatch (array): raw photometry array
magx (array): corrected photometry array
Returns:
lc (array): light curve data
"""
lc = frame_info.copy()
lc = lc.assign(mag=magmatch[star_index, :, 0])
lc = lc.assign(mag_err=magmatch[star_index, :, 1])
lc = lc.assign(magx=magx[star_index, :, 0])
lc = lc.assign(magx_err=magx[star_index, :, 1])
lc = lc[~np.isnan(lc.magx) & (lc.magx > 1)]
return lc
def handle_margin(mag_surronding, mag_zoom_index):
if mag_zoom_index >= -2:
return mag_surronding * 1.4 ** mag_zoom_index
else:
return (mag_zoom_index + 2) * mag_surronding / 2
def plot_lc(file_name, init_star_index):
"""Plot light curve
Keyboard interaction:
n: next star; next day (day mode)
N: previous star; previous day (day mode)
d: switch day mode
a: switch airmass / ligt curve
z: zoom out
Z: zoom in
r: reset scale
s: save the data of this star
q: quit
Args:
file_name (str): pkl file name with magx
"""
print("Plotting ...")
# Remove "s", "a", "f", "l" from keymap
for key in plt.rcParams:
if key[:6] == "keymap":
for key_to_remove in ["s", "a", "f", "l"]:
if key_to_remove in plt.rcParams[key]:
plt.rcParams[key].remove(key_to_remove)
mergecat_dict = pickle.load(open(file_name, "rb"))
(
nframe,
medframe_index,
nstar,
ndate,
frame_info,
nomatch,
coord,
psfmagmatch,
apmagmatch,
magtype,
magx,
ommag,
ommag_err,
nframe_date_list,
mjd_date_list,
ncs,
nband,
band_list,
) = itemgetter(
"nframe",
"medframe_index",
"nstar",
"ndate",
"frame_info",
"nomatch",
"coord",
"psfmagmatch",
"apmagmatch",
"magtype",
"magx",
"ommag",
"ommag_err",
"nframe_date_list",
"mjd_date_list",
"ncs",
"nband",
"band_list",
)(
mergecat_dict
)
if magtype == "a":
magmatch = apmagmatch
else:
magmatch = psfmagmatch
global star_index
global mday_flag
global mjd_index
global plot_flag
global plot_flag_old
global mag_zoom_index
global fwhm_zoom_index
star_index = init_star_index
mjd_index = -1
mday_flag = False
plot_flag = "l"
plot_flag_old = "l"
mag_surronding = 0.02
fwhm_surronding = 10
mag_zoom_index = 0
fwhm_zoom_index = 0
plot_label_dict = {"l": "Light curve", "a": "Airmass", "f": "FWHM"}
marker_list = ["o", "v", "^", "D", "s"]
color_list = ["C0", "C1", "C2", "C3", "C4"]
def on_key(event):
global star_index
global mday_flag
global mjd_index
global plot_flag
global plot_flag_old
global mag_zoom_index
global fwhm_zoom_index
plt.clf()
text_x = 0.05
text_y = 0.02
if event.key == "n":
fwhm_zoom_index = 0
if mday_flag:
mjd_index += 1
mjd_index = mjd_index % ndate
plt.text(
text_x,
text_y,
"Day: ({0}/{1})".format(mjd_index + 1, ndate),
transform=plt.gca().transAxes,
)
else:
star_index += 1
print(
"#: {0:4d} M = {1:5.2f} RA: {2} DEC: {3}".format(
star_index,
ommag[star_index],
*coord_to_str(coord[star_index][0], coord[star_index][1]),
)
)
elif event.key == "N":
fwhm_zoom_index = 0
if mday_flag:
mjd_index -= 1
mjd_index = mjd_index % ndate
plt.text(
text_x,
text_y,
"Day: ({0}/{1})".format(mjd_index + 1, ndate),
transform=plt.gca().transAxes,
)
else:
star_index -= 1
print(
"#: {0:4d} M = {1:5.2f} RA: {2} DEC: {3}".format(
star_index,
ommag[star_index],
*coord_to_str(coord[star_index][0], coord[star_index][1]),
)
)
elif event.key == "s":
save_single_phot(
file_name,
magtype,
star_index,
frame_info["amjd"],
coord,
frame_info,
magmatch,
magx,
)
return
elif event.key == "d":
mday_flag = not mday_flag
fwhm_zoom_index = 0
if not mday_flag:
mjd_index = -1
plt.text(text_x, text_y, "NORMAL mode", transform=plt.gca().transAxes)
else:
plt.text(text_x, text_y, "DAY mode", transform=plt.gca().transAxes)
elif event.key == "z":
if plot_flag == "f":
fwhm_zoom_index += 1
else:
mag_zoom_index += 1
plt.text(text_x, text_y, "Zoom Out", transform=plt.gca().transAxes)
elif event.key == "Z":
if plot_flag == "f":
fwhm_zoom_index -= 1
else:
mag_zoom_index -= 1
plt.text(text_x, text_y, "Zoom In", transform=plt.gca().transAxes)
elif event.key == "a":
if plot_flag != "a":
plot_flag_old = plot_flag
plot_flag = "a"
else:
plot_flag_old, plot_flag = plot_flag, plot_flag_old
plt.text(
text_x,
text_y,
plot_label_dict[plot_flag],
transform=plt.gca().transAxes,
)
elif event.key == "f":
if plot_flag != "f":
plot_flag_old = plot_flag
plot_flag = "f"
else:
plot_flag_old, plot_flag = plot_flag, plot_flag_old
plt.text(
text_x,
text_y,
plot_label_dict[plot_flag],
transform=plt.gca().transAxes,
)
elif event.key == "l":
if plot_flag != "l":
plot_flag_old = plot_flag
plot_flag = "l"
else:
plot_flag_old, plot_flag = plot_flag, plot_flag_old
plt.text(
text_x,
text_y,
plot_label_dict[plot_flag],
transform=plt.gca().transAxes,
)
elif event.key == "r":
mag_zoom_index = 0
fwhm_zoom_index = 0
plt.text(text_x, text_y, "Reset", transform=plt.gca().transAxes)
else:
return
inner_plot(
frame_info,
magmatch,
magx,
star_index,
mday_flag,
mjd_index,
plot_flag,
plot_flag_old,
mag_zoom_index,
fwhm_zoom_index,
)
plt.draw()
def inner_plot(
frame_info,
magmatch,
magx,
star_index,
mday_flag,
mjd_index,
plot_flag,
plot_flag_old,
mag_zoom_index,
fwhm_zoom_index,
):
lc = prepare_lc_df(star_index, frame_info, magmatch, magx)
if plot_flag == "l":
if mjd_index != -1:
lc = lc[lc.mjd == mjd_date_list[mjd_index]]
xlabel = "MJD"
ylabel = "magnitude"
if len(lc) > 0:
if mday_flag:
x = lc.amjd - int(min(lc.amjd))
else:
x = lc.mid_time
y1 = lc.mag
y2 = lc.magx
for i in range(nband):
plt.scatter(
x[lc.band == band_list[i]],
y1[lc.band == band_list[i]],
s=2,
c=color_list[i],
alpha=0.4,
)
plt.scatter(
x[lc.band == band_list[i]],
y2[lc.band == band_list[i]],
s=4,
c=color_list[i],
label=band_list[i],
)
plt.scatter(
x[(lc.band == band_list[i]) & (lc.is_bad)],
y2[(lc.band == band_list[i]) & (lc.is_bad)],
s=20,
marker="x",
c=color_list[i],
label="__nolegend__",
)
plt.legend()
plt.hlines(
ommag[star_index], min(x), max(x), linestyles="dashed", colors="red"
)
plt.ylim(
np.percentile(y2, 3)
- handle_margin(mag_surronding, mag_zoom_index),
np.percentile(y2, 97)
+ handle_margin(mag_surronding, mag_zoom_index),
)
plt.gca().invert_yaxis()
elif plot_flag == "a":
if mjd_index != -1:
lc = lc[lc.mjd == mjd_date_list[mjd_index]]
xlabel = "airmass"
ylabel = "magnitude"
if len(lc) > 0:
x = lc.airmass
y1 = lc.mag
y2 = lc.magx
if not mday_flag:
for i in range(nband):
plt.scatter(
x[lc.band == band_list[i]],
y1[lc.band == band_list[i]],
s=4,
c=color_list[i],
label=band_list[i],
)
plt.scatter(
x[(lc.band == band_list[i]) & (lc.is_bad)],
y1[(lc.band == band_list[i]) & (lc.is_bad)],
s=20,
c=color_list[i],
marker="x",
label="__nolegend__",
)
plt.legend()
else:
cmap = cm.get_cmap("rainbow")
if mjd_index == -1:
scatter = plt.scatter(x, y1, s=4, c=lc.mjd, cmap="rainbow")
plt.scatter(
x[lc.is_bad],
y1[lc.is_bad],
s=20,
marker="x",
c=lc[lc.is_bad].mjd,
cmap="rainbow",
)
plt.legend(*scatter.legend_elements())
else:
for i in range(nband):
plt.scatter(
x[lc.band == band_list[i]],
y1[lc.band == band_list[i]],
s=4,
c=color_list[i],
label=band_list[i],
)
plt.scatter(
x[(lc.band == band_list[i]) & (lc.is_bad)],
y1[(lc.band == band_list[i]) & (lc.is_bad)],
s=20,
c=color_list[i],
marker="x",
label="__nolegend__",
)
plt.legend()
plt.ylim(
np.percentile(y2, 3)
- handle_margin(mag_surronding, mag_zoom_index),
np.percentile(y2, 97)
+ handle_margin(mag_surronding, mag_zoom_index),
)
plt.gca().invert_yaxis()
elif plot_flag == "f":
if mjd_index != -1:
lc = lc[lc.mjd == mjd_date_list[mjd_index]]
xlabel = "MJD"
ylabel = "FWHM"
if len(lc) > 0:
if mday_flag:
x = lc.amjd - int(min(lc.amjd))
else:
x = lc.mid_time
y1 = lc.fwhm
for i in range(nband):
plt.scatter(
x[lc.band == band_list[i]],
y1[lc.band == band_list[i]],
s=2,
c=color_list[i],
label=band_list[i],
)
plt.scatter(
x[(lc.band == band_list[i]) & (lc.is_bad)],
y1[(lc.band == band_list[i]) & (lc.is_bad)],
s=20,
marker="x",
c=color_list[i],
label="__nolegend__",
)
plt.legend()
plt.ylim(
min(y1),
max(y1)
+ handle_margin(fwhm_surronding, fwhm_zoom_index)
- fwhm_surronding,
)
title = "#: {0:4d} M = {1:.2f} RA: {3} DEC: {4} MJD+{2:d}".format(
star_index,
ommag[star_index],
int(min(lc.amjd, default=0)),
*coord_to_str(coord[star_index][0], coord[star_index][1]),
)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
fig, ax = plt.subplots(1, 1)
inner_plot(
frame_info,
magmatch,
magx,
star_index,
mday_flag,
mjd_index,
plot_flag,
plot_flag_old,
mag_zoom_index,
fwhm_zoom_index,
)
print(
"#: {0:4d} M = {1:5.2f} RA: {2} DEC: {3}".format(
star_index,
ommag[star_index],
*coord_to_str(coord[star_index][0], coord[star_index][1]),
)
)
fig.canvas.mpl_connect("key_press_event", on_key)
plt.show()
if __name__ == "__main__":
cli()