def testFluxListForSedList(self): """ Test that fluxListForSedList calculates the correct fluxes """ nBandpasses = 7 bpNameList, bpList = self.getListOfBandpasses(nBandpasses) testBpDict = BandpassDict(bpList, bpNameList) nSed = 20 sedNameList = self.getListOfSedNames(nSed) magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList = self.rng.random_sample(nSed)*5.0 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 # first, test on an SedList without a wavelenMatch testSedList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList) fluxList = testBpDict.fluxListForSedList(testSedList) self.assertEqual(fluxList.shape[0], nSed) self.assertEqual(fluxList.shape[1], nBandpasses) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) for iy, bp in enumerate(testBpDict): flux = dummySed.calcFlux(bpList[iy]) self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) # now use wavelenMatch testSedList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList, wavelenMatch=testBpDict.wavelenMatch) fluxList = testBpDict.fluxListForSedList(testSedList) self.assertEqual(fluxList.shape[0], nSed) self.assertEqual(fluxList.shape[1], nBandpasses) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) for iy, bp in enumerate(testBpDict): flux = dummySed.calcFlux(bpList[iy]) self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
def Get_SED_Restframe(self, Sed_time): a = 1. / (1. + self.z) bandpass_besselb = Bandpass( wavelen=sncosmo.get_bandpass('bessellB').wave, sb=sncosmo.get_bandpass('bessellB').trans) print 'before', Sed_time.wavelen, Sed_time.flambda #print 'there we go',Sed_time.wavelen,Sed_time.flambda SED_rest = Sed(wavelen=Sed_time.wavelen * a * 10., flambda=Sed_time.flambda * np.power(self.lumidist, 2.) / a / 10. / HC_ERG_AA) print 'hello', Sed_time.wavelen * a * 10, Sed_time.flambda * np.power( self.lumidist, 2.) / a / 10. / HC_ERG_AA print 'heelp', SED_rest.wavelen, SED_rest.flambda SED_new = Sed(wavelen=SED_rest.wavelen / a, flambda=a * SED_rest.flambda / np.power(self.lumidist, 2.)) #print 'ici',SED_new.wavelen,SED_new.flambda #estimate the flux in the B band bessellb flux_B_rest = SED_rest.calcFlux(bandpass=bandpass_besselb) flux_B_new = SED_new.calcFlux(bandpass=bandpass_besselb) #now the magnitudes (apparent and absolute) vega_SED = Sed() vega_SED.readSED_flambda('vega.txt') flux_vega = vega_SED.calcFlux(bandpass=bandpass_besselb) mag_B_rest = -2.5 * np.log10(flux_B_rest / flux_vega) mag_B_new = -2.5 * np.log10(flux_B_new / 3631.) print 'hello', len(vega_SED.wavelen), len(vega_SED.flambda), len( bandpass_besselb.sb), flux_vega, flux_B_rest bwave = bandpass_besselb.wavelen btrans = bandpass_besselb.sb vega_wave = vega_SED.wavelen vega_flambda = vega_SED.flambda mask = ((vega_wave > bwave[0]) & (vega_wave < bwave[-1])) d = vega_wave[mask] f = vega_flambda[mask] trans = np.interp(d, bwave, btrans) binw = np.gradient(d) ftot = np.sum(f * trans * binw) print 'vega flux', flux_vega, ftot return SED_rest, mag_B_rest, mag_B_new
def _parallel_fitting(mag_array, redshift, redshift_true, H0, Om0, wav_min, wav_width, lsst_mag_array, out_dict, tag): pid = os.getpid() (sed_names, mag_norms, av_arr, rv_arr) = sed_from_galacticus_mags(mag_array, redshift, redshift_true, H0, Om0, wav_min, wav_width, lsst_mag_array) tot_bp_dict = BandpassDict.loadTotalBandpassesFromFiles() sed_dir = getPackageDir('sims_sed_library') lsst_fit_fluxes = np.zeros((6, len(sed_names)), dtype=float) t_start = time.time() ccm_w = None restframe_seds = {} imsim_bp = Bandpass() imsim_bp.imsimBandpass() n04_ln10 = -0.4 * np.log(10) for ii in range(len(sed_names)): av_val = av_arr[ii] rv_val = rv_arr[ii] sed_tag = '%s_%.3f_%.3f' % (sed_names[ii], av_val, rv_val) if sed_tag not in restframe_seds: rest_sed = Sed() rest_sed.readSED_flambda(os.path.join(sed_dir, sed_names[ii])) mag = rest_sed.calcMag(imsim_bp) if ccm_w is None or not np.array_equal(rest_sed.wavelen, ccm_w): ccm_w = np.copy(rest_sed.wavelen) ax, bx = rest_sed.setupCCM_ab() rest_sed.addDust(ax, bx, A_v=av_val, R_v=rv_val) restframe_seds[sed_tag] = (rest_sed, mag) for i_bp, bp in enumerate('ugrizy'): m_norm = mag_norms[i_bp][ii] if m_norm > 0.0 and not np.isfinite(m_norm): continue spec = Sed(wavelen=restframe_seds[sed_tag][0].wavelen, flambda=restframe_seds[sed_tag][0].flambda) fnorm = np.exp(n04_ln10 * (m_norm - restframe_seds[sed_tag][1])) try: assert np.isfinite(fnorm) assert fnorm > 0.0 except AssertionError: print('\n\nmagnorm %e\n\n' % (m_norm)) raise spec.multiplyFluxNorm(fnorm) spec.redshiftSED(redshift[ii], dimming=True) ff = spec.calcFlux(tot_bp_dict[bp]) lsst_fit_fluxes[i_bp][ii] = ff out_dict[tag] = (sed_names, mag_norms, av_arr, rv_arr, lsst_fit_fluxes)
def test_mags_vs_flux(self): """ Verify that the relationship between Sed.calcMag() and Sed.calcFlux() is as expected """ wavelen = np.arange(100.0, 1500.0, 1.0) flambda = np.exp(-0.5 * np.power((wavelen - 500.0) / 100.0, 2)) sb = (wavelen - 100.0) / 1400.0 ss = Sed(wavelen=wavelen, flambda=flambda) bp = Bandpass(wavelen=wavelen, sb=sb) mag = ss.calcMag(bp) flux = ss.calcFlux(bp) self.assertAlmostEqual(ss.magFromFlux(flux) / mag, 1.0, 10) self.assertAlmostEqual(ss.fluxFromMag(mag) / flux, 1.0, 10)
def test_mags_vs_flux(self): """ Verify that the relationship between Sed.calcMag() and Sed.calcFlux() is as expected """ wavelen = np.arange(100.0, 1500.0, 1.0) flambda = np.exp(-0.5*np.power((wavelen-500.0)/100.0,2)) sb = (wavelen-100.0)/1400.0 ss = Sed(wavelen=wavelen, flambda=flambda) bp = Bandpass(wavelen=wavelen, sb=sb) mag = ss.calcMag(bp) flux = ss.calcFlux(bp) self.assertAlmostEqual(ss.magFromFlux(flux)/mag, 1.0, 10) self.assertAlmostEqual(ss.fluxFromMag(mag)/flux, 1.0, 10)
def testFluxDictForSed(self): """ Test that fluxDictForSed calculates the correct fluxes """ wavelen = numpy.arange(10.0,2000.0,1.0) flux = (wavelen*2.0-5.0)*1.0e-6 spectrum = Sed(wavelen=wavelen, flambda=flux) for nBp in range(3, 10, 1): nameList, bpList = self.getListOfBandpasses(nBp) testDict = BandpassDict(bpList, nameList) self.assertFalse(len(testDict.values()[0].wavelen)==len(spectrum.wavelen)) fluxDict = testDict.fluxDictForSed(spectrum) for ix, (name, bp) in enumerate(zip(nameList, bpList)): fluxControl = spectrum.calcFlux(bp) self.assertAlmostEqual(fluxDict[name]/fluxControl, 1.0, 2)
def testSedMagErrors(self): """Test error handling at mag and adu calculation levels of sed.""" sedwavelen = np.arange(self.wmin+50, self.wmax, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) # Test handling in calcMag with warnings.catch_warnings(record=True) as w: mag = testsed.calcMag(self.testbandpass) self.assertEqual(len(w), 1) self.assertIn("non-overlap", str(w[-1].message)) np.testing.assert_equal(mag, np.NaN) # Test handling in calcADU with warnings.catch_warnings(record=True) as w: adu = testsed.calcADU(self.testbandpass, photParams=PhotometricParameters()) self.assertEqual(len(w), 1) self.assertIn("non-overlap", str(w[-1].message)) np.testing.assert_equal(adu, np.NaN) # Test handling in calcFlux with warnings.catch_warnings(record=True) as w: flux = testsed.calcFlux(self.testbandpass) self.assertEqual(len(w), 1) self.assertIn("non-overlap", str(w[-1].message)) np.testing.assert_equal(flux, np.NaN)
def testSedMagErrors(self): """Test error handling at mag and adu calculation levels of sed.""" sedwavelen = np.arange(self.wmin + 50, self.wmax, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) # Test handling in calcMag with warnings.catch_warnings(record=True) as w: mag = testsed.calcMag(self.testbandpass) self.assertEqual(len(w), 1) self.assertIn("non-overlap", str(w[-1].message)) np.testing.assert_equal(mag, np.NaN) # Test handling in calcADU with warnings.catch_warnings(record=True) as w: adu = testsed.calcADU(self.testbandpass, photParams=PhotometricParameters()) self.assertEqual(len(w), 1) self.assertIn("non-overlap", str(w[-1].message)) np.testing.assert_equal(adu, np.NaN) # Test handling in calcFlux with warnings.catch_warnings(record=True) as w: flux = testsed.calcFlux(self.testbandpass) self.assertEqual(len(w), 1) self.assertIn("non-overlap", str(w[-1].message)) np.testing.assert_equal(flux, np.NaN)
def Simulate_LC_last(self): sed_SN=self.SN.get_SED(self.obs['mjd']) #print 'SED loaded' for i in range(len(sed_SN.wavelen)): obs=self.obs[i] sed=Sed(wavelen=sed_SN.wavelen[i],flambda=sed_SN.flambda[i]) visittime=obs['exptime'] filtre=obs['band'][-1] photParams = PhotometricParameters(nexp=visittime/15.) e_per_sec = sed.calcADU(bandpass=self.transmission.lsst_atmos_aerosol[filtre], photParams=photParams) #number of ADU counts for expTime e_per_sec/=visittime/photParams.gain flux_SN=sed.calcFlux(bandpass=self.transmission.lsst_atmos_aerosol[filtre]) FWHMeff=obs['FWHMeff'] if flux_SN >0: mag_SN=-2.5 * np.log10(flux_SN / 3631.0) m5_calc,snr_m5_through=self.Get_m5(filtre,mag_SN,obs['sky'],photParams,FWHMeff) self.table_LC.add_row(('LSST::'+filtre,obs['mjd'],visittime,FWHMeff,obs['moon_frac'],obs['sky'],obs['kAtm'],obs['airmass'],obs['m5sigmadepth'],obs['Nexp'],e_per_sec,e_per_sec/snr_m5_through))
def testIndicesOnFlux(self): """ Test that, when you pass a list of indices into the calcFluxList methods, you get the correct fluxes out. """ nBandpasses = 7 nameList, bpList = self.getListOfBandpasses(nBandpasses) testBpDict = BandpassDict(bpList, nameList) # first try it with a single Sed wavelen = numpy.arange(10.0,2000.0,1.0) flux = (wavelen*2.0-5.0)*1.0e-6 spectrum = Sed(wavelen=wavelen, flambda=flux) indices = [1,2,5] fluxList = testBpDict.fluxListForSed(spectrum, indices=indices) ctNaN = 0 for ix, (name, bp, fluxTest) in enumerate(zip(nameList, bpList, fluxList)): if ix in indices: fluxControl = spectrum.calcFlux(bp) self.assertAlmostEqual(fluxTest/fluxControl, 1.0, 2) else: ctNaN += 1 self.assertTrue(numpy.isnan(fluxTest)) self.assertEqual(ctNaN, 4) nSed = 20 sedNameList = self.getListOfSedNames(nSed) magNormList = numpy.random.random_sample(nSed)*5.0 + 15.0 internalAvList = numpy.random.random_sample(nSed)*0.3 + 0.1 redshiftList = numpy.random.random_sample(nSed)*5.0 galacticAvList = numpy.random.random_sample(nSed)*0.3 + 0.1 # now try a SedList without a wavelenMatch testSedList = SedList(sedNameList, magNormList, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList) fluxList = testBpDict.fluxListForSedList(testSedList, indices=indices) self.assertEqual(fluxList.shape[0], nSed) self.assertEqual(fluxList.shape[1], nBandpasses) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) ctNaN = 0 for iy, bp in enumerate(testBpDict): if iy in indices: flux = dummySed.calcFlux(testBpDict[bp]) self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) else: ctNaN += 1 self.assertTrue(numpy.isnan(fluxList[ix][iy])) self.assertEqual(ctNaN, 4) # now use wavelenMatch testSedList = SedList(sedNameList, magNormList, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList, wavelenMatch=testBpDict.wavelenMatch) fluxList = testBpDict.fluxListForSedList(testSedList, indices=indices) self.assertEqual(fluxList.shape[0], nSed) self.assertEqual(fluxList.shape[1], nBandpasses) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) ctNaN = 0 for iy, bp in enumerate(testBpDict): if iy in indices: flux = dummySed.calcFlux(testBpDict[bp]) self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) else: ctNaN += 1 self.assertTrue(numpy.isnan(fluxList[ix][iy])) self.assertEqual(ctNaN, 4)
vega_SED.readSED_flambda('vega.txt') bwave = bandpass_besselb.wavelen bdwave = np.gradient(bwave) btrans = bandpass_besselb.sb vega_wave = vega_SED.wavelen vega_flambda = vega_SED.flambda mask = ((vega_wave > bwave[0]) & (vega_wave < bwave[-1])) d = vega_wave[mask] f = vega_flambda[mask] f = f / const.h.cgs.value / u.AA.to(u.Hz, d, u.spectral()) trans = np.interp(d, bwave, btrans) binw = np.gradient(d) ftot = np.sum(f * trans * binw) flux_vega = vega_SED.calcFlux(bandpass=bandpass_besselb) flux_sn_B = SED_rest.calcFlux(bandpass=bandpass_besselb) flux_snb_B = SED_restb.calcFlux(bandpass=bandpass_besselb) mag_SN = -2.5 * np.log10(flux_sn_B / flux_vega) mag_SNb = -2.5 * np.log10(flux_snb_B / flux_vega) #plt.plot(sed_SN.wavelen,sed_SN.flambda,linestyle='-',color='k') f = SN.SN._flux(T0, bwave) print 'hello man', f, bwave, SED_rest.wavelen, SED_rest.flambda fsum = np.sum(f * btrans * bwave * bdwave, axis=1) / HC_ERG_AA mask = ((SED_rest.wavelen > bwave[0]) & (SED_rest.wavelen < bwave[-1]))
def test_flare_magnitudes_mixed_with_dummy(self): """ Test that we get the expected magnitudes out """ db = MLT_test_DB(database=self.db_name, driver='sqlite') # load the quiescent SEDs of the objects in our catalog sed_list = SedList(['lte028-5.0+0.5a+0.0.BT-Settl.spec.gz']*4, [17.1, 17.2, 17.3, 17.4], galacticAvList = [2.432, 1.876, 2.654, 2.364], fileDir=getPackageDir('sims_sed_library'), specMap=defaultSpecMap) bp_dict = BandpassDict.loadTotalBandpassesFromFiles() # calculate the quiescent fluxes of the objects in our catalog baseline_fluxes = bp_dict.fluxListForSedList(sed_list) bb_wavelen = np.arange(100.0, 1600.0, 0.1) bb_flambda = blackbody_lambda(bb_wavelen*10.0, 9000.0) # this data is taken from the setUpClass() classmethod above t0_list = [456.2, 41006.2, 117.2, 10456.2] av_list = [2.432, 1.876, 2.654, 2.364] parallax_list = np.array([0.25, 0.15, 0.3, 0.22]) distance_list = 1.0/(206265.0*radiansFromArcsec(0.001*parallax_list)) distance_list *= 3.0857e18 # convert to cm dtype = np.dtype([('id', int), ('u', float), ('g', float)]) photParams = PhotometricParameters() ss = Sed() quiet_cat_name = os.path.join(self.scratch_dir, 'mlt_mixed_with_dummy_quiet_cat.txt') flare_cat_name = os.path.join(self.scratch_dir, 'mlt_mixed_with_dummy_flaring_cat.txt') # loop over several MJDs and verify that, to within a # milli-mag, our flaring model gives us the magnitudes # expected, given the light curves specified in # setUpClass() for mjd in (59580.0, 60000.0, 70000.0, 80000.0): obs = ObservationMetaData(mjd=mjd) quiet_cat = QuiescentCatalog(db, obs_metadata=obs) quiet_cat.write_catalog(quiet_cat_name) flare_cat = FlaringCatalogDummy(db, obs_metadata=obs) flare_cat.scratch_dir = self.scratch_dir flare_cat._mlt_lc_file = self.mlt_lc_name flare_cat.write_catalog(flare_cat_name) quiescent_data = np.genfromtxt(quiet_cat_name, dtype=dtype, delimiter=',') flaring_data = np.genfromtxt(flare_cat_name, dtype=dtype, delimiter=',') self.assertGreater(len(quiescent_data), 2) self.assertEqual(len(quiescent_data), len(flaring_data)) self.assertIn(3, flaring_data['id']) for ix in range(len(flaring_data)): obj_id = flaring_data['id'][ix] self.assertEqual(obj_id, ix) msg = ('failed on object %d; mjd %.2f\n u_quiet %e u_flare %e\n g_quiet %e g_flare %e' % (obj_id, mjd, quiescent_data['u'][obj_id], flaring_data['u'][obj_id], quiescent_data['g'][obj_id], flaring_data['g'][obj_id])) self.assertEqual(quiescent_data['id'][obj_id], flaring_data['id'][obj_id], msg=msg) self.assertAlmostEqual(ss.magFromFlux(baseline_fluxes[obj_id][0]), quiescent_data['u'][obj_id], 3, msg=msg) self.assertAlmostEqual(ss.magFromFlux(baseline_fluxes[obj_id][1]), quiescent_data['g'][obj_id], 3, msg=msg) if obj_id != 3: # the models below are as specified in the # setUpClass() method if obj_id == 0 or obj_id == 1: amp = 1.0e42 dt = 3652.5 t_min = flare_cat._survey_start - t0_list[obj_id] tt = mjd - t_min while tt > dt: tt -= dt u_flux = amp*(1.0+np.power(np.sin(tt/100.0), 2)) g_flux = amp*(1.0+np.power(np.cos(tt/100.0), 2)) elif obj_id==2: amp = 2.0e41 dt = 365.25 t_min = flare_cat._survey_start - t0_list[obj_id] tt = mjd - t_min while tt > dt: tt -= dt u_flux = amp*(1.0+np.power(np.sin(tt/50.0), 2)) g_flux = amp*(1.0+np.power(np.cos(tt/50.0), 2)) # calculate the multiplicative effect of dust on a 9000K # black body bb_sed = Sed(wavelen=bb_wavelen, flambda=bb_flambda) u_bb_flux = bb_sed.calcFlux(bp_dict['u']) g_bb_flux = bb_sed.calcFlux(bp_dict['g']) a_x, b_x = bb_sed.setupCCM_ab() bb_sed.addDust(a_x, b_x, A_v=av_list[obj_id]) u_bb_dusty_flux = bb_sed.calcFlux(bp_dict['u']) g_bb_dusty_flux = bb_sed.calcFlux(bp_dict['g']) dust_u = u_bb_dusty_flux/u_bb_flux dust_g = g_bb_dusty_flux/g_bb_flux area = 4.0*np.pi*np.power(distance_list[obj_id], 2) tot_u_flux = baseline_fluxes[obj_id][0] + u_flux*dust_u/area tot_g_flux = baseline_fluxes[obj_id][1] + g_flux*dust_g/area self.assertAlmostEqual(ss.magFromFlux(tot_u_flux), flaring_data['u'][obj_id], 3, msg=msg) self.assertAlmostEqual(ss.magFromFlux(tot_g_flux), flaring_data['g'][obj_id], 3, msg=msg) self.assertGreater(np.abs(flaring_data['g'][obj_id]-quiescent_data['g'][obj_id]), 0.001, msg=msg) self.assertGreater(np.abs(flaring_data['u'][obj_id]-quiescent_data['u'][obj_id]), 0.001, msg=msg) else: self.assertAlmostEqual(flaring_data['g'][obj_id], quiescent_data['g'][obj_id]+3*(mjd-59580.0)/10000.0, 3, msg=msg) self.assertAlmostEqual(flaring_data['u'][obj_id], quiescent_data['u'][obj_id]+2*(mjd-59580.0)/10000.0, 3, msg=msg) if os.path.exists(quiet_cat_name): os.unlink(quiet_cat_name) if os.path.exists(flare_cat_name): os.unlink(flare_cat_name)
sed_dir = os.environ['SIMS_SED_LIBRARY_DIR'] tot_bp_dict = BandpassDict.loadTotalBandpassesFromFiles() for ii in dexes_to_validate: disk_name = os.path.join(sed_dir, sed_names[disk_sed_idx[ii]].decode()) bulge_name = os.path.join(sed_dir, sed_names[bulge_sed_idx[ii]].decode()) for i_bp, bp in enumerate('ugrizy'): disk_s = Sed() disk_s.readSED_flambda(disk_name) fnorm = getImsimFluxNorm(disk_s, disk_magnorm[i_bp][ii]) disk_s.multiplyFluxNorm(fnorm) ax, bx = disk_s.setupCCM_ab() disk_s.addDust(ax, bx, A_v=disk_av[ii], R_v=disk_rv[ii]) disk_s.redshiftSED(control_qties['redshift'][ii], dimming=True) disk_f = disk_s.calcFlux(tot_bp_dict[bp]) bulge_s = Sed() bulge_s.readSED_flambda(bulge_name) fnorm = getImsimFluxNorm(bulge_s, bulge_magnorm[i_bp][ii]) bulge_s.multiplyFluxNorm(fnorm) ax, bx = bulge_s.setupCCM_ab() bulge_s.addDust(ax, bx, A_v=bulge_av[ii], R_v=bulge_rv[ii]) bulge_s.redshiftSED(control_qties['redshift'][ii], dimming=True) bulge_f = bulge_s.calcFlux(tot_bp_dict[bp]) tot_f = disk_f + bulge_f f_true = dummy_spec.fluxFromMag( control_qties['mag_true_%s_lsst' % bp][ii]) offset = np.abs(1.0 - (tot_f / f_true))
def __call__(self,mjds,filtre,expTime,out_q): fluxes=10.*self.SN.flux(mjds,self.wave) wavelength=self.wave/10. wavelength=np.repeat(wavelength[np.newaxis,:], len(fluxes), 0) SED_time = Sed(wavelen=wavelength, flambda=fluxes) r=[] visittime=expTime if self.airmass > 0.: trans=self.transmission.atmosphere[filtre] else: trans=self.transmission.system[filtre] photParams = PhotometricParameters(nexp=visittime/15.) itemindex = np.where(mjds==0.) #print 'hello',itemindex,mjds[itemindex] for i in range(len(SED_time.wavelen)): sed=Sed(wavelen=SED_time.wavelen[i],flambda=SED_time.flambda[i]) """ if filtre == 'r' and i==itemindex[0]: filtercolors = {'u':'b', 'g':'c', 'r':'g', 'i':'y', 'z':'r', 'y':'m'} plt.plot(SED_time.wavelen[i],SED_time.flambda[i],ls='-',label='z ='+str(self.param['z'])) plt.ylabel('Flux density [$ergs/cm^2/s/nm$]') plt.xlabel('$\lambda$ [nm]') plt.legend(loc='upper right') plt.title('x0= '+str(self.X0)+' x1= '+str(self.param['X1'])+' c= '+str(self.param['Color'])) #self.SED[self.param['z']]=(SED_time.wavelen[i],SED_time.flambda[i]) if self.param['z']==0.5: ax = plt.gca().twinx() for i,band in enumerate(['u','g','r','i','z','y']): ax.plot(self.transmission.system[filtre].wavelen,self.transmission.system[filtre].sb,linestyle='--',color=filtercolors[filtre]) """ flux_SN=sed.calcFlux(bandpass=self.transmission.system[filtre]) if flux_SN >0: mag_SN=-2.5 * np.log10(flux_SN / 3631.0) snr_m5_opsim,gamma_opsim=SignalToNoise.calcSNR_m5(mag_SN,trans,self.m5[filtre],photParams) err_flux_SN=flux_SN/snr_m5_opsim e_per_sec = sed.calcADU(bandpass=trans, photParams=photParams) #number of ADU counts for expTime #e_per_sec = sed.calcADU(bandpass=self.transmission.lsst_atmos[filtre], photParams=photParams) e_per_sec/=visittime/photParams.gain #print 'ref',filtre,i,mjds[i],e_per_sec #self.lc[filtre].append(e_per_sec) r.append((e_per_sec,mjds[i],flux_SN)) self.table_for_fit.add_row((mjds[i],flux_SN,err_flux_SN,'LSST::'+filtre,25,'ab')) #print 'hello',r self.lc[filtre]=np.rec.fromrecords(r, names = ['flux','mjd','flux_SN']) #print 'yyyy',len(self.lc[filtre]),self.lc[filtre]['flux_SN'] res=np.rec.fromrecords(r, names = ['flux','mjd','flux_SN']) out_q.put({filtre :(res,self.table_for_fit)}) return res
def test_flare_magnitudes_mixed_with_none(self): """ Test that we get the expected magnitudes out """ db = MLT_test_DB(database=self.db_name, driver='sqlite') # load the quiescent SEDs of the objects in our catalog sed_list = SedList(['lte028-5.0+0.5a+0.0.BT-Settl.spec.gz']*4, [17.1, 17.2, 17.3, 17.4], galacticAvList = [2.432, 1.876, 2.654, 2.364], fileDir=getPackageDir('sims_sed_library'), specMap=defaultSpecMap) bp_dict = BandpassDict.loadTotalBandpassesFromFiles() # calculate the quiescent fluxes of the objects in our catalog baseline_fluxes = bp_dict.fluxListForSedList(sed_list) bb_wavelen = np.arange(100.0, 1600.0, 0.1) bb = models.BlackBody(temperature=9000.0 * u.K, scale=1.0 * u.erg / (u.cm ** 2 * u.AA * u.s * u.sr)) bb_flambda = bb(bb_wavelen * u.nm).to_value() # this data is taken from the setUpClass() classmethod above t0_list = [456.2, 41006.2, 117.2, 10456.2] av_list = [2.432, 1.876, 2.654, 2.364] parallax_list = np.array([0.25, 0.15, 0.3, 0.22]) distance_list = 1.0/(206265.0*radiansFromArcsec(0.001*parallax_list)) distance_list *= 3.0857e18 # convert to cm dtype = np.dtype([('id', int), ('u', float), ('g', float)]) photParams = PhotometricParameters() ss = Sed() quiet_cat_name = os.path.join(self.scratch_dir, 'mlt_mixed_with_none_quiet_cat.txt') flare_cat_name = os.path.join(self.scratch_dir, 'mlt_mixed_with_none_flaring_cat.txt') # loop over several MJDs and verify that, to within a # milli-mag, our flaring model gives us the magnitudes # expected, given the light curves specified in # setUpClass() for mjd in (59580.0, 60000.0, 70000.0, 80000.0): obs = ObservationMetaData(mjd=mjd) quiet_cat = QuiescentCatalog(db, obs_metadata=obs) quiet_cat.write_catalog(quiet_cat_name) flare_cat = FlaringCatalog(db, obs_metadata=obs) flare_cat._mlt_lc_file = self.mlt_lc_name flare_cat.write_catalog(flare_cat_name) quiescent_data = np.genfromtxt(quiet_cat_name, dtype=dtype, delimiter=',') flaring_data = np.genfromtxt(flare_cat_name, dtype=dtype, delimiter=',') self.assertGreater(len(flaring_data), 3) for ix in range(len(flaring_data)): obj_id = flaring_data['id'][ix] self.assertEqual(obj_id, ix) # the models below are as specified in the # setUpClass() method if obj_id == 0 or obj_id == 1: amp = 1.0e42 dt = 3652.5 t_min = flare_cat._survey_start - t0_list[obj_id] tt = mjd - t_min while tt > dt: tt -= dt u_flux = amp*(1.0+np.power(np.sin(tt/100.0), 2)) g_flux = amp*(1.0+np.power(np.cos(tt/100.0), 2)) elif obj_id==2: amp = 2.0e41 dt = 365.25 t_min = flare_cat._survey_start - t0_list[obj_id] tt = mjd - t_min while tt > dt: tt -= dt u_flux = amp*(1.0+np.power(np.sin(tt/50.0), 2)) g_flux = amp*(1.0+np.power(np.cos(tt/50.0), 2)) else: u_flux = 0.0 g_flux = 0.0 # calculate the multiplicative effect of dust on a 9000K # black body bb_sed = Sed(wavelen=bb_wavelen, flambda=bb_flambda) u_bb_flux = bb_sed.calcFlux(bp_dict['u']) g_bb_flux = bb_sed.calcFlux(bp_dict['g']) a_x, b_x = bb_sed.setupCCM_ab() bb_sed.addDust(a_x, b_x, A_v=av_list[obj_id]) u_bb_dusty_flux = bb_sed.calcFlux(bp_dict['u']) g_bb_dusty_flux = bb_sed.calcFlux(bp_dict['g']) dust_u = u_bb_dusty_flux/u_bb_flux dust_g = g_bb_dusty_flux/g_bb_flux area = 4.0*np.pi*np.power(distance_list[obj_id], 2) tot_u_flux = baseline_fluxes[obj_id][0] + u_flux*dust_u/area tot_g_flux = baseline_fluxes[obj_id][1] + g_flux*dust_g/area msg = ('failed on object %d; mjd %.2f\n u_quiet %e u_flare %e\n g_quiet %e g_flare %e' % (obj_id, mjd, quiescent_data['u'][obj_id], flaring_data['u'][obj_id], quiescent_data['g'][obj_id], flaring_data['g'][obj_id])) self.assertEqual(quiescent_data['id'][obj_id], flaring_data['id'][obj_id], msg=msg) self.assertAlmostEqual(ss.magFromFlux(baseline_fluxes[obj_id][0]), quiescent_data['u'][obj_id], 3, msg=msg) self.assertAlmostEqual(ss.magFromFlux(baseline_fluxes[obj_id][1]), quiescent_data['g'][obj_id], 3, msg=msg) self.assertAlmostEqual(ss.magFromFlux(tot_u_flux), flaring_data['u'][obj_id], 3, msg=msg) self.assertAlmostEqual(ss.magFromFlux(tot_g_flux), flaring_data['g'][obj_id], 3, msg=msg) if obj_id != 3: self.assertGreater(np.abs(flaring_data['g'][obj_id]-quiescent_data['g'][obj_id]), 0.001, msg=msg) self.assertGreater(np.abs(flaring_data['u'][obj_id]-quiescent_data['u'][obj_id]), 0.001, msg=msg) else: self.assertEqual(flaring_data['g'][obj_id]-quiescent_data['g'][obj_id], 0.0, msg=msg) self.assertEqual(flaring_data['u'][obj_id]-quiescent_data['u'][obj_id], 0.0, msg=msg) if os.path.exists(quiet_cat_name): os.unlink(quiet_cat_name) if os.path.exists(flare_cat_name): os.unlink(flare_cat_name)
def __init__(self,logobs,parameters,model,version): self.mjd_name='mjd' self.FWHMeff='seeing' peakAbsMagBesselB=-19.0906 alpha=0.13 beta=3. mycosmology=FlatLambdaCDM(H0=70, Om0=0.25) self.astropy_cosmo=FlatLambdaCDM(H0= mycosmology.H0, Om0=mycosmology.Om0) instrument = instruments.InstrumentModel("STANDARD") B = instrument.EffectiveFilterByBand("B") magsys = instruments.MagSys('VEGA') zp = magsys.ZeroPoint(B) print 'zeropoint for b-band',zp flux_at_10pc = np.power(10., -0.4 * (peakAbsMagBesselB-zp)) fs = salt2.load_filters(['STANDARD::B']) mc = salt2.ModelComponents('salt2.npz') s = salt2.SALT2([0.], ['STANDARD::B'], mc, fs, z=0.) raw_model_norm = s()[0] # (10pc)^2 in kpc^2 self.X0_norm = flux_at_10pc * 1.E-4 / raw_model_norm table_obs=ascii.read(logobs,fast_reader=False) colnames=[] sfile=open(logobs,'r') for line in sfile.readlines(): if line.count('#'): colnames.append(line[1:].split(':')[0].strip()) for i,val in enumerate(table_obs.colnames): #if val.count('#') >= 1: table_obs.rename_column(val, colnames[i]) self.lc=[] print table_obs self.params=Table(names=('t0','c','x1','z','ra','dec','status','fit','sn_type','sn_model','sn_version','mbsim','x0','dL'),dtype=('f8','f8','f8','f8','f8','S8','S8','f8','S8','S8','S8','f8','f8','f8')) self.transmission=Throughputs() dust = sncosmo.OD94Dust() model=model version=version if model == 'salt2-extended': model_min=300. model_max=180000. wave_min=3000. wave_max=11501. if model=='salt2': model_min=2000. model_max=9200.0 wave_min=model_min wave_max=model_max wave= np.arange(wave_min,wave_max,1.) sn_type='Ia' source=sncosmo.get_source(model,version=version) ra_field=0. dec_field=0. table_obs.sort(self.mjd_name) for iv,param in enumerate(parameters): #mysn=Simul_Fit_SN(param['DayMax'],param['Color'],param['X1'],param['z'],table_obs,ra=param['ra'],dec=param['dec']) table_LC=Table(names=('idSN','filter','expMJD','visitExpTime','FWHMeff','moon_frac','filtSkyBrightness','kAtm','airmass','fiveSigmaDepth','Nexp','e_per_sec','e_per_sec_err'), dtype=('i8','S7','f8', 'f8','f8','f8', 'f8','f8','i8','f8','f8','f8','f8')) lumidist=self.astropy_cosmo.luminosity_distance(param['z']).value*1.e3 X0 = self.X0_norm / lumidist** 2 X0 *= np.power(10., 0.4*(alpha*param['X1'] -beta*param['Color'])) print 'X0 val',X0 SN=sncosmo.Model(source=source,effects=[dust, dust], effect_names=['host', 'mw'], effect_frames=['rest', 'obs']) SN.set(z=param['z']) SN.set(t0=param['DayMax']) SN.set(c=param['Color']) SN.set(x1=param['X1']) SN.set(x0=X0) #SN.set_source_peakabsmag(peakAbsMagBesselB, 'bessellB', 'vega',cosmo=mycosmology) fluxes=10.*SN.flux(table_obs[self.mjd_name],wave) wavelength=wave/10. wavelength=np.repeat(wavelength[np.newaxis,:], len(fluxes), 0) SED_time = Sed(wavelen=wavelength, flambda=fluxes) self.params.add_row((param['DayMax'],param['Color'],param['X1'],param['z'],ra_field,dec_field,'unkown',None,sn_type,model,version,SN._source.peakmag('bessellb','vega'),SN.get('x0'),lumidist)) for i in range(len(SED_time.wavelen)): obs=table_obs[i] sed=Sed(wavelen=SED_time.wavelen[i],flambda=SED_time.flambda[i]) visittime=obs['exptime'] filtre=obs['band'][-1] photParams = PhotometricParameters(nexp=visittime/15.) e_per_sec = sed.calcADU(bandpass=self.transmission.lsst_atmos_aerosol[filtre], photParams=photParams) #number of ADU counts for expTime e_per_sec/=visittime/photParams.gain flux_SN=sed.calcFlux(bandpass=self.transmission.lsst_atmos_aerosol[filtre]) FWHMeff=obs[self.FWHMeff] if flux_SN >0: #print 'hello flux',flux_SN,fluxes[i] mag_SN=-2.5 * np.log10(flux_SN / 3631.0) m5_calc,snr_m5_through=self.Get_m5(filtre,mag_SN,obs['sky'],photParams,FWHMeff) table_LC.add_row((iv,'LSST::'+filtre,obs[self.mjd_name],visittime,FWHMeff,obs['moon_frac'],obs['sky'],obs['kAtm'],obs['airmass'],obs['m5sigmadepth'],obs['Nexp'],e_per_sec,e_per_sec/snr_m5_through)) self.lc.append(table_LC)
def lsst_flare_fluxes_from_u(ju_flux): """ Convert from Johnson U band flux to flux in the LSST bands by assuming the flare is a 9000K black body (see Section 4 of Hawley et al 2003, ApJ 597, 535) Parameters ---------- flux in Johnson U band (either a float or a numpy array) Returns ------- floats/numpy arrays of fluxes in all 6 LSST bands """ if not hasattr(lsst_flare_fluxes_from_u, 'johnson_u_raw_flux'): t_start = time.time() throughputs_dir = getPackageDir('throughputs') johnson_dir = os.path.join(throughputs_dir, 'johnson') johnson_u_hw = Bandpass() johnson_u_hw.readThroughput(os.path.join(johnson_dir, 'johnson_U.dat')) atm = Bandpass() atm.readThroughput( os.path.join(throughputs_dir, 'baseline', 'atmos_std.dat')) wv, sb = johnson_u_hw.multiplyThroughputs(atm.wavelen, atm.sb) johnson_u = Bandpass(wavelen=wv, sb=sb) boltzmann_k = 1.3807e-16 # erg/K planck_h = 6.6261e-27 # erg*s _c = 2.9979e10 # cm/s hc_over_k = 1.4387e7 # nm*K temp = 9000.0 # black body temperature in Kelvin bb_wavelen = np.arange(200.0, 1500.0, 0.1) # in nanometers exp_arg = hc_over_k / (temp * bb_wavelen) exp_term = 1.0 / (np.exp(exp_arg) - 1.0) ln_exp_term = np.log(exp_term) # the -7.0 np.log(10) will convert wavelen into centimeters log_bb_flambda = -5.0 * (np.log(bb_wavelen) - 7.0 * np.log(10.0)) + ln_exp_term log_bb_flambda += np.log(2.0) + np.log(planck_h) + 2.0 * np.log(_c) # assume these stars all have radii half that of the Sun; # see Boyajian et al. 2012 (ApJ 757, 112) r_sun = 6.957e10 # cm log_bb_flambda += np.log( 4.0 * np.pi * np.pi) + 2.0 * np.log(0.5 * r_sun) # thee -7.0*np.log(10.0) makes sure we get ergs/s/cm^2/nm bb_flambda = np.exp(log_bb_flambda - 7.0 * np.log(10)) bb_sed = Sed(wavelen=bb_wavelen, flambda=bb_flambda) # because we have a flux in ergs/s but need a flux in # the normalized units of Sed.calcFlux (see eqn 2.1 # of the LSST Science Book), we will calculate the # ergs/s/cm^2 of a raw, unnormalized blackbody spectrum # in the Johnson U band, find the factor that converts # that raw flux into our specified flux, and then # multiply that factor *by the fluxes calculated for the # blackbody in the LSST filters using Sed.calcFlux()*. # This should give us the correct normalized flux for # the flares in the LSST filters. lsst_flare_fluxes_from_u.johnson_u_raw_flux = bb_sed.calcErgs( johnson_u) lsst_bands = BandpassDict.loadTotalBandpassesFromFiles() norm_raw = None lsst_flare_fluxes_from_u.lsst_raw_flux_dict = {} for band_name in ('u', 'g', 'r', 'i', 'z', 'y'): bp = lsst_bands[band_name] flux = bb_sed.calcFlux(bp) lsst_flare_fluxes_from_u.lsst_raw_flux_dict[band_name] = flux if norm_raw is None: norm_raw = flux print('raw flux in %s = %e; %e; %e' % (band_name, flux, flux / norm_raw, bb_sed.calcErgs(bp))) print('sed johnson flux %e' % lsst_flare_fluxes_from_u.johnson_u_raw_flux) print('that init took %e' % (time.time() - t_start)) factor = ju_flux / lsst_flare_fluxes_from_u.johnson_u_raw_flux u_flux_out = factor * lsst_flare_fluxes_from_u.lsst_raw_flux_dict['u'] g_flux_out = factor * lsst_flare_fluxes_from_u.lsst_raw_flux_dict['g'] r_flux_out = factor * lsst_flare_fluxes_from_u.lsst_raw_flux_dict['r'] i_flux_out = factor * lsst_flare_fluxes_from_u.lsst_raw_flux_dict['i'] z_flux_out = factor * lsst_flare_fluxes_from_u.lsst_raw_flux_dict['z'] y_flux_out = factor * lsst_flare_fluxes_from_u.lsst_raw_flux_dict['y'] return (u_flux_out, g_flux_out, r_flux_out, i_flux_out, z_flux_out, y_flux_out)