def testStellarPhotometricUncertainties(self):
        """
        Test in the case of a catalog of stars
        """
        lsstDefaults = LSSTdefaults()
        starDB = testStarsDBObj(driver=self.driver, host=self.host, database=self.dbName)
        starCat = testStarCatalog(starDB, obs_metadata=self.obs_metadata)
        phot = PhotometryStars()

        ct = 0
        for line in starCat.iter_catalog():
            starSed = Sed()
            starSed.readSED_flambda(os.path.join(lsst.utils.getPackageDir('sims_sed_library'),
                                                 defaultSpecMap[line[14]]))
            imsimband = Bandpass()
            imsimband.imsimBandpass()
            fNorm = starSed.calcFluxNorm(line[15], imsimband)
            starSed.multiplyFluxNorm(fNorm)

            aV = numpy.float(line[16])
            a_int, b_int = starSed.setupCCMab()
            starSed.addCCMDust(a_int, b_int, A_v=aV)

            for i in range(len(self.bandpasses)):
                controlSigma = calcMagError_sed(starSed, self.totalBandpasses[i],
                                             self.skySeds[i],
                                             self.hardwareBandpasses[i],
                                             FWHMeff=lsstDefaults.FWHMeff(self.bandpasses[i]),
                                             photParams=PhotometricParameters())

                testSigma = line[8+i]
                self.assertAlmostEqual(controlSigma, testSigma, 4)
                ct += 1
        self.assertGreater(ct, 0)
    def _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.setupCCMab()
            sed.addCCMDust(a_int, b_int, A_v=iAv, R_v=iRv)

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

        # apply dust extinction (galactic)
        if gAv != 0.0 and gRv != 0.0:
            a_int, b_int = sed.setupCCMab()
            sed.addCCMDust(a_int, b_int, A_v=gAv, R_v=gRv)
        return sed
示例#3
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.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
示例#4
0
    def test_stars(self):
        obs = ObservationMetaData(bandpassName=['c_u', 'c_g'], m5=[25.0, 26.0])

        db_dtype = np.dtype([('id', np.int),
                             ('raJ2000', np.float),
                             ('decJ2000', np.float),
                             ('sedFilename', str, 100),
                             ('magNorm', np.float),
                             ('galacticAv', np.float)])

        inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'testData')
        inputFile = os.path.join(inputDir, 'IndicesTestCatalogStars.txt')
        db = fileDBObject(inputFile, dtype=db_dtype, runtable='test', idColKey='id')
        catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'cartoonStarCat.txt')
        cat = CartoonStars(db, obs_metadata=obs)
        cat.write_catalog(catName)

        dtype = np.dtype([(name, np.float) for name in cat.column_outputs])

        controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',')

        if os.path.exists(catName):
            os.unlink(catName)

        db_columns = db.query_columns(['id', 'raJ2000', 'decJ2000', 'sedFilename', 'magNorm', 'galacticAv'])

        sedDir = os.path.join(getPackageDir('sims_sed_library'), 'starSED', 'kurucz')

        for ix, line in enumerate(next(db_columns)):
            spectrum = Sed()
            spectrum.readSED_flambda(os.path.join(sedDir, line[3]))
            fnorm = spectrum.calcFluxNorm(line[4], self.normband)
            spectrum.multiplyFluxNorm(fnorm)
            a_x, b_x = spectrum.setupCCMab()
            spectrum.addCCMDust(a_x, b_x, A_v=line[5])
            umag = spectrum.calcMag(self.uband)
            self.assertAlmostEqual(umag, controlData['cartoon_u'][ix], 3)
            gmag = spectrum.calcMag(self.gband)
            self.assertAlmostEqual(gmag, controlData['cartoon_g'][ix], 3)
            umagError, gamma = calcMagError_m5(umag, self.uband, obs.m5['c_u'], PhotometricParameters())
            gmagError, gamma = calcMagError_m5(gmag, self.gband, obs.m5['c_g'], PhotometricParameters())
            self.assertAlmostEqual(umagError, controlData['sigma_cartoon_u'][ix], 3)
            self.assertAlmostEqual(gmagError, controlData['sigma_cartoon_g'][ix], 3)
def _create_library_one_sed(_galaxy_sed_dir, sed_file_name_list, av_grid,
                            rv_grid, bandpass_dict, out_dict):

    n_obj = len(av_grid) * len(rv_grid)

    imsim_bp = Bandpass()
    imsim_bp.imsimBandpass()

    t_start = time.time()
    for i_sed, sed_file_name in enumerate(sed_file_name_list):
        if i_sed > 0 and i_sed % 10 == 0:
            duration = (time.time() - t_start) / 3600.0
            pred = len(sed_file_name_list) * duration / i_sed
            print('%d of %d; dur %.2e pred %.2e' %
                  (i_sed, len(sed_file_name_list), duration, pred))

        base_spec = Sed()
        base_spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name))
        ax, bx = base_spec.setupCCMab()

        mag_norm = base_spec.calcMag(imsim_bp)

        sed_names = np.array([defaultSpecMap[sed_file_name]] * n_obj)
        rv_out_list = np.zeros(n_obj, dtype=float)
        av_out_list = np.zeros(n_obj, dtype=float)
        sed_mag_norm = mag_norm * np.ones(n_obj, dtype=float)
        sed_mag_list = []

        i_obj = 0
        for av in av_grid:
            for rv in rv_grid:
                spec = Sed(wavelen=base_spec.wavelen,
                           flambda=base_spec.flambda)
                spec.addCCMDust(ax, bx, A_v=av, R_v=rv)
                av_out_list[i_obj] = av
                rv_out_list[i_obj] = rv
                sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec)))
                i_obj += 1

        out_dict[sed_file_name] = (sed_names, sed_mag_norm, sed_mag_list,
                                   av_out_list, rv_out_list)
示例#6
0
    def testStellarPhotometricUncertainties(self):
        """
        Test in the case of a catalog of stars
        """
        lsstDefaults = LSSTdefaults()
        starDB = testStarsDBObj(driver=self.driver,
                                host=self.host,
                                database=self.dbName)
        starCat = testStarCatalog(starDB, obs_metadata=self.obs_metadata)

        ct = 0
        for line in starCat.iter_catalog():
            starSed = Sed()
            starSed.readSED_flambda(
                os.path.join(getPackageDir('sims_sed_library'),
                             defaultSpecMap[line[14]]))
            imsimband = Bandpass()
            imsimband.imsimBandpass()
            fNorm = starSed.calcFluxNorm(line[15], imsimband)
            starSed.multiplyFluxNorm(fNorm)

            aV = np.float(line[16])
            a_int, b_int = starSed.setupCCMab()
            starSed.addCCMDust(a_int, b_int, A_v=aV)

            for i in range(len(self.bandpasses)):
                controlSigma = calcMagError_sed(
                    starSed,
                    self.totalBandpasses[i],
                    self.skySeds[i],
                    self.hardwareBandpasses[i],
                    FWHMeff=lsstDefaults.FWHMeff(self.bandpasses[i]),
                    photParams=PhotometricParameters())

                testSigma = line[8 + i]
                self.assertAlmostEqual(controlSigma, testSigma, 4)
                ct += 1
        self.assertGreater(ct, 0)
def calcADUwrapper(sedName=None, magNorm=None, redshift=None, internalAv=None, internalRv=None,
                   galacticAv=None, galacticRv=None, bandpass=None):

    imsimband = Bandpass()
    imsimband.imsimBandpass()
    sed = Sed()
    sed.readSED_flambda(sedName)
    fNorm = sed.calcFluxNorm(magNorm, imsimband)
    sed.multiplyFluxNorm(fNorm)
    if internalAv is not None and internalRv is not None:
        if internalAv != 0.0 and internalRv != 0.0:
            a_int, b_int = sed.setupCCMab()
            sed.addCCMDust(a_int, b_int, A_v=internalAv, R_v=internalRv)
    
    if redshift is not None and redshift != 0.0:
        sed.redshiftSED(redshift, dimming=True)
    
    a_int, b_int = sed.setupCCMab()
    sed.addCCMDust(a_int, b_int, A_v=galacticAv, R_v=galacticRv)
    
    adu = sed.calcADU(bandpass, photParams=PhotometricParameters())
    
    return adu
