示例#1
0
 def testRedshiftName(self):
     testsed = Sed(self.testsed.wavelen, self.testsed.flambda, name=self.testsed.name)
     redshift = .2
     testsed.redshiftSED(redshift=redshift)
     newname = testsed.name + '_Z' + '%.2f' % (redshift)
     testsed.name = newname
     self.assertEqual(testsed.name, newname)
def get_sed(name, magnorm, redshift, av, rv):
    if not hasattr(get_sed, '_rest_dict'):
        get_sed._rest_dict = {}
        get_sed._imsim_bp = Bandpass()
        get_sed._imsim_bp.imsimBandpass()
        get_sed._sed_dir = os.environ['SIMS_SED_LIBRARY_DIR']
        get_sed._ccm_w = None

    tag = '%s_%.2f_%.2f' % (name, av, rv)
    if tag not in get_sed._rest_dict:
        ss = Sed()
        ss.readSED_flambda(os.path.join(get_sed._sed_dir, name))
        if get_sed._ccm_w is None or not np.array_equal(
                ss.wavelen, get_sed._ccm_w):
            get_sed._ccm_w = np.copy(ss.wavelen)
            get_sed._ax, get_sed._bx = ss.setupCCM_ab()

        mn = ss.calcMag(get_sed._imsim_bp)
        ss.addDust(get_sed._ax, get_sed._bx, A_v=av, R_v=rv)
        get_sed._rest_dict[tag] = (ss, mn)

    base_sed = get_sed._rest_dict[tag][0]
    ss = Sed(wavelen=base_sed.wavelen, flambda=base_sed.flambda)
    dmag = magnorm - get_sed._rest_dict[tag][1]
    fnorm = np.power(10.0, -0.4 * dmag)
    ss.multiplyFluxNorm(fnorm)
    ss.redshiftSED(redshift, dimming=True)
    return ss
def _fluxes(sed_name, mag_norm, redshift):
    """
    Find the fluxes for a galaxy component

    Parameters
    ----------
    sed_name is an SED file name

    mag_norm is a float

    redshift is a float

    Returns
    -------
    array of fluxes in ugrizy order
    """
    if not hasattr(_fluxes, '_bp_dict'):
        bp_dir = getPackageDir('throughputs')
        bp_dir = os.path.join(bp_dir, 'imsim', 'goal')
        _fluxes._bp_dict = BandpassDict.loadTotalBandpassesFromFiles(
            bandpassDir=bp_dir)

        _fluxes._sed_dir = getPackageDir('sims_sed_library')

    spec = Sed()
    full_sed_name = os.path.join(_fluxes._sed_dir, sed_name)
    if not os.path.isfile(full_sed_name):
        full_sed_name = os.path.join(_fluxes._sed_dir,
                                     defaultSpecMap[sed_name])
    spec.readSED_flambda(full_sed_name)
    fnorm = getImsimFluxNorm(spec, mag_norm)
    spec.multiplyFluxNorm(fnorm)
    spec.redshiftSED(redshift, dimming=True)
    return _fluxes._bp_dict.fluxListForSed(spec)
示例#4
0
    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
示例#5
0
 def testRedshiftName(self):
     testsed = Sed(self.testsed.wavelen,
                   self.testsed.flambda,
                   name=self.testsed.name)
     redshift = .2
     testsed.redshiftSED(redshift=redshift)
     newname = testsed.name + '_Z' + '%.2f' % (redshift)
     testsed.name = newname
     self.assertEqual(testsed.name, newname)
def _parallel_fitting(mag_array, redshift, redshift_true, H0, Om0, wav_min,
                      wav_width, lsst_mag_array, out_dict, tag):
    pid = os.getpid()
    (sed_names, mag_norms, av_arr,
     rv_arr) = sed_from_galacticus_mags(mag_array, redshift, redshift_true, H0,
                                        Om0, wav_min, wav_width,
                                        lsst_mag_array)

    tot_bp_dict = BandpassDict.loadTotalBandpassesFromFiles()

    sed_dir = getPackageDir('sims_sed_library')
    lsst_fit_fluxes = np.zeros((6, len(sed_names)), dtype=float)
    t_start = time.time()

    ccm_w = None
    restframe_seds = {}
    imsim_bp = Bandpass()
    imsim_bp.imsimBandpass()
    n04_ln10 = -0.4 * np.log(10)

    for ii in range(len(sed_names)):
        av_val = av_arr[ii]
        rv_val = rv_arr[ii]

        sed_tag = '%s_%.3f_%.3f' % (sed_names[ii], av_val, rv_val)
        if sed_tag not in restframe_seds:
            rest_sed = Sed()
            rest_sed.readSED_flambda(os.path.join(sed_dir, sed_names[ii]))
            mag = rest_sed.calcMag(imsim_bp)
            if ccm_w is None or not np.array_equal(rest_sed.wavelen, ccm_w):
                ccm_w = np.copy(rest_sed.wavelen)
                ax, bx = rest_sed.setupCCM_ab()
            rest_sed.addDust(ax, bx, A_v=av_val, R_v=rv_val)
            restframe_seds[sed_tag] = (rest_sed, mag)

        for i_bp, bp in enumerate('ugrizy'):
            m_norm = mag_norms[i_bp][ii]
            if m_norm > 0.0 and not np.isfinite(m_norm):
                continue

            spec = Sed(wavelen=restframe_seds[sed_tag][0].wavelen,
                       flambda=restframe_seds[sed_tag][0].flambda)
            fnorm = np.exp(n04_ln10 * (m_norm - restframe_seds[sed_tag][1]))
            try:
                assert np.isfinite(fnorm)
                assert fnorm > 0.0
            except AssertionError:
                print('\n\nmagnorm %e\n\n' % (m_norm))
                raise
            spec.multiplyFluxNorm(fnorm)
            spec.redshiftSED(redshift[ii], dimming=True)
            ff = spec.calcFlux(tot_bp_dict[bp])
            lsst_fit_fluxes[i_bp][ii] = ff

    out_dict[tag] = (sed_names, mag_norms, av_arr, rv_arr, lsst_fit_fluxes)
示例#7
0
def calcADUwrapper(sedName=None,
                   magNorm=None,
                   redshift=None,
                   internalAv=None,
                   internalRv=None,
                   galacticAv=None,
                   galacticRv=None,
                   bandpass=None):
    """
    Read in an SED and calculat the number of ADU produced by that SED in a specified bandpass

    Parameters
    ----------
    sedName is a string specifying the file name of the SED

    magNorm is the normalizing magnitude of the SED in the imsimBandpass

    redshift is the redshift of the SED

    internalAv is the Av due to internal dust of the source (if a galaxy)

    internalRv is the Rv due to internal dust of the source (if a galaxy)

    galacticAv is the Av due to Milky Way dust between observer and source

    galacticRv is the Rv due to Milky Way dust between observer and source

    bandpass is an intantiation of Bandpass representing the band in which the ADUs are measured

    Returns
    -------
    A float representing the number of ADUs measured in the bandpass
    """

    imsimband = Bandpass()
    imsimband.imsimBandpass()
    sed = Sed()
    sed.readSED_flambda(sedName)
    fNorm = sed.calcFluxNorm(magNorm, imsimband)
    sed.multiplyFluxNorm(fNorm)
    if internalAv is not None and internalRv is not None:
        if internalAv != 0.0 and internalRv != 0.0:
            a_int, b_int = sed.setupCCM_ab()
            sed.addDust(a_int, b_int, A_v=internalAv, R_v=internalRv)

    if redshift is not None and redshift != 0.0:
        sed.redshiftSED(redshift, dimming=True)

    a_int, b_int = sed.setupCCM_ab()
    sed.addDust(a_int, b_int, A_v=galacticAv, R_v=galacticRv)

    adu = sed.calcADU(bandpass, photParams=PhotometricParameters())

    return adu
示例#8
0
    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 _calcSingleGalSimSed(self, sedName, zz, iAv, iRv, gAv, gRv, norm):
        """
        correct the SED for redshift, dust, etc.  Return an Sed object as defined in
        sims_photUtils/../../Sed.py
        """
        if _is_null(sedName):
            return None
        sed = Sed()
        sed.readSED_flambda(os.path.join(self.sedDir, sedName))
        imsimband = Bandpass()
        imsimband.imsimBandpass()
        # normalize the SED
        # Consulting the file sed.py in GalSim/galsim/ it appears that GalSim expects
        # its SEDs to ultimately be in units of ergs/nm so that, when called, they can
        # be converted to photons/nm (see the function __call__() and the assignment of
        # self._rest_photons in the __init__() of galsim's sed.py file).  Thus, we need
        # to read in our SEDs, normalize them, and then multiply by the exposure time
        # and the effective area to get from ergs/s/cm^2/nm to ergs/nm.
        #
        # The gain parameter should convert between photons and ADU (so: it is the
        # traditional definition of "gain" -- electrons per ADU -- multiplied by the
        # quantum efficiency of the detector).  Because we fold the quantum efficiency
        # of the detector into our total_[u,g,r,i,z,y].dat bandpass files
        # (see the readme in the THROUGHPUTS_DIR/baseline/), we only need to multiply
        # by the electrons per ADU gain.
        #
        # We will take these parameters from an instantiation of the PhotometricParameters
        # class (which can be reassigned by defining a daughter class of this class)
        #
        fNorm = sed.calcFluxNorm(norm, imsimband)
        sed.multiplyFluxNorm(fNorm)

        # apply dust extinction (internal)
        if iAv != 0.0 and iRv != 0.0:
            a_int, b_int = sed.setupCCM_ab()
            sed.addDust(a_int, b_int, A_v=iAv, R_v=iRv)

        # 22 June 2015
        # apply redshift; there is no need to apply the distance modulus from
        # sims/photUtils/CosmologyWrapper; magNorm takes that into account
        # however, magNorm does not take into account cosmological dimming
        if zz != 0.0:
            sed.redshiftSED(zz, dimming=True)

        # apply dust extinction (galactic)
        if gAv != 0.0 and gRv != 0.0:
            a_int, b_int = sed.setupCCM_ab()
            sed.addDust(a_int, b_int, A_v=gAv, R_v=gRv)
        return sed
