def init_dict(): sciimg = np.ones((1000, 1000)).astype(float) # Slits left = np.full((1000, 3), 2, dtype=float) right = np.full((1000, 3), 8, dtype=float) left[:, 1] = 15. right[:, 1] = 21. left[:, 2] = 25. right[:, 2] = 31. slits = slittrace.SlitTraceSet(left, right, 'MultiSlit', nspat=1000, PYP_SPEC='dummy') # sdict = dict( sciimg=sciimg, ivarraw=0.1 * np.ones_like(sciimg), skymodel=0.95 * np.ones_like(sciimg), objmodel=np.ones_like(sciimg), ivarmodel=0.05 * np.ones_like(sciimg), waveimg=1000 * np.ones_like(sciimg), bpmmask=np.ones_like(sciimg).astype(int), det=1, detector=None, slits=slits, tilts=np.ones_like(sciimg).astype(float), #tilts=wavetilts.WaveTilts(**test_wavetilts.instant_dict), sci_spat_flexure=3.5, ) return sdict
def generate_mask(self): """Generate the mask of sky regions Returns: ndarray : Boolean mask containing sky regions """ nreg = 0 left_edg, righ_edg = np.zeros((self.nspec, 0)), np.zeros( (self.nspec, 0)) spec_min, spec_max = np.array([]), np.array([]) for sl in range(self._nslits): diff = self.slits_right[:, sl] - self.slits_left[:, sl] tmp = np.zeros(self._resolution + 2) tmp[1:-1] = self._skyreg[sl] wl = np.where(tmp[1:] > tmp[:-1])[0] wr = np.where(tmp[1:] < tmp[:-1])[0] for rr in range(wl.size): left = self.slits_left[:, sl] + wl[rr] * diff / ( self._resolution - 1.0) righ = self.slits_left[:, sl] + wr[rr] * diff / ( self._resolution - 1.0) left_edg = np.append(left_edg, left[:, np.newaxis], axis=1) righ_edg = np.append(righ_edg, righ[:, np.newaxis], axis=1) nreg += 1 spec_min = np.append(spec_min, self.slits.specmin[sl]) spec_max = np.append(spec_max, self.slits.specmax[sl]) # Instantiate the regions regions = slittrace.SlitTraceSet(left_edg, righ_edg, self.pypeline, nspec=self.nspec, nspat=self.nspat, mask=self.slits.mask, specmin=spec_min, specmax=spec_max, binspec=self.slits.binspec, binspat=self.slits.binspat, pad=0) # Generate the mask, and return return (regions.slit_img(use_spatial=False) >= 0).astype(np.int)
def init_dict(): sciimg = np.ones((1000, 1000)).astype(float) # Slits left = np.full((1000, 3), 2, dtype=float) right = np.full((1000, 3), 8, dtype=float) left[:, 1] = 15. right[:, 1] = 21. left[:, 2] = 25. right[:, 2] = 31. slits = slittrace.SlitTraceSet(left, right, 'MultiSlit', nspat=1000, PYP_SPEC='dummy') # Construct table of spectral flexure spec_flex_table = Table() spec_flex_table['spat_id'] = slits.spat_id spec_flex_table['sci_spec_flexure'] = np.zeros(left.shape[1]) # sdict = dict( sciimg=sciimg, ivarraw=0.1 * np.ones_like(sciimg), skymodel=0.95 * np.ones_like(sciimg), objmodel=np.ones_like(sciimg), ivarmodel=0.05 * np.ones_like(sciimg), scaleimg=np.ones_like(sciimg), waveimg=1000 * np.ones_like(sciimg), bpmmask=np.ones_like(sciimg).astype(int), det=1, detector=None, slits=slits, tilts=np.ones_like(sciimg).astype(float), #tilts=wavetilts.WaveTilts(**test_wavetilts.instant_dict), sci_spat_flexure=3.5, sci_spec_flexure=spec_flex_table, vel_type='HELIOCENTRIC', vel_corr=1.0 + 1.0e-5) return sdict
def test_wavecalib(): "Fuss with the WaveCalib DataContainer" out_file = data_path('test_wavecalib.fits') if os.path.isfile(out_file): os.remove(out_file) # Pieces pypeitFit = fitting.PypeItFit(fitc=np.arange(5).astype(float), xval=np.linspace(1, 100., 100)) # 2D fit pypeitFit2 = fitting.PypeItFit(fitc=np.linspace((1, 2), (10, 20), 10), xval=np.linspace(1, 100., 100), x2=np.linspace(1, 100., 100)) waveFit = wv_fitting.WaveFit(232, pypeitfit=pypeitFit, pixel_fit=np.arange(10).astype(float), wave_fit=np.linspace(1., 10., 10)) waveCalib = wavecalib.WaveCalib(wv_fits=np.asarray([waveFit]), nslits=1, spat_ids=np.asarray([232]), wv_fit2d=pypeitFit2) # Write waveCalib.to_file(out_file) # Read waveCalib2 = wavecalib.WaveCalib.from_file(out_file) # Test assert np.array_equal(waveCalib.spat_ids, waveCalib2.spat_ids), 'Bad spat_ids' assert np.array_equal(waveCalib.wv_fits[0].pypeitfit.fitc, waveCalib2.wv_fits[0].pypeitfit.fitc), 'Bad fitc' assert np.array_equal(waveCalib.wv_fit2d.xval, waveCalib2.wv_fit2d.xval) # Write again! waveCalib2.to_file(out_file, overwrite=True) # Finish os.remove(out_file) # With None (failed wave) spat_ids = np.asarray([232, 949]) waveCalib3 = wavecalib.WaveCalib(wv_fits=np.asarray( [waveFit, wv_fitting.WaveFit(949)]), nslits=2, spat_ids=spat_ids, wv_fit2d=pypeitFit2) waveCalib3.to_file(out_file) waveCalib4 = wavecalib.WaveCalib.from_file(out_file) # Check masking slits = slittrace.SlitTraceSet(left_init=np.full((1000, 2), 2, dtype=float), right_init=np.full((1000, 2), 8, dtype=float), pypeline='MultiSlit', spat_id=spat_ids, nspat=2, PYP_SPEC='dummy') slits.mask_wvcalib(waveCalib3) assert slits.bitmask.flagged(slits.mask[1], flag='BADWVCALIB') # Finish os.remove(out_file)
def create_pseudo_image(self, coadd_list): """ ..todo.. see below THIS UNDOCUMENTED CODE PROBABLY SHOULD GENERATE AND RETURN STANDARD PYPEIT OBJCTS INSTEAD OF SOME UNDEFINED DICT""" # Masking # TODO -- Make this a method or something slits = self.stack_dict['slits_list'][0] reduce_bpm = (slits.mask > 0) & (np.invert(slits.bitmask.flagged( slits.mask, flag=slits.bitmask.exclude_for_reducing))) good_slits = np.where(np.invert(reduce_bpm))[0] nspec_vec = np.zeros(self.nslits,dtype=int) nspat_vec = np.zeros(self.nslits,dtype=int) for kk, cdict in enumerate(coadd_list): islit = good_slits[kk] nspec_vec[islit]=cdict['nspec'] nspat_vec[islit]=cdict['nspat'] # Determine the size of the pseudo image nspat_pad = 10 nspec_pseudo = nspec_vec.max() nspat_pseudo = int(np.sum(nspat_vec) + (self.nslits + 1)*nspat_pad) # Cast for SlitTraceSet spec_vec_pseudo = np.arange(nspec_pseudo) shape_pseudo = (nspec_pseudo, nspat_pseudo) imgminsky_pseudo = np.zeros(shape_pseudo) sciivar_pseudo = np.zeros(shape_pseudo) waveimg_pseudo = np.zeros(shape_pseudo) tilts_pseudo = np.zeros(shape_pseudo) spat_img_pseudo = np.zeros(shape_pseudo) nused_pseudo = np.zeros(shape_pseudo, dtype=int) inmask_pseudo = np.zeros(shape_pseudo, dtype=bool) wave_mid = np.zeros((nspec_pseudo, self.nslits)) wave_mask = np.zeros((nspec_pseudo, self.nslits),dtype=bool) wave_min = np.zeros((nspec_pseudo, self.nslits)) wave_max = np.zeros((nspec_pseudo, self.nslits)) dspat_mid = np.zeros((nspat_pseudo, self.nslits)) spat_left = nspat_pad slit_left = np.zeros((nspec_pseudo, self.nslits)) slit_righ = np.zeros((nspec_pseudo, self.nslits)) spec_min1 = np.zeros(self.nslits) spec_max1 = np.zeros(self.nslits) nspec_grid = self.wave_grid_mid.size for kk, coadd_dict in enumerate(coadd_list): islit = good_slits[kk] spat_righ = spat_left + nspat_vec[islit] ispec = slice(0,nspec_vec[islit]) ispat = slice(spat_left,spat_righ) imgminsky_pseudo[ispec, ispat] = coadd_dict['imgminsky'] sciivar_pseudo[ispec, ispat] = coadd_dict['sciivar'] waveimg_pseudo[ispec, ispat] = coadd_dict['waveimg'] tilts_pseudo[ispec, ispat] = coadd_dict['tilts'] # spat_img_pseudo is the sub-pixel image position on the rebinned pseudo image inmask_pseudo[ispec, ispat] = coadd_dict['outmask'] image_temp = (coadd_dict['dspat'] - coadd_dict['dspat_mid'][0] + spat_left)*coadd_dict['outmask'] spat_img_pseudo[ispec, ispat] = image_temp nused_pseudo[ispec, ispat] = coadd_dict['nused'] wave_min[ispec, islit] = coadd_dict['wave_min'] wave_max[ispec, islit] = coadd_dict['wave_max'] wave_mid[ispec, islit] = coadd_dict['wave_mid'] wave_mask[ispec, islit] = True # Fill in the rest of the wave_mid with the corresponding points in the wave_grid #wave_this = wave_mid[wave_mask[:,islit], islit] #ind_upper = np.argmin(np.abs(self.wave_grid_mid - wave_this.max())) + 1 #if nspec_vec[islit] != nspec_pseudo: # wave_mid[nspec_vec[islit]:, islit] = self.wave_grid_mid[ind_upper:ind_upper + (nspec_pseudo-nspec_vec[islit])] dspat_mid[ispat, islit] = coadd_dict['dspat_mid'] slit_left[:,islit] = np.full(nspec_pseudo, spat_left) slit_righ[:,islit] = np.full(nspec_pseudo, spat_righ) spec_max1[islit] = nspec_vec[islit]-1 spat_left = spat_righ + nspat_pad slits_pseudo \ = slittrace.SlitTraceSet(slit_left, slit_righ, self.pypeline, nspat=nspat_pseudo, PYP_SPEC=self.spectrograph.name, specmin=spec_min1, specmax=spec_max1, ech_order=slits.ech_order) #master_key=self.stack_dict['master_key_dict']['trace'], #master_dir=self.master_dir) slitmask_pseudo = slits_pseudo.slit_img() # This is a kludge to deal with cases where bad wavelengths result in large regions where the slit is poorly sampled, # which wreaks havoc on the local sky-subtraction min_slit_frac = 0.70 spec_min = np.zeros(self.nslits) spec_max = np.zeros(self.nslits) for slit_idx in good_slits: spat_id = slits_pseudo.spat_id[slit_idx] slit_width = np.sum(inmask_pseudo*(slitmask_pseudo == spat_id),axis=1) slit_width_img = np.outer(slit_width, np.ones(nspat_pseudo)) med_slit_width = np.median(slit_width_img[slitmask_pseudo == spat_id]) # TODO -- need inline docs nspec_eff = np.sum(slit_width > min_slit_frac*med_slit_width) nsmooth = int(np.fmax(np.ceil(nspec_eff*0.02),10)) slit_width_sm = ndimage.filters.median_filter(slit_width, size=nsmooth, mode='reflect') igood = (slit_width_sm > min_slit_frac*med_slit_width) # TODO -- need inline docs spec_min[slit_idx] = spec_vec_pseudo[igood].min() spec_max[slit_idx] = spec_vec_pseudo[igood].max() bad_pix = (slit_width_img < min_slit_frac*med_slit_width) & (slitmask_pseudo == spat_id) inmask_pseudo[bad_pix] = False # Update slits_pseudo slits_pseudo.specmin = spec_min slits_pseudo.specmax = spec_max return dict(nspec=nspec_pseudo, nspat=nspat_pseudo, imgminsky=imgminsky_pseudo, sciivar=sciivar_pseudo, inmask=inmask_pseudo, tilts=tilts_pseudo, waveimg=waveimg_pseudo, spat_img=spat_img_pseudo, slits=slits_pseudo, wave_mask=wave_mask, wave_mid=wave_mid, wave_min=wave_min, wave_max=wave_max)