def get_dust_grid():

    # Construct a look-up table to determine the factor
    # by which to multiply the flares' flux to account for
    # dust as a function of E(B-V).  Recall that we are
    # modeling all MLT flares as 9000K blackbodies.

    lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles()

    ebv_grid = np.arange(0.0, 7.01, 0.01)
    bb_wavelen = np.arange(200.0, 1500.0, 0.1)
    hc_over_k = 1.4387e7  # nm*K
    temp = 9000.0  # black body temperature in Kelvin
    exp_arg = hc_over_k / (temp * bb_wavelen)
    exp_term = 1.0 / (np.exp(exp_arg) - 1.0)
    ln_exp_term = np.log(exp_term)

    # Blackbody f_lambda function;
    # discard normalizing factors; we only care about finding the
    # ratio of fluxes between the case with dust extinction and
    # the case without dust extinction
    log_bb_flambda = -5.0 * np.log(bb_wavelen) + ln_exp_term
    bb_flambda = np.exp(log_bb_flambda)
    bb_sed = Sed(wavelen=bb_wavelen, flambda=bb_flambda)

    base_fluxes = lsstBandpassDict.fluxListForSed(bb_sed)

    a_x, b_x = bb_sed.setupCCMab()
    _mlt_dust_lookup = {}
    _mlt_dust_lookup['ebv'] = ebv_grid
    list_of_bp = lsstBandpassDict.keys()
    for bp in list_of_bp:
        _mlt_dust_lookup[bp] = np.zeros(len(ebv_grid))
    for iebv, ebv_val in enumerate(ebv_grid):
        wv, fl = bb_sed.addCCMDust(a_x,
                                   b_x,
                                   ebv=ebv_val,
                                   wavelen=bb_wavelen,
                                   flambda=bb_flambda)

        dusty_bb = Sed(wavelen=wv, flambda=fl)
        dusty_fluxes = lsstBandpassDict.fluxListForSed(dusty_bb)
        for ibp, bp in enumerate(list_of_bp):
            _mlt_dust_lookup[bp][iebv] = dusty_fluxes[ibp] / base_fluxes[ibp]

    return _mlt_dust_lookup
示例#9
0
def gen_gums_mag_cat(istart=0, nstars=100, workdir='', noisyResponse=False, verbose=False, save=True):
    """
    generate a catalog of true and observed magnitudes for stars from the gums catalog
    """
    # Load response functions
    responses = []
    response_temperatures = []
    response_files = glob.glob('gaia_response_*00.npz')

    for fn in response_files:
        responses.append(gaia_response(restore_file=fn))
        response_temperatures.append(float(fn.replace('.npz', '').split('_')[-1]))

    response_temperatures = np.array(response_temperatures)

    #if noisyResponse:
    #    response = gaia_response()
    #else:
    #    response = gaia_response(restore_file='gaia_response_nonoise.npz')

    # Load gums catalog
    gum_cat = gums_catalog()
    gum_cat.prune()
    gum_cat = gum_cat.catalog

    print 'using %i stars from GAIA' % gum_cat.size

    sed = Sed()

    # Load up bandpass
    imsimBand = Bandpass()
    imsimBand.imsimBandpass()

    bps = lsst_filters()

    bps.update(stubb_fitlers())

    names = ['id', 'sourceExtendedId', 'raj2000', 'dej2000', 'u_truncated', 'u', 'g', 'r',
             'i', 'z', 'y', 'y_truncated',
             'u_truncated_noiseless', 'u_noiseless', 'g_noiseless', 'r_noiseless',
             'i_noiseless', 'z_noiseless', 'y_noiseless', 'y_truncated_noiseless',
             'u_truncated_true', 'u_true', 'g_true', 'r_true', 'i_true', 'z_true',
             'y_true', 'y_truncated_true', 'teff', 'feH', 'logg', 'Av', 'magG']
    types = [int, '|S30']
    types.extend([float]*(len(names)-1))
    result_cat = np.zeros(nstars, dtype=zip(names, types))
    copy_keys = ['sourceExtendedId', 'raj2000', 'dej2000']
    a_x = None
    b_x = None
    maxI = float(nstars)
    for i, gems_index in enumerate(np.arange(istart, istart+nstars)):
        result_cat['id'][i] = gems_index
        for key in copy_keys:
            result_cat[key][i] = gum_cat[key][gems_index]
        # Lookup the file with the closest teff, feH, and logg to gum_cat[i]
        sed, pd = read_close_Kurucz(gum_cat['teff'][gems_index], gum_cat['feH'][gems_index],
                                   gum_cat['logg'][gems_index])
        result_cat['teff'][i] = gum_cat['teff'][gems_index]
        result_cat['feH'][i] = gum_cat['feH'][gems_index]
        result_cat['logg'][i] = gum_cat['logg'][gems_index]
        result_cat['Av'][i] = gum_cat['Av'][gems_index]
        result_cat['magG'] = gum_cat['magG'][gems_index]
        # Let's figure out the GAIA-G to SDSS g conversion. Assume SDSS_g ~ LSST_g
        # poly fit from https://arxiv.org/pdf/1008.0815.pdf
        g_mag = sed.calcMag(bps['g'])
        i_mag = sed.calcMag(bps['i'])
        g_i = g_mag - i_mag
        gnorm = gum_cat['magG'][gems_index] + 0.0094 + 0.531*(g_i) + 0.0974*(g_i)**2 - 0.0052*(g_i)**3
        if a_x is None:
            a_x, b_x = sed.setupCCMab()
        sed.addCCMDust(a_x, b_x, A_v=gum_cat['Av'][gems_index])
        # do the magnorm here
        fNorm = sed.calcFluxNorm(gnorm, bps['g'])
        sed.multiplyFluxNorm(fNorm)
        # Observe sed with GAIA, both with and without noise
        # Wrap in a try block so if ULYSSES fails for some reason the star just gets skipped
        
        gaia_observed = SED2GAIA(sed, workdir=workdir)
        tdiff = np.abs(response_temperatures - result_cat['teff'][i])
        tmatch = np.where(tdiff == tdiff.min())[0]
        observed_sed = ulysses2SED(data=gaia_observed, response=responses[tmatch])
        not_nan = ~np.isnan(observed_sed.flambda)
        # Let's interpolate out any nans
        observed_sed.flambda = np.interp(observed_sed.wavelen, observed_sed.wavelen[not_nan],
                                         observed_sed.flambda[not_nan])

        for filtername in bps:
            try:
                result_cat[filtername][i] = observed_sed.calcMag(bps[filtername])
            except:
                pass
            result_cat[filtername+'_true'][i] = sed.calcMag(bps[filtername])

        # Now do it on a noiseless spectra
        gaia_observed = SED2GAIA(sed, workdir=workdir, noise=0)
        observed_sed = ulysses2SED(data=gaia_observed, response=responses[tmatch], noisy=False)
        not_nan = ~np.isnan(observed_sed.flambda)
        # Let's interpolate out any nans
        observed_sed.flambda = np.interp(observed_sed.wavelen, observed_sed.wavelen[not_nan],
                                         observed_sed.flambda[not_nan])
        for filtername in bps:
            try:
                result_cat[filtername+'_noiseless'][i] = observed_sed.calcMag(bps[filtername])
            except:
                pass

        if verbose:
            progress = i/maxI*100
            text = "\rprogress = %.1f%%"%progress
            sys.stdout.write(text)
            sys.stdout.flush()
        
        # Try to catch a failure example:
        #if (result_cat['g'][i] < 18.) & (result_cat['g'][i] > 0.):
        #    if (np.abs(result_cat['g'][i]-result_cat['g_true'][i]) > 0.75) | (np.abs(result_cat['r'][i]-result_cat['r_true'][i]) > 0.75):
        #        import matplotlib.pylab as plt
        #        import pdb ; pdb.set_trace()
    print ''

    if save:
        np.savez('%i_%i_gum_mag_cat.npz' % (istart, nstars+istart), result_cat=result_cat)
    else:
        return result_cat, sed, observed_sed
# galaxies require more calculations (which aren't part of manyMagCalc) and partly because we're using a different
# optimization (bandpass.manyMagCalc instead of sed.manyMagCalc).

# Start 'regular' magnitude calculation.

# Calculate dust extinction a_x and b_x vectors.
a_mw, b_mw = stars[starlist[0]].setupCCMab()
# Set up dictionary + arrays to hold calculated magnitude information.
mags1 = {}
for f in filterlist:
    mags1[f] = numpy.zeros(num_star, dtype="float")
# For each star (in num_star's), apply apply MW dust, fluxnorm & calculate mags.
for i in range(num_star):
    starname = starlist[star_name[i]]
    tmpstar = Sed(wavelen=stars[starname].wavelen, flambda=stars[starname].flambda)
    tmpstar.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i])
    tmpstar.multiplyFluxNorm(fluxnorm[i])
    # Note that the stars have already been matched to the bandpass wavelength grid.
    # Just want to be sure that fnu is already calculated.
    tmpstar.flambdaTofnu()
    for f in filterlist:
        mags1[f][i] = tmpstar.calcMag(lsstbp[f])
dt, t = dtime(t)
print "Calculating dust/fluxnorm/%d magnitudes with some smart usage for %d stars took %f s" % (
    len(filterlist),
    num_star,
    dt,
)


# Test Sed.manyMagCalc :
示例#11
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,
                           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.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]

            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.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]

            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)
示例#12
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,
                           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.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]

            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.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]

            np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen)
            np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda)
            np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
    def testAddingNonesToList(self):
        """
        Test what happens if you add SEDs to an SedList that have None for
        one or more of the physical parameters (i.e. galacticAv, internalAv, or redshift)
        """
        imsimBand = Bandpass()
        imsimBand.imsimBandpass()
        nSed = 10
        sedNameList_0 = self.getListOfSedNames(nSed)
        magNormList_0 = 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 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)
