def setUp(self): starFileName = os.path.join(lsst.utils.getPackageDir("sims_sed_library"), "starSED") starFileName = os.path.join(starFileName, "kurucz", "km20_5750.fits_g40_5790.gz") starName = os.path.join(lsst.utils.getPackageDir("sims_sed_library"), starFileName) self.starSED = Sed() self.starSED.readSED_flambda(starName) imsimband = Bandpass() imsimband.imsimBandpass() fNorm = self.starSED.calcFluxNorm(22.0, imsimband) self.starSED.multiplyFluxNorm(fNorm) hardwareDir = os.path.join(lsst.utils.getPackageDir("throughputs"), "baseline") componentList = ["detector.dat", "m1.dat", "m2.dat", "m3.dat", "lens1.dat", "lens2.dat", "lens3.dat"] self.skySed = Sed() self.skySed.readSED_flambda(os.path.join(hardwareDir, "darksky.dat")) totalNameList = ["total_u.dat", "total_g.dat", "total_r.dat", "total_i.dat", "total_z.dat", "total_y.dat"] self.bpList = [] self.hardwareList = [] for name in totalNameList: dummy = Bandpass() dummy.readThroughput(os.path.join(hardwareDir, name)) self.bpList.append(dummy) dummy = Bandpass() hardwareNameList = [os.path.join(hardwareDir, name)] for component in componentList: hardwareNameList.append(os.path.join(hardwareDir, component)) dummy.readThroughputList(hardwareNameList) self.hardwareList.append(dummy) self.filterNameList = ["u", "g", "r", "i", "z", "y"]
def test_norm(self): """ Test that the special test case getImsimFluxNorm returns the same value as calling calcFluxNorm actually passing in the imsim Bandpass """ bp = Bandpass() bp.imsimBandpass() rng = np.random.RandomState(1123) wavelen = np.arange(300.0, 2000.0, 0.17) for ix in range(10): flux = rng.random_sample(len(wavelen))*100.0 sed = Sed() sed.setSED(wavelen=wavelen, flambda=flux) magmatch = rng.random_sample()*5.0 + 10.0 control = sed.calcFluxNorm(magmatch, bp) test = getImsimFluxNorm(sed, magmatch) # something about how interpolation is done in Sed means # that the values don't come out exactly equal. They come # out equal to 8 seignificant digits, though. self.assertEqual(control, test)
def test_norm(self): """ Test that the special test case getImsimFluxNorm returns the same value as calling calcFluxNorm actually passing in the imsim Bandpass """ bp = Bandpass() bp.imsimBandpass() rng = np.random.RandomState(1123) wavelen = np.arange(300.0, 2000.0, 0.17) for ix in range(10): flux = rng.random_sample(len(wavelen)) * 100.0 sed = Sed() sed.setSED(wavelen=wavelen, flambda=flux) magmatch = rng.random_sample() * 5.0 + 10.0 control = sed.calcFluxNorm(magmatch, bp) test = getImsimFluxNorm(sed, magmatch) # something about how interpolation is done in Sed means # that the values don't come out exactly equal. They come # out equal to 8 seignificant digits, though. self.assertEqual(control, test)
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) phot = PhotometryStars() ct = 0 for line in starCat.iter_catalog(): starSed = Sed() starSed.readSED_flambda(os.path.join(lsst.utils.getPackageDir('sims_sed_library'), defaultSpecMap[line[14]])) imsimband = Bandpass() imsimband.imsimBandpass() fNorm = starSed.calcFluxNorm(line[15], imsimband) starSed.multiplyFluxNorm(fNorm) aV = numpy.float(line[16]) a_int, b_int = starSed.setupCCMab() starSed.addCCMDust(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 get_phosimVars(self): """ Obtain variables sedFilepath to be used to obtain unique filenames for each SED for phoSim and MagNorm which is also used. Note that aside from acting as a getter, this also writes spectra to `self.sn_sedfile_prefix`snid_mjd_band.dat for each observation of interest """ # construct the unique filename # method: snid_mjd(to 4 places of decimal)_bandpassname mjd = "_{:0.4f}_".format(self.mjdobs) mjd += self.obs_metadata.bandpass + '.dat' fnames = np.array([ self.sn_sedfile_prefix + str(int(elem)) + mjd if isinstance( elem, numbers.Number) else self.sn_sedfile_prefix + str(elem) + mjd for elem in self.column_by_name('snid') ], dtype='str') c, x1, x0, t0, z = self.column_by_name('c'),\ self.column_by_name('x1'),\ self.column_by_name('x0'),\ self.column_by_name('t0'),\ self.column_by_name('redshift') bp = Bandpass() bp.imsimBandpass() magNorms = np.zeros(len(fnames)) snobject = SNObject() snobject.rectifySED = True for i in range(len(self.column_by_name('snid'))): # if t0 is nan, this was set by the catalog for dim SN, or SN # outside redshift range, We will not provide a SED file for these if np.isnan(t0[i]): magNorms[i] = np.nan fnames[i] = None else: snobject.set(c=c[i], x1=x1[i], x0=x0[i], t0=t0[i], z=z[i]) if snobject.modelOutSideTemporalRange == 'zero': if self.mjdobs > snobject.maxtime( ) or self.mjdobs < snobject.mintime(): magNorms[i] = np.nan fnames[i] = None # SED in rest frame sed = snobject.SNObjectSourceSED(time=self.mjdobs) try: magNorms[i] = sed.calcMag(bandpass=bp) except: # sed.flambda = 1.0e-20 magNorms[i] = 1000. # sed.calcMag(bandpass=bp) if self.writeSedFile: sed.writeSED(fnames[i]) return (fnames, magNorms)
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 calcMagNorm(self, objectMags, sedObj, bandpassDict, mag_error=None, redshift=None, filtRange=None): """ This will find the magNorm value that gives the closest match to the magnitudes of the object using the matched SED. Uses scipy.optimize.leastsq to find the values of fluxNorm that minimizes the function: ((flux_obs - (fluxNorm*flux_model))/flux_error)**2. @param [in] objectMags are the magnitude values for the object with extinction matching that of the SED object. In the normal case using the selectSED routines above it will be dereddened mags. @param [in] sedObj is an Sed class instance that is set with the wavelength and flux of the matched SED @param [in] bandpassDict is a BandpassDict class instance with the Bandpasses set to those for the magnitudes given for the catalog object @param [in] mag_error are provided error values for magnitudes in objectMags. If none provided then this defaults to 1.0. This should be an array of the same length as objectMags. @param [in] redshift is the redshift of the object if the magnitude is observed @param [in] filtRange is a selected range of filters specified by their indices in the bandpassList to match up against. Used when missing data in some magnitude bands. @param [out] bestMagNorm is the magnitude normalization for the given magnitudes and SED """ import scipy.optimize as opt sedTest = Sed() sedTest.setSED(sedObj.wavelen, flambda=sedObj.flambda) if redshift is not None: sedTest.redshiftSED(redshift) imSimBand = Bandpass() imSimBand.imsimBandpass() zp = -2.5 * np.log10(3631) #Note using default AB zeropoint flux_obs = np.power(10, (objectMags + zp) / (-2.5)) sedTest.resampleSED(wavelen_match=bandpassDict.wavelenMatch) sedTest.flambdaTofnu() flux_model = sedTest.manyFluxCalc(bandpassDict.phiArray, bandpassDict.wavelenStep) if filtRange is not None: flux_obs = flux_obs[filtRange] flux_model = flux_model[filtRange] if mag_error is None: flux_error = np.ones(len(flux_obs)) else: flux_error = np.abs(flux_obs * (np.log(10) / (-2.5)) * mag_error) bestFluxNorm = opt.leastsq( lambda x: ((flux_obs - (x * flux_model)) / flux_error), 1.0)[0][0] sedTest.multiplyFluxNorm(bestFluxNorm) bestMagNorm = sedTest.calcMag(imSimBand) return bestMagNorm
def _get_sed_mags_and_cosmology(catalog_name): """ Returns 3 numpy arrays: sed_names, sed_mag_list, sed_mag_norm and a dictionary for cosmology sed_names is 1d str array, with length = number of SED files in the library sed_mag_list is MxN float array, with M = number of SED files in the library, and N = number of top hat filters in the catalog sed_mag_norm is 1d float array, with length = number of SED files in the library cosmology = {'H0': H0, 'Om0': Om0} """ gc = GCRCatalogs.load_catalog(catalog_name, config_overwrite=dict(md5=False)) cosmo = dict(H0=gc.cosmology.H0.value, Om0=gc.cosmology.Om0) bp_params_raw = {'disk': set(), 'bulge': set()} for q in gc.list_all_quantities(): m = _gcr_sed_re.match(q) if m: wav0, width, tag = m.groups() bp_params_raw[tag].add((int(wav0), int(width))) assert bp_params_raw['disk'] == bp_params_raw[ 'bulge'], 'SEDs for disk and bulge do not match' assert bp_params_raw['disk'], 'No SED found' bp_params_sorted = sorted(bp_params_raw['disk'], key=lambda p: p[0]) # SED labels in GCR specify the band pass in angstrom, but CatSim uses nm # Hence the conversion factor 0.1 in the code below wav_min = bp_params_sorted[0][0] * 0.1 wav_max = max((0.1 * (wav0 + width) for wav0, width in bp_params_sorted)) wav_grid = np.arange(wav_min, wav_max, 0.1) bp_name_list = list() bp_list = list() for wav0, width in bp_params_sorted: sb_grid = ((wav_grid >= wav0 * 0.1) & (wav_grid <= (wav0 + width) * 0.1)).astype(float) bp_list.append(Bandpass(wavelen=wav_grid, sb=sb_grid)) bp_name_list.append('%d_%d' % (wav0, width)) bandpass_dict = BandpassDict(bp_list, bp_name_list) sed_names = list() sed_mag_list = list() sed_mag_norm = list() imsim_bp = Bandpass() imsim_bp.imsimBandpass() for sed_file_name in os.listdir(_galaxy_sed_dir): spec = Sed() spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name)) sed_names.append(sed_file_name) sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec))) sed_mag_norm.append(spec.calcMag(imsim_bp)) return np.array(sed_names), np.array(sed_mag_list), np.array( sed_mag_norm), cosmo
def get_phosimVars(self): """ Obtain variables sedFilepath to be used to obtain unique filenames for each SED for phoSim and MagNorm which is also used. Note that aside from acting as a getter, this also writes spectra to `self.sn_sedfile_prefix`snid_mjd_band.dat for each observation of interest """ # construct the unique filename # method: snid_mjd(to 4 places of decimal)_bandpassname mjd = "_{:0.4f}_".format(self.mjdobs) mjd += self.obs_metadata.bandpass + '.dat' fnames = np.array([self.sn_sedfile_prefix + str(int(elem)) + mjd if isinstance(elem, numbers.Number) else self.sn_sedfile_prefix + str(elem) + mjd for elem in self.column_by_name('snid')], dtype='str') c, x1, x0, t0, z = self.column_by_name('c'),\ self.column_by_name('x1'),\ self.column_by_name('x0'),\ self.column_by_name('t0'),\ self.column_by_name('redshift') bp = Bandpass() bp.imsimBandpass() magNorms = np.zeros(len(fnames)) snobject = SNObject() snobject.rectifySED = True for i in range(len(self.column_by_name('snid'))): # if t0 is nan, this was set by the catalog for dim SN, or SN # outside redshift range, We will not provide a SED file for these if np.isnan(t0[i]): magNorms[i] = np.nan fnames[i] = None else: snobject.set(c=c[i], x1=x1[i], x0=x0[i], t0=t0[i], z=z[i]) if snobject.modelOutSideTemporalRange == 'zero': if self.mjdobs > snobject.maxtime() or self.mjdobs < snobject.mintime(): magNorms[i] = np.nan fnames[i] = None # SED in rest frame sed = snobject.SNObjectSourceSED(time=self.mjdobs) try: magNorms[i] = sed.calcMag(bandpass=bp) except: # sed.flambda = 1.0e-20 magNorms[i] = 1000. # sed.calcMag(bandpass=bp) if self.writeSedFile: sed.writeSED(fnames[i]) return (fnames, magNorms)
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 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 calcMagNorm(self, objectMags, sedObj, bandpassDict, mag_error = None, redshift = None, filtRange = None): """ This will find the magNorm value that gives the closest match to the magnitudes of the object using the matched SED. Uses scipy.optimize.leastsq to find the values of fluxNorm that minimizes the function: ((flux_obs - (fluxNorm*flux_model))/flux_error)**2. @param [in] objectMags are the magnitude values for the object with extinction matching that of the SED object. In the normal case using the selectSED routines above it will be dereddened mags. @param [in] sedObj is an Sed class instance that is set with the wavelength and flux of the matched SED @param [in] bandpassDict is a BandpassDict class instance with the Bandpasses set to those for the magnitudes given for the catalog object @param [in] mag_error are provided error values for magnitudes in objectMags. If none provided then this defaults to 1.0. This should be an array of the same length as objectMags. @param [in] redshift is the redshift of the object if the magnitude is observed @param [in] filtRange is a selected range of filters specified by their indices in the bandpassList to match up against. Used when missing data in some magnitude bands. @param [out] bestMagNorm is the magnitude normalization for the given magnitudes and SED """ import scipy.optimize as opt sedTest = Sed() sedTest.setSED(sedObj.wavelen, flambda = sedObj.flambda) if redshift is not None: sedTest.redshiftSED(redshift) imSimBand = Bandpass() imSimBand.imsimBandpass() zp = -2.5*np.log10(3631) #Note using default AB zeropoint flux_obs = np.power(10,(objectMags + zp)/(-2.5)) sedTest.resampleSED(wavelen_match=bandpassDict.wavelenMatch) sedTest.flambdaTofnu() flux_model = sedTest.manyFluxCalc(bandpassDict.phiArray, bandpassDict.wavelenStep) if filtRange is not None: flux_obs = flux_obs[filtRange] flux_model = flux_model[filtRange] if mag_error is None: flux_error = np.ones(len(flux_obs)) else: flux_error = np.abs(flux_obs*(np.log(10)/(-2.5))*mag_error) bestFluxNorm = opt.leastsq(lambda x: ((flux_obs - (x*flux_model))/flux_error), 1.0)[0][0] sedTest.multiplyFluxNorm(bestFluxNorm) bestMagNorm = sedTest.calcMag(imSimBand) return bestMagNorm
def setUpClass(cls): cls.scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='allowedChipsTest-') cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1, mjd=57381.2, rotSkyPos=43.2, bandpassName='r') cls.camera = camTestUtils.CameraWrapper().camera cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt') if os.path.exists(cls.dbFileName): os.unlink(cls.dbFileName) cls.controlSed = Sed() cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'), 'flatSED', 'sed_flat.txt.gz')) cls.magNorm = 18.1 imsim = Bandpass() imsim.imsimBandpass() ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim) cls.controlSed.multiplyFluxNorm(ff) a_x, b_x = cls.controlSed.setupCCMab() cls.controlSed.addCCMDust(a_x, b_x, A_v=0.1, R_v=3.1) bpd = BandpassDict.loadTotalBandpassesFromFiles() pp = PhotometricParameters() cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp) cls.countSigma = np.sqrt(cls.controlADU/pp.gain) cls.x_pix = 50 cls.y_pix = 50 x_list = [] y_list = [] name_list = [] for dd in cls.camera: x_list.append(cls.x_pix) y_list.append(cls.y_pix) name_list.append(dd.getName()) x_list = np.array(x_list) y_list = np.array(y_list) ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list, camera=cls.camera, obs_metadata=cls.obs, epoch=2000.0) dra_list = 3600.0*(ra_list-cls.obs.pointingRA) ddec_list = 3600.0*(dec_list-cls.obs.pointingDec) create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list, mag_norm=[cls.magNorm]*len(dra_list)) cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
def setUpClass(cls): cls.scratchDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1, mjd=57381.2, rotSkyPos=43.2) cls.camera = camTestUtils.CameraWrapper().camera cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt') if os.path.exists(cls.dbFileName): os.unlink(cls.dbFileName) cls.controlSed = Sed() cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'), 'flatSED','sed_flat.txt.gz')) cls.magNorm = 18.1 imsim = Bandpass() imsim.imsimBandpass() ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim) cls.controlSed.multiplyFluxNorm(ff) a_x, b_x = cls.controlSed.setupCCMab() cls.controlSed.addCCMDust(a_x, b_x, A_v=0.1, R_v=3.1) bpd = BandpassDict.loadTotalBandpassesFromFiles() pp = PhotometricParameters() cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp) cls.countSigma = np.sqrt(cls.controlADU/pp.gain) cls.x_pix = 50 cls.y_pix = 50 x_list = [] y_list = [] name_list = [] for dd in cls.camera: x_list.append(cls.x_pix) y_list.append(cls.y_pix) name_list.append(dd.getName()) x_list = np.array(x_list) y_list = np.array(y_list) ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list, camera=cls.camera, obs_metadata=cls.obs, epoch=2000.0) dra_list = 3600.0*(ra_list-cls.obs.pointingRA) ddec_list = 3600.0*(dec_list-cls.obs.pointingDec) create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list, mag_norm=[cls.magNorm]*len(dra_list)) cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
def _create_sed_library_mags(wav_min, wav_width): """ Calculate the magnitudes of the SEDs in sims_sed_library dir in the tophat filters specified by wav_min, wav_width Parameters ---------- wav_min is a numpy array of the minimum wavelengths of the tophat filters (in nm) wav_width is a numpy array of the widths of the tophat filters (in nm) Returns ------- sed_names is an array containing the names of the SED files sed_mag_list is MxN float array, with M = number of SED files in the library, and N = number of top hat filters in the catalog sed_mag_norm is 1d float array, with length = number of SED files in the library """ wav_max = max((wav0 + width for wav0, width in zip(wav_min, wav_width))) wav_grid = np.arange(wav_min.min(), wav_max, 0.1) bp_name_list = list() bp_list = list() for wav0, width in zip(wav_min, wav_width): sb_grid = ((wav_grid >= wav0) & (wav_grid <= (wav0 + width))).astype(float) bp_list.append(Bandpass(wavelen=wav_grid, sb=sb_grid)) bp_name_list.append('%d_%d' % (wav0, width)) bandpass_dict = BandpassDict(bp_list, bp_name_list) sed_names = list() sed_mag_list = list() sed_mag_norm = list() imsim_bp = Bandpass() imsim_bp.imsimBandpass() for sed_file_name in os.listdir(_galaxy_sed_dir): spec = Sed() spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name)) sed_names.append(defaultSpecMap[sed_file_name]) sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec))) sed_mag_norm.append(spec.calcMag(imsim_bp)) return np.array(sed_names), np.array(sed_mag_list), np.array(sed_mag_norm)
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 _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 = self._getSedCopy(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.setupCCMab() sed.addCCMDust(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.setupCCMab() sed.addCCMDust(a_int, b_int, A_v=gAv, R_v=gRv) return sed
def make_response_func(magnorm=16., filename='starSED/wDs/bergeron_14000_85.dat_14200.gz', savefile='gaia_response.npz', noise=1, count_min=8., bluecut=700., redcut=650): """ Declare some stars as "standards" and build a simple GAIA response function? Multiply GAIA observations by response function to get spectra in flambda units. """ imsimBand = Bandpass() imsimBand.imsimBandpass() sed_dir = getPackageDir('sims_sed_library') filepath = os.path.join(sed_dir, filename) wd = Sed() wd.readSED_flambda(filepath) # Let's just use a flat spectrum wd.setFlatSED() fNorm = wd.calcFluxNorm(magnorm, imsimBand) wd.multiplyFluxNorm(fNorm) red_wd = copy.copy(wd) blue_wd = copy.copy(wd) gaia_obs = SED2GAIA(wd, noise=noise) red_wd.resampleSED(wavelen_match = gaia_obs['RP_wave']) blue_wd.resampleSED(wavelen_match = gaia_obs['BP_wave']) if noise == 1: red_response = red_wd.flambda / gaia_obs['noisySpec'][0]['RPNoisySpec'] blue_response = blue_wd.flambda / gaia_obs['noisySpec'][0]['BPNoisySpec'] too_low = np.where(gaia_obs['noisySpec'][0]['RPNoisySpec'] < count_min) red_response[too_low] = 0 too_low = np.where(gaia_obs['noisySpec'][0]['BPNoisySpec'] < count_min) blue_response[too_low] = 0 elif noise == 0: red_response = red_wd.flambda / gaia_obs['noiseFreeSpec']['RPNoiseFreeSpec'] blue_response = blue_wd.flambda / gaia_obs['noiseFreeSpec']['BPNoiseFreeSpec'] too_low = np.where(gaia_obs['noiseFreeSpec']['RPNoiseFreeSpec'] < count_min) red_response[too_low] = 0 too_low = np.where(gaia_obs['noiseFreeSpec']['BPNoiseFreeSpec'] < count_min) blue_response[too_low] = 0 blue_response[np.where(gaia_obs['BP_wave'] > bluecut)] = 0. red_response[np.where(gaia_obs['RP_wave'] < redcut)] = 0. # XXX check the mags of the original WD and the blue and red WD. np.savez(savefile, red_response=red_response, blue_response=blue_response, red_wavelen=gaia_obs['RP_wave'], blue_wavelen=gaia_obs['BP_wave'])
def setUp(self): starName = os.path.join(lsst.utils.getPackageDir('sims_photUtils'), 'tests/cartoonSedTestData/starSed/') starName = os.path.join(starName, 'kurucz', 'km20_5750.fits_g40_5790.gz') self.starSED = Sed() self.starSED.readSED_flambda(starName) imsimband = Bandpass() imsimband.imsimBandpass() fNorm = self.starSED.calcFluxNorm(22.0, imsimband) self.starSED.multiplyFluxNorm(fNorm) hardwareDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'baseline') componentList = [ 'detector.dat', 'm1.dat', 'm2.dat', 'm3.dat', 'lens1.dat', 'lens2.dat', 'lens3.dat' ] self.skySed = Sed() self.skySed.readSED_flambda(os.path.join(hardwareDir, 'darksky.dat')) totalNameList = [ 'total_u.dat', 'total_g.dat', 'total_r.dat', 'total_i.dat', 'total_z.dat', 'total_y.dat' ] self.bpList = [] self.hardwareList = [] for name in totalNameList: dummy = Bandpass() dummy.readThroughput(os.path.join(hardwareDir, name)) self.bpList.append(dummy) dummy = Bandpass() hardwareNameList = [os.path.join(hardwareDir, name)] for component in componentList: hardwareNameList.append(os.path.join(hardwareDir, component)) dummy.readThroughputList(hardwareNameList) self.hardwareList.append(dummy) self.filterNameList = ['u', 'g', 'r', 'i', 'z', 'y']
def getImsimFluxNorm(sed, magmatch): """ Calculate the flux normalization of an SED in the imsim bandpass. Parameters: ----------- sed is the SED to be normalized magmatch is the desired magnitude in the imsim bandpass Output ------ The factor by which the flux of sed needs to be multiplied to achieve the desired magnitude. """ # This method works based on the assumption that the imsim bandpass # is a delta function. If that ever ceases to be true, the unit test # testSedUtils.py, which checks that the results of this method are # identical to calling Sed.calcFluxNorm and passing in the imsim bandpass, # will fail and we will know to modify this method. if not hasattr(getImsimFluxNorm, 'imsim_wavelen'): bp = Bandpass() bp.imsimBandpass() non_zero_dex = np.where(bp.sb > 0.0)[0][0] getImsimFluxNorm.imsim_wavelen = bp.wavelen[non_zero_dex] if sed.fnu is None: sed.flambdaTofnu() if (getImsimFluxNorm.imsim_wavelen < sed.wavelen.min() or getImsimFluxNorm.imsim_wavelen > sed.wavelen.max()): raise RuntimeError("Cannot normalize sed " "at wavelength of %e nm\n" % getImsimFluxNorm.imsim_wavelen + "The SED does not cover that wavelength\n" + "(Covers %e < lambda %e)" % (sed.wavelen.min(), sed.wavelen.max())) mag = -2.5*np.log10(np.interp(getImsimFluxNorm.imsim_wavelen, sed.wavelen, sed.fnu)) - sed.zp dmag = magmatch - mag return np.power(10, (-0.4*dmag))
def _create_library_one_sed(_galaxy_sed_dir, sed_file_name_list, av_grid, rv_grid, bandpass_dict, out_dict): n_obj = len(av_grid) * len(rv_grid) imsim_bp = Bandpass() imsim_bp.imsimBandpass() t_start = time.time() for i_sed, sed_file_name in enumerate(sed_file_name_list): if i_sed > 0 and i_sed % 10 == 0: duration = (time.time() - t_start) / 3600.0 pred = len(sed_file_name_list) * duration / i_sed print('%d of %d; dur %.2e pred %.2e' % (i_sed, len(sed_file_name_list), duration, pred)) base_spec = Sed() base_spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name)) ax, bx = base_spec.setupCCMab() 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 = [] i_obj = 0 for av in av_grid: for rv in rv_grid: spec = Sed(wavelen=base_spec.wavelen, flambda=base_spec.flambda) spec.addCCMDust(ax, bx, A_v=av, R_v=rv) av_out_list[i_obj] = av rv_out_list[i_obj] = rv sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec))) i_obj += 1 out_dict[sed_file_name] = (sed_names, sed_mag_norm, sed_mag_list, av_out_list, rv_out_list)
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.setupCCMab() starSed.addCCMDust(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 calcADUwrapper(sedName=None, magNorm=None, redshift=None, internalAv=None, internalRv=None, galacticAv=None, galacticRv=None, bandpass=None): 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.setupCCMab() sed.addCCMDust(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.setupCCMab() sed.addCCMDust(a_int, b_int, A_v=galacticAv, R_v=galacticRv) adu = sed.calcADU(bandpass, photParams=PhotometricParameters()) return adu
def setUp(self): starName = os.path.join(lsst.utils.getPackageDir('sims_photUtils'), 'tests/cartoonSedTestData/starSed/') starName = os.path.join(starName, 'kurucz', 'km20_5750.fits_g40_5790.gz') self.starSED = Sed() self.starSED.readSED_flambda(starName) imsimband = Bandpass() imsimband.imsimBandpass() fNorm = self.starSED.calcFluxNorm(22.0, imsimband) self.starSED.multiplyFluxNorm(fNorm) hardwareDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'baseline') componentList = ['detector.dat', 'm1.dat', 'm2.dat', 'm3.dat', 'lens1.dat', 'lens2.dat', 'lens3.dat'] self.skySed = Sed() self.skySed.readSED_flambda(os.path.join(hardwareDir, 'darksky.dat')) totalNameList = ['total_u.dat', 'total_g.dat', 'total_r.dat', 'total_i.dat', 'total_z.dat', 'total_y.dat'] self.bpList = [] self.hardwareList = [] for name in totalNameList: dummy = Bandpass() dummy.readThroughput(os.path.join(hardwareDir, name)) self.bpList.append(dummy) dummy = Bandpass() hardwareNameList = [os.path.join(hardwareDir, name)] for component in componentList: hardwareNameList.append(os.path.join(hardwareDir, component)) dummy.readThroughputList(hardwareNameList) self.hardwareList.append(dummy) self.filterNameList = ['u', 'g', 'r', 'i', 'z', 'y']
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 = numpy.random.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = numpy.random.random_sample(nSed)*5.0 galacticAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1 wavelen_match = numpy.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, internalAvList=internalAvList_0, \ redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) sedNameList_1 = self.getListOfSedNames(nSed) magNormList_1 = list(numpy.random.random_sample(nSed)*5.0 + 15.0) internalAvList_1 = list(numpy.random.random_sample(nSed)*0.3 + 0.1) redshiftList_1 = list(numpy.random.random_sample(nSed)*5.0) galacticAvList_1 = list(numpy.random.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) numpy.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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.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.setupCCMab() sedControl.addCCMDust(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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix+nSed] numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
class PhotometrySSM(PhotometryBase): """ A mixin to calculate photometry for solar system objects. """ # Because solar system objects will not have dust extinctions, we should be able to read in every # SED exactly once, calculate the colors and magnitudes, and then get actual magnitudes by adding # an offset based on magNorm. def _quiescentMagnitudeGetter(self, bandpassDict, columnNameList, bandpassTag='lsst'): """ Method that actually does the work calculating magnitudes for solar system objects. Because solar system objects have no dust extinction, this method works by loading each unique Sed once, normalizing it, calculating its magnitudes in the desired bandpasses, and then storing the normalizing magnitudes and the bandpass magnitudes in a dict. Magnitudes for subsequent objects with identical Seds will be calculated by adding an offset to the magnitudes. The offset is determined by comparing normalizing magnitues. @param [in] bandpassDict is an instantiation of BandpassDict representing the bandpasses to be integrated over @param [in] columnNameList is a list of the names of the columns being calculated by this getter @param [in] bandpassTag (optional) is a string indicating the name of the bandpass system (i.e. 'lsst', 'sdss', etc.). This is in case the user wants to calculate the magnitudes in multiple systems simultaneously. In that case, the dict will store magnitudes for each Sed in each magnitude system separately. @param [out] a numpy array of magnitudes corresponding to bandpassDict. """ # figure out which of these columns we are actually calculating indices = [ii for ii, name in enumerate(columnNameList) if name in self._actually_calculated_columns] if len(indices) == len(columnNameList): indices = None if not hasattr(self, '_ssmMagDict'): self._ssmMagDict = {} self._ssmMagNormDict = {} self._file_dir = getPackageDir('sims_sed_library') self._spec_map = defaultSpecMap self._normalizing_bandpass = Bandpass() self._normalizing_bandpass.imsimBandpass() sedNameList = self.column_by_name('sedFilename') magNormList = self.column_by_name('magNorm') if len(sedNameList)==0: # need to return something when InstanceCatalog goes through # it's "dry run" to determine what columns are required from # the database return np.zeros((len(bandpassDict.keys()),0)) magListOut = [] for sedName, magNorm in zip(sedNameList, magNormList): magTag = bandpassTag+'_'+sedName if sedName not in self._ssmMagNormDict or magTag not in self._ssmMagDict: dummySed = Sed() dummySed.readSED_flambda(os.path.join(self._file_dir, self._spec_map[sedName])) fnorm = dummySed.calcFluxNorm(magNorm, self._normalizing_bandpass) dummySed.multiplyFluxNorm(fnorm) magList = bandpassDict.magListForSed(dummySed, indices=indices) self._ssmMagDict[magTag] = magList self._ssmMagNormDict[sedName] = magNorm else: dmag = magNorm - self._ssmMagNormDict[sedName] magList = self._ssmMagDict[magTag] + dmag magListOut.append(magList) return np.array(magListOut).transpose() @compound('lsst_u','lsst_g','lsst_r','lsst_i','lsst_z','lsst_y') def get_lsst_magnitudes(self): """ getter for LSST magnitudes of solar system objects """ if not hasattr(self, 'lsstBandpassDict'): self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() return self._quiescentMagnitudeGetter(self.lsstBandpassDict, self.get_lsst_magnitudes._colnames) def get_magFilter(self): """ Generate the magnitude in the filter of the observation. """ magFilter = 'lsst_' + self.obs_metadata.bandpass return self.column_by_name(magFilter) def get_magSNR(self): """ Calculate the SNR for the observation, given m5 from obs_metadata and the trailing losses. """ magFilter = self.column_by_name('magFilter') bandpass = self.lsstBandpassDict[self.obs_metadata.bandpass] # Get m5 for the visit m5 = self.obs_metadata.m5[self.obs_metadata.bandpass] # Adjust the magnitude of the source for the trailing losses. dmagSNR = self.column_by_name('dmagTrailing') magObj = magFilter - dmagSNR if len(magObj) == 0: snr = [] else: snr, gamma = calcSNR_m5(magObj, bandpass, m5, self.photParams) return snr def get_visibility(self): """ Generate a None/1 flag indicating whether the object was detected or not. Sets the random seed for 'calculateVisibility' using the obs_metadata.obsHistId """ magFilter = self.column_by_name('magFilter') dmagDetect = self.column_by_name('dmagDetection') magObj = magFilter - dmagDetect # Adjusted m5 value, accounting for the fact these are moving objects. mjdSeed = np.int(self.obs_metadata.mjd.TAI * 1000000) % 4294967295 visibility = self.calculateVisibility(magObj, randomSeed=mjdSeed, pre_generate_randoms=True) return visibility @compound('dmagTrailing', 'dmagDetection') def get_ssm_dmag(self): """ This getter will calculate: dmagTrailing: the offset in m5 used to represent the loss in signal to noise resulting from the fact that the object's motion smears out its PSF dmagDetection: the offset in m5 used to represent the shift in detection threshold resulting from the fact that the object's motion smears out its PSF """ if self.obs_metadata.seeing is None: raise RuntimeError("Cannot calculate dmagTraling/dmagDetection. " "Your catalog's ObservationMetaData does not " "specify seeing.") if len(self.obs_metadata.seeing)>1: valueList = list(self.obs_metadata.seeing.values()) for ix in range(1, len(valueList)): if np.abs(valueList[ix]-valueList[0])>0.0001: raise RuntimeError("dmagTrailing/dmagDetection calculation is confused. " "Your catalog's ObservationMetaData contains multiple " "seeing values. Re-create your catalog with only one seeing value.") if not hasattr(self, 'photParams') or self.photParams is None: raise RuntimeError("You cannot calculate dmagTrailing/dmagDetection. " "Your catalog does not have an associated PhotometricParameters " "member variable. It is impossible to know what the exposure time is.") dradt = self.column_by_name('velRa') # in radians per day (actual sky velocity; # i.e., no need to divide by cos(dec)) ddecdt = self.column_by_name('velDec') # in radians per day if len(dradt)==0: return np.zeros((2,0)) a_trail = 0.76 b_trail = 1.16 a_det = 0.42 b_det = 0.00 seeing = self.obs_metadata.seeing[self.obs_metadata.bandpass] # this will be in arcsec texp = self.photParams.nexp*self.photParams.exptime # in seconds velocity = np.sqrt(np.power(np.degrees(dradt),2) + np.power(np.degrees(ddecdt),2)) # in degrees/day x = velocity*texp/(24.0*seeing) xsq = np.power(x,2) dmagTrail = 1.25*np.log10(1.0 + a_trail * xsq/(1.0+b_trail*x)) dmagDetect = 1.25*np.log10(1.0 + a_det * xsq/(1.0 + b_det*x)) return np.array([dmagTrail, dmagDetect])
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 = numpy.random.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = numpy.random.random_sample(nSed)*5.0 galacticAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1 wavelen_match = numpy.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, internalAvList=internalAvList_0, \ redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) self.assertEqual(len(testList), nSed) numpy.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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) testList.flush() sedNameList_1 = self.getListOfSedNames(nSed/2) magNormList_1 = numpy.random.random_sample(nSed/2)*5.0 + 15.0 internalAvList_1 = numpy.random.random_sample(nSed/2)*0.3 + 0.1 redshiftList_1 = numpy.random.random_sample(nSed/2)*5.0 galacticAvList_1 = numpy.random.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) numpy.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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
class PhotometrySSM(PhotometryBase): """ A mixin to calculate photometry for solar system objects. """ # Because solar system objects will not have dust extinctions, we should be able to read in every # SED exactly once, calculate the colors and magnitudes, and then get actual magnitudes by adding # an offset based on magNorm. def _quiescentMagnitudeGetter(self, bandpassDict, columnNameList, bandpassTag='lsst'): """ Method that actually does the work calculating magnitudes for solar system objects. Because solar system objects have no dust extinction, this method works by loading each unique Sed once, normalizing it, calculating its magnitudes in the desired bandpasses, and then storing the normalizing magnitudes and the bandpass magnitudes in a dict. Magnitudes for subsequent objects with identical Seds will be calculated by adding an offset to the magnitudes. The offset is determined by comparing normalizing magnitues. @param [in] bandpassDict is an instantiation of BandpassDict representing the bandpasses to be integrated over @param [in] columnNameList is a list of the names of the columns being calculated by this getter @param [in] bandpassTag (optional) is a string indicating the name of the bandpass system (i.e. 'lsst', 'sdss', etc.). This is in case the user wants to calculate the magnitudes in multiple systems simultaneously. In that case, the dict will store magnitudes for each Sed in each magnitude system separately. @param [out] a numpy array of magnitudes corresponding to bandpassDict. """ # figure out which of these columns we are actually calculating indices = [ii for ii, name in enumerate(columnNameList) if name in self._actually_calculated_columns] if len(indices) == len(columnNameList): indices = None if not hasattr(self, '_ssmMagDict'): self._ssmMagDict = {} self._ssmMagNormDict = {} self._file_dir = getPackageDir('sims_sed_library') self._spec_map = defaultSpecMap self._normalizing_bandpass = Bandpass() self._normalizing_bandpass.imsimBandpass() sedNameList = self.column_by_name('sedFilename') magNormList = self.column_by_name('magNorm') if len(sedNameList)==0: # need to return something when InstanceCatalog goes through # it's "dry run" to determine what columns are required from # the database return numpy.zeros((len(bandpassDict.keys()),0)) magListOut = [] for sedName, magNorm in zip(sedNameList, magNormList): magTag = bandpassTag+'_'+sedName if sedName not in self._ssmMagNormDict or magTag not in self._ssmMagDict: dummySed = Sed() dummySed.readSED_flambda(os.path.join(self._file_dir, self._spec_map[sedName])) fnorm = dummySed.calcFluxNorm(magNorm, self._normalizing_bandpass) dummySed.multiplyFluxNorm(fnorm) magList = bandpassDict.magListForSed(dummySed, indices=indices) self._ssmMagDict[magTag] = magList self._ssmMagNormDict[sedName] = magNorm else: dmag = magNorm - self._ssmMagNormDict[sedName] magList = self._ssmMagDict[magTag] + dmag magListOut.append(magList) return numpy.array(magListOut).transpose() @compound('lsst_u','lsst_g','lsst_r','lsst_i','lsst_z','lsst_y') def get_lsst_magnitudes(self): """ getter for LSST magnitudes of solar system objects """ if not hasattr(self, 'lsstBandpassDict'): self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() return self._quiescentMagnitudeGetter(self.lsstBandpassDict, self.get_lsst_magnitudes._colnames) def get_magFilter(self): """ Generate the magnitude in the filter of the observation. """ magFilter = 'lsst_' + self.obs_metadata.bandpass return self.column_by_name(magFilter) def get_magSNR(self): """ Calculate the SNR for the observation, given m5 from obs_metadata and the trailing losses. """ magFilter = self.column_by_name('magFilter') bandpass = self.lsstBandpassDict[self.obs_metadata.bandpass] # Get m5 for the visit m5 = self.obs_metadata.m5[self.obs_metadata.bandpass] # Adjust the magnitude of the source for the trailing losses. dmagSNR = self.column_by_name('dmagTrailing') magObj = magFilter - dmagSNR if len(magObj) == 0: snr = [] else: snr, gamma = calcSNR_m5(magObj, bandpass, m5, self.photParams) return snr def get_visibility(self): """ Generate a None/1 flag indicating whether the object was detected or not. Sets the random seed for 'calculateVisibility' using the obs_metadata.obsHistId """ magFilter = self.column_by_name('magFilter') dmagDetect = self.column_by_name('dmagDetection') magObj = magFilter - dmagDetect # Adjusted m5 value, accounting for the fact these are moving objects. mjdSeed = numpy.int(self.obs_metadata.mjd.TAI * 1000000) % 4294967295 visibility = self.calculateVisibility(magObj, randomSeed=mjdSeed, pre_generate_randoms=True) return visibility @compound('dmagTrailing', 'dmagDetection') def get_ssm_dmag(self): """ This getter will calculate: dmagTrailing: the offset in m5 used to represent the loss in signal to noise resulting from the fact that the object's motion smears out its PSF dmagDetection: the offset in m5 used to represent the shift in detection threshold resulting from the fact that the object's motion smears out its PSF """ if self.obs_metadata.seeing is None: raise RuntimeError("Cannot calculate dmagTraling/dmagDetection. " "Your catalog's ObservationMetaData does not " "specify seeing.") if len(self.obs_metadata.seeing)>1: valueList = list(self.obs_metadata.seeing.values()) for ix in range(1, len(valueList)): if numpy.abs(valueList[ix]-valueList[0])>0.0001: raise RuntimeError("dmagTrailing/dmagDetection calculation is confused. " "Your catalog's ObservationMetaData contains multiple " "seeing values. Re-create your catalog with only one seeing value.") if not hasattr(self, 'photParams') or self.photParams is None: raise RuntimeError("You cannot calculate dmagTrailing/dmagDetection. " "Your catalog does not have an associated PhotometricParameters " "member variable. It is impossible to know what the exposure time is.") dradt = self.column_by_name('velRa') # in radians per day (actual sky velocity; # i.e., no need to divide by cos(dec)) ddecdt = self.column_by_name('velDec') # in radians per day if len(dradt)==0: return numpy.zeros((2,0)) a_trail = 0.76 b_trail = 1.16 a_det = 0.42 b_det = 0.00 seeing = self.obs_metadata.seeing[self.obs_metadata.bandpass] # this will be in arcsec texp = self.photParams.nexp*self.photParams.exptime # in seconds velocity = numpy.sqrt(numpy.power(numpy.degrees(dradt),2) + numpy.power(numpy.degrees(ddecdt),2)) # in degrees/day x = velocity*texp/(24.0*seeing) xsq = numpy.power(x,2) dmagTrail = 1.25*numpy.log10(1.0 + a_trail * xsq/(1.0+b_trail*x)) dmagDetect = 1.25*numpy.log10(1.0 + a_det * xsq/(1.0 + b_det*x)) return numpy.array([dmagTrail, dmagDetect])
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 = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') 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.setupCCMab() controlSed.addCCMDust(a_int, b_int, A_v=0.1, R_v=3.1) nSamples = 5 numpy.random.seed(42) pointingRaList = numpy.random.random_sample(nSamples)*360.0 pointingDecList = numpy.random.random_sample(nSamples)*180.0 - 90.0 rotSkyPosList = numpy.random.random_sample(nSamples)*360.0 fwhmList = numpy.random.random_sample(nSamples)*1.0 + 0.3 actualCounts = None for pointingRA, pointingDec, rotSkyPos, fwhm in \ zip(pointingRaList, pointingDecList, rotSkyPosList, fwhmList): obs = ObservationMetaData(unrefractedRA=pointingRA, unrefractedDec=pointingDec, boundType='circle', boundLength=4.0, mjd=49250.0, rotSkyPos=rotSkyPos) xDisplacementList = numpy.random.random_sample(nSamples)*60.0-30.0 yDisplacementList = numpy.random.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) if actualCounts is None: actualCounts = controlSed.calcADU(uBandpass, cat.photParams) psf = SNRdocumentPSF(fwhm=fwhm) cat.setPSF(psf) cat.camera = camera 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(numpy.radians(numpy.float(words[2]))) objDecList.append(numpy.radians(numpy.float(words[3]))) objRaList = numpy.array(objRaList) objDecList = numpy.array(objDecList) self.check_placement(imageName, objRaList, objDecList, [fwhm]*len(objRaList), numpy.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)
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)
class lensedSneCat(instCatUtils): def __init__(self, truth_cat, out_dir, cat_file_name, sed_folder_name, write_sn_sed=True): self.truth_cat = truth_cat self.imSimBand = Bandpass() self.imSimBand.imsimBandpass() self.sed_folder_name = sed_folder_name self.out_dir = out_dir self.sed_dir = os.path.join(out_dir, sed_folder_name) self.write_sn_sed = write_sn_sed if not os.path.exists(self.sed_dir): os.mkdir(self.sed_dir) def calc_sne_mags(self, obs_mjd, obs_filter): wavelen_max = 1800. wavelen_min = 30. wavelen_step = 0.1 sn_magnorm_list = [] sn_sed_names = [] add_to_cat_list = [] for idx in range(len(self.truth_cat)): sed_mjd = obs_mjd - self.truth_cat['t_delay'].iloc[idx] current_sn_obj = SNObject(ra=self.truth_cat['ra'].iloc[idx], dec=self.truth_cat['dec'].iloc[idx]) current_sn_obj.set(z=self.truth_cat['redshift'].iloc[idx], t0=self.truth_cat['t0'].iloc[idx], x0=self.truth_cat['x0'].iloc[idx], x1=self.truth_cat['x1'].iloc[idx], c=self.truth_cat['c'].iloc[idx]) # Following follows from # https://github.com/lsst/sims_catUtils/blob/master/python/lsst/sims/catUtils/mixins/sncat.py sn_sed_obj = current_sn_obj.SNObjectSourceSED( time=sed_mjd, wavelen=np.arange(wavelen_min, wavelen_max, wavelen_step)) flux_500 = sn_sed_obj.flambda[np.where( sn_sed_obj.wavelen >= 499.99)][0] if flux_500 > 0.: sn_magnorm = sn_sed_obj.calcMag(bandpass=self.imSimBand) sn_name = None if self.write_sn_sed: sn_name = '%s/specFileGLSN_%s_%s_%.4f.txt' % ( self.sed_folder_name, self.truth_cat['dc2_sys_id'].iloc[idx], self.truth_cat['image_number'].iloc[idx], obs_mjd) sed_filename = '%s/%s' % (self.out_dir, sn_name) sn_sed_obj.writeSED(sed_filename) with open(sed_filename, 'rb') as f_in, gzip.open( str(sed_filename + '.gz'), 'wb') as f_out: shutil.copyfileobj(f_in, f_out) os.remove(sed_filename) sn_magnorm_list.append(sn_magnorm) sn_sed_names.append(sn_name + '.gz') add_to_cat_list.append(idx) return add_to_cat_list, np.array(sn_magnorm_list), sn_sed_names def output_instance_catalog(self, add_to_cat_idx, sne_magnorms, sne_sed_names, obs_md, filename): full_cat_name = os.path.join(self.out_dir, filename) lensed_mags = sne_magnorms - \ 2.5*np.log10(np.abs(self.truth_cat['magnification'].iloc[add_to_cat_idx].values)) phosim_coords = self.get_phosim_coords( np.radians(self.truth_cat['ra'].values), np.radians(self.truth_cat['dec'].values), obs_md) phosim_ra, phosim_dec = np.degrees(phosim_coords) with open(full_cat_name, 'w') as f: for truth_cat_idx, output_idx in zip( add_to_cat_idx, np.arange(len(add_to_cat_idx))): f.write('object %s %f %f %f %s %f 0 0 0 0 0 point none CCM %f %f\n' \ % ('%s_%s' % (self.truth_cat['dc2_sys_id'].iloc[truth_cat_idx], self.truth_cat['image_number'].iloc[truth_cat_idx]), phosim_ra[truth_cat_idx], phosim_dec[truth_cat_idx], lensed_mags[output_idx], sne_sed_names[output_idx], self.truth_cat['redshift'].iloc[truth_cat_idx], self.truth_cat['av_mw'].iloc[truth_cat_idx], self.truth_cat['rv_mw'].iloc[truth_cat_idx])) return
def gen_gums_mag_cat(istart=0, nstars=100, workdir='', noisyResponse=False, verbose=False, save=True): """ generate a catalog of true and observed magnitudes for stars from the gums catalog """ # Load response functions responses = [] response_temperatures = [] response_files = glob.glob('gaia_response_*00.npz') for fn in response_files: responses.append(gaia_response(restore_file=fn)) response_temperatures.append(float(fn.replace('.npz', '').split('_')[-1])) response_temperatures = np.array(response_temperatures) #if noisyResponse: # response = gaia_response() #else: # response = gaia_response(restore_file='gaia_response_nonoise.npz') # Load gums catalog gum_cat = gums_catalog() gum_cat.prune() gum_cat = gum_cat.catalog print 'using %i stars from GAIA' % gum_cat.size sed = Sed() # Load up bandpass imsimBand = Bandpass() imsimBand.imsimBandpass() bps = lsst_filters() bps.update(stubb_fitlers()) names = ['id', 'sourceExtendedId', 'raj2000', 'dej2000', 'u_truncated', 'u', 'g', 'r', 'i', 'z', 'y', 'y_truncated', 'u_truncated_noiseless', 'u_noiseless', 'g_noiseless', 'r_noiseless', 'i_noiseless', 'z_noiseless', 'y_noiseless', 'y_truncated_noiseless', 'u_truncated_true', 'u_true', 'g_true', 'r_true', 'i_true', 'z_true', 'y_true', 'y_truncated_true', 'teff', 'feH', 'logg', 'Av', 'magG'] types = [int, '|S30'] types.extend([float]*(len(names)-1)) result_cat = np.zeros(nstars, dtype=zip(names, types)) copy_keys = ['sourceExtendedId', 'raj2000', 'dej2000'] a_x = None b_x = None maxI = float(nstars) for i, gems_index in enumerate(np.arange(istart, istart+nstars)): result_cat['id'][i] = gems_index for key in copy_keys: result_cat[key][i] = gum_cat[key][gems_index] # Lookup the file with the closest teff, feH, and logg to gum_cat[i] sed, pd = read_close_Kurucz(gum_cat['teff'][gems_index], gum_cat['feH'][gems_index], gum_cat['logg'][gems_index]) result_cat['teff'][i] = gum_cat['teff'][gems_index] result_cat['feH'][i] = gum_cat['feH'][gems_index] result_cat['logg'][i] = gum_cat['logg'][gems_index] result_cat['Av'][i] = gum_cat['Av'][gems_index] result_cat['magG'] = gum_cat['magG'][gems_index] # Let's figure out the GAIA-G to SDSS g conversion. Assume SDSS_g ~ LSST_g # poly fit from https://arxiv.org/pdf/1008.0815.pdf g_mag = sed.calcMag(bps['g']) i_mag = sed.calcMag(bps['i']) g_i = g_mag - i_mag gnorm = gum_cat['magG'][gems_index] + 0.0094 + 0.531*(g_i) + 0.0974*(g_i)**2 - 0.0052*(g_i)**3 if a_x is None: a_x, b_x = sed.setupCCMab() sed.addCCMDust(a_x, b_x, A_v=gum_cat['Av'][gems_index]) # do the magnorm here fNorm = sed.calcFluxNorm(gnorm, bps['g']) sed.multiplyFluxNorm(fNorm) # Observe sed with GAIA, both with and without noise # Wrap in a try block so if ULYSSES fails for some reason the star just gets skipped gaia_observed = SED2GAIA(sed, workdir=workdir) tdiff = np.abs(response_temperatures - result_cat['teff'][i]) tmatch = np.where(tdiff == tdiff.min())[0] observed_sed = ulysses2SED(data=gaia_observed, response=responses[tmatch]) not_nan = ~np.isnan(observed_sed.flambda) # Let's interpolate out any nans observed_sed.flambda = np.interp(observed_sed.wavelen, observed_sed.wavelen[not_nan], observed_sed.flambda[not_nan]) for filtername in bps: try: result_cat[filtername][i] = observed_sed.calcMag(bps[filtername]) except: pass result_cat[filtername+'_true'][i] = sed.calcMag(bps[filtername]) # Now do it on a noiseless spectra gaia_observed = SED2GAIA(sed, workdir=workdir, noise=0) observed_sed = ulysses2SED(data=gaia_observed, response=responses[tmatch], noisy=False) not_nan = ~np.isnan(observed_sed.flambda) # Let's interpolate out any nans observed_sed.flambda = np.interp(observed_sed.wavelen, observed_sed.wavelen[not_nan], observed_sed.flambda[not_nan]) for filtername in bps: try: result_cat[filtername+'_noiseless'][i] = observed_sed.calcMag(bps[filtername]) except: pass if verbose: progress = i/maxI*100 text = "\rprogress = %.1f%%"%progress sys.stdout.write(text) sys.stdout.flush() # Try to catch a failure example: #if (result_cat['g'][i] < 18.) & (result_cat['g'][i] > 0.): # if (np.abs(result_cat['g'][i]-result_cat['g_true'][i]) > 0.75) | (np.abs(result_cat['r'][i]-result_cat['r_true'][i]) > 0.75): # import matplotlib.pylab as plt # import pdb ; pdb.set_trace() print '' if save: np.savez('%i_%i_gum_mag_cat.npz' % (istart, nstars+istart), result_cat=result_cat) else: return result_cat, sed, observed_sed
('y', float)]) # read in and normalize the SED sed_name = os.path.join(phosim_dir, 'data', 'SEDs', 'flatSED', 'sed_flat_short.txt.gz') np_sed = np.genfromtxt(sed_name, dtype=sed_dtype) np_fnorm = get_sed_normalization(21.0, np_sed['wav_nm'], np_sed['flambda']) np_sed['flambda'] *= np_fnorm if _LSST_STACK_INSTALLED: phot_params = PhotometricParameters(nexp=1, exptime=30.0) spec = Sed() spec.readSED_flambda(sed_name) imsim_bp = Bandpass() imsim_bp.imsimBandpass() fnorm = spec.calcFluxNorm(21.0, imsim_bp) spec.multiplyFluxNorm(fnorm) # read in the throughputs for the mirrors, lenses, detector and atmosphere componentList = [m1_file, m2_file, m3_file, l1_file, l2_file, l3_file] if det_file.lower() != 'none': print('adding detector') componentList.append(det_file) if atmos_file.lower() != 'none': print('adding astmosphere') componentList.append(atmos_file) np_component_list = [] for file_name in componentList: print('multiplying %s' % file_name)
import numpy import gzip import eups from lsst.sims.photUtils import Bandpass, Sed, PhotometryBase lsstDir = os.path.join(eups.productDir('sims_sed_library'),'starSED','gizis_SED') burrowsDir = '/Users/danielsf/physics/burrowsBrownDwarfs' btSettlDir = '/Users/danielsf/physics/newSedLibrary/starSED/mlt/' sdssPhotometry = PhotometryBase() sdssPhotometry.loadTotalBandpassesFromFiles(bandpassNames = ['u','g','r','i','z'], bandpassDir = os.path.join(eups.productDir('throughputs'),'sdss'), bandpassRoot = 'sdss_') controlBandpass = Bandpass() controlBandpass.imsimBandpass() linesDict = {} with open('linesLookup.txt', 'r') as linesInput: for line in linesInput: vv = line.split() if vv[0] =='>': linesDict[vv[1]] = numpy.float(vv[2]) listOfSubDirs = os.listdir(burrowsDir) dtype = numpy.dtype([ ('id', numpy.int), ('freq', numpy.float), ('wavelen', numpy.float), ('fnu', numpy.float),
def get_TotalMags(result, bandpasses=('u','g','r','i','z','y')): datadir = os.environ.get("SIMS_SED_LIBRARY_DIR") tpath = os.getenv('LSST_THROUGHPUTS_DEFAULT') bands = {"u":None, "g":None, "r":None, "i":None, "z":None, "y":None} for k in bands: bands[k] = Bandpass() bands[k].readThroughput(os.path.join(tpath, "total_%s.dat"%k)) # Set up phi, the wavelength-normalized system response for each filter, # for each bandpass for manyMagCalc method. bplist = [] for f in ['u','g','r','i','z','y']: bands[f].sbTophi() bplist.append(bands[f]) ids = result['galid'] diskfile = result['sedFilenameDisk'] bulgefile = result['sedFilenameBulge'] agnfile = result['sedFilenameAgn'] diskmn = result['magNormDisk'] bulgemn = result['magNormBulge'] agnmn = result['magNormAgn'] bulgeAv = result['internalAvBulge'] diskAv = result['internalAvDisk'] redshift = result['redshift'] imsimband = Bandpass() imsimband.imsimBandpass() sedDict = {} retMags = dict([(k, []) for k in bands]) a_int = None b_int = None tmpwavelen = None for id, df, dm, dav, bf, bm, bav, af, am, z in zip(ids, diskfile, diskmn, diskAv, bulgefile, bulgemn, bulgeAv, agnfile, agnmn, redshift): tmpflux = None for comp in ((df, dm, dav, 'galaxySED', False), (bf, bm, bav, 'galaxySED', False), (af, am, None, 'agnSED', True)): #Zero out the AGN contribution #for comp in ((df, dm, dav, 'galaxySED', False), (bf, bm, bav, 'galaxySED', False), (af, 99.99, None, 'agnSED', True)): if not comp[0] == u'None': if sedDict.has_key(comp[0]): sed = copy.deepcopy(sedDict[comp[0]]) else: sed = Sed() print os.path.join(datadir,comp[3],comp[0]) sed.readSED_flambda(os.path.join(datadir,comp[3],comp[0])) if comp[4]: sed.resampleSED(wavelen_match=tmpwavelen) sedDict[comp[0]] = sed if a_int is None: phiarray, dlambda = sed.setupPhiArray(bplist) a_int, b_int = sed.setupCCMab() #Careful, this assumes that a disk or bulge sed is read #before any agn sed tmpwavelen = sed.wavelen fNorm = sed.calcFluxNorm(comp[1], imsimband) sed.multiplyFluxNorm(fNorm) #I guess this assumes rv=3.1?? if comp[2]: sed.addCCMDust(a_int, b_int, A_v=comp[2]) wavelenArr=sed.wavelen if tmpflux is None: tmpflux = sed.flambda else: tmpflux += sed.flambda newgal = Sed(wavelen=wavelenArr, flambda=tmpflux) #a_mw, b_mw = sed.setupCCMab() #sed.addCCMDust(a_mw, b_mw, A_v=mwav) newgal.redshiftSED(z, dimming=True) newgal.resampleSED(wavelen_match=bplist[0].wavelen) newgal.flambdaTofnu() mags = newgal.manyMagCalc(phiarray, dlambda) for i,k in enumerate(['u','g','r','i','z','y']): retMags[k].append(mags[i]) return retMags
class CartoonUncertaintyTestCase(unittest.TestCase): """ This unit test suite will verify that our 'arbitrarily extensible' framework for writing magnitude uncertainty getters actually behaves as advertised """ def setUp(self): self.normband = Bandpass() self.normband.imsimBandpass() self.uband = Bandpass() self.uband.readThroughput(os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData', 'test_bandpass_u.dat')) self.gband = Bandpass() self.gband.readThroughput(os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData', 'test_bandpass_g.dat')) 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') catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'cartoonStarCat.txt') cat = CartoonStars(db, obs_metadata=obs) cat.write_catalog(catName) dtype = np.dtype([(name, np.float) for name in cat.column_outputs]) controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',') if os.path.exists(catName): os.unlink(catName) 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.setupCCMab() spectrum.addCCMDust(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_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') catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'cartoonStarCat.txt') cat = CartoonStars(db, obs_metadata=obs, column_outputs=['lsst_u', 'lsst_g', 'sigma_lsst_u', 'sigma_lsst_g']) cat.write_catalog(catName) dtype = np.dtype([(name, np.float) for name in cat._column_outputs]) controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',') if os.path.exists(catName): os.unlink(catName) 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.setupCCMab() spectrum.addCCMDust(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)
import lsst.sims.photUtils.Sed as Sed import lsst.sims.photUtils.Bandpass as Bandpass from gaia_spec import * import copy # let's look at the magnitude of things response = gaia_response(restore_file='gaia_response.npz') # response = gaia_response(restore_file='gaia_response_nonoise.npz') filename = 'starSED/wDs/bergeron_14000_85.dat_14200.gz' imsimBand = Bandpass() imsimBand.imsimBandpass() sed_dir = getPackageDir('sims_sed_library') filepath = os.path.join(sed_dir, filename) wd = Sed() wd.readSED_flambda(filepath) magnorm = 16 fNorm = wd.calcFluxNorm(magnorm, imsimBand) wd.multiplyFluxNorm(fNorm) throughPath = os.path.join(getPackageDir('throughputs'), 'baseline') lsstKeys = ['u', 'g', 'r', 'i', 'z', 'y'] # lsstKeys = ['r'] bps = {} for key in lsstKeys: bp = np.loadtxt(os.path.join(throughPath, 'total_'+key+'.dat'),
def testGalaxyPhotometricUncertainties(self): """ Test in the case of a catalog of galaxies """ lsstDefaults = LSSTdefaults() phot = PhotometryGalaxies() 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(lsst.utils.getPackageDir('sims_sed_library'), defaultSpecMap[bulgeSedName])) fNorm=bulgeSed.calcFluxNorm(magNormBulge, imsimband) bulgeSed.multiplyFluxNorm(fNorm) diskSed = Sed() diskSed.readSED_flambda(os.path.join(lsst.utils.getPackageDir('sims_sed_library'), defaultSpecMap[diskSedName])) fNorm = diskSed.calcFluxNorm(magNormDisk, imsimband) diskSed.multiplyFluxNorm(fNorm) agnSed = Sed() agnSed.readSED_flambda(os.path.join(lsst.utils.getPackageDir('sims_sed_library'), defaultSpecMap[agnSedName])) fNorm = agnSed.calcFluxNorm(magNormAgn, imsimband) agnSed.multiplyFluxNorm(fNorm) a_int, b_int = bulgeSed.setupCCMab() bulgeSed.addCCMDust(a_int, b_int, A_v=avBulge) a_int, b_int = diskSed.setupCCMab() diskSed.addCCMDust(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) numpy.testing.assert_almost_equal(bulgeSed.wavelen, diskSed.wavelen) numpy.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)
obs_root.site = Site(name='LSST', pressure=0.0, humidity=0.0) phosim_header = DefaultPhoSimHeaderMap phosim_header['nsnap'] = 1 phosim_header['vistime'] = 30.0 galaxy_dir = os.path.join(getPackageDir('sims_sed_library'), 'galaxySED') galaxy_sed_list = os.listdir(galaxy_dir) magnorm_interp = {} from lsst.sims.photUtils import PhotometricParameters, Bandpass phot_params = PhotometricParameters(nexp=1, exptime=30.0, gain=1) imsim = Bandpass() imsim.imsimBandpass() target=20000.0 for name in galaxy_sed_list: magnorm_interp[name] = {} magnorm_interp[name]['z'] = np.arange(0.0, 2.4, 0.2) magnorm_interp[name]['mag'] = np.zeros(len(magnorm_interp[name]['z']), dtype=float) for i_zz, zz in enumerate(magnorm_interp[name]['z']): ss = Sed() ss.readSED_flambda(os.path.join(galaxy_dir, name)) mag = ss.calcMag(imsim) ss.redshiftSED(zz, dimming=True) cts = ss.calcADU(bp_dict['r'], photParams=phot_params) magnorm = mag -2.5*np.log10(target/cts) ss = Sed()
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 = numpy.random.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = numpy.random.random_sample(nSed)*5.0 galacticAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1 wavelen_match = numpy.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, 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, internalAvList=internalAvList_0, \ redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) sedNameList_1 = self.getListOfSedNames(nSed) magNormList_1 = numpy.random.random_sample(nSed)*5.0 + 15.0 if addIav: internalAvList_1 = numpy.random.random_sample(nSed)*0.3 + 0.1 else: internalAvList_1 = None if addRedshift: redshiftList_1 = numpy.random.random_sample(nSed)*5.0 else: redshiftList_1 = None if addGav: galacticAvList_1 = numpy.random.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) numpy.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.assertTrue(testList.internalAvList[ix+nSed] is None) if addGav: self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix+nSed], 10) else: self.assertTrue(testList.galacticAvList[ix+nSed] is None) if addRedshift: self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix+nSed], 10) else: self.assertTrue(testList.redshiftList[ix+nSed] is None) 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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.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.setupCCMab() sedControl.addCCMDust(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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix+nSed] numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
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.setupCCMab() controlSed.addCCMDust(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 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)
class sprinkler(): def __init__(self, catsim_cat, visit_mjd, specFileMap, sed_path, om10_cat='twinkles_lenses_v2.fits', sne_cat='dc2_sne_cat.csv', density_param=1., cached_sprinkling=False, agn_cache_file=None, sne_cache_file=None, defs_file=None): """ Parameters ---------- catsim_cat: catsim catalog The results array from an instance catalog. visit_mjd: float The mjd of the visit specFileMap: This will tell the instance catalog where to write the files om10_cat: optional, defaults to 'twinkles_lenses_v2.fits fits file with OM10 catalog sne_cat: optional, defaults to 'dc2_sne_cat.csv' density_param: `np.float`, optioanl, defaults to 1.0 the fraction of eligible agn objects that become lensed and should be between 0.0 and 1.0. cached_sprinkling: boolean If true then pick from a preselected list of galtileids agn_cache_file: str sne_cache_file: str defs_file: str Returns ------- updated_catalog: A new results array with lens systems added. """ twinklesDir = getPackageDir('Twinkles') om10_cat = os.path.join(twinklesDir, 'data', om10_cat) self.catalog = catsim_cat # ****** THIS ASSUMES THAT THE ENVIRONMENT VARIABLE OM10_DIR IS SET ******* lensdb = om10.DB(catalog=om10_cat, vb=False) self.lenscat = lensdb.lenses.copy() self.density_param = density_param self.bandpassDict = BandpassDict.loadTotalBandpassesFromFiles( bandpassNames=['i']) self.sne_catalog = pd.read_csv( os.path.join(twinklesDir, 'data', sne_cat)) #self.sne_catalog = self.sne_catalog.iloc[:101] ### Remove this after testing self.used_systems = [] self.visit_mjd = visit_mjd self.sn_obj = SNObject(0., 0.) self.write_dir = specFileMap.subdir_map['(^specFileGLSN)'] self.sed_path = sed_path self.cached_sprinkling = cached_sprinkling if self.cached_sprinkling is True: if ((agn_cache_file is None) | (sne_cache_file is None)): raise AttributeError( 'Must specify cache files if using cached_sprinkling.') #agn_cache_file = os.path.join(twinklesDir, 'data', 'test_agn_galtile_cache.csv') self.agn_cache = pd.read_csv(agn_cache_file) #sne_cache_file = os.path.join(twinklesDir, 'data', 'test_sne_galtile_cache.csv') self.sne_cache = pd.read_csv(sne_cache_file) else: self.agn_cache = None self.sne_cache = None if defs_file is None: self.defs_file = os.path.join(twinklesDir, 'data', 'catsim_defs.csv') else: self.defs_file = defs_file specFileStart = 'Burst' for key, val in sorted(iteritems(SpecMap.subdir_map)): if re.match(key, specFileStart): galSpecDir = str(val) self.galDir = str( getPackageDir('sims_sed_library') + '/' + galSpecDir + '/') self.imSimBand = Bandpass() self.imSimBand.imsimBandpass() #self.LRG_name = 'Burst.25E09.1Z.spec' #self.LRG = Sed() #self.LRG.readSED_flambda(str(galDir + self.LRG_name)) #return #Calculate imsimband magnitudes of source galaxies for matching agn_fname = str( getPackageDir('sims_sed_library') + '/agnSED/agn.spec.gz') src_iband = self.lenscat['MAGI_IN'] src_z = self.lenscat['ZSRC'] self.src_mag_norm = [] for src, s_z in zip(src_iband, src_z): agn_sed = Sed() agn_sed.readSED_flambda(agn_fname) agn_sed.redshiftSED(s_z, dimming=True) self.src_mag_norm.append(matchBase().calcMagNorm( [src], agn_sed, self.bandpassDict)) #self.src_mag_norm = matchBase().calcMagNorm(src_iband, # [agn_sed]*len(src_iband), # # self.bandpassDict) self.defs_dict = {} with open(self.defs_file, 'r') as f: for line in f: line_defs = line.split(',') if len(line_defs) > 1: self.defs_dict[line_defs[0]] = line_defs[1].split('\n')[0] def sprinkle(self): # Define a list that we can write out to a text file lenslines = [] # For each galaxy in the catsim catalog updated_catalog = self.catalog.copy() # print("Running sprinkler. Catalog Length: ", len(self.catalog)) for rowNum, row in enumerate(self.catalog): # if rowNum == 100 or rowNum % 100000==0: # print("Gone through ", rowNum, " lines of catalog.") if not np.isnan(row[self.defs_dict['galaxyAgn_magNorm']]): candidates = self.find_lens_candidates( row[self.defs_dict['galaxyAgn_redshift']], row[self.defs_dict['galaxyAgn_magNorm']]) #varString = json.loads(row[self.defs_dict['galaxyAgn_varParamStr']]) # varString[self.defs_dict['pars']]['t0_mjd'] = 59300.0 #row[self.defs_dict['galaxyAgn_varParamStr']] = json.dumps(varString) np.random.seed(row[self.defs_dict['galtileid']] % (2 ^ 32 - 1)) pick_value = np.random.uniform() # If there aren't any lensed sources at this redshift from OM10 move on the next object if (((len(candidates) > 0) and (pick_value <= self.density_param) and (self.cached_sprinkling is False)) | ((self.cached_sprinkling is True) and (row[self.defs_dict['galtileid']] in self.agn_cache['galtileid'].values))): # Randomly choose one the lens systems # (can decide with or without replacement) # Sort first to make sure the same choice is made every time if self.cached_sprinkling is True: twinkles_sys_cache = self.agn_cache.query( 'galtileid == %i' % row[self.defs_dict['galtileid']] )['twinkles_system'].values[0] newlens = self.lenscat[np.where( self.lenscat['twinklesId'] == twinkles_sys_cache) [0]][0] else: candidates = candidates[np.argsort( candidates['twinklesId'])] newlens = np.random.choice(candidates) # Append the lens galaxy # For each image, append the lens images for i in range(newlens['NIMG']): lensrow = row.copy() # XIMG and YIMG are in arcseconds # raPhSim and decPhoSim are in radians #Shift all parts of the lensed object, not just its agn part for lensPart in [ 'galaxyBulge', 'galaxyDisk', 'galaxyAgn' ]: lens_ra = lensrow[self.defs_dict[str(lensPart + '_raJ2000')]] lens_dec = lensrow[self.defs_dict[str( lensPart + '_decJ2000')]] delta_ra = np.radians( newlens['XIMG'][i] / 3600.0) / np.cos(lens_dec) delta_dec = np.radians(newlens['YIMG'][i] / 3600.0) lensrow[self.defs_dict[str( lensPart + '_raJ2000')]] = lens_ra + delta_ra lensrow[self.defs_dict[ str(lensPart + '_decJ2000')]] = lens_dec + delta_dec mag_adjust = 2.5 * np.log10(np.abs(newlens['MAG'][i])) lensrow[ self.defs_dict['galaxyAgn_magNorm']] -= mag_adjust varString = json.loads( lensrow[self.defs_dict['galaxyAgn_varParamStr']]) varString[self.defs_dict['pars']]['t0Delay'] = newlens[ 'DELAY'][i] varString[self.defs_dict[ 'varMethodName']] = 'applyAgnTimeDelay' lensrow[self.defs_dict[ 'galaxyAgn_varParamStr']] = json.dumps(varString) lensrow[self.defs_dict['galaxyDisk_majorAxis']] = 0.0 lensrow[self.defs_dict['galaxyDisk_minorAxis']] = 0.0 lensrow[ self.defs_dict['galaxyDisk_positionAngle']] = 0.0 lensrow[self.defs_dict['galaxyDisk_internalAv']] = 0.0 lensrow[self.defs_dict[ 'galaxyDisk_magNorm']] = 999. #np.nan To be fixed post run1.1 lensrow[ self.defs_dict['galaxyDisk_sedFilename']] = None lensrow[self.defs_dict['galaxyBulge_majorAxis']] = 0.0 lensrow[self.defs_dict['galaxyBulge_minorAxis']] = 0.0 lensrow[ self.defs_dict['galaxyBulge_positionAngle']] = 0.0 lensrow[self.defs_dict['galaxyBulge_internalAv']] = 0.0 lensrow[self.defs_dict[ 'galaxyBulge_magNorm']] = 999. #np.nan To be fixed post run1.1 lensrow[ self.defs_dict['galaxyBulge_sedFilename']] = None lensrow[self.defs_dict[ 'galaxyBulge_redshift']] = newlens['ZSRC'] lensrow[self.defs_dict[ 'galaxyDisk_redshift']] = newlens['ZSRC'] lensrow[self.defs_dict[ 'galaxyAgn_redshift']] = newlens['ZSRC'] #To get back twinklesID in lens catalog from phosim catalog id number #just use np.right_shift(phosimID-28, 10). Take the floor of the last #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is #the image number minus 1. lensrow[self.defs_dict['galtileid']] = ( lensrow[self.defs_dict['galtileid']] * 10000 + newlens['twinklesId'] * 4 + i) updated_catalog = np.append(updated_catalog, lensrow) #Now manipulate original entry to be the lens galaxy with desired properties #Start by deleting Disk and AGN properties if not np.isnan(row[self.defs_dict['galaxyDisk_magNorm']]): row[self.defs_dict['galaxyDisk_majorAxis']] = 0.0 row[self.defs_dict['galaxyDisk_minorAxis']] = 0.0 row[self.defs_dict['galaxyDisk_positionAngle']] = 0.0 row[self.defs_dict['galaxyDisk_internalAv']] = 0.0 row[self.defs_dict[ 'galaxyDisk_magNorm']] = 999. #np.nan To be fixed post run1.1 row[self.defs_dict['galaxyDisk_sedFilename']] = None row[self.defs_dict[ 'galaxyAgn_magNorm']] = None #np.nan To be fixed post run1.1 row[self.defs_dict[ 'galaxyDisk_magNorm']] = 999. # To be fixed in run1.1 row[self.defs_dict['galaxyAgn_sedFilename']] = None #Now insert desired Bulge properties row[self.defs_dict['galaxyBulge_sedFilename']] = newlens[ 'lens_sed'] row[self. defs_dict['galaxyBulge_redshift']] = newlens['ZLENS'] row[self. defs_dict['galaxyDisk_redshift']] = newlens['ZLENS'] row[self. defs_dict['galaxyAgn_redshift']] = newlens['ZLENS'] row_lens_sed = Sed() row_lens_sed.readSED_flambda( str(self.galDir + newlens['lens_sed'])) row_lens_sed.redshiftSED(newlens['ZLENS'], dimming=True) row[self.defs_dict['galaxyBulge_magNorm']] = matchBase( ).calcMagNorm( [newlens['APMAG_I']], row_lens_sed, self.bandpassDict) #Changed from i band to imsimband row[self.defs_dict[ 'galaxyBulge_majorAxis']] = radiansFromArcsec( newlens['REFF'] / np.sqrt(1 - newlens['ELLIP'])) row[self.defs_dict[ 'galaxyBulge_minorAxis']] = radiansFromArcsec( newlens['REFF'] * np.sqrt(1 - newlens['ELLIP'])) #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians row[self.defs_dict['galaxyBulge_positionAngle']] = newlens[ 'PHIE'] * (-1.0) * np.pi / 180.0 #Replace original entry with new entry updated_catalog[rowNum] = row else: if self.cached_sprinkling is True: if row[self.defs_dict['galtileid']] in self.sne_cache[ 'galtileid'].values: use_system = self.sne_cache.query( 'galtileid == %i' % row[self.defs_dict['galtileid']] )['twinkles_system'].values use_df = self.sne_catalog.query( 'twinkles_sysno == %i' % use_system) self.used_systems.append(use_system) else: continue else: lens_sne_candidates = self.find_sne_lens_candidates( row[self.defs_dict['galaxyDisk_redshift']]) candidate_sysno = np.unique( lens_sne_candidates['twinkles_sysno']) num_candidates = len(candidate_sysno) if num_candidates == 0: continue used_already = np.array([ sys_num in self.used_systems for sys_num in candidate_sysno ]) unused_sysno = candidate_sysno[~used_already] if len(unused_sysno) == 0: continue np.random.seed(row[self.defs_dict['galtileid']] % (2 ^ 32 - 1)) use_system = np.random.choice(unused_sysno) use_df = self.sne_catalog.query('twinkles_sysno == %i' % use_system) for i in range(len(use_df)): lensrow = row.copy() for lensPart in ['galaxyBulge', 'galaxyDisk', 'galaxyAgn']: lens_ra = lensrow[self.defs_dict[str(lensPart + '_raJ2000')]] lens_dec = lensrow[self.defs_dict[str(lensPart + '_decJ2000')]] delta_ra = np.radians( use_df['x'].iloc[i] / 3600.0) / np.cos(lens_dec) delta_dec = np.radians(use_df['y'].iloc[i] / 3600.0) lensrow[self.defs_dict[str( lensPart + '_raJ2000')]] = lens_ra + delta_ra lensrow[self.defs_dict[str( lensPart + '_decJ2000')]] = lens_dec + delta_dec # varString = json.loads(lensrow[self.defs_dict['galaxyAgn_varParamStr']]) varString = 'None' lensrow[ self.defs_dict['galaxyAgn_varParamStr']] = varString lensrow[self.defs_dict['galaxyDisk_majorAxis']] = 0.0 lensrow[self.defs_dict['galaxyDisk_minorAxis']] = 0.0 lensrow[self.defs_dict['galaxyDisk_positionAngle']] = 0.0 lensrow[self.defs_dict['galaxyDisk_internalAv']] = 0.0 lensrow[self.defs_dict[ 'galaxyDisk_magNorm']] = 999. #np.nan To be fixed post run1.1 lensrow[self.defs_dict['galaxyDisk_sedFilename']] = None lensrow[self.defs_dict['galaxyBulge_majorAxis']] = 0.0 lensrow[self.defs_dict['galaxyBulge_minorAxis']] = 0.0 lensrow[self.defs_dict['galaxyBulge_positionAngle']] = 0.0 lensrow[self.defs_dict['galaxyBulge_internalAv']] = 0.0 lensrow[self.defs_dict[ 'galaxyBulge_magNorm']] = 999. #np.nan To be fixed post run1.1 lensrow[self.defs_dict['galaxyBulge_sedFilename']] = None z_s = use_df['zs'].iloc[i] lensrow[self.defs_dict['galaxyBulge_redshift']] = z_s lensrow[self.defs_dict['galaxyDisk_redshift']] = z_s lensrow[self.defs_dict['galaxyAgn_redshift']] = z_s #To get back twinklesID in lens catalog from phosim catalog id number #just use np.right_shift(phosimID-28, 10). Take the floor of the last #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is #the image number minus 1. lensrow[self.defs_dict['galtileid']] = ( lensrow[self.defs_dict['galtileid']] * 10000 + use_system * 4 + i) add_to_cat, sn_magnorm, sn_fname = self.create_sn_sed( use_df.iloc[i], lensrow[self.defs_dict['galaxyAgn_raJ2000']], lensrow[self.defs_dict['galaxyAgn_decJ2000']], self.visit_mjd) lensrow[self.defs_dict['galaxyAgn_sedFilename']] = sn_fname lensrow[self.defs_dict[ 'galaxyAgn_magNorm']] = sn_magnorm #This will need to be adjusted to proper band mag_adjust = 2.5 * np.log10(np.abs(use_df['mu'].iloc[i])) lensrow[self.defs_dict['galaxyAgn_magNorm']] -= mag_adjust if add_to_cat is True: updated_catalog = np.append(updated_catalog, lensrow) else: continue #Now manipulate original entry to be the lens galaxy with desired properties #Start by deleting Disk and AGN properties if not np.isnan(row[self.defs_dict['galaxyDisk_magNorm']]): row[self.defs_dict['galaxyDisk_majorAxis']] = 0.0 row[self.defs_dict['galaxyDisk_minorAxis']] = 0.0 row[self.defs_dict['galaxyDisk_positionAngle']] = 0.0 row[self.defs_dict['galaxyDisk_internalAv']] = 0.0 row[self.defs_dict[ 'galaxyDisk_magNorm']] = 999. #np.nan To be fixed post run1.1 row[self.defs_dict['galaxyDisk_sedFilename']] = None row[self.defs_dict[ 'galaxyAgn_magNorm']] = None #np.nan To be fixed post run1.1 row[self.defs_dict[ 'galaxyDisk_magNorm']] = 999. #To be fixed post run1.1 row[self.defs_dict['galaxyAgn_sedFilename']] = None #Now insert desired Bulge properties row[self.defs_dict['galaxyBulge_sedFilename']] = use_df[ 'lens_sed'].iloc[0] row[self. defs_dict['galaxyBulge_redshift']] = use_df['zl'].iloc[0] row[self. defs_dict['galaxyDisk_redshift']] = use_df['zl'].iloc[0] row[self. defs_dict['galaxyAgn_redshift']] = use_df['zl'].iloc[0] row[self.defs_dict['galaxyBulge_magNorm']] = use_df[ 'bulge_magnorm'].iloc[0] # row[self.defs_dict['galaxyBulge_magNorm']] = matchBase().calcMagNorm([newlens['APMAG_I']], self.LRG, self.bandpassDict) #Changed from i band to imsimband row[self. defs_dict['galaxyBulge_majorAxis']] = radiansFromArcsec( use_df['r_eff'].iloc[0] / np.sqrt(1 - use_df['e'].iloc[0])) row[self. defs_dict['galaxyBulge_minorAxis']] = radiansFromArcsec( use_df['r_eff'].iloc[0] * np.sqrt(1 - use_df['e'].iloc[0])) #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians row[self.defs_dict['galaxyBulge_positionAngle']] = use_df[ 'theta_e'].iloc[0] * (-1.0) * np.pi / 180.0 #Replace original entry with new entry updated_catalog[rowNum] = row return updated_catalog def find_lens_candidates(self, galz, gal_mag): # search the OM10 catalog for all sources +- 0.1 dex in redshift # and within .25 mags of the CATSIM source w = np.where( (np.abs(np.log10(self.lenscat['ZSRC']) - np.log10(galz)) <= 0.1) & (np.abs(self.src_mag_norm - gal_mag) <= .25))[0] lens_candidates = self.lenscat[w] return lens_candidates def find_sne_lens_candidates(self, galz): w = np.where( (np.abs(np.log10(self.sne_catalog['zs']) - np.log10(galz)) <= 0.1)) lens_candidates = self.sne_catalog.iloc[w] return lens_candidates def create_sn_sed(self, system_df, sn_ra, sn_dec, sed_mjd): sn_param_dict = copy.deepcopy(self.sn_obj.SNstate) sn_param_dict['_ra'] = sn_ra sn_param_dict['_dec'] = sn_dec sn_param_dict['z'] = system_df['zs'] sn_param_dict['c'] = system_df['c'] sn_param_dict['x0'] = system_df['x0'] sn_param_dict['x1'] = system_df['x1'] sn_param_dict['t0'] = system_df['t_start'] # sn_param_dict['t0'] = 61681.083859 #+1500. ### For testing only current_sn_obj = self.sn_obj.fromSNState(sn_param_dict) current_sn_obj.mwEBVfromMaps() wavelen_max = 1800. wavelen_min = 30. wavelen_step = 0.1 sn_sed_obj = current_sn_obj.SNObjectSED(time=sed_mjd, wavelen=np.arange( wavelen_min, wavelen_max, wavelen_step)) flux_500 = sn_sed_obj.flambda[np.where( sn_sed_obj.wavelen >= 499.99)][0] if flux_500 > 0.: add_to_cat = True sn_magnorm = current_sn_obj.catsimBandMag(self.imSimBand, sed_mjd) sn_name = 'specFileGLSN_%i_%i_%.4f.txt' % ( system_df['twinkles_sysno'], system_df['imno'], sed_mjd) sed_filename = '%s/%s' % (self.sed_path, sn_name) sn_sed_obj.writeSED(sed_filename) with open(sed_filename, 'rb') as f_in, gzip.open(str(sed_filename + '.gz'), 'wb') as f_out: shutil.copyfileobj(f_in, f_out) os.remove(sed_filename) else: add_to_cat = False sn_magnorm = np.nan sn_name = None return add_to_cat, sn_magnorm, sn_name def update_catsim(self): # Remove the catsim object # Add lensed images to the catsim given source brightness and magnifications # Add lens galaxy to catsim return def catsim_to_phosim(self): # Pass this catsim to phosim to make images return
import os import bz2 import gzip import numpy from lsst.sims.photUtils import Bandpass, Sed inputDir = '/astro/store/pogo3/danielsf/CIFIST2011raw' outputDir = '/astro/store/pogo3/danielsf/CIFIST2011binned' normBandpass = Bandpass() normBandpass.imsimBandpass() inNames = [] for name in os.listdir(inputDir): if name[:3]=='lte': inNames.append(name) dwav = 0.5 #bin width in nm wavMax = 3.1e4 #maximum wavelength in nm for filename in inNames: outname = filename.strip('bz2') outname = outname.strip('.7') outname = outname + '.gz' outname = os.path.join(outputDir, outname) if not os.path.exists(outname): name = os.path.join(inputDir,filename) rawArray = [] inSpectrum = bz2.BZ2File(name,'r')
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 testSetUp(self): """ Test the SedList can be successfully initialized """ ############## Try just reading in an normalizing some SEDs nSed = 10 sedNameList = self.getListOfSedNames(nSed) magNormList = numpy.random.random_sample(nSed)*5.0 + 15.0 testList = SedList(sedNameList, magNormList) self.assertEqual(len(testList), nSed) self.assertTrue(testList.internalAvList is None) self.assertTrue(testList.galacticAvList is None) self.assertTrue(testList.redshiftList is None) self.assertTrue(testList.wavelenMatch is None) self.assertTrue(testList.cosmologicalDimming is True) 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) numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################# now add an internalAv sedNameList = self.getListOfSedNames(nSed) magNormList = numpy.random.random_sample(nSed)*5.0 + 15.0 internalAvList = numpy.random.random_sample(nSed)*0.3 + 0.1 testList = SedList(sedNameList, magNormList, internalAvList=internalAvList) self.assertTrue(testList.galacticAvList is None) self.assertTrue(testList.redshiftList is None) self.assertTrue(testList.wavelenMatch is None) self.assertTrue(testList.cosmologicalDimming is True) 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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=av) numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################ now add redshift 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 testList = SedList(sedNameList, magNormList, internalAvList=internalAvList, redshiftList=redshiftList) self.assertTrue(testList.galacticAvList is None) self.assertTrue(testList.wavelenMatch is None) self.assertTrue(testList.cosmologicalDimming is True) 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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=True) numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################# without cosmological dimming 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 testList = SedList(sedNameList, magNormList, internalAvList=internalAvList, redshiftList=redshiftList, cosmologicalDimming=False) self.assertTrue(testList.galacticAvList is None) self.assertTrue(testList.wavelenMatch is None) self.assertTrue(testList.cosmologicalDimming is False) 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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=False) numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################ now add galacticAv 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 testList = SedList(sedNameList, magNormList, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList) self.assertTrue(testList.wavelenMatch is None) self.assertTrue(testList.cosmologicalDimming is True) 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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=True) a_coeff, b_coeff = sedControl.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) ################ now use a wavelen_match 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 wavelen_match = numpy.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList, magNormList, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList, wavelenMatch=wavelen_match) self.assertTrue(testList.cosmologicalDimming is True) 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) numpy.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.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=av) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCMab() sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav) numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
class CartoonUncertaintyTestCase(unittest.TestCase): """ This unit test suite will verify that our 'arbitrarily extensible' framework for writing magnitude uncertainty getters actually behaves as advertised """ def setUp(self): self.normband = Bandpass() self.normband.imsimBandpass() self.uband = Bandpass() self.uband.readThroughput(os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData', 'test_bandpass_u.dat')) self.gband = Bandpass() self.gband.readThroughput(os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData', 'test_bandpass_g.dat')) 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_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 test_object_extraction_galaxies(self): """ Test that method to get GalSimCelestialObjects from InstanceCatalogs works """ 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) (gs_object_arr, gs_object_dict) = desc.imsim.sources_from_file( galaxy_phosim_file, obs_md, phot_params) id_arr = np.zeros(len(gs_object_arr), dtype=int) for i_obj in range(len(gs_object_arr)): id_arr[i_obj] = gs_object_arr[i_obj].uniqueId truth_dtype = np.dtype([('uniqueId', int), ('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=';') 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 i_obj, gs_obj in enumerate(gs_object_arr): 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(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 i_obj, gs_obj in enumerate(gs_object_arr): self.assertEqual(truth_data['uniqueId'][i_obj], gs_obj.uniqueId) 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 i_obj, gs_obj in enumerate(gs_object_arr): 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.setupCCMab() sed.addCCMDust(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.setupCCMab() sed.addCCMDust(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)[0] 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 _calculateGalSimSeds(self): """ Apply any physical corrections to the objects' SEDS (redshift them, apply dust, etc.). Return a list of Sed objects containing the SEDS """ sedList = [] actualSEDnames = self.column_by_name('sedFilepath') redshift = self.column_by_name('redshift') internalAv = self.column_by_name('internalAv') internalRv = self.column_by_name('internalRv') galacticAv = self.column_by_name('galacticAv') galacticRv = self.column_by_name('galacticRv') magNorm = self.column_by_name('magNorm') #for setting magNorm imsimband = Bandpass() imsimband.imsimBandpass() outputNames=[] for (sedName, zz, iAv, iRv, gAv, gRv, norm) in \ zip(actualSEDnames, redshift, internalAv, internalRv, galacticAv, galacticRv, magNorm): if is_null(sedName): sedList.append(None) else: if sedName in self.uniqueSeds: #we have already read in this file; no need to do it again sed = Sed(wavelen=self.uniqueSeds[sedName].wavelen, flambda=self.uniqueSeds[sedName].flambda, fnu=self.uniqueSeds[sedName].fnu, name=self.uniqueSeds[sedName].name) else: #load the SED of the object sed = Sed() sedFile = os.path.join(self.sedDir, sedName) sed.readSED_flambda(sedFile) flambdaCopy = copy.deepcopy(sed.flambda) #If the SED is zero inside of the bandpass, GalSim raises an error. #This sets a minimum flux value of 1.0e-30 so that the SED is never technically #zero inside of the bandpass. sed.flambda = numpy.array([ff if ff>1.0e-30 else 1.0e-30 for ff in flambdaCopy]) sed.fnu = None #copy the unnormalized file to uniqueSeds so we don't have to read it in again sedCopy = Sed(wavelen=sed.wavelen, flambda=sed.flambda, fnu=sed.fnu, name=sed.name) self.uniqueSeds[sedName] = sedCopy #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.setupCCMab() sed.addCCMDust(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) a_int, b_int = sed.setupCCMab() sed.addCCMDust(a_int, b_int, A_v=gAv, R_v=gRv) sedList.append(sed) return sedList
import os import bz2 import gzip import numpy from lsst.sims.photUtils import Bandpass, Sed inputDir = '/astro/store/pogo3/danielsf/CIFIST2011raw' outputDir = '/astro/store/pogo3/danielsf/CIFIST2011binned' normBandpass = Bandpass() normBandpass.imsimBandpass() inNames = [] for name in os.listdir(inputDir): if name[:3] == 'lte': inNames.append(name) dwav = 0.5 #bin width in nm wavMax = 3.1e4 #maximum wavelength in nm for filename in inNames: outname = filename.strip('bz2') outname = outname.strip('.7') outname = outname + '.gz' outname = os.path.join(outputDir, outname) if not os.path.exists(outname): name = os.path.join(inputDir, filename) rawArray = [] inSpectrum = bz2.BZ2File(name, 'r')
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 __init__(self, sedNameList, magNormList, normalizingBandpass=None, specMap=defaultSpecMap, fileDir = getPackageDir('sims_sed_library'), wavelenMatch = None, redshiftList = None, galacticAvList = None, internalAvList = None, cosmologicalDimming = True): """ @param [in] sedNameList is a list of SED file names. @param [in] magNormList is a list of magnitude normalizations (in the normalizingBandpass) for each of the Seds. @param[in] normalizingBandpass is an instantiation of the Bandpass class defining the bandpass in which the magNorms from magNormList are calculated. This defaults to the Bandpass().imsimBandpass(), which is essentially a delta function at 500 nm. @param [in] fileDir is the base directory where the Sed files are stored (defaults to LSST sims_sed_library package). @param [in] specMap is a specMap (defined in sims_utils/../fileMaps.py) that maps the names in sedNameList to paths of the files relative to fileDir (defaults to defaultSpecMap defined in sims_utils) @param [in] wavelenMatch is an optional numpy array representing the wavelength grid to which all Seds will be re-mapped. @param [in] redshiftList is an optional list of redshifts for the Sed @param [in] internalAvList is an optional list of A(V) due to internal dust (for spectra of galaxies). @param [in] galacticAvList is an optional list of A(V) due to Milky Way Dust. @param [in] cosmologicalDimming is a boolean indicating whether cosmological dimming (the extray (1+z)^-1 factor in flux) should be applied to spectra when they are redshifted (defaults to True) Note: once wavelenMatch and cosmologicalDimming have been set in the constructor, they cannot be un-set. Similarly: if you construct a SedList without a galacticAvList, internalAvList, or redshiftList, you cannot later add spectra with whichever of those features were left out. """ self._initialized = False self._spec_map = specMap self._wavelen_match = copy.deepcopy(wavelenMatch) self._file_dir = fileDir self._cosmological_dimming = cosmologicalDimming #self._unique_sed_dict will store all of the unique SED files that have been #loaded. If an object requires an SED that has already been loaded, #it will just copy it from the dict. self._unique_sed_dict = {} self._unique_sed_dict['None'] = Sed() if normalizingBandpass is None: normalizingBandpass = Bandpass() normalizingBandpass.imsimBandpass() self._normalizing_bandpass = normalizingBandpass self._sed_list = [] self._redshift_list = None self._galactic_av_list = None self._internal_av_list = None self._a_int = None self._b_int = None self._av_int_wavelen = None self._a_gal = None self._b_gal = None self._av_gal_wavelen = None self.loadSedsFromList(sedNameList, magNormList, internalAvList = internalAvList, galacticAvList = galacticAvList, redshiftList = redshiftList)
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)
ra_full = ra_full[sorted_dex] dec_full = dec_full[sorted_dex] log_edd_ratio_full = log_edd_ratio_full[sorted_dex] bp_dict = BandpassDict.loadTotalBandpassesFromFiles() sed_dir = os.path.join(getPackageDir('sims_sed_library'), 'agnSED') sed_name = os.path.join(sed_dir, 'agn.spec.gz') if not os.path.exists(sed_name): raise RuntimeError('\n\n%s\n\nndoes not exist\n\n' % sed_name) base_sed = Sed() base_sed.readSED_flambda(sed_name) imsimband = Bandpass() imsimband.imsimBandpass() z_grid = np.arange(0.0, redshift_full.max(), 0.01) m_i_grid = np.zeros(len(z_grid), dtype=float) mag_norm_grid = np.zeros(len(z_grid), dtype=float) for i_z, zz in enumerate(z_grid): ss = Sed(wavelen=base_sed.wavelen, flambda=base_sed.flambda) ss.redshiftSED(zz, dimming=True) m_i_grid[i_z] = ss.calcMag(bp_dict['i']) mag_norm_grid[i_z] = ss.calcMag(imsimband) bands = 'ugrizy' # Effective wavelengths of each filter in Angstroms. eff_wls = np.array( [10.0 * bp_dict[bp].calcEffWavelen()[0] for bp in bands]) htmid_level = 8