Esempio n. 1
0
    def testDeReddenMags(self):

        """Test that consistent numbers come out of deReddening procedure"""

        am = 0.5
        coeffs = np.ones(5)
        mags = np.arange(2, -3, -1)

        testDeRed = matchBase().deReddenMags(am, mags, coeffs)

        # Test Output
        np.testing.assert_equal(testDeRed, [mags-(am*coeffs)])
    def testDeReddenMags(self):

        """Test that consistent numbers come out of deReddening procedure"""

        am = 0.5
        coeffs = np.ones(5)
        mags = np.arange(2,-3,-1)

        testDeRed = matchBase().deReddenMags(am, mags, coeffs)

        #Test Output
        np.testing.assert_equal(testDeRed,[ mags-(am*coeffs)])
Esempio n. 3
0
    def __init__(self, catsim_cat, om10_cat='twinkles_lenses_v2.fits',
                 density_param=1.):
        """
        Parameters
        ----------
        catsim_cat: catsim catalog
            The results array from an instance catalog.
        om10_cat: optional, defaults to 'twinkles_tdc_rung4.fits
            fits file with OM10 catalog
        density_param: `np.float`, optioanl, defaults to 1.0
            the fraction of eligible agn objects that become lensed and should
            be between 0.0 and 1.0.

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

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

        specFileStart = 'Burst'
        for key, val in sorted(iteritems(SpecMap.subdir_map)):
            if re.match(key, specFileStart):
                galSpecDir = str(val)
        galDir = str(getPackageDir('sims_sed_library') + '/' + galSpecDir + '/')
        self.LRG_name = 'Burst.25E09.1Z.spec'
        self.LRG = Sed()
        self.LRG.readSED_flambda(str(galDir + self.LRG_name))
        #return

        #Calculate imsimband magnitudes of source galaxies for matching
        agn_sed = Sed()
        agn_fname = str(getPackageDir('sims_sed_library') + '/agnSED/agn.spec.gz')
        agn_sed.readSED_flambda(agn_fname)
        src_iband = self.lenscat['MAGI_IN']
        self.src_mag_norm = []
        for src in src_iband:
            self.src_mag_norm.append(matchBase().calcMagNorm([src],
                                                             agn_sed,
                                                             self.bandpassDict))
    def testCalcBasicColors(self):

        """Tests the calculation of the colors of an SED in given bandpasses."""

        testUtils = matchBase()
        testSED = Sed()
        testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList,
                                        bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),'sdss'),
                                        bandpassRoot = 'sdss_')

        testSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0]))
        testMags = testPhot.magListForSed(testSED)
        testColors = []
        for filtNum in range(0, len(self.filterList)-1):
            testColors.append(testMags[filtNum] - testMags[filtNum+1])

        testOutput = testUtils.calcBasicColors([testSED], testPhot)
        np.testing.assert_equal([testColors], testOutput)
Esempio n. 5
0
    def testCalcBasicColors(self):

        """Tests the calculation of the colors of an SED in given bandpasses."""

        testUtils = matchBase()
        testSED = Sed()
        testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList,
                                        bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),'sdss'),
                                        bandpassRoot = 'sdss_')

        testSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0]))
        testMags = testPhot.magListForSed(testSED)
        testColors = []
        for filtNum in range(0, len(self.filterList)-1):
            testColors.append(testMags[filtNum] - testMags[filtNum+1])

        testOutput = testUtils.calcBasicColors([testSED], testPhot)
        np.testing.assert_equal([testColors], testOutput)
    def testSEDCopyBasicColors(self):

        """Tests that when makeCopy=True in calcBasicColors the SED object is unchanged after calling
        and that colors are still accurately calculated"""

        testUtils = matchBase()
        testSED = Sed()
        copyTest = Sed()
        testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList,
                                        bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),'sdss'),
                                        bandpassRoot = 'sdss_')
        testSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0]))
        copyTest.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda)
        testLambda = copyTest.wavelen[0]
        testMags = testPhot.magListForSed(testSED)
        testColors = []
        for filtNum in range(0, len(self.filterList)-1):
            testColors.append(testMags[filtNum] - testMags[filtNum+1])
        testOutput = testUtils.calcBasicColors([copyTest], testPhot, makeCopy=True)

        self.assertEqual(testLambda, copyTest.wavelen[0])
        np.testing.assert_equal([testColors], testOutput)
Esempio n. 7
0
    def testSEDCopyBasicColors(self):

        """Tests that when makeCopy=True in calcBasicColors the SED object is unchanged after calling
        and that colors are still accurately calculated"""

        testUtils = matchBase()
        testSED = Sed()
        copyTest = Sed()
        testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList,
                                        bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),'sdss'),
                                        bandpassRoot = 'sdss_')
        testSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0]))
        copyTest.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda)
        testLambda = copyTest.wavelen[0]
        testMags = testPhot.magListForSed(testSED)
        testColors = []
        for filtNum in range(0, len(self.filterList)-1):
            testColors.append(testMags[filtNum] - testMags[filtNum+1])
        testOutput = testUtils.calcBasicColors([copyTest], testPhot, makeCopy=True)

        self.assertEqual(testLambda, copyTest.wavelen[0])
        np.testing.assert_equal([testColors], testOutput)
Esempio n. 8
0
    def testCalcMagNorm(self):

        """Tests the calculation of magnitude normalization for an SED with the given magnitudes
        in the given bandpasses."""

        testUtils = matchBase()
        bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'sdss')
        testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList,
                                                             bandpassDir = bandpassDir,
                                                             bandpassRoot = 'sdss_')

        unChangedSED = Sed()
        unChangedSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0]))

        imSimBand = Bandpass()
        imSimBand.imsimBandpass()
        testSED = Sed()
        testSED.setSED(unChangedSED.wavelen, flambda = unChangedSED.flambda)
        magNorm = 20.0
        redVal = 0.1
        testSED.redshiftSED(redVal)
        fluxNorm = testSED.calcFluxNorm(magNorm, imSimBand)
        testSED.multiplyFluxNorm(fluxNorm)
        sedMags = testPhot.magListForSed(testSED)
        stepSize = 0.001
        testMagNorm = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, redshift = redVal)
        # Test adding in mag_errors. If an array of np.ones is passed in we should get same result
        testMagNormWithErr = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot,
                                                   mag_error = np.ones(len(sedMags)), redshift = redVal)
        # Also need to add in test for filtRange
        sedMagsIncomp = sedMags
        sedMagsIncomp[1] = None
        filtRangeTest = [0, 2, 3, 4]
        testMagNormFiltRange = testUtils.calcMagNorm(sedMagsIncomp, unChangedSED, testPhot,
                                                     redshift = redVal, filtRange = filtRangeTest)
        self.assertAlmostEqual(magNorm, testMagNorm, delta = stepSize)
        self.assertAlmostEqual(magNorm, testMagNormWithErr, delta = stepSize)
        self.assertAlmostEqual(magNorm, testMagNormFiltRange, delta = stepSize)
    def testCalcMagNorm(self):

        """Tests the calculation of magnitude normalization for an SED with the given magnitudes
        in the given bandpasses."""

        testUtils = matchBase()
        testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList,
                                        bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),'sdss'),
                                        bandpassRoot = 'sdss_')

        unChangedSED = Sed()
        unChangedSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0]))

        imSimBand = Bandpass()
        imSimBand.imsimBandpass()
        testSED = Sed()
        testSED.setSED(unChangedSED.wavelen, flambda = unChangedSED.flambda)
        magNorm = 20.0
        redVal = 0.1
        testSED.redshiftSED(redVal)
        fluxNorm = testSED.calcFluxNorm(magNorm, imSimBand)
        testSED.multiplyFluxNorm(fluxNorm)
        sedMags = testPhot.magListForSed(testSED)
        stepSize = 0.001
        testMagNorm = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, redshift = redVal)
        #Test adding in mag_errors. If an array of np.ones is passed in we should get same result
        testMagNormWithErr = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot,
                                                   mag_error = np.ones(len(sedMags)), redshift = redVal)
        #Also need to add in test for filtRange
        sedMagsIncomp = sedMags
        sedMagsIncomp[1] = None
        filtRangeTest = [0, 2, 3, 4]
        testMagNormFiltRange = testUtils.calcMagNorm(sedMagsIncomp, unChangedSED, testPhot,
                                                     redshift = redVal, filtRange = filtRangeTest)
        self.assertAlmostEqual(magNorm, testMagNorm, delta = stepSize)
        self.assertAlmostEqual(magNorm, testMagNormWithErr, delta = stepSize)
        self.assertAlmostEqual(magNorm, testMagNormFiltRange, delta = stepSize)
Esempio n. 10
0
    def __init__(self,
                 catsim_cat,
                 visit_mjd,
                 specFileMap,
                 sed_path,
                 om10_cat='twinkles_lenses_v2.fits',
                 sne_cat='dc2_sne_cat.csv',
                 density_param=1.,
                 cached_sprinkling=False,
                 agn_cache_file=None,
                 sne_cache_file=None,
                 defs_file=None):
        """
        Parameters
        ----------
        catsim_cat: catsim catalog
            The results array from an instance catalog.
        visit_mjd: float
            The mjd of the visit
        specFileMap: 
            This will tell the instance catalog where to write the files
        om10_cat: optional, defaults to 'twinkles_lenses_v2.fits
            fits file with OM10 catalog
        sne_cat: optional, defaults to 'dc2_sne_cat.csv'
        density_param: `np.float`, optioanl, defaults to 1.0
            the fraction of eligible agn objects that become lensed and should
            be between 0.0 and 1.0.
        cached_sprinkling: boolean
            If true then pick from a preselected list of galtileids
        agn_cache_file: str
        sne_cache_file: str
        defs_file: str

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

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

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

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

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

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

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

        #Calculate imsimband magnitudes of source galaxies for matching

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

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

                        updated_catalog = np.append(updated_catalog, lensrow)

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

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

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

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

        return updated_catalog