示例#10
0
def calcADUwrapper(sedName=None, magNorm=None, redshift=None, internalAv=None, internalRv=None,
                   galacticAv=None, galacticRv=None, bandpass=None):
    """
    Read in an SED and calculat the number of ADU produced by that SED in a specified bandpass

    Parameters
    ----------
    sedName is a string specifying the file name of the SED

    magNorm is the normalizing magnitude of the SED in the imsimBandpass

    redshift is the redshift of the SED

    internalAv is the Av due to internal dust of the source (if a galaxy)

    internalRv is the Rv due to internal dust of the source (if a galaxy)

    galacticAv is the Av due to Milky Way dust between observer and source

    galacticRv is the Rv due to Milky Way dust between observer and source

    bandpass is an intantiation of Bandpass representing the band in which the ADUs are measured

    Returns
    -------
    A float representing the number of ADUs measured in the bandpass
    """

    imsimband = Bandpass()
    imsimband.imsimBandpass()
    sed = Sed()
    sed.readSED_flambda(sedName)
    fNorm = sed.calcFluxNorm(magNorm, imsimband)
    sed.multiplyFluxNorm(fNorm)
    if internalAv is not None and internalRv is not None:
        if internalAv != 0.0 and internalRv != 0.0:
            a_int, b_int = sed.setupCCM_ab()
            sed.addDust(a_int, b_int, A_v=internalAv, R_v=internalRv)

    if redshift is not None and redshift != 0.0:
        sed.redshiftSED(redshift, dimming=True)

    a_int, b_int = sed.setupCCM_ab()
    sed.addDust(a_int, b_int, A_v=galacticAv, R_v=galacticRv)

    adu = sed.calcADU(bandpass, photParams=PhotometricParameters())

    return adu
def calcADUwrapper(sedName=None, magNorm=None, redshift=None, internalAv=None, internalRv=None,
                   galacticAv=None, galacticRv=None, bandpass=None):

    imsimband = Bandpass()
    imsimband.imsimBandpass()
    sed = Sed()
    sed.readSED_flambda(sedName)
    fNorm = sed.calcFluxNorm(magNorm, imsimband)
    sed.multiplyFluxNorm(fNorm)
    if internalAv is not None and internalRv is not None:
        if internalAv != 0.0 and internalRv != 0.0:
            a_int, b_int = sed.setupCCMab()
            sed.addCCMDust(a_int, b_int, A_v=internalAv, R_v=internalRv)
    
    if redshift is not None and redshift != 0.0:
        sed.redshiftSED(redshift, dimming=True)
    
    a_int, b_int = sed.setupCCMab()
    sed.addCCMDust(a_int, b_int, A_v=galacticAv, R_v=galacticRv)
    
    adu = sed.calcADU(bandpass, photParams=PhotometricParameters())
    
    return adu
示例#12
0
def create_k_corr_grid(redshift):
    """
    (Edited from original because of error)
    Returns a grid of redshifts and K corrections on the
    LSST Simulations AGN SED that can be used for K correction
    interpolation.
    """
    bp_dict = BandpassDict.loadTotalBandpassesFromFiles()
    bp_i = bp_dict['i']
    sed_dir = os.path.join(getPackageDir('sims_sed_library'), 'agnSED')
    sed_name = os.path.join(sed_dir, 'agn.spec.gz')
    if not os.path.exists(sed_name):
        raise RuntimeError('\n\n%s\n\nndoes not exist\n\n' % sed_name)
    base_sed = Sed()
    base_sed.readSED_flambda(sed_name)
    z_grid = np.arange(0.0, redshift.max(), 0.01)
    k_grid = np.zeros(len(z_grid), dtype=float)
    for i_z, zz in enumerate(z_grid):
        ss = Sed(flambda=base_sed.flambda, wavelen=base_sed.wavelen)
        ss.redshiftSED(zz, dimming=True)
        k = k_correction(ss, bp_i, zz)
        k_grid[i_z] = k
    return z_grid, k_grid
示例#13
0
    def test_k_correction(self):
        """
        Test that the K correction correctly converts absolute magnitude
        to observed magnitude.
        """
        bp_dict = BandpassDict.loadTotalBandpassesFromFiles()
        rng = np.random.RandomState(41321)
        sed_dir = os.path.join(getPackageDir('sims_sed_library'), 'galaxySED')
        list_of_sed_files = os.listdir(sed_dir)
        list_of_sed_files.sort()
        sed_to_check = rng.choice(list_of_sed_files, size=10)
        redshift_arr = rng.random_sample(len(sed_to_check)) * 2.0 + 0.1

        bp = bp_dict['g']
        for sed_name, zz in zip(sed_to_check, redshift_arr):
            full_name = os.path.join(sed_dir, sed_name)
            ss = Sed()
            ss.readSED_flambda(full_name)
            true_rest_mag = ss.calcMag(bp)
            ss.redshiftSED(zz, dimming=True)
            obs_mag = ss.calcMag(bp)
            k_corr = k_correction(ss, bp, zz)
            self.assertLess(np.abs(true_rest_mag - obs_mag + k_corr), 0.001)
def read_quasar():
    # read quasar spectra and redshift
    homedir = os.getenv("HOME")
    quasardir = os.path.join(homedir, "seds/quasar")
    # read zero redshift quasar
    base = Sed()
    base.readSED_flambda(os.path.join(quasardir, "quasar.dat"))
    # redshift 
    #redshifts = [0, 0.1, 0.2, 0.3, 0.5, 0.8, 1.0, 1.3, 1.6, 1.9, 2.2, 2.5]
    #redshifts = numpy.array(redshifts)
    redshifts= numpy.arange(0, 2.8, 0.1)
    quasars = {}
    for z in redshifts:
        wavelen, flambda = base.redshiftSED(z, wavelen=base.wavelen, flambda=base.flambda)
        quasars[z] = Sed(wavelen=wavelen, flambda=flambda)
    print "# Generated %d quasars at redshifts between %f and %f" %(len(redshifts), redshifts.min(), redshifts.max())
    # resample onto the standard bandpass for Bandpass obj's and calculate fnu to speed later calculations
    for z in redshifts:
        quasars[z].synchronizeSED(wavelen_min=WMIN, wavelen_max=WMAX, wavelen_step=WSTEP)
    return quasars, redshifts
示例#15
0
    raw_agn_sed.readSED_flambda(sed_file)

    imsim_bp = Bandpass()
    imsim_bp.imsimBandpass()

    bp_dict = BandpassDict.loadTotalBandpassesFromFiles()

    with open('agn_variability_distribution.txt', 'w') as out_file:
        out_file.write('# z u g r i z y tau sfu sfg sfr sfi sfz sfy\n')
        for chunk in data_iter:
            for line in chunk:
                agn_sed = Sed(wavelen=raw_agn_sed.wavelen,
                              flambda=raw_agn_sed.flambda)

                fnorm = agn_sed.calcFluxNorm(line['magNorm'], imsim_bp)
                agn_sed.multiplyFluxNorm(fnorm)
                agn_sed.redshiftSED(line['z'], dimming=True)
                mags = bp_dict.magListForSed(agn_sed)
                params = json.loads(line['varParamStr'])
                tau = params['pars']['agn_tau']
                sfu = params['pars']['agn_sfu']
                sfg = params['pars']['agn_sfg']
                sfr = params['pars']['agn_sfr']
                sfi = params['pars']['agn_sfi']
                sfz = params['pars']['agn_sfz']
                sfy = params['pars']['agn_sfy']
                out_file.write(
                    '%e %e %e %e %e %e %e %e %e %e %e %e %e %e\n' %
                    (line['z'], mags[0], mags[1], mags[2], mags[3], mags[4],
                     mags[5], tau, sfu, sfg, sfr, sfi, sfz, sfy))
    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)
示例#17
0
    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
    sed_name = os.path.join(getPackageDir('sims_sed_library'), 'agnSED',
                            'agn.spec.gz')
    assert os.path.exists(sed_name)
    base_sed = Sed()
    base_sed.readSED_flambda(sed_name)

    bp_dict = BandpassDict.loadTotalBandpassesFromFiles()
    bp = bp_dict['i']

    z_grid = np.arange(0.0, 16.0, 0.01)
    k_grid = np.zeros(len(z_grid), dtype=float)

    for i_z, zz in enumerate(z_grid):
        ss = Sed(flambda=base_sed.flambda, wavelen=base_sed.wavelen)
        ss.redshiftSED(zz, dimming=True)
        k = k_correction(ss, bp, zz)
        k_grid[i_z] = k

    cosmo = CosmologyObject()

    db = DBObject(database='LSSTCATSIM',
                  host='fatboy.phys.washington.edu',
                  port=1433,
                  driver='mssql+pymssql')

    query = 'SELECT magnorm_agn, redshift, varParamStr FROM '
    query += 'galaxy WHERE varParamStr IS NOT NULL '
    query += 'AND dec BETWEEN -2.5 AND 2.5 '
    query += 'AND (ra<2.5 OR ra>357.5)'
示例#19
0
#  same wavelength range we can just calculate a/b once, and this is slow)

a_gal, b_gal = gals[galaxykeys[0]].setupCCMab()

# pretend we want to read mags into an array .. you could just as easily put it into a
# dictionary or list, with small variations in the code
mags = n.empty(len(galaxykeys), dtype='float')

for i in range(len(galaxykeys)):
    # make a copy of the original SED if you want to 'reuse' the SED for multiple magnitude
    # calculations with various fluxnorms, redshifts and dusts
    tmpgal = Sed(wavelen=gals[galaxykeys[i]].wavelen,
                 flambda=gals[galaxykeys[i]].flambda)
    # add the dust internal to the distant galaxy
    tmpgal.addCCMDust(a_gal, b_gal, ebv=ebv_gal[i])
    # redshift the galaxy
    tmpgal.redshiftSED(redshifts[i], dimming=False)
    # add the dust from our milky way - have to recalculate a/b because now wavelenghts
    # for each galaxy are *different*
    a_mw, b_mw = tmpgal.setupCCMab()
    tmpgal.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i])
    tmpgal.multiplyFluxNorm(fluxnorm[i])
    mags[i] = tmpgal.calcMag(rband)

# show results
print "#sedname      fluxnorm     redshift  ebv_gal   ebv_mw  magnitude "
for i in range(len(galaxykeys)):
    print "%s %.5g  %.3f %.5f %.5f %.5f" % (galaxykeys[i], fluxnorm[i],
                                            redshifts[i], ebv_gal[i],
                                            ebv_mw[i], mags[i])
# create the a,b arrays for all the gals (because we resampled the gals onto the
#  same wavelength range we can just calculate a/b once, and this is slow)

