def setup_cube(ngroups, nrows, ncols): ''' Set up fake data to test.''' nints = 1 data_model = RampModel() data_model.data = np.zeros(shape=(nints, ngroups, nrows, ncols), dtype=np.float32) data_model.pixeldq = np.zeros(shape=(nrows, ncols), dtype=np.int32) data_model.meta.subarray.xstart = 1 data_model.meta.subarray.ystart = 1 data_model.meta.subarray.xsize = ncols data_model.meta.subarray.ysize = nrows data_model.meta.instrument.name = 'NIRCAM' bias_model = SuperBiasModel() bias_model.data = np.zeros(shape=(2048, 2048), dtype=np.float32) bias_model.dq = np.zeros(shape=(2048, 2048), dtype=np.int32) bias_model.meta.subarray.xstart = 1 bias_model.meta.subarray.ystart = 1 bias_model.meta.subarray.xsize = 2048 bias_model.meta.subarray.ysize = 2048 bias_model.meta.instrument.name = 'NIRCAM' bias_model.meta.description = 'Fake data.' bias_model.meta.telescope = 'JWST' bias_model.meta.reftype = 'SuperBiasModel' bias_model.meta.author = 'Alicia' bias_model.meta.pedigree = 'Dummy' bias_model.meta.useafter = '2015-10-01T00:00:00' return data_model, bias_model
def get_superbias(self, file): """Read in superbias and deal with bad pixels""" sb_model = SuperBiasModel(file) sb = sb_model.data bad = np.isnan(sb) sb[bad] = 0. return sb
def _cube(xstart, ystart, ngroups, nrows, ncols): nints = 1 # create a JWST datamodel for NIRCam SUB320A335R data data_model = RampModel((nints, ngroups, nrows, ncols)) data_model.meta.subarray.name = 'SUB320A335R' data_model.meta.subarray.xstart = xstart data_model.meta.subarray.ystart = ystart data_model.meta.subarray.xsize = ncols data_model.meta.subarray.ysize = nrows data_model.meta.exposure.ngroups = ngroups data_model.meta.instrument.name = 'NIRCAM' data_model.meta.instrument.detector = 'NRCALONG' data_model.meta.observation.date = '2019-10-14' data_model.meta.observation.time = '16:44:12.000' # create a superbias model for the superbias step bias_model = SuperBiasModel((2048, 2048)) bias_model.meta.subarray.xstart = 1 bias_model.meta.subarray.ystart = 1 bias_model.meta.subarray.xsize = 2048 bias_model.meta.subarray.ysize = 2048 bias_model.meta.instrument.name = 'NIRCAM' bias_model.meta.description = 'Fake data.' bias_model.meta.telescope = 'JWST' bias_model.meta.reftype = 'SuperBiasModel' bias_model.meta.author = 'Alicia' bias_model.meta.pedigree = 'Dummy' bias_model.meta.useafter = '2015-10-01T00:00:00' return data_model, bias_model
def compare(self): #run the pipeline on the input ramp pipeout = self.run_superbias_step() #Manual superbias subtraction sb = SuperBiasModel(self.sbfile) ramp = RampModel(self.infile) superbias = sb.data #extract the appropriate subarray from the reference #file if necessary if ramp.data.shape[-2] != 2048: xs, xe, ys, ye = ex.get_coords_rampmodel(ramp) superbias = sb.data[ys:ye, xs:xe] #subtract superbias ramp.data -= superbias #Compare pipeline output with manual output self.test_superbias_sub(pipeout.data, ramp.data) self.test_pixeldq_propagation(pipeout, sb) print("Superbias testing complete")
#! /usr/bin/env python ''' Modify a superbias reference file for pipeline testing. Currently all we should need to do is make sure that the reference file contains at least one pixel flagged with UNRELIABLE_BIAS, so that in testing we can make sure that the superbias is still subtracted from this pixel ''' from jwst.datamodels import SuperBiasModel infile = '../reffiles/A1_superbias_from_list_of_biasfiles.list.fits' outfile = 'modified_superbias_reffile.fits' #read in reffile sb = SuperBiasModel(infile) #set the unreliable_bias flag for one pixel sb.dq[50, 50] = 2 #save sb.save(outfile)
def save_superbias(superbias, error, dq, instrument='', detector='', subarray='GENERIC', readpatt='ANY', outfile='superbias_jwst_reffiles.fits', author='jwst_reffiles', description='Super Bias Image', pedigree='GROUND', useafter='2000-01-01T00:00:00', history='', fastaxis=-1, slowaxis=2, substrt1=1, substrt2=1, filenames=[]): """Saves a CRDS-formatted superbias reference file. Parameters ---------- superbias : numpy.ndarray The 2D superbias image. error : numpy.ndarray The 2D superbias error image. dq : numpy.ndarray The 2D superbias data quality image. instrument : str CRDS-required instrument for which to use this reference file for. detector : str CRDS-required detector for which to use this reference file for. subarray : str CRDS-required subarray for which to use this reference file for. readpatt : str CRDS-required read pattern for which to use this reference file for. outfile : str Name of the CRDS-formatted superbias reference file to save the final superbias map to. author : str CRDS-required name of the reference file author, to be placed in the referece file header. description : str CRDS-required description of the reference file, to be placed in the reference file header. pedigree : str CRDS-required pedigree of the data used to create the reference file. useafter : str CRDS-required date of earliest data with which this referece file should be used. (e.g. '2019-04-01T00:00:00'). history : str CRDS-required history section to place in the reference file header. fastaxis : int CRDS-required fastaxis of the reference file. slowaxis : int CRDS-required slowaxis of the reference file. substrt1 : int CRDS-required starting pixel in axis 1 direction. substrt2 : int CRDS-required starting pixel in axis 2 direction. filenames : list List of dark current files that were used to generate the reference file. """ s = SuperBiasModel() s.data = superbias s.err = error s.dq = dq s.dq_def = [(0, 0, 'GOOD', ''), (0, 1, 'DO_NOT_USE', ''), (1, 2, 'UNRELIABLE_BIAS', '')] s.meta.instrument.name = instrument s.meta.instrument.detector = detector s.meta.subarray.name = subarray s.meta.exposure.readpatt = readpatt s.meta.author = author s.meta.description = description s.meta.pedigree = pedigree s.meta.useafter = useafter s.meta.subarray.fastaxis = fastaxis s.meta.subarray.slowaxis = slowaxis s.meta.reftype = 'SUPERBIAS' yd, xd = superbias.shape s.meta.subarray.xstart = substrt1 s.meta.subarray.xsize = xd s.meta.subarray.ystart = substrt2 s.meta.subarray.ysize = yd package_note = ('This file was created using the superbias.py module ' 'within the jwst_reffiles package.') software_dict = {'name': 'jwst_reffiles.superbias.py', 'author': 'STScI', 'homepage': 'https://github.com/spacetelescope/jwst_reffiles', 'version': '0.0.0'} entry = util.create_history_entry(package_note, software=software_dict) s.history.append(entry) # Add the list of input files used to create the superbias reference file s.history.append('DATA USED:') for f in filenames: f = os.path.basename(f) totlen = len(f) div = np.arange(0, totlen, 60) for val in div: if totlen > (val+60): s.history.append(util.create_history_entry(f[val:val+60])) else: s.history.append(util.create_history_entry(f[val:])) if history != '': s.history.append(util.create_history_entry(history)) s.save(outfile, overwrite=True) print('Final CRDS-formatted superbias map saved to {}'.format(outfile))
def make_files(self): #loop over detector for det in self.detectors: satfile = [s for s in self.satfiles if det in s][0] sat_model = SaturationModel(satfile) sat = sat_model.data #pixels with saturation level of 0. #set sat level to 65535 bad = sat == 0 sat[bad] = 65535 linfile = [s for s in self.linfiles if det in s][0] lin_model = LinearityModel(linfile) lin = lin_model.coeffs #pixels with bad linearity coeffs #set so no correction is applied nans = np.isnan(lin[1, :, :]) tmp = lin[1, :, :] tmp[nans] = 1. lin[1, :, :] = tmp for i in range(2, 7): tmp = lin[i, :, :] nans = np.isnan(tmp) tmp[nans] = 0. lin[i, :, :] = tmp #superbias file sbfile = [s for s in self.sbfiles if det in s][0] sb_model = SuperBiasModel(sbfile) superbias = sb_model.data #linearize the saturation values sat_lin = self.linearize(sat - superbias, lin) #loop over readout patterns for readpat in self.readpatts: nframe, nskip = self.readpatts[readpat] #optional output plot if self.plot: xx = 400 yy = 400 fsize = 12 f = plt.figure() a = f.add_subplot(111) a2 = a.twiny() f.subplots_adjust(bottom=0.2) xs = np.arange(self.maxgroups * (nframe + nskip)) a.plot(xs, np.repeat(sat[yy, xx], len(xs)), linestyle=':', color='blue', linewidth=2, label='Original Saturation') a.plot(xs, np.repeat(sat_lin[yy, xx] + superbias[yy, xx], len(xs)), linestyle=':', color='red', linewidth=2, label='Linearized Saturation') a.plot(xs, np.repeat(superbias[yy, xx], len(xs)), linestyle=':', color='black', linewidth=2, label='Superbias Level') #loop over groups satramp = np.zeros((self.maxgroups, 2048, 2048)) linsatramp = np.zeros((self.maxgroups, 2048, 2048)) xfmeans = [] for i in range(self.maxgroups): #exposure time to the final frame in the group exptime = self.frametime * (nframe + nskip) * (i + 1) satslope = sat_lin / exptime #now calculate signals for each frame within the #group, by reducing exposure time by one frametime #for each fsigs = np.zeros((nframe, 2048, 2048)) fsigs[0, :, :] = sat_lin for frame in range(1, nframe): fsigs[frame, :, :] = satslope * ( exptime - (self.frametime * frame)) linsatramp[i, :, :] = np.mean(fsigs, axis=0) + superbias #non-linearize fsigs fsigs_nl = self.unlinearize(fsigs, lin, sat - superbias) #add superbias back in fsigs_nl += superbias satramp[i, :, :] = np.mean(fsigs_nl, axis=0) #print("Group: {}".format(i)) #print("Exptime: {}".format(exptime)) #print("Sat: {}, Lin Sat: {}".format(sat[yy,xx],sat_lin[yy,xx])) #print("Satslope: {}".format(satslope[yy,xx])) #print("Frame exps: {}".format(exptime-(self.frametime*np.arange(1,nframe)))) #print("signals: {}".format(fsigs[:,yy,xx])) #print("mean signal: {}".format(satramp[i,yy,xx])) #if i == 1: # stop if self.plot: xf = np.array([-1]) xf = np.append( xf, np.arange((i + 1) * (nframe + nskip) - nframe, (i + 1) * (nframe + nskip))) yf = np.array([superbias[yy, xx]]) yf = np.append( yf, fsigs[:, yy, xx][::-1] + superbias[yy, xx]) yfnl = np.array([superbias[yy, xx]]) yfnl = np.append(yfnl, fsigs_nl[:, yy, xx][::-1]) if i == 9: a.plot(xf, yf, mfc='red', mec='red', color='black', marker='o', linestyle='-', label='Linearized Frames', alpha=0.5) #a.plot(np.mean(xf[1:]),linsatramp[i,yy,xx],color='red',marker='8',mfc='none',mec='red',markersize=10,label='Linearized Frames Mean') a.plot(xf, yfnl, mfc='black', mec='black', color='black', marker='o', linestyle='-', label='Non-Linearized Frames', alpha=0.5) #a.plot(np.mean(xf[1:]),linsatramp[i,yy,xx],color='red',marker='8',mfc='none',mec='red',markersize=10,label='Linearized Frames Mean') else: a.plot(xf, yf, mfc='red', mec='red', color='black', marker='o', linestyle='-', alpha=0.5) #a.plot(np.mean(xf[1:]),linsatramp[i,yy,xx],color='red',mfc='none',mec='red',marker='8',markersize=10) a.plot(xf, yfnl, mfc='black', mec='black', color='black', marker='o', linestyle='-', alpha=0.5) xfmeans.append(np.mean(xf[1:])) #optional plot if self.plot: new_tick_locations = np.array(xfmeans) a.plot(xfmeans, satramp[:, yy, xx], color='blue', marker='8', markersize=10, label='Non-Linear Frames Mean') a.legend(loc='lower right', numpoints=1, fontsize=fsize) a.set_xlabel('Frame Number') a.set_ylabel('Signal (DN)') a.set_title('NRC{}, Readpattern: {}, Pixel ({},{})'.format( det, readpat, xx, yy)) a.set_xlim(-1, np.max(xf) + 1) a.set_ylim(0, 60000) # Move twinned axis ticks and label from top to bottom a2.xaxis.set_ticks_position("bottom") a2.xaxis.set_label_position("bottom") # Offset the twin axis below the host a2.spines["bottom"].set_position(("axes", -0.15)) # Turn on the frame for the twin axis, but then hide all # but the bottom spine a2.set_frame_on(True) a2.patch.set_visible(False) for sp in a2.spines.itervalues(): sp.set_visible(False) a2.spines["bottom"].set_visible(True) a2.set_xticks(new_tick_locations) a2.set_xticklabels(np.arange(len(new_tick_locations))) a2.set_xlabel("Group Number") f.savefig( 'GroupSatPlot_forTR_{}_{}_C_filledcircs.png'.format( det, readpat)) #save output file self.savefile(satramp, det, readpat)