예제 #1
0
def examplePhoSimCatalogs():
    """
    This method outputs several phoSim input files consisting of different
    types of objects(stars, galaxy bulges, galaxy disks, and AGNs)

    The files created are
    catalog_test_msstars.dat
    catalog_test_galaxyDisk.dat
    catalog_test_galaxyBulge.dat
    catalog_test_galaxyAgn.dat

    (versions are also created that end in _chunked.dat; these should have
    the same contents)

    """
    obsMD = bcm.OpSim3_61DBObject()
    obs_metadata_list = obsMD.getObservationMetaData((55.0, -20.0), 1.0, fovRadius=0.1, makeCircBounds=True)
    obs_metadata = obs_metadata_list[0]
    objectDict = {}
    objectDict['testStars'] = {'dbobj':CatalogDBObject.from_objid('msstars'),
                               'constraint':None,
                               'filetype':'phoSim_catalog_POINT',
                               'obsMetadata':obs_metadata}
    objectDict['testGalaxyBulge'] = {'dbobj':CatalogDBObject.from_objid('galaxyBulge'),
                               'constraint':"mass_bulge > 1. and sedname_bulge is not NULL",
                               'filetype':'phoSim_catalog_SERSIC2D',
                               'obsMetadata':obs_metadata}
    objectDict['testGalaxyDisk'] = {'dbobj':CatalogDBObject.from_objid('galaxyDisk'),
                               'constraint':"DiskLSSTg < 20. and sedname_disk is not NULL",
                               'filetype':'phoSim_catalog_SERSIC2D',
                               'obsMetadata':obs_metadata}
    objectDict['testGalaxyAgn'] = {'dbobj':CatalogDBObject.from_objid('galaxyAgn'),
                               'constraint':"sedname_agn is not NULL",
                               'filetype':'phoSim_catalog_ZPOINT',
                               'obsMetadata':obs_metadata}

    for objKey in objectDict:
        dbobj = objectDict[objKey]['dbobj']
        t = dbobj.getCatalog(objectDict[objKey]['filetype'],
                             obs_metadata=objectDict[objKey]['obsMetadata'],
                             constraint=objectDict[objKey]['constraint'])

        t.phoSimHeaderMap = {}  # technically only needed for PhoSim InstanceCatalog classes

        print()
        print("These are the required columns from the database:")
        print(t.db_required_columns())
        print()
        print("These are the columns that will be output to the file:")
        print(t.column_outputs)
        print()

        filename = 'catalog_test_%s.dat'%(dbobj.objid)
        print("querying and writing catalog to %s:" % filename)
        t.write_catalog(filename)
        filename = 'catalog_test_%s_chunked.dat'%(dbobj.objid)
        t.write_catalog(filename, chunk_size=10)
        print(" - finished")
예제 #2
0
        def testCanBeNull(self):
            """
            Test to make sure that we can still write all rows to catalogs,
            even those with null values in key columns
            """
            dbobj = CatalogDBObject.from_objid('cannotBeNull')
            cat = dbobj.getCatalog('canBeNull')
            fileName = os.path.join(self.scratch_dir, 'canBeNullTestFile.txt')
            cat.write_catalog(fileName)
            dtype = np.dtype([('id', int), ('n1', np.float64), ('n2', np.float64), ('n3', np.float64),
                              ('n4', (str_type, 40)), ('n5', (str_type, 40))])
            testData = np.genfromtxt(fileName, dtype=dtype, delimiter=',')

            for i in range(len(self.baselineOutput)):
                # make sure that all of the rows in self.baselineOutput are represented in
                # testData
                for (k, xx) in enumerate(self.baselineOutput[i]):
                    if k < 4:
                        if not np.isnan(xx):
                            self.assertAlmostEqual(xx, testData[i][k], 3)
                        else:
                            np.testing.assert_equal(testData[i][k], np.NaN)
                    else:
                        msg = '%s is not %s' % (xx, testData[i][k])
                        self.assertEqual(xx.strip(), testData[i][k].strip(), msg=msg)

            self.assertEqual(i, 99)
            self.assertEqual(len(testData), len(self.baselineOutput))

            if os.path.exists(fileName):
                os.unlink(fileName)
예제 #3
0
def exampleAirmass(airmass,ra = 0.0, dec = 0.0, tol = 30.0, radiusDeg = 0.1,
            makeBoxBounds=False, makeCircBounds=True):
    """
    This method will output a catalog of stars based on an OpSim pointing with
    a specific airmass.  It searches OpSim for pointings with the specified airmass
    and RA, Dec a circle on the sky of radius 'tol' centred on 'ra', 'dec'.
    It creates observation metadata out of the first pointing found and
    uses that to construct the catalog.

    The catalog is output to stars_airmass_test.dat
    """

    obsMD=bcm.OpSim3_61DBObject()

    #The code below will query the OpSim data base object created above.
    #The query will be based on a box in RA, Dec and a specific airmass value

    airmassConstraint = "airmass="+str(airmass) #an SQL constraint that the airmass must be equal to
                                                #the passed value

    #convert q into observation meta data for use in a catalog
    obsMetaData_list = obsMD.getObservationMetaData((ra, dec), tol, constraint=airmassConstraint,
                                                    fovRadius=radiusDeg,makeBoxBounds=makeBoxBounds,
                                                    makeCircBounds=makeCircBounds)

    obsMetaData = obsMetaData_list[0]

    #create and output a reference catalog of stars based on our query to opSim
    dbobj = CatalogDBObject.from_objid('allstars')
    catalog = dbobj.getCatalog('ref_catalog_star', obs_metadata = obsMetaData)
    catalog.write_catalog('stars_airmass_test.dat')
    def testCanBeNull(self):
        """
            Test to make sure that we can still write all rows to catalogs,
            even those with null values in key columns
            """
        dbobj = CatalogDBObject.from_objid('cannotBeNull')
        cat = dbobj.getCatalog('canBeNull')
        fileName = os.path.join(self.scratch_dir, 'canBeNullTestFile.txt')
        cat.write_catalog(fileName)
        dtype = np.dtype([('id', int), ('n1', np.float64), ('n2', np.float64),
                          ('n3', np.float64), ('n4', (str_type, 40)),
                          ('n5', (str_type, 40))])
        testData = np.genfromtxt(fileName, dtype=dtype, delimiter=',')

        for i in range(len(self.baselineOutput)):
            # make sure that all of the rows in self.baselineOutput are represented in
            # testData
            for (k, xx) in enumerate(self.baselineOutput[i]):
                if k < 4:
                    if not np.isnan(xx):
                        self.assertAlmostEqual(xx, testData[i][k], 3)
                    else:
                        np.testing.assert_equal(testData[i][k], np.NaN)
                else:
                    msg = '%s is not %s' % (xx, testData[i][k])
                    self.assertEqual(xx.strip(),
                                     testData[i][k].strip(),
                                     msg=msg)

        self.assertEqual(i, 99)
        self.assertEqual(len(testData), len(self.baselineOutput))

        if os.path.exists(fileName):
            os.unlink(fileName)