a_gal, b_gal = gals[galaxykeys[0]].setupCCMab()

# pretend we want to read mags into an array .. you could just as easily put it into a
# dictionary or list, with small variations in the code
mags = n.empty(len(galaxykeys), dtype='float')

for i in range(len(galaxykeys)):
    # make a copy of the original SED if you want to 'reuse' the SED for multiple magnitude
    # calculations with various fluxnorms, redshifts and dusts
    tmpgal = Sed(wavelen=gals[galaxykeys[i]].wavelen, flambda=gals[galaxykeys[i]].flambda)
    # add the dust internal to the distant galaxy
    tmpgal.addCCMDust(a_gal, b_gal, ebv=ebv_gal[i])
    # redshift the galaxy
    tmpgal.redshiftSED(redshifts[i], dimming=False)
    # add the dust from our milky way - have to recalculate a/b because now wavelenghts
    # for each galaxy are *different*
    a_mw, b_mw = tmpgal.setupCCMab()
    tmpgal.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i])
    tmpgal.multiplyFluxNorm(fluxnorm[i])
    mags[i] = tmpgal.calcMag(rband)


# show results
print "#sedname      fluxnorm     redshift  ebv_gal   ebv_mw  magnitude "
for i in range(len(galaxykeys)):
    print "%s %.5g  %.3f %.5f %.5f %.5f" %(galaxykeys[i], fluxnorm[i], redshifts[i], ebv_gal[i], ebv_mw[i], mags[i])
    
def sed_from_galacticus_mags(galacticus_mags, redshift, redshift_true, H0, Om0,
                             wav_min, wav_width, obs_lsst_mags):
    """
    Fit SEDs from sims_sed_library to Galacticus galaxies based on the
    magnitudes in tophat filters.

    Parameters
    ----------

    galacticus_mags is a numpy array such that
    galacticus_mags[i][j] is the magnitude of the jth star in the ith bandpass,
    where the bandpasses are ordered in ascending order of minimum wavelength.

    redshift is an array of redshifts for the galaxies being fit
    (includes cosmology and proper motion)

    redshift_true is an array of cosmological redshifts for the galaxies
    being fit

    H0 is the Hubbleparameter in units of km/s/Mpc

    Om0 is the critical density parameter for matter

    wav_min is a numpy array of the minimum wavelengths of the tophat
    filters (in nm)

    wav_grid is a numpy array of the widths of the tophat filters
    (in nm)

    ob_lsst_mags is a numpy array of observer frame LSST magnitudes.
    obs_lsst_mags[0] will contain the u band magnitudes of every object.

    Returns
    -------
    a numpy array of SED names and a numpy array of magNorms.
    """

    if (not hasattr(sed_from_galacticus_mags, '_color_tree')
            or not np.allclose(wav_min,
                               sed_from_galacticus_mags._wav_min,
                               atol=1.0e-10,
                               rtol=0.0)
            or not np.allclose(wav_width,
                               sed_from_galacticus_mags._wav_width,
                               atol=1.0e-10,
                               rtol=0.0)):

        (sed_names, sed_mag_list, sed_mag_norm, av_grid,
         rv_grid) = _create_sed_library_mags(wav_min, wav_width)

        assert rv_grid.min() > 0.0
        assert len(np.where(np.logical_not(np.isfinite(rv_grid)))[0]) == 0

        sed_colors = sed_mag_list[:, 1:] - sed_mag_list[:, :-1]
        sed_from_galacticus_mags._sed_names = sed_names
        sed_from_galacticus_mags._mag_norm = sed_mag_norm  # N_sed
        sed_from_galacticus_mags._av_grid = av_grid
        sed_from_galacticus_mags._rv_grid = rv_grid
        sed_from_galacticus_mags._sed_mags = sed_mag_list  # N_sed by N_mag
        sed_from_galacticus_mags._color_tree = scipy_spatial.cKDTree(
            sed_colors)
        sed_from_galacticus_mags._wav_min = wav_min
        sed_from_galacticus_mags._wav_width = wav_width

    if (not hasattr(sed_from_galacticus_mags, '_cosmo')
            or np.abs(sed_from_galacticus_mags._cosmo.H() - H0) > 1.0e-6
            or np.abs(sed_from_galacticus_mags._cosmo.OmegaMatter() - Om0) >
            1.0e-6):

        sed_from_galacticus_mags._cosmo = CosmologyObject(H0=H0, Om0=Om0)

    galacticus_mags_t = np.asarray(galacticus_mags).T  # N_star by N_mag
    assert galacticus_mags_t.shape == (
        len(redshift), sed_from_galacticus_mags._sed_mags.shape[1])

    with np.errstate(invalid='ignore', divide='ignore'):
        galacticus_colors = galacticus_mags_t[:,
                                              1:] - galacticus_mags_t[:, :
                                                                      -1]  # N_star by (N_mag - 1)

    t_start = time.time()
    (sed_dist,
     sed_idx) = sed_from_galacticus_mags._color_tree.query(galacticus_colors,
                                                           k=1)

    # cKDTree returns an invalid index (==len(tree_data)) in cases
    # where the distance is not finite
    sed_idx = np.where(sed_idx < len(sed_from_galacticus_mags._sed_names),
                       sed_idx, 0)

    distance_modulus = sed_from_galacticus_mags._cosmo.distanceModulus(
        redshift=redshift_true)

    output_names = sed_from_galacticus_mags._sed_names[sed_idx]

    (lsst_bp_dict, dummy_bp_dict) = BandpassDict.loadBandpassesFromFiles()

    output_mag_norm = np.zeros((6, len(output_names)), dtype=float)
    base_norm = sed_from_galacticus_mags._mag_norm[sed_idx]
    assert len(np.where(np.logical_not(np.isfinite(base_norm)))[0]) == 0
    ccm_w = None
    av_arr = sed_from_galacticus_mags._av_grid[sed_idx]
    rv_arr = sed_from_galacticus_mags._rv_grid[sed_idx]
    assert rv_arr.min() > 0.0
    assert len(np.where(np.logical_not(np.isfinite(rv_arr)))[0]) == 0
    for i_bp in range(6):
        output_mag_norm[i_bp, :] = base_norm + distance_modulus

    sed_dir = getPackageDir('sims_sed_library')

    for i_obj in range(len(output_names)):
        spec = Sed()
        spec.readSED_flambda(os.path.join(sed_dir, output_names[i_obj]))
        if ccm_w is None or not np.array_equal(spec.wavelen, ccm_w):
            ccm_w = np.copy(spec.wavelen)
            ax, bx = spec.setupCCM_ab()
        spec.addDust(ax, bx, A_v=av_arr[i_obj], R_v=rv_arr[i_obj])
        spec.redshiftSED(redshift[i_obj], dimming=True)
        lsst_mags = lsst_bp_dict.magListForSed(spec)
        d_mag = obs_lsst_mags[:, i_obj] - lsst_mags
        output_mag_norm[:, i_obj] += d_mag

    return (output_names, output_mag_norm, av_arr, rv_arr)