Esempio n. 12
0
    def sprinkle(self):
        # Define a list that we can write out to a text file
        lenslines = []
        # For each galaxy in the catsim catalog
        updated_catalog = self.catalog.copy()
        print("Running sprinkler. Catalog Length: ", len(self.catalog))
        for rowNum, row in enumerate(self.catalog):
            if rowNum == 100 or rowNum % 100000==0:
                print("Gone through ", rowNum, " lines of catalog.")
            if not np.isnan(row['galaxyAgn_magNorm']):
                candidates = self.find_lens_candidates(row['galaxyAgn_redshift'],
                                                       row['galaxyAgn_magNorm'])
                varString = json.loads(row['galaxyAgn_varParamStr'])
                varString['pars']['t0_mjd'] = 59300.0
                row['galaxyAgn_varParamStr'] = json.dumps(varString)
                np.random.seed(row['galtileid'] % (2^32 -1))
                pick_value = np.random.uniform()
            # If there aren't any lensed sources at this redshift from OM10 move on the next object
                if ((len(candidates) > 0) and (pick_value <= self.density_param)):
                    # Randomly choose one the lens systems
                    # (can decide with or without replacement)
                    # Sort first to make sure the same choice is made every time
                    candidates = candidates[np.argsort(candidates['twinklesId'])]
                    newlens = np.random.choice(candidates)

                    # Append the lens galaxy
                    # For each image, append the lens images
                    for i in range(newlens['NIMG']):
                        lensrow = row.copy()
                        # XIMG and YIMG are in arcseconds
                        # raPhSim and decPhoSim are in radians
                        #Shift all parts of the lensed object, not just its agn part
                        for lensPart in ['galaxyBulge', 'galaxyDisk', 'galaxyAgn']:
                            lens_ra = lensrow[str(lensPart+'_raJ2000')]
                            lens_dec = lensrow[str(lensPart+'_decJ2000')]
                            delta_ra = np.radians(newlens['XIMG'][i] / 3600.0) / np.cos(lens_dec)
                            delta_dec = np.radians(newlens['YIMG'][i] / 3600.0)
                            lensrow[str(lensPart + '_raJ2000')] = lens_ra + delta_ra
                            lensrow[str(lensPart + '_decJ2000')] = lens_dec + delta_dec
                        mag_adjust = 2.5*np.log10(np.abs(newlens['MAG'][i]))
                        lensrow['galaxyAgn_magNorm'] -= mag_adjust
                        varString = json.loads(lensrow['galaxyAgn_varParamStr'])
                        varString['pars']['t0Delay'] = newlens['DELAY'][i]
                        varString['varMethodName'] = 'applyAgnTimeDelay'
                        lensrow['galaxyAgn_varParamStr'] = json.dumps(varString)
                        lensrow['galaxyDisk_majorAxis'] = 0.0
                        lensrow['galaxyDisk_minorAxis'] = 0.0
                        lensrow['galaxyDisk_positionAngle'] = 0.0
                        lensrow['galaxyDisk_internalAv'] = 0.0
                        lensrow['galaxyDisk_magNorm'] = np.nan
                        lensrow['galaxyDisk_sedFilename'] = None
                        lensrow['galaxyBulge_majorAxis'] = 0.0
                        lensrow['galaxyBulge_minorAxis'] = 0.0
                        lensrow['galaxyBulge_positionAngle'] = 0.0
                        lensrow['galaxyBulge_internalAv'] = 0.0
                        lensrow['galaxyBulge_magNorm'] = np.nan
                        lensrow['galaxyBulge_sedFilename'] = None
                        lensrow['galaxyBulge_redshift'] = newlens['ZSRC']
                        lensrow['galaxyDisk_redshift'] = newlens['ZSRC']
                        lensrow['galaxyAgn_redshift'] = newlens['ZSRC']
                        #To get back twinklesID in lens catalog from phosim catalog id number
                        #just use np.right_shift(phosimID-28, 10). Take the floor of the last
                        #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is
                        #the image number minus 1.
                        lensrow['galtileid'] = (lensrow['galtileid']*10000 +
                                                newlens['twinklesId']*4 + i)

                        updated_catalog = np.append(updated_catalog, lensrow)

                    #Now manipulate original entry to be the lens galaxy with desired properties
                    #Start by deleting Disk and AGN properties
                    if not np.isnan(row['galaxyDisk_magNorm']):
                        row['galaxyDisk_majorAxis'] = 0.0
                        row['galaxyDisk_minorAxis'] = 0.0
                        row['galaxyDisk_positionAngle'] = 0.0
                        row['galaxyDisk_internalAv'] = 0.0
                        row['galaxyDisk_magNorm'] = np.nan
                        row['galaxyDisk_sedFilename'] = None
                    row['galaxyAgn_magNorm'] = np.nan
                    row['galaxyAgn_sedFilename'] = None
                    #Now insert desired Bulge properties
                    row['galaxyBulge_sedFilename'] = newlens['lens_sed']
                    row['galaxyBulge_redshift'] = newlens['ZLENS']
                    row['galaxyDisk_redshift'] = newlens['ZLENS']
                    row['galaxyAgn_redshift'] = newlens['ZLENS']
                    row['galaxyBulge_magNorm'] = matchBase().calcMagNorm([newlens['APMAG_I']], self.LRG, self.bandpassDict) #Changed from i band to imsimband
                    row['galaxyBulge_majorAxis'] = radiansFromArcsec(newlens['REFF'] / np.sqrt(1 - newlens['ELLIP']))
                    row['galaxyBulge_minorAxis'] = radiansFromArcsec(newlens['REFF'] * np.sqrt(1 - newlens['ELLIP']))
                    #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians
                    row['galaxyBulge_positionAngle'] = newlens['PHIE']*(-1.0)*np.pi/180.0
                    #Replace original entry with new entry
                    updated_catalog[rowNum] = row

        return updated_catalog
