def test_norm(self): """ Test that the special test case getImsimFluxNorm returns the same value as calling calcFluxNorm actually passing in the imsim Bandpass """ bp = Bandpass() bp.imsimBandpass() rng = np.random.RandomState(1123) wavelen = np.arange(300.0, 2000.0, 0.17) for ix in range(10): flux = rng.random_sample(len(wavelen)) * 100.0 sed = Sed() sed.setSED(wavelen=wavelen, flambda=flux) magmatch = rng.random_sample() * 5.0 + 10.0 control = sed.calcFluxNorm(magmatch, bp) test = getImsimFluxNorm(sed, magmatch) # something about how interpolation is done in Sed means # that the values don't come out exactly equal. They come # out equal to 8 seignificant digits, though. self.assertEqual(control, test)
def calcBasicColors(self, sedList, bandpassDict, makeCopy=False): """ This will calculate a set of colors from a list of SED objects when there is no need to redshift the SEDs. @param [in] sedList is the set of spectral objects from the models SEDs provided by loaders in rgStar or rgGalaxy. NOTE: Since this uses photometryBase.manyMagCalc_list the SED objects will be changed. @param [in] bandpassDict is a BandpassDict class instance with the Bandpasses set to those for the magnitudes given for the catalog object @param [in] makeCopy indicates whether or not to operate on copies of the SED objects in sedList since this method will change the wavelength grid. @param [out] modelColors is the set of colors in the Bandpasses provided for the given sedList. """ modelColors = [] for specObj in sedList: if makeCopy == True: fileSED = Sed() fileSED.setSED(wavelen=specObj.wavelen, flambda=specObj.flambda) sEDMags = bandpassDict.magListForSed(fileSED) else: sEDMags = bandpassDict.magListForSed(specObj) colorInfo = [] for filtNum in range(0, len(bandpassDict) - 1): colorInfo.append(sEDMags[filtNum] - sEDMags[filtNum + 1]) modelColors.append(colorInfo) return modelColors
def test_norm(self): """ Test that the special test case getImsimFluxNorm returns the same value as calling calcFluxNorm actually passing in the imsim Bandpass """ bp = Bandpass() bp.imsimBandpass() rng = np.random.RandomState(1123) wavelen = np.arange(300.0, 2000.0, 0.17) for ix in range(10): flux = rng.random_sample(len(wavelen))*100.0 sed = Sed() sed.setSED(wavelen=wavelen, flambda=flux) magmatch = rng.random_sample()*5.0 + 10.0 control = sed.calcFluxNorm(magmatch, bp) test = getImsimFluxNorm(sed, magmatch) # something about how interpolation is done in Sed means # that the values don't come out exactly equal. They come # out equal to 8 seignificant digits, though. self.assertEqual(control, test)
def calcBasicColors(self, sedList, bandpassDict, makeCopy = False): """ This will calculate a set of colors from a list of SED objects when there is no need to redshift the SEDs. @param [in] sedList is the set of spectral objects from the models SEDs provided by loaders in rgStar or rgGalaxy. NOTE: Since this uses photometryBase.manyMagCalc_list the SED objects will be changed. @param [in] bandpassDict is a BandpassDict class instance with the Bandpasses set to those for the magnitudes given for the catalog object @param [in] makeCopy indicates whether or not to operate on copies of the SED objects in sedList since this method will change the wavelength grid. @param [out] modelColors is the set of colors in the Bandpasses provided for the given sedList. """ modelColors = [] for specObj in sedList: if makeCopy==True: fileSED = Sed() fileSED.setSED(wavelen = specObj.wavelen, flambda = specObj.flambda) sEDMags = bandpassDict.magListForSed(fileSED) else: sEDMags = bandpassDict.magListForSed(specObj) colorInfo = [] for filtNum in range(0, len(bandpassDict)-1): colorInfo.append(sEDMags[filtNum] - sEDMags[filtNum+1]) modelColors.append(colorInfo) return modelColors
def returnMags(self, bandpass=None): """ Convert the computed spectra to magnitudes using the supplied bandpasses, or, if self.mags=True, just return the mags in the LSST filters If mags=True when initialized, return mags returns an structured array with dtype names u,g,r,i,z,y. """ if self.mags: if bandpass: warnings.warn('Ignoring set bandpasses and returning LSST ugrizy.') mags = -2.5*np.log10(self.spec)+np.log10(3631.) # Mask out high airmass mags[self.mask] *= np.nan # Convert to a structured array mags = np.core.records.fromarrays(mags.transpose(), names='u,g,r,i,z,y', formats='float,'*6) else: mags = np.zeros(self.npts, dtype=float)-666 tempSed = Sed() isThrough = np.where(bandpass.sb > 0) minWave = bandpass.wavelen[isThrough].min() maxWave = bandpass.wavelen[isThrough].max() inBand = np.where((self.wave >= minWave) & (self.wave <= maxWave)) for i, ra in enumerate(self.ra): # Check that there is flux in the band, otherwise calcMag fails if np.max(self.spec[i, inBand]) > 0: tempSed.setSED(self.wave, flambda=self.spec[i, :]) mags[i] = tempSed.calcMag(bandpass) # Mask out high airmass mags[self.mask] *= np.nan return mags
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 get_mags(self,source, phase ): sed = Sed() print "===========================================" print phase print "===========================================" if phase > -20 and phase < 50 : sourceflux = source.flux(phase=phase, wave=self.rband.wavelen*10.) sed.setSED(wavelen=self.rband.wavelen, flambda=sourceflux/10.) else: sed.setSED(wavelen=self.rband.wavelen, flambda=flambda) #sed.redshiftSED(redshift=_z[i], dimming=True) return [sed.calcMag(bandpass=self.uband), sed.calcMag(bandpass=self.gband), sed.calcMag(bandpass=self.rband), sed.calcMag(bandpass=self.iband), sed.calcMag(bandpass=self.zband)]
def returnMags(self, bandpasses=None): """ Convert the computed spectra to a magnitude using the supplied bandpass, or, if self.mags=True, return the mags in the LSST filters If mags=True when initialized, return mags returns an structured array with dtype names u,g,r,i,z,y. bandpasses: optional dictionary with bandpass name keys and bandpass object values. """ if self.azs is None: raise ValueError( 'No coordinates set. Use setRaDecMjd, setRaDecAltAzMjd, or setParams methods before calling returnMags.' ) if self.mags: if bandpasses: warnings.warn( 'Ignoring set bandpasses and returning LSST ugrizy.') mags = -2.5 * np.log10(self.spec) + np.log10(3631.) # Mask out high airmass mags[self.mask] *= np.nan mags = mags.swapaxes(0, 1) magsBack = {} for i, f in enumerate(self.filterNames): magsBack[f] = mags[i] else: magsBack = {} for key in bandpasses: mags = np.zeros(self.npts, dtype=float) - 666 tempSed = Sed() isThrough = np.where(bandpasses[key].sb > 0) minWave = bandpasses[key].wavelen[isThrough].min() maxWave = bandpasses[key].wavelen[isThrough].max() inBand = np.where((self.wave >= minWave) & (self.wave <= maxWave)) for i, ra in enumerate(self.ra): # Check that there is flux in the band, otherwise calcMag fails if np.max(self.spec[i, inBand]) > 0: tempSed.setSED(self.wave, flambda=self.spec[i, :]) mags[i] = tempSed.calcMag(bandpasses[key]) # Mask out high airmass mags[self.mask] *= np.nan magsBack[key] = mags return magsBack
def computeMags(self, bandpass=None): """After the spectra have been computed, optionally convert to mags""" if self.mags: mags = -2.5 * np.log10(self.spec) + np.log10(3631.) else: mags = np.zeros(self.npts, dtype=float) - 666 tempSed = Sed() isThrough = np.where(bandpass.sb > 0) minWave = bandpass.wavelen[isThrough].min() maxWave = bandpass.wavelen[isThrough].max() inBand = np.where((self.wave >= minWave) & (self.wave <= maxWave)) for i, ra in enumerate(self.ra): if np.max(self.spec[i, inBand]) > 0: tempSed.setSED(self.wave, flambda=self.spec[i, :]) # Need to try/except because the spectra might be zero in the filter # XXX-upgrade this to check if it's zero mags[i] = tempSed.calcMag(bandpass) return mags
def computeMags(self, bandpass=None): """After the spectra have been computed, optionally convert to mags""" if self.mags: mags = -2.5*np.log10(self.spec)+np.log10(3631.) else: mags = np.zeros(self.npts, dtype=float)-666 tempSed = Sed() isThrough = np.where(bandpass.sb > 0) minWave = bandpass.wavelen[isThrough].min() maxWave = bandpass.wavelen[isThrough].max() inBand = np.where( (self.wave >= minWave) & (self.wave <= maxWave)) for i, ra in enumerate(self.ra): if np.max(self.spec[i,inBand]) > 0: tempSed.setSED(self.wave, flambda=self.spec[i,:]) # Need to try/except because the spectra might be zero in the filter # XXX-upgrade this to check if it's zero mags[i] = tempSed.calcMag(bandpass) return mags
def returnMags(self, bandpasses=None): """ Convert the computed spectra to a magnitude using the supplied bandpass, or, if self.mags=True, return the mags in the LSST filters If mags=True when initialized, return mags returns an structured array with dtype names u,g,r,i,z,y. bandpasses: optional dictionary with bandpass name keys and bandpass object values. """ if self.azs is None: raise ValueError('No coordinates set. Use setRaDecMjd, setRaDecAltAzMjd, or setParams methods before calling returnMags.') if self.mags: if bandpasses: warnings.warn('Ignoring set bandpasses and returning LSST ugrizy.') mags = -2.5*np.log10(self.spec)+np.log10(3631.) # Mask out high airmass mags[self.mask] *= np.nan mags = mags.swapaxes(0, 1) magsBack = {} for i, f in enumerate(self.filterNames): magsBack[f] = mags[i] else: magsBack = {} for key in bandpasses: mags = np.zeros(self.npts, dtype=float)-666 tempSed = Sed() isThrough = np.where(bandpasses[key].sb > 0) minWave = bandpasses[key].wavelen[isThrough].min() maxWave = bandpasses[key].wavelen[isThrough].max() inBand = np.where((self.wave >= minWave) & (self.wave <= maxWave)) for i, ra in enumerate(self.ra): # Check that there is flux in the band, otherwise calcMag fails if np.max(self.spec[i, inBand]) > 0: tempSed.setSED(self.wave, flambda=self.spec[i, :]) mags[i] = tempSed.calcMag(bandpasses[key]) # Mask out high airmass mags[self.mask] *= np.nan magsBack[key] = mags return magsBack
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 packageLowerAtm(): dataDir = getPackageDir('SIMS_SKYBRIGHTNESS_DATA') outDir = os.path.join(dataDir, 'ESO_Spectra/LowerAtm') # Read in all the spectra from ESO call and package into a single npz file files = glob.glob('LowerAtm/skytable*.fits') temp = pyfits.open(files[0]) wave = temp[1].data['lam'].copy()*1e3 airmasses = [] nightTimes = [] specs = [] for i,filename in enumerate(files): fits = pyfits.open(filename) if np.max(fits[1].data['flux']) > 0: specs.append(fits[1].data['flux'].copy()) header = fits[0].header['comment'] for card in header: if 'SKYMODEL.TARGET.AIRMASS' in card: airmasses.append(float(card.split('=')[-1])) elif 'SKYMODEL.TIME' in card: nightTimes.append(float(card.split('=')[-1])) airmasses = np.array(airmasses) nigtTimes = np.array(nightTimes) nrec = airmasses.size nwave = wave.size dtype = [('airmass', 'float'), ('nightTimes', 'float'), ('spectra', 'float', (nwave)), ('mags', 'float', (6))] Spectra = np.zeros(nrec, dtype=dtype) Spectra['airmass'] = airmasses Spectra['nightTimes'] = nightTimes Spectra['spectra'] = specs hPlank = 6.626068e-27 # erg s cLight = 2.99792458e10 # cm/s # Convert spectra from ph/s/m2/micron/arcsec2 to erg/s/cm2/nm/arcsec2 Spectra['spectra'] = Spectra['spectra']/(100.**2)*hPlank*cLight/(wave*1e-7)/1e3 # Sort things since this might be helpful later Spectra.sort(order=['airmass','nightTimes']) # Load LSST filters throughPath = os.path.join(getPackageDir('throughputs'),'baseline') keys = ['u','g','r','i','z','y'] nfilt = len(keys) filters = {} for filtername in keys: bp = np.loadtxt(os.path.join(throughPath, 'filter_'+filtername+'.dat'), dtype=zip(['wave','trans'],[float]*2 )) tempB = Bandpass() tempB.setBandpass(bp['wave'],bp['trans']) filters[filtername] = tempB filterWave = np.array([filters[f].calcEffWavelen()[0] for f in keys ]) for i,spectrum in enumerate(Spectra['spectra']): tempSed = Sed() tempSed.setSED(wave,flambda=spectrum) for j,filtName in enumerate(keys): try: Spectra['mags'][i][j] = tempSed.calcMag(filters[filtName]) except: pass np.savez(os.path.join(outDir,'Spectra.npz'), wave = wave, spec=Spectra, filterWave=filterWave)
def packageZodiacal(): dataDir = getPackageDir('SIMS_SKYBRIGHTNESS_DATA') outDir = os.path.join(dataDir, 'ESO_Spectra/Zodiacal') nside = 4 # Read in all the spectra from ESO call and package into a single npz file files = glob.glob('Zodiacal/skytable*.fits') temp = pyfits.open(files[0]) wave = temp[1].data['lam'].copy()*1e3 airmasses = [] eclLon = [] eclLat = [] specs = [] for i,filename in enumerate(files): fits = pyfits.open(filename) if np.max(fits[1].data['flux']) > 0: specs.append(fits[1].data['flux'].copy()) header = fits[0].header['comment'] for card in header: if 'SKYMODEL.TARGET.AIRMASS' in card: airmasses.append(float(card.split('=')[-1])) elif 'SKYMODEL.ECL.LON' in card: eclLon.append(float(card.split('=')[-1])) elif 'SKYMODEL.ECL.LAT' in card: eclLat.append(float(card.split('=')[-1])) airmasses = np.array(airmasses) eclLon = np.array(eclLon) eclLat = np.array(eclLat) wrapA = np.where(eclLon < 0.) eclLon[wrapA] = eclLon[wrapA]+360. uAM = np.unique(airmasses) nAM = uAM.size nwave = wave.size dtype = [('airmass', 'float'), ('hpid', 'int' ), ('spectra', 'float', (nwave)), ('mags', 'float', (6))] npix = hp.nside2npix(nside) Spectra = np.zeros(nAM*npix, dtype=dtype) for i,am in enumerate(uAM): Spectra['airmass'][i*npix:i*npix+npix] = am Spectra['hpid'][i*npix:i*npix+npix] = np.arange(npix) for am, lat, lon, spec in zip(airmasses,eclLat, eclLon, specs): hpid = hp.ang2pix(nside, np.radians(lat+90.), np.radians(lon) ) good = np.where( (Spectra['airmass'] == am) & (Spectra['hpid'] == hpid)) Spectra['spectra'][good] = spec.copy() hPlank = 6.626068e-27 # erg s cLight = 2.99792458e10 # cm/s # Convert spectra from ph/s/m2/micron/arcsec2 to erg/s/cm2/nm/arcsec2 Spectra['spectra'] = Spectra['spectra']/(100.**2)*hPlank*cLight/(wave*1e-7)/1e3 # Sort things since this might be helpful later Spectra.sort(order=['airmass', 'hpid']) # Load LSST filters throughPath = os.path.join(getPackageDir('throughputs'),'baseline') keys = ['u','g','r','i','z','y'] nfilt = len(keys) filters = {} for filtername in keys: bp = np.loadtxt(os.path.join(throughPath, 'filter_'+filtername+'.dat'), dtype=zip(['wave','trans'],[float]*2 )) tempB = Bandpass() tempB.setBandpass(bp['wave'],bp['trans']) filters[filtername] = tempB filterWave = np.array([filters[f].calcEffWavelen()[0] for f in keys ]) for i,spectrum in enumerate(Spectra['spectra']): tempSed = Sed() tempSed.setSED(wave,flambda=spectrum) for j,filtName in enumerate(keys): try: Spectra['mags'][i][j] = tempSed.calcMag(filters[filtName]) except: pass #span this over multiple files to store in github nbreak = 3 nrec = np.size(Spectra) for i in np.arange(nbreak): np.savez(os.path.join(outDir,'zodiacalSpectra_'+str(i)+'.npz'), wave = wave, spec=Spectra[i*nrec/nbreak:(i+1)*nrec/nbreak], filterWave=filterWave)
def packageMoon(): dataDir = getPackageDir('SIMS_SKYBRIGHTNESS_DATA') outDir = os.path.join(dataDir, 'ESO_Spectra/Moon') # Read in all the spectra from ESO call and package into a single npz file files = glob.glob('Moon/skytable*.fits') temp = pyfits.open(files[0]) moonWave = temp[1].data['lam'].copy()*1e3 moonSpec = [] moonAM = [] # Actually target airmass moonAlt=[] # altitude of the moon moonSunSep=[] # moon Phase moonTargetSep=[] hpid=[] for i,filename in enumerate(files): fits = pyfits.open(filename) try: if np.max(fits[1].data['flux']) > 0: moonSpec.append(fits[1].data['flux'].copy()) header = fits[0].header['comment'] for card in header: if 'SKYMODEL.MOON.SUN.SEP' in card: moonSunSep.append(float(card.split('=')[-1])) elif 'SKYMODEL.TARGET.AIRMASS' in card: #moonAM.append( 1./np.cos(np.radians(90.-float(card.split('=')[-1]))) ) moonAM.append( float(card.split('=')[-1]) ) elif 'SKYMODEL.MOON.TARGET.SEP' in card: moonTargetSep.append(float(card.split('=')[-1])) elif 'SKYMODEL.MOON.ALT' in card: moonAlt.append(float(card.split('=')[-1])) except: print filename, ' Failed' import healpy as hp from lsst.sims.utils import haversine nside = 4 lat, az = hp.pix2ang(nside, np.arange(hp.nside2npix(nside))) alt = np.pi/2.-lat airmass = 1./np.cos(np.pi/2.-alt) # Only need low airmass and then 1/2 to sky good = np.where( (az >= 0) & (az <= np.pi) & (airmass <=2.6) & (airmass >= 1.) ) airmass = airmass[good] alt=alt[good] az = az[good] moonAM = np.array(moonAM) moonAlt = np.array(moonAlt) moonSunSep = np.array(moonSunSep) moonTargetSep = np.array(moonTargetSep) moonAzDiff = moonTargetSep*0 targetAlt = np.pi/2.-np.arccos(1./moonAM) # Compute the azimuth difference given the moon-target-seperation # Let's just do a stupid loop: for i in np.arange(targetAlt.size): possibleDistances = haversine(0., np.radians(moonAlt[i]), az, az*0+targetAlt[i]) diff = np.abs(possibleDistances - np.radians(moonTargetSep[i])) good = np.where(diff == diff.min()) moonAzDiff[i] = az[good][0] # ok, now I have an alt and az, I can convert that back to a healpix id. hpid.append(hp.ang2pix(nside, np.pi/2.-targetAlt[i], moonAzDiff[i])) nrec = moonAM.size nwave = moonWave.size dtype = [('hpid', 'int'), ('moonAltitude', 'float'), ('moonSunSep', 'float'), ('spectra', 'float', (nwave)), ('mags', 'float', (6))] moonSpectra = np.zeros(nrec, dtype=dtype) moonSpectra['hpid'] = hpid moonSpectra['moonAltitude'] = moonAlt moonSpectra['moonSunSep'] = moonSunSep moonSpectra['spectra'] = moonSpec hPlank = 6.626068e-27 # erg s cLight = 2.99792458e10 # cm/s # Convert spectra from ph/s/m2/micron/arcsec2 to erg/s/cm2/nm/arcsec2 moonSpectra['spectra'] = moonSpectra['spectra']/(100.**2)*hPlank*cLight/(moonWave*1e-7)/1e3 # Sort things since this might be helpful later moonSpectra.sort(order=['moonSunSep','moonAltitude', 'hpid']) # Crop off the incomplete ones good =np.where((moonSpectra['moonAltitude'] >= 0) & (moonSpectra['moonAltitude'] < 89.) ) moonSpectra = moonSpectra[good] # Load LSST filters throughPath = os.path.join(getPackageDir('throughputs'),'baseline') keys = ['u','g','r','i','z','y'] nfilt = len(keys) filters = {} for filtername in keys: bp = np.loadtxt(os.path.join(throughPath, 'filter_'+filtername+'.dat'), dtype=zip(['wave','trans'],[float]*2 )) tempB = Bandpass() tempB.setBandpass(bp['wave'],bp['trans']) filters[filtername] = tempB filterWave = np.array([filters[f].calcEffWavelen()[0] for f in keys ]) for i,spectrum in enumerate(moonSpectra['spectra']): tempSed = Sed() tempSed.setSED(moonWave,flambda=spectrum) for j,filtName in enumerate(keys): try: moonSpectra['mags'][i][j] = tempSed.calcMag(filters[filtName]) except: pass nbreak=5 nrec = np.size(moonSpectra) for i in np.arange(nbreak): np.savez(os.path.join(outDir,'moonSpectra_'+str(i)+'.npz'), wave = moonWave, spec=moonSpectra[i*nrec/nbreak:(i+1)*nrec/nbreak], filterWave=filterWave)
def matchToObserved(self, sedList, catMags, catRedshifts, catRA = None, catDec = None, mag_error = None, bandpassDict = None, dzAcc = 2, reddening = True, extCoeffs = (4.239, 3.303, 2.285, 1.698, 1.263)): """ This will find the closest match to the magnitudes of a galaxy catalog if those magnitudes are in the observed frame and can correct for reddening from within the milky way as well if needed. In order to make things faster it first calculates colors for all model SEDs at redshifts between the minimum and maximum redshifts of the catalog objects provided with a grid spacing in redshift defined by the parameter dzAcc. Objects without magnitudes in at least two adjacent bandpasses will return as none and print out a message. @param [in] sedList is the set of spectral objects from the models SEDs provided by loadBC03 or other custom loader routine. @param [in] catMags is an array of the magnitudes of catalog objects to be matched with a model SED. It should be organized so that there is one object's magnitudes along each row. @param [in] catRedshifts is an array of the redshifts of each catalog object. @param [in] catRA is an array of the RA positions for each catalog object. @param [in] catDec is an array of the Dec position for each 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 size as catMags. @param [in] bandpassDict is a BandpassDict with which to calculate magnitudes. If left equal to None it will by default load the SDSS [u,g,r,i,z] bandpasses and therefore agree with default extCoeffs. @param [in] dzAcc is the number of decimal places you want to use when building the redshift grid. For example, dzAcc = 2 will create a grid between the minimum and maximum redshifts with colors calculated at every 0.01 change in redshift. @param [in] reddening is a boolean that determines whether to correct catalog magnitudes for dust in the milky way. By default, it is True. If true, this uses calculateEBV from EBV.py to find an EBV value for the object's ra and dec coordinates and then uses the coefficients provided by extCoeffs which should come from Schlafly and Finkbeiner (2011) for the correct filters and in the same order as provided in bandpassDict. If false, this means it will not run the dereddening procedure. @param [in] extCoeffs are the Schlafly and Finkbeiner (2011) (ApJ, 737, 103) coefficients for the given filters from bandpassDict and need to be in the same order as bandpassDict. The default given are the SDSS [u,g,r,i,z] values. @param [out] sedMatches is a list with the name of a model SED that matches most closely to each object in the catalog. @param [out] magNormMatches are the magnitude normalizations for the given magnitudes and matched SED. @param [out] matchErrors contains the Mean Squared Error between the colors of each object and the colors of the matched SED. """ #Set up photometry to calculate model Mags if bandpassDict is None: galPhot = BandpassDict.loadTotalBandpassesFromFiles(['u','g','r','i','z'], bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),'sdss'), bandpassRoot = 'sdss_') else: galPhot = bandpassDict #Calculate ebv from ra, dec coordinates if needed if reddening == True: #Check that catRA and catDec are included if catRA is None or catDec is None: raise RuntimeError("Reddening is True, but catRA and catDec are not included.") calcEBV = ebv() raDec = np.array((catRA,catDec)) #If only matching one object need to reshape for calculateEbv if len(raDec.shape) == 1: raDec = raDec.reshape((2,1)) ebvVals = calcEBV.calculateEbv(equatorialCoordinates = raDec) objMags = self.deReddenMags(ebvVals, catMags, extCoeffs) else: objMags = catMags minRedshift = np.round(np.min(catRedshifts), dzAcc) maxRedshift = np.round(np.max(catRedshifts), dzAcc) dz = np.power(10., (-1*dzAcc)) redshiftRange = np.round(np.arange(minRedshift - dz, maxRedshift + (2*dz), dz), dzAcc) numRedshifted = 0 sedMatches = [None] * len(catRedshifts) magNormMatches = [None] * len(catRedshifts) matchErrors = [None] * len(catRedshifts) redshiftIndex = np.argsort(catRedshifts) numOn = 0 notMatched = 0 lastRedshift = -100 print('Starting Matching. Arranged by redshift value.') for redshift in redshiftRange: if numRedshifted % 10 == 0: print('%i out of %i redshifts gone through' % (numRedshifted, len(redshiftRange))) numRedshifted += 1 colorSet = [] for galSpec in sedList: sedColors = [] fileSED = Sed() fileSED.setSED(wavelen = galSpec.wavelen, flambda = galSpec.flambda) fileSED.redshiftSED(redshift) sedColors = self.calcBasicColors([fileSED], galPhot, makeCopy = True) colorSet.append(sedColors) colorSet = np.transpose(colorSet) for currentIndex in redshiftIndex[numOn:]: matchMags = objMags[currentIndex] if lastRedshift < np.round(catRedshifts[currentIndex],dzAcc) <= redshift: colorRange = np.arange(0, len(galPhot)-1) matchColors = [] for colorNum in colorRange: matchColors.append(matchMags[colorNum] - matchMags[colorNum+1]) #This is done to handle objects with incomplete magnitude data filtNums = np.arange(0, len(galPhot)) if np.isnan(np.amin(matchColors))==True: colorRange = np.where(np.isnan(matchColors)==False)[0] filtNums = np.unique([colorRange, colorRange+1]) #Pick right filters in calcMagNorm if len(colorRange) == 0: print('Could not match object #%i. No magnitudes for two adjacent bandpasses.' \ % (currentIndex)) notMatched += 1 #Don't need to assign 'None' here in result array, b/c 'None' is default value else: distanceArray = [np.zeros(len(sedList))] for colorNum in colorRange: distanceArray += np.power((colorSet[colorNum] - matchColors[colorNum]),2) matchedSEDNum = np.nanargmin(distanceArray) sedMatches[currentIndex] = sedList[matchedSEDNum].name magNormVal = self.calcMagNorm(np.array(matchMags), sedList[matchedSEDNum], galPhot, mag_error = mag_error, redshift = catRedshifts[currentIndex], filtRange = filtNums) magNormMatches[currentIndex] = magNormVal matchErrors[currentIndex] = (distanceArray[0,matchedSEDNum]/len(colorRange)) numOn += 1 else: break lastRedshift = redshift print('Done Matching. Matched %i of %i catalog objects to SEDs' % (len(catMags)-notMatched, len(catMags))) if notMatched > 0: print('%i objects did not get matched.' % (notMatched)) return sedMatches, magNormMatches, matchErrors
throughPath = os.getenv('LSST_THROUGHPUTS_BASELINE') keys = ['u', 'g', 'r', 'i', 'z', 'y'] nfilt = len(keys) filters = {} for filtername in keys: bp = np.loadtxt(os.path.join(throughPath, 'filter_' + filtername + '.dat'), dtype=zip(['wave', 'trans'], [float] * 2)) tempB = Bandpass() tempB.setBandpass(bp['wave'], bp['trans']) filters[filtername] = tempB filterWave = np.array([filters[f].calcEffWavelen()[0] for f in keys]) for i, spectrum in enumerate(moonSpectra['spectra']): tempSed = Sed() tempSed.setSED(moonWave, flambda=spectrum) for j, filtName in enumerate(keys): try: moonSpectra['mags'][i][j] = tempSed.calcMag(filters[filtName]) except: pass nbreak = 5 nrec = np.size(moonSpectra) for i in np.arange(nbreak): np.savez(os.path.join(outDir, 'moonSpectra_' + str(i) + '.npz'), wave=moonWave, spec=moonSpectra[i * nrec / nbreak:(i + 1) * nrec / nbreak], filterWave=filterWave)
# Sort things since this might be helpful later Spectra.sort(order=['airmass', 'nightTimes']) # Load LSST filters throughPath = os.getenv('LSST_THROUGHPUTS_BASELINE') keys = ['u', 'g', 'r', 'i', 'z', 'y'] nfilt = len(keys) filters = {} for filtername in keys: bp = np.loadtxt(os.path.join(throughPath, 'filter_' + filtername + '.dat'), dtype=list(zip(['wave', 'trans'], [float] * 2))) tempB = Bandpass() tempB.setBandpass(bp['wave'], bp['trans']) filters[filtername] = tempB filterWave = np.array([filters[f].calcEffWavelen()[0] for f in keys]) for i, spectrum in enumerate(Spectra['spectra']): tempSed = Sed() tempSed.setSED(wave, flambda=spectrum) for j, filtName in enumerate(keys): try: Spectra['mags'][i][j] = tempSed.calcMag(filters[filtName]) except: pass np.savez(os.path.join(outDir, 'Spectra.npz'), wave=wave, spec=Spectra, filterWave=filterWave)
throughPath = os.getenv('LSST_THROUGHPUTS_BASELINE') keys = ['u', 'g', 'r', 'i', 'z', 'y'] nfilt = len(keys) filters = {} for filtername in keys: bp = np.loadtxt(os.path.join(throughPath, 'filter_'+filtername+'.dat'), dtype=list(zip(['wave', 'trans'], [float]*2))) tempB = Bandpass() tempB.setBandpass(bp['wave'], bp['trans']) filters[filtername] = tempB filterWave = np.array([filters[f].calcEffWavelen()[0] for f in keys]) for i, spectrum in enumerate(moonSpectra['spectra']): tempSed = Sed() tempSed.setSED(moonWave, flambda=spectrum) for j, filtName in enumerate(keys): try: moonSpectra['mags'][i][j] = tempSed.calcMag(filters[filtName]) except: pass nbreak = 5 nrec = np.size(moonSpectra) for i in np.arange(nbreak): np.savez(os.path.join(outDir, 'moonSpectra_'+str(i)+'.npz'), wave=moonWave, spec=moonSpectra[i*nrec/nbreak:(i+1)*nrec/nbreak], filterWave=filterWave)
# Sort things since this might be helpful later Spectra.sort(order=['airmass', 'nightTimes']) # Load LSST filters throughPath = os.getenv('LSST_THROUGHPUTS_BASELINE') keys = ['u', 'g', 'r', 'i', 'z', 'y'] nfilt = len(keys) filters = {} for filtername in keys: bp = np.loadtxt(os.path.join(throughPath, 'filter_'+filtername+'.dat'), dtype=list(zip(['wave', 'trans'], [float]*2))) tempB = Bandpass() tempB.setBandpass(bp['wave'], bp['trans']) filters[filtername] = tempB filterWave = np.array([filters[f].calcEffWavelen()[0] for f in keys]) for i, spectrum in enumerate(Spectra['spectra']): tempSed = Sed() tempSed.setSED(wave, flambda=spectrum) for j, filtName in enumerate(keys): try: Spectra['mags'][i][j] = tempSed.calcMag(filters[filtName]) except: pass np.savez(os.path.join(outDir, 'Spectra.npz'), wave=wave, spec=Spectra, filterWave=filterWave)
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 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)