示例#22
0
def write_sprinkled_lc(out_file_name, total_obs_md,
                       pointing_dir, opsim_db_name,
                       ra_colname='descDitheredRA',
                       dec_colname='descDitheredDec',
                       rottel_colname = 'descDitheredRotTelPos',
                       sql_file_name=None,
                       bp_dict=None):

    """
    Create database of light curves

    Note: this is still under development.  It has not yet been
    used for a production-level truth catalog

    Parameters
    ----------
    out_file_name is the name of the sqlite file to be written

    total_obs_md is an ObservationMetaData covering the whole
    survey area

    pointing_dir contains a series of files that are two columns: obshistid, mjd.
    The files must each have 'visits' in their name.  These specify the pointings
    for which we are assembling data.  See:
        https://github.com/LSSTDESC/DC2_Repo/tree/master/data/Run1.1
    for an example.

    opsim_db_name is the name of the OpSim database to be queried for pointings

    ra_colname is the column used for RA of the pointing (default:
    descDitheredRA)

    dec_colname is the column used for the Dec of the pointing (default:
    descDitheredDec)

    rottel_colname is the column used for the rotTelPos of the pointing
    (default: desckDitheredRotTelPos')

    sql_file_name is the name of the parameter database produced by
    write_sprinkled_param_db to be used

    bp_dict is a BandpassDict of the telescope filters to be used

    Returns
    -------
    None

    Writes out a database to out_file_name.  The tables of this database and
    their columns are:

    light_curves:
        - uniqueId -- an int unique to all objects
        - obshistid -- an int unique to all pointings
        - mag -- the magnitude observed for this object at that pointing

    obs_metadata:
        - obshistid -- an int unique to all pointings
        - mjd -- the date of the pointing
        - filter -- an int corresponding to the telescope filter (0==u, 1==g..)

    variables_and_transients:
        - uniqueId -- an int unique to all objects
        - galaxy_id -- an int indicating the host galaxy
        - ra -- in degrees
        - dec -- in degrees
        - agn -- ==1 if object is an AGN
        - sn -- ==1 if object is a supernova
    """

    t0_master = time.time()

    if not os.path.isfile(sql_file_name):
        raise RuntimeError('%s does not exist' % sql_file_name)


    sn_simulator = SneSimulator(bp_dict)
    sed_dir = os.environ['SIMS_SED_LIBRARY_DIR']

    create_sprinkled_sql_file(out_file_name)

    t_start = time.time()

    # get data about the pointings being simulated
    (htmid_dict,
     mjd_dict,
     filter_dict,
     obsmd_dict) = get_pointing_htmid(pointing_dir, opsim_db_name,
                                      ra_colname=ra_colname,
                                      dec_colname=dec_colname)

    t_htmid_dict = time.time()-t_start

    bp_to_int = {'u':0, 'g':1, 'r':2, 'i':3, 'z':4, 'y':5}

    # put the data about the pointings in the obs_metadata table
    with sqlite3.connect(out_file_name) as conn:
        cursor = conn.cursor()
        values = ((int(obs), mjd_dict[obs], bp_to_int[filter_dict[obs]])
                  for obs in mjd_dict)
        cursor.executemany('''INSERT INTO obs_metadata VALUES (?,?,?)''', values)
        cursor.execute('''CREATE INDEX obs_filter
                       ON obs_metadata (obshistid, filter)''')
        conn.commit()

    print('\ngot htmid_dict -- %d in %e seconds' % (len(htmid_dict), t_htmid_dict))

    db = DBObject(sql_file_name, driver='sqlite')

    # get a list of htmid corresponding to trixels in which
    # variables and transients can be found
    query = 'SELECT DISTINCT htmid FROM zpoint WHERE is_agn=1 OR is_sn=1'
    dtype = np.dtype([('htmid', int)])

    results = db.execute_arbitrary(query, dtype=dtype)

    object_htmid = results['htmid']

    agn_dtype = np.dtype([('uniqueId', int), ('galaxy_id', int),
                          ('ra', float), ('dec', float),
                          ('redshift', float), ('sed', str, 500),
                          ('magnorm', float), ('varParamStr', str, 500),
                          ('is_sprinkled', int)])

    agn_base_query = 'SELECT uniqueId, galaxy_id, '
    agn_base_query += 'raJ2000, decJ2000, '
    agn_base_query += 'redshift, sedFilepath, '
    agn_base_query += 'magNorm, varParamStr, is_sprinkled '
    agn_base_query += 'FROM zpoint WHERE is_agn=1 '

    sn_dtype = np.dtype([('uniqueId', int), ('galaxy_id', int),
                         ('ra', float), ('dec', float),
                         ('redshift', float), ('sn_truth_params', str, 500),
                         ('is_sprinkled', int)])

    sn_base_query = 'SELECT uniqueId, galaxy_id, '
    sn_base_query += 'raJ2000, decJ2000, '
    sn_base_query += 'redshift, sn_truth_params, is_sprinkled '
    sn_base_query += 'FROM zpoint WHERE is_sn=1 '

    filter_to_int = {'u':0, 'g':1, 'r':2, 'i':3, 'z':4, 'y':5}

    n_floats = 0
    with sqlite3.connect(out_file_name) as conn:
        cursor = conn.cursor()
        t_before_htmid = time.time()

        # loop over trixels containing variables and transients, simulating
        # the light curves of those objects
        for htmid_dex, htmid in enumerate(object_htmid):
            if htmid_dex>0:
                htmid_duration = (time.time()-t_before_htmid)/3600.0
                htmid_prediction = len(object_htmid)*htmid_duration/htmid_dex
                print('%d htmid out of %d in %e hours; predict %e hours remaining' %
                (htmid_dex, len(object_htmid), htmid_duration,htmid_prediction-htmid_duration))
            mjd_arr = []
            obs_arr = []
            filter_arr = []

            # Find only those pointings which overlap the current trixel
            for obshistid in htmid_dict:
                is_contained = False
                for bounds in htmid_dict[obshistid]:
                    if htmid<=bounds[1] and htmid>=bounds[0]:
                        is_contained = True
                        break
                if is_contained:
                    mjd_arr.append(mjd_dict[obshistid])
                    obs_arr.append(obshistid)
                    filter_arr.append(filter_to_int[filter_dict[obshistid]])
            if len(mjd_arr) == 0:
                continue
            mjd_arr = np.array(mjd_arr)
            obs_arr = np.array(obs_arr)
            filter_arr = np.array(filter_arr)
            sorted_dex = np.argsort(mjd_arr)
            mjd_arr = mjd_arr[sorted_dex]
            obs_arr = obs_arr[sorted_dex]
            filter_arr = filter_arr[sorted_dex]

            agn_query = agn_base_query + 'AND htmid=%d' % htmid

            agn_iter = db.get_arbitrary_chunk_iterator(agn_query,
                                                       dtype=agn_dtype,
                                                       chunk_size=10000)

            # put static data about the AGN (position, etc.) into the
            # variables_and_transients table
            for i_chunk, agn_results in enumerate(agn_iter):
                values = ((int(agn_results['uniqueId'][i_obj]),
                           int(agn_results['galaxy_id'][i_obj]),
                           np.degrees(agn_results['ra'][i_obj]),
                           np.degrees(agn_results['dec'][i_obj]),
                           int(agn_results['is_sprinkled'][i_obj]),
                           1,0)
                          for i_obj in range(len(agn_results)))

                cursor.executemany('''INSERT INTO variables_and_transients VALUES
                                      (?,?,?,?,?,?,?)''', values)

                agn_simulator = AgnSimulator(agn_results['redshift'])

                quiescent_mag = np.zeros((len(agn_results), 6), dtype=float)
                for i_obj, (sed_name, zz, mm) in enumerate(zip(agn_results['sed'],
                                                               agn_results['redshift'],
                                                               agn_results['magnorm'])):
                    spec = Sed()
                    spec.readSED_flambda(os.path.join(sed_dir, sed_name))
                    fnorm = getImsimFluxNorm(spec, mm)
                    spec.multiplyFluxNorm(fnorm)
                    spec.redshiftSED(zz, dimming=True)
                    mag_list = bp_dict.magListForSed(spec)
                    quiescent_mag[i_obj] = mag_list

                # simulate AGN variability
                dmag = agn_simulator.applyVariability(agn_results['varParamStr'],
                                                      expmjd=mjd_arr)

                # loop over pointings that overlap the current trixel, writing
                # out simulated photometry for each AGN observed in that pointing
                for i_time, obshistid in enumerate(obs_arr):

                    # only include objects that were actually on a detector
                    are_on_chip = _actually_on_chip(np.degrees(agn_results['ra']),
                                                    np.degrees(agn_results['dec']),
                                                    obsmd_dict[obshistid])

                    valid_agn = np.where(are_on_chip)

                    if len(valid_agn[0])==0:
                        continue

                    values = ((int(agn_results['uniqueId'][i_obj]),
                               int(obs_arr[i_time]),
                               quiescent_mag[i_obj][filter_arr[i_time]]+
                               dmag[filter_arr[i_time]][i_obj][i_time])
                              for i_obj in valid_agn[0])
                    cursor.executemany('''INSERT INTO light_curves VALUES
                                       (?,?,?)''', values)

                conn.commit()
                n_floats += len(dmag.flatten())

            sn_query = sn_base_query + 'AND htmid=%d' % htmid

            sn_iter = db.get_arbitrary_chunk_iterator(sn_query,
                                                      dtype=sn_dtype,
                                                      chunk_size=10000)

            for sn_results in sn_iter:
                t0_sne = time.time()

                # write static information about SNe to the
                # variables_and_transients table
                values = ((int(sn_results['uniqueId'][i_obj]),
                           int(sn_results['galaxy_id'][i_obj]),
                           np.degrees(sn_results['ra'][i_obj]),
                           np.degrees(sn_results['dec'][i_obj]),
                           int(sn_results['is_sprinkled'][i_obj]),
                           0,1)
                          for i_obj in range(len(sn_results)))

                cursor.executemany('''INSERT INTO variables_and_transients VALUES
                                      (?,?,?,?,?,?,?)''', values)
                conn.commit()

                sn_mags = sn_simulator.calculate_sn_magnitudes(sn_results['sn_truth_params'],
                                                               mjd_arr, filter_arr)
                print('    did %d sne in %e seconds' % (len(sn_results), time.time()-t0_sne))

                # loop over pointings that overlap the current trixel, writing
                # out simulated photometry for each SNe observed in that pointing
                for i_time, obshistid in enumerate(obs_arr):

                    # only include objects that fell on a detector
                    are_on_chip = _actually_on_chip(np.degrees(sn_results['ra']),
                                                    np.degrees(sn_results['dec']),
                                                    obsmd_dict[obshistid])

                    valid_obj = np.where(np.logical_and(np.isfinite(sn_mags[:,i_time]),
                                                        are_on_chip))

                    if len(valid_obj[0]) == 0:
                        continue

                    values = ((int(sn_results['uniqueId'][i_obj]),
                               int(obs_arr[i_time]),
                               sn_mags[i_obj][i_time])
                              for i_obj in valid_obj[0])

                    cursor.executemany('''INSERT INTO light_curves VALUES (?,?,?)''', values)
                    conn.commit()
                    n_floats += len(valid_obj[0])

        cursor.execute('CREATE INDEX unq_obs ON light_curves (uniqueId, obshistid)')
        conn.commit()

    print('n_floats %d' % n_floats)
    print('in %e seconds' % (time.time()-t0_master))
示例#23
0
    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 = 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)
# wavelen_step = 0.1 nm. (wavelen_step will have an impact on the speed difference).
# (w/ wavelen_step=0.25, find a 2.5 times faster speedup in the optimized version).

# Start 'regular' magnitude calculation.
# Calculate internal a/b on the wavelength range required for calculating internal dust extinction.
a_int, b_int = gals[gallist[0]].setupCCMab()
# Set up dictionary + arrays to hold calculated magnitude information.
mags1 = {}
for f in filterlist:
    mags1[f] = numpy.zeros(num_gal, dtype='float')
# For each galaxy (in num_gal's), apply internal dust, redshift, apply MW dust, fluxnorm & calculate mags.
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])
    a_mw, b_mw = tmpgal.setupCCMab()
    tmpgal.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i])
    tmpgal.multiplyFluxNorm(fluxnorm[i])
    # If you comment out the synchronize sed here, then the difference between this method and the optimized
    # version increases to a 2.5 times difference.  (i.e. this 'synchronizeSED' buys you 1.6x faster, by itself.)
    tmpgal.synchronizeSED(wavelen_min=wavelen_min,
                          wavelen_max=wavelen_max,
                          wavelen_step=wavelen_step)
    for f in filterlist:
        mags1[f][i] = tmpgal.calcMag(lsstbp[f])
dt, t = dtime(t)
print "Calculating dust/redshift/dust/fluxnorm/%d magnitudes for %d galaxies took %f s" \
      %(len(filterlist), num_gal, dt)

