def testAlternateBandpassesGalaxies(self): """ the same as testAlternateBandpassesStars, but for galaxies """ catName = os.path.join( getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'testPhotMix_testAlternateBandpassesGalaxies.txt') if os.path.exists(catName): os.unlink(catName) obs_metadata_pointed = ObservationMetaData(mjd=50000.0, boundType='circle', pointingRA=0.0, pointingDec=0.0, boundLength=10.0) test_cat = cartoonGalaxies(self.galaxy, obs_metadata=obs_metadata_pointed) test_cat.write_catalog(catName) dtype = np.dtype([('galid', np.int), ('ra', np.float), ('dec', np.float), ('uTotal', np.float), ('gTotal', np.float), ('rTotal', np.float), ('iTotal', np.float), ('zTotal', np.float), ('uBulge', np.float), ('gBulge', np.float), ('rBulge', np.float), ('iBulge', np.float), ('zBulge', np.float), ('uDisk', np.float), ('gDisk', np.float), ('rDisk', np.float), ('iDisk', np.float), ('zDisk', np.float), ('uAgn', np.float), ('gAgn', np.float), ('rAgn', np.float), ('iAgn', np.float), ('zAgn', np.float), ('bulgeName', str, 200), ('bulgeNorm', np.float), ('bulgeAv', np.float), ('diskName', str, 200), ('diskNorm', np.float), ('diskAv', np.float), ('agnName', str, 200), ('agnNorm', np.float), ('redshift', np.float)]) catData = np.genfromtxt(catName, dtype=dtype, delimiter=', ') self.assertGreater(len(catData), 0) cartoonDir = getPackageDir('sims_photUtils') cartoonDir = os.path.join(cartoonDir, 'tests', 'cartoonSedTestData') sedDir = getPackageDir('sims_sed_library') testBandpasses = {} keys = ['u', 'g', 'r', 'i', 'z'] for kk in keys: testBandpasses[kk] = Bandpass() testBandpasses[kk].readThroughput( os.path.join(cartoonDir, "test_bandpass_%s.dat" % kk)) imsimBand = Bandpass() imsimBand.imsimBandpass() specMap = defaultSpecMap ct = 0 for line in catData: bulgeMagList = [] diskMagList = [] agnMagList = [] if line['bulgeName'] == 'None': for bp in keys: np.testing.assert_equal(line['%sBulge' % bp], np.NaN) bulgeMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda( os.path.join(sedDir, specMap[line['bulgeName']])) fnorm = dummySed.calcFluxNorm(line['bulgeNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) a_int, b_int = dummySed.setupCCMab() dummySed.addCCMDust(a_int, b_int, A_v=line['bulgeAv']) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sBulge' % bpName], 10) bulgeMagList.append(mag) if line['diskName'] == 'None': for bp in keys: np.assert_equal(line['%sDisk' % bp], np.NaN) diskMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda( os.path.join(sedDir, specMap[line['diskName']])) fnorm = dummySed.calcFluxNorm(line['diskNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) a_int, b_int = dummySed.setupCCMab() dummySed.addCCMDust(a_int, b_int, A_v=line['diskAv']) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sDisk' % bpName], 10) diskMagList.append(mag) if line['agnName'] == 'None': for bp in keys: np.testing.assert_true(line['%sAgn' % bp], np.NaN) agnMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda( os.path.join(sedDir, specMap[line['agnName']])) fnorm = dummySed.calcFluxNorm(line['agnNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sAgn' % bpName], 10) agnMagList.append(mag) totalMags = PhotometryGalaxies().sum_magnitudes( bulge=np.array(bulgeMagList), disk=np.array(diskMagList), agn=np.array(agnMagList)) for testMag, bpName in zip(totalMags, keys): if np.isnan(line['%sTotal' % bpName]): np.testing.assert_equal(testMag, np.NaN) else: self.assertAlmostEqual(testMag, line['%sTotal' % bpName], 10) self.assertGreater(ct, 0) if os.path.exists(catName): os.unlink(catName)
def SNObjectSED(self, time, wavelen=None, bandpass=None, applyExtinction=True): ''' return a `lsst.sims.photUtils.sed` object from the SN model at the requested time and wavelengths with or without extinction from MW according to the SED extinction methods. The wavelengths may be obtained from a `lsst.sims.Bandpass` object or a `lsst.sims.BandpassDict` object instead. (Currently, these have the same wavelengths). See notes for details on handling of exceptions. If the sed is requested at times outside the validity range of the model, the flux density is returned as 0. If the time is within the range of validity of the model, but the wavelength range requested is outside the range, then the returned fluxes are np.nan outside the range, and the model fluxes inside Parameters ---------- time: float time of observation wavelen: `np.ndarray` of floats, optional, defaults to None array containing wavelengths in nm bandpass: `lsst.sims.photUtils.Bandpass` object or `lsst.sims.photUtils.BandpassDict`, optional, defaults to `None`. Using the dict assumes that the wavelength sampling and range is the same for all elements of the dict. if provided, overrides wavelen input and the SED is obtained at the wavelength values native to bandpass object. Returns ------- `sims_photutils.sed` object containing the wavelengths and SED values from the SN at time time in units of ergs/cm^2/sec/nm .. note: If both wavelen and bandpassobject are `None` then exception, will be raised. Examples -------- >>> sed = SN.SNObjectSED(time=0., wavelen=wavenm) ''' if wavelen is None and bandpass is None: raise ValueError('A non None input to either wavelen or\ bandpassobject must be provided') # if bandpassobject present, it overrides wavelen if bandpass is not None: if isinstance(bandpass, BandpassDict): firstfilter = bandpass.keys()[0] bp = bandpass[firstfilter] else: bp = bandpass # remember this is in nm wavelen = bp.wavelen flambda = np.zeros(len(wavelen)) # self.mintime() and self.maxtime() are properties describing # the ranges of SNCosmo.Model in time. # Set SED to 0 beyond the model phase range, will change this if # SNCosmo includes a more sensible decay later. if (time > self.mintime()) & (time < self.maxtime()): # If SNCosmo is requested a SED value beyond the model range # it will crash. Try to prevent that by returning np.nan for # such wavelengths. This will still not help band flux calculations # but helps us get past this stage. flambda = flambda * np.nan # Convert to Ang wave = wavelen * 10.0 mask1 = wave > self.minwave() mask2 = wave < self.maxwave() mask = mask1 & mask2 wave = wave[mask] # flux density dE/dlambda returned from SNCosmo in # ergs/cm^2/sec/Ang, convert to ergs/cm^2/sec/nm flambda[mask] = self.flux(time=time, wave=wave) flambda[mask] = flambda[mask] * 10.0 SEDfromSNcosmo = Sed(wavelen=wavelen, flambda=flambda) if not applyExtinction: return SEDfromSNcosmo # Apply LSST extinction ax, bx = SEDfromSNcosmo.setupCCMab() if self.ebvofMW is None: raise ValueError('ebvofMW attribute cannot be None Type and must' ' be set by hand using set_MWebv before this' 'stage, or by using setcoords followed by' 'mwEBVfromMaps\n') SEDfromSNcosmo.addCCMDust(a_x=ax, b_x=bx, ebv=self.ebvofMW) return SEDfromSNcosmo
def SNObjectSED(self, time, wavelen=None, bandpass=None, applyExtinction=True): ''' return a `lsst.sims.photUtils.sed` object from the SN model at the requested time and wavelengths with or without extinction from MW according to the SED extinction methods. The wavelengths may be obtained from a `lsst.sims.Bandpass` object or a `lsst.sims.BandpassDict` object instead. (Currently, these have the same wavelengths). See notes for details on handling of exceptions. If the sed is requested at times outside the validity range of the model, the flux density is returned as 0. If the time is within the range of validity of the model, but the wavelength range requested is outside the range, then the returned fluxes are np.nan outside the range, and the model fluxes inside Parameters ---------- time: float time of observation wavelen: `np.ndarray` of floats, optional, defaults to None array containing wavelengths in nm bandpass: `lsst.sims.photUtils.Bandpass` object or `lsst.sims.photUtils.BandpassDict`, optional, defaults to `None`. Using the dict assumes that the wavelength sampling and range is the same for all elements of the dict. if provided, overrides wavelen input and the SED is obtained at the wavelength values native to bandpass object. Returns ------- `sims_photutils.sed` object containing the wavelengths and SED values from the SN at time time in units of ergs/cm^2/sec/nm .. note: If both wavelen and bandpassobject are `None` then exception, will be raised. Examples -------- >>> sed = SN.SNObjectSED(time=0., wavelen=wavenm) ''' if wavelen is None and bandpass is None: raise ValueError('A non None input to either wavelen or\ bandpassobject must be provided') # if bandpassobject present, it overrides wavelen if bandpass is not None: if isinstance(bandpass, BandpassDict): firstfilter = bandpass.keys()[0] bp = bandpass[firstfilter] else: bp = bandpass # remember this is in nm wavelen = bp.wavelen flambda = np.zeros(len(wavelen)) # self.mintime() and self.maxtime() are properties describing # the ranges of SNCosmo.Model in time. Behavior beyond this is # determined by self.modelOutSideTemporalRange if (time >= self.mintime()) and (time <= self.maxtime()): # If SNCosmo is requested a SED value beyond the wavelength range # of model it will crash. Try to prevent that by returning np.nan for # such wavelengths. This will still not help band flux calculations # but helps us get past this stage. flambda = flambda * np.nan # Convert to Ang wave = wavelen * 10.0 mask1 = wave >= self.minwave() mask2 = wave <= self.maxwave() mask = mask1 & mask2 wave = wave[mask] # flux density dE/dlambda returned from SNCosmo in # ergs/cm^2/sec/Ang, convert to ergs/cm^2/sec/nm flambda[mask] = self.flux(time=time, wave=wave) flambda[mask] = flambda[mask] * 10.0 else: # use prescription for modelOutSideTemporalRange if self.modelOutSideTemporalRange != 'zero': raise NotImplementedError('Model not implemented, change to zero\n') # Else Do nothing as flambda is already 0. # This takes precedence over being outside wavelength range if self.rectifySED: # Note that this converts nans into 0. flambda = np.where(flambda > 0., flambda, 0.) SEDfromSNcosmo = Sed(wavelen=wavelen, flambda=flambda) if not applyExtinction: return SEDfromSNcosmo # Apply LSST extinction global _sn_ax_cache global _sn_bx_cache global _sn_ax_bx_wavelen if _sn_ax_bx_wavelen is None \ or len(wavelen)!=len(_sn_ax_bx_wavelen) \ or (wavelen!=_sn_ax_bx_wavelen).any(): ax, bx = SEDfromSNcosmo.setupCCMab() _sn_ax_cache = ax _sn_bx_cache = bx _sn_ax_bx_wavelen = np.copy(wavelen) else: ax = _sn_ax_cache bx = _sn_bx_cache if self.ebvofMW is None: raise ValueError('ebvofMW attribute cannot be None Type and must' ' be set by hand using set_MWebv before this' 'stage, or by using setcoords followed by' 'mwEBVfromMaps\n') SEDfromSNcosmo.addCCMDust(a_x=ax, b_x=bx, ebv=self.ebvofMW) return SEDfromSNcosmo