def compute_lincor_model(hdu, pix_x, pix_y, ndegree=3): """ Compute the lineary correction model Parameters ---------- hdu : astropy.fits.hdu header data unit pix_x, pix_y: int x, y pixel coordiates Returns ------- cormod : astropy.modeling.model model the linearity correction with x = measured DN """ # for fitting x = [] y = [] # get all the diffs for k in range(nints): gnum, ydata = get_ramp(hdu, pix_x, pix_y, k) ggnum, gdata, aveDN, diffDN = get_good_ramp(gnum, ydata) # accumulate data x.append(aveDN) y.append(diffDN) # find the ramp that spans the largest range of DN # and save some info -> needed for creating the correction # if (gdata.max() - gdata.min()) > mm_delta: # mm_delta = gdata.max() - gdata.min() if k == 3: max_ramp_k = k # max_ramp_gdata = gdata max_ramp_aveDN = aveDN # fit the aveDN versus diffDN combined data from all integrations x = np.concatenate(x) y = np.concatenate(y) mod = fit_diffs(x, y, ndegree=ndegree - 1) # determine the startDN for all ramps (all not actually needed) startDNvals = np.arange(0.0, 20000.0, 200.0) chival = np.zeros((nints, len(startDNvals))) ints = range(nints) for i, startDN in enumerate(startDNvals): (DN_exp, cor, cor_mod) = calc_lincor(mod[1], max_ramp_aveDN, startDN, ndegree=ndegree) for k in ints: gnum, ydata = get_ramp(hdu, pix_x, pix_y, k) ggnum, gdata, aveDN, diffDN = get_good_ramp(gnum, ydata) # correct the ramps ycor = cor_mod(gdata) gdata_cor = gdata * ycor # calculate the chisqr for each integration set of differences # from the expected flat line diffDN = np.diff(gdata_cor) aveDN = 0.5 * (gdata[:-1] + gdata[1:]) cindxs, = np.where(aveDN > 10000.0) chival[k, i] = np.sum((diffDN[aveDN > 15000.0] - mod[1].c0)**2) minindx = np.zeros((nints), dtype=int) for k in ints[1:]: minindx[k] = np.argmin(chival[k, :]) # only use the startDN from one ramp startDN = startDNvals[minindx[max_ramp_k]] # get the correction (DN_exp, cor, cor_mod) = calc_lincor(mod[1], max_ramp_aveDN, startDN) return cor_mod
pix_x, pix_y = args.pixel ngrps = hdu[0].header["NGROUPS"] nints = min([10, hdu[0].header["NINT"]]) nrej = args.nrej # for fitting x = [] y = [] # for plotting pcol = plt.cm.jet(np.linspace(0, 1, nints)) # plot all integrations folded for k in range(nints): gnum, ydata = get_ramp(hdu[0], pix_x, pix_y, k) ggnum, gdata, aveDN, diffDN = get_good_ramp(gnum, ydata) sax[0, z].plot(gnum, ydata, label=f"Int #{k+1}", color=pcol[k]) # plot the 2pt diffs versus average DN # ax[1].plot(aveDN, diffDN, label=f"Int #{k+1}", color=pcol[k]) # if k == 0: # ax[1].set_ylim(0.9 * min(diffDN), 1.4 * max(diffDN)) # accumulate data for fitting x.append(aveDN) y.append(diffDN) if z == 0: # fit the aveDN versus diffDN combined data from all integrations
# for fitting x = [] y = [] # for plotting pcol = ["b", "g", "r", "c", "y", "b", "g", "r", "c", "y"] # plot all integrations folded mm_delta = 0.0 max_ramp_k = -1 rampoffval = rampoffvals[0] # print("# ints = ", nints) for k in range(nints): gnum, ydata = get_ramp(hdu[0], pix_x, pix_y, k, rampoffval=rampoffvals[0]) ggnum, gdata, aveDN, diffDN = get_good_ramp(gnum, ydata) ax[0].plot(gnum, ydata, label=f"Int #{k+1}", color=pcol[k]) # plot the 2pt diffs ax[1].plot(ggnum[:-1], diffDN, label=f"Int #{k+1}", color=pcol[k]) # plot the 2pt diffs versus average DN ax[2].plot(aveDN, diffDN, label=f"Int #{k+1}", color=pcol[k]) if k == 0: ax[1].set_ylim(0.9 * min(diffDN), 1.1 * max(diffDN)) ax[2].set_ylim(0.9 * min(diffDN), 1.1 * max(diffDN)) # accumulate data for later plotting x.append(aveDN)