# For next test: want to also do all the same steps, but in an optimized form. This means
示例#26
0
        max_offset = -1.0
        sed_dir = os.environ['SIMS_SED_LIBRARY_DIR']
        tot_bp_dict = BandpassDict.loadTotalBandpassesFromFiles()
        for ii in dexes_to_validate:
            disk_name = os.path.join(sed_dir,
                                     sed_names[disk_sed_idx[ii]].decode())
            bulge_name = os.path.join(sed_dir,
                                      sed_names[bulge_sed_idx[ii]].decode())
            for i_bp, bp in enumerate('ugrizy'):
                disk_s = Sed()
                disk_s.readSED_flambda(disk_name)
                fnorm = getImsimFluxNorm(disk_s, disk_magnorm[i_bp][ii])
                disk_s.multiplyFluxNorm(fnorm)
                ax, bx = disk_s.setupCCM_ab()
                disk_s.addDust(ax, bx, A_v=disk_av[ii], R_v=disk_rv[ii])
                disk_s.redshiftSED(control_qties['redshift'][ii], dimming=True)
                disk_f = disk_s.calcFlux(tot_bp_dict[bp])

                bulge_s = Sed()
                bulge_s.readSED_flambda(bulge_name)
                fnorm = getImsimFluxNorm(bulge_s, bulge_magnorm[i_bp][ii])
                bulge_s.multiplyFluxNorm(fnorm)
                ax, bx = bulge_s.setupCCM_ab()
                bulge_s.addDust(ax, bx, A_v=bulge_av[ii], R_v=bulge_rv[ii])
                bulge_s.redshiftSED(control_qties['redshift'][ii],
                                    dimming=True)
                bulge_f = bulge_s.calcFlux(tot_bp_dict[bp])

                tot_f = disk_f + bulge_f
                f_true = dummy_spec.fluxFromMag(
                    control_qties['mag_true_%s_lsst' % bp][ii])
    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 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)