Esempio n. 13
0
    def sprinkle(self):
        # Define a list that we can write out to a text file
        lenslines = []
        # For each galaxy in the catsim catalog
        updated_catalog = self.catalog.copy()
        print("Running sprinkler. Catalog Length: ", len(self.catalog))
        for rowNum, row in enumerate(self.catalog):
            if rowNum == 100 or rowNum % 100000 == 0:
                print("Gone through ", rowNum, " lines of catalog.")
            if not np.isnan(row['galaxyAgn_magNorm']):
                candidates = self.find_lens_candidates(
                    row['galaxyAgn_redshift'])
                varString = json.loads(row['galaxyAgn_varParamStr'])
                varString['pars']['t0_mjd'] = 59300.0
                row['galaxyAgn_varParamStr'] = json.dumps(varString)
                np.random.seed(row['galtileid'] % (2 ^ 32 - 1))
                pick_value = np.random.uniform()
                # If there aren't any lensed sources at this redshift from OM10 move on the next object
                if ((len(candidates) > 0)
                        and (pick_value <= self.density_param)):
                    # Randomly choose one the lens systems
                    # (can decide with or without replacement)
                    newlens = np.random.choice(candidates)

                    # Append the lens galaxy
                    # For each image, append the lens images
                    for i in range(newlens['NIMG']):
                        lensrow = row.copy()
                        # XIMG and YIMG are in arcseconds
                        # raPhSim and decPhoSim are in radians
                        #Shift all parts of the lensed object, not just its agn part
                        for lensPart in [
                                'galaxyBulge', 'galaxyDisk', 'galaxyAgn'
                        ]:
                            lensrow[str(lensPart + '_raJ2000')] += np.radians(
                                newlens['XIMG'][i] / (np.cos(
                                    np.radians(lensrow[str(lensPart +
                                                           '_decJ2000')])) *
                                                      3600.))
                            lensrow[str(lensPart + '_decJ2000')] += np.radians(
                                newlens['YIMG'][i] / 3600.)
                        mag_adjust = 2.5 * np.log10(np.abs(newlens['MAG'][i]))
                        lensrow['galaxyAgn_magNorm'] -= mag_adjust
                        varString = json.loads(
                            lensrow['galaxyAgn_varParamStr'])
                        varString['pars']['t0Delay'] = newlens['DELAY'][i]
                        varString['varMethodName'] = 'applyAgnTimeDelay'
                        lensrow['galaxyAgn_varParamStr'] = json.dumps(
                            varString)
                        lensrow['galaxyDisk_majorAxis'] = 0.0
                        lensrow['galaxyDisk_minorAxis'] = 0.0
                        lensrow['galaxyDisk_positionAngle'] = 0.0
                        lensrow['galaxyDisk_internalAv'] = 0.0
                        lensrow['galaxyDisk_magNorm'] = np.nan
                        lensrow['galaxyDisk_sedFilename'] = None
                        lensrow['galaxyBulge_majorAxis'] = 0.0
                        lensrow['galaxyBulge_minorAxis'] = 0.0
                        lensrow['galaxyBulge_positionAngle'] = 0.0
                        lensrow['galaxyBulge_internalAv'] = 0.0
                        lensrow['galaxyBulge_magNorm'] = np.nan
                        lensrow['galaxyBulge_sedFilename'] = None
                        lensrow['galaxyBulge_redshift'] = newlens['ZSRC']
                        lensrow['galaxyDisk_redshift'] = newlens['ZSRC']
                        lensrow['galaxyAgn_redshift'] = newlens['ZSRC']
                        #To get back twinklesID in lens catalog from phosim catalog id number
                        #just use np.right_shift(phosimID-28, 10). Take the floor of the last
                        #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is
                        #the image number minus 1.
                        lensrow['galtileid'] = (lensrow['galtileid'] * 10000 +
                                                newlens['twinklesId'] * 4 + i)

                        updated_catalog = np.append(updated_catalog, lensrow)

                    #Now manipulate original entry to be the lens galaxy with desired properties
                    #Start by deleting Disk and AGN properties
                    if not np.isnan(row['galaxyDisk_magNorm']):
                        row['galaxyDisk_majorAxis'] = 0.0
                        row['galaxyDisk_minorAxis'] = 0.0
                        row['galaxyDisk_positionAngle'] = 0.0
                        row['galaxyDisk_internalAv'] = 0.0
                        row['galaxyDisk_magNorm'] = np.nan
                        row['galaxyDisk_sedFilename'] = None
                    row['galaxyAgn_magNorm'] = np.nan
                    row['galaxyAgn_sedFilename'] = None
                    #Now insert desired Bulge properties
                    row['galaxyBulge_sedFilename'] = self.LRG_name
                    row['galaxyBulge_redshift'] = newlens['ZLENS']
                    row['galaxyDisk_redshift'] = newlens['ZLENS']
                    row['galaxyAgn_redshift'] = newlens['ZLENS']
                    row['galaxyBulge_magNorm'] = matchBase().calcMagNorm(
                        [newlens['APMAG_I']], self.LRG,
                        self.bandpassDict)  #Changed from i band to imsimband
                    newlens[
                        'REFF'] = 1.0  #Hard coded for now. See issue in OM10 github.
                    row['galaxyBulge_majorAxis'] = radiansFromArcsec(
                        newlens['REFF'])
                    row['galaxyBulge_minorAxis'] = radiansFromArcsec(
                        newlens['REFF'] * (1 - newlens['ELLIP']))
                    #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians
                    row['galaxyBulge_positionAngle'] = newlens['PHIE'] * (
                        -1.0) * np.pi / 180.0
                    #Replace original entry with new entry
                    updated_catalog[rowNum] = row

        return updated_catalog