예제 #5
0
        def testCannotBeNull_pre_screen(self):
            """
            Check that writing a catalog with self._pre_screen = True produces
            the same results as writing one with self._pre_screen = False.
            """

            # each of these classes flags a different column with a different datatype as cannot_be_null
            availableCatalogs = [floatCannotBeNullCatalog, strCannotBeNullCatalog, unicodeCannotBeNullCatalog,
                                 severalCannotBeNullCatalog]
            dbobj = CatalogDBObject.from_objid('cannotBeNull')

            for catClass in availableCatalogs:
                cat = catClass(dbobj)
                cat._pre_screen = True
                control_cat = catClass(dbobj)
                fileName = os.path.join(self.scratch_dir, 'cannotBeNullTestFile_prescreen.txt')
                control_fileName = os.path.join(self.scratch_dir, 'cannotBeNullTestFile_prescreen_control.txt')
                cat.write_catalog(fileName)
                control_cat.write_catalog(control_fileName)

                with open(fileName, 'r') as test_file:
                    test_lines = test_file.readlines()
                    with open(control_fileName, 'r') as control_file:
                        control_lines = control_file.readlines()
                        for line in control_lines:
                            self.assertIn(line, test_lines)
                        for line in test_lines:
                            self.assertIn(line, control_lines)

                if os.path.exists(fileName):
                    os.unlink(fileName)
                if os.path.exists(control_fileName):
                    os.unlink(control_fileName)
예제 #6
0
def make_refcat(opsim_db, obsHistID, boundLength, outfile,
                catsim_db_info=None, chunk_size=20000):
    """
    Create a reference catalog of stars to use for astrometry from the
    CatSim db tables.

    Parameters
    ----------
    opsim_db : str
        OpSim database sqlite file
    obsHistID : int
        Visit number to provide the center of the extraction region.
    boundLength : float
        Radius of the extraction region in units of degrees.
    outfile : str
        Filename for the reference catalog output file.
    catsim_db_info : dict, optional
        Connection information (host, port, database, driver) for the CatSim
        database.  Default: connection info for the UW fatboy server.
    chunk_size : int, optional
        The memory chunk size to pass to InstanceCatalog.write_catalog
    """
    if catsim_db_info is None:
        catsim_db_info = catsim_uw
    generator = ObservationMetaDataGenerator(database=opsim_db, driver='sqlite')
    obs_metadata = generator.getObservationMetaData(obsHistID=obsHistID,
                                                    boundLength=boundLength)[0]
    stars = CatalogDBObject.from_objid('allstars', **catsim_db_info)
    ref_stars = SimulationReference(stars, obs_metadata=obs_metadata)
    ref_stars.write_catalog(outfile, write_mode='w', write_header=True,
                            chunk_size=chunk_size)
예제 #7
0
    def make_instance_catalog(self, obsHistID, band, boundLength, outfile=None):
        """
        Method to create instance catalogs.

        Parameters
        ----------
        obsHistID : int
            obsHistID for the desired visit from the opsim db file.

        band : str
            Desired LSST filter to use, ugrizy.

        boundLength : float
            Radius in degrees of sky cone in which to produce objects.

        outfile : str, optional
            File name of the instance catalog to be produced.  If None,
            a default name will be generated, e.g.,
            phosim_input_0000230_r_0.3deg.txt.
        """
        if outfile is None:
            outfile = 'phosim_input_%07i_%s_%.1fdeg.txt' % (obsHistID, band,
                                                            boundLength)
            obs_md = self.gen.getObservationMetaData(obsHistID=obsHistID,
                                                     boundLength=boundLength)[0]
        do_header = True
        for objid in self.star_objs:
            self.logger.info("processing %s", objid)
            db_obj = CatalogDBObject.from_objid(objid, **self.db_config)
            phosim_object = PhoSimCatalogPoint(db_obj, obs_metadata=obs_md)
            if do_header:
                with open(outfile, 'w') as file_obj:
                    phosim_object.write_header(file_obj)
                do_header = False
            phosim_object.write_catalog(outfile, write_mode='a',
                                        write_header=False,
                                        chunk_size=20000)

        for objid in self.gal_objs:
            self.logger.info("processing %s", objid)
            db_obj = CatalogDBObject.from_objid(objid, **self.db_config)
            phosim_object = PhoSimCatalogSersic2D(db_obj, obs_metadata=obs_md)
            phosim_object.write_catalog(outfile, write_mode='a',
                                        write_header=False,
                                        chunk_size=20000)
예제 #8
0
    def testEb(self):
        cat_name = os.path.join(self.scratch_dir, 'ebTestCatalog.dat')
        makeEbTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('ebTest', database=self.variability_db)
        myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata)
        myCatalog.write_catalog(cat_name, chunk_size=1000)

        self.verify_catalogs(cat_name)
        if os.path.exists(cat_name):
            os.unlink(cat_name)
예제 #9
0
    def testEb(self):
        cat_name = os.path.join(self.scratch_dir, 'ebTestCatalog.dat')
        makeEbTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('ebTest', database=self.variability_db)
        myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata)
        myCatalog.write_catalog(cat_name, chunk_size=1000)

        self.verify_catalogs(cat_name)
        if os.path.exists(cat_name):
            os.unlink(cat_name)
    def testCepheids(self):
        cat_name = os.path.join(self.scratch_dir, 'cepheidTestCatalog.dat')
        makeCepheidTable()
        myDB = CatalogDBObject.from_objid('cepheidTest')
        myCatalog = StellarVariabilityCatalog(myDB,
                                              obs_metadata=self.obs_metadata)
        myCatalog.write_catalog(cat_name, chunk_size=1000)

        self.verify_catalogs(cat_name)
        if os.path.exists(cat_name):
            os.unlink(cat_name)
예제 #11
0
    def testObsCat(self):
        objname = 'wdstars'
        catDir = tempfile.mkdtemp('basicAccessTest_testObsCat')
        if not os.path.exists(catDir):
            os.mkdir(catDir)
        catName = tempfile.mktemp(prefix='basicAccessTest_testObsCat',
                                  dir=catDir,
                                  suffix='.txt')

        try:
            dbobj = CatalogDBObject.from_objid(objname)
            obs_metadata = dbobj.testObservationMetaData
            # To cover the central ~raft
            obs_metadata.boundLength = 0.4
            obs_metadata.rotSkyPos = 0.0
            cat = dbobj.getCatalog('obs_star_cat', obs_metadata)
            if os.path.exists(catName):
                os.unlink(catName)
            try:
                cat.write_catalog(catName)
                dtypeList = [(name, np.float) for name in cat._column_outputs]
                testData = np.genfromtxt(catName,
                                         delimiter=', ',
                                         dtype=np.dtype(dtypeList))
                self.assertGreater(len(testData), 0)
            finally:
                if os.path.exists(catName):
                    os.unlink(catName)
                if os.path.exists(catDir):
                    shutil.rmtree(catDir, ignore_errors=True)

            print('\ntestObsCat successfully connected to fatboy')

        except:
            trace = traceback.extract_tb(sys.exc_info()[2], limit=20)
            msg = sys.exc_info()[1].args[0]
            if 'Failed to connect' in str(msg) or failedOnFatboy(trace):

                # if the exception was because of a failed connection
                # to fatboy, ignore it.

                print('\ntestObsCat failed to connect to fatboy')
                print('Sometimes that happens.  Do not worry.')

                if os.path.exists(catDir):
                    shutil.rmtree(catDir, ignore_errors=True)

                pass
            else:
                raise