示例#15
0
    def test_mixed_stars(self):
        """
        Here we will test the (somewhat absurd) case of a catalog with two different bandpasses
        (lsst_ and cartoon_) in order to verify that gamma values are being cached correctly
        """

        lsst_u_band = Bandpass()
        lsst_u_band.readThroughput(os.path.join(getPackageDir('throughputs'), 'baseline', 'total_u.dat'))
        lsst_g_band = Bandpass()
        lsst_g_band.readThroughput(os.path.join(getPackageDir('throughputs'), 'baseline', 'total_g.dat'))

        obs = ObservationMetaData(bandpassName=['c_u', 'c_g', 'u', 'g'],
                                  m5=[25.0, 26.0, 15.0, 16.0])
        # make the difference in m5 between the two bandpass systems extreme
        # so that, in the unit test, we can be sure that the correct values
        # are being used for the correct getters

        db_dtype = np.dtype([('id', np.int),
                             ('raJ2000', np.float),
                             ('decJ2000', np.float),
                             ('sedFilename', str, 100),
                             ('magNorm', np.float),
                             ('galacticAv', np.float)])

        inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'testData')
        inputFile = os.path.join(inputDir, 'IndicesTestCatalogStars.txt')
        db = fileDBObject(inputFile, dtype=db_dtype, runtable='test', idColKey='id')
        catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'cartoonStarCat.txt')
        cat = CartoonStars(db, obs_metadata=obs, column_outputs=['lsst_u', 'lsst_g',
                                                                 'sigma_lsst_u', 'sigma_lsst_g'])
        cat.write_catalog(catName)

        dtype = np.dtype([(name, np.float) for name in cat._column_outputs])

        controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',')

        if os.path.exists(catName):
            os.unlink(catName)

        db_columns = db.query_columns(['id', 'raJ2000', 'decJ2000', 'sedFilename', 'magNorm', 'galacticAv'])

        sedDir = os.path.join(getPackageDir('sims_sed_library'), 'starSED', 'kurucz')

        for ix, line in enumerate(next(db_columns)):
            spectrum = Sed()
            spectrum.readSED_flambda(os.path.join(sedDir, line[3]))
            fnorm = spectrum.calcFluxNorm(line[4], self.normband)
            spectrum.multiplyFluxNorm(fnorm)
            a_x, b_x = spectrum.setupCCMab()
            spectrum.addCCMDust(a_x, b_x, A_v=line[5])
            umag = spectrum.calcMag(self.uband)
            self.assertAlmostEqual(umag, controlData['cartoon_u'][ix], 3)
            gmag = spectrum.calcMag(self.gband)
            self.assertAlmostEqual(gmag, controlData['cartoon_g'][ix], 3)
            lsst_umag = spectrum.calcMag(lsst_u_band)
            self.assertAlmostEqual(lsst_umag, controlData['lsst_u'][ix], 3)
            lsst_gmag = spectrum.calcMag(lsst_g_band)
            self.assertAlmostEqual(lsst_gmag, controlData['lsst_g'][ix], 3)
            umagError, gamma = calcMagError_m5(umag, self.uband, obs.m5['c_u'], PhotometricParameters())
            gmagError, gamma = calcMagError_m5(gmag, self.gband, obs.m5['c_g'], PhotometricParameters())
            self.assertAlmostEqual(umagError, controlData['sigma_cartoon_u'][ix], 3)
            self.assertAlmostEqual(gmagError, controlData['sigma_cartoon_g'][ix], 3)

            lsst_umagError, gamma = calcMagError_m5(lsst_umag, lsst_u_band,
                                                    obs.m5['u'], PhotometricParameters())
            lsst_gmagError, gamma = calcMagError_m5(lsst_gmag, lsst_g_band,
                                                    obs.m5['g'], PhotometricParameters())

            self.assertAlmostEqual(lsst_umagError, controlData['sigma_lsst_u'][ix], 3)
            self.assertAlmostEqual(lsst_gmagError, controlData['sigma_lsst_g'][ix], 3)
            self.assertGreater(np.abs(lsst_umagError-umagError), 0.01)
            self.assertGreater(np.abs(lsst_gmagError-gmagError), 0.01)
    def testAlternateNormalizingBandpass(self):
        """
        A reiteration of testAddingToList, but testing with a non-imsimBandpass
        normalizing bandpass
        """
        normalizingBand = Bandpass()
        normalizingBand.readThroughput(os.path.join(getPackageDir('throughputs'),'baseline','total_r.dat'))
        nSed = 10
        sedNameList_0 = self.getListOfSedNames(nSed)
        magNormList_0 = numpy.random.random_sample(nSed)*5.0 + 15.0
        internalAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1
        redshiftList_0 = numpy.random.random_sample(nSed)*5.0
        galacticAvList_0 = numpy.random.random_sample(nSed)*0.3 + 0.1
        wavelen_match = numpy.arange(300.0, 1500.0, 10.0)
        testList = SedList(sedNameList_0, magNormList_0,
                           normalizingBandpass=normalizingBand,
                           internalAvList=internalAvList_0,
                           redshiftList=redshiftList_0, galacticAvList=galacticAvList_0,
                           wavelenMatch=wavelen_match)


        sedNameList_1 = self.getListOfSedNames(nSed)
        magNormList_1 = numpy.random.random_sample(nSed)*5.0 + 15.0

        internalAvList_1 = numpy.random.random_sample(nSed)*0.3 + 0.1

        redshiftList_1 = numpy.random.random_sample(nSed)*5.0

        galacticAvList_1 = numpy.random.random_sample(nSed)*0.3 + 0.1


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

        self.assertEqual(len(testList), 2*nSed)
        numpy.testing.assert_array_equal(wavelen_match, testList.wavelenMatch)

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


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

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

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

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

            a_coeff, b_coeff = sedControl.setupCCMab()
            sedControl.addCCMDust(a_coeff, b_coeff, A_v=iav)

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

            a_coeff, b_coeff = sedControl.setupCCMab()
            sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav)

            sedTest = testList[ix]

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

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

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

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

            a_coeff, b_coeff = sedControl.setupCCMab()
            sedControl.addCCMDust(a_coeff, b_coeff, A_v=iav)

            sedControl.redshiftSED(zz, dimming=True)

            sedControl.resampleSED(wavelen_match=wavelen_match)

            a_coeff, b_coeff = sedControl.setupCCMab()
            sedControl.addCCMDust(a_coeff, b_coeff, A_v=gav)

            sedTest = testList[ix+nSed]

            numpy.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen)
            numpy.testing.assert_array_equal(sedControl.flambda, sedTest.flambda)
            numpy.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
    def 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)
示例#18
0
    def testGalaxyPhotometricUncertainties(self):
        """
        Test in the case of a catalog of galaxies
        """
        lsstDefaults = LSSTdefaults()
        galDB = testGalaxyTileDBObj(driver=self.driver,
                                    host=self.host,
                                    database=self.dbName)
        galCat = testGalaxyCatalog(galDB, obs_metadata=self.obs_metadata)
        imsimband = Bandpass()
        imsimband.imsimBandpass()
        ct = 0
        for line in galCat.iter_catalog():
            bulgeSedName = line[50]
            diskSedName = line[51]
            agnSedName = line[52]
            magNormBulge = line[53]
            magNormDisk = line[54]
            magNormAgn = line[55]
            avBulge = line[56]
            avDisk = line[57]
            redshift = line[58]

            bulgeSed = Sed()
            bulgeSed.readSED_flambda(
                os.path.join(getPackageDir('sims_sed_library'),
                             defaultSpecMap[bulgeSedName]))
            fNorm = bulgeSed.calcFluxNorm(magNormBulge, imsimband)
            bulgeSed.multiplyFluxNorm(fNorm)

            diskSed = Sed()
            diskSed.readSED_flambda(
                os.path.join(getPackageDir('sims_sed_library'),
                             defaultSpecMap[diskSedName]))
            fNorm = diskSed.calcFluxNorm(magNormDisk, imsimband)
            diskSed.multiplyFluxNorm(fNorm)

            agnSed = Sed()
            agnSed.readSED_flambda(
                os.path.join(getPackageDir('sims_sed_library'),
                             defaultSpecMap[agnSedName]))
            fNorm = agnSed.calcFluxNorm(magNormAgn, imsimband)
            agnSed.multiplyFluxNorm(fNorm)

            a_int, b_int = bulgeSed.setupCCMab()
            bulgeSed.addCCMDust(a_int, b_int, A_v=avBulge)

            a_int, b_int = diskSed.setupCCMab()
            diskSed.addCCMDust(a_int, b_int, A_v=avDisk)

            bulgeSed.redshiftSED(redshift, dimming=True)
            diskSed.redshiftSED(redshift, dimming=True)
            agnSed.redshiftSED(redshift, dimming=True)

            bulgeSed.resampleSED(wavelen_match=self.totalBandpasses[0].wavelen)
            diskSed.resampleSED(wavelen_match=bulgeSed.wavelen)
            agnSed.resampleSED(wavelen_match=bulgeSed.wavelen)

            np.testing.assert_almost_equal(bulgeSed.wavelen, diskSed.wavelen)
            np.testing.assert_almost_equal(bulgeSed.wavelen, agnSed.wavelen)

            fl = bulgeSed.flambda + diskSed.flambda + agnSed.flambda

            totalSed = Sed(wavelen=bulgeSed.wavelen, flambda=fl)

            sedList = [totalSed, bulgeSed, diskSed, agnSed]

            for i, spectrum in enumerate(sedList):
                if i == 0:
                    msgroot = 'failed on total'
                elif i == 1:
                    msgroot = 'failed on bulge'
                elif i == 2:
                    msgroot = 'failed on disk'
                elif i == 3:
                    msgroot = 'failed on agn'

                for j, b in enumerate(self.bandpasses):
                    controlSigma = calcMagError_sed(
                        spectrum,
                        self.totalBandpasses[j],
                        self.skySeds[j],
                        self.hardwareBandpasses[j],
                        FWHMeff=lsstDefaults.FWHMeff(b),
                        photParams=PhotometricParameters())

                    testSigma = line[26 + (i * 6) + j]
                    msg = '%e neq %e; ' % (testSigma, controlSigma) + msgroot
                    self.assertAlmostEqual(testSigma,
                                           controlSigma,
                                           10,
                                           msg=msg)
                    ct += 1

        self.assertGreater(ct, 0)
