-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_repeatability.py
215 lines (179 loc) · 7.94 KB
/
plot_repeatability.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
import argparse
import numpy as np
import matplotlib.pyplot as plt
from astropy.modeling import models, fitting
from astropy.table import QTable
from astropy.time import Time
from calc_calfactors import get_calfactors
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--show_prev", help="show previous time dependence", action="store_true")
parser.add_argument("--png", help="save figure as a png file", action="store_true")
parser.add_argument("--pdf", help="save figure as a pdf file", action="store_true")
args = parser.parse_args()
filters = ["F560W", "F770W", "F1000W", "F1130W", "F1280W",
"F1500W", "F1800W", "F2100W", "F2550W"]
# make plot
fontsize = 16
font = {"size": fontsize}
plt.rc("font", **font)
plt.rc("lines", linewidth=2)
plt.rc("axes", linewidth=2)
plt.rc("xtick.major", width=2)
plt.rc("ytick.major", width=2)
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(16, 10))
if args.show_prev:
# cftab = QTable.read("CalFactors/jwst_miri_photom_0201.fits", hdu=1)
# cftab_time = QTable.read("CalFactors/jwst_miri_photom_0201.fits", hdu=2)
cftab = QTable.read("Photom/jwst_miri_photom_flight_30aug24.fits", hdu=1)
cftab_time = QTable.read("Photom/jwst_miri_photom_flight_30aug24.fits", hdu=2)
ax = axs[0]
startday = 59720
for k, cfilter in enumerate(filters):
if cfilter == "F2550W":
bkgsub = True
rstr = "_bkgsub"
else:
bkgsub = False
rstr = ""
cfacs = get_calfactors(
"ADwarfs",
cfilter,
xaxisval="timemid",
repeat=True,
startday=startday,
bkgsub=bkgsub,
grieke=True,
)
# remove point that is near 350 - excess stripping in images
gvals = np.absolute(cfacs[2] - 350.0) < 5.
yvals = cfacs[0][~gvals]
yvals_unc = cfacs[1][~gvals]
xvals = cfacs[2][~gvals]
# now get the two stars that we repeated twice to fill in the time gap
for stype, sname in zip(["SolarAnalogs", "ADwarfs"], ["HD 37962", "del UMi"]):
cfacs2 = get_calfactors(
stype,
cfilter,
xaxisval="timemid",
startday=startday,
bkgsub=bkgsub,
grieke=True,
)
nvals = [pname == sname for pname in cfacs2[4]]
if (np.sum(nvals) > 1):
nxvals = cfacs2[2][nvals]
nyvals = cfacs2[0][nvals]
nyvals_unc = cfacs2[1][nvals]
#print(sname, cfilter)
#print(nyvals)
#exit()
# find the value of BD+60 1753 that is closest to the last value
# use this value to adjust the new star to be on the same scale
# later interpolate between values
sindxs = np.argsort(np.absolute(nxvals[-1] - xvals))
nyvals *= yvals[sindxs[0]] / nyvals[-1]
yvals = np.append(yvals, nyvals)
yvals_unc = np.append(yvals_unc, nyvals_unc)
xvals = np.append(xvals, nxvals)
# fit an exponential
# ignore the bad data point for F770W
gvals = abs(xvals - (60070. - startday)) > 20.
fit = fitting.LevMarLSQFitter()
mod_init = (models.Exponential1D(tau=-200., amplitude=-0.2)
+ models.Const1D(amplitude=0.70))
# mod_init = (models.Exponential1D(tau=-150., amplitude=-0.2)
# + models.Linear1D(intercept=0.70, slope=0.0))
mod_init[0].amplitude.bounds = [None, 0.0]
# mod_init[1].slope.bounds = [0.0, 1e-10]
if cfilter in ["F560W", "F770W", "F1000W", "F1130W", "F1280W", "F1500W"]:
mod_init[0].tau.fixed = True
else:
mod_init[0].tau.bounds = [-400., -100.]
fitx = xvals[gvals]
fity = yvals[gvals]
sindxs = np.argsort(fitx)
mod_fit = fit(mod_init, fitx[sindxs], fity[sindxs])
per_dev = (mod_fit(fitx) - fity) / mod_fit(fitx)
per_dev = 100.0 * np.sqrt(np.sum(np.square(per_dev) / (len(fitx) - 2)))
mod_dev = (mod_fit(fitx) - fity)
mod_dev = np.sqrt(np.sum(np.square(mod_dev) / (len(fitx) - 2)))
# save the fit results
atab = QTable()
atab[f"fit_exp_amp_{cfilter}"] = [mod_fit[0].amplitude.value]
atab[f"fit_exp_tau_{cfilter}"] = [mod_fit[0].tau.value]
atab[f"fit_exp_const_{cfilter}"] = [mod_fit[1].amplitude.value]
# atab[f"fit_exp_intercept_{cfilter}"] = [mod_fit[1].intercept.value]
# atab[f"fit_exp_slope_{cfilter}"] = [mod_fit[1].slope.value]
atab[f"fit_exp_startday_{cfilter}"] = [startday]
atab[f"fit_exp_std_{cfilter}"] = [mod_dev]
atab[f"fit_exp_std_per_{cfilter}"] = [per_dev]
sext = "_fit.dat"
atab.write(
f"CalFacs/miri_calfactors{rstr}_repeat_{cfilter}_fit.dat",
format="ascii.commented_header",
overwrite=True,
)
pxvals = np.arange(0, max(fitx))
per_amp = 100. * (mod_fit[0].amplitude.value / mod_fit[1].amplitude.value)
# per_amp = 100. * (mod_fit[0].amplitude.value / mod_fit[1].intercept.value)
meanval = mod_fit[1].amplitude.value
# meanval = mod_fit[1].intercept.value
yvals = meanval / yvals
# yvals_unc /= np.nanmean(yvals)
modvals = (meanval / mod_fit(pxvals))
yoff = k * 0.25
yoff2 = k * 0.1
ax.errorbar(xvals, yvals+yoff, yerr=yvals_unc, fmt="ko", alpha=0.5)
ax.plot([0., max(fitx)], [1. + yoff, 1. + yoff], "k:", alpha=0.5)
ax.plot(pxvals, modvals + yoff, "m-")
modxvals = meanval / mod_fit(xvals)
axs[1].errorbar(xvals, (yvals - modxvals) + yoff2, yerr=yvals_unc, fmt="ko",
alpha=0.5)
axs[1].plot([0., max(fitx)], [0. + yoff2, 0. + yoff2], "m:", alpha=0.5)
# axs[1].plot(pxvals, modvals + yoff2, "m-")
shifty = 0.05
ax.text(425., 1. + yoff + shifty, cfilter)
ax.text(0.0, yoff + shifty + modvals[0],
rf"A={-1.*per_amp:.1f}% / $\tau$={-1.*mod_fit[0].tau.value:.0f} days / $\sigma$={per_dev:.1f}%",
backgroundcolor="w",
fontsize=0.8*fontsize)
shifty2 = 0.02
axs[1].text(550., 0. + yoff2 + shifty2, cfilter)
if args.show_prev:
amp = cftab_time["amplitude"][cftab["filter"] == cfilter][0]
tau = cftab_time["tau"][cftab["filter"] == cfilter][0]
startday = cftab_time["t0"][cftab["filter"] == cfilter][0]
mod_fit[0].amplitude = amp
mod_fit[0].tau = -1. * tau
modvals = (meanval / mod_fit(pxvals))
ax.plot(pxvals, modvals + yoff, "b:")
ax.set_ylim(0.9, 3.5)
ntvals = np.arange(0, max(fitx)+50, 100)
ax.set_xticks(ntvals)
ax.set_xticklabels(Time(ntvals + startday, format="mjd").to_value(format='iso', subfmt='date'))
ax.tick_params(axis='x', labelrotation=60)
# ax.set_xlabel(f"Date")
ax.set_ylabel("Fractional change (+ const)")
axs[1].yaxis.tick_right()
axs[1].yaxis.set_label_position("right")
#axs[1].set_xlim(400., 800.)
axs[1].set_ylim(-0.04, 1.00)
#axs[1].set_xlabel(f"Date")
axs[1].set_xticks(ntvals)
axs[1].set_xticklabels(Time(ntvals + startday, format="mjd").to_value(format='iso', subfmt='date'))
axs[1].tick_params(axis='x', labelrotation=60)
secax = ax.secondary_xaxis('top')
secax.set_xlabel(f"MJD Time - {startday} [days]")
secax = axs[1].secondary_xaxis('top')
secax.set_xlabel(f"MJD Time - {startday} [days]")
axs[1].set_ylabel("Data - Model Residual (+ const)")
# axs[1].set_ylabel("Fractional change (+ const)")
plt.tight_layout()
fname = "all_repeatability"
if args.png:
fig.savefig(f"Figs/{fname}.png")
elif args.pdf:
fig.savefig(f"Figs/{fname}.pdf")
else:
plt.show()