예제 #12
0
    def testObsCat(self):
        objname = 'wdstars'
        catDir = tempfile.mkdtemp('basicAccessTest_testObsCat')
        if not os.path.exists(catDir):
            os.mkdir(catDir)
        catName = tempfile.mktemp(prefix='basicAccessTest_testObsCat',
                                  dir=catDir, suffix='.txt')

        try:
            dbobj = CatalogDBObject.from_objid(objname)
            obs_metadata = dbobj.testObservationMetaData
            # To cover the central ~raft
            obs_metadata.boundLength = 0.4
            obs_metadata.rotSkyPos = 0.0
            cat = dbobj.getCatalog('obs_star_cat', obs_metadata)
            if os.path.exists(catName):
                os.unlink(catName)
            try:
                cat.write_catalog(catName)
                dtypeList = [(name, np.float) for name in cat._column_outputs]
                testData = np.genfromtxt(catName, delimiter = ', ',
                                         dtype=np.dtype(dtypeList))
                self.assertGreater(len(testData), 0)
            finally:
                if os.path.exists(catName):
                    os.unlink(catName)
                if os.path.exists(catDir):
                    shutil.rmtree(catDir)

            print('\ntestObsCat successfully connected to fatboy')

        except:
            trace = traceback.extract_tb(sys.exc_info()[2], limit=20)
            msg = sys.exc_info()[1].args[0]
            if 'Failed to connect' in str(msg) or failedOnFatboy(trace):

                # if the exception was because of a failed connection
                # to fatboy, ignore it.

                print('\ntestObsCat failed to connect to fatboy')
                print('Sometimes that happens.  Do not worry.')

                if os.path.exists(catDir):
                    shutil.rmtree(catDir)

                pass
            else:
                raise
예제 #13
0
    def testAmcvn(self):
        # Note:  this test assumes that the parameters for the Amcvn variability
        # model occur in a standard varParamStr column in the database.
        # Actually, the current database of Amcvn events simply store the variability
        # parameters as independent columns in the database.
        # The varParamStr formalism is how the applyAmcvn method is written, however,
        # so that is what we will test.

        cat_name = os.path.join(self.scratch_dir, 'amcvnTestCatalog.dat')
        makeAmcvnTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('amcvnTest', database=self.variability_db)
        myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata)
        myCatalog.write_catalog(cat_name, chunk_size=1000)

        self.verify_catalogs(cat_name)
        if os.path.exists(cat_name):
            os.unlink(cat_name)
예제 #14
0
    def testAmcvn(self):
        # Note:  this test assumes that the parameters for the Amcvn variability
        # model occur in a standard varParamStr column in the database.
        # Actually, the current database of Amcvn events simply store the variability
        # parameters as independent columns in the database.
        # The varParamStr formalism is how the applyAmcvn method is written, however,
        # so that is what we will test.

        cat_name = os.path.join(self.scratch_dir, 'amcvnTestCatalog.dat')
        makeAmcvnTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('amcvnTest', database=self.variability_db)
        myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata)
        myCatalog.write_catalog(cat_name, chunk_size=1000)

        self.verify_catalogs(cat_name)
        if os.path.exists(cat_name):
            os.unlink(cat_name)
예제 #15
0
def exampleReferenceCatalog():
    """
    This method outputs a reference catalog of galaxies (i.e. a catalog of
    galaxies in which the columns are simply the data stored in the database).

    The catalog class is defined in
    python/lsst/sims/catUtils/exampleCatalogDefinitions/refCatalogExamples.py

    The catalog is output to the file test_reference.dat
    """

    obs_metadata = ObservationMetaData(boundType='circle',pointingRA=0.0,pointingDec=0.0,
                       boundLength=0.01, mjd=53850.0)
    dbobj = CatalogDBObject.from_objid('galaxyBase')

    t = dbobj.getCatalog('ref_catalog_galaxy', obs_metadata=obs_metadata)
    filename = 'test_reference.dat'
    t.write_catalog(filename, chunk_size=10)
예제 #16
0
    def testHybridVariability(self):
        """
        Test that we can generate a catalog which inherits from multiple variability mixins
        (in this case, TestVariability and VariabilityStars).  This is to make sure that
        the register_method and register_class decorators do not mangle inheritance of
        methods from mixins.
        """
        cat_name = os.path.join(self.scratch_dir, 'hybridTestCatalog.dat')
        makeHybridTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('hybridTest',
                                          database=self.variability_db)
        myCatalog = StellarVariabilityCatalogWithTest(
            myDB, obs_metadata=self.obs_metadata)

        myCatalog.write_catalog(cat_name, chunk_size=1000)
        self.verify_catalogs(cat_name)

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

        # make sure order of mixin inheritance does not matter
        myCatalog = OtherVariabilityCatalogWithTest(
            myDB, obs_metadata=self.obs_metadata)
        myCatalog.write_catalog(cat_name, chunk_size=1000)
        self.verify_catalogs(cat_name)

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

        # make sure that, if a catalog does not contain a variability method,
        # an error is thrown; verify that it contains the correct error message
        myCatalog = StellarVariabilityCatalog(myDB,
                                              obs_metadata=self.obs_metadata)

        with self.assertRaises(RuntimeError) as context:
            myCatalog.write_catalog(cat_name)

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

        expectedMessage = "Your InstanceCatalog does not contain a variability method"
        expectedMessage += " corresponding to 'testVar'"
        self.assertEqual(context.exception.args[0], expectedMessage)
예제 #17
0
    def testObsCat(self):
        objname = 'wdstars'
        catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace',
                               'testObsCat.txt')

        try:
            dbobj = CatalogDBObject.from_objid(objname)
            obs_metadata = dbobj.testObservationMetaData
            # To cover the central ~raft
            obs_metadata.boundLength = 0.4
            obs_metadata.rotSkyPos = 0.0
            cat = dbobj.getCatalog('obs_star_cat', obs_metadata)
            if os.path.exists(catName):
                os.unlink(catName)
            try:
                cat.write_catalog(catName)
                dtypeList = [(name, np.float) for name in cat._column_outputs]
                testData = np.genfromtxt(catName, delimiter = ', ',
                                         dtype=np.dtype(dtypeList))
                self.assertGreater(len(testData), 0)
            finally:
                if os.path.exists(catName):
                    os.unlink(catName)

            print('\ntestObsCat successfully connected to fatboy')

        except:
            trace = traceback.extract_tb(sys.exc_info()[2], limit=20)
            msg = sys.exc_info()[1].args[0]
            if 'Failed to connect' in msg or failedOnFatboy(trace):

                # if the exception was because of a failed connection
                # to fatboy, ignore it.

                print('\ntestObsCat failed to connect to fatboy')
                print('Sometimes that happens.  Do not worry.')

                pass
            else:
                raise
