def calcADUwrapper(sedName=None, magNorm=None, redshift=None, internalAv=None, internalRv=None, galacticAv=None, galacticRv=None, bandpass=None): """ Read in an SED and calculat the number of ADU produced by that SED in a specified bandpass Parameters ---------- sedName is a string specifying the file name of the SED magNorm is the normalizing magnitude of the SED in the imsimBandpass redshift is the redshift of the SED internalAv is the Av due to internal dust of the source (if a galaxy) internalRv is the Rv due to internal dust of the source (if a galaxy) galacticAv is the Av due to Milky Way dust between observer and source galacticRv is the Rv due to Milky Way dust between observer and source bandpass is an intantiation of Bandpass representing the band in which the ADUs are measured Returns ------- A float representing the number of ADUs measured in the bandpass """ imsimband = Bandpass() imsimband.imsimBandpass() sed = Sed() sed.readSED_flambda(sedName) fNorm = sed.calcFluxNorm(magNorm, imsimband) sed.multiplyFluxNorm(fNorm) if internalAv is not None and internalRv is not None: if internalAv != 0.0 and internalRv != 0.0: a_int, b_int = sed.setupCCM_ab() sed.addDust(a_int, b_int, A_v=internalAv, R_v=internalRv) if redshift is not None and redshift != 0.0: sed.redshiftSED(redshift, dimming=True) a_int, b_int = sed.setupCCM_ab() sed.addDust(a_int, b_int, A_v=galacticAv, R_v=galacticRv) adu = sed.calcADU(bandpass, photParams=PhotometricParameters()) return adu
def _calcSingleGalSimSed(self, sedName, zz, iAv, iRv, gAv, gRv, norm): """ correct the SED for redshift, dust, etc. Return an Sed object as defined in sims_photUtils/../../Sed.py """ if _is_null(sedName): return None sed = Sed() sed.readSED_flambda(os.path.join(self.sedDir, sedName)) imsimband = Bandpass() imsimband.imsimBandpass() # normalize the SED # Consulting the file sed.py in GalSim/galsim/ it appears that GalSim expects # its SEDs to ultimately be in units of ergs/nm so that, when called, they can # be converted to photons/nm (see the function __call__() and the assignment of # self._rest_photons in the __init__() of galsim's sed.py file). Thus, we need # to read in our SEDs, normalize them, and then multiply by the exposure time # and the effective area to get from ergs/s/cm^2/nm to ergs/nm. # # The gain parameter should convert between photons and ADU (so: it is the # traditional definition of "gain" -- electrons per ADU -- multiplied by the # quantum efficiency of the detector). Because we fold the quantum efficiency # of the detector into our total_[u,g,r,i,z,y].dat bandpass files # (see the readme in the THROUGHPUTS_DIR/baseline/), we only need to multiply # by the electrons per ADU gain. # # We will take these parameters from an instantiation of the PhotometricParameters # class (which can be reassigned by defining a daughter class of this class) # fNorm = sed.calcFluxNorm(norm, imsimband) sed.multiplyFluxNorm(fNorm) # apply dust extinction (internal) if iAv != 0.0 and iRv != 0.0: a_int, b_int = sed.setupCCM_ab() sed.addDust(a_int, b_int, A_v=iAv, R_v=iRv) # 22 June 2015 # apply redshift; there is no need to apply the distance modulus from # sims/photUtils/CosmologyWrapper; magNorm takes that into account # however, magNorm does not take into account cosmological dimming if zz != 0.0: sed.redshiftSED(zz, dimming=True) # apply dust extinction (galactic) if gAv != 0.0 and gRv != 0.0: a_int, b_int = sed.setupCCM_ab() sed.addDust(a_int, b_int, A_v=gAv, R_v=gRv) return sed
def __init__(self, m5Col='fiveSigmaDepth', metricName='ExgalM5', units='mag', lsstFilter='r', wavelen_min=None , wavelen_max=None , **kwargs): # Set the name for the dust map to use. This is gathered into the MetricBundle. maps = ['DustMap'] # Set the default wavelength limits for the lsst filters. These are approximately correct. waveMins = {'u':330.,'g':403.,'r':552.,'i':691.,'z':818.,'y':950.} waveMaxes = {'u':403.,'g':552.,'r':691.,'i':818.,'z':922.,'y':1070.} if lsstFilter is not None: wavelen_min = waveMins[lsstFilter] wavelen_max = waveMaxes[lsstFilter] self.m5Col = m5Col super().__init__(col=[self.m5Col], maps=maps, metricName=metricName, units=units, **kwargs) # Set up internal values for the dust extinction. testsed = Sed() testsed.setFlatSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=1.0) testbandpass = Bandpass(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=1.0) testbandpass.setBandpass(wavelen=testsed.wavelen, sb=np.ones(len(testsed.wavelen))) self.R_v = 3.1 self.ref_ebv = 1.0 # Calculate non-dust-extincted magnitude flatmag = testsed.calcMag(testbandpass) # Add dust self.a, self.b = testsed.setupCCM_ab() testsed.addDust(self.a, self.b, ebv=self.ref_ebv, R_v=self.R_v) # Calculate difference due to dust when EBV=1.0 (m_dust = m_nodust - Ax, Ax > 0) self.Ax1 = testsed.calcMag(testbandpass) - flatmag # We will call Coaddm5Metric to calculate the coadded depth. Set it up here. self.Coaddm5Metric = Coaddm5Metric(m5Col=m5Col)
def __init__(self, metricName='TDEsPopMetric', mjdCol='observationStartMJD', m5Col='fiveSigmaDepth', filterCol='filter', nightCol='night', ptsNeeded=2, file_list=None, mjd0=59853.5, **kwargs): maps = ['DustMap'] self.mjdCol = mjdCol self.m5Col = m5Col self.filterCol = filterCol self.nightCol = nightCol self.ptsNeeded = ptsNeeded self.lightcurves = Tde_lc(file_list=file_list) self.mjd0 = mjd0 waveMins = {'u': 330., 'g': 403., 'r': 552., 'i': 691., 'z': 818., 'y': 950.} waveMaxes = {'u': 403., 'g': 552., 'r': 691., 'i': 818., 'z': 922., 'y': 1070.} self.a = {} self.b = {} for filtername in waveMins.keys(): testsed = Sed() testsed.setFlatSED(wavelen_min=waveMins[filtername], wavelen_max=waveMaxes[filtername], wavelen_step=1) self.a[filtername], self.b[filtername] = testsed.setupCCM_ab() self.R_v = 3.1 cols = [self.mjdCol, self.m5Col, self.filterCol, self.nightCol] super(TdePopMetric, self).__init__(col=cols, units='Detected, 0 or 1', metricName=metricName, maps=maps, **kwargs)
def get_sed(name, magnorm, redshift, av, rv): if not hasattr(get_sed, '_rest_dict'): get_sed._rest_dict = {} get_sed._imsim_bp = Bandpass() get_sed._imsim_bp.imsimBandpass() get_sed._sed_dir = os.environ['SIMS_SED_LIBRARY_DIR'] get_sed._ccm_w = None tag = '%s_%.2f_%.2f' % (name, av, rv) if tag not in get_sed._rest_dict: ss = Sed() ss.readSED_flambda(os.path.join(get_sed._sed_dir, name)) if get_sed._ccm_w is None or not np.array_equal( ss.wavelen, get_sed._ccm_w): get_sed._ccm_w = np.copy(ss.wavelen) get_sed._ax, get_sed._bx = ss.setupCCM_ab() mn = ss.calcMag(get_sed._imsim_bp) ss.addDust(get_sed._ax, get_sed._bx, A_v=av, R_v=rv) get_sed._rest_dict[tag] = (ss, mn) base_sed = get_sed._rest_dict[tag][0] ss = Sed(wavelen=base_sed.wavelen, flambda=base_sed.flambda) dmag = magnorm - get_sed._rest_dict[tag][1] fnorm = np.power(10.0, -0.4 * dmag) ss.multiplyFluxNorm(fnorm) ss.redshiftSED(redshift, dimming=True) return ss
def __init__(self, metricName='plasticc_transient', mjdCol='observationStartMJD', m5Col='fiveSigmaDepth', filterCol='filter', color_gap=0.5, pre_slope_range=0.3, days_around_peak=200, r_mag_limit=28, nbins=10, nsamples=5, maps=['DustMap'], apply_dust=True, units='fraction', **kwargs): self.mjdCol = mjdCol self.m5Col = m5Col self.filterCol = filterCol self.color_gap = color_gap self.pre_slope_range = pre_slope_range self.days_around_peak = days_around_peak self.rmag_limit = r_mag_limit self.nbins = nbins self.nsamples = nsamples self.apply_dust = apply_dust super(Plasticc_metric, self).__init__(col=[self.mjdCol, self.m5Col, self.filterCol], metricName=metricName, maps=maps, units=units, **kwargs) # Let's set up the dust stuff waveMins = {'u': 330., 'g': 403., 'r': 552., 'i': 691., 'z': 818., 'y': 950.} waveMaxes = {'u': 403., 'g': 552., 'r': 691., 'i': 818., 'z': 922., 'y': 1070.} self.a_extinc = {} self.b_extinc = {} for filtername in waveMins: testsed = Sed() testsed.setFlatSED(wavelen_min=waveMins[filtername], wavelen_max=waveMaxes[filtername], wavelen_step=1) self.a_extinc[filtername], self.b_extinc[filtername] = testsed.setupCCM_ab() self.R_v = 3.1
def _create_library_one_sed(_galaxy_sed_dir, sed_file_name_list, av_grid, rv_grid, bandpass_dict, out_dict, out_lock): n_obj = 0 for i_av, av in enumerate(av_grid): for i_rv, rv in enumerate(rv_grid): if av < 0.01 and i_rv > 0: continue n_obj += 1 imsim_bp = Bandpass() imsim_bp.imsimBandpass() t_start = time.time() sed_names_out = [] rv_out_list_out = [] av_out_list_out = [] sed_mag_norm_out = [] sed_mag_list_out = [] for i_sed, sed_file_name in enumerate(sed_file_name_list): base_spec = Sed() base_spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name)) ax, bx = base_spec.setupCCM_ab() mag_norm = base_spec.calcMag(imsim_bp) sed_names = np.array([defaultSpecMap[sed_file_name]] * n_obj) rv_out_list = np.zeros(n_obj, dtype=float) av_out_list = np.zeros(n_obj, dtype=float) sed_mag_norm = mag_norm * np.ones(n_obj, dtype=float) sed_mag_list = np.zeros((n_obj, len(bandpass_dict)), dtype=float) i_obj = 0 for i_av, av in enumerate(av_grid): for i_rv, rv in enumerate(rv_grid): if av < 0.01 and i_rv > 0: continue spec = Sed(wavelen=base_spec.wavelen, flambda=base_spec.flambda) spec.addDust(ax, bx, A_v=av, R_v=rv) av_out_list[i_obj] = av rv_out_list[i_obj] = rv sed_mag_list[i_obj][:] = bandpass_dict.magListForSed(spec) i_obj += 1 sed_names_out.append(sed_names) sed_mag_norm_out.append(sed_mag_norm) sed_mag_list_out.append(sed_mag_list) av_out_list_out.append(av_out_list) rv_out_list_out.append(rv_out_list) with out_lock: out_dict['sed_names'] += sed_names_out out_dict['sed_mag_norm'] += sed_mag_norm_out out_dict['sed_mag_list'] += sed_mag_list_out out_dict['av_out_list'] += av_out_list_out out_dict['rv_out_list'] += rv_out_list_out
def __init__(self, m5Col='fiveSigmaDepth', units='mag', lsstFilter='i', wavelen_min=None, wavelen_max=None, wavelen_step=1., extinction_cut=0.2, depth_cut=26, **kwargs): """ Args: m5Col (str): Column name that ('fiveSigmaDepth') units (str): units of the metric ('mag') lsstFilter (str): Which LSST filter to calculate m5 for wavelen_min (float): Minimum wavength of your filter (None) wavelen_max (float): (None) wavelen_step (float): (1.) **kwargs: """ maps = ['DustMap'] waveMins = { 'u': 330., 'g': 403., 'r': 552., 'i': 691., 'z': 818., 'y': 950. } waveMaxes = { 'u': 403., 'g': 552., 'r': 691., 'i': 818., 'z': 922., 'y': 1070. } if lsstFilter is not None: wavelen_min = waveMins[lsstFilter] wavelen_max = waveMaxes[lsstFilter] self.m5Col = m5Col super(ExgalM5_cut, self).__init__(col=[self.m5Col], maps=maps, units=units, **kwargs) testsed = Sed() testsed.setFlatSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=1) self.a, self.b = testsed.setupCCM_ab() self.R_v = 3.1 self.Coaddm5Metric = Coaddm5Metric(m5Col=m5Col) self.extinction_cut = extinction_cut self.depth_cut = depth_cut
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_stars(self): obs = ObservationMetaData(bandpassName=['c_u', 'c_g'], m5=[25.0, 26.0]) db_dtype = np.dtype([('id', np.int), ('raJ2000', np.float), ('decJ2000', np.float), ('sedFilename', str, 100), ('magNorm', np.float), ('galacticAv', np.float)]) inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'testData') inputFile = os.path.join(inputDir, 'IndicesTestCatalogStars.txt') db = fileDBObject(inputFile, dtype=db_dtype, runtable='test', idColKey='id') cat = CartoonStars(db, obs_metadata=obs) with lsst.utils.tests.getTempFilePath('.txt') as catName: cat.write_catalog(catName) dtype = np.dtype([(name, np.float) for name in cat.column_outputs]) controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',') db_columns = db.query_columns([ 'id', 'raJ2000', 'decJ2000', 'sedFilename', 'magNorm', 'galacticAv' ]) sedDir = os.path.join(getPackageDir('sims_sed_library'), 'starSED', 'kurucz') for ix, line in enumerate(next(db_columns)): spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, line[3])) fnorm = spectrum.calcFluxNorm(line[4], self.normband) spectrum.multiplyFluxNorm(fnorm) a_x, b_x = spectrum.setupCCM_ab() spectrum.addDust(a_x, b_x, A_v=line[5]) umag = spectrum.calcMag(self.uband) self.assertAlmostEqual(umag, controlData['cartoon_u'][ix], 3) gmag = spectrum.calcMag(self.gband) self.assertAlmostEqual(gmag, controlData['cartoon_g'][ix], 3) umagError, gamma = calcMagError_m5(umag, self.uband, obs.m5['c_u'], PhotometricParameters()) gmagError, gamma = calcMagError_m5(gmag, self.gband, obs.m5['c_g'], PhotometricParameters()) self.assertAlmostEqual(umagError, controlData['sigma_cartoon_u'][ix], 3) self.assertAlmostEqual(gmagError, controlData['sigma_cartoon_g'][ix], 3)
def test_stars(self): obs = ObservationMetaData(bandpassName=['c_u', 'c_g'], m5=[25.0, 26.0]) db_dtype = np.dtype([('id', np.int), ('raJ2000', np.float), ('decJ2000', np.float), ('sedFilename', str, 100), ('magNorm', np.float), ('galacticAv', np.float)]) inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'testData') inputFile = os.path.join(inputDir, 'IndicesTestCatalogStars.txt') db = fileDBObject(inputFile, dtype=db_dtype, runtable='test', idColKey='id') cat = CartoonStars(db, obs_metadata=obs) with lsst.utils.tests.getTempFilePath('.txt') as catName: cat.write_catalog(catName) dtype = np.dtype([(name, np.float) for name in cat.column_outputs]) controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',') db_columns = db.query_columns(['id', 'raJ2000', 'decJ2000', 'sedFilename', 'magNorm', 'galacticAv']) sedDir = os.path.join(getPackageDir('sims_sed_library'), 'starSED', 'kurucz') for ix, line in enumerate(next(db_columns)): spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, line[3])) fnorm = spectrum.calcFluxNorm(line[4], self.normband) spectrum.multiplyFluxNorm(fnorm) a_x, b_x = spectrum.setupCCM_ab() spectrum.addDust(a_x, b_x, A_v=line[5]) umag = spectrum.calcMag(self.uband) self.assertAlmostEqual(umag, controlData['cartoon_u'][ix], 3) gmag = spectrum.calcMag(self.gband) self.assertAlmostEqual(gmag, controlData['cartoon_g'][ix], 3) umagError, gamma = calcMagError_m5(umag, self.uband, obs.m5['c_u'], PhotometricParameters()) gmagError, gamma = calcMagError_m5(gmag, self.gband, obs.m5['c_g'], PhotometricParameters()) self.assertAlmostEqual(umagError, controlData['sigma_cartoon_u'][ix], 3) self.assertAlmostEqual(gmagError, controlData['sigma_cartoon_g'][ix], 3)
def testStellarPhotometricUncertainties(self): """ Test in the case of a catalog of stars """ lsstDefaults = LSSTdefaults() starDB = testStarsDBObj(driver=self.driver, host=self.host, database=self.dbName) starCat = testStarCatalog(starDB, obs_metadata=self.obs_metadata) ct = 0 for line in starCat.iter_catalog(): starSed = Sed() starSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), defaultSpecMap[line[14]])) imsimband = Bandpass() imsimband.imsimBandpass() fNorm = starSed.calcFluxNorm(line[15], imsimband) starSed.multiplyFluxNorm(fNorm) aV = np.float(line[16]) a_int, b_int = starSed.setupCCM_ab() starSed.addDust(a_int, b_int, A_v=aV) for i in range(len(self.bandpasses)): controlSigma = calcMagError_sed( starSed, self.totalBandpasses[i], self.skySeds[i], self.hardwareBandpasses[i], FWHMeff=lsstDefaults.FWHMeff(self.bandpasses[i]), photParams=PhotometricParameters()) testSigma = line[8 + i] self.assertAlmostEqual(controlSigma, testSigma, 4) ct += 1 self.assertGreater(ct, 0)
def add_flux(self, sed_name, redshift, magnorm_dict, av, rv, bp_dict=None): if bp_dict is None: bp_dict = BandpassDict.loadTotalBandpassesFromFiles() result_dict_no_mw = {} result_dict_mw = {} sed_obj = Sed() sed_obj.readSED_flambda(os.path.join(os.environ['SIMS_SED_LIBRARY_DIR'], sed_name)) a_x, b_x = sed_obj.setupCCM_ab() for bandpass_name in bp_dict.keys(): sed_copy = deepcopy(sed_obj) flux_norm = getImsimFluxNorm(sed_copy, magnorm_dict[bandpass_name]) sed_copy.multiplyFluxNorm(flux_norm) sed_copy.redshiftSED(redshift, dimming=True) band_flux = sed_copy.calcFlux(bp_dict[bandpass_name]) result_dict_no_mw[bandpass_name] = band_flux sed_copy.addDust(a_x, b_x, A_v=av, R_v=rv) band_flux_mw = sed_copy.calcFlux(bp_dict[bandpass_name]) result_dict_mw[bandpass_name] = band_flux_mw return result_dict_no_mw, result_dict_mw
def __init__(self, R_v=3.1, bandpassDict=None, ref_ebv=1.): # Calculate dust extinction values self.Ax1 = {} if bandpassDict is None: bandpassDict = BandpassDict.loadTotalBandpassesFromFiles( ['u', 'g', 'r', 'i', 'z', 'y']) for filtername in bandpassDict: wavelen_min = bandpassDict[filtername].wavelen.min() wavelen_max = bandpassDict[filtername].wavelen.max() testsed = Sed() testsed.setFlatSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=1.0) self.ref_ebv = ref_ebv # Calculate non-dust-extincted magnitude flatmag = testsed.calcMag(bandpassDict[filtername]) # Add dust a, b = testsed.setupCCM_ab() testsed.addDust(a, b, ebv=self.ref_ebv, R_v=R_v) # Calculate difference due to dust when EBV=1.0 (m_dust = m_nodust - Ax, Ax > 0) self.Ax1[filtername] = testsed.calcMag( bandpassDict[filtername]) - flatmag
def testAddingToList(self): """ Test that we can add Seds to an already instantiated SedList """ imsimBand = Bandpass() imsimBand.imsimBandpass() nSed = 10 sedNameList_0 = self.getListOfSedNames(nSed) magNormList_0 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = self.rng.random_sample(nSed)*5.0 galacticAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 wavelen_match = np.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, fileDir=self.sedDir, internalAvList=internalAvList_0, redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) # experiment with adding different combinations of physical parameter lists # as None and not None for addIav in [True, False]: for addRedshift in [True, False]: for addGav in [True, False]: testList = SedList(sedNameList_0, magNormList_0, fileDir=self.sedDir, internalAvList=internalAvList_0, redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) sedNameList_1 = self.getListOfSedNames(nSed) magNormList_1 = self.rng.random_sample(nSed)*5.0 + 15.0 if addIav: internalAvList_1 = self.rng.random_sample(nSed)*0.3 + 0.1 else: internalAvList_1 = None if addRedshift: redshiftList_1 = self.rng.random_sample(nSed)*5.0 else: redshiftList_1 = None if addGav: galacticAvList_1 = self.rng.random_sample(nSed)*0.3 + 0.1 else: galacticAvList_1 = None testList.loadSedsFromList(sedNameList_1, magNormList_1, internalAvList=internalAvList_1, galacticAvList=galacticAvList_1, redshiftList=redshiftList_1) self.assertEqual(len(testList), 2*nSed) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_0)): self.assertAlmostEqual(internalAvList_0[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_0[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_0[ix], testList.redshiftList[ix], 10) for ix in range(len(sedNameList_1)): if addIav: self.assertAlmostEqual(internalAvList_1[ix], testList.internalAvList[ix+nSed], 10) else: self.assertIsNone(testList.internalAvList[ix+nSed]) if addGav: self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix+nSed], 10) else: self.assertIsNone(testList.galacticAvList[ix+nSed]) if addRedshift: self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix+nSed], 10) else: self.assertIsNone(testList.redshiftList[ix+nSed]) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_0, magNormList_0, internalAvList_0, galacticAvList_0, redshiftList_0)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) if not addIav: internalAvList_1 = [None] * nSed if not addRedshift: redshiftList_1 = [None] * nSed if not addGav: galacticAvList_1 = [None] * nSed for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_1, magNormList_1, internalAvList_1, galacticAvList_1, redshiftList_1)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) if addIav: a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) if addRedshift: sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) if addGav: a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix+nSed] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
sed = Sed() sed.readSED_flambda( os.path.join(os.environ['SIMS_SED_LIBRARY_DIR'], defaultSpecMap[star['sedFilename']])) fnorm = getImsimFluxNorm(sed, star['magNorm']) sed.multiplyFluxNorm(fnorm) mags = lsst_bp.magListForSed(sed) du_nodust = np.abs(mags[0] - star['umag']) dg_nodust = np.abs(mags[1] - star['gmag']) dr_nodust = np.abs(mags[2] - star['rmag']) di_nodust = np.abs(mags[3] - star['imag']) dz_nodust = np.abs(mags[4] - star['zmag']) dy_nodust = np.abs(mags[5] - star['ymag']) a_x, b_x = sed.setupCCM_ab() sed.addDust(a_x, b_x, R_v=3.1, ebv=star['ebv']) mags = lsst_bp.magListForSed(sed) du = np.abs(mags[0] - star['umag']) dg = np.abs(mags[1] - star['gmag']) dr = np.abs(mags[2] - star['rmag']) di = np.abs(mags[3] - star['imag']) dz = np.abs(mags[4] - star['zmag']) dy = np.abs(mags[5] - star['ymag']) print('%.2e %.2e %2e %.2e %.2e %.2e -- %.2e %.2e %2e %.2e %.2e %.2e' % (du_nodust, dg_nodust, dr_nodust, di_nodust, dz_nodust, dy_nodust, du, dg, dr, di, dz, dy)) break
def test_mixed_stars(self): """ Here we will test the (somewhat absurd) case of a catalog with two different bandpasses (lsst_ and cartoon_) in order to verify that gamma values are being cached correctly """ lsst_u_band = Bandpass() lsst_u_band.readThroughput( os.path.join(getPackageDir('throughputs'), 'baseline', 'total_u.dat')) lsst_g_band = Bandpass() lsst_g_band.readThroughput( os.path.join(getPackageDir('throughputs'), 'baseline', 'total_g.dat')) obs = ObservationMetaData(bandpassName=['c_u', 'c_g', 'u', 'g'], m5=[25.0, 26.0, 15.0, 16.0]) # make the difference in m5 between the two bandpass systems extreme # so that, in the unit test, we can be sure that the correct values # are being used for the correct getters db_dtype = np.dtype([('id', np.int), ('raJ2000', np.float), ('decJ2000', np.float), ('sedFilename', str, 100), ('magNorm', np.float), ('galacticAv', np.float)]) inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'testData') inputFile = os.path.join(inputDir, 'IndicesTestCatalogStars.txt') db = fileDBObject(inputFile, dtype=db_dtype, runtable='test', idColKey='id') cat = CartoonStars(db, obs_metadata=obs, column_outputs=[ 'lsst_u', 'lsst_g', 'sigma_lsst_u', 'sigma_lsst_g' ]) with lsst.utils.tests.getTempFilePath('.txt') as catName: cat.write_catalog(catName) dtype = np.dtype([(name, np.float) for name in cat._column_outputs]) controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',') db_columns = db.query_columns([ 'id', 'raJ2000', 'decJ2000', 'sedFilename', 'magNorm', 'galacticAv' ]) sedDir = os.path.join(getPackageDir('sims_sed_library'), 'starSED', 'kurucz') for ix, line in enumerate(next(db_columns)): spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, line[3])) fnorm = spectrum.calcFluxNorm(line[4], self.normband) spectrum.multiplyFluxNorm(fnorm) a_x, b_x = spectrum.setupCCM_ab() spectrum.addDust(a_x, b_x, A_v=line[5]) umag = spectrum.calcMag(self.uband) self.assertAlmostEqual(umag, controlData['cartoon_u'][ix], 3) gmag = spectrum.calcMag(self.gband) self.assertAlmostEqual(gmag, controlData['cartoon_g'][ix], 3) lsst_umag = spectrum.calcMag(lsst_u_band) self.assertAlmostEqual(lsst_umag, controlData['lsst_u'][ix], 3) lsst_gmag = spectrum.calcMag(lsst_g_band) self.assertAlmostEqual(lsst_gmag, controlData['lsst_g'][ix], 3) umagError, gamma = calcMagError_m5(umag, self.uband, obs.m5['c_u'], PhotometricParameters()) gmagError, gamma = calcMagError_m5(gmag, self.gband, obs.m5['c_g'], PhotometricParameters()) self.assertAlmostEqual(umagError, controlData['sigma_cartoon_u'][ix], 3) self.assertAlmostEqual(gmagError, controlData['sigma_cartoon_g'][ix], 3) lsst_umagError, gamma = calcMagError_m5(lsst_umag, lsst_u_band, obs.m5['u'], PhotometricParameters()) lsst_gmagError, gamma = calcMagError_m5(lsst_gmag, lsst_g_band, obs.m5['g'], PhotometricParameters()) self.assertAlmostEqual(lsst_umagError, controlData['sigma_lsst_u'][ix], 3) self.assertAlmostEqual(lsst_gmagError, controlData['sigma_lsst_g'][ix], 3) self.assertGreater(np.abs(lsst_umagError - umagError), 0.01) self.assertGreater(np.abs(lsst_gmagError - gmagError), 0.01)
def sed_from_galacticus_mags(galacticus_mags, redshift, redshift_true, H0, Om0, wav_min, wav_width, obs_lsst_mags): """ Fit SEDs from sims_sed_library to Galacticus galaxies based on the magnitudes in tophat filters. Parameters ---------- galacticus_mags is a numpy array such that galacticus_mags[i][j] is the magnitude of the jth star in the ith bandpass, where the bandpasses are ordered in ascending order of minimum wavelength. redshift is an array of redshifts for the galaxies being fit (includes cosmology and proper motion) redshift_true is an array of cosmological redshifts for the galaxies being fit H0 is the Hubbleparameter in units of km/s/Mpc Om0 is the critical density parameter for matter wav_min is a numpy array of the minimum wavelengths of the tophat filters (in nm) wav_grid is a numpy array of the widths of the tophat filters (in nm) ob_lsst_mags is a numpy array of observer frame LSST magnitudes. obs_lsst_mags[0] will contain the u band magnitudes of every object. Returns ------- a numpy array of SED names and a numpy array of magNorms. """ if (not hasattr(sed_from_galacticus_mags, '_color_tree') or not np.allclose(wav_min, sed_from_galacticus_mags._wav_min, atol=1.0e-10, rtol=0.0) or not np.allclose(wav_width, sed_from_galacticus_mags._wav_width, atol=1.0e-10, rtol=0.0)): (sed_names, sed_mag_list, sed_mag_norm, av_grid, rv_grid) = _create_sed_library_mags(wav_min, wav_width) assert rv_grid.min() > 0.0 assert len(np.where(np.logical_not(np.isfinite(rv_grid)))[0]) == 0 sed_colors = sed_mag_list[:, 1:] - sed_mag_list[:, :-1] sed_from_galacticus_mags._sed_names = sed_names sed_from_galacticus_mags._mag_norm = sed_mag_norm # N_sed sed_from_galacticus_mags._av_grid = av_grid sed_from_galacticus_mags._rv_grid = rv_grid sed_from_galacticus_mags._sed_mags = sed_mag_list # N_sed by N_mag sed_from_galacticus_mags._color_tree = scipy_spatial.cKDTree( sed_colors) sed_from_galacticus_mags._wav_min = wav_min sed_from_galacticus_mags._wav_width = wav_width if (not hasattr(sed_from_galacticus_mags, '_cosmo') or np.abs(sed_from_galacticus_mags._cosmo.H() - H0) > 1.0e-6 or np.abs(sed_from_galacticus_mags._cosmo.OmegaMatter() - Om0) > 1.0e-6): sed_from_galacticus_mags._cosmo = CosmologyObject(H0=H0, Om0=Om0) galacticus_mags_t = np.asarray(galacticus_mags).T # N_star by N_mag assert galacticus_mags_t.shape == ( len(redshift), sed_from_galacticus_mags._sed_mags.shape[1]) with np.errstate(invalid='ignore', divide='ignore'): galacticus_colors = galacticus_mags_t[:, 1:] - galacticus_mags_t[:, : -1] # N_star by (N_mag - 1) t_start = time.time() (sed_dist, sed_idx) = sed_from_galacticus_mags._color_tree.query(galacticus_colors, k=1) # cKDTree returns an invalid index (==len(tree_data)) in cases # where the distance is not finite sed_idx = np.where(sed_idx < len(sed_from_galacticus_mags._sed_names), sed_idx, 0) distance_modulus = sed_from_galacticus_mags._cosmo.distanceModulus( redshift=redshift_true) output_names = sed_from_galacticus_mags._sed_names[sed_idx] (lsst_bp_dict, dummy_bp_dict) = BandpassDict.loadBandpassesFromFiles() output_mag_norm = np.zeros((6, len(output_names)), dtype=float) base_norm = sed_from_galacticus_mags._mag_norm[sed_idx] assert len(np.where(np.logical_not(np.isfinite(base_norm)))[0]) == 0 ccm_w = None av_arr = sed_from_galacticus_mags._av_grid[sed_idx] rv_arr = sed_from_galacticus_mags._rv_grid[sed_idx] assert rv_arr.min() > 0.0 assert len(np.where(np.logical_not(np.isfinite(rv_arr)))[0]) == 0 for i_bp in range(6): output_mag_norm[i_bp, :] = base_norm + distance_modulus sed_dir = getPackageDir('sims_sed_library') for i_obj in range(len(output_names)): spec = Sed() spec.readSED_flambda(os.path.join(sed_dir, output_names[i_obj])) if ccm_w is None or not np.array_equal(spec.wavelen, ccm_w): ccm_w = np.copy(spec.wavelen) ax, bx = spec.setupCCM_ab() spec.addDust(ax, bx, A_v=av_arr[i_obj], R_v=rv_arr[i_obj]) spec.redshiftSED(redshift[i_obj], dimming=True) lsst_mags = lsst_bp_dict.magListForSed(spec) d_mag = obs_lsst_mags[:, i_obj] - lsst_mags output_mag_norm[:, i_obj] += d_mag return (output_names, output_mag_norm, av_arr, rv_arr)
def test_mixed_stars(self): """ Here we will test the (somewhat absurd) case of a catalog with two different bandpasses (lsst_ and cartoon_) in order to verify that gamma values are being cached correctly """ lsst_u_band = Bandpass() lsst_u_band.readThroughput(os.path.join(getPackageDir('throughputs'), 'baseline', 'total_u.dat')) lsst_g_band = Bandpass() lsst_g_band.readThroughput(os.path.join(getPackageDir('throughputs'), 'baseline', 'total_g.dat')) obs = ObservationMetaData(bandpassName=['c_u', 'c_g', 'u', 'g'], m5=[25.0, 26.0, 15.0, 16.0]) # make the difference in m5 between the two bandpass systems extreme # so that, in the unit test, we can be sure that the correct values # are being used for the correct getters db_dtype = np.dtype([('id', np.int), ('raJ2000', np.float), ('decJ2000', np.float), ('sedFilename', str, 100), ('magNorm', np.float), ('galacticAv', np.float)]) inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'testData') inputFile = os.path.join(inputDir, 'IndicesTestCatalogStars.txt') db = fileDBObject(inputFile, dtype=db_dtype, runtable='test', idColKey='id') cat = CartoonStars(db, obs_metadata=obs, column_outputs=['lsst_u', 'lsst_g', 'sigma_lsst_u', 'sigma_lsst_g']) with lsst.utils.tests.getTempFilePath('.txt') as catName: cat.write_catalog(catName) dtype = np.dtype([(name, np.float) for name in cat._column_outputs]) controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',') db_columns = db.query_columns(['id', 'raJ2000', 'decJ2000', 'sedFilename', 'magNorm', 'galacticAv']) sedDir = os.path.join(getPackageDir('sims_sed_library'), 'starSED', 'kurucz') for ix, line in enumerate(next(db_columns)): spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, line[3])) fnorm = spectrum.calcFluxNorm(line[4], self.normband) spectrum.multiplyFluxNorm(fnorm) a_x, b_x = spectrum.setupCCM_ab() spectrum.addDust(a_x, b_x, A_v=line[5]) umag = spectrum.calcMag(self.uband) self.assertAlmostEqual(umag, controlData['cartoon_u'][ix], 3) gmag = spectrum.calcMag(self.gband) self.assertAlmostEqual(gmag, controlData['cartoon_g'][ix], 3) lsst_umag = spectrum.calcMag(lsst_u_band) self.assertAlmostEqual(lsst_umag, controlData['lsst_u'][ix], 3) lsst_gmag = spectrum.calcMag(lsst_g_band) self.assertAlmostEqual(lsst_gmag, controlData['lsst_g'][ix], 3) umagError, gamma = calcMagError_m5(umag, self.uband, obs.m5['c_u'], PhotometricParameters()) gmagError, gamma = calcMagError_m5(gmag, self.gband, obs.m5['c_g'], PhotometricParameters()) self.assertAlmostEqual(umagError, controlData['sigma_cartoon_u'][ix], 3) self.assertAlmostEqual(gmagError, controlData['sigma_cartoon_g'][ix], 3) lsst_umagError, gamma = calcMagError_m5(lsst_umag, lsst_u_band, obs.m5['u'], PhotometricParameters()) lsst_gmagError, gamma = calcMagError_m5(lsst_gmag, lsst_g_band, obs.m5['g'], PhotometricParameters()) self.assertAlmostEqual(lsst_umagError, controlData['sigma_lsst_u'][ix], 3) self.assertAlmostEqual(lsst_gmagError, controlData['sigma_lsst_g'][ix], 3) self.assertGreater(np.abs(lsst_umagError-umagError), 0.01) self.assertGreater(np.abs(lsst_gmagError-gmagError), 0.01)
dexes_to_validate = np.arange(len(control_qties['redshift']), dtype=int) max_offset = -1.0 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
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)
def test_object_extraction_stars(self): """ Test that method to get GalSimCelestialObjects from InstanceCatalogs works """ commands = desc.imsim.metadata_from_file(self.phosim_file) obs_md = desc.imsim.phosim_obs_metadata(commands) phot_params = desc.imsim.photometricParameters(commands) with desc.imsim.fopen(self.phosim_file, mode='rt') as input_: lines = [x for x in input_ if x.startswith('object')] truth_dtype = np.dtype([('uniqueId', str, 200), ('x_pupil', float), ('y_pupil', float), ('sedFilename', str, 200), ('magNorm', float), ('raJ2000', float), ('decJ2000', float), ('pmRA', float), ('pmDec', float), ('parallax', float), ('v_rad', float), ('Av', float), ('Rv', float)]) truth_data = np.genfromtxt(os.path.join(self.data_dir, 'truth_stars.txt'), dtype=truth_dtype, delimiter=';') truth_data.sort() gs_object_arr, gs_object_dict \ = sources_from_list(lines, obs_md, phot_params, self.phosim_file) id_arr = [None] * len(gs_object_arr) for i_obj in range(len(gs_object_arr)): id_arr[i_obj] = gs_object_arr[i_obj].uniqueId id_arr = sorted(id_arr) np.testing.assert_array_equal(truth_data['uniqueId'], id_arr) ######## test that pupil coordinates are correct to within ######## half a milliarcsecond x_pup_test, y_pup_test = _pupilCoordsFromRaDec( truth_data['raJ2000'], truth_data['decJ2000'], pm_ra=truth_data['pmRA'], pm_dec=truth_data['pmDec'], v_rad=truth_data['v_rad'], parallax=truth_data['parallax'], obs_metadata=obs_md) for gs_obj in gs_object_arr: i_obj = np.where(truth_data['uniqueId'] == gs_obj.uniqueId)[0][0] dd = np.sqrt((x_pup_test[i_obj] - gs_obj.xPupilRadians)**2 + (y_pup_test[i_obj] - gs_obj.yPupilRadians)**2) dd = arcsecFromRadians(dd) self.assertLess(dd, 0.0005) ######## test that fluxes are correctly calculated bp_dict = BandpassDict.loadTotalBandpassesFromFiles() imsim_bp = Bandpass() imsim_bp.imsimBandpass() phot_params = PhotometricParameters(nexp=1, exptime=30.0) for gs_obj in gs_object_arr: i_obj = np.where(truth_data['uniqueId'] == gs_obj.uniqueId)[0][0] sed = Sed() full_sed_name = os.path.join(os.environ['SIMS_SED_LIBRARY_DIR'], truth_data['sedFilename'][i_obj]) sed.readSED_flambda(full_sed_name) fnorm = sed.calcFluxNorm(truth_data['magNorm'][i_obj], imsim_bp) sed.multiplyFluxNorm(fnorm) sed.resampleSED(wavelen_match=bp_dict.wavelenMatch) a_x, b_x = sed.setupCCM_ab() sed.addDust(a_x, b_x, A_v=truth_data['Av'][i_obj], R_v=truth_data['Rv'][i_obj]) for bp in ('u', 'g', 'r', 'i', 'z', 'y'): flux = sed.calcADU(bp_dict[bp], phot_params) * phot_params.gain self.assertAlmostEqual(flux / gs_obj.flux(bp), 1.0, 10) ######## test that objects are assigned to the right chip in ######## gs_object_dict unique_id_dict = {} for chip_name in gs_object_dict: local_unique_id_list = [] for gs_object in gs_object_dict[chip_name]: local_unique_id_list.append(gs_object.uniqueId) local_unique_id_list = set(local_unique_id_list) unique_id_dict[chip_name] = local_unique_id_list valid = 0 valid_chip_names = set() for unq, xpup, ypup in zip(truth_data['uniqueId'], truth_data['x_pupil'], truth_data['y_pupil']): chip_name = chipNameFromPupilCoordsLSST(xpup, ypup) if chip_name is not None: self.assertIn(unq, unique_id_dict[chip_name]) valid_chip_names.add(chip_name) valid += 1 self.assertGreater(valid, 10) self.assertGreater(len(valid_chip_names), 5)
def test_object_extraction_galaxies(self): """ Test that method to get GalSimCelestialObjects from InstanceCatalogs works """ # Read in test_imsim_configs since default ones may change. desc.imsim.read_config( os.path.join(self.data_dir, 'test_imsim_configs')) galaxy_phosim_file = os.path.join(self.data_dir, 'phosim_galaxies.txt') commands = desc.imsim.metadata_from_file(galaxy_phosim_file) obs_md = desc.imsim.phosim_obs_metadata(commands) phot_params = desc.imsim.photometricParameters(commands) with desc.imsim.fopen(galaxy_phosim_file, mode='rt') as input_: lines = [x for x in input_ if x.startswith('object')] truth_dtype = np.dtype([('uniqueId', str, 200), ('x_pupil', float), ('y_pupil', float), ('sedFilename', str, 200), ('magNorm', float), ('raJ2000', float), ('decJ2000', float), ('redshift', float), ('gamma1', float), ('gamma2', float), ('kappa', float), ('galacticAv', float), ('galacticRv', float), ('internalAv', float), ('internalRv', float), ('minorAxis', float), ('majorAxis', float), ('positionAngle', float), ('sindex', float)]) truth_data = np.genfromtxt(os.path.join(self.data_dir, 'truth_galaxies.txt'), dtype=truth_dtype, delimiter=';') truth_data.sort() gs_object_arr, gs_object_dict \ = sources_from_list(lines, obs_md, phot_params, galaxy_phosim_file) id_arr = [None] * len(gs_object_arr) for i_obj in range(len(gs_object_arr)): id_arr[i_obj] = gs_object_arr[i_obj].uniqueId id_arr = sorted(id_arr) np.testing.assert_array_equal(truth_data['uniqueId'], id_arr) ######## test that galaxy parameters are correctly read in g1 = truth_data['gamma1'] / (1.0 - truth_data['kappa']) g2 = truth_data['gamma2'] / (1.0 - truth_data['kappa']) mu = 1.0 / ((1.0 - truth_data['kappa'])**2 - (truth_data['gamma1']**2 + truth_data['gamma2']**2)) for gs_obj in gs_object_arr: i_obj = np.where(truth_data['uniqueId'] == gs_obj.uniqueId)[0][0] self.assertAlmostEqual(gs_obj.mu / mu[i_obj], 1.0, 6) self.assertAlmostEqual(gs_obj.g1 / g1[i_obj], 1.0, 6) self.assertAlmostEqual(gs_obj.g2 / g2[i_obj], 1.0, 6) self.assertGreater(np.abs(gs_obj.mu), 0.0) self.assertGreater(np.abs(gs_obj.g1), 0.0) self.assertGreater(np.abs(gs_obj.g2), 0.0) self.assertAlmostEqual(gs_obj.halfLightRadiusRadians, truth_data['majorAxis'][i_obj], 13) self.assertAlmostEqual(gs_obj.minorAxisRadians, truth_data['minorAxis'][i_obj], 13) self.assertAlmostEqual(gs_obj.majorAxisRadians, truth_data['majorAxis'][i_obj], 13) self.assertAlmostEqual(2. * np.pi - gs_obj.positionAngleRadians, truth_data['positionAngle'][i_obj], 7) self.assertAlmostEqual(gs_obj.sindex, truth_data['sindex'][i_obj], 10) ######## test that pupil coordinates are correct to within ######## half a milliarcsecond x_pup_test, y_pup_test = _pupilCoordsFromRaDec(truth_data['raJ2000'], truth_data['decJ2000'], obs_metadata=obs_md) for gs_obj in gs_object_arr: i_obj = np.where(truth_data['uniqueId'] == gs_obj.uniqueId)[0][0] dd = np.sqrt((x_pup_test[i_obj] - gs_obj.xPupilRadians)**2 + (y_pup_test[i_obj] - gs_obj.yPupilRadians)**2) dd = arcsecFromRadians(dd) self.assertLess(dd, 0.0005) ######## test that fluxes are correctly calculated bp_dict = BandpassDict.loadTotalBandpassesFromFiles() imsim_bp = Bandpass() imsim_bp.imsimBandpass() phot_params = PhotometricParameters(nexp=1, exptime=30.0) for gs_obj in gs_object_arr: i_obj = np.where(truth_data['uniqueId'] == gs_obj.uniqueId)[0][0] sed = Sed() full_sed_name = os.path.join(os.environ['SIMS_SED_LIBRARY_DIR'], truth_data['sedFilename'][i_obj]) sed.readSED_flambda(full_sed_name) fnorm = sed.calcFluxNorm(truth_data['magNorm'][i_obj], imsim_bp) sed.multiplyFluxNorm(fnorm) a_x, b_x = sed.setupCCM_ab() sed.addDust(a_x, b_x, A_v=truth_data['internalAv'][i_obj], R_v=truth_data['internalRv'][i_obj]) sed.redshiftSED(truth_data['redshift'][i_obj], dimming=True) sed.resampleSED(wavelen_match=bp_dict.wavelenMatch) a_x, b_x = sed.setupCCM_ab() sed.addDust(a_x, b_x, A_v=truth_data['galacticAv'][i_obj], R_v=truth_data['galacticRv'][i_obj]) for bp in ('u', 'g', 'r', 'i', 'z', 'y'): flux = sed.calcADU(bp_dict[bp], phot_params) * phot_params.gain self.assertAlmostEqual(flux / gs_obj.flux(bp), 1.0, 6) ######## test that objects are assigned to the right chip in ######## gs_object_dict unique_id_dict = {} for chip_name in gs_object_dict: local_unique_id_list = [] for gs_object in gs_object_dict[chip_name]: local_unique_id_list.append(gs_object.uniqueId) local_unique_id_list = set(local_unique_id_list) unique_id_dict[chip_name] = local_unique_id_list valid = 0 valid_chip_names = set() for unq, xpup, ypup in zip(truth_data['uniqueId'], truth_data['x_pupil'], truth_data['y_pupil']): chip_name = chipNameFromPupilCoordsLSST(xpup, ypup) if chip_name is not None: self.assertIn(unq, unique_id_dict[chip_name]) valid_chip_names.add(chip_name) valid += 1 self.assertGreater(valid, 10) self.assertGreater(len(valid_chip_names), 5)
def testObjectPlacement(self): """ Test that GalSim places objects on the correct pixel by drawing images, reading them in, and then comparing the flux contained in circles of 2 fwhm radii about the object's expected positions with the actual expected flux of the objects. """ scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testObjectPlacement-') catName = os.path.join(scratchDir, 'placementCatalog.dat') imageRoot = os.path.join(scratchDir, 'placementImage') dbFileName = os.path.join(scratchDir, 'placementInputCatalog.dat') cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData') camera = ReturnCamera(cameraDir) detector = camera[0] imageName = '%s_%s_u.fits' % (imageRoot, detector.getName()) controlSed = Sed() controlSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), 'flatSED', 'sed_flat.txt.gz')) uBandpass = Bandpass() uBandpass.readThroughput( os.path.join(getPackageDir('throughputs'), 'baseline', 'total_u.dat')) controlBandpass = Bandpass() controlBandpass.imsimBandpass() ff = controlSed.calcFluxNorm(self.magNorm, uBandpass) controlSed.multiplyFluxNorm(ff) a_int, b_int = controlSed.setupCCM_ab() controlSed.addDust(a_int, b_int, A_v=0.1, R_v=3.1) nSamples = 3 rng = np.random.RandomState(42) pointingRaList = rng.random_sample(nSamples) * 360.0 pointingDecList = rng.random_sample(nSamples) * 180.0 - 90.0 rotSkyPosList = rng.random_sample(nSamples) * 360.0 fwhmList = rng.random_sample(nSamples) * 1.0 + 0.3 actualCounts = None for pointingRA, pointingDec, rotSkyPos, fwhm in \ zip(pointingRaList, pointingDecList, rotSkyPosList, fwhmList): obs = ObservationMetaData(pointingRA=pointingRA, pointingDec=pointingDec, boundType='circle', boundLength=4.0, mjd=49250.0, rotSkyPos=rotSkyPos) xDisplacementList = rng.random_sample(nSamples) * 60.0 - 30.0 yDisplacementList = rng.random_sample(nSamples) * 60.0 - 30.0 create_text_catalog(obs, dbFileName, xDisplacementList, yDisplacementList, mag_norm=[self.magNorm] * len(xDisplacementList)) db = placementFileDBObj(dbFileName, runtable='test') cat = placementCatalog(db, obs_metadata=obs) cat.camera_wrapper = GalSimCameraWrapper(camera) if actualCounts is None: actualCounts = controlSed.calcADU(uBandpass, cat.photParams) psf = SNRdocumentPSF(fwhm=fwhm) cat.setPSF(psf) cat.write_catalog(catName) cat.write_images(nameRoot=imageRoot) objRaList = [] objDecList = [] with open(catName, 'r') as inFile: for line in inFile: if line[0] != '#': words = line.split(';') objRaList.append(np.radians(np.float(words[2]))) objDecList.append(np.radians(np.float(words[3]))) objRaList = np.array(objRaList) objDecList = np.array(objDecList) self.assertGreater(len(objRaList), 0) # make sure we aren't testing # an empty catalog/image self.check_placement(imageName, objRaList, objDecList, [fwhm] * len(objRaList), np.array([actualCounts] * len(objRaList)), cat.photParams.gain, detector, camera, obs, epoch=2000.0) if os.path.exists(dbFileName): os.unlink(dbFileName) if os.path.exists(catName): os.unlink(catName) if os.path.exists(imageName): os.unlink(imageName) if os.path.exists(scratchDir): shutil.rmtree(scratchDir)
def testGalaxyPhotometricUncertainties(self): """ Test in the case of a catalog of galaxies """ lsstDefaults = LSSTdefaults() galDB = testGalaxyTileDBObj(driver=self.driver, host=self.host, database=self.dbName) galCat = testGalaxyCatalog(galDB, obs_metadata=self.obs_metadata) imsimband = Bandpass() imsimband.imsimBandpass() ct = 0 for line in galCat.iter_catalog(): bulgeSedName = line[50] diskSedName = line[51] agnSedName = line[52] magNormBulge = line[53] magNormDisk = line[54] magNormAgn = line[55] avBulge = line[56] avDisk = line[57] redshift = line[58] bulgeSed = Sed() bulgeSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), defaultSpecMap[bulgeSedName])) fNorm = bulgeSed.calcFluxNorm(magNormBulge, imsimband) bulgeSed.multiplyFluxNorm(fNorm) diskSed = Sed() diskSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), defaultSpecMap[diskSedName])) fNorm = diskSed.calcFluxNorm(magNormDisk, imsimband) diskSed.multiplyFluxNorm(fNorm) agnSed = Sed() agnSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), defaultSpecMap[agnSedName])) fNorm = agnSed.calcFluxNorm(magNormAgn, imsimband) agnSed.multiplyFluxNorm(fNorm) a_int, b_int = bulgeSed.setupCCM_ab() bulgeSed.addDust(a_int, b_int, A_v=avBulge) a_int, b_int = diskSed.setupCCM_ab() diskSed.addDust(a_int, b_int, A_v=avDisk) bulgeSed.redshiftSED(redshift, dimming=True) diskSed.redshiftSED(redshift, dimming=True) agnSed.redshiftSED(redshift, dimming=True) bulgeSed.resampleSED(wavelen_match=self.totalBandpasses[0].wavelen) diskSed.resampleSED(wavelen_match=bulgeSed.wavelen) agnSed.resampleSED(wavelen_match=bulgeSed.wavelen) np.testing.assert_almost_equal(bulgeSed.wavelen, diskSed.wavelen) np.testing.assert_almost_equal(bulgeSed.wavelen, agnSed.wavelen) fl = bulgeSed.flambda + diskSed.flambda + agnSed.flambda totalSed = Sed(wavelen=bulgeSed.wavelen, flambda=fl) sedList = [totalSed, bulgeSed, diskSed, agnSed] for i, spectrum in enumerate(sedList): if i == 0: msgroot = 'failed on total' elif i == 1: msgroot = 'failed on bulge' elif i == 2: msgroot = 'failed on disk' elif i == 3: msgroot = 'failed on agn' for j, b in enumerate(self.bandpasses): controlSigma = calcMagError_sed( spectrum, self.totalBandpasses[j], self.skySeds[j], self.hardwareBandpasses[j], FWHMeff=lsstDefaults.FWHMeff(b), photParams=PhotometricParameters()) testSigma = line[26 + (i * 6) + j] msg = '%e neq %e; ' % (testSigma, controlSigma) + msgroot self.assertAlmostEqual(testSigma, controlSigma, 10, msg=msg) ct += 1 self.assertGreater(ct, 0)
def testFlush(self): """ Test that the flush method of SedList behaves properly """ imsimBand = Bandpass() imsimBand.imsimBandpass() nSed = 10 sedNameList_0 = self.getListOfSedNames(nSed) magNormList_0 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = self.rng.random_sample(nSed)*5.0 galacticAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 wavelen_match = np.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, fileDir=self.sedDir, internalAvList=internalAvList_0, redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) self.assertEqual(len(testList), nSed) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_0)): self.assertAlmostEqual(internalAvList_0[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_0[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_0[ix], testList.redshiftList[ix], 10) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_0, magNormList_0, internalAvList_0, galacticAvList_0, redshiftList_0)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) testList.flush() sedNameList_1 = self.getListOfSedNames(nSed//2) magNormList_1 = self.rng.random_sample(nSed//2)*5.0 + 15.0 internalAvList_1 = self.rng.random_sample(nSed//2)*0.3 + 0.1 redshiftList_1 = self.rng.random_sample(nSed//2)*5.0 galacticAvList_1 = self.rng.random_sample(nSed//2)*0.3 + 0.1 testList.loadSedsFromList(sedNameList_1, magNormList_1, internalAvList=internalAvList_1, galacticAvList=galacticAvList_1, redshiftList=redshiftList_1) self.assertEqual(len(testList), nSed/2) self.assertEqual(len(testList.redshiftList), nSed/2) self.assertEqual(len(testList.internalAvList), nSed/2) self.assertEqual(len(testList.galacticAvList), nSed/2) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_1)): self.assertAlmostEqual(internalAvList_1[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix], 10) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_1, magNormList_1, internalAvList_1, galacticAvList_1, redshiftList_1)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
def testAddingNonesToList(self): """ Test what happens if you add SEDs to an SedList that have None for one or more of the physical parameters (i.e. galacticAv, internalAv, or redshift) """ imsimBand = Bandpass() imsimBand.imsimBandpass() nSed = 10 sedNameList_0 = self.getListOfSedNames(nSed) magNormList_0 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = self.rng.random_sample(nSed)*5.0 galacticAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 wavelen_match = np.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, fileDir=self.sedDir, internalAvList=internalAvList_0, redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) sedNameList_1 = self.getListOfSedNames(nSed) magNormList_1 = list(self.rng.random_sample(nSed)*5.0 + 15.0) internalAvList_1 = list(self.rng.random_sample(nSed)*0.3 + 0.1) redshiftList_1 = list(self.rng.random_sample(nSed)*5.0) galacticAvList_1 = list(self.rng.random_sample(nSed)*0.3 + 0.1) internalAvList_1[0] = None redshiftList_1[1] = None galacticAvList_1[2] = None internalAvList_1[3] = None redshiftList_1[3] = None internalAvList_1[4] = None galacticAvList_1[4] = None redshiftList_1[5] = None galacticAvList_1[5] = None internalAvList_1[6] = None redshiftList_1[6] = None galacticAvList_1[6] = None testList.loadSedsFromList(sedNameList_1, magNormList_1, internalAvList=internalAvList_1, galacticAvList=galacticAvList_1, redshiftList=redshiftList_1) self.assertEqual(len(testList), 2*nSed) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_0)): self.assertAlmostEqual(internalAvList_0[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_0[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_0[ix], testList.redshiftList[ix], 10) for ix in range(len(sedNameList_1)): self.assertAlmostEqual(internalAvList_1[ix], testList.internalAvList[ix+nSed], 10) self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix+nSed], 10) self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix+nSed], 10) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_0, magNormList_0, internalAvList_0, galacticAvList_0, redshiftList_0)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_1, magNormList_1, internalAvList_1, galacticAvList_1, redshiftList_1)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) if iav is not None: a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) if zz is not None: sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) if gav is not None: a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix+nSed] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
def testAlternateNormalizingBandpass(self): """ A reiteration of testAddingToList, but testing with a non-imsimBandpass normalizing bandpass """ normalizingBand = Bandpass() normalizingBand.readThroughput(os.path.join(getPackageDir('throughputs'), 'baseline', 'total_r.dat')) nSed = 10 sedNameList_0 = self.getListOfSedNames(nSed) magNormList_0 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = self.rng.random_sample(nSed)*5.0 galacticAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 wavelen_match = np.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, fileDir=self.sedDir, normalizingBandpass=normalizingBand, internalAvList=internalAvList_0, redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) sedNameList_1 = self.getListOfSedNames(nSed) magNormList_1 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_1 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_1 = self.rng.random_sample(nSed)*5.0 galacticAvList_1 = self.rng.random_sample(nSed)*0.3 + 0.1 testList.loadSedsFromList(sedNameList_1, magNormList_1, internalAvList=internalAvList_1, galacticAvList=galacticAvList_1, redshiftList=redshiftList_1) self.assertEqual(len(testList), 2*nSed) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_0)): self.assertAlmostEqual(internalAvList_0[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_0[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_0[ix], testList.redshiftList[ix], 10) for ix in range(len(sedNameList_1)): self.assertAlmostEqual(internalAvList_1[ix], testList.internalAvList[ix+nSed], 10) self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix+nSed], 10) self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix+nSed], 10) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_0, magNormList_0, internalAvList_0, galacticAvList_0, redshiftList_0)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, normalizingBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_1, magNormList_1, internalAvList_1, galacticAvList_1, redshiftList_1)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, normalizingBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix+nSed] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
sed_dir = os.environ['SIMS_SED_LIBRARY_DIR'] sed_name = 'sed_flat.txt' lsst_bp = BandpassDict.loadTotalBandpassesFromFiles() ebv_grid_1 = np.arange(0.01, 8.0, 0.01) ebv_grid_2 = np.arange(9.0, 120.0, 1.0) ebv_grid = np.concatenate([ebv_grid_1, ebv_grid_2]) ext_grid = np.zeros((6, len(ebv_grid)), dtype=float) unextincted_sed = Sed() unextincted_sed.readSED_flambda(os.path.join(sed_dir, defaultSpecMap[sed_name])) unextincted_mags = lsst_bp.magListForSed(unextincted_sed) a_x, b_x = unextincted_sed.setupCCM_ab() for i_ebv, ebv in enumerate(ebv_grid): extincted_sed = Sed(wavelen=unextincted_sed.wavelen, flambda=unextincted_sed.flambda) extincted_sed.addDust(a_x, b_x, R_v=3.1, ebv=ebv) mags = lsst_bp.magListForSed(extincted_sed) for i_bp, bp in enumerate('ugrizy'): ext_grid[i_bp][i_ebv] = mags[i_bp] - unextincted_mags[i_bp] assert ext_grid.min() > 0.0 with h5py.File('data/ebv_grid.h5', 'w') as out_file: out_file.create_dataset('ebv_grid', data=ebv_grid) out_file.create_dataset('extinction_grid', data=ext_grid)
def testSetUp(self): """ Test the SedList can be successfully initialized """ ############## Try just reading in an normalizing some SEDs nSed = 10 sedNameList = self.getListOfSedNames(nSed) magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 testList = SedList(sedNameList, magNormList, fileDir=self.sedDir) self.assertEqual(len(testList), nSed) self.assertIsNone(testList.internalAvList) self.assertIsNone(testList.galacticAvList) self.assertIsNone(testList.redshiftList) self.assertIsNone(testList.wavelenMatch) self.assertTrue(testList.cosmologicalDimming) imsimBand = Bandpass() imsimBand.imsimBandpass() for name, norm, sedTest in zip(sedNameList, magNormList, testList): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################# now add an internalAv sedNameList = self.getListOfSedNames(nSed) magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 testList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList) self.assertIsNone(testList.galacticAvList) self.assertIsNone(testList.redshiftList) self.assertIsNone(testList.wavelenMatch) self.assertTrue(testList.cosmologicalDimming) for avControl, avTest in zip(internalAvList, testList.internalAvList): self.assertAlmostEqual(avControl, avTest, 10) for name, norm, av, sedTest in zip(sedNameList, magNormList, internalAvList, testList): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=av) np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################ now add redshift 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 testList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList) self.assertIsNone(testList.galacticAvList) self.assertIsNone(testList.wavelenMatch) self.assertTrue(testList.cosmologicalDimming) for avControl, avTest in zip(internalAvList, testList.internalAvList): self.assertAlmostEqual(avControl, avTest, 10) for zControl, zTest in zip(redshiftList, testList.redshiftList): self.assertAlmostEqual(zControl, zTest, 10) for name, norm, av, zz, sedTest in \ zip(sedNameList, magNormList, internalAvList, redshiftList, testList): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=True) np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################# without cosmological dimming 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 testList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, cosmologicalDimming=False) self.assertIsNone(testList.galacticAvList) self.assertIsNone(testList.wavelenMatch) self.assertFalse(testList.cosmologicalDimming) for avControl, avTest in zip(internalAvList, testList.internalAvList): self.assertAlmostEqual(avControl, avTest, 10) for zControl, zTest in zip(redshiftList, testList.redshiftList): self.assertAlmostEqual(zControl, zTest, 10) for name, norm, av, zz, sedTest in \ zip(sedNameList, magNormList, internalAvList, redshiftList, testList): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=False) np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################ now add galacticAv 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 testList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList) self.assertIsNone(testList.wavelenMatch) self.assertTrue(testList.cosmologicalDimming) for avControl, avTest in zip(internalAvList, testList.internalAvList): self.assertAlmostEqual(avControl, avTest, 10) for zControl, zTest in zip(redshiftList, testList.redshiftList): self.assertAlmostEqual(zControl, zTest, 10) for avControl, avTest in zip(galacticAvList, testList.galacticAvList): self.assertAlmostEqual(avControl, avTest, 10) for name, norm, av, zz, gav, sedTest in \ zip(sedNameList, magNormList, internalAvList, redshiftList, galacticAvList, testList): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=True) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################ now use a wavelen_match 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 wavelen_match = np.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList, wavelenMatch=wavelen_match) self.assertTrue(testList.cosmologicalDimming) for avControl, avTest in zip(internalAvList, testList.internalAvList): self.assertAlmostEqual(avControl, avTest, 10) for zControl, zTest in zip(redshiftList, testList.redshiftList): self.assertAlmostEqual(zControl, zTest, 10) for avControl, avTest in zip(galacticAvList, testList.galacticAvList): self.assertAlmostEqual(avControl, avTest, 10) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for name, norm, av, zz, gav, sedTest in \ zip(sedNameList, magNormList, internalAvList, redshiftList, galacticAvList, testList): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
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)