示例#19
0
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" \
# create the a,b arrays for all the stars (because we resampled the stars onto the
#  same wavelength range we can just calculate a/b once, and this is slow)

a, b = stars[starskeys[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(starskeys), dtype="float")
mags2 = n.empty(len(starskeys), dtype="float")
sedlist = []
for i in range(len(starskeys)):
    # make a copy of the original SED *if* you want to 'reuse' the SED for multiple magnitude
    # calculations with various fluxnorms and dust applications (otherwise just use the object
    # you instantiated above)
    tmpstar = Sed(wavelen=stars[starskeys[i]].wavelen, flambda=stars[starskeys[i]].flambda)
    tmpstar.addCCMDust(a, b, ebv=ebv[i])
    tmpstar.multiplyFluxNorm(fluxnorm[i])
    mags[i] = tmpstar.calcMag(rband)
    # This is for showing an example of the manyMagCalc function on bandpass
    sedlist.append(tmpstar)


# Now, pretend we're actually wanting to calculate the magnitude in multiple bandpasses.
# (ignore the part that uses exampleBandpass.dat as the source .. you would replace that with
#  rootdir + "total_" + filter where filter is a member of lsstfilterlist
lsstfilterlist = ["u", "g", "r", "i", "z", "y"]
lsst = {}
rootdir = "./"
for filter in lsstfilterlist:
    lsst[filter] = Bandpass()
    lsst[filter].readThroughput(rootdir + "exampleBandpass.dat")
示例#21
0
    def test_flare_magnitudes_mixed_with_dummy(self):
        """
        Test that we get the expected magnitudes out
        """
        db = MLT_test_DB(database=self.db_name, driver='sqlite')

        # load the quiescent SEDs of the objects in our catalog
        sed_list = SedList(['lte028-5.0+0.5a+0.0.BT-Settl.spec.gz'] * 4,
                           [17.1, 17.2, 17.3, 17.4],
                           galacticAvList=[2.432, 1.876, 2.654, 2.364])

        bp_dict = BandpassDict.loadTotalBandpassesFromFiles()

        # calculate the quiescent fluxes of the objects in our catalog
        baseline_fluxes = bp_dict.fluxListForSedList(sed_list)

        bb_wavelen = np.arange(100.0, 1600.0, 0.1)
        bb_flambda = blackbody_lambda(bb_wavelen * 10.0, 9000.0)

        # this data is taken from the setUpClass() classmethod above
        t0_list = [456.2, 41006.2, 117.2, 10456.2]
        av_list = [2.432, 1.876, 2.654, 2.364]
        parallax_list = np.array([0.25, 0.15, 0.3, 0.22])
        distance_list = 1.0 / (206265.0 *
                               radiansFromArcsec(0.001 * parallax_list))
        distance_list *= 3.0857e16  # convert to cm

        dtype = np.dtype([('id', int), ('u', float), ('g', float)])

        photParams = PhotometricParameters()

        ss = Sed()

        quiet_cat_name = os.path.join(self.scratch_dir,
                                      'mlt_mixed_with_dummy_quiet_cat.txt')
        flare_cat_name = os.path.join(self.scratch_dir,
                                      'mlt_mixed_with_dummy_flaring_cat.txt')

        # loop over several MJDs and verify that, to within a
        # milli-mag, our flaring model gives us the magnitudes
        # expected, given the light curves specified in
        # setUpClass()
        for mjd in (59580.0, 60000.0, 70000.0, 80000.0):

            obs = ObservationMetaData(mjd=mjd)

            quiet_cat = QuiescentCatalog(db, obs_metadata=obs)
            quiet_cat.write_catalog(quiet_cat_name)

            flare_cat = FlaringCatalogDummy(db, obs_metadata=obs)
            flare_cat._mlt_lc_file = self.mlt_lc_name
            flare_cat.write_catalog(flare_cat_name)

            quiescent_data = np.genfromtxt(quiet_cat_name,
                                           dtype=dtype,
                                           delimiter=',')
            flaring_data = np.genfromtxt(flare_cat_name,
                                         dtype=dtype,
                                         delimiter=',')

            for ix in range(len(flaring_data)):
                obj_id = flaring_data['id'][ix]
                self.assertEqual(obj_id, ix)

                msg = (
                    'failed on object %d; mjd %.2f\n u_quiet %e u_flare %e\n g_quiet %e g_flare %e'
                    % (obj_id, mjd, quiescent_data['u'][obj_id],
                       flaring_data['u'][obj_id], quiescent_data['g'][obj_id],
                       flaring_data['g'][obj_id]))

                self.assertEqual(quiescent_data['id'][obj_id],
                                 flaring_data['id'][obj_id],
                                 msg=msg)
                self.assertAlmostEqual(ss.magFromFlux(
                    baseline_fluxes[obj_id][0]),
                                       quiescent_data['u'][obj_id],
                                       3,
                                       msg=msg)
                self.assertAlmostEqual(ss.magFromFlux(
                    baseline_fluxes[obj_id][1]),
                                       quiescent_data['g'][obj_id],
                                       3,
                                       msg=msg)
                if obj_id != 3:

                    # the models below are as specified in the
                    # setUpClass() method
                    if obj_id == 0 or obj_id == 1:
                        amp = 1.0e32
                        dt = 3652.5
                        t_min = flare_cat._survey_start - t0_list[obj_id]

                        tt = mjd - t_min
                        while tt > dt:
                            tt -= dt

                        u_flux = amp * (1.0 + np.power(np.sin(tt / 100.0), 2))
                        g_flux = amp * (1.0 + np.power(np.cos(tt / 100.0), 2))
                    elif obj_id == 2:
                        amp = 2.0e31
                        dt = 365.25
                        t_min = flare_cat._survey_start - t0_list[obj_id]

                        tt = mjd - t_min
                        while tt > dt:
                            tt -= dt
                        u_flux = amp * (1.0 + np.power(np.sin(tt / 50.0), 2))
                        g_flux = amp * (1.0 + np.power(np.cos(tt / 50.0), 2))

                    # calculate the multiplicative effect of dust on a 9000K
                    # black body
                    bb_sed = Sed(wavelen=bb_wavelen, flambda=bb_flambda)
                    u_bb_flux = bb_sed.calcFlux(bp_dict['u'])
                    g_bb_flux = bb_sed.calcFlux(bp_dict['g'])
                    a_x, b_x = bb_sed.setupCCMab()
                    bb_sed.addCCMDust(a_x, b_x, A_v=av_list[obj_id])
                    u_bb_dusty_flux = bb_sed.calcFlux(bp_dict['u'])
                    g_bb_dusty_flux = bb_sed.calcFlux(bp_dict['g'])

                    dust_u = u_bb_dusty_flux / u_bb_flux
                    dust_g = g_bb_dusty_flux / g_bb_flux

                    area = 4.0 * np.pi * np.power(distance_list[obj_id], 2)
                    tot_u_flux = baseline_fluxes[obj_id][
                        0] + u_flux * dust_u * photParams.effarea / area
                    tot_g_flux = baseline_fluxes[obj_id][
                        1] + g_flux * dust_g * photParams.effarea / area

                    self.assertAlmostEqual(ss.magFromFlux(tot_u_flux),
                                           flaring_data['u'][obj_id],
                                           3,
                                           msg=msg)
                    self.assertAlmostEqual(ss.magFromFlux(tot_g_flux),
                                           flaring_data['g'][obj_id],
                                           3,
                                           msg=msg)

                    self.assertGreater(np.abs(flaring_data['g'][obj_id] -
                                              quiescent_data['g'][obj_id]),
                                       0.001,
                                       msg=msg)
                    self.assertGreater(np.abs(flaring_data['u'][obj_id] -
                                              quiescent_data['u'][obj_id]),
                                       0.001,
                                       msg=msg)
                else:
                    self.assertAlmostEqual(flaring_data['g'][obj_id],
                                           quiescent_data['g'][obj_id] + 3 *
                                           (mjd - 59580.0) / 10000.0,
                                           3,
                                           msg=msg)
                    self.assertAlmostEqual(flaring_data['u'][obj_id],
                                           quiescent_data['u'][obj_id] + 2 *
                                           (mjd - 59580.0) / 10000.0,
                                           3,
                                           msg=msg)

        if os.path.exists(quiet_cat_name):
            os.unlink(quiet_cat_name)
        if os.path.exists(flare_cat_name):
            os.unlink(flare_cat_name)
示例#22
0
# 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])
示例#23
0
    sed.readSED_flambda(full_sed_name)
    dustless.readSED_flambda(full_sed_name)
    rest_sed.readSED_flambda(full_sed_name)
    rest_sed_dustless.readSED_flambda(full_sed_name)

    f_norm = getImsimFluxNorm(sed, mag_norm)
    sed.multiplyFluxNorm(f_norm)
    dustless.multiplyFluxNorm(f_norm)

    f_norm = getImsimFluxNorm(rest_sed, rest_mag_norm)
    rest_sed.multiplyFluxNorm(f_norm)
    rest_sed_dustless.multiplyFluxNorm(f_norm)

    a_x, b_x = sed.setupCCMab()
    R_v = av/ebv
    sed.addCCMDust(a_x, b_x, ebv=ebv, R_v=R_v)
    rest_sed.addCCMDust(a_x, b_x, ebv=ebv, R_v=R_v)

    sed.redshiftSED(full_redshift, dimming=True)
    dustless.redshiftSED(full_redshift, dimming=True)

    mag_list = lsst_bp_dict.magListForSed(sed)
    dustless_list = lsst_bp_dict.magListForSed(dustless)
    rest_list = lsst_bp_dict.magListForSed(rest_sed)
    rest_dustless_list = lsst_bp_dict.magListForSed(rest_sed_dustless)

    dd = 0.0
    dd += (mag_list[0] - uu)**2
    dd += (mag_list[1] - gg)**2
    dd += (mag_list[2] - rr)**2
    dd += (mag_list[3] - ii)**2
# by simply running 'python example_fastgals.py', but on my mac it was about 1.7 times faster optimized, with
# 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)
    def testObjectPlacement(self):
        """
        Test that GalSim places objects on the correct pixel by drawing
        images, reading them in, and then comparing the flux contained in
        circles of 2 fwhm radii about the object's expected positions with
        the actual expected flux of the objects.
        """
        scratchDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace')
        catName = os.path.join(scratchDir, 'placementCatalog.dat')
        imageRoot = os.path.join(scratchDir, 'placementImage')
        dbFileName = os.path.join(scratchDir, 'placementInputCatalog.dat')

        cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData')
        camera = ReturnCamera(cameraDir)
        detector = camera[0]
        imageName = '%s_%s_u.fits' % (imageRoot, detector.getName())

        controlSed = Sed()
        controlSed.readSED_flambda(
                                   os.path.join(getPackageDir('sims_sed_library'),
                                               'flatSED','sed_flat.txt.gz')
                                   )

        uBandpass = Bandpass()
        uBandpass.readThroughput(
                                 os.path.join(getPackageDir('throughputs'),
                                              'baseline','total_u.dat')
                                )

        controlBandpass = Bandpass()
        controlBandpass.imsimBandpass()

        ff = controlSed.calcFluxNorm(self.magNorm, uBandpass)
        controlSed.multiplyFluxNorm(ff)
        a_int, b_int = controlSed.setupCCMab()
        controlSed.addCCMDust(a_int, b_int, A_v=0.1, R_v=3.1)

        nSamples = 5
        numpy.random.seed(42)
        pointingRaList = numpy.random.random_sample(nSamples)*360.0
        pointingDecList = numpy.random.random_sample(nSamples)*180.0 - 90.0
        rotSkyPosList = numpy.random.random_sample(nSamples)*360.0
        fwhmList = numpy.random.random_sample(nSamples)*1.0 + 0.3

        actualCounts = None

        for pointingRA, pointingDec, rotSkyPos, fwhm in \
        zip(pointingRaList, pointingDecList, rotSkyPosList, fwhmList):


            obs = ObservationMetaData(unrefractedRA=pointingRA,
                                      unrefractedDec=pointingDec,
                                      boundType='circle',
                                      boundLength=4.0,
                                      mjd=49250.0,
                                      rotSkyPos=rotSkyPos)

            xDisplacementList = numpy.random.random_sample(nSamples)*60.0-30.0
            yDisplacementList = numpy.random.random_sample(nSamples)*60.0-30.0
            create_text_catalog(obs, dbFileName, xDisplacementList, yDisplacementList,
                                mag_norm=[self.magNorm]*len(xDisplacementList))
            db = placementFileDBObj(dbFileName, runtable='test')
            cat = placementCatalog(db, obs_metadata=obs)
            if actualCounts is None:
                actualCounts = controlSed.calcADU(uBandpass, cat.photParams)

            psf = SNRdocumentPSF(fwhm=fwhm)
            cat.setPSF(psf)
            cat.camera = camera

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            objRaList = []
            objDecList = []
            with open(catName, 'r') as inFile:
                for line in inFile:
                    if line[0] != '#':
                        words = line.split(';')
                        objRaList.append(numpy.radians(numpy.float(words[2])))
                        objDecList.append(numpy.radians(numpy.float(words[3])))

            objRaList = numpy.array(objRaList)
            objDecList = numpy.array(objDecList)

            self.check_placement(imageName, objRaList, objDecList,
                                [fwhm]*len(objRaList),
                                numpy.array([actualCounts]*len(objRaList)),
                                cat.photParams.gain, detector, camera, obs, epoch=2000.0)

            if os.path.exists(dbFileName):
                os.unlink(dbFileName)
            if os.path.exists(catName):
                os.unlink(catName)
            if os.path.exists(imageName):
                os.unlink(imageName)
示例#26
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)
print "After scaling, magnitude of SED is now %.4f (desired magnitude was %.4f)" %(mag, mag_desired)