예제 #18
0
def examplePhoSimNoOpSim():
    """
    This method outputs phoSim input files based on arbitrary input coordinates
    (rather than an OpSim pointing).

    catalog_test_stars_rd.dat is a file created from a specified RA, Dec pointing

    catalog_test_stars_aa.dat is a file created from a specified Alt, Az pointing
    """
    raDeg= 15.
    decDeg = -30.
    mjd = 51999.75

    obs_metadata_rd = ObservationMetaData(boundType='circle',
                                          boundLength=0.1,
                                          mjd=mjd,
                                          pointingRA=raDeg,
                                          pointingDec=decDeg,
                                          rotSkyPos=22.0,
                                          bandpassName='g')
    az = 220.0
    alt = 79.0
    mjd = 55958.0
    obs_dummy = ObservationMetaData(mjd=mjd)
    ra, dec = raDecFromAltAz(alt, az, obs_dummy)
    obs_metadata_aa = ObservationMetaData(boundType='circle',
                                          boundLength=0.1,
                                          mjd=mjd,
                                          rotSkyPos=22.0,
                                          pointingRA=ra,
                                          pointingDec=dec,
                                          bandpassName='g')

    dbobj = CatalogDBObject.from_objid('msstars')
    t = dbobj.getCatalog('phoSim_catalog_POINT', obs_metadata= obs_metadata_rd)
    t.phoSimHeaderMap = {}
    t.write_catalog('catalog_test_stars_rd.dat')
    t = dbobj.getCatalog('phoSim_catalog_POINT', obs_metadata= obs_metadata_aa)
    t.phoSimHeaderMap = {}
    t.write_catalog('catalog_test_stars_aa.dat')
예제 #19
0
    def testAgn(self):
        """
        Just verify that the catalog generation code runs in this case
        """

        cat_name = os.path.join(self.scratch_dir, 'agnTestCatalog.dat')
        makeAgnTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('agnTest', database=self.variability_db)
        obs = ObservationMetaData(pointingRA=self.obs_metadata.pointingRA,
                                  pointingDec=self.obs_metadata.pointingDec,
                                  boundType=self.obs_metadata.boundType,
                                  boundLength=self.obs_metadata.boundLength,
                                  mjd=60000.0)
        myCatalog = GalaxyVariabilityCatalog(myDB, obs_metadata=obs)
        myCatalog.write_catalog(cat_name, chunk_size=1000)

        with open(cat_name,'r') as input_file:
            lines = input_file.readlines()
            self.assertGreater(len(lines), 10)

        if os.path.exists(cat_name):
            os.unlink(cat_name)
예제 #20
0
    def testHybridVariability(self):
        """
        Test that we can generate a catalog which inherits from multiple variability mixins
        (in this case, TestVariability and VariabilityStars).  This is to make sure that
        the register_method and register_class decorators do not mangle inheritance of
        methods from mixins.
        """
        cat_name = os.path.join(self.scratch_dir, 'hybridTestCatalog.dat')
        makeHybridTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('hybridTest', database=self.variability_db)
        myCatalog = StellarVariabilityCatalogWithTest(myDB, obs_metadata=self.obs_metadata)

        myCatalog.write_catalog(cat_name, chunk_size=1000)
        self.verify_catalogs(cat_name)

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

        # make sure order of mixin inheritance does not matter
        myCatalog = OtherVariabilityCatalogWithTest(myDB, obs_metadata=self.obs_metadata)
        myCatalog.write_catalog(cat_name, chunk_size=1000)
        self.verify_catalogs(cat_name)

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

        # make sure that, if a catalog does not contain a variability method,
        # an error is thrown; verify that it contains the correct error message
        myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata)

        with self.assertRaises(RuntimeError) as context:
            myCatalog.write_catalog(cat_name)

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

        expectedMessage = "Your InstanceCatalog does not contain a variability method"
        expectedMessage += " corresponding to 'testVar'"
        self.assertEqual(context.exception.args[0], expectedMessage)
예제 #21
0
    def testAgn(self):
        """
        Just verify that the catalog generation code runs in this case
        """

        cat_name = os.path.join(self.scratch_dir, 'agnTestCatalog.dat')
        makeAgnTable(database=self.variability_db)
        myDB = CatalogDBObject.from_objid('agnTest', database=self.variability_db)
        obs = ObservationMetaData(pointingRA=self.obs_metadata.pointingRA,
                                  pointingDec=self.obs_metadata.pointingDec,
                                  boundType=self.obs_metadata.boundType,
                                  boundLength=self.obs_metadata.boundLength,
                                  mjd=60000.0)
        myCatalog = GalaxyVariabilityCatalog(myDB, obs_metadata=obs)
        myCatalog.write_catalog(cat_name, chunk_size=1000)

        with open(cat_name,'r') as input_file:
            lines = input_file.readlines()
            self.assertGreater(len(lines), 10)

        if os.path.exists(cat_name):
            os.unlink(cat_name)
    def testCannotBeNull_pre_screen(self):
        """
            Check that writing a catalog with self._pre_screen = True produces
            the same results as writing one with self._pre_screen = False.
            """

        # each of these classes flags a different column with a different datatype as cannot_be_null
        availableCatalogs = [
            floatCannotBeNullCatalog, strCannotBeNullCatalog,
            unicodeCannotBeNullCatalog, severalCannotBeNullCatalog
        ]
        dbobj = CatalogDBObject.from_objid('cannotBeNull')

        for catClass in availableCatalogs:
            cat = catClass(dbobj)
            cat._pre_screen = True
            control_cat = catClass(dbobj)
            fileName = os.path.join(self.scratch_dir,
                                    'cannotBeNullTestFile_prescreen.txt')
            control_fileName = os.path.join(
                self.scratch_dir, 'cannotBeNullTestFile_prescreen_control.txt')
            cat.write_catalog(fileName)
            control_cat.write_catalog(control_fileName)

            with open(fileName, 'r') as test_file:
                test_lines = test_file.readlines()
                with open(control_fileName, 'r') as control_file:
                    control_lines = control_file.readlines()
                    for line in control_lines:
                        self.assertIn(line, test_lines)
                    for line in test_lines:
                        self.assertIn(line, control_lines)

            if os.path.exists(fileName):
                os.unlink(fileName)
            if os.path.exists(control_fileName):
                os.unlink(control_fileName)
예제 #23
0
        PhoSimCatalogPoint, PhoSimCatalogSersic2D, PhoSimCatalogZPoint, \
        DefaultPhoSimHeaderMap

from lsst.sims.catUtils.baseCatalogModels import *

starObjNames = ['msstars', 'bhbstars', 'wdstars', 'rrlystars', 'cepheidstars']

obsMD = OpSim3_61DBObject()
obs_list = obsMD.getObservationMetaData((23.0, -50.0), 2.0,
                                        fovRadius=0.1, makeCircBounds = True)

obs_metadata = obs_list[0]

doHeader= True
for starName in starObjNames:
    stars = CatalogDBObject.from_objid(starName)
    star_phoSim=PhoSimCatalogPoint(stars,obs_metadata=obs_metadata) #the class for phoSim input files

                                                                #containing point sources
    star_phoSim.phoSimHeaderMap = DefaultPhoSimHeaderMap
    if (doHeader):
        with open("phoSim_example.txt","w") as fh:
            star_phoSim.write_header(fh)
        doHeader = False

    #below, write_header=False prevents the code from overwriting the header just written
    #write_mode = 'a' allows the code to append the new objects to the output file, rather
    #than overwriting the file for each different class of object.
    star_phoSim.write_catalog("phoSim_example.txt",write_mode='a',write_header=False,chunk_size=20000)

gals = CatalogDBObject.from_objid('galaxyBulge')
from lsst.sims.catUtils.baseCatalogModels import GalaxyBulgeObj, OpSim3_61DBObject
from lsst.sims.GalSimInterface import GalSimGalaxies, SNRdocumentPSF

# if you want to use the actual LSST camera
# from lsst.obs.lsstSim import LsstSimMapper


