def test_filter_scale(): # Test scale_in_filter() method which is called in coadding wave = np.arange(3000., 10000.) flux = np.ones_like(wave) gdm = np.ones_like(wave, dtype=bool) # par = Coadd1DPar() par['filter'] = 'DECAM-R' par['filter_mag'] = 17. # Run scale = flux_calib.scale_in_filter(wave, flux, gdm, par) assert np.isclose(scale, 41.698475048180406, rtol=1e-3)
def run(self): """ Runs the coadding """ # Load the data self.waves, self.fluxes, self.ivars, self.gpms, self.header = self.load_arrays() # Coadd the data self.wave_grid_mid, self.wave_coadd, self.flux_coadd, self.ivar_coadd, self.gpm_coadd = self.coadd() # Scale to a filter magnitude? if self.par['filter'] != 'none': scale = flux_calib.scale_in_filter(self.wave_coadd, self.flux_coadd, self.gpm_coadd, self.par) self.flux_coadd *= scale self.ivar_coadd = self.ivar_coadd / scale**2
def order_phot_scale(spectra, phot_scale_dicts, nsig=3.0, niter=5, debug=False): ''' Scale coadded spectra with photometric data. Parameters: spectra: XSpectrum1D spectra (longslit) or spectra list (echelle) phot_scale_dicts: A dict contains photometric information of each orders (if echelle). An example is given below. phot_scale_dicts = {0: {'filter': None, 'mag': None, 'mag_type': None, 'masks': None}, 1: {'filter': 'UKIRT-Y', 'mag': 20.33, 'mag_type': 'AB', 'masks': None}, 2: {'filter': 'UKIRT-J', 'mag': 20.19, 'mag_type': 'AB', 'masks': None}, 3: {'filter': 'UKIRT-H', 'mag': 20.02, 'mag_type': 'AB', 'masks': None}, 4: {'filter': 'UKIRT-K', 'mag': 19.92, 'mag_type': 'AB', 'masks': None}} Show QA plot if debug=True Return a new scaled XSpectrum1D spectra ''' from pypeit.core.flux_calib import scale_in_filter norder = spectra.nspec # scaling spectrum order by order. spectra_list_new = [] for iord in range(norder): phot_scale_dict = phot_scale_dicts[iord] if (phot_scale_dict['filter'] is not None) & (phot_scale_dict['mag'] is not None): speci = scale_in_filter(spectra[iord], phot_scale_dict) else: #ToDo: Think a better way to do the following try: spec0 = scale_in_filter(spectra[iord - 1], phot_scale_dicts[iord - 1]) speci = spectra[iord] med_flux = spec0.data['flux'] / speci.data['flux'] mn_scale, med_scale, std_scale = stats.sigma_clipped_stats( med_flux, sigma=nsig, iters=niter) med_scale = np.minimum(med_scale, 5.0) spectra.data['flux'] *= med_scale spectra.data['sig'] *= med_scale msgs.warn( "Not enough photometric information given. Scaled order {:d} to order {:d}" .format(iord, iord - 1)) except KeyError: msgs.warn( "Not enough photometric information given. Scale order {:d} to order {:d} failed" .format(iord, iord - 1)) try: spec0 = scale_in_filter(spectra[iord + 1], phot_scale_dicts[iord + 1]) speci = spectra[iord] med_flux = spec0.data['flux'] / speci.data['flux'] mn_scale, med_scale, std_scale = stats.sigma_clipped_stats( med_flux, sigma=nsig, iters=niter) med_scale = np.minimum(med_scale, 5.0) speci.data['flux'] *= med_scale speci.data['sig'] *= med_scale msgs.warn( "Not enough photometric information given. Scaled order {:d} to order {:d}" .format(iord, iord + 1)) except: msgs.warn( "Not enough photometric information given. No scaling on order {:d}" .format(iord)) speci = spectra[iord] spectra_list_new.append(speci) if debug: gdp = speci.sig > 0 plt.plot(spectra[iord].wavelength[gdp], spectra[iord].flux[gdp], 'k-', label='raw spectrum') plt.plot(speci.wavelength[gdp], speci.flux[gdp], 'b-', label='scaled spectrum') mny, medy, stdy = stats.sigma_clipped_stats(speci.flux[gdp], sigma=3, iters=5) plt.ylim([0.1 * medy, 4.0 * medy]) plt.legend() plt.xlabel('wavelength') plt.ylabel('Flux') plt.show() return collate(spectra_list_new)