# And let's calculate what the expected photon counts for LSST would be.
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 ""
示例#28
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,
                           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 = 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.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]

                        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.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]

                        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)
示例#29
0
    def testObjectPlacement(self):
        """
        Test that GalSim places objects on the correct pixel by drawing
        images, reading them in, and then comparing the flux contained in
        circles of 2 fwhm radii about the object's expected positions with
        the actual expected flux of the objects.
        """
        scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testObjectPlacement-')
        catName = os.path.join(scratchDir, 'placementCatalog.dat')
        imageRoot = os.path.join(scratchDir, 'placementImage')
        dbFileName = os.path.join(scratchDir, 'placementInputCatalog.dat')

        cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData')
        camera = ReturnCamera(cameraDir)
        detector = camera[0]
        imageName = '%s_%s_u.fits' % (imageRoot, detector.getName())

        controlSed = Sed()
        controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'),
                                                'flatSED', 'sed_flat.txt.gz'))

        uBandpass = Bandpass()
        uBandpass.readThroughput(os.path.join(getPackageDir('throughputs'),
                                              'baseline', 'total_u.dat'))

        controlBandpass = Bandpass()
        controlBandpass.imsimBandpass()

        ff = controlSed.calcFluxNorm(self.magNorm, uBandpass)
        controlSed.multiplyFluxNorm(ff)
        a_int, b_int = controlSed.setupCCMab()
        controlSed.addCCMDust(a_int, b_int, A_v=0.1, R_v=3.1)

        nSamples = 3
        rng = np.random.RandomState(42)
        pointingRaList = rng.random_sample(nSamples)*360.0
        pointingDecList = rng.random_sample(nSamples)*180.0 - 90.0
        rotSkyPosList = rng.random_sample(nSamples)*360.0
        fwhmList = rng.random_sample(nSamples)*1.0 + 0.3

        actualCounts = None

        for pointingRA, pointingDec, rotSkyPos, fwhm in \
        zip(pointingRaList, pointingDecList, rotSkyPosList, fwhmList):

            obs = ObservationMetaData(pointingRA=pointingRA,
                                      pointingDec=pointingDec,
                                      boundType='circle',
                                      boundLength=4.0,
                                      mjd=49250.0,
                                      rotSkyPos=rotSkyPos)

            xDisplacementList = rng.random_sample(nSamples)*60.0-30.0
            yDisplacementList = rng.random_sample(nSamples)*60.0-30.0
            create_text_catalog(obs, dbFileName, xDisplacementList, yDisplacementList,
                                mag_norm=[self.magNorm]*len(xDisplacementList))
            db = placementFileDBObj(dbFileName, runtable='test')
            cat = placementCatalog(db, obs_metadata=obs)
            cat.camera_wrapper = GalSimCameraWrapper(camera)
            if actualCounts is None:
                actualCounts = controlSed.calcADU(uBandpass, cat.photParams)

            psf = SNRdocumentPSF(fwhm=fwhm)
            cat.setPSF(psf)

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            objRaList = []
            objDecList = []
            with open(catName, 'r') as inFile:
                for line in inFile:
                    if line[0] != '#':
                        words = line.split(';')
                        objRaList.append(np.radians(np.float(words[2])))
                        objDecList.append(np.radians(np.float(words[3])))

            objRaList = np.array(objRaList)
            objDecList = np.array(objDecList)

            self.assertGreater(len(objRaList), 0)  # make sure we aren't testing
                                                   # an empty catalog/image

            self.check_placement(imageName, objRaList, objDecList,
                                 [fwhm]*len(objRaList),
                                 np.array([actualCounts]*len(objRaList)),
                                 cat.photParams.gain, detector, camera, obs, epoch=2000.0)

            if os.path.exists(dbFileName):
                os.unlink(dbFileName)
            if os.path.exists(catName):
                os.unlink(catName)
            if os.path.exists(imageName):
                os.unlink(imageName)

        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