class testGalSimGalaxies(GalSimGalaxies):
    # only draw images for u and g bands (for speed)
    bandpassNames = ["u", "g"]

    PSF = SNRdocumentPSF()

    # If you want to use the LSST camera, uncomment the line below.
    # You can similarly assign any camera object you want here
    # camera = LsstSimMapper().camera


# select an OpSim pointing
obsMD = OpSim3_61DBObject()
obs_metadata = obsMD.getObservationMetaData(88625744, 0.05, makeCircBounds=True)

# grab a database of galaxies (in this case, galaxy bulges)
gals = CatalogDBObject.from_objid("galaxyBulge")

# now append a bunch of objects with 2D sersic profiles to our output file
galaxy_galSim = testGalSimGalaxies(gals, obs_metadata=obs_metadata)

galaxy_galSim.write_catalog("galSim_bulge_example.txt", chunk_size=10000)
galaxy_galSim.write_images(nameRoot="bulge")
예제 #25
0
 def _final_pass(self, *args, **kwargs):
     return CatalogDBObject._final_pass(self,*args, **kwargs)
예제 #26
0
 def query_columns(self, *args, **kwargs):
     return CatalogDBObject.query_columns(self, *args, **kwargs)
    #defined in galSimInterface/galSimUtilities.py
    noise_and_background = ExampleCCDNoise(99)

#select an OpSim pointing
obsMD = OpSim3_61DBObject()
raw_obs_metadata = obsMD.getObservationMetaData(88625744, 0.05, makeCircBounds = True)

defaults = LSSTdefaults()
obs_metadata = ObservationMetaData(pointingRA=raw_obs_metadata.pointingRA,
                                   pointingDec=raw_obs_metadata.pointingDec,
                                   boundType='circle',
                                   boundLength=0.05,
                                   mjd=raw_obs_metadata.mjd,
                                   rotSkyPos=raw_obs_metadata.rotSkyPos,
                                   bandpassName=['u','g'],
                                   m5=[defaults.m5('u'), defaults.m5('g')],
                                   seeing=[defaults.seeing('u'), defaults.seeing('g')])

#grab a database of galaxies (in this case, galaxy bulges)
gals = CatalogDBObject.from_objid('galaxyBulge')

#now append a bunch of objects with 2D sersic profiles to our output file
gal_noiseless = testGalSimGalaxiesNoiseless(gals, obs_metadata=obs_metadata)

gal_noiseless.write_catalog('galSim_NoiselessGalaxies_example.txt', chunk_size=10000)
gal_noiseless.write_images(nameRoot='noiselessGalaxies')

gal_noisy = testGalSimGalaxiesNoisy(gals, obs_metadata=obs_metadata)
gal_noisy.write_catalog('galSim_NoisyGalaxies_example.txt', chunk_size=10000)
gal_noisy.write_images(nameRoot='noisyGalaxies')
예제 #28
0
"""
This is a version of tutorial00.ipynb without the
running commentary
"""

from lsst.sims.catalogs.db import CatalogDBObject
from lsst.sims.catUtils.baseCatalogModels import *

myGalaxyDB = CatalogDBObject.from_objid('galaxyTiled')
myStarDB = CatalogDBObject.from_objid('allstars')

from lsst.sims.utils import ObservationMetaData

obs_metadata = ObservationMetaData(pointingRA=220.0,
                                   pointingDec=19.0,
                                   boundType='circle',
                                   boundLength=0.2,
                                   mjd=52000.0)


from lsst.sims.catUtils.exampleCatalogDefinitions import RefCatalogGalaxyBase, \
                                                  RefCatalogStarBase

myStarCat = RefCatalogStarBase(myStarDB, obs_metadata=obs_metadata)
myStarCat.write_catalog('star_example.txt')

myGalaxyCat = RefCatalogGalaxyBase(myGalaxyDB, obs_metadata=obs_metadata)
myGalaxyCat.write_catalog('galaxy_example.txt')