示例#29
0
    def sprinkle(self):
        # Define a list that we can write out to a text file
        lenslines = []
        # For each galaxy in the catsim catalog
        updated_catalog = self.catalog.copy()
        # print("Running sprinkler. Catalog Length: ", len(self.catalog))
        for rowNum, row in enumerate(self.catalog):
            # if rowNum == 100 or rowNum % 100000==0:
            #     print("Gone through ", rowNum, " lines of catalog.")
            if not np.isnan(row[self.defs_dict['galaxyAgn_magNorm']]):
                candidates = self.find_lens_candidates(
                    row[self.defs_dict['galaxyAgn_redshift']],
                    row[self.defs_dict['galaxyAgn_magNorm']])
                #varString = json.loads(row[self.defs_dict['galaxyAgn_varParamStr']])
                # varString[self.defs_dict['pars']]['t0_mjd'] = 59300.0
                #row[self.defs_dict['galaxyAgn_varParamStr']] = json.dumps(varString)
                np.random.seed(row[self.defs_dict['galtileid']] % (2 ^ 32 - 1))
                pick_value = np.random.uniform()
                # If there aren't any lensed sources at this redshift from OM10 move on the next object
                if (((len(candidates) > 0) and
                     (pick_value <= self.density_param) and
                     (self.cached_sprinkling is False)) |
                    ((self.cached_sprinkling is True) and
                     (row[self.defs_dict['galtileid']]
                      in self.agn_cache['galtileid'].values))):
                    # Randomly choose one the lens systems
                    # (can decide with or without replacement)
                    # Sort first to make sure the same choice is made every time
                    if self.cached_sprinkling is True:
                        twinkles_sys_cache = self.agn_cache.query(
                            'galtileid == %i' %
                            row[self.defs_dict['galtileid']]
                        )['twinkles_system'].values[0]
                        newlens = self.lenscat[np.where(
                            self.lenscat['twinklesId'] == twinkles_sys_cache)
                                               [0]][0]
                    else:
                        candidates = candidates[np.argsort(
                            candidates['twinklesId'])]
                        newlens = np.random.choice(candidates)
                    # Append the lens galaxy
                    # For each image, append the lens images
                    for i in range(newlens['NIMG']):
                        lensrow = row.copy()
                        # XIMG and YIMG are in arcseconds
                        # raPhSim and decPhoSim are in radians
                        #Shift all parts of the lensed object, not just its agn part
                        for lensPart in [
                                'galaxyBulge', 'galaxyDisk', 'galaxyAgn'
                        ]:
                            lens_ra = lensrow[self.defs_dict[str(lensPart +
                                                                 '_raJ2000')]]
                            lens_dec = lensrow[self.defs_dict[str(
                                lensPart + '_decJ2000')]]
                            delta_ra = np.radians(
                                newlens['XIMG'][i] / 3600.0) / np.cos(lens_dec)
                            delta_dec = np.radians(newlens['YIMG'][i] / 3600.0)
                            lensrow[self.defs_dict[str(
                                lensPart + '_raJ2000')]] = lens_ra + delta_ra
                            lensrow[self.defs_dict[
                                str(lensPart +
                                    '_decJ2000')]] = lens_dec + delta_dec
                        mag_adjust = 2.5 * np.log10(np.abs(newlens['MAG'][i]))
                        lensrow[
                            self.defs_dict['galaxyAgn_magNorm']] -= mag_adjust
                        varString = json.loads(
                            lensrow[self.defs_dict['galaxyAgn_varParamStr']])
                        varString[self.defs_dict['pars']]['t0Delay'] = newlens[
                            'DELAY'][i]
                        varString[self.defs_dict[
                            'varMethodName']] = 'applyAgnTimeDelay'
                        lensrow[self.defs_dict[
                            'galaxyAgn_varParamStr']] = json.dumps(varString)
                        lensrow[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
                        lensrow[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
                        lensrow[
                            self.defs_dict['galaxyDisk_positionAngle']] = 0.0
                        lensrow[self.defs_dict['galaxyDisk_internalAv']] = 0.0
                        lensrow[self.defs_dict[
                            'galaxyDisk_magNorm']] = 999.  #np.nan To be fixed post run1.1
                        lensrow[
                            self.defs_dict['galaxyDisk_sedFilename']] = None
                        lensrow[self.defs_dict['galaxyBulge_majorAxis']] = 0.0
                        lensrow[self.defs_dict['galaxyBulge_minorAxis']] = 0.0
                        lensrow[
                            self.defs_dict['galaxyBulge_positionAngle']] = 0.0
                        lensrow[self.defs_dict['galaxyBulge_internalAv']] = 0.0
                        lensrow[self.defs_dict[
                            'galaxyBulge_magNorm']] = 999.  #np.nan To be fixed post run1.1
                        lensrow[
                            self.defs_dict['galaxyBulge_sedFilename']] = None
                        lensrow[self.defs_dict[
                            'galaxyBulge_redshift']] = newlens['ZSRC']
                        lensrow[self.defs_dict[
                            'galaxyDisk_redshift']] = newlens['ZSRC']
                        lensrow[self.defs_dict[
                            'galaxyAgn_redshift']] = newlens['ZSRC']
                        #To get back twinklesID in lens catalog from phosim catalog id number
                        #just use np.right_shift(phosimID-28, 10). Take the floor of the last
                        #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is
                        #the image number minus 1.
                        lensrow[self.defs_dict['galtileid']] = (
                            lensrow[self.defs_dict['galtileid']] * 10000 +
                            newlens['twinklesId'] * 4 + i)

                        updated_catalog = np.append(updated_catalog, lensrow)

                    #Now manipulate original entry to be the lens galaxy with desired properties
                    #Start by deleting Disk and AGN properties
                    if not np.isnan(row[self.defs_dict['galaxyDisk_magNorm']]):
                        row[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
                        row[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
                        row[self.defs_dict['galaxyDisk_positionAngle']] = 0.0
                        row[self.defs_dict['galaxyDisk_internalAv']] = 0.0
                        row[self.defs_dict[
                            'galaxyDisk_magNorm']] = 999.  #np.nan To be fixed post run1.1
                        row[self.defs_dict['galaxyDisk_sedFilename']] = None
                    row[self.defs_dict[
                        'galaxyAgn_magNorm']] = None  #np.nan To be fixed post run1.1
                    row[self.defs_dict[
                        'galaxyDisk_magNorm']] = 999.  # To be fixed in run1.1
                    row[self.defs_dict['galaxyAgn_sedFilename']] = None
                    #Now insert desired Bulge properties
                    row[self.defs_dict['galaxyBulge_sedFilename']] = newlens[
                        'lens_sed']
                    row[self.
                        defs_dict['galaxyBulge_redshift']] = newlens['ZLENS']
                    row[self.
                        defs_dict['galaxyDisk_redshift']] = newlens['ZLENS']
                    row[self.
                        defs_dict['galaxyAgn_redshift']] = newlens['ZLENS']
                    row_lens_sed = Sed()
                    row_lens_sed.readSED_flambda(
                        str(self.galDir + newlens['lens_sed']))
                    row_lens_sed.redshiftSED(newlens['ZLENS'], dimming=True)
                    row[self.defs_dict['galaxyBulge_magNorm']] = matchBase(
                    ).calcMagNorm(
                        [newlens['APMAG_I']], row_lens_sed,
                        self.bandpassDict)  #Changed from i band to imsimband
                    row[self.defs_dict[
                        'galaxyBulge_majorAxis']] = radiansFromArcsec(
                            newlens['REFF'] / np.sqrt(1 - newlens['ELLIP']))
                    row[self.defs_dict[
                        'galaxyBulge_minorAxis']] = radiansFromArcsec(
                            newlens['REFF'] * np.sqrt(1 - newlens['ELLIP']))
                    #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians
                    row[self.defs_dict['galaxyBulge_positionAngle']] = newlens[
                        'PHIE'] * (-1.0) * np.pi / 180.0
                    #Replace original entry with new entry
                    updated_catalog[rowNum] = row
            else:
                if self.cached_sprinkling is True:
                    if row[self.defs_dict['galtileid']] in self.sne_cache[
                            'galtileid'].values:
                        use_system = self.sne_cache.query(
                            'galtileid == %i' %
                            row[self.defs_dict['galtileid']]
                        )['twinkles_system'].values
                        use_df = self.sne_catalog.query(
                            'twinkles_sysno == %i' % use_system)
                        self.used_systems.append(use_system)
                    else:
                        continue
                else:
                    lens_sne_candidates = self.find_sne_lens_candidates(
                        row[self.defs_dict['galaxyDisk_redshift']])
                    candidate_sysno = np.unique(
                        lens_sne_candidates['twinkles_sysno'])
                    num_candidates = len(candidate_sysno)
                    if num_candidates == 0:
                        continue
                    used_already = np.array([
                        sys_num in self.used_systems
                        for sys_num in candidate_sysno
                    ])
                    unused_sysno = candidate_sysno[~used_already]
                    if len(unused_sysno) == 0:
                        continue
                    np.random.seed(row[self.defs_dict['galtileid']] %
                                   (2 ^ 32 - 1))
                    use_system = np.random.choice(unused_sysno)
                    use_df = self.sne_catalog.query('twinkles_sysno == %i' %
                                                    use_system)

                for i in range(len(use_df)):
                    lensrow = row.copy()
                    for lensPart in ['galaxyBulge', 'galaxyDisk', 'galaxyAgn']:
                        lens_ra = lensrow[self.defs_dict[str(lensPart +
                                                             '_raJ2000')]]
                        lens_dec = lensrow[self.defs_dict[str(lensPart +
                                                              '_decJ2000')]]
                        delta_ra = np.radians(
                            use_df['x'].iloc[i] / 3600.0) / np.cos(lens_dec)
                        delta_dec = np.radians(use_df['y'].iloc[i] / 3600.0)
                        lensrow[self.defs_dict[str(
                            lensPart + '_raJ2000')]] = lens_ra + delta_ra
                        lensrow[self.defs_dict[str(
                            lensPart + '_decJ2000')]] = lens_dec + delta_dec
                    # varString = json.loads(lensrow[self.defs_dict['galaxyAgn_varParamStr']])
                    varString = 'None'
                    lensrow[
                        self.defs_dict['galaxyAgn_varParamStr']] = varString
                    lensrow[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
                    lensrow[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
                    lensrow[self.defs_dict['galaxyDisk_positionAngle']] = 0.0
                    lensrow[self.defs_dict['galaxyDisk_internalAv']] = 0.0
                    lensrow[self.defs_dict[
                        'galaxyDisk_magNorm']] = 999.  #np.nan To be fixed post run1.1
                    lensrow[self.defs_dict['galaxyDisk_sedFilename']] = None
                    lensrow[self.defs_dict['galaxyBulge_majorAxis']] = 0.0
                    lensrow[self.defs_dict['galaxyBulge_minorAxis']] = 0.0
                    lensrow[self.defs_dict['galaxyBulge_positionAngle']] = 0.0
                    lensrow[self.defs_dict['galaxyBulge_internalAv']] = 0.0
                    lensrow[self.defs_dict[
                        'galaxyBulge_magNorm']] = 999.  #np.nan To be fixed post run1.1
                    lensrow[self.defs_dict['galaxyBulge_sedFilename']] = None
                    z_s = use_df['zs'].iloc[i]
                    lensrow[self.defs_dict['galaxyBulge_redshift']] = z_s
                    lensrow[self.defs_dict['galaxyDisk_redshift']] = z_s
                    lensrow[self.defs_dict['galaxyAgn_redshift']] = z_s
                    #To get back twinklesID in lens catalog from phosim catalog id number
                    #just use np.right_shift(phosimID-28, 10). Take the floor of the last
                    #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is
                    #the image number minus 1.
                    lensrow[self.defs_dict['galtileid']] = (
                        lensrow[self.defs_dict['galtileid']] * 10000 +
                        use_system * 4 + i)

                    add_to_cat, sn_magnorm, sn_fname = self.create_sn_sed(
                        use_df.iloc[i],
                        lensrow[self.defs_dict['galaxyAgn_raJ2000']],
                        lensrow[self.defs_dict['galaxyAgn_decJ2000']],
                        self.visit_mjd)
                    lensrow[self.defs_dict['galaxyAgn_sedFilename']] = sn_fname
                    lensrow[self.defs_dict[
                        'galaxyAgn_magNorm']] = sn_magnorm  #This will need to be adjusted to proper band
                    mag_adjust = 2.5 * np.log10(np.abs(use_df['mu'].iloc[i]))
                    lensrow[self.defs_dict['galaxyAgn_magNorm']] -= mag_adjust

                    if add_to_cat is True:
                        updated_catalog = np.append(updated_catalog, lensrow)
                    else:
                        continue
                    #Now manipulate original entry to be the lens galaxy with desired properties
                    #Start by deleting Disk and AGN properties
                if not np.isnan(row[self.defs_dict['galaxyDisk_magNorm']]):
                    row[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
                    row[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
                    row[self.defs_dict['galaxyDisk_positionAngle']] = 0.0
                    row[self.defs_dict['galaxyDisk_internalAv']] = 0.0
                    row[self.defs_dict[
                        'galaxyDisk_magNorm']] = 999.  #np.nan To be fixed post run1.1
                    row[self.defs_dict['galaxyDisk_sedFilename']] = None
                row[self.defs_dict[
                    'galaxyAgn_magNorm']] = None  #np.nan To be fixed post run1.1
                row[self.defs_dict[
                    'galaxyDisk_magNorm']] = 999.  #To be fixed post run1.1
                row[self.defs_dict['galaxyAgn_sedFilename']] = None
                #Now insert desired Bulge properties
                row[self.defs_dict['galaxyBulge_sedFilename']] = use_df[
                    'lens_sed'].iloc[0]
                row[self.
                    defs_dict['galaxyBulge_redshift']] = use_df['zl'].iloc[0]
                row[self.
                    defs_dict['galaxyDisk_redshift']] = use_df['zl'].iloc[0]
                row[self.
                    defs_dict['galaxyAgn_redshift']] = use_df['zl'].iloc[0]
                row[self.defs_dict['galaxyBulge_magNorm']] = use_df[
                    'bulge_magnorm'].iloc[0]
                # row[self.defs_dict['galaxyBulge_magNorm']] = matchBase().calcMagNorm([newlens['APMAG_I']], self.LRG, self.bandpassDict) #Changed from i band to imsimband
                row[self.
                    defs_dict['galaxyBulge_majorAxis']] = radiansFromArcsec(
                        use_df['r_eff'].iloc[0] /
                        np.sqrt(1 - use_df['e'].iloc[0]))
                row[self.
                    defs_dict['galaxyBulge_minorAxis']] = radiansFromArcsec(
                        use_df['r_eff'].iloc[0] *
                        np.sqrt(1 - use_df['e'].iloc[0]))
                #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians
                row[self.defs_dict['galaxyBulge_positionAngle']] = use_df[
                    'theta_e'].iloc[0] * (-1.0) * np.pi / 180.0
                #Replace original entry with new entry
                updated_catalog[rowNum] = row

        return updated_catalog
counts = star.calcADU(rband, expTime=30)
print "This would correspond to roughly %f counts in the LSST focal plane, in a 30s exposure." %(counts)

# For fun, let's see what else can happen.

ebv = 0.5
print ""
print "Let's try adding %.2f E(B-V) dust extinction to this star." %(ebv)
a, b = star.setupCCMab()
# You can use addCCMDust on the 'star' object itself, but I'm illustrating here how you could also
#  do otherwise - preserve the original 'star' object as is, and create a new Sed object that does
#  include the effects of dust ('dustystar').  Star's data will be unchanged by the dust. 
dustywavelen, dustyflambda = star.addCCMDust(a, b, ebv=ebv, wavelen=star.wavelen, flambda=star.flambda)
dustystar = Sed(wavelen=dustywavelen, flambda=dustyflambda)
magdust = dustystar.calcMag(rband)
print "With this dust, the magnitude of the star in this bandpass is now %.4f." %(magdust)

redshift = 0.2
print "What if this star was at a redshift of %f?" %(redshift)
# Here (unlike above with the dust), I'm applying the redshift to the 'star' object itself.
#  Star's data will be changed by the redshifting. 
star.redshiftSED(redshift, dimming=True)
magredshift = star.calcMag(rband)
print ""
print "Redshifted to %.2f, and adding cosmological dimming, the magnitude is now %.4f" \
      %(redshift, magredshift)
print " (this was a pretty hot star, so redshifting brings more flux into this particular bandpass.)"
print ""

# There is more functionality in the class. Please see the code. 
    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)
示例#32
0
    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
示例#33
0
    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)
from lsst.sims.photUtils import PhotometricParameters, Bandpass
phot_params = PhotometricParameters(nexp=1, exptime=30.0, gain=1)
imsim = Bandpass()
imsim.imsimBandpass()

target=20000.0
for name in galaxy_sed_list:
    magnorm_interp[name] = {}
    magnorm_interp[name]['z'] = np.arange(0.0, 2.4, 0.2)
    magnorm_interp[name]['mag'] = np.zeros(len(magnorm_interp[name]['z']), dtype=float)
    for i_zz, zz in enumerate(magnorm_interp[name]['z']):
        ss = Sed()
        ss.readSED_flambda(os.path.join(galaxy_dir, name))
        mag = ss.calcMag(imsim)
        ss.redshiftSED(zz, dimming=True)
        cts = ss.calcADU(bp_dict['r'], photParams=phot_params)
        magnorm = mag -2.5*np.log10(target/cts)

        ss = Sed()
        ss.readSED_flambda(os.path.join(galaxy_dir, name))
        fnorm = ss.calcFluxNorm(magnorm, imsim)
        ss.multiplyFluxNorm(fnorm)
        ss.redshiftSED(zz, dimming=True)
        new_cts = ss.calcADU(bp_dict['r'], photParams=phot_params)
        d_ct = np.abs(new_cts-target)
        
        magnorm_interp[name]['mag'][i_zz] = magnorm
        
        if d_ct>500.0:
            raise RuntimeWarning('d_ct %e' % d_ct)
示例#35
0
    def testFlush(self):
        """
        Test that the flush method of SedList behaves properly
        """
        imsimBand = Bandpass()
        imsimBand.imsimBandpass()
        nSed = 10
        sedNameList_0 = self.getListOfSedNames(nSed)
        magNormList_0 = self.rng.random_sample(nSed)*5.0 + 15.0
        internalAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1
        redshiftList_0 = self.rng.random_sample(nSed)*5.0
        galacticAvList_0 = self.rng.random_sample(nSed)*0.3 + 0.1
        wavelen_match = np.arange(300.0, 1500.0, 10.0)
        testList = SedList(sedNameList_0, magNormList_0,
                           fileDir=self.sedDir,
                           internalAvList=internalAvList_0,
                           redshiftList=redshiftList_0, galacticAvList=galacticAvList_0,
                           wavelenMatch=wavelen_match)

        self.assertEqual(len(testList), nSed)
        np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch)

        for ix in range(len(sedNameList_0)):
            self.assertAlmostEqual(internalAvList_0[ix], testList.internalAvList[ix], 10)
            self.assertAlmostEqual(galacticAvList_0[ix], testList.galacticAvList[ix], 10)
            self.assertAlmostEqual(redshiftList_0[ix], testList.redshiftList[ix], 10)

        for ix, (name, norm, iav, gav, zz) in \
            enumerate(zip(sedNameList_0, magNormList_0, internalAvList_0,
                      galacticAvList_0, redshiftList_0)):

            sedControl = Sed()
            sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz'))

            fnorm = sedControl.calcFluxNorm(norm, imsimBand)
            sedControl.multiplyFluxNorm(fnorm)

            a_coeff, b_coeff = sedControl.setupCCM_ab()
            sedControl.addDust(a_coeff, b_coeff, A_v=iav)

            sedControl.redshiftSED(zz, dimming=True)
            sedControl.resampleSED(wavelen_match=wavelen_match)

            a_coeff, b_coeff = sedControl.setupCCM_ab()
            sedControl.addDust(a_coeff, b_coeff, A_v=gav)

            sedTest = testList[ix]

            np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen)
            np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda)
            np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)

        testList.flush()

        sedNameList_1 = self.getListOfSedNames(nSed//2)
        magNormList_1 = self.rng.random_sample(nSed//2)*5.0 + 15.0
        internalAvList_1 = self.rng.random_sample(nSed//2)*0.3 + 0.1
        redshiftList_1 = self.rng.random_sample(nSed//2)*5.0
        galacticAvList_1 = self.rng.random_sample(nSed//2)*0.3 + 0.1

        testList.loadSedsFromList(sedNameList_1, magNormList_1,
                                  internalAvList=internalAvList_1,
                                  galacticAvList=galacticAvList_1,
                                  redshiftList=redshiftList_1)

        self.assertEqual(len(testList), nSed/2)
        self.assertEqual(len(testList.redshiftList), nSed/2)
        self.assertEqual(len(testList.internalAvList), nSed/2)
        self.assertEqual(len(testList.galacticAvList), nSed/2)
        np.testing.assert_array_equal(wavelen_match, testList.wavelenMatch)

        for ix in range(len(sedNameList_1)):
            self.assertAlmostEqual(internalAvList_1[ix], testList.internalAvList[ix], 10)
            self.assertAlmostEqual(galacticAvList_1[ix], testList.galacticAvList[ix], 10)
            self.assertAlmostEqual(redshiftList_1[ix], testList.redshiftList[ix], 10)

        for ix, (name, norm, iav, gav, zz) in \
            enumerate(zip(sedNameList_1, magNormList_1, internalAvList_1,
                      galacticAvList_1, redshiftList_1)):

            sedControl = Sed()
            sedControl.readSED_flambda(os.path.join(self.sedDir, name+'.gz'))

            fnorm = sedControl.calcFluxNorm(norm, imsimBand)
            sedControl.multiplyFluxNorm(fnorm)

            a_coeff, b_coeff = sedControl.setupCCM_ab()
            sedControl.addDust(a_coeff, b_coeff, A_v=iav)

            sedControl.redshiftSED(zz, dimming=True)
            sedControl.resampleSED(wavelen_match=wavelen_match)

            a_coeff, b_coeff = sedControl.setupCCM_ab()
            sedControl.addDust(a_coeff, b_coeff, A_v=gav)

            sedTest = testList[ix]

            np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen)
            np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda)
            np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
示例#36
0
    def __init__(self,
                 catsim_cat,
                 visit_mjd,
                 specFileMap,
                 sed_path,
                 om10_cat='twinkles_lenses_v2.fits',
                 sne_cat='dc2_sne_cat.csv',
                 density_param=1.,
                 cached_sprinkling=False,
                 agn_cache_file=None,
                 sne_cache_file=None,
                 defs_file=None):
        """
        Parameters
        ----------
        catsim_cat: catsim catalog
            The results array from an instance catalog.
        visit_mjd: float
            The mjd of the visit
        specFileMap: 
            This will tell the instance catalog where to write the files
        om10_cat: optional, defaults to 'twinkles_lenses_v2.fits
            fits file with OM10 catalog
        sne_cat: optional, defaults to 'dc2_sne_cat.csv'
        density_param: `np.float`, optioanl, defaults to 1.0
            the fraction of eligible agn objects that become lensed and should
            be between 0.0 and 1.0.
        cached_sprinkling: boolean
            If true then pick from a preselected list of galtileids
        agn_cache_file: str
        sne_cache_file: str
        defs_file: str

        Returns
        -------
        updated_catalog:
            A new results array with lens systems added.
        """

        twinklesDir = getPackageDir('Twinkles')
        om10_cat = os.path.join(twinklesDir, 'data', om10_cat)
        self.catalog = catsim_cat
        # ****** THIS ASSUMES THAT THE ENVIRONMENT VARIABLE OM10_DIR IS SET *******
        lensdb = om10.DB(catalog=om10_cat, vb=False)
        self.lenscat = lensdb.lenses.copy()
        self.density_param = density_param
        self.bandpassDict = BandpassDict.loadTotalBandpassesFromFiles(
            bandpassNames=['i'])

        self.sne_catalog = pd.read_csv(
            os.path.join(twinklesDir, 'data', sne_cat))
        #self.sne_catalog = self.sne_catalog.iloc[:101] ### Remove this after testing
        self.used_systems = []
        self.visit_mjd = visit_mjd
        self.sn_obj = SNObject(0., 0.)
        self.write_dir = specFileMap.subdir_map['(^specFileGLSN)']
        self.sed_path = sed_path

        self.cached_sprinkling = cached_sprinkling
        if self.cached_sprinkling is True:
            if ((agn_cache_file is None) | (sne_cache_file is None)):
                raise AttributeError(
                    'Must specify cache files if using cached_sprinkling.')
            #agn_cache_file = os.path.join(twinklesDir, 'data', 'test_agn_galtile_cache.csv')
            self.agn_cache = pd.read_csv(agn_cache_file)
            #sne_cache_file = os.path.join(twinklesDir, 'data', 'test_sne_galtile_cache.csv')
            self.sne_cache = pd.read_csv(sne_cache_file)
        else:
            self.agn_cache = None
            self.sne_cache = None

        if defs_file is None:
            self.defs_file = os.path.join(twinklesDir, 'data',
                                          'catsim_defs.csv')
        else:
            self.defs_file = defs_file

        specFileStart = 'Burst'
        for key, val in sorted(iteritems(SpecMap.subdir_map)):
            if re.match(key, specFileStart):
                galSpecDir = str(val)
        self.galDir = str(
            getPackageDir('sims_sed_library') + '/' + galSpecDir + '/')

        self.imSimBand = Bandpass()
        self.imSimBand.imsimBandpass()
        #self.LRG_name = 'Burst.25E09.1Z.spec'
        #self.LRG = Sed()
        #self.LRG.readSED_flambda(str(galDir + self.LRG_name))
        #return

        #Calculate imsimband magnitudes of source galaxies for matching

        agn_fname = str(
            getPackageDir('sims_sed_library') + '/agnSED/agn.spec.gz')
        src_iband = self.lenscat['MAGI_IN']
        src_z = self.lenscat['ZSRC']
        self.src_mag_norm = []
        for src, s_z in zip(src_iband, src_z):
            agn_sed = Sed()
            agn_sed.readSED_flambda(agn_fname)
            agn_sed.redshiftSED(s_z, dimming=True)
            self.src_mag_norm.append(matchBase().calcMagNorm(
                [src], agn_sed, self.bandpassDict))
        #self.src_mag_norm = matchBase().calcMagNorm(src_iband,
        #                                            [agn_sed]*len(src_iband),
        #
        #                                            self.bandpassDict)

        self.defs_dict = {}
        with open(self.defs_file, 'r') as f:
            for line in f:
                line_defs = line.split(',')
                if len(line_defs) > 1:
                    self.defs_dict[line_defs[0]] = line_defs[1].split('\n')[0]
    def 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)
示例#38
0
    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 run_lsst_baker(redshift_grid):
    """
    Accepts a redshift grid (a numpy array).  Reads in all of the SEDs from
    ../data/sed/ and the calculates their colors in all of the lsst bands,
    given those redshifts

    Returns a structured numpy array containing
    sedname
    redshift
    ug
    gr
    ri
    iz
    zy
    time (the time spent calculating the magnitudes)
    """
    bandpass_dir = os.path.join('..', 'data', 'bandpass')
    bandpass_list = []
    for bp_name in ('total_u.dat', 'total_g.dat', 'total_r.dat', 'total_i.dat',
                    'total_z.dat', 'total_y.dat'):

        bp = Bandpass()
        bp.readThroughput(os.path.join(bandpass_dir, bp_name))
        bandpass_list.append(bp)

    sed_dir = os.path.join('..', 'data', 'sed')
    list_of_sed_names = sorted([nn for nn in os.listdir(sed_dir)
                                if 'spec' in nn and 'ang' not in nn])

    n_rows = len(list_of_sed_names)*len(redshift_grid)
    dtype = np.dtype([('sedname', str, 300), ('redshift', np.float),
                      ('ug', np.float), ('gr', np.float), ('ri', np.float),
                      ('iz', np.float), ('zy', np.float), ('time', np.float)])

    dummy = ('aaaa', 1.0, 1, 1, 1, 1, 1, 1.0)
    output_array = np.array([dummy]*n_rows, dtype=dtype)

    i_row = 0
    for redshift in redshift_grid:
        for sed_name in list_of_sed_names:
            ss = Sed()
#            t_start = time.clock()
            t_start = time.time()
            ss.readSED_flambda(os.path.join(sed_dir, sed_name))
            ss.redshiftSED(redshift)
            local_list = []
            for bp in bandpass_list:
                mm = ss.calcMag(bp)
                local_list.append(mm)
            #print local_list[0], local_list[1], local_list[0]-local_list[1]
#            time_spent = time.clock()-t_start
            time_spent = time.time()-t_start
            output_array[i_row][0] = sed_name
            output_array[i_row][1] = redshift
            output_array[i_row][2] = local_list[0]-local_list[1]
            output_array[i_row][3] = local_list[1]-local_list[2]
            output_array[i_row][4] = local_list[2]-local_list[3]
            output_array[i_row][5] = local_list[3]-local_list[4]
            output_array[i_row][6] = local_list[4]-local_list[5]
            output_array[i_row][7] = time_spent
            i_row += 1

    return output_array
    def _calculateGalSimSeds(self):
        """
        Apply any physical corrections to the objects' SEDS (redshift them, apply dust, etc.).
        Return a list of Sed objects containing the SEDS
        """

        sedList = []
        actualSEDnames = self.column_by_name('sedFilepath')
        redshift = self.column_by_name('redshift')
        internalAv = self.column_by_name('internalAv')
        internalRv = self.column_by_name('internalRv')
        galacticAv = self.column_by_name('galacticAv')
        galacticRv = self.column_by_name('galacticRv')
        magNorm = self.column_by_name('magNorm')

        #for setting magNorm
        imsimband = Bandpass()
        imsimband.imsimBandpass()

        outputNames=[]

        for (sedName, zz, iAv, iRv, gAv, gRv, norm) in \
            zip(actualSEDnames, redshift, internalAv, internalRv, galacticAv, galacticRv, magNorm):

            if is_null(sedName):
                sedList.append(None)
            else:
                if sedName in self.uniqueSeds:
                    #we have already read in this file; no need to do it again
                    sed = Sed(wavelen=self.uniqueSeds[sedName].wavelen,
                              flambda=self.uniqueSeds[sedName].flambda,
                              fnu=self.uniqueSeds[sedName].fnu,
                              name=self.uniqueSeds[sedName].name)
                else:
                    #load the SED of the object
                    sed = Sed()
                    sedFile = os.path.join(self.sedDir, sedName)
                    sed.readSED_flambda(sedFile)

                    flambdaCopy = copy.deepcopy(sed.flambda)

                    #If the SED is zero inside of the bandpass, GalSim raises an error.
                    #This sets a minimum flux value of 1.0e-30 so that the SED is never technically
                    #zero inside of the bandpass.
                    sed.flambda = numpy.array([ff if ff>1.0e-30 else 1.0e-30 for ff in flambdaCopy])
                    sed.fnu = None

                    #copy the unnormalized file to uniqueSeds so we don't have to read it in again
                    sedCopy = Sed(wavelen=sed.wavelen, flambda=sed.flambda,
                                  fnu=sed.fnu, name=sed.name)
                    self.uniqueSeds[sedName] = sedCopy

                #normalize the SED
                #Consulting the file sed.py in GalSim/galsim/ it appears that GalSim expects
                #its SEDs to ultimately be in units of ergs/nm so that, when called, they can
                #be converted to photons/nm (see the function __call__() and the assignment of
                #self._rest_photons in the __init__() of galsim's sed.py file).  Thus, we need
                #to read in our SEDs, normalize them, and then multiply by the exposure time
                #and the effective area to get from ergs/s/cm^2/nm to ergs/nm.
                #
                #The gain parameter should convert between photons and ADU (so: it is the
                #traditional definition of "gain" -- electrons per ADU -- multiplied by the
                #quantum efficiency of the detector).  Because we fold the quantum efficiency
                #of the detector into our total_[u,g,r,i,z,y].dat bandpass files
                #(see the readme in the THROUGHPUTS_DIR/baseline/), we only need to multiply
                #by the electrons per ADU gain.
                #
                #We will take these parameters from an instantiation of the PhotometricParameters
                #class (which can be reassigned by defining a daughter class of this class)
                #
                fNorm = sed.calcFluxNorm(norm, imsimband)
                sed.multiplyFluxNorm(fNorm)

                #apply dust extinction (internal)
                if iAv != 0.0 and iRv != 0.0:
                    a_int, b_int = sed.setupCCMab()
                    sed.addCCMDust(a_int, b_int, A_v=iAv, R_v=iRv)

                #22 June 2015
                #apply redshift; there is no need to apply the distance modulus from
                #sims/photUtils/CosmologyWrapper; magNorm takes that into account
                #however, magNorm does not take into account cosmological dimming
                if zz != 0.0:
                    sed.redshiftSED(zz, dimming=True)

                #apply dust extinction (galactic)
                a_int, b_int = sed.setupCCMab()
                sed.addCCMDust(a_int, b_int, A_v=gAv, R_v=gRv)
                sedList.append(sed)

        return sedList
def process_instance_catalog(catalog_name, centroid_dir, bp_dict):

    sed_dir = getPackageDir('sims_sed_library')

    i_filter = -1
    obshistid = -1
    with open(catalog_name, 'r') as in_file:
        for line in in_file:
            params = line.strip().split()
            if params[0] == 'filter':
                i_filter = int(params[1])
            if params[0] == 'obshistid':
                obshistid = int(params[1])

            if i_filter >= 0 and obshistid >= 0:
                break

    filter_name = 'ugrizy'[i_filter]
    bp = bp_dict[filter_name]

    objid_arr = []
    flux_arr = []
    redshift_arr = []
    magnorm_arr = []
    phot_params = PhotometricParameters(nexp=1, exptime=30.0, gain=1.0)

    with open(catalog_name, 'r') as in_file:
        for line in in_file:
            params = line.strip().split()
            if params[0] != 'object':
                continue

            sed_name = params[5]
            obj_id = int(params[1])
            magnorm = float(params[4])
            redshift = float(params[6])
            spec = Sed()
            spec.readSED_flambda(os.path.join(sed_dir, sed_name))
            fnorm = getImsimFluxNorm(spec, magnorm)
            spec.multiplyFluxNorm(fnorm)
            spec.redshiftSED(redshift, dimming=True)
            adu = spec.calcADU(bp, photParams=phot_params)
            objid_arr.append(obj_id)
            flux_arr.append(adu)
            redshift_arr.append(redshift)
            magnorm_arr.append(magnorm)

    centroid_files = os.listdir(centroid_dir)
    phosim_objid_arr = []
    phosim_flux_arr = []
    x_arr = []
    y_arr = []
    chip_name_arr = []
    dtype = np.dtype([('id', int), ('phot', float), ('x', float),
                      ('y', float)])
    for file_name in centroid_files:
        if 'e_%d' % obshistid not in file_name or 'f%d' % i_filter not in file_name:
            continue
        name_params = file_name.split('_')
        raft = name_params[5]
        sensor = name_params[6]
        chip_name = '%s:%s,%s %s:%s,%s' % (raft[0], raft[1], raft[2],
                                           sensor[0], sensor[1], sensor[2])
        full_name = os.path.join(centroid_dir, file_name)
        data = np.genfromtxt(full_name, dtype=dtype, skip_header=1)
        for line in data:
            phosim_objid_arr.append(line['id'])
            phosim_flux_arr.append(line['phot'])
            x_arr.append(line['x'])
            y_arr.append(line['y'])
            chip_name_arr.append(chip_name)

    objid_arr = np.array(objid_arr)
    flux_arr = np.array(flux_arr)
    redshift_arr = np.array(redshift_arr)
    magnorm_arr = np.array(magnorm_arr)
    phosim_objid_arr = np.array(phosim_objid_arr)
    phosim_flux_arr = np.array(phosim_flux_arr)
    x_arr = np.array(x_arr)
    y_arr = np.array(y_arr)
    chip_name_arr = np.array(chip_name_arr)

    sorted_dex = np.argsort(objid_arr)
    objid_arr = objid_arr[sorted_dex]
    flux_arr = flux_arr[sorted_dex]
    redshift_arr = redshift_arr[sorted_dex]
    magnorm_arr = magnorm_arr[sorted_dex]

    sorted_dex = np.argsort(phosim_objid_arr)
    phosim_objid_arr = phosim_objid_arr[sorted_dex]
    phosim_flux_arr = phosim_flux_arr[sorted_dex]
    x_arr = x_arr[sorted_dex]
    y_arr = y_arr[sorted_dex]
    chip_name_arr = chip_name_arr[sorted_dex]

    np.testing.assert_array_equal(phosim_objid_arr, objid_arr)

    return (objid_arr, flux_arr, phosim_flux_arr, redshift_arr, magnorm_arr,
            x_arr, y_arr, chip_name_arr, filter_name)
示例#42
0
    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)
示例#43
0
                out_name = os.path.join(
                    out_dir,
                    'instcat_galaxy_grid_%.1f_%d.txt' % (redshift, i_filter))
                with open(out_name, 'w') as instcat_handle:
                    phosim_header['obshistid'] = i_filter * 100 + int(
                        redshift / 0.1) + 1

                    write_phoSim_header(obs, instcat_handle, phosim_header)
                    for ii, (rr, dd) in enumerate(zip(ra, dec)):
                        unqid = ii + 1
                        instcat_handle.write('object %d ' % unqid)
                        instcat_handle.write('%.17f %.17f %.6f ' %
                                             (rr, dd, magnorm))
                        instcat_handle.write('galaxySED/%s ' % sed_names[ii])
                        instcat_handle.write(
                            '%1.f 0 0 0 0 0 point none none\n' % redshift)
                        if i_filter == 0:
                            ss = Sed()
                            ss.readSED_flambda(
                                os.path.join(galaxy_dir, sed_names[ii]))
                            fnorm = ss.calcFluxNorm(magnorm, imsim)
                            ss.multiplyFluxNorm(fnorm)
                            ss.redshiftSED(redshift, dimming=True)
                            ref_handle.write('%d %.17f %.17f ' %
                                             (unqid, rr, dd))
                            for bp in bp_dict:
                                adu = ss.calcADU(bp_dict[bp],
                                                 photParams=phot_params)
                                ref_handle.write('%e ' % adu)
                            ref_handle.write('\n')
示例#44
0
    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)
示例#45
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
示例#46
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 = 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)
# wavelen_step = 0.1 nm. (wavelen_step will have an impact on the speed difference).
# (w/ wavelen_step=0.25, find a 2.5 times faster speedup in the optimized version). 

# Start 'regular' magnitude calculation.
# Calculate internal a/b on the wavelength range required for calculating internal dust extinction. 
a_int, b_int = gals[gallist[0]].setupCCMab()
# Set up dictionary + arrays to hold calculated magnitude information. 
mags1 = {}
for f in filterlist:
    mags1[f] = numpy.zeros(num_gal, dtype='float')
# For each galaxy (in num_gal's), apply internal dust, redshift, apply MW dust, fluxnorm & calculate mags. 
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])
    a_mw, b_mw = tmpgal.setupCCMab()
    tmpgal.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i])
    tmpgal.multiplyFluxNorm(fluxnorm[i])
    # If you comment out the synchronize sed here, then the difference between this method and the optimized
    # version increases to a 2.5 times difference.  (i.e. this 'synchronizeSED' buys you 1.6x faster, by itself.)
    tmpgal.synchronizeSED(wavelen_min=wavelen_min,
                          wavelen_max=wavelen_max,
                          wavelen_step = wavelen_step)
    for f in filterlist:
        mags1[f][i] = tmpgal.calcMag(lsstbp[f])
dt, t = dtime(t)
print "Calculating dust/redshift/dust/fluxnorm/%d magnitudes for %d galaxies took %f s" \
      %(len(filterlist), num_gal, dt)

# For next test: want to also do all the same steps, but in an optimized form. This means