示例#30
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,
                           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.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]

            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.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]

            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)
#  same wavelength range we can just calculate a/b once, and this is slow)

a, b = stars[starskeys[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(starskeys), dtype='float')
mags2 = n.empty(len(starskeys), dtype='float')
sedlist = []
for i in range(len(starskeys)):
    # make a copy of the original SED *if* you want to 'reuse' the SED for multiple magnitude
    # calculations with various fluxnorms and dust applications (otherwise just use the object
    # you instantiated above)
    tmpstar = Sed(wavelen=stars[starskeys[i]].wavelen,
                  flambda=stars[starskeys[i]].flambda)
    tmpstar.addCCMDust(a, b, ebv=ebv[i])
    tmpstar.multiplyFluxNorm(fluxnorm[i])
    mags[i] = tmpstar.calcMag(rband)
    # This is for showing an example of the manyMagCalc function on bandpass
    sedlist.append(tmpstar)

# Now, pretend we're actually wanting to calculate the magnitude in multiple bandpasses.
# (ignore the part that uses exampleBandpass.dat as the source .. you would replace that with
#  rootdir + "total_" + filter where filter is a member of lsstfilterlist
lsstfilterlist = ['u', 'g', 'r', 'i', 'z', 'y']
lsst = {}
rootdir = "./"
for filter in lsstfilterlist:
    lsst[filter] = Bandpass()
    lsst[filter].readThroughput(rootdir + "exampleBandpass.dat")
    # you have to do this now - sbToPhi to use the multi-mag calc
示例#32
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)
        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,
                           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.setupCCMab()
            sedControl.addCCMDust(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,
                           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.setupCCMab()
            sedControl.addCCMDust(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,
                           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.setupCCMab()
            sedControl.addCCMDust(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,
                           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.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)

            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,
                           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.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)

            np.testing.assert_array_equal(sedControl.wavelen, sedTest.wavelen)
            np.testing.assert_array_equal(sedControl.flambda, sedTest.flambda)
            np.testing.assert_array_equal(sedControl.fnu, sedTest.fnu)
    def 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)
示例#34
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
    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)
# 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])
    
