-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsdb_lc.py
127 lines (97 loc) · 3.75 KB
/
wsdb_lc.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
#!/usr/bin/env python
import numpy as np
import argparse
from astropy.time import Time
from wsdb import *
def cmdargs():
parser = argparse.ArgumentParser(
description="Grab a lightcurve from VIRAC v2 from the wsdb.")
parser.add_argument("sourceid", type=int,
help="Source ID")
parser.add_argument("-p", "--plot", action="store_true",
help="Display lightcurve figure")
parser.add_argument("-i", "--saveimage", action="store_true",
help="Save lightcurve figure to disk")
parser.add_argument("-f", "--fold", type=float, default=0.0,
help="Phase fold period (days default, or years with -y flag)")
parser.add_argument("-y", "--years", action="store_true",
help="Time axis in decimal years (default days)")
parser.add_argument("-x", "--xscale", action="store_true",
help="Scale the time axis to epochs with detections")
parser.add_argument("-s", "--shiftx", action="store_true",
help="Shift the time axis so the first detection is at t=0")
return parser.parse_args()
# generate sql query
def gen_sql(sourceid, cols=None):
if cols is None:
cols = ["detid", "catid", "mjdobs", "mag", "emag", "x", "y",
"dp_objtype", "dp_chi", "ext", "pxl_cnf", "sky"]
colsql = ", ".join(["unnest({0}) as {0}".format(c) for c in cols])
return "select %s from virac_lc where sourceid=%d" % (colsql, sourceid)
class LightCurve:
def __init__(self, sourceid):
# generate the sql query
sql = gen_sql(sourceid)
# execute the sql query
# (i.e. grab the data from wsdb)
lcdata = getsql(sql)
# populate the class attributes
self.sourceid = sourceid
self.detid = lcdata["detid"]
self.catid = lcdata["catid"]
self.t = lcdata["mjdobs"]
self.mag = lcdata["mag"]
self.emag = lcdata["emag"]
self.x = lcdata["x"]
self.y = lcdata["y"]
self.dp_objtype = lcdata["dp_objtype"]
self.dp_chi = lcdata["dp_chi"]
self.ext = lcdata["ext"]
self.pxl_cnf = lcdata["pxl_cnf"]
self.sky = lcdata["sky"]
self.epoch_count = lcdata["mag"].size
self.t_unit = "Julian days"
def set_t_unit(self, t_unit):
self.t_unit = t_unit
def to_years(self):
from astropy.time import Time
_t = Time(self.t, format='mjd')
self.t = _t.jyear
self.set_t_unit("Julian years")
if __name__=="__main__":
args = cmdargs()
lc = LightCurve(args.sourceid)
# convert from mjd to jyear if requested
if args.years:
lc.to_years()
# if a plot is requested
if args.plot or args.saveimage:
# phase fold the data if requested
if args.fold>0:
lc.t = np.mod(lc.t, args.fold)
xlabel = "t, phase folded (period %.3f) / %s" % (args.fold,
lc.t_unit)
elif args.shiftx:
shift = np.min(lc.t[~np.isnan(lc.emags)])
lc.t = lc.t - shift
xlabel = "t - %.2f / %s" % (shift, lc.t_unit)
else:
xlabel = "t / %s" % lc.t_unit
# import matplotlib
if not args.plot:
import matplotlib as mpl
mpl.use("Agg")
import matplotlib.pyplot as plt
plt.figure(figsize=(12,3))
plt.errorbar(lc.t, lc.mag, yerr=lc.emag,
fmt='.', markersize=3, linewidth=1, capsize=2)
plt.xlabel(xlabel)
plt.ylabel("K$_s$ / mag")
plt.gca().invert_yaxis()
plt.grid()
plt.tight_layout()
if args.saveimage:
plt.savefig("./Vv2_KsLC_{}.png".format(args.sourceid),
dpi=150, bbox_inches='tight')
if args.plot:
plt.show()