def testSedBandpassMatch(self): """Test errors when bandpass and sed do not completely overlap in wavelength range.""" # Test case where they do match (no error message) sedwavelen = np.arange(self.wmin, self.wmax+.5, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) print('') # Test that no warning is made. with warnings.catch_warnings(record=True) as wa: w, f = testsed.resampleSED(wavelen_match=self.testbandpass.wavelen, wavelen=testsed.wavelen, flux=testsed.flambda) self.assertEqual(len(wa), 0) np.testing.assert_equal(w, testsed.wavelen) np.testing.assert_equal(f, testsed.flambda) # Test that warning is given for non-overlap at either top or bottom end of wavelength range. sedwavelen = np.arange(self.wmin, self.wmax - 50, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) with warnings.catch_warnings(record=True) as wa: testsed.resampleSED(wavelen_match=self.testbandpass.wavelen) self.assertEqual(len(wa), 1) self.assertIn('non-overlap', str(wa[-1].message)) np.testing.assert_equal(testsed.flambda[-1:], np.NaN) sedwavelen = np.arange(self.wmin+50, self.wmax, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) with warnings.catch_warnings(record=True) as wa: testsed.resampleSED(wavelen_match=self.testbandpass.wavelen) self.assertEqual(len(wa), 1) self.assertIn('non-overlap', str(wa[-1].message)) np.testing.assert_equal(testsed.flambda[0], np.NaN) np.testing.assert_equal(testsed.flambda[49], np.NaN)
def magListForSed(self, sedobj, indices=None): """ Return a list of magnitudes for a single Sed object. @param [in] sedobj is an Sed object. Its wavelength grid can be arbitrary. If necessary, a copy will be created and resampled onto the wavelength grid of the Bandpasses before magnitudes are calculated. The original Sed will be unchanged. @param [in] indices is an optional list of indices indicating which bandpasses to actually calculate magnitudes for. Other magnitudes will be listed as numpy.NaN (i.e. this method will return as many magnitudes as were loaded with the loadBandpassesFromFiles methods; it will just return numpy.NaN for magnitudes you did not actually ask for) @param [out] magList is a list of magnitudes in the bandpasses stored in this BandpassDict """ if sedobj.wavelen is not None: # If the Sed's wavelength grid agrees with self._wavelen_match to one part in # 10^6, just use the Sed as-is. Otherwise, copy it and resample it onto # self._wavelen_match if sedobj._needResample(wavelen_match=self._wavelen_match): dummySed = Sed(wavelen=sedobj.wavelen, flambda=sedobj.flambda) dummySed.resampleSED(force=True, wavelen_match=self._bandpassDict.values()[0].wavelen) else: dummySed = sedobj return numpy.array(self._magListForSed(dummySed, indices=indices)) else: return numpy.array([numpy.NaN]*len(self._bandpassDict))
def magListForSed(self, sedobj, indices=None): """ Return a list of magnitudes for a single Sed object. @param [in] sedobj is an Sed object. Its wavelength grid can be arbitrary. If necessary, a copy will be created and resampled onto the wavelength grid of the Bandpasses before magnitudes are calculated. The original Sed will be unchanged. @param [in] indices is an optional list of indices indicating which bandpasses to actually calculate magnitudes for. Other magnitudes will be listed as numpy.NaN (i.e. this method will return as many magnitudes as were loaded with the loadBandpassesFromFiles methods; it will just return numpy.NaN for magnitudes you did not actually ask for) @param [out] magList is a list of magnitudes in the bandpasses stored in this BandpassDict """ if sedobj.wavelen is not None: # If the Sed's wavelength grid agrees with self._wavelen_match to one part in # 10^6, just use the Sed as-is. Otherwise, copy it and resample it onto # self._wavelen_match if sedobj._needResample(wavelen_match=self._wavelen_match): dummySed = Sed(wavelen=sedobj.wavelen, flambda=sedobj.flambda) dummySed.resampleSED( force=True, wavelen_match=self._bandpassDict.values()[0].wavelen) else: dummySed = sedobj return numpy.array(self._magListForSed(dummySed, indices=indices)) else: return numpy.array([numpy.NaN] * len(self._bandpassDict))
def testSedBandpassMatch(self): """Test errors when bandpass and sed do not completely overlap in wavelength range.""" # Test case where they do match (no error message) sedwavelen = np.arange(self.wmin, self.wmax + .5, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) print('') # Test that no warning is made. with warnings.catch_warnings(record=True) as wa: w, f = testsed.resampleSED(wavelen_match=self.testbandpass.wavelen, wavelen=testsed.wavelen, flux=testsed.flambda) self.assertEqual(len(wa), 0) np.testing.assert_equal(w, testsed.wavelen) np.testing.assert_equal(f, testsed.flambda) # Test that warning is given for non-overlap at either top or bottom end of wavelength range. sedwavelen = np.arange(self.wmin, self.wmax - 50, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) with warnings.catch_warnings(record=True) as wa: testsed.resampleSED(wavelen_match=self.testbandpass.wavelen) self.assertEqual(len(wa), 1) self.assertIn('non-overlap', str(wa[-1].message)) np.testing.assert_equal(testsed.flambda[-1:], np.NaN) sedwavelen = np.arange(self.wmin + 50, self.wmax, 1) sedflambda = np.ones(len(sedwavelen)) testsed = Sed(wavelen=sedwavelen, flambda=sedflambda) with warnings.catch_warnings(record=True) as wa: testsed.resampleSED(wavelen_match=self.testbandpass.wavelen) self.assertEqual(len(wa), 1) self.assertIn('non-overlap', str(wa[-1].message)) np.testing.assert_equal(testsed.flambda[0], np.NaN) np.testing.assert_equal(testsed.flambda[49], np.NaN)
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 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 fluxListForSed(self, sedobj, indices=None): """ Return a list of Fluxes for a single Sed object. @param [in] sedobj is an Sed object. Its wavelength grid can be arbitrary. If necessary, a copy will be created and resampled onto the wavelength grid of the Bandpasses before fluxes are calculated. The original Sed will be unchanged. @param [in] indices is an optional list of indices indicating which bandpasses to actually calculate fluxes for. Other fluxes will be listed as numpy.NaN (i.e. this method will return as many fluxes as were loaded with the loadBandpassesFromFiles methods; it will just return numpy.NaN for fluxes you did not actually ask for) @param [out] fluxList is a list of fluxes in the bandpasses stored in this BandpassDict Note on units: Fluxes calculated this way will be the flux density integrated over the weighted response curve of the bandpass. See equaiton 2.1 of the LSST Science Book http://www.lsst.org/scientists/scibook """ if sedobj.wavelen is not None: # If the Sed's wavelength grid agrees with self._wavelen_match to one part in # 10^6, just use the Sed as-is. Otherwise, copy it and resample it onto # self._wavelen_match if sedobj._needResample(wavelen_match=self._wavelen_match): dummySed = Sed(wavelen=sedobj.wavelen, flambda=sedobj.flambda) dummySed.resampleSED( force=True, wavelen_match=self._bandpassDict.values()[0].wavelen) else: dummySed = sedobj return numpy.array(self._fluxListForSed(dummySed, indices=indices)) else: return numpy.array([numpy.NaN] * len(self._bandpassDict))
def fluxListForSed(self, sedobj, indices=None): """ Return a list of Fluxes for a single Sed object. @param [in] sedobj is an Sed object. Its wavelength grid can be arbitrary. If necessary, a copy will be created and resampled onto the wavelength grid of the Bandpasses before fluxes are calculated. The original Sed will be unchanged. @param [in] indices is an optional list of indices indicating which bandpasses to actually calculate fluxes for. Other fluxes will be listed as numpy.NaN (i.e. this method will return as many fluxes as were loaded with the loadBandpassesFromFiles methods; it will just return numpy.NaN for fluxes you did not actually ask for) @param [out] fluxList is a list of fluxes in the bandpasses stored in this BandpassDict Note on units: Fluxes calculated this way will be the flux density integrated over the weighted response curve of the bandpass. See equaiton 2.1 of the LSST Science Book http://www.lsst.org/scientists/scibook """ if sedobj.wavelen is not None: # If the Sed's wavelength grid agrees with self._wavelen_match to one part in # 10^6, just use the Sed as-is. Otherwise, copy it and resample it onto # self._wavelen_match if sedobj._needResample(wavelen_match=self._wavelen_match): dummySed = Sed(wavelen=sedobj.wavelen, flambda=sedobj.flambda) dummySed.resampleSED(force=True, wavelen_match=self._bandpassDict.values()[0].wavelen) else: dummySed = sedobj return numpy.array(self._fluxListForSed(dummySed, indices=indices)) else: return numpy.array([numpy.NaN]*len(self._bandpassDict))
class Telescope(Throughputs): def __init__(self, airmass=1, atmos=True, aerosol=False, libradtran=False, **kwargs): Throughputs.__init__(self, **kwargs) print("**** Telescope.__init__******") #self.filters=filterlist params = [ 'mag_sky', 'm5', 'FWHMeff', 'Tb', 'Sigmab', 'zp', 'counts_zp', 'Skyb', 'flux_sky' ] self.data = {} for par in params: self.data[par] = {} self.data['FWHMeff'] = dict( zip('ugrizy', [0.92, 0.87, 0.83, 0.80, 0.78, 0.76])) self.atmos = atmos self.libradtran = libradtran self.Load_Atmosphere(airmass) self.sed = None self.sedAB0 = None self.Inputs() self.Sky() self.ZP() self.Set_SED_AB0() # set a AB reference source #------------------------------------------------------------------------- @property def FWHMeff(self): return self.data['FWHMeff'] #------------------------------------------------------------------------- @property def mag_sky(self): return self.data['mag_sky'] #------------------------------------------------------------------------- @property def m5(self): return self.data['m5'] #------------------------------------------------------------------------- @property def Tb(self): return self.data['Tb'] #------------------------------------------------------------------------- @property def Sigmab(self): return self.data['Sigmab'] #------------------------------------------------------------------------- @property def zp(self): return self.data['zp'] #------------------------------------------------------------------------- @property def ADU_zp(self): return self.data['counts_zp'] #------------------------------------------------------------------------- @property def flux_sky(self): return self.data['flux_sky'] #------------------------------------------------------------------------- def Inputs(self): for filtre in self.filterlist: myup = self.Calc_Integ_Sed(self.darksky, self.system[filtre]) self.data['Tb'][filtre] = self.Calc_Integ(self.atmosphere[filtre]) self.data['Sigmab'][filtre] = self.Calc_Integ(self.system[filtre]) self.data['mag_sky'][filtre] = -2.5 * np.log10( myup / (3631. * self.Sigmab[filtre])) #------------------------------------------------------------------------- def Sky(self): for filtre in self.filterlist: self.Calc_m5(filtre) #------------------------------------------------------------------------- def Calc_m5(self, filtre): """ Calc_m5(filtre): Compute m5 or SNR at five sigma Tool function implemented by Phillie Gris (IN2P3) """ # get telescope passband (no atmosphere) filtre_trans = self.system[filtre] wavelen_min, wavelen_max, wavelen_step = filtre_trans.getWavelenLimits( None, None, None) bandpass = Bandpass(wavelen=filtre_trans.wavelen, sb=filtre_trans.sb) # create a Flat sed S_nu from the sky brightness magnitude flatSedb = Sed() flatSedb.setFlatSED(wavelen_min, wavelen_max, wavelen_step) flux0b = np.power(10., -0.4 * self.mag_sky[filtre]) flatSedb.multiplyFluxNorm(flux0b) # Get LSST photometric parameters photParams = PhotometricParameters(bandpass=filtre) norm = photParams.platescale**2 / 2. * photParams.exptime / photParams.gain # Use LSST sims (SignalToNoise) to calculate M5 with atmosphere or without atmosphere if self.atmos: self.data['m5'][filtre] = SignalToNoise.calcM5( flatSedb, self.atmosphere[filtre], self.system[filtre], photParams=photParams, FWHMeff=self.FWHMeff[filtre]) adu_int = flatSedb.calcADU(bandpass=self.atmosphere[filtre], photParams=photParams) self.data['flux_sky'][filtre] = adu_int * norm else: self.data['m5'][filtre] = SignalToNoise.calcM5( flatSedb, self.system[filtre], self.system[filtre], photParams=photParams, FWHMeff=self.FWHMeff[filtre]) adu_int = flatSedb.calcADU(bandpass=self.system[filtre], photParams=photParams) self.data['flux_sky'][filtre] = adu_int * norm #------------------------------------------------------------------------- def ZP(self): for filtre in self.filterlist: self.ZP_filtre(filtre) #print 'zeropoints',self.data['zp'],self.data['counts_zp'] #self.data['zp']=dict(zip(['u','g','r','i','z','y'],[27.03,28.53,28.27,27.91,27.49,26.78])) #------------------------------------------------------------------------- def ZP_filtre(self, filtre): """ ZP_filtre() : Compute zero point in filter band - platescale is 0.2 arcsec per pixel Tool function implemented by Phillie Gris (IN2P3) """ # extract parameters from lsst_sims photParams = PhotometricParameters(bandpass=filtre) # compute Diameter in meters : D=2*R = 2*sqrt(S/pi) Diameter = 2. * np.sqrt( photParams.effarea * 1.e-4 / np.pi) # diameter in meter # AB flux is 3.6307805477e-20 erg/cm2/s/Hz or 3.6307805477e-16 erg/m2/s/Hz # or 3.6307e-23 J/m2/s/Hz, h=6.626e-34 J.s # What is the meaning of this Cte ???? # especcialy what is 1e36 ? Cte = 3631. * np.pi * Diameter**2 * 2. * photParams.exptime / 4 / h / 1.e36 #print('Telescope::ZP_filtre: hello Cte=',Cte, ' Diam=',Diameter, 'h=',h,' exptime=',photParams.exptime) # What is the meaning of Skyb ????? self.data['Skyb'][filtre] = Cte * np.power( Diameter / 6.5, 2.) * np.power( 2. * photParams.exptime / 30., 2.) * np.power( photParams.platescale, 2.) * np.power( 10., 0.4 * (25. - self.mag_sky[filtre])) * self.Sigmab[filtre] #What is the meaning of Sigmab, Tb, Zb such Zb is used to calculate zero point Zb = 181.8 * np.power(Diameter / 6.5, 2.) * self.Tb[filtre] mbZ = 25. + 2.5 * np.log10(Zb) #filter without atmosphere filtre_trans = self.system[filtre] wavelen_min, wavelen_max, wavelen_step = filtre_trans.getWavelenLimits( None, None, None) bandpass = Bandpass(wavelen=filtre_trans.wavelen, sb=filtre_trans.sb) flatSed = Sed() flatSed.setFlatSED(wavelen_min, wavelen_max, wavelen_step) flux0 = np.power(10., -0.4 * mbZ) flatSed.multiplyFluxNorm(flux0) photParams = PhotometricParameters(bandpass=filtre) counts = flatSed.calcADU( bandpass, photParams=photParams) #number of counts for exptime self.data['zp'][filtre] = mbZ #print('Telescope::ZP_filtre hello',counts/self.photParams.exptime) self.data['counts_zp'][filtre] = counts / 2. * photParams.exptime #------------------------------------------------------------------------- def Calc_Integ(self, bandpass): """ Calc_Integ(): Compute sum F(lambda).dlambda/lambda in band (no unit) - F(lamba) : pass band - lambda : wavelength Tool function implemented by Phillie Gris (IN2P3) """ resu = 0. dlam = 0 for i, wave in enumerate(bandpass.wavelen): if i < len(bandpass.wavelen) - 1: dlam = bandpass.wavelen[i + 1] - wave resu += dlam * bandpass.sb[i] / wave #resu+=dlam*bandpass.sb[i] return resu #------------------------------------------------------------------------- def Calc_Integ_Sed(self, sed, bandpass, wavelen=None, fnu=None): """ Calc_Integ_Sed(self,sed,bandpass,wavelen=None, fnu=None) Compute sum of S_nu*F(lambda).dlambda/lambda in band units in erg/cm2/s/Hz - S_nu : SED in erg/cm2/s/Hz - F(lamba) : pass band - lambda : wavelength - force to use the SED S_nu (erg/cm2/s/Hz) instead of S_lamba (erg/cm2/s/nm) Tool function implemented by Phillie Gris (IN2P3) """ use_self = sed._checkUseSelf(wavelen, fnu) # Use self values if desired, otherwise use values passed to function. if use_self: # Calculate fnu if required. if sed.fnu is None: # If fnu not present, calculate. (does not regrid). sed.flambdaTofnu() wavelen = sed.wavelen fnu = sed.fnu # Make sure wavelen/fnu are on the same wavelength grid as bandpass. wavelen, fnu = sed.resampleSED(wavelen, fnu, wavelen_match=bandpass.wavelen) fnu = np.nan_to_num( fnu) # SDC(29/06/18) reset to 0 out of band where there are nan # Calculate the number of photons. nphoton = (fnu / wavelen * bandpass.sb).sum() dlambda = wavelen[1] - wavelen[0] return nphoton * dlambda #--------------------------------------------------------------- def CalcMyMagnitudes(self): """ CalcMyMagnitudes(sed) - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 4th 2018 Check how LSST Sim compute the magnitudes. Compute here with self.Calc_Integ_Sed() and Sed.calcMag() Used just for debug purpose """ all_mag1 = [] all_mag2 = [] #sed.flambdaTofnu() for i, band in enumerate(self.filterlist): filter = self.lsst_atmos[band] #phinorm=filter.sbTophi() # resample the wavelength each time for the filter wl, fnu = self.sed.getSED_fnu() wavelen, fnu = self.sed.resampleSED(wl, fnu, wavelen_match=filter.wavelen) fnu = np.nan_to_num( fnu ) # SDC(29/06/18) reset to 0 out of band where there are nan self.sed = Sed(wavelen=wavelen, fnu=fnu, name=self.sed.name) mag1 = self.sed.calcMag(bandpass=filter, wavelen=wavelen, fnu=fnu) mag2 = -2.5 * np.log10(self.Calc_Integ_Sed(self.sed, filter)) all_mag1.append(mag1) all_mag2.append(mag2) print('CalcMyMagnitudes :: band = {}, mag1= {} , mag2= {}'.format( i, mag1, mag2)) return np.array(all_mag1), np.array(all_mag2) #--------------------------------------------------------------- def CalcMyZP(self): """ CalcMyZP() - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 5th 2018 Calculate the Zero Points for all bands. This calculation should assume 1 second exposure and unit electronic gain """ for i, band in enumerate(self.filterlist): #for filtre in self.filterlist: filtre = self.lsst_atmos[band] # parameters photParams = PhotometricParameters(bandpass=band) Diameter = 2. * np.sqrt( photParams.effarea * 1.e-4 / np.pi) # diameter in meter exptime = 2 * photParams.exptime gain = photParams.gain # lsst sim calculation # by definition Zero point is defined for unit gain and unit exposure zp1 = filtre.calcZP_t(photParams) + 2.5 * np.log10(gain / exptime) # my calculation : Zero point should be calculated for unit gain and per second of exposure # in Jansky divided by J (photon energy E=hc/lambda) Snu_Tl_dldivl_AB0 = self.Calc_Integ_Sed(self.sedAB0, filtre) # in photoelectron per meter squared per meters per second # h is the Planck constant h=6.626x 10^-34 J.s dN_PhEl_AB0 = Snu_Tl_dldivl_AB0 / h * 1e-26 * np.pi * Diameter**2 / 4. zp2 = +2.5 * np.log10(dN_PhEl_AB0) print( "CalcMyZP :: band = {}, zp1(lsst_sim) = {}, zp2(me)= {}, deltaZP= {}" .format(i, zp1, zp2, zp1 - zp2)) #--------------------------------------------------------------- def CalcMyPhElMagnitudes(self): """ CalcMyElectronagnitudes(sed) - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 5th 2018 Calculate the instrumental magnitude (Photoelectrn unit) for all bands. """ all_magPhEl = [] for i, band in enumerate(self.filterlist): filter = self.lsst_atmos[band] #typical parameters of the band photParams = PhotometricParameters(bandpass=band) Diameter = 2. * np.sqrt( photParams.effarea * 1.e-4 / np.pi) # diameter in meter exptime = 2 * photParams.exptime # resample the wavelength each time for the filter wl, fnu = self.sed.getSED_fnu() wavelen, fnu = self.sed.resampleSED(wl, fnu, wavelen_match=filter.wavelen) fnu = np.nan_to_num( fnu ) # SDC(29/06/18) reset to 0 out of band where there are nan #this SED_nu is now in Jansky, units of 10-23 erg/cm2/s/Hz # 1 erg=10-7 J # 1 cm^-2 = 10^4 m^-2 # we have to multiply the SED_nu by 10-26 to be in J/m2/s/Hz self.sed = Sed(wavelen=wavelen, fnu=fnu, name=self.sed.name) # in Jansky divided by J (photon energy E=hc/lambda) Snu_Tl_dldivl = self.Calc_Integ_Sed(self.sed, filter) # in photoelectron per meter squared per meters per second # h is the Planck constant h=6.626x 10^-34 J.s dN_el = Snu_Tl_dldivl / h * 1e-26 * np.pi * Diameter**2 / 4. * exptime mag_el = -2.5 * np.log10(dN_el) print('CalcMyPhElMagnitudes :: band = {}, mag= {}'.format( i, mag_el)) all_magPhEl.append(mag_el) return np.array(all_magPhEl) #--------------------------------------------------------------- def CalcMyADUMagnitudes(self): """ CalcMyADUMagnitudes(sed) - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 5th 2018 Calculate the instrumental magnitude (ADU unit) for all bands. """ all_magADU = [] for i, band in enumerate(self.filterlist): filter = self.lsst_atmos[band] #typical parameters of the band photParams = PhotometricParameters(bandpass=band) Diameter = 2. * np.sqrt( photParams.effarea * 1.e-4 / np.pi) # diameter in meter exptime = 2 * photParams.exptime gain = photParams.gain # resample the wavelength each time for the filter wl, fnu = self.sed.getSED_fnu() wavelen, fnu = self.sed.resampleSED(wl, fnu, wavelen_match=filter.wavelen) fnu = np.nan_to_num( fnu ) # SDC(29/06/18) reset to 0 out of band where there are nan #this SED_nu is now in Jansky, units of 10-23 erg/cm2/s/Hz # 1 erg=10-7 J # 1 cm^-2 = 10^4 m^-2 # we have to multiply the SED_nu by 10-26 to be in J/m2/s/Hz self.sed = Sed(wavelen=wavelen, fnu=fnu, name=self.sed.name) # in Jansky divided by J (photon energy E=hc/lambda) Snu_Tl_dldivl = self.Calc_Integ_Sed(self.sed, filter) # in photoelectron per meter squared per meters per second # h is the Planck constant h=6.626x 10^-34 J.s dN_ADU = Snu_Tl_dldivl / h * 1e-26 * np.pi * Diameter**2 / 4. * exptime / gain mag_ADU = -2.5 * np.log10(dN_ADU) mag_ADU2 = -2.5 * np.log10( self.sed.calcADU(bandpass=filter, photParams=photParams, wavelen=wavelen, fnu=fnu)) print( 'CalcMyADUMagnitudes :: band = {}, mag1= {}, mag2={}, deltaM={}' .format(i, mag_ADU, mag_ADU2, mag_ADU - mag_ADU2)) all_magADU.append(mag_ADU) return np.array(all_magADU) #--------------------------------------------------------------- def CalcMyADUMagnitude_filter(self, band): """ CalcMyADUMagnitude_filter(band) - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 5th 2018 Calculate the instrumental magnitude (ADU unit) for one band. """ filter = self.lsst_atmos[band] #typical parameters of the band photParams = PhotometricParameters(bandpass=band) Diameter = 2. * np.sqrt( photParams.effarea * 1.e-4 / np.pi) # diameter in meter exptime = 2 * photParams.exptime gain = photParams.gain # resample the wavelength each time for the filter wl, fnu = self.sed.getSED_fnu() wavelen, fnu = self.sed.resampleSED(wl, fnu, wavelen_match=filter.wavelen) fnu = np.nan_to_num( fnu) # SDC(29/06/18) reset to 0 out of band where there are nan #this SED_nu is now in Jansky, units of 10-23 erg/cm2/s/Hz # 1 erg=10-7 J # 1 cm^-2 = 10^4 m^-2 # we have to multiply the SED_nu by 10-26 to be in J/m2/s/Hz self.sed = Sed(wavelen=wavelen, fnu=fnu, name=self.sed.name) # in Jansky divided by J (photon energy E=hc/lambda) Snu_Tl_dldivl = self.Calc_Integ_Sed(self.sed, filter) # in photoelectron per meter squared per meters per second # h is the Planck constant h=6.626x 10^-34 J.s dN_ADU = Snu_Tl_dldivl / h * 1e-26 * np.pi * Diameter**2 / 4. * exptime / gain mag_ADU = -2.5 * np.log10(dN_ADU) mag_ADU2 = -2.5 * np.log10( self.sed.calcADU(bandpass=filter, photParams=photParams, wavelen=wavelen, fnu=fnu)) #print('CalcMyADUMagnitude_filter :: band = {}, mag1(me)= {}, mag2(lsst_sim)={}, deltaM={}'.format(band,mag_ADU,mag_ADU2,mag_ADU-mag_ADU2)) return mag_ADU #--------------------------------------------------------------------- def CalcMyABMagnitudes(self): """ CalcMyABMagnitudes() - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 4th 2018 Calculate the magnitude in AB system unit for all bands. """ all_magAB = [] for i, band in enumerate(self.filterlist): filter = self.lsst_atmos[band] # resample the wavelength each time for the filter wl, fnu = self.sed.getSED_fnu() wavelen, fnu = self.sed.resampleSED(wl, fnu, wavelen_match=filter.wavelen) fnu = np.nan_to_num( fnu ) # SDC(29/06/18) reset to 0 out of band where there are nan self.sed = Sed(wavelen=wavelen, fnu=fnu, name=self.sed.name) mag1 = -2.5 * np.log10(self.Calc_Integ_Sed(self.sed, filter)) mag2 = -2.5 * np.log10(self.Calc_Integ_Sed(self.sedAB0, filter)) all_magAB.append(mag1 - mag2) mag3 = self.sed.calcMag(bandpass=filter, wavelen=wavelen, fnu=fnu) print( 'CalcMyABMagnitudes :: band = {}, mag1={} , mag2={} , deltaM(me)={}, mag3(lsst)={}' .format(i, mag1, mag2, mag1 - mag2, mag3)) return np.array(all_magAB) #--------------------------------------------------------------------- def CalcMyABMagnitude_filter(self, band): """ CalcMyABMagnitudes_filter() - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 4th 2018 Calculate the magnitude in AB system unit for all bands. """ filter = self.lsst_atmos[band] # resample the wavelength each time for the filter wl, fnu = self.sed.getSED_fnu() wavelen, fnu = self.sed.resampleSED(wl, fnu, wavelen_match=filter.wavelen) fnu = np.nan_to_num( fnu) # SDC(29/06/18) reset to 0 out of band where there are nan self.sed = Sed(wavelen=wavelen, fnu=fnu, name=self.sed.name) mag1 = -2.5 * np.log10(self.Calc_Integ_Sed(self.sed, filter)) mag2 = -2.5 * np.log10(self.Calc_Integ_Sed(self.sedAB0, filter)) mag3 = self.sed.calcMag(bandpass=filter, wavelen=wavelen, fnu=fnu) #print('CalcMyABMagnitude_filter :: band = {}, mag1={} , mag2={} , deltaM(me)={}, mag3(lsst)={}'.format(band,mag1,mag2,mag1-mag2,mag3)) return mag3 #--------------------------------------------------------------- def CalcMyABMagnitudesErrors(self): """ CalcMyABMagnitudesErrors(self) - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 4th 2018 Calculate magnitude errors for all bands """ all_magABErr = [] for i, band in enumerate(self.filterlist): filtre_atm = self.lsst_atmos[band] filtre_syst = self.lsst_system[band] wavelen_min, wavelen_max, wavelen_step = filtre_syst.getWavelenLimits( None, None, None) photParams = PhotometricParameters(bandpass=band) FWHMeff = self.data['FWHMeff'][band] # create a Flat sed S_nu from the sky brightness magnitude skysed = Sed() skysed.setFlatSED(wavelen_min, wavelen_max, wavelen_step) flux0b = np.power(10., -0.4 * self.mag_sky[band]) skysed.multiplyFluxNorm(flux0b) #calcMagError filled according doc magerr=SignalToNoise.calcMagError_sed( \ self.sed,filtre_atm,skysed,filtre_syst,photParams,FWHMeff,verbose=False) all_magABErr.append(magerr) return np.array(all_magABErr) #--------------------------------------------------------------- def CalcMyABMagnitudesError_filter(self, band, SkyBrightnessMag, FWHMGeom): """ CalcMyABMagnitudesError_filter(self,band,SkyBrightnessMag,FWHMGeom) - author : Sylvie Dagoret-Campagne - affiliation : LAL/IN2P3/CNRS/FRANCE - date : July 5th 2018 Calculate magnitude errors for one band. Input args: - band : filter band - SkyBrighnessMag : Sky Brighness Magnitude in the band - FWHMGeom : Geometrical PSF in the band """ filtre_atm = self.lsst_atmos[band] filtre_syst = self.lsst_system[band] wavelen_min, wavelen_max, wavelen_step = filtre_syst.getWavelenLimits( None, None, None) #calculation of effective PSF FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(FWHMGeom) photParams = PhotometricParameters(bandpass=band) # create a Flat sed S_nu from the sky brightness magnitude skysed = Sed() skysed.setFlatSED(wavelen_min, wavelen_max, wavelen_step) flux0b = np.power(10., -0.4 * SkyBrightnessMag) skysed.multiplyFluxNorm(flux0b) #calcMagError filled according doc mag_err = SignalToNoise.calcMagError_sed(self.sed, filtre_atm, skysed, filtre_syst, photParams, FWHMeff, verbose=False) return mag_err #--------------------------------------------------------------- def Plot_Filter(self): plt.figure(figsize=(5, 4)) for i, band in enumerate(self.filterlist): filter = self.lsst_atmos[band] #phinorm=filter.sbTophi() #print('phinorm',phinorm) plt.plot(filter.wavelen, filter.sb, 'k:') #plt.plot(filter.wavelen, phinorm, 'r.') plt.show() #------------------------------------------------------------------------- def flux_to_mag(self, flux, band, zp=None): if zp is None: zp = self.zero_points(band) print('Telescope::flux_to_mag: zp', zp, band) m = -2.5 * np.log10(flux) + zp return m #------------------------------------------------------------------------- def mag_to_flux(self, mag, band, zp=None): if zp is None: zp = self.zero_points(band) return np.power(10., -0.4 * (mag - zp)) #------------------------------------------------------------------------- def zero_points(self, band): return np.asarray([self.zp[b] for b in band]) #------------------------------------------------------------------------- def mag_to_flux_e_sec(self, mag, band, trans, sed): #this should be debugged at some point photrams = PhotometricParameters(bandpass=band) E_per_sec = sed.calcADU(bandpass=trans, photParams=photParams) e_per_sec /= exptime / photParams.gain return e_per_sec #------------------------------------------------------------------------- def Set_SEDAB(self): """ Set AB source : Enter the SED in erg/cm2/s/nm, """ M0 = 48.6 # magnitude of a AB source S_nu0 = 10**(-M0 / 2.5) # flux in erg/cm2/s/Hz : 3.630780547701003e-20 c = 2.99792458e10 # speed of light in cm/s in CGS nm_to_cm = 1e-7 # conversion nm to cm wavelength = np.arange(300., 1151., 1) S_lambda0 = S_nu0 * c / (nm_to_cm) / wavelength**2 # in erg/cm2/s/nm self.Set_SED(wavel=wavelength, newsed=S_lambda0, name='AB-source') #------------------------------------------------------------------------- def Set_SED_AB0(self): """ Set AB source : Enter the SED in erg/cm2/s/nm, """ M0 = 48.6 # magnitude of a AB source S_nu0 = 10**(-M0 / 2.5) # flux in erg/cm2/s/Hz : 3.630780547701003e-20 c = 2.99792458e10 # speed of light in cm/s in CGS nm_to_cm = 1e-7 # conversion nm to cm wavelength = np.arange(300., 1151., 1) S_lambda0 = S_nu0 * c / (nm_to_cm) / wavelength**2 # in erg/cm2/s/nm self.sedAB0 = Sed(wavelen=wavelength, flambda=S_lambda0, fnu=None, name='AB0-source') self.sedAB0.flambdaTofnu() #------------------------------------------------------------------------- def Set_SED(self, wavel, newsed, name='Pickles'): """ Set_SED(self,wavel,sed): Enter the SED in erg/cm2/s/nm, """ self.sed = Sed(wavelen=wavel, flambda=newsed, fnu=None, name=name) self.sed.flambdaTofnu() #------------------------------------------------------------------------- def Plot_SED(self): wl, fnu = self.sed.getSED_fnu() plt.plot(wl, fnu, 'b-') plt.xlabel("$\lambda$ (nm)") plt.ylabel("Flux in Jansky ($10^{-23}erg/cm^2/s/Hz$") title = 'SED ' plt.title(title) plt.grid(True)
from builtins import zip import numpy as np import lsst.sims.photUtils.Sed as Sed import os dataDir = os.getenv('SIMS_SKYBRIGHTNESS_DATA_DIR') data = np.genfromtxt(os.path.join(dataDir, 'solarSpec/solarSpec.dat'), dtype=list(zip(['microns', 'Irr'], [float]*2))) # data['Irr'] = data['Irr']*1 #convert W/m2/micron to erg/s/cm2/nm (HA, it's the same!) sun = Sed() sun.setSED(data['microns']*1e3, flambda=data['Irr']) # Match the wavelenth spacing and range to the ESO spectra airglowSpec = np.load(os.path.join(dataDir, 'ESO_Spectra/Airglow/airglowSpectra.npz')) sun.resampleSED(wavelen_match=airglowSpec['wave']) np.savez(os.path.join(dataDir, 'solarSpec/solarSpec.npz'), wave=sun.wavelen, spec=sun.flambda)
def ulysses2SED(data=None, workdir='output', wavefile='Ulysses_GaiaBPRP_meanSpecWavelength.txt', specfile='Ulysses_GaiaBPRP_noiseFreeSpectra.txt', noiseRoot='Ulysses_GaiaBPRP_noisyPhotSpec', noisy=True, response=None, wavelen_step=1.0, switch=675.): """ Read in some ulysses output and return a single SED object. """ if data is None: data = read_ulysses(workdir=workdir, wavefile=wavefile, specfile=specfile, noiseRoot=noiseRoot) if response is None: response = gaia_response() if noisy: datakey = 'noisySpec' key2 = 'NoisySpec' red_spec = response.apply(data[datakey][0]['RP'+key2], blue=False) blue_spec = response.apply(data[datakey][0]['BP'+key2], blue=True) else: datakey = 'noiseFreeSpec' key2 = 'NoiseFreeSpec' red_spec = response.apply(data[datakey]['RP'+key2], blue=False) blue_spec = response.apply(data[datakey]['BP'+key2], blue=True) red_sed = Sed(wavelen=data['RP_wave'], flambda=red_spec) # * 1e3) blue_sed = Sed(wavelen=data['BP_wave'], flambda=blue_spec) # * 1e3) wavelen_min = data['BP_wave'].min() wavelen_max = data['RP_wave'].max() # Rebin the red and blue to a common wavelength array red_sed.resampleSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=wavelen_step) blue_sed.resampleSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=wavelen_step) # Clean up nan's from resampling red_sed.flambda[np.isnan(red_sed.flambda)] = 0. blue_sed.flambda[np.isnan(blue_sed.flambda)] = 0. # Assume Poisson stats for the noise. red_weight = np.ones(red_sed.flambda.size, dtype=float) # 1./red_sed.flambda blue_weight = np.ones(red_sed.flambda.size, dtype=float) # 1./blue_sed.flambda red_weight[np.where(red_sed.flambda == 0)] = 0. blue_weight[np.where(blue_sed.flambda == 0)] = 0. # red_cutoff = np.min(response.red_wavelen[np.where(response.red_response == 0.)]) # blue_cutoff = np.max(response.blue_wavelen[np.where(response.blue_response == 0.)]) #red_weight[np.where(red_sed.wavelen < red_cutoff+cutoff_pad)] = 0. #blue_weight[np.where(blue_sed.wavelen > blue_cutoff-cutoff_pad)] = 0. # Just make a simple switchover wavelength red_weight[np.where(red_sed.wavelen <= switch)] = 0. blue_weight[np.where(blue_sed.wavelen > switch)] = 0. # weight = np.zeros(red_sed.wavelen.size, dtype=float) # weight[np.where(red_sed.flambda > 0)] += 1 # weight[np.where(blue_sed.flambda > 0)] += 1 flambda = (red_sed.flambda*red_weight + blue_sed.flambda*blue_weight) / (red_weight + blue_weight) # flambda[np.where(weight == 0.)] = 0. finalSED = Sed(flambda=flambda, wavelen=red_sed.wavelen) return finalSED
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)
def applyIGM(self, redshift, sedobj): """ Apply IGM extinction to already redshifted sed with redshift between zMin and zMax defined by range of lookup tables @param [in] redshift is the redshift of the incoming SED object @param [in] sedobj is the SED object to which IGM extinction will be applied. This object will be modified as a result of this. """ if self.IGMisInitialized == False: self.initializeIGM() #First make sure redshift is in range of lookup tables. if (redshift < self.zMin) or (redshift > self.zMax): warnings.warn(str("IGM Lookup tables only applicable for " + str(self.zMin) + " < z < " + str(self.zMax) + ". No action taken")) return #Now read in closest two lookup tables for given redshift lowerSed = Sed() upperSed = Sed() for lower, upper in zip(self.zRange[:-1], self.zRange[1:]): if lower <= redshift <= upper: lowerSed.setSED(self.meanLookups['%.1f' % lower][:,0], flambda = self.meanLookups['%.1f' % lower][:,1]) upperSed.setSED(self.meanLookups['%.1f' % upper][:,0], flambda = self.meanLookups['%.1f' % lower][:,1]) break #Redshift lookup tables to redshift of source, i.e. if source redshift is 1.78 shift lookup #table for 1.7 and lookup table for 1.8 to up and down to 1.78, respectively zLowerShift = ((1.0 + redshift)/(1.0 + lower)) - 1.0 zUpperShift = ((1.0 + redshift)/(1.0 + upper)) - 1.0 lowerSed.redshiftSED(zLowerShift) upperSed.redshiftSED(zUpperShift) #Resample lower and upper transmission data onto same wavelength grid. minWavelen = 300. #All lookup tables are usable above 300nm maxWavelen = np.amin([lowerSed.wavelen[-1], upperSed.wavelen[-1]]) - 0.01 lowerSed.resampleSED(wavelen_min = minWavelen, wavelen_max = maxWavelen, wavelen_step = 0.01) upperSed.resampleSED(wavelen_match = lowerSed.wavelen) #Now insert this into a transmission array of 1.0 beyond the limits of current application #So that we can get an sed back that extends to the longest wavelengths of the incoming sed finalWavelen = np.arange(300., sedobj.wavelen[-1]+0.01, 0.01) finalFlambdaExtended = np.ones(len(finalWavelen)) #Weighted Average of Transmission from each lookup table to get final transmission #table at desired redshift dzGrid = self.zDelta #Step in redshift between transmission lookup table files finalSed = Sed() finalFlambda = (lowerSed.flambda*(1.0 - ((redshift - lower)/dzGrid)) + upperSed.flambda*(1.0 - ((upper - redshift)/dzGrid))) finalFlambdaExtended[0:len(finalFlambda)] = finalFlambda finalSed.setSED(wavelen = finalWavelen, flambda = finalFlambdaExtended) #Resample incoming sed to new grid so that we don't get warnings from multiplySED #about matching wavelength grids sedobj.resampleSED(wavelen_match=finalSed.wavelen) #Now multiply transmission curve by input SED to get final result and make it the new flambda #data in the original sed which also is now on a new grid starting at 300 nm test = sedobj.multiplySED(finalSed) sedobj.flambda = test.flambda
import numpy as np import lsst.sims.photUtils.Sed as Sed import os dataDir = os.getenv('SIMS_SKYBRIGHTNESS_DATA_DIR') data = np.genfromtxt(os.path.join(dataDir, 'solarSpec/solarSpec.dat'), dtype=zip(['microns','Irr'],[float]*2)) #data['Irr'] = data['Irr']*1 #convert W/m2/micron to erg/s/cm2/nm (HA, it's the same!) sun = Sed() sun.setSED(data['microns']*1e3, flambda=data['Irr']) # Match the wavelenth spacing and range to the ESO spectra airglowSpec = np.load(os.path.join(dataDir, 'ESO_Spectra/Airglow/airglowSpectra.npz')) sun.resampleSED(wavelen_match=airglowSpec['wave']) np.savez(os.path.join(dataDir,'solarSpec/solarSpec.npz'), wave=sun.wavelen, spec=sun.flambda)
def sources_from_file(file_name, obs_md, phot_params, numRows=None): """ Read in an InstanceCatalog and extract all of the astrophysical sources from it Parameters ---------- file_name: str The name of the InstanceCatalog obs_md: ObservationMetaData The ObservationMetaData characterizing the pointing phot_params: PhotometricParameters The PhotometricParameters characterizing this telescope numRows: int (optional) The number of rows of the InstanceCatalog to read in (including the header) Returns ------- gs_obj_arr: numpy array Contains the GalSimCelestialObjects for all of the astrophysical sources in this InstanceCatalog out_obj_dict: dict Keyed on the names of the detectors in the LSST camera. The values are numpy arrays of GalSimCelestialObjects that should be simulated for that detector, including objects that are near the edge of the chip or just bright (in which case, they might still illuminate the detector). """ camera = get_obs_lsstSim_camera() num_objects = 0 ct_rows = 0 with fopen(file_name, mode='rt') as input_file: for line in input_file: ct_rows += 1 params = line.strip().split() if params[0] == 'object': num_objects += 1 if numRows is not None and ct_rows >= numRows: break # RA, Dec in the coordinate system expected by PhoSim ra_phosim = np.zeros(num_objects, dtype=float) dec_phosim = np.zeros(num_objects, dtype=float) sed_name = [None] * num_objects mag_norm = 55.0 * np.ones(num_objects, dtype=float) gamma1 = np.zeros(num_objects, dtype=float) gamma2 = np.zeros(num_objects, dtype=float) kappa = np.zeros(num_objects, dtype=float) internal_av = np.zeros(num_objects, dtype=float) internal_rv = np.zeros(num_objects, dtype=float) galactic_av = np.zeros(num_objects, dtype=float) galactic_rv = np.zeros(num_objects, dtype=float) semi_major_arcsec = np.zeros(num_objects, dtype=float) semi_minor_arcsec = np.zeros(num_objects, dtype=float) position_angle_degrees = np.zeros(num_objects, dtype=float) sersic_index = np.zeros(num_objects, dtype=float) npoints = np.zeros(num_objects, dtype=int) redshift = np.zeros(num_objects, dtype=float) unique_id = np.zeros(num_objects, dtype=int) object_type = np.zeros(num_objects, dtype=int) i_obj = -1 with fopen(file_name, mode='rt') as input_file: for line in input_file: params = line.strip().split() if params[0] != 'object': continue i_obj += 1 if numRows is not None and i_obj >= num_objects: break unique_id[i_obj] = int(params[1]) ra_phosim[i_obj] = float(params[2]) dec_phosim[i_obj] = float(params[3]) mag_norm[i_obj] = float(params[4]) sed_name[i_obj] = params[5] redshift[i_obj] = float(params[6]) gamma1[i_obj] = float(params[7]) gamma2[i_obj] = float(params[8]) kappa[i_obj] = float(params[9]) if params[12].lower() == 'point': object_type[i_obj] = _POINT_SOURCE i_gal_dust_model = 14 if params[13].lower() != 'none': i_gal_dust_model = 16 internal_av[i_obj] = float(params[14]) internal_rv[i_obj] = float(params[15]) if params[i_gal_dust_model].lower() != 'none': galactic_av[i_obj] = float(params[i_gal_dust_model + 1]) galactic_rv[i_obj] = float(params[i_gal_dust_model + 2]) elif params[12].lower() == 'sersic2d': object_type[i_obj] = _SERSIC_2D semi_major_arcsec[i_obj] = float(params[13]) semi_minor_arcsec[i_obj] = float(params[14]) position_angle_degrees[i_obj] = float(params[15]) sersic_index[i_obj] = float(params[16]) i_gal_dust_model = 18 if params[17].lower() != 'none': i_gal_dust_model = 20 internal_av[i_obj] = float(params[18]) internal_rv[i_obj] = float(params[19]) if params[i_gal_dust_model].lower() != 'none': galactic_av[i_obj] = float(params[i_gal_dust_model + 1]) galactic_rv[i_obj] = float(params[i_gal_dust_model + 2]) elif params[12].lower() == 'knots': object_type[i_obj] = _RANDOM_WALK semi_major_arcsec[i_obj] = float(params[13]) semi_minor_arcsec[i_obj] = float(params[14]) position_angle_degrees[i_obj] = float(params[15]) npoints[i_obj] = int(params[16]) i_gal_dust_model = 18 if params[17].lower() != 'none': i_gal_dust_model = 20 internal_av[i_obj] = float(params[18]) internal_rv[i_obj] = float(params[19]) if params[i_gal_dust_model].lower() != 'none': galactic_av[i_obj] = float(params[i_gal_dust_model + 1]) galactic_rv[i_obj] = float(params[i_gal_dust_model + 2]) else: raise RuntimeError("Do not know how to handle " "object type: %s" % params[12]) ra_appGeo, dec_appGeo = PhoSimAstrometryBase._appGeoFromPhoSim( np.radians(ra_phosim), np.radians(dec_phosim), obs_md) (ra_obs_rad, dec_obs_rad) = _observedFromAppGeo(ra_appGeo, dec_appGeo, obs_metadata=obs_md, includeRefraction=True) semi_major_radians = radiansFromArcsec(semi_major_arcsec) semi_minor_radians = radiansFromArcsec(semi_minor_arcsec) position_angle_radians = np.radians(position_angle_degrees) x_pupil, y_pupil = _pupilCoordsFromObserved(ra_obs_rad, dec_obs_rad, obs_md) bp_dict = BandpassDict.loadTotalBandpassesFromFiles() sed_dir = lsstUtils.getPackageDir('sims_sed_library') object_is_valid = np.array([True] * num_objects) invalid_objects = np.where( np.logical_or( np.logical_or( mag_norm > 50.0, np.logical_and(galactic_av == 0.0, galactic_rv == 0.0)), np.logical_or( np.logical_and(object_type == _SERSIC_2D, semi_major_arcsec < semi_minor_arcsec), np.logical_and(object_type == _RANDOM_WALK, npoints <= 0)))) object_is_valid[invalid_objects] = False if len(invalid_objects[0]) > 0: message = "\nOmitted %d suspicious objects from " % len( invalid_objects[0]) message += "the instance catalog:\n" n_bad_mag_norm = len(np.where(mag_norm > 50.0)[0]) message += " %d had mag_norm > 50.0\n" % n_bad_mag_norm n_bad_av = len( np.where(np.logical_and(galactic_av == 0.0, galactic_rv == 0.0))[0]) message += " %d had galactic_Av == galactic_Rv == 0\n" % n_bad_av n_bad_axes = len( np.where( np.logical_and(object_type == _SERSIC_2D, semi_major_arcsec < semi_minor_arcsec))[0]) message += " %d had semi_major_axis < semi_minor_axis\n" % n_bad_axes n_bad_knots = len( np.where(np.logical_and(object_type == _RANDOM_WALK, npoints <= 0))[0]) message += " %d had n_points <= 0 \n" % n_bad_knots warnings.warn(message) wav_int = None wav_gal = None gs_object_arr = [] for i_obj in range(num_objects): if not object_is_valid[i_obj]: continue if object_type[i_obj] == _POINT_SOURCE: gs_type = 'pointSource' elif object_type[i_obj] == _SERSIC_2D: gs_type = 'sersic' elif object_type[i_obj] == _RANDOM_WALK: gs_type = 'RandomWalk' # load the SED sed_obj = Sed() sed_obj.readSED_flambda(os.path.join(sed_dir, sed_name[i_obj])) fnorm = getImsimFluxNorm(sed_obj, mag_norm[i_obj]) sed_obj.multiplyFluxNorm(fnorm) if internal_av[i_obj] != 0.0: if wav_int is None or not np.array_equal(sed_obj.wavelen, wav_int): a_int, b_int = sed_obj.setupCCMab() wav_int = copy.deepcopy(sed_obj.wavelen) sed_obj.addCCMDust(a_int, b_int, A_v=internal_av[i_obj], R_v=internal_rv[i_obj]) if redshift[i_obj] != 0.0: sed_obj.redshiftSED(redshift[i_obj], dimming=True) sed_obj.resampleSED(wavelen_match=bp_dict.wavelenMatch) if galactic_av[i_obj] != 0.0: if wav_gal is None or not np.array_equal(sed_obj.wavelen, wav_gal): a_g, b_g = sed_obj.setupCCMab() wav_gal = copy.deepcopy(sed_obj.wavelen) sed_obj.addCCMDust(a_g, b_g, A_v=galactic_av[i_obj], R_v=galactic_rv[i_obj]) gs_object = GalSimCelestialObject(gs_type, x_pupil[i_obj], y_pupil[i_obj], semi_major_radians[i_obj], semi_minor_radians[i_obj], semi_major_radians[i_obj], position_angle_radians[i_obj], sersic_index[i_obj], sed_obj, bp_dict, phot_params, npoints[i_obj], gamma1=gamma1[i_obj], gamma2=gamma2[i_obj], kappa=kappa[i_obj], uniqueId=unique_id[i_obj]) gs_object_arr.append(gs_object) gs_object_arr = np.array(gs_object_arr) # how close to the edge of the detector a source has # to be before we will just simulate it anyway pix_tol = 50.0 # any source brighter than this will be considered # so bright that it should be simulated for all # detectors, just in case light scatters onto them. max_mag = 16.0 # down-select mag_norm, x_pupil, and y_pupil # to only contain those objects that were # deemed to be valid above valid = np.where(object_is_valid) mag_norm = mag_norm[valid] x_pupil = x_pupil[valid] y_pupil = y_pupil[valid] assert len(mag_norm) == len(gs_object_arr) assert len(x_pupil) == len(gs_object_arr) assert len(y_pupil) == len(gs_object_arr) out_obj_dict = {} for det in lsst_camera(): chip_name = det.getName() pixel_corners = getCornerPixels(chip_name, lsst_camera()) x_min = pixel_corners[0][0] x_max = pixel_corners[2][0] y_min = pixel_corners[0][1] y_max = pixel_corners[3][1] xpix, ypix = pixelCoordsFromPupilCoords(x_pupil, y_pupil, chipName=chip_name, camera=lsst_camera()) on_chip = np.where( np.logical_or( mag_norm < max_mag, np.logical_and( xpix > x_min - pix_tol, np.logical_and( xpix < x_max + pix_tol, np.logical_and(ypix > y_min - pix_tol, ypix < y_max + pix_tol))))) out_obj_dict[chip_name] = gs_object_arr[on_chip] return gs_object_arr, out_obj_dict
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 testGalaxyPhotometricUncertainties(self): """ Test in the case of a catalog of galaxies """ lsstDefaults = LSSTdefaults() galDB = testGalaxyTileDBObj(driver=self.driver, host=self.host, database=self.dbName) galCat = testGalaxyCatalog(galDB, obs_metadata=self.obs_metadata) imsimband = Bandpass() imsimband.imsimBandpass() ct = 0 for line in galCat.iter_catalog(): bulgeSedName = line[50] diskSedName = line[51] agnSedName = line[52] magNormBulge = line[53] magNormDisk = line[54] magNormAgn = line[55] avBulge = line[56] avDisk = line[57] redshift = line[58] bulgeSed = Sed() bulgeSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), defaultSpecMap[bulgeSedName])) fNorm = bulgeSed.calcFluxNorm(magNormBulge, imsimband) bulgeSed.multiplyFluxNorm(fNorm) diskSed = Sed() diskSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), defaultSpecMap[diskSedName])) fNorm = diskSed.calcFluxNorm(magNormDisk, imsimband) diskSed.multiplyFluxNorm(fNorm) agnSed = Sed() agnSed.readSED_flambda( os.path.join(getPackageDir('sims_sed_library'), defaultSpecMap[agnSedName])) fNorm = agnSed.calcFluxNorm(magNormAgn, imsimband) agnSed.multiplyFluxNorm(fNorm) a_int, b_int = bulgeSed.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) np.testing.assert_almost_equal(bulgeSed.wavelen, diskSed.wavelen) np.testing.assert_almost_equal(bulgeSed.wavelen, agnSed.wavelen) fl = bulgeSed.flambda + diskSed.flambda + agnSed.flambda totalSed = Sed(wavelen=bulgeSed.wavelen, flambda=fl) sedList = [totalSed, bulgeSed, diskSed, agnSed] for i, spectrum in enumerate(sedList): if i == 0: msgroot = 'failed on total' elif i == 1: msgroot = 'failed on bulge' elif i == 2: msgroot = 'failed on disk' elif i == 3: msgroot = 'failed on agn' for j, b in enumerate(self.bandpasses): controlSigma = calcMagError_sed( spectrum, self.totalBandpasses[j], self.skySeds[j], self.hardwareBandpasses[j], FWHMeff=lsstDefaults.FWHMeff(b), photParams=PhotometricParameters()) testSigma = line[26 + (i * 6) + j] msg = '%e neq %e; ' % (testSigma, controlSigma) + msgroot self.assertAlmostEqual(testSigma, controlSigma, 10, msg=msg) ct += 1 self.assertGreater(ct, 0)
def 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)
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)
def testAlternateNormalizingBandpass(self): """ A reiteration of testAddingToList, but testing with a non-imsimBandpass normalizing bandpass """ normalizingBand = Bandpass() normalizingBand.readThroughput(os.path.join(getPackageDir('throughputs'),'baseline','total_r.dat')) nSed = 10 sedNameList_0 = self.getListOfSedNames(nSed) magNormList_0 = 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, normalizingBandpass=normalizingBand, 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 internalAvList_1 = numpy.random.random_sample(nSed)*0.3 + 0.1 redshiftList_1 = numpy.random.random_sample(nSed)*5.0 galacticAvList_1 = numpy.random.random_sample(nSed)*0.3 + 0.1 testList.loadSedsFromList(sedNameList_1, magNormList_1, internalAvList=internalAvList_1, galacticAvList=galacticAvList_1, redshiftList=redshiftList_1) self.assertEqual(len(testList), 2*nSed) 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, normalizingBand) 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, normalizingBand) 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+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 applyIGM(self, redshift, sedobj): """ Apply IGM extinction to already redshifted sed with redshift between zMin and zMax defined by range of lookup tables @param [in] redshift is the redshift of the incoming SED object @param [in] sedobj is the SED object to which IGM extinction will be applied. This object will be modified as a result of this. """ if self.IGMisInitialized == False: self.initializeIGM() #First make sure redshift is in range of lookup tables. if (redshift < self.zMin) or (redshift > self.zMax): warnings.warn( str("IGM Lookup tables only applicable for " + str(self.zMin) + " < z < " + str(self.zMax) + ". No action taken")) return #Now read in closest two lookup tables for given redshift lowerSed = Sed() upperSed = Sed() for lower, upper in zip(self.zRange[:-1], self.zRange[1:]): if lower <= redshift <= upper: lowerSed.setSED(self.meanLookups['%.1f' % lower][:, 0], flambda=self.meanLookups['%.1f' % lower][:, 1]) upperSed.setSED(self.meanLookups['%.1f' % upper][:, 0], flambda=self.meanLookups['%.1f' % lower][:, 1]) break #Redshift lookup tables to redshift of source, i.e. if source redshift is 1.78 shift lookup #table for 1.7 and lookup table for 1.8 to up and down to 1.78, respectively zLowerShift = ((1.0 + redshift) / (1.0 + lower)) - 1.0 zUpperShift = ((1.0 + redshift) / (1.0 + upper)) - 1.0 lowerSed.redshiftSED(zLowerShift) upperSed.redshiftSED(zUpperShift) #Resample lower and upper transmission data onto same wavelength grid. minWavelen = 300. #All lookup tables are usable above 300nm maxWavelen = np.amin([lowerSed.wavelen[-1], upperSed.wavelen[-1] ]) - 0.01 lowerSed.resampleSED(wavelen_min=minWavelen, wavelen_max=maxWavelen, wavelen_step=0.01) upperSed.resampleSED(wavelen_match=lowerSed.wavelen) #Now insert this into a transmission array of 1.0 beyond the limits of current application #So that we can get an sed back that extends to the longest wavelengths of the incoming sed finalWavelen = np.arange(300., sedobj.wavelen[-1] + 0.01, 0.01) finalFlambdaExtended = np.ones(len(finalWavelen)) #Weighted Average of Transmission from each lookup table to get final transmission #table at desired redshift dzGrid = self.zDelta #Step in redshift between transmission lookup table files finalSed = Sed() finalFlambda = (lowerSed.flambda * (1.0 - ((redshift - lower) / dzGrid)) + upperSed.flambda * (1.0 - ((upper - redshift) / dzGrid))) finalFlambdaExtended[0:len(finalFlambda)] = finalFlambda finalSed.setSED(wavelen=finalWavelen, flambda=finalFlambdaExtended) #Resample incoming sed to new grid so that we don't get warnings from multiplySED #about matching wavelength grids sedobj.resampleSED(wavelen_match=finalSed.wavelen) #Now multiply transmission curve by input SED to get final result and make it the new flambda #data in the original sed which also is now on a new grid starting at 300 nm test = sedobj.multiplySED(finalSed) sedobj.flambda = test.flambda
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)
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
lsstbp[f].sbTophi() bplist.append(lsstbp[f]) phiarray, dlambda = tmpgal.setupPhiArray(bplist) # Set up dictionary + arrays to hold calculated magnitude information. mags2 = {} for f in filterlist: mags2[f] = numpy.zeros(num_gal, dtype='float') # For each galaxy (in num_gal's), apply internal dust, redshift, resample to 300-1200 nm, apply MW dust on # shorter (and standardized) wavelength range, fluxnorm, and then calculate mags using manyMagCalc. for i in range(num_gal): galname = gallist[gal_name[i]] tmpgal = Sed(wavelen=gals[galname].wavelen, flambda=gals[galname].flambda) tmpgal.addCCMDust(a_int, b_int, ebv=ebv_int[i]) tmpgal.redshiftSED(redshifts[i]) tmpgal.resampleSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=wavelen_step) tmpgal.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i]) tmpgal.multiplyFluxNorm(fluxnorm[i]) #tmpgal.flambdaTofnu() #Not needed because multiplyFluxNorm calculates fnu tmpmags = tmpgal.manyMagCalc(phiarray, dlambda) j = 0 for f in filterlist: mags2[f][i] = tmpmags[j] j = j + 1 dt, t = dtime(t) print "Calculating dust/redshift/dust/fluxnorm/%d magnitudes for %d galaxies optimized way took %f s" \ %(len(filterlist), num_gal, dt) # Check for differences in magnitudes. import pylab
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 testFlush(self): """ Test that the flush method of SedList behaves properly """ imsimBand = Bandpass() imsimBand.imsimBandpass() nSed = 10 sedNameList_0 = self.getListOfSedNames(nSed) magNormList_0 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = self.rng.random_sample(nSed)*5.0 galacticAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 wavelen_match = np.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, fileDir=self.sedDir, internalAvList=internalAvList_0, redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) self.assertEqual(len(testList), nSed) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_0)): self.assertAlmostEqual(internalAvList_0[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_0[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_0[ix], testList.redshiftList[ix], 10) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_0, magNormList_0, internalAvList_0, galacticAvList_0, redshiftList_0)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) testList.flush() sedNameList_1 = self.getListOfSedNames(nSed//2) magNormList_1 = self.rng.random_sample(nSed//2)*5.0 + 15.0 internalAvList_1 = self.rng.random_sample(nSed//2)*0.3 + 0.1 redshiftList_1 = self.rng.random_sample(nSed//2)*5.0 galacticAvList_1 = self.rng.random_sample(nSed//2)*0.3 + 0.1 testList.loadSedsFromList(sedNameList_1, magNormList_1, internalAvList=internalAvList_1, galacticAvList=galacticAvList_1, redshiftList=redshiftList_1) self.assertEqual(len(testList), nSed/2) self.assertEqual(len(testList.redshiftList), nSed/2) self.assertEqual(len(testList.internalAvList), nSed/2) self.assertEqual(len(testList.galacticAvList), nSed/2) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_1)): self.assertAlmostEqual(internalAvList_1[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix], 10) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_1, magNormList_1, internalAvList_1, galacticAvList_1, redshiftList_1)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, imsimBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
def 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)
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 testAlternateNormalizingBandpass(self): """ A reiteration of testAddingToList, but testing with a non-imsimBandpass normalizing bandpass """ normalizingBand = Bandpass() normalizingBand.readThroughput(os.path.join(getPackageDir('throughputs'), 'baseline', 'total_r.dat')) nSed = 10 sedNameList_0 = self.getListOfSedNames(nSed) magNormList_0 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_0 = self.rng.random_sample(nSed)*5.0 galacticAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1 wavelen_match = np.arange(300.0, 1500.0, 10.0) testList = SedList(sedNameList_0, magNormList_0, fileDir=self.sedDir, normalizingBandpass=normalizingBand, internalAvList=internalAvList_0, redshiftList=redshiftList_0, galacticAvList=galacticAvList_0, wavelenMatch=wavelen_match) sedNameList_1 = self.getListOfSedNames(nSed) magNormList_1 = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList_1 = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList_1 = self.rng.random_sample(nSed)*5.0 galacticAvList_1 = self.rng.random_sample(nSed)*0.3 + 0.1 testList.loadSedsFromList(sedNameList_1, magNormList_1, internalAvList=internalAvList_1, galacticAvList=galacticAvList_1, redshiftList=redshiftList_1) self.assertEqual(len(testList), 2*nSed) np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch) for ix in range(len(sedNameList_0)): self.assertAlmostEqual(internalAvList_0[ix], testList.internalAvList[ix], 10) self.assertAlmostEqual(galacticAvList_0[ix], testList.galacticAvList[ix], 10) self.assertAlmostEqual(redshiftList_0[ix], testList.redshiftList[ix], 10) for ix in range(len(sedNameList_1)): self.assertAlmostEqual(internalAvList_1[ix], testList.internalAvList[ix+nSed], 10) self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix+nSed], 10) self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix+nSed], 10) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_0, magNormList_0, internalAvList_0, galacticAvList_0, redshiftList_0)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, normalizingBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu) for ix, (name, norm, iav, gav, zz) in \ enumerate(zip(sedNameList_1, magNormList_1, internalAvList_1, galacticAvList_1, redshiftList_1)): sedControl = Sed() sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz')) fnorm = sedControl.calcFluxNorm(norm, normalizingBand) sedControl.multiplyFluxNorm(fnorm) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=iav) sedControl.redshiftSED(zz, dimming=True) sedControl.resampleSED(wavelen_match=wavelen_match) a_coeff, b_coeff = sedControl.setupCCM_ab() sedControl.addDust(a_coeff, b_coeff, A_v=gav) sedTest = testList[ix+nSed] np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen) np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda) np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
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 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)
for f in filterlist: lsstbp[f].sbTophi() bplist.append(lsstbp[f]) phiarray, dlambda = tmpgal.setupPhiArray(bplist) # Set up dictionary + arrays to hold calculated magnitude information. mags2 = {} for f in filterlist: mags2[f] = numpy.zeros(num_gal, dtype='float') # For each galaxy (in num_gal's), apply internal dust, redshift, resample to 300-1200 nm, apply MW dust on # shorter (and standardized) wavelength range, fluxnorm, and then calculate mags using manyMagCalc. for i in range(num_gal): galname = gallist[gal_name[i]] tmpgal = Sed(wavelen=gals[galname].wavelen, flambda=gals[galname].flambda) tmpgal.addCCMDust(a_int, b_int, ebv=ebv_int[i]) tmpgal.redshiftSED(redshifts[i]) tmpgal.resampleSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=wavelen_step) tmpgal.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i]) tmpgal.multiplyFluxNorm(fluxnorm[i]) #tmpgal.flambdaTofnu() #Not needed because multiplyFluxNorm calculates fnu tmpmags = tmpgal.manyMagCalc(phiarray, dlambda) j = 0 for f in filterlist: mags2[f][i] = tmpmags[j] j = j+1 dt, t = dtime(t) print "Calculating dust/redshift/dust/fluxnorm/%d magnitudes for %d galaxies optimized way took %f s" \ %(len(filterlist), num_gal, dt) # Check for differences in magnitudes. import pylab