示例#37
0
def sources_from_file(file_name, obs_md, phot_params, numRows=None):
    """
    Read in an InstanceCatalog and extract all of the astrophysical
    sources from it

    Parameters
    ----------
    file_name: str
        The name of the InstanceCatalog

    obs_md: ObservationMetaData
        The ObservationMetaData characterizing the pointing

    phot_params: PhotometricParameters
        The PhotometricParameters characterizing this telescope

    numRows: int (optional)
        The number of rows of the InstanceCatalog to read in (including the
        header)

    Returns
    -------
    gs_obj_arr: numpy array
        Contains the GalSimCelestialObjects for all of the
        astrophysical sources in this InstanceCatalog

    out_obj_dict: dict
        Keyed on the names of the detectors in the LSST camera.
        The values are numpy arrays of GalSimCelestialObjects
        that should be simulated for that detector, including
        objects that are near the edge of the chip or
        just bright (in which case, they might still illuminate
        the detector).
    """

    camera = get_obs_lsstSim_camera()

    num_objects = 0
    ct_rows = 0
    with fopen(file_name, mode='rt') as input_file:
        for line in input_file:
            ct_rows += 1
            params = line.strip().split()
            if params[0] == 'object':
                num_objects += 1
            if numRows is not None and ct_rows >= numRows:
                break

    # RA, Dec in the coordinate system expected by PhoSim
    ra_phosim = np.zeros(num_objects, dtype=float)
    dec_phosim = np.zeros(num_objects, dtype=float)

    sed_name = [None] * num_objects
    mag_norm = 55.0 * np.ones(num_objects, dtype=float)
    gamma1 = np.zeros(num_objects, dtype=float)
    gamma2 = np.zeros(num_objects, dtype=float)
    kappa = np.zeros(num_objects, dtype=float)

    internal_av = np.zeros(num_objects, dtype=float)
    internal_rv = np.zeros(num_objects, dtype=float)
    galactic_av = np.zeros(num_objects, dtype=float)
    galactic_rv = np.zeros(num_objects, dtype=float)
    semi_major_arcsec = np.zeros(num_objects, dtype=float)
    semi_minor_arcsec = np.zeros(num_objects, dtype=float)
    position_angle_degrees = np.zeros(num_objects, dtype=float)
    sersic_index = np.zeros(num_objects, dtype=float)
    npoints = np.zeros(num_objects, dtype=int)
    redshift = np.zeros(num_objects, dtype=float)

    unique_id = np.zeros(num_objects, dtype=int)
    object_type = np.zeros(num_objects, dtype=int)

    i_obj = -1
    with fopen(file_name, mode='rt') as input_file:
        for line in input_file:
            params = line.strip().split()
            if params[0] != 'object':
                continue
            i_obj += 1
            if numRows is not None and i_obj >= num_objects:
                break
            unique_id[i_obj] = int(params[1])
            ra_phosim[i_obj] = float(params[2])
            dec_phosim[i_obj] = float(params[3])
            mag_norm[i_obj] = float(params[4])
            sed_name[i_obj] = params[5]
            redshift[i_obj] = float(params[6])
            gamma1[i_obj] = float(params[7])
            gamma2[i_obj] = float(params[8])
            kappa[i_obj] = float(params[9])
            if params[12].lower() == 'point':
                object_type[i_obj] = _POINT_SOURCE
                i_gal_dust_model = 14
                if params[13].lower() != 'none':
                    i_gal_dust_model = 16
                    internal_av[i_obj] = float(params[14])
                    internal_rv[i_obj] = float(params[15])
                if params[i_gal_dust_model].lower() != 'none':
                    galactic_av[i_obj] = float(params[i_gal_dust_model + 1])
                    galactic_rv[i_obj] = float(params[i_gal_dust_model + 2])
            elif params[12].lower() == 'sersic2d':
                object_type[i_obj] = _SERSIC_2D
                semi_major_arcsec[i_obj] = float(params[13])
                semi_minor_arcsec[i_obj] = float(params[14])
                position_angle_degrees[i_obj] = float(params[15])
                sersic_index[i_obj] = float(params[16])
                i_gal_dust_model = 18
                if params[17].lower() != 'none':
                    i_gal_dust_model = 20
                    internal_av[i_obj] = float(params[18])
                    internal_rv[i_obj] = float(params[19])
                if params[i_gal_dust_model].lower() != 'none':
                    galactic_av[i_obj] = float(params[i_gal_dust_model + 1])
                    galactic_rv[i_obj] = float(params[i_gal_dust_model + 2])
            elif params[12].lower() == 'knots':
                object_type[i_obj] = _RANDOM_WALK
                semi_major_arcsec[i_obj] = float(params[13])
                semi_minor_arcsec[i_obj] = float(params[14])
                position_angle_degrees[i_obj] = float(params[15])
                npoints[i_obj] = int(params[16])
                i_gal_dust_model = 18
                if params[17].lower() != 'none':
                    i_gal_dust_model = 20
                    internal_av[i_obj] = float(params[18])
                    internal_rv[i_obj] = float(params[19])
                if params[i_gal_dust_model].lower() != 'none':
                    galactic_av[i_obj] = float(params[i_gal_dust_model + 1])
                    galactic_rv[i_obj] = float(params[i_gal_dust_model + 2])
            else:
                raise RuntimeError("Do not know how to handle "
                                   "object type: %s" % params[12])

    ra_appGeo, dec_appGeo = PhoSimAstrometryBase._appGeoFromPhoSim(
        np.radians(ra_phosim), np.radians(dec_phosim), obs_md)

    (ra_obs_rad, dec_obs_rad) = _observedFromAppGeo(ra_appGeo,
                                                    dec_appGeo,
                                                    obs_metadata=obs_md,
                                                    includeRefraction=True)

    semi_major_radians = radiansFromArcsec(semi_major_arcsec)
    semi_minor_radians = radiansFromArcsec(semi_minor_arcsec)
    position_angle_radians = np.radians(position_angle_degrees)

    x_pupil, y_pupil = _pupilCoordsFromObserved(ra_obs_rad, dec_obs_rad,
                                                obs_md)

    bp_dict = BandpassDict.loadTotalBandpassesFromFiles()

    sed_dir = lsstUtils.getPackageDir('sims_sed_library')

    object_is_valid = np.array([True] * num_objects)

    invalid_objects = np.where(
        np.logical_or(
            np.logical_or(
                mag_norm > 50.0,
                np.logical_and(galactic_av == 0.0, galactic_rv == 0.0)),
            np.logical_or(
                np.logical_and(object_type == _SERSIC_2D,
                               semi_major_arcsec < semi_minor_arcsec),
                np.logical_and(object_type == _RANDOM_WALK, npoints <= 0))))

    object_is_valid[invalid_objects] = False

    if len(invalid_objects[0]) > 0:
        message = "\nOmitted %d suspicious objects from " % len(
            invalid_objects[0])
        message += "the instance catalog:\n"
        n_bad_mag_norm = len(np.where(mag_norm > 50.0)[0])
        message += "    %d had mag_norm > 50.0\n" % n_bad_mag_norm
        n_bad_av = len(
            np.where(np.logical_and(galactic_av == 0.0,
                                    galactic_rv == 0.0))[0])
        message += "    %d had galactic_Av == galactic_Rv == 0\n" % n_bad_av
        n_bad_axes = len(
            np.where(
                np.logical_and(object_type == _SERSIC_2D,
                               semi_major_arcsec < semi_minor_arcsec))[0])
        message += "    %d had semi_major_axis < semi_minor_axis\n" % n_bad_axes
        n_bad_knots = len(
            np.where(np.logical_and(object_type == _RANDOM_WALK,
                                    npoints <= 0))[0])
        message += "    %d had n_points <= 0 \n" % n_bad_knots
        warnings.warn(message)

    wav_int = None
    wav_gal = None

    gs_object_arr = []
    for i_obj in range(num_objects):
        if not object_is_valid[i_obj]:
            continue

        if object_type[i_obj] == _POINT_SOURCE:
            gs_type = 'pointSource'
        elif object_type[i_obj] == _SERSIC_2D:
            gs_type = 'sersic'
        elif object_type[i_obj] == _RANDOM_WALK:
            gs_type = 'RandomWalk'

        # load the SED
        sed_obj = Sed()
        sed_obj.readSED_flambda(os.path.join(sed_dir, sed_name[i_obj]))
        fnorm = getImsimFluxNorm(sed_obj, mag_norm[i_obj])
        sed_obj.multiplyFluxNorm(fnorm)
        if internal_av[i_obj] != 0.0:
            if wav_int is None or not np.array_equal(sed_obj.wavelen, wav_int):
                a_int, b_int = sed_obj.setupCCMab()
                wav_int = copy.deepcopy(sed_obj.wavelen)

            sed_obj.addCCMDust(a_int,
                               b_int,
                               A_v=internal_av[i_obj],
                               R_v=internal_rv[i_obj])

        if redshift[i_obj] != 0.0:
            sed_obj.redshiftSED(redshift[i_obj], dimming=True)

        sed_obj.resampleSED(wavelen_match=bp_dict.wavelenMatch)

        if galactic_av[i_obj] != 0.0:
            if wav_gal is None or not np.array_equal(sed_obj.wavelen, wav_gal):
                a_g, b_g = sed_obj.setupCCMab()
                wav_gal = copy.deepcopy(sed_obj.wavelen)

            sed_obj.addCCMDust(a_g,
                               b_g,
                               A_v=galactic_av[i_obj],
                               R_v=galactic_rv[i_obj])

        gs_object = GalSimCelestialObject(gs_type,
                                          x_pupil[i_obj],
                                          y_pupil[i_obj],
                                          semi_major_radians[i_obj],
                                          semi_minor_radians[i_obj],
                                          semi_major_radians[i_obj],
                                          position_angle_radians[i_obj],
                                          sersic_index[i_obj],
                                          sed_obj,
                                          bp_dict,
                                          phot_params,
                                          npoints[i_obj],
                                          gamma1=gamma1[i_obj],
                                          gamma2=gamma2[i_obj],
                                          kappa=kappa[i_obj],
                                          uniqueId=unique_id[i_obj])

        gs_object_arr.append(gs_object)

    gs_object_arr = np.array(gs_object_arr)

    # how close to the edge of the detector a source has
    # to be before we will just simulate it anyway
    pix_tol = 50.0

    # any source brighter than this will be considered
    # so bright that it should be simulated for all
    # detectors, just in case light scatters onto them.
    max_mag = 16.0

    # down-select mag_norm, x_pupil, and y_pupil
    # to only contain those objects that were
    # deemed to be valid above
    valid = np.where(object_is_valid)
    mag_norm = mag_norm[valid]
    x_pupil = x_pupil[valid]
    y_pupil = y_pupil[valid]

    assert len(mag_norm) == len(gs_object_arr)
    assert len(x_pupil) == len(gs_object_arr)
    assert len(y_pupil) == len(gs_object_arr)

    out_obj_dict = {}
    for det in lsst_camera():
        chip_name = det.getName()
        pixel_corners = getCornerPixels(chip_name, lsst_camera())
        x_min = pixel_corners[0][0]
        x_max = pixel_corners[2][0]
        y_min = pixel_corners[0][1]
        y_max = pixel_corners[3][1]
        xpix, ypix = pixelCoordsFromPupilCoords(x_pupil,
                                                y_pupil,
                                                chipName=chip_name,
                                                camera=lsst_camera())

        on_chip = np.where(
            np.logical_or(
                mag_norm < max_mag,
                np.logical_and(
                    xpix > x_min - pix_tol,
                    np.logical_and(
                        xpix < x_max + pix_tol,
                        np.logical_and(ypix > y_min - pix_tol,
                                       ypix < y_max + pix_tol)))))

        out_obj_dict[chip_name] = gs_object_arr[on_chip]

    return gs_object_arr, out_obj_dict