squareObsMetadata = ObservationMetaData(pointingRA=220.0,
                                        pointingDec=19.0,
예제 #29
0
 def _get_column_query(self, *args, **kwargs):
     return CatalogDBObject._get_column_query(self,*args, **kwargs)
예제 #30
0

class testGalSimStars(GalSimStars):
    #only draw images for u and g bands (for speed)
    bandpassNames = ['u', 'g']

    #defined in galSimInterface/galSimUtilities.py
    PSF = SNRdocumentPSF()

    #If you want to use the LSST camera, uncomment the line below.
    #You can similarly assign any camera object you want here
    #camera = LsstSimMapper().camera


#select an OpSim pointing
#select an OpSim pointing
opsimdb = os.path.join(getPackageDir('sims_data'), 'OpSimData',
                       'opsimblitz1_1133_sqlite.db')
obs_gen = ObservationMetaDataGenerator(database=opsimdb, driver='sqlite')
obs_list = obs_gen.getObservationMetaData(obsHistID=10, boundLength=0.05)
obs_metadata = obs_list[0]

#grab a database of stars
stars = CatalogDBObject.from_objid('allstars')

#now append a bunch of objects with 2D sersic profiles to our output file
stars_galSim = testGalSimStars(stars, obs_metadata=obs_metadata)

stars_galSim.write_catalog('galSim_star_example.txt', chunk_size=100)
stars_galSim.write_images(nameRoot='star')
    def testCannotBeNull(self):
        """
            Test to make sure that the code for filtering out rows with null values
            in key rows works.
            """

        # each of these classes flags a different column with a different datatype as cannot_be_null
        availableCatalogs = [
            floatCannotBeNullCatalog, strCannotBeNullCatalog,
            unicodeCannotBeNullCatalog, severalCannotBeNullCatalog
        ]
        dbobj = CatalogDBObject.from_objid('cannotBeNull')

        ct_n2 = 0  # number of rows in floatCannotBeNullCatalog
        ct_n4 = 0  # number of rows in strCannotBeNullCatalog
        ct_n2_n4 = 0  # number of rows in severalCannotBeNullCatalog

        for catClass in availableCatalogs:
            cat = catClass(dbobj)
            fileName = os.path.join(self.scratch_dir,
                                    'cannotBeNullTestFile.txt')
            cat.write_catalog(fileName)
            dtype = np.dtype([('id', int), ('n1', np.float64),
                              ('n2', np.float64), ('n3', np.float64),
                              ('n4', (str_type, 40)), ('n5', (str_type, 40))])
            testData = np.genfromtxt(fileName, dtype=dtype, delimiter=',')

            ct_good = 0  # a counter to keep track of the rows read in from the catalog
            ct_total = len(self.baselineOutput)

            for i in range(len(self.baselineOutput)):

                # self.baselineOutput contains all of the rows from the dbobj
                # first, we must assess whether or not the row we are currently
                # testing would, in fact, pass the cannot_be_null test
                validLine = True
                for col_name in cat.cannot_be_null:
                    if (isinstance(self.baselineOutput[col_name][i], str)
                            or isinstance(self.baselineOutput[col_name][i],
                                          str_type)):

                        if self.baselineOutput[col_name][i].strip().lower(
                        ) == 'none':
                            validLine = False
                    else:
                        if np.isnan(self.baselineOutput[col_name][i]):
                            validLine = False

                if validLine:
                    if catClass is floatCannotBeNullCatalog:
                        ct_n2 += 1
                    elif catClass is strCannotBeNullCatalog:
                        ct_n4 += 1
                    elif catClass is severalCannotBeNullCatalog:
                        ct_n2_n4 += 1

                    # if the row in self.baslineOutput should be in the catalog, we now check
                    # that baseline and testData agree on column values (there are some gymnastics
                    # here because you cannot do an == on NaN's
                    for (k, xx) in enumerate(self.baselineOutput[i]):
                        if k < 4:
                            if not np.isnan(xx):
                                msg = ('k: %d -- %s %s -- %s' %
                                       (k, str(xx), str(testData[ct_good][k]),
                                        cat.cannot_be_null))
                                self.assertAlmostEqual(xx,
                                                       testData[ct_good][k],
                                                       3,
                                                       msg=msg)
                            else:
                                np.testing.assert_equal(
                                    testData[ct_good][k], np.NaN)
                        else:
                            msg = ('%s (%s) is not %s (%s)' %
                                   (xx, type(xx), testData[ct_good][k],
                                    type(testData[ct_good][k])))
                            self.assertEqual(xx.strip(),
                                             testData[ct_good][k].strip(),
                                             msg=msg)
                    ct_good += 1

            self.assertEqual(ct_good, len(
                testData))  # make sure that we tested all of the testData rows
            msg = '%d >= %d' % (ct_good, ct_total)
            self.assertLess(ct_good, ct_total,
                            msg=msg)  # make sure that some rows did not make
            # it into the catalog

        # make sure that severalCannotBeNullCatalog weeded out rows that were individually in
        # floatCannotBeNullCatalog or strCannotBeNullCatalog
        self.assertGreater(ct_n2, ct_n2_n4)
        self.assertGreater(ct_n4, ct_n2_n4)

        if os.path.exists(fileName):
            os.unlink(fileName)
예제 #32
0
        # Actually calculate the magnitudes
        return self._magnitudeGetter(self.sdssBandpassDict,
                                     self.get_sdss_magnitudes._colnames)


if __name__ == "__main__":

    obs_metadata_pointed = ObservationMetaData(mjd=2013.23,
                                               boundType='circle',
                                               pointingRA=200.0,
                                               pointingDec=-30.0,
                                               boundLength=1.0)

    obs_metadata_pointed.metadata = {}
    obs_metadata_pointed.metadata['Opsim_filter'] = 'i'
    dbObj = CatalogDBObject.from_objid('rrlystars')
    sdssStars = sdssStars(dbObj, obs_metadata=obs_metadata_pointed)
    sdssStars.write_catalog("example_sdss_stars.txt")

    obs_metadata_pointed = ObservationMetaData(mjd=50000.0,
                                               boundType='circle',
                                               pointingRA=0.0,
                                               pointingDec=0.0,
                                               boundLength=0.01)

    obs_metadata_pointed.metadata = {}
    obs_metadata_pointed.metadata['Opsim_filter'] = 'i'
    dbObj = CatalogDBObject.from_objid('galaxyBase')
    sdssGalaxies = sdssGalaxies(dbObj, obs_metadata=obs_metadata_pointed)
    sdssGalaxies.write_catalog("example_sdss_galaxies.txt")
예제 #33
0
    # Set the min to 15 since we saturate there. CatSim max is 28
    bins = np.arange(15., 28.2, .2)
    starDensity = np.zeros((hp.nside2npix(nside), np.size(bins) - 1),
                           dtype=float)
    overMaxMask = np.zeros(hp.nside2npix(nside), dtype=bool)
    lat, ra = hp.pix2ang(nside, np.arange(0, hp.nside2npix(nside)))
    dec = np.pi / 2. - lat

    # Square root of pixel area.
    hpsizeDeg = hp.nside2resol(nside, arcmin=True) / 60.

    # Limit things to a 10 arcmin radius
    hpsizeDeg = np.min([10. / 60., hpsizeDeg])

    # Options include galaxyBase, cepheidstars, wdstars, rrlystars, msstars, bhbstars, allstars, and more...
    dbobj = CatalogDBObject.from_objid(args.stars)

    indxMin = 0

    restoreFile = glob.glob('starDensity_%s_%s_nside_%i.npz' %
                            (filterName, starNames, nside))
    if len(restoreFile) > 0:
        data = np.load(restoreFile[0])
        starDensity = data['starDensity'].copy()
        indxMin = data['icheck'].copy()
        overMaxMask = data['overMaxMask'].copy()

    print('')
    # Look at a cirular area the same area as the healpix it's centered on.
    boundLength = hpsizeDeg / np.pi**0.5
예제 #34
0
from lsst.sims.catalogs.db import CatalogDBObject
from lsst.sims.utils import ObservationMetaData
#The following is to get the object ids in the registry
import lsst.sims.catUtils.baseCatalogModels as bcm
from lsst.sims.catUtils.exampleCatalogDefinitions import RefCatalogGalaxyBase

if __name__ == '__main__':

    obs_metadata = ObservationMetaData(boundType='circle', pointingRA=0.0, pointingDec=0.0,
                                       boundLength=0.01, mjd=57388.0)
    dbobj = CatalogDBObject.from_objid('galaxyBase')
    t = dbobj.getCatalog('galaxy_photometry_cat', obs_metadata=obs_metadata)
    filename = 'galaxy_photometry_test.dat'
    t.write_catalog(filename, chunk_size=10000)

예제 #35
0
"""
This is a version of tutorial00.ipynb without the
running commentary
"""

from lsst.sims.catalogs.db import CatalogDBObject
from lsst.sims.catUtils.baseCatalogModels import *

myGalaxyDB = CatalogDBObject.from_objid('galaxyTiled')
myStarDB = CatalogDBObject.from_objid('allstars')

from lsst.sims.utils import ObservationMetaData

obs_metadata = ObservationMetaData(pointingRA = 220.0,
                                   pointingDec = 19.0,
                                   boundType = 'circle',
                                   boundLength = 0.2,
                                   mjd = 52000.0)


from lsst.sims.catUtils.exampleCatalogDefinitions import RefCatalogGalaxyBase, \
                                                  RefCatalogStarBase

myStarCat = RefCatalogStarBase(myStarDB, obs_metadata=obs_metadata)
myStarCat.write_catalog('star_example.txt')

myGalaxyCat = RefCatalogGalaxyBase(myGalaxyDB, obs_metadata=obs_metadata)
myGalaxyCat.write_catalog('galaxy_example.txt')


squareObsMetadata = ObservationMetaData(pointingRA = 220.0,
예제 #36
0
from lsst.sims.catUtils.baseCatalogModels import *

starObjNames = ['msstars', 'bhbstars', 'wdstars', 'rrlystars', 'cepheidstars']

obsMD = OpSim3_61DBObject()
obs_list = obsMD.getObservationMetaData((23.0, -50.0),
                                        2.0,
                                        fovRadius=0.1,
                                        makeCircBounds=True)

obs_metadata = obs_list[0]

doHeader = True
for starName in starObjNames:
    stars = CatalogDBObject.from_objid(starName)
    star_phoSim = PhoSimCatalogPoint(
        stars, obs_metadata=obs_metadata)  #the class for phoSim input files

    #containing point sources
    star_phoSim.phoSimHeaderMap = DefaultPhoSimHeaderMap
    if (doHeader):
        with open("phoSim_example.txt", "w") as fh:
            star_phoSim.write_header(fh)
        doHeader = False

    #below, write_header=False prevents the code from overwriting the header just written
    #write_mode = 'a' allows the code to append the new objects to the output file, rather
    #than overwriting the file for each different class of object.
    star_phoSim.write_catalog("phoSim_example.txt",
                              write_mode='a',
예제 #37
0
    #
    #Note that 'raPlusOneRadian' is not converted and will thus be written
    #in radians.
    transformations = {'raJ2000':numpy.degrees, 'decJ2000':numpy.degrees,
                       'sum':numpy.degrees, 'difference':numpy.degrees,
                       'raInArcSec':radiansToArcSec}

    #This is the key value that needs to be passed to CatalogDBObject.getCatalog()
    #in order to instantiate a TutorialCatalog
    catalog_type = 'tutorial_catalog'





myDB = CatalogDBObject.from_objid('allstars')
obs_metadata = ObservationMetaData(pointingRA=220.0, pointingDec=19.0,
                                   boundType='circle', boundLength=0.1,
                                   mjd=52000.0)

#First just write a catalog the way we are used to
cat = TutorialCatalog(myDB, obs_metadata=obs_metadata)
cat.write_catalog('tutorial_mixin_catalog.txt')


#Now use CatalogDBObject.getCatalog() to write a catalog (using a different
#ObservationMetaData)
obs_metadata = ObservationMetaData(pointingRA=120.0, pointingDec=-5.0,
                                   boundType='circle', boundLength=0.1,
                                   mjd=52000.0)
예제 #38
0
from astropy.coordinates import SkyCoord
from astropy.coordinates import ICRS, Galactic
from lsst.sims.catalogs.db import CatalogDBObject
from lsst.sims.catUtils.baseCatalogModels import *
from lsst.sims.catUtils.exampleCatalogDefinitions import *
from lsst.sims.utils import ObservationMetaData

# Coordinates at the galactic pole
c = SkyCoord(Galactic, l=0.*u.degree, b=-90*u.degree)

# Radius to query, in degrees
boundLength = 3.5/2.

colnames = ['raJ2000', 'decJ2000', 'umag', 'gmag', 'rmag', 'imag', 'zmag', 'ymag']
constraint = 'rmag < 16'

# dbobj = CatalogDBObject.from_objid('allstars')
dbobj = CatalogDBObject.from_objid('brightstars')

obs_metadata = ObservationMetaData(boundType='circle',
                                   pointingRA=c.icrs.ra.deg,
                                   pointingDec=c.icrs.dec.deg,
                                   boundLength=boundLength, mjd=5700)

t = dbobj.getCatalog('ref_catalog_star', obs_metadata=obs_metadata)

stars = t.db_obj.query_columns(colnames=colnames, obs_metadata=obs_metadata,
                               constraint=constraint, limit=1e6, chunk_size=None)
stars = [chunk for chunk in stars][0]

예제 #39
0
            e1s.append(e1)
            e2s.append(e2)
            r_hls.append(r_hls)
    fh.close()
    import pdb;pdb.set_trace()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--skipCounts", help="Skip running the galaxy count validation", 
                        action="store_true")
    parser.add_argument("--skipNofz", help="Skip running the redshift distribution validation", 
                        action="store_true")
    parser.add_argument("--skipEllip", help="Skip running the ellipticity distribution validation", 
                        action="store_true")
    parser.add_argument("--savePlots", help="Save plots as PNG files instead of displaying them?",
                        action="store_true")
    parser.add_argument("--baseOutputName", help="Base filename for output plots",
                        default='validation')
    parser.add_argument("--boxsize", help="size of the side of the box to get in degrees",
                        default=8., type=float)
    args = parser.parse_args()
    obs_metadata = ObservationMetaData(circ_bounds=dict(ra=0., dec=0., radius=args.boxsize/2.))
    dbobj = CatalogDBObject.from_objid('galaxyBase')
    filename = None
    if not args.skipCounts:
        galaxyCountValidation(dbobj, args.savePlots, args.baseOutputName)
    if not args.skipNofz:
        redshiftDistributionValidation(dbobj, obs_metadata, args.savePlots, args.baseOutputName)
    if not args.skipEllip:
        ellipticityDistributionValidation(dbobj, obs_metadata, args.savePlots, args.baseOutputName)
예제 #40
0
        def testCannotBeNull(self):
            """
            Test to make sure that the code for filtering out rows with null values
            in key rows works.
            """

            # each of these classes flags a different column with a different datatype as cannot_be_null
            availableCatalogs = [floatCannotBeNullCatalog, strCannotBeNullCatalog, unicodeCannotBeNullCatalog,
                                 severalCannotBeNullCatalog]
            dbobj = CatalogDBObject.from_objid('cannotBeNull')

            ct_n2 = 0  # number of rows in floatCannotBeNullCatalog
            ct_n4 = 0  # number of rows in strCannotBeNullCatalog
            ct_n2_n4 = 0  # number of rows in severalCannotBeNullCatalog

            for catClass in availableCatalogs:
                cat = catClass(dbobj)
                fileName = os.path.join(self.scratch_dir, 'cannotBeNullTestFile.txt')
                cat.write_catalog(fileName)
                dtype = np.dtype([('id', int), ('n1', np.float64), ('n2', np.float64), ('n3', np.float64),
                                  ('n4', (str_type, 40)), ('n5', (str_type, 40))])
                testData = np.genfromtxt(fileName, dtype=dtype, delimiter=',')

                ct_good = 0  # a counter to keep track of the rows read in from the catalog
                ct_total = len(self.baselineOutput)

                for i in range(len(self.baselineOutput)):

                    # self.baselineOutput contains all of the rows from the dbobj
                    # first, we must assess whether or not the row we are currently
                    # testing would, in fact, pass the cannot_be_null test
                    validLine = True
                    for col_name in cat.cannot_be_null:
                        if (isinstance(self.baselineOutput[col_name][i], str) or
                            isinstance(self.baselineOutput[col_name][i], str_type)):

                            if self.baselineOutput[col_name][i].strip().lower() == 'none':
                                validLine = False
                        else:
                            if np.isnan(self.baselineOutput[col_name][i]):
                                validLine = False

                    if validLine:
                        if catClass is floatCannotBeNullCatalog:
                            ct_n2 += 1
                        elif catClass is strCannotBeNullCatalog:
                            ct_n4 += 1
                        elif catClass is severalCannotBeNullCatalog:
                            ct_n2_n4 += 1

                        # if the row in self.baslineOutput should be in the catalog, we now check
                        # that baseline and testData agree on column values (there are some gymnastics
                        # here because you cannot do an == on NaN's
                        for (k, xx) in enumerate(self.baselineOutput[i]):
                            if k < 4:
                                if not np.isnan(xx):
                                    msg = ('k: %d -- %s %s -- %s' %
                                           (k, str(xx), str(testData[ct_good][k]), cat.cannot_be_null))
                                    self.assertAlmostEqual(xx, testData[ct_good][k], 3, msg=msg)
                                else:
                                    np.testing.assert_equal(testData[ct_good][k], np.NaN)
                            else:
                                msg = ('%s (%s) is not %s (%s)' %
                                       (xx, type(xx), testData[ct_good][k], type(testData[ct_good][k])))
                                self.assertEqual(xx.strip(), testData[ct_good][k].strip(), msg=msg)
                        ct_good += 1

                self.assertEqual(ct_good, len(testData))  # make sure that we tested all of the testData rows
                msg = '%d >= %d' % (ct_good, ct_total)
                self.assertLess(ct_good, ct_total, msg=msg)  # make sure that some rows did not make
                                                             # it into the catalog

            # make sure that severalCannotBeNullCatalog weeded out rows that were individually in
            # floatCannotBeNullCatalog or strCannotBeNullCatalog
            self.assertGreater(ct_n2, ct_n2_n4)
            self.assertGreater(ct_n4, ct_n2_n4)

            if os.path.exists(fileName):
                os.unlink(fileName)
from lsst.sims.GalSimInterface import GalSimGalaxies, SNRdocumentPSF
from lsst.sims.GalSimInterface import LSSTCameraWrapper

#if you want to use the actual LSST camera
#from lsst.obs.lsstSim import LsstSimMapper


class testGalSimGalaxies(GalSimGalaxies):
    #only draw images for u and g bands (for speed)
    bandpassNames = ['u', 'g']

    PSF = SNRdocumentPSF()


#select an OpSim pointing
opsimdb = os.path.join(getPackageDir('sims_data'), 'OpSimData',
                       'opsimblitz1_1133_sqlite.db')
obs_gen = ObservationMetaDataGenerator(database=opsimdb, driver='sqlite')
obs_list = obs_gen.getObservationMetaData(obsHistID=10, boundLength=0.05)
obs_metadata = obs_list[0]

#grab a database of galaxies (in this case, galaxy bulges)
gals = CatalogDBObject.from_objid('galaxyBulge')

#now append a bunch of objects with 2D sersic profiles to our output file
galaxy_galSim = testGalSimGalaxies(gals, obs_metadata=obs_metadata)
galaxy_galSim.camera_wrapper = LSSTCameraWrapper()

galaxy_galSim.write_catalog('galSim_bulge_example.txt', chunk_size=10000)
galaxy_galSim.write_images(nameRoot='bulge')
 def query_columns(self, *args, **kwargs):
     return CatalogDBObject.query_columns(self, *args, **kwargs)
예제 #43
0
    # Set the min to 15 since we saturate there. CatSim max is 28
    bins = np.arange(15., 28.2, .2)
    starDensity = np.zeros((hp.nside2npix(nside), np.size(bins)-1), dtype=float)
    overMaxMask = np.zeros(hp.nside2npix(nside), dtype=bool)
    lat, ra = hp.pix2ang(nside, np.arange(0, hp.nside2npix(nside)))
    dec = np.pi/2.-lat

    # Square root of pixel area.
    hpsizeDeg = hp.nside2resol(nside, arcmin=True)/60.

    # Limit things to a 10 arcmin radius
    hpsizeDeg = np.min([10./60., hpsizeDeg])

    # Options include galaxyBase, cepheidstars, wdstars, rrlystars, msstars, bhbstars, allstars, and more...
    dbobj = CatalogDBObject.from_objid(args.stars)

    indxMin = 0

    restoreFile = glob.glob('starDensity_%s_%s_nside_%i.npz' % (filterName, starNames, nside))
    if len(restoreFile) > 0:
        data = np.load(restoreFile[0])
        starDensity = data['starDensity'].copy()
        indxMin = data['icheck'].copy()
        overMaxMask = data['overMaxMask'].copy()

    print('')
    # Look at a cirular area the same area as the healpix it's centered on.
    boundLength = hpsizeDeg/np.pi**0.5

    blockArea = hpsizeDeg**2  # sq deg
예제 #44
0
            bandpassDir = os.path.join(getPackageDir('throughputs'),'sdss')
            bandpassRoot = 'sdss_'

            self.sdssBandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames,
                                                                 bandpassRoot = bandpassRoot,
                                                                 bandpassDir = bandpassDir)

        # Actually calculate the magnitudes
        return self._magnitudeGetter(self.sdssBandpassDict, self.get_sdss_magnitudes._colnames)


if __name__ == "__main__":

    obs_metadata_pointed = ObservationMetaData(mjd=2013.23, boundType='circle',
                                               pointingRA=200.0, pointingDec=-30.0, boundLength=1.0)

    obs_metadata_pointed.metadata = {}
    obs_metadata_pointed.metadata['Opsim_filter'] = 'i'
    dbObj = CatalogDBObject.from_objid('rrlystars')
    sdssStars = sdssStars(dbObj, obs_metadata = obs_metadata_pointed)
    sdssStars.write_catalog("example_sdss_stars.txt")

    obs_metadata_pointed = ObservationMetaData(mjd=50000.0, boundType='circle',
                             pointingRA=0.0, pointingDec=0.0, boundLength=0.01)

    obs_metadata_pointed.metadata = {}
    obs_metadata_pointed.metadata['Opsim_filter'] = 'i'
    dbObj = CatalogDBObject.from_objid('galaxyBase')
    sdssGalaxies = sdssGalaxies(dbObj, obs_metadata = obs_metadata_pointed)
    sdssGalaxies.write_catalog("example_sdss_galaxies.txt")
    PSF = SNRdocumentPSF()

    #If you want to use the LSST camera, uncomment the line below.
    #You can similarly assign any camera object you want here
    #camera = LsstSimMapper().camera



#select an OpSim pointing
opsimdb = os.path.join(getPackageDir('sims_data'), 'OpSimData', 'opsimblitz1_1133_sqlite.db')
obs_gen = ObservationMetaDataGenerator(database=opsimdb)
obs_list = obs_gen.getObservationMetaData(obsHistID=10, boundLength=0.05)
obs_metadata = obs_list[0]

#grab a database of galaxies (in this case, galaxy bulges)
stars = CatalogDBObject.from_objid('allstars')

#now append a bunch of objects with 2D sersic profiles to our output file
stars_galSim = testGalSimStars(stars, obs_metadata=obs_metadata)

catName = 'galSim_compound_example.txt'
stars_galSim.write_catalog(catName, chunk_size=100)

print('done with stars')

bulges = CatalogDBObject.from_objid('galaxyBulge')
bulge_galSim = testGalSimGalaxies(bulges, obs_metadata=obs_metadata)

#This will make sure that the galaxies are drawn to the same images
#as the stars were.  It copies the GalSimInterpreter from the star
#catalog.  It also copies the camera.  The PSF in the GalSimInterpreter
    #defined in galSimInterface/galSimUtilities.py
    PSF = SNRdocumentPSF()

    #If you want to use the LSST camera, uncomment the line below.
    #You can similarly assign any camera object you want here
    #camera = LsstSimMapper().camera



#select an OpSim pointing
obsMD = OpSim3_61DBObject()
obs_metadata = obsMD.getObservationMetaData(88625744, 0.05, makeCircBounds = True)

#grab a database of galaxies (in this case, galaxy bulges)
stars = CatalogDBObject.from_objid('allstars')

#now append a bunch of objects with 2D sersic profiles to our output file
stars_galSim = testGalSimStars(stars, obs_metadata=obs_metadata)

catName = 'galSim_compound_example.txt'
stars_galSim.write_catalog(catName, chunk_size=100)

print 'done with stars'

bulges = CatalogDBObject.from_objid('galaxyBulge')
bulge_galSim = testGalSimGalaxies(bulges, obs_metadata=obs_metadata)

#This will make sure that the galaxies are drawn to the same images
#as the stars were.  It copies the GalSimInterpreter from the star
#catalog.  It also copies the camera.  The PSF in the GalSimInterpreter