示例#38
0
    def applyMLTflaring(self, valid_dexes, params, expmjd,
                        parallax=None, ebv=None, quiescent_mags=None):
        """
        parallax, ebv, and quiescent_mags are optional kwargs for use if you are
        calling this method outside the context of an InstanceCatalog (presumably
        with a numpy array of expmjd)

        parallax is the parallax of your objects in radians

        ebv is the E(B-V) value for your objects

        quiescent_mags is a dict keyed on ('u', 'g', 'r', 'i', 'z', 'y')
        with the quiescent magnitudes of the objects
        """

        if parallax is None:
            parallax = self.column_by_name('parallax')
        if ebv is None:
            ebv = self.column_by_name('ebv')

        global _MLT_LC_NPZ
        global _MLT_LC_NPZ_NAME
        global _MLT_LC_TIME_CACHE
        global _MLT_LC_FLUX_CACHE

        # this needs to occur before loading the MLT light curve cache,
        # just in case the user wants to override the light curve cache
        # file by hand before generating the catalog
        if len(params) == 0:
            return numpy.array([[],[],[],[],[],[]])

        if quiescent_mags is None:
            quiescent_mags = {}
            for mag_name in ('u', 'g', 'r', 'i', 'z', 'y'):
                if ('lsst_%s' % mag_name in self._actually_calculated_columns or
                    'delta_lsst_%s' % mag_name in self._actually_calculated_columns):

                    quiescent_mags[mag_name] = self.column_by_name('quiescent_lsst_%s' % mag_name)

        if not hasattr(self, 'photParams'):
            raise RuntimeError("To apply MLT dwarf flaring, your "
                               "InstanceCatalog must have a member variable "
                               "photParams which is an instantiation of the "
                               "class PhotometricParameters, which can be "
                               "imported from lsst.sims.photUtils. "
                               "This is so that your InstanceCatalog has "
                               "knowledge of the effective area of the LSST "
                               "mirror.")

        if _MLT_LC_NPZ is None or _MLT_LC_NPZ_NAME != self._mlt_lc_file or _MLT_LC_NPZ.fid is None:
            if not os.path.exists(self._mlt_lc_file):
                catutils_scripts = os.path.join(getPackageDir('sims_catUtils'), 'support_scripts')
                raise RuntimeError("The MLT flaring light curve file:\n"
                                    + "\n%s\n" % self._mlt_lc_file
                                    + "\ndoes not exist."
                                    +"\n\n"
                                    + "Go into %s " % catutils_scripts
                                    + "and run get_mdwarf_flares.sh "
                                    + "to get the data")

            _MLT_LC_NPZ = numpy.load(self._mlt_lc_file)
            sims_clean_up.targets.append(_MLT_LC_NPZ)
            _MLT_LC_NPZ_NAME = self._mlt_lc_file
            _MLT_LC_TIME_CACHE = {}
            _MLT_LC_FLUX_CACHE = {}

        if not hasattr(self, '_mlt_dust_lookup'):
            # Construct a look-up table to determine the factor
            # by which to multiply the flares' flux to account for
            # dust as a function of E(B-V).  Recall that we are
            # modeling all MLT flares as 9000K blackbodies.

            if not hasattr(self, 'lsstBandpassDict'):
                raise RuntimeError('You are asking for MLT dwarf flaring '
                                   'magnitudes in a catalog that has not '
                                   'defined lsstBandpassDict.  The MLT '
                                   'flaring magnitudes model does not know '
                                   'how to apply dust extinction to the '
                                   'flares without the member variable '
                                   'lsstBandpassDict being defined.')

            ebv_grid = numpy.arange(0.0, 7.01, 0.01)
            bb_wavelen = numpy.arange(200.0, 1500.0, 0.1)
            hc_over_k = 1.4387e7  # nm*K
            temp = 9000.0  # black body temperature in Kelvin
            exp_arg = hc_over_k/(temp*bb_wavelen)
            exp_term = 1.0/(numpy.exp(exp_arg) - 1.0)
            ln_exp_term = numpy.log(exp_term)

            # Blackbody f_lambda function;
            # discard normalizing factors; we only care about finding the
            # ratio of fluxes between the case with dust extinction and
            # the case without dust extinction
            log_bb_flambda = -5.0*numpy.log(bb_wavelen) + ln_exp_term
            bb_flambda = numpy.exp(log_bb_flambda)
            bb_sed = Sed(wavelen=bb_wavelen, flambda=bb_flambda)

            base_fluxes = self.lsstBandpassDict.fluxListForSed(bb_sed)

            a_x, b_x = bb_sed.setupCCMab()
            self._mlt_dust_lookup = {}
            self._mlt_dust_lookup['ebv'] = ebv_grid
            list_of_bp = self.lsstBandpassDict.keys()
            for bp in list_of_bp:
                self._mlt_dust_lookup[bp] = numpy.zeros(len(ebv_grid))
            for iebv, ebv_val in enumerate(ebv_grid):
                wv, fl = bb_sed.addCCMDust(a_x, b_x,
                                           ebv=ebv_val,
                                           wavelen=bb_wavelen,
                                           flambda=bb_flambda)

                dusty_bb = Sed(wavelen=wv, flambda=fl)
                dusty_fluxes = self.lsstBandpassDict.fluxListForSed(dusty_bb)
                for ibp, bp in enumerate(list_of_bp):
                    self._mlt_dust_lookup[bp][iebv] = dusty_fluxes[ibp]/base_fluxes[ibp]

        # get the distance to each star in parsecs
        _au_to_parsec = 1.0/206265.0
        dd = _au_to_parsec/parallax

        # get the area of the sphere through which the star's energy
        # is radiating to get to us (in cm^2)
        _cm_per_parsec = 3.08576e16
        sphere_area = 4.0*numpy.pi*numpy.power(dd*_cm_per_parsec, 2)

        flux_factor = self.photParams.effarea/sphere_area

        if isinstance(expmjd, numbers.Number):
            dMags = numpy.zeros((6, self.num_variable_obj(params)))
        else:
            dMags = numpy.zeros((6, self.num_variable_obj(params), len(expmjd)))

        mag_name_tuple = ('u', 'g', 'r', 'i', 'z', 'y')
        base_fluxes = {}
        base_mags = {}
        ss = Sed()
        for mag_name in mag_name_tuple:
            if ('lsst_%s' % mag_name in self._actually_calculated_columns or
                'delta_lsst_%s' % mag_name in self._actually_calculated_columns):

                mm = quiescent_mags[mag_name]
                base_mags[mag_name] = mm
                base_fluxes[mag_name] = ss.fluxFromMag(mm)

        lc_name_arr = params['lc'].astype(str)
        lc_names_unique = numpy.unique(lc_name_arr)
        for lc_name in lc_names_unique:
            if 'None' in lc_name:
                continue

            use_this_lc = numpy.where(numpy.char.find(lc_name_arr, lc_name)==0)

            lc_name = lc_name.replace('.txt', '')

            # 2017 May 1
            # There isn't supposed to be a 'late_inactive' light curve.
            # Unfortunately, I (Scott Daniel) assigned 'late_inactive'
            # light curves to some of the stars on our database.  Rather
            # than fix the database table (which will take about a week of
            # compute time), I am going to fix the problem here by mapping
            # 'late_inactive' into 'late_active'.
            if 'late' in lc_name:
                lc_name = lc_name.replace('in', '')

            if lc_name in _MLT_LC_TIME_CACHE:
                raw_time_arr = _MLT_LC_TIME_CACHE[lc_name]
            else:
                raw_time_arr = _MLT_LC_NPZ['%s_time' % lc_name]
                _MLT_LC_TIME_CACHE[lc_name] = raw_time_arr

            time_arr = self._survey_start + raw_time_arr
            dt = time_arr.max() - time_arr.min()

            if isinstance(expmjd, numbers.Number):
                t_interp = (expmjd + params['t0'][use_this_lc]).astype(float)
            else:
                n_obj = len(use_this_lc[0])
                n_time = len(expmjd)
                t_interp = numpy.ones(shape=(n_obj, n_time))*expmjd
                t_interp += numpy.array([[tt]*n_time for tt in params['t0'][use_this_lc].astype(float)])

            while t_interp.max() > time_arr.max():
                bad_dexes = numpy.where(t_interp>time_arr.max())
                t_interp[bad_dexes] -= dt

            for i_mag, mag_name in enumerate(mag_name_tuple):
                if ('lsst_%s' % mag_name in self._actually_calculated_columns or
                    'delta_lsst_%s' % mag_name in self._actually_calculated_columns):

                    flux_name = '%s_%s' % (lc_name, mag_name)
                    if flux_name in _MLT_LC_FLUX_CACHE:
                        flux_arr = _MLT_LC_FLUX_CACHE[flux_name]
                    else:
                        flux_arr = _MLT_LC_NPZ[flux_name]
                        _MLT_LC_FLUX_CACHE[flux_name] = flux_arr

                    dflux = numpy.interp(t_interp, time_arr, flux_arr)

                    if isinstance(expmjd, numbers.Number):
                        dflux *= flux_factor[use_this_lc]
                    else:
                        dflux *= numpy.array([flux_factor[use_this_lc]]*n_time).transpose()

                    dust_factor = numpy.interp(ebv[use_this_lc],
                                               self._mlt_dust_lookup['ebv'],
                                               self._mlt_dust_lookup[mag_name])

                    if not isinstance(expmjd, numbers.Number):
                        dust_factor = numpy.array([dust_factor]*n_time).transpose()

                    dflux *= dust_factor

                    if isinstance(expmjd, numbers.Number):
                        local_base_fluxes = base_fluxes[mag_name][use_this_lc]
                        local_base_mags = base_mags[mag_name][use_this_lc]
                    else:
                        local_base_fluxes = numpy.array([base_fluxes[mag_name][use_this_lc]]*n_time).transpose()
                        local_base_mags = numpy.array([base_mags[mag_name][use_this_lc]]*n_time).transpose()

                    dMags[i_mag][use_this_lc] = (ss.magFromFlux(local_base_fluxes + dflux)
                                                 - local_base_mags)

        return dMags
# galaxies require more calculations (which aren't part of manyMagCalc) and partly because we're using a different
# optimization (bandpass.manyMagCalc instead of sed.manyMagCalc). 

# Start 'regular' magnitude calculation.

# Calculate dust extinction a_x and b_x vectors.
a_mw, b_mw = stars[starlist[0]].setupCCMab()
# Set up dictionary + arrays to hold calculated magnitude information. 
mags1 = {}
for f in filterlist:
    mags1[f] = numpy.zeros(num_star, dtype='float')
# For each star (in num_star's), apply apply MW dust, fluxnorm & calculate mags. 
for i in range(num_star):
    starname = starlist[star_name[i]]
    tmpstar = Sed(wavelen=stars[starname].wavelen, flambda=stars[starname].flambda)
    tmpstar.addCCMDust(a_mw, b_mw, ebv=ebv_mw[i])
    tmpstar.multiplyFluxNorm(fluxnorm[i])
    # Note that the stars have already been matched to the bandpass wavelength grid.
    # Just want to be sure that fnu is already calculated.
    tmpstar.flambdaTofnu()
    for f in filterlist:
        mags1[f][i] = tmpstar.calcMag(lsstbp[f])
dt, t = dtime(t)
print "Calculating dust/fluxnorm/%d magnitudes with some smart usage for %d stars took %f s" \
      %(len(filterlist), num_star, dt)


# Test Sed.manyMagCalc : 

# First: (re) calculate internal a/b on wavelength range required for dust extinction.
a_mw, b_mw = stars[starlist[0]].setupCCMab()  
    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
# by simply running 'python example_fastgals.py', but on my mac it was about 1.7 times faster optimized, with
# 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)