Esempio n. 1
0
def phosim_obs_metadata(phosim_commands):
    """
    Factory function to create an ObservationMetaData object based
    on the PhoSim commands extracted from an instance catalog.

    Parameters
    ----------
    phosim_commands : dict
        Dictionary of PhoSim physics commands.

    Returns
    -------
    lsst.sims.utils.ObservationMetaData
    """
    bandpass = phosim_commands['bandpass']
    fwhm_eff = FWHMeff(phosim_commands['seeing'], bandpass,
                       phosim_commands['altitude'])
    fwhm_geom = FWHMgeom(phosim_commands['seeing'], bandpass,
                         phosim_commands['altitude'])
    obs_md = ObservationMetaData(pointingRA=phosim_commands['rightascension'],
                                 pointingDec=phosim_commands['declination'],
                                 mjd=phosim_commands['mjd'],
                                 rotSkyPos=phosim_commands['rotskypos'],
                                 bandpassName=bandpass,
                                 m5=LSSTdefaults().m5(bandpass),
                                 seeing=fwhm_eff)
    # Set the OpsimMetaData attribute with the obshistID info.
    obs_md.OpsimMetaData = {'obshistID': phosim_commands['obshistid']}
    obs_md.OpsimMetaData['FWHMgeom'] = fwhm_geom
    obs_md.OpsimMetaData['FWHMeff'] = fwhm_eff
    obs_md.OpsimMetaData['rawSeeing'] = phosim_commands['seeing']
    obs_md.OpsimMetaData['altitude'] = phosim_commands['altitude']
    obs_md.OpsimMetaData['airmass'] = airmass(phosim_commands['altitude'])
    obs_md.OpsimMetaData['seed'] = phosim_commands['seed']
    return obs_md
Esempio n. 2
0
File: imSim.py Progetto: belaa/imSim
def phosim_obs_metadata(phosim_commands):
    """
    Factory function to create an ObservationMetaData object based
    on the PhoSim commands extracted from an instance catalog.

    Parameters
    ----------
    phosim_commands : dict
        Dictionary of PhoSim physics commands.

    Returns
    -------
    lsst.sims.utils.ObservationMetaData

    Notes
    -----
    The seeing from the instance catalog is the value at 500nm at
    zenith.  Do we need to do a band-specific calculation?
    """
    bandpass = phosim_commands['bandpass']
    obs_md = ObservationMetaData(pointingRA=phosim_commands['rightascension'],
                                 pointingDec=phosim_commands['declination'],
                                 mjd=phosim_commands['mjd'],
                                 rotSkyPos=phosim_commands['rotskypos'],
                                 bandpassName=bandpass,
                                 m5=LSSTdefaults().m5(bandpass),
                                 seeing=phosim_commands['FWHMeff'])
    # Set the OpsimMetaData attribute with the obshistID info.
    obs_md.OpsimMetaData = {'obshistID': phosim_commands['obshistid']}
    obs_md.OpsimMetaData['FWHMgeom'] = phosim_commands['FWHMgeom']
    obs_md.OpsimMetaData['FWHMeff'] = phosim_commands['FWHMeff']
    obs_md.OpsimMetaData['rawSeeing'] = phosim_commands['rawSeeing']
    obs_md.OpsimMetaData['altitude'] = phosim_commands['altitude']
    return obs_md
Esempio n. 3
0
def createTestSNDB():
    """
    Create a CatalogDBObject-like database that contains everything needed to create
    a supernova catalog.  Return the CatalogDBObject and an ObservationMetaData pointing
    to its center.

    Note: the OpsimMetaData for the returned ObservationMetaData will be inconsistent.
    It is just there so that PhoSim InstanceCatalog classes can write out a header.
    """

    raCenter = 23.0
    decCenter = -19.0
    radius = 0.1
    obs = ObservationMetaData(pointingRA=raCenter, pointingDec=decCenter, boundType='circle',
                              boundLength=radius, rotSkyPos=33.0, mjd=59580.0,
                              bandpassName='r')

    # these will be totally inconsistent; just need something to put in the header
    obs.OpsimMetaData = {'dist2Moon': 0.1, 'moonalt': -0.2,
                         'moonra': 1.1, 'moondec': 0.5,
                         'rottelpos': 0.4, 'sunalt': -1.1}

    rng = np.random.RandomState(88)
    n_obj = 10
    rr = rng.random_sample(n_obj)*radius
    theta = rng.random_sample(n_obj)*2.0*np.pi
    ra_list = raCenter + rr*np.cos(theta)
    dec_list = decCenter + rr*np.sin(theta)
    t0_list = 0.5*rng.random_sample(n_obj)
    x0_list = rng.random_sample(n_obj)*1.0e-4
    x1_list = rng.random_sample(n_obj)
    c_list = rng.random_sample(n_obj)*2.0
    z_list = rng.random_sample(n_obj)*1.1 + 0.1

    txt_file_name = tempfile.mktemp(dir=ROOT, prefix='test_phosim_sn_source', suffix='.txt')

    dtype = np.dtype([('id', int), ('snra', float), ('sndec', float),
                      ('t0', float), ('x0', float), ('x1', float),
                      ('c', float), ('redshift', float), ('galtileid', int)])

    with open(txt_file_name, 'w') as output_file:
        for ix, (ra, dec, t0, x0, x1, c, z) in enumerate(zip(ra_list, dec_list, t0_list, x0_list,
                                                             x1_list, c_list, z_list)):
            output_file.write('%d %e %e %e %e %e %e %e %d\n' %
                              (ix, ra, dec, t0, x0, x1, c, z, ix))

    class testSNDBObj(SNDBObj, fileDBObject):
        tableid = 'test'

        def query_columns(self, *args, **kwargs):
            return fileDBObject.query_columns(self, *args, **kwargs)

    dbobj = testSNDBObj(txt_file_name, runtable='test', dtype=dtype)

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

    return dbobj, obs
Esempio n. 4
0
    def test_raw_file_writing(self):
        """
        Test the writing of raw files directly from a galsim.Image.
        This is mostly an operational test that the raw files can be
        written from a galsim image.
        """
        camera_wrapper = sims_gsi.LSSTCameraWrapper()
        phot_params = PhotometricParameters()
        obs_md = ObservationMetaData(pointingRA=31.1133844,
                                     pointingDec=-10.0970060,
                                     rotSkyPos=69.0922930,
                                     mjd=59797.2854090,
                                     bandpassName='r')
        obs_md.OpsimMetaData = {
            'obshistID': 161899,
            'airmass': desc.imsim.airmass(43.6990272)
        }
        detname = "R:2,2 S:1,1"
        chipid = 'R{}_S{}'.format(detname[2:5:2], detname[8:11:2])
        detector = sims_gsi.make_galsim_detector(camera_wrapper, detname,
                                                 phot_params, obs_md)
        gs_interpreter = sims_gsi.GalSimInterpreter(detectors=[detector])
        gs_image = gs_interpreter.blankImage(detector)
        raw_image = desc.imsim.ImageSource.create_from_galsim_image(gs_image)
        self.outfile = 'lsst_a_{}_r.fits'.format(chipid)

        # Add keyword values via an optional dict.
        added_keywords = {'GAUSFWHM': 0.4, 'FOOBAR': 'hello, world'}

        raw_image.write_fits_file(self.outfile,
                                  overwrite=True,
                                  added_keywords=added_keywords)

        # Test some keywords.
        with fits.open(self.outfile) as raw_file:
            hdr = raw_file[0].header
            self.assertAlmostEqual(hdr['RATEL'], obs_md.pointingRA)
            self.assertAlmostEqual(hdr['DECTEL'], obs_md.pointingDec)
            self.assertAlmostEqual(hdr['ROTANGLE'], obs_md.rotSkyPos)
            self.assertEqual(hdr['CHIPID'], chipid)

            # Ensure the following keywords are set.
            self.assertNotEqual(hdr['AMSTART'], hdr['AMEND'])
            self.assertNotEqual(hdr['HASTART'], hdr['HAEND'])
            self.assertEqual(hdr['DATE-OBS'], '2022-08-06T06:50:59.338')
            self.assertEqual(hdr['DATE-END'], '2022-08-06T06:51:29.338')
            self.assertEqual(hdr['TIMESYS'], 'TAI')
            self.assertEqual(hdr['OBSTYPE'], 'SKYEXP')

            # Test the added_keywords.
            for key, value in added_keywords.items():
                self.assertEqual(hdr[key], value)
Esempio n. 5
0
    def testOpsimMetaData(self):
        """
        Make sure that an exception is raised if you pass a non-dict
        object in as OpsimMetaData
        """
        obs = ObservationMetaData(pointingRA=23.0, pointingDec=-11.0)

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = 5.0
        self.assertIn("must be a dict", ee.exception.args[0])

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = 5
        self.assertIn("must be a dict", ee.exception.args[0])

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = [5.0, 3.0]
        self.assertIn("must be a dict", ee.exception.args[0])

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = (5.0, 3.0)
        self.assertIn("must be a dict", ee.exception.args[0])

        obs.OpsimMetaData = {'a': 1, 'b': 2}
Esempio n. 6
0
    def testOpsimMetaData(self):
        """
        Make sure that an exception is raised if you pass a non-dict
        object in as OpsimMetaData
        """
        obs = ObservationMetaData(pointingRA=23.0, pointingDec=-11.0)

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = 5.0
        self.assertIn("must be a dict", ee.exception.args[0])

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = 5
        self.assertIn("must be a dict", ee.exception.args[0])

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = [5.0, 3.0]
        self.assertIn("must be a dict", ee.exception.args[0])

        with self.assertRaises(RuntimeError) as ee:
            obs.OpsimMetaData = (5.0, 3.0)
        self.assertIn("must be a dict", ee.exception.args[0])

        obs.OpsimMetaData = {'a': 1, 'b': 2}
Esempio n. 7
0
def get_obs_md(instcat):
    with open(instcat, 'r') as fd:
        params = dict()
        for line in fd:
            if line.startswith('object'):
                break
            tokens = line.strip().split()
            try:
                params[tokens[0]] = float(tokens[1])
            except ValueError:
                pass
    obs_md = ObservationMetaData(pointingRA=params['rightascension'],
                                 pointingDec=params['declination'],
                                 mjd=params['mjd'],
                                 rotSkyPos=params['rotskypos'])
    obs_md.OpsimMetaData = dict(obshistid=params['obshistid'])
    return obs_md
Esempio n. 8
0
def get_obs_md_from_raw(raw_file):
    """
    Get the ObservationMetaData object from the raw file header info.

    Parameters
    ----------
    raw_file: str
        The path to the raw file.

    Results
    -------
    lsst.sims.utils.ObservationMetaData
    """
    with fits.open(raw_file) as fd:
        hdr = fd[0].header
        obs_md = ObservationMetaData(pointingRA=hdr['RATEL'],
                                     pointingDec=hdr['DECTEL'],
                                     mjd=hdr['MJD-OBS'],
                                     rotSkyPos=hdr['ROTANGLE'])
        obs_md.OpsimMetaData = dict(obshistid=hdr['OBSID'])
    return obs_md
    def ObservationMetaDataFromPointing(self, OpSimPointingRecord, OpSimColumns=None,
                                        boundLength=1.75, boundType='circle'):
        """
        Return instance of ObservationMetaData for an OpSim Pointing record
        from OpSim.

        Parameters
        ----------
        OpSimPointingRecord : Dictionary, mandatory
            Dictionary of values with keys corresponding to certain columns of
            the Summary table in the OpSim database. The minimal list of keys
            required for catsim to work is 'fiveSigmaDepth',
            'filtSkyBrightness', and at least one of ('finSeeing', 'FWHMeff').
            More keys defined in columnMap may be necessary for PhoSim to work.
        OpSimColumns : tuple of strings, optional, defaults to None
            The columns corresponding to the OpSim records. If None, attempts
            to obtain these from the OpSimRecord as OpSimRecord.dtype.names
        boundType : {'circle', 'box'}, optional, defaults to 'circle'
            Shape of the observation
        boundLength : scalar float, optional, defaults to 1.75
            'characteristic size' of observation field, in units of degrees.
            For boundType='circle', this is a radius, for boundType='box', this
            is a size of the box
        """

        pointing = OpSimPointingRecord
        pointing_column_names = pointing.dtype.names
        # Decide what is the name of the column in the OpSim database
        # corresponding to the Seeing. For older OpSim outputs, this is
        # 'finSeeing'. For later OpSim outputs this is 'FWHMeff'
        if OpSimColumns is None:
            OpSimColumns = pointing_column_names

        self._set_seeing_column(OpSimColumns)

        # check to make sure the OpSim pointings being supplied contain
        # the minimum required information
        for required_column in ('fieldRA', 'fieldDec', 'expMJD', 'filter'):
            if required_column not in OpSimColumns:
                raise RuntimeError("ObservationMetaDataGenerator requires that the database of "
                                   "pointings include the coluns:\nfieldRA (in radians)"
                                   "\nfieldDec (in radians)\nexpMJD\nfilter")

        # construct a raw dict of all of the OpSim columns associated with this pointing
        raw_dict = dict([(col, pointing[col]) for col in pointing_column_names])

        obs = ObservationMetaData(pointingRA=np.degrees(pointing['fieldRA']),
                                  pointingDec=np.degrees(pointing['fieldDec']),
                                  mjd=pointing['expMJD'],
                                  bandpassName=pointing['filter'],
                                  boundType=boundType,
                                  boundLength=boundLength)

        if 'fiveSigmaDepth' in pointing_column_names:
            obs.m5 = pointing['fiveSigmaDepth']
        if 'filtSkyBrightness' in pointing_column_names:
            obs.skyBrightness = pointing['filtSkyBrightness']
        if self._seeing_column in pointing_column_names:
            obs.seeing = pointing[self._seeing_column]
        if 'rotSkyPos' in pointing_column_names:
            obs.rotSkyPos = np.degrees(pointing['rotSkyPos'])

        obs.OpsimMetaData = raw_dict

        return obs
Esempio n. 10
0
def makePhoSimTestDB(filename='PhoSimTestDatabase.db', size=1000, seedVal=32, radius=0.1,
                     deltaRA=None, deltaDec=None,
                     bandpass='******', m5=None, seeing=None,
                     magnorm_min=17.0, delta_magnorm=4.0, **kwargs):
    """
    Make a test database to storing cartoon information for the test phoSim input
    catalog to use.

    The method will return an ObservationMetaData object guaranteed to encompass the
    objects in this database.

    @param [in] filename is a string indicating the name of the DB file to be created

    @param [in] size is the number of objects int he database

    @param [in] seedVal is the seed passed to the random number generator

    @param [in] radius is the radius (in degrees) of the field of view to be returned

    @param [in] bandpass is the bandpas(es) of the observation to be passed to
    ObservationMetaData (optional)

    @param [in] m5 is the m5 value(s) to be passed to ObservationMetaData
    (optional)

    @param [in] seeing is the seeing value(s) in arcseconds to be passed to
    ObservationMetaData (optional)

    @param [in] deltaRA/Dec are numpy arrays that indicate where (in relation to the center
    of the field of view) objects should be placed.  These coordinates are in degrees.  Specifying
    either of these paramters will overwrite size.  If you only specify one of these parameters, the other
    will be set randomly.  These parameters are optional.

    @param [in] magnorm_min is the min magnorm (magNorms for sources in the database will be
    distributed according to a random deviate drawn from magnorm_min + random*delta_magnorm)

    @param [in] delta_magnorm (see documentation for magnorm_min)
    """

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

    # just an example of some valid SED file names
    galaxy_seds = ['Const.80E07.02Z.spec', 'Inst.80E07.002Z.spec', 'Burst.19E07.0005Z.spec']
    agn_sed = 'agn.spec'
    star_seds = ['km20_5750.fits_g40_5790', 'm2.0Full.dat', 'bergeron_6500_85.dat_6700']

    rng = np.random.RandomState(seedVal)

    if deltaRA is not None and deltaDec is not None:
        if len(deltaRA) != len(deltaDec):
            raise RuntimeError("WARNING in makePhoSimTestDB deltaRA and "
                               "deltaDec have different lengths")

    if deltaRA is not None:
        size = len(deltaRA)
    elif deltaDec is not None:
        size = len(deltaDec)

    # create the ObservationMetaData object
    mjd = 52000.0
    alt = np.pi/2.0
    az = 0.0

    testSite = Site(name='LSST')
    obsTemp = ObservationMetaData(mjd=mjd, site=testSite)
    centerRA, centerDec = _raDecFromAltAz(alt, az, obsTemp)
    rotTel = _getRotTelPos(centerRA, centerDec, obsTemp, 0.0)
    rotSkyPos = _getRotSkyPos(centerRA, centerDec, obsTemp, rotTel)

    obs_metadata = ObservationMetaData(pointingRA=np.degrees(centerRA),
                                       pointingDec=np.degrees(centerDec),
                                       rotSkyPos=np.degrees(rotSkyPos),
                                       bandpassName=bandpass,
                                       mjd=mjd,
                                       boundType = 'circle', boundLength = 2.0*radius,
                                       site=testSite,
                                       m5=m5, seeing=seeing)

    moon_alt = -90.0
    sun_alt = -90.0

    moon_ra, moon_dec = raDecFromAltAz(moon_alt, 0.0, obs_metadata)
    dist2moon = haversine(np.radians(moon_ra), np.radians(moon_dec),
                          obs_metadata._pointingRA, obs_metadata._pointingDec)

    obs_metadata.OpsimMetaData = {'moonra': moon_ra,
                                  'moondec': moon_dec,
                                  'moonalt': moon_alt,
                                  'sunalt': sun_alt,
                                  'dist2moon': dist2moon,
                                  'rottelpos': np.degrees(rotTel)}

    # Now begin building the database.
    # First create the tables.
    conn = sqlite3.connect(filename)
    c = conn.cursor()
    try:
        c.execute('''CREATE TABLE galaxy_bulge
                 (galtileid int, galid int, bra real, bdec real, ra real, dec real, magnorm_bulge real,
                 sedname_bulge text, a_b real, b_b real, pa_bulge real, bulge_n int,
                 ext_model_b text, av_b real, rv_b real, u_ab real, g_ab real, r_ab real, i_ab real,
                 z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real)''')
        conn.commit()
    except:
        raise RuntimeError("Error creating galaxy_bulge table.")

    try:
        c.execute('''CREATE TABLE galaxy
                     (galtileid int, galid int, ra real, dec real,
                      bra real, bdec real, dra real, ddec real,
                      agnra real, agndec real,
                      magnorm_bulge, magnorm_disk, magnorm_agn,
                      sedname_bulge text, sedname_disk text, sedname_agn text,
                      varParamStr text,
                      a_b real, b_b real, pa_bulge real, bulge_n int,
                      a_d real, b_d real, pa_disk real, disk_n int,
                      ext_model_b text, av_b real, rv_b real,
                      ext_model_d text, av_d real, rv_d real,
                      u_ab real, g_ab real, r_ab real, i_ab real,
                      z_ab real, y_ab real,
                      redshift real, BulgeHalfLightRadius real, DiskHalfLightRadius real)''')

        conn.commit()
    except:
        raise RuntimeError("Error creating galaxy table.")

    try:
        c.execute('''CREATE TABLE galaxy_agn
                  (galtileid int, galid int, agnra real, agndec real, ra real, dec real,
                  magnorm_agn real, sedname_agn text, varParamStr text, u_ab real,
                  g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real)''')
    except:
        raise RuntimeError("Error creating galaxy_agn table.")

    try:
        c.execute('''CREATE TABLE StarAllForceseek
                  (simobjid int, ra real, decl real, magNorm real,
                  mudecl real, mura real, galacticAv real, vrad real, varParamStr text,
                  sedFilename text, parallax real, ebv real)''')
    except:
        raise RuntimeError("Error creating StarAllForceseek table.")

    # Now generate the data to be stored in the tables.

    rr = rng.random_sample(size)*np.radians(radius)
    theta = rng.random_sample(size)*2.0*np.pi

    if deltaRA is None:
        ra = np.degrees(centerRA + rr*np.cos(theta))
    else:
        ra = np.degrees(centerRA) + deltaRA

    if deltaDec is None:
        dec = np.degrees(centerDec + rr*np.sin(theta))
    else:
        dec = np.degrees(centerDec) + deltaDec

    bra = np.radians(ra+rng.random_sample(size)*0.01*radius)
    bdec = np.radians(dec+rng.random_sample(size)*0.01*radius)
    dra = np.radians(ra + rng.random_sample(size)*0.01*radius)
    ddec = np.radians(dec + rng.random_sample(size)*0.01*radius)
    agnra = np.radians(ra + rng.random_sample(size)*0.01*radius)
    agndec = np.radians(dec + rng.random_sample(size)*0.01*radius)

    magnorm_bulge = rng.random_sample(size)*delta_magnorm + magnorm_min
    magnorm_disk = rng.random_sample(size)*delta_magnorm + magnorm_min
    magnorm_agn = rng.random_sample(size)*delta_magnorm + magnorm_min
    b_b = rng.random_sample(size)*0.2
    a_b = b_b+rng.random_sample(size)*0.05
    b_d = rng.random_sample(size)*0.5
    a_d = b_d+rng.random_sample(size)*0.1

    BulgeHalfLightRadius = rng.random_sample(size)*0.2
    DiskHalfLightRadius = rng.random_sample(size)*0.5

    pa_bulge = rng.random_sample(size)*360.0
    pa_disk = rng.random_sample(size)*360.0

    av_b = rng.random_sample(size)*0.3
    av_d = rng.random_sample(size)*0.3
    rv_b = rng.random_sample(size)*0.1 + 2.0
    rv_d = rng.random_sample(size)*0.1 + 2.0

    u_ab = rng.random_sample(size)*4.0 + 17.0
    g_ab = rng.random_sample(size)*4.0 + 17.0
    r_ab = rng.random_sample(size)*4.0 + 17.0
    i_ab = rng.random_sample(size)*4.0 + 17.0
    z_ab = rng.random_sample(size)*4.0 + 17.0
    y_ab = rng.random_sample(size)*4.0 + 17.0
    redshift = rng.random_sample(size)*2.0

    t0_mjd = mjd - rng.random_sample(size)*1000.0
    agn_tau = rng.random_sample(size)*1000.0 + 1000.0
    agnSeed = rng.randint(2, 4001, size=size)
    agn_sfu = rng.random_sample(size)
    agn_sfg = rng.random_sample(size)
    agn_sfr = rng.random_sample(size)
    agn_sfi = rng.random_sample(size)
    agn_sfz = rng.random_sample(size)
    agn_sfy = rng.random_sample(size)

    rrStar = rng.random_sample(size)*np.radians(radius)
    thetaStar = rng.random_sample(size)*2.0*np.pi

    if deltaRA is None:
        raStar = centerRA + rrStar*np.cos(thetaStar)
    else:
        raStar = centerRA + np.radians(deltaRA)

    if deltaDec is None:
        decStar = centerDec + rrStar*np.sin(thetaStar)
    else:
        decStar = centerDec + np.radians(deltaDec)

    raStar = np.degrees(raStar)
    decStar = np.degrees(decStar)

    magnormStar = rng.random_sample(size)*delta_magnorm + magnorm_min
    mudecl = rng.random_sample(size)*0.0001
    mura = rng.random_sample(size)*0.0001
    galacticAv = rng.random_sample(size)*0.05*3.1
    vrad = rng.random_sample(size)*1.0
    parallax = 0.00045+rng.random_sample(size)*0.00001
    period = rng.random_sample(size)*20.0
    amp = rng.random_sample(size)*5.0

    # write the data to the tables.
    for i in range(size):

        cmd = '''INSERT INTO galaxy_bulge VALUES (%i, %i, %f, %f, %f, %f, %f,
              '%s', %f, %f, %f, %i, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' % \
              (i, i, bra[i], bdec[i], ra[i], dec[i], magnorm_bulge[i], galaxy_seds[i%len(galaxy_seds)],
               a_b[i], b_b[i], pa_bulge[i], 4, 'CCM', av_b[i], rv_b[i], u_ab[i], g_ab[i],
               r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i])

        c.execute(cmd)

        varParam = {'varMethodName': 'applyAgn',
                    'pars': {'agn_tau': round(agn_tau[i], 4), 't0_mjd': round(t0_mjd[i], 4),
                             'agn_sfu': round(agn_sfu[i], 4), 'agn_sfg': round(agn_sfg[i], 4),
                             'agn_sfr': round(agn_sfr[i], 4), 'agn_sfi': round(agn_sfi[i], 4),
                             'agn_sfz': round(agn_sfz[i], 4), 'agn_sfy': round(agn_sfy[i], 4),
                             'seed': int(agnSeed[i])}}

        paramStr = json.dumps(varParam)

        cmd = '''INSERT INTO galaxy VALUES (%i, %i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f,
                                            '%s', '%s', '%s', '%s',
                                            %f, %f, %f, %i,
                                            %f, %f, %f, %i,
                                            '%s', %f, %f,
                                            '%s', %f, %f,
                                            %f, %f, %f, %f, %f, %f,
                                            %f, %f, %f)''' % \
              (i, i, ra[i], dec[i], bra[i], bdec[i], dra[i], ddec[i], agnra[i], agndec[i],
               magnorm_bulge[i], magnorm_disk[i], magnorm_agn[i],
               galaxy_seds[i%len(galaxy_seds)], galaxy_seds[i%len(galaxy_seds)], agn_sed,
               paramStr,
               a_b[i], b_b[i], pa_bulge[i], 4,
               a_d[i], b_d[i], pa_disk[i], 1,
               'CCM', av_b[i], rv_b[i],
               'CCM', av_d[i], rv_d[i],
               u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i],
               BulgeHalfLightRadius[i], DiskHalfLightRadius[i])
        c.execute(cmd)

        cmd = '''INSERT INTO galaxy_agn VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', '%s',
              %f, %f, %f, %f, %f, %f, %f)''' % \
              (i, i, agnra[i], agndec[i], ra[i], dec[i],
               magnorm_agn[i], agn_sed, paramStr,
               u_ab[i], g_ab[i], r_ab[i], i_ab[i],
               z_ab[i], y_ab[i], redshift[i])

        c.execute(cmd)

        varParam = {'varMethodName': 'testVar', 'pars': {'period': period[i], 'amplitude': amp[i]}}
        paramStr = json.dumps(varParam)
        cmd = '''INSERT INTO StarAllForceseek VALUES (%i, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', %f, %f)''' %\
              (i, raStar[i], decStar[i], magnormStar[i], mudecl[i], mura[i],
               galacticAv[i], vrad[i], paramStr, star_seds[i%len(star_seds)], parallax[i],
               galacticAv[i]/3.1)

        c.execute(cmd)

    conn.commit()
    conn.close()
    return obs_metadata
Esempio n. 11
0
    def testFitsHeader(self):
        """
        Create a test image with the LSST camera and with the
        cartoon camera.  Verify that the image created with the LSST
        camera has the DM-required cards in its FITS header while the
        image created with the cartoon camera does not
        """

        lsstCamera = LsstSimMapper().camera
        cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData')
        cartoonCamera = ReturnCamera(cameraDir)

        outputDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                                 'scratchSpace')

        lsst_cat_name = os.path.join(outputDir, 'fits_test_lsst_cat.txt')
        lsst_cat_root = os.path.join(outputDir, 'fits_test_lsst_image')

        cartoon_cat_name = os.path.join(outputDir, 'fits_test_cartoon_cat.txt')
        cartoon_cat_root = os.path.join(outputDir, 'fits_test_cartoon_image')

        obs = ObservationMetaData(pointingRA=32.0, pointingDec=22.0,
                                  boundLength=0.1, boundType='circle',
                                  mjd=58000.0, rotSkyPos=14.0, bandpassName='u')

        obs.OpsimMetaData = {'obshistID': 112}

        dbFileName = os.path.join(outputDir, 'fits_test_db.dat')
        create_text_catalog(obs, dbFileName, np.array([30.0]), np.array([30.0]), [22.0])
        db = fitsHeaderFileDBObj(dbFileName, runtable='test')

        # first test the lsst camera
        lsstCat = fitsHeaderCatalog(db, obs_metadata=obs)
        lsstCat.camera = lsstCamera
        lsstCat.PSF = SNRdocumentPSF()
        lsstCat.write_catalog(lsst_cat_name)
        lsstCat.write_images(nameRoot=lsst_cat_root)

        list_of_files = os.listdir(outputDir)
        ct = 0
        for file_name in list_of_files:
            true_name = os.path.join(outputDir, file_name)
            if lsst_cat_root in true_name:
                ct += 1
                fitsTest = fits.open(true_name)
                header = fitsTest[0].header
                self.assertIn('CHIPID', header)
                self.assertIn('OBSID', header)
                self.assertIn('OUTFILE', header)
                self.assertEqual(header['OBSID'], 112)
                self.assertEqual(header['CHIPID'], 'R22_S11')
                self.assertEqual(header['OUTFILE'], 'lsst_e_112_f0_R22_S11_E000')
                os.unlink(true_name)

        self.assertGreater(ct, 0)
        if os.path.exists(lsst_cat_name):
            os.unlink(lsst_cat_name)

        # now test with the cartoon camera
        cartoonCat = fitsHeaderCatalog(db, obs_metadata=obs)
        cartoonCat.camera = cartoonCamera
        cartoonCat.PSF = SNRdocumentPSF()
        cartoonCat.write_catalog(cartoon_cat_name)
        cartoonCat.write_images(nameRoot=cartoon_cat_root)
        list_of_files = os.listdir(outputDir)
        ct = 0
        for file_name in list_of_files:
            true_name = os.path.join(outputDir, file_name)
            if cartoon_cat_root in true_name:
                ct += 1
                fitsTest = fits.open(true_name)
                header = fitsTest[0].header
                self.assertNotIn('CHIPID', header)
                self.assertNotIn('OBSID', header)
                self.assertNotIn('OUTFILE', header)
                os.unlink(true_name)

        self.assertGreater(ct, 0)
        if os.path.exists(cartoon_cat_name):
            os.unlink(cartoon_cat_name)

        if os.path.exists(dbFileName):
            os.unlink(dbFileName)
out_dir = 'ratio_test/catalogs'

sed_candidates = os.listdir(galaxy_dir)

sed_names = rng.choice(sed_candidates, size=len(ra), replace=True)
redshift_vals = rng.random_sample(len(sed_names))*2.0

for i_filter in range(6):
    obs = ObservationMetaData(pointingRA=obs_root.pointingRA,
                              pointingDec=obs_root.pointingDec,
                              rotSkyPos=obs_root.rotSkyPos,
                              mjd=obs_root.mjd,
                              site=obs_root.site,
                              bandpassName='ugrizy'[i_filter])
    obs.OpsimMetaData = obs_root.OpsimMetaData
    obs.OpsimMetaData['sunalt'] = -90.0
    phosim_header['sunalt'] = -90.0
    phosim_header['camconfig'] = 1

    out_name = os.path.join(out_dir, 'instcat_ratio_grid_%d.txt' % (i_filter))
    with open(out_name, 'w') as instcat_handle:
        phosim_header['obshistid'] = 100+i_filter

        write_phoSim_header(obs, instcat_handle, phosim_header)
        obj_ct = 0
        redshift = 0.0
        for ii, (rr, dd, zz, ss) in enumerate(zip(ra, dec, redshift_vals, sed_names)):
            magnorm = np.interp(zz, magnorm_interp[ss]['z'], magnorm_interp[ss]['mag'])
            unqid = ii+1
            instcat_handle.write('object %d ' % unqid)
Esempio n. 13
0
    def getObservationMetaData(self,
                               searchCenter,
                               searchRadius,
                               constraint=None,
                               fovRadius=1.75,
                               makeCircBounds=True,
                               makeBoxBounds=False):
        """
        This method will perform a query on the OpSim3_61 for pointings within a given region
        on the sky.  It will then return a list of ObservationMetaData corresponding to the
        OpSim pointings returned by that query.

        param [in] searchCenter is a tuple (RA, Dec) indicating the center of the region
        of the sky wherein to search for OpSim pointings (in degrees).

        param [in] searchRadius is the radius of the region of the sky wherein to search
        for OpSim pointings (in degrees).

        param [in] constraint an optional constraint on another column.  Should be a string
        of the form 'airmass=1.1'

        param [in] fovRadius is the radius (in degrees) of the resulting ObservationMetaDatas'
        fields of view.  If you have specified a box bounds this will be half the length of each
        side of the box.

        param [in] makeCircBounds a boolean telling this method to construct an ObservationMetaData
        for a circular region of the sky centered on the specified OpSim pointing

        param [in] make BoxBounds a boolean telling this method to construct an
        ObservationMetaData for a square region of the sky centered on the specified
        OpSim pointing
        """

        spatialBound = CircleBounds(numpy.radians(searchCenter[0]),
                                    numpy.radians(searchCenter[1]),
                                    numpy.radians(searchRadius))

        # get the results of the query as a numpy recarray
        result = self.executeConstrainedQuery(spatialBound,
                                              constraint=constraint)

        obs_list = []
        ra_list = numpy.degrees(result['pointingRA'])
        dec_list = numpy.degrees(result['pointingDec'])
        rotSkyPos_list = numpy.degrees(result['rotSkyPos'])
        for pointing, ra, dec, rotSkyPos in zip(result, ra_list, dec_list,
                                                rotSkyPos_list):

            mjd = pointing['expMJD']

            #because makeCircBounds defaults to True, check whether the user is
            #requesting boxBounds before deciding to instantiate
            #circBounds

            boundType = None
            boundLength = None
            if makeBoxBounds:
                boundType = 'box'
                boundLength = numpy.array(
                    [fovRadius / math.cos(numpy.radians(dec)), fovRadius])
            elif makeCircBounds:
                boundType = 'circle'
                boundLength = fovRadius
            else:
                raise ValueErr("Need either makeBoxBounds or makeCircBounds")

            obs = ObservationMetaData(pointingRA=ra,
                                      pointingDec=dec,
                                      rotSkyPos=rotSkyPos,
                                      mjd=mjd,
                                      bandpassName=result['filter'][0],
                                      boundType=boundType,
                                      boundLength=boundLength)

            raw_opsim_dict = dict([(k, pointing[k])
                                   for k in result.dtype.names])
            obs.OpsimMetaData = raw_opsim_dict
            obs_list.append(obs)

        return obs_list
Esempio n. 14
0
    def getObservationMetaData(self, searchCenter, searchRadius, constraint=None,
                               fovRadius=1.75, makeCircBounds=True, makeBoxBounds=False):
        """
        This method will perform a query on the OpSim3_61 for pointings within a given region
        on the sky.  It will then return a list of ObservationMetaData corresponding to the
        OpSim pointings returned by that query.

        param [in] searchCenter is a tuple (RA, Dec) indicating the center of the region
        of the sky wherein to search for OpSim pointings (in degrees).

        param [in] searchRadius is the radius of the region of the sky wherein to search
        for OpSim pointings (in degrees).

        param [in] constraint an optional constraint on another column.  Should be a string
        of the form 'airmass=1.1'

        param [in] fovRadius is the radius (in degrees) of the resulting ObservationMetaDatas'
        fields of view.  If you have specified a box bounds this will be half the length of each
        side of the box.

        param [in] makeCircBounds a boolean telling this method to construct an ObservationMetaData
        for a circular region of the sky centered on the specified OpSim pointing

        param [in] make BoxBounds a boolean telling this method to construct an
        ObservationMetaData for a square region of the sky centered on the specified
        OpSim pointing
        """

        spatialBound = CircleBounds(numpy.radians(searchCenter[0]), numpy.radians(searchCenter[1]),
                                    numpy.radians(searchRadius))

        # get the results of the query as a numpy recarray
        result = self.executeConstrainedQuery(spatialBound, constraint=constraint)

        obs_list = []
        ra_list = numpy.degrees(result['pointingRA'])
        dec_list = numpy.degrees(result['pointingDec'])
        rotSkyPos_list = numpy.degrees(result['rotSkyPos'])
        for pointing, ra, dec, rotSkyPos in zip(result, ra_list, dec_list, rotSkyPos_list):

            mjd = pointing['expMJD']

            #because makeCircBounds defaults to True, check whether the user is
            #requesting boxBounds before deciding to instantiate
            #circBounds

            boundType = None
            boundLength = None
            if makeBoxBounds:
                boundType = 'box'
                boundLength = numpy.array([fovRadius/math.cos(numpy.radians(dec)), fovRadius])
            elif makeCircBounds:
                boundType = 'circle'
                boundLength = fovRadius
            else:
                raise ValueErr("Need either makeBoxBounds or makeCircBounds")

            obs = ObservationMetaData(pointingRA=ra, pointingDec=dec,
                                      rotSkyPos=rotSkyPos, mjd=mjd,
                                      bandpassName=result['filter'][0],
                                      boundType=boundType, boundLength=boundLength)

            raw_opsim_dict = dict([(k, pointing[k]) for k in result.dtype.names])
            obs.OpsimMetaData = raw_opsim_dict
            obs_list.append(obs)

        return obs_list
Esempio n. 15
0
def makePhoSimTestDB(filename='PhoSimTestDatabase.db',
                     size=1000,
                     seedVal=32,
                     radius=0.1,
                     deltaRA=None,
                     deltaDec=None,
                     bandpass='******',
                     m5=None,
                     seeing=None,
                     magnorm_min=17.0,
                     delta_magnorm=4.0,
                     **kwargs):
    """
    Make a test database to storing cartoon information for the test phoSim input
    catalog to use.

    The method will return an ObservationMetaData object guaranteed to encompass the
    objects in this database.

    @param [in] filename is a string indicating the name of the DB file to be created

    @param [in] size is the number of objects int he database

    @param [in] seedVal is the seed passed to the random number generator

    @param [in] radius is the radius (in degrees) of the field of view to be returned

    @param [in] bandpass is the bandpas(es) of the observation to be passed to
    ObservationMetaData (optional)

    @param [in] m5 is the m5 value(s) to be passed to ObservationMetaData
    (optional)

    @param [in] seeing is the seeing value(s) in arcseconds to be passed to
    ObservationMetaData (optional)

    @param [in] deltaRA/Dec are numpy arrays that indicate where (in relation to the center
    of the field of view) objects should be placed.  These coordinates are in degrees.  Specifying
    either of these paramters will overwrite size.  If you only specify one of these parameters, the other
    will be set randomly.  These parameters are optional.

    @param [in] magnorm_min is the min magnorm (magNorms for sources in the database will be
    distributed according to a random deviate drawn from magnorm_min + random*delta_magnorm)

    @param [in] delta_magnorm (see documentation for magnorm_min)
    """

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

    # just an example of some valid SED file names
    galaxy_seds = [
        'Const.80E07.02Z.spec', 'Inst.80E07.002Z.spec',
        'Burst.19E07.0005Z.spec'
    ]
    agn_sed = 'agn.spec'
    star_seds = [
        'km20_5750.fits_g40_5790', 'm2.0Full.dat', 'bergeron_6500_85.dat_6700'
    ]

    rng = np.random.RandomState(seedVal)

    if deltaRA is not None and deltaDec is not None:
        if len(deltaRA) != len(deltaDec):
            raise RuntimeError("WARNING in makePhoSimTestDB deltaRA and "
                               "deltaDec have different lengths")

    if deltaRA is not None:
        size = len(deltaRA)
    elif deltaDec is not None:
        size = len(deltaDec)

    # create the ObservationMetaData object
    mjd = 52000.0
    alt = np.pi / 2.0
    az = 0.0

    testSite = Site(name='LSST')
    obsTemp = ObservationMetaData(mjd=mjd, site=testSite)
    centerRA, centerDec = _raDecFromAltAz(alt, az, obsTemp)
    rotTel = _getRotTelPos(centerRA, centerDec, obsTemp, 0.0)
    rotSkyPos = _getRotSkyPos(centerRA, centerDec, obsTemp, rotTel)

    obs_metadata = ObservationMetaData(pointingRA=np.degrees(centerRA),
                                       pointingDec=np.degrees(centerDec),
                                       rotSkyPos=np.degrees(rotSkyPos),
                                       bandpassName=bandpass,
                                       mjd=mjd,
                                       boundType='circle',
                                       boundLength=2.0 * radius,
                                       site=testSite,
                                       m5=m5,
                                       seeing=seeing)

    moon_alt = -90.0
    sun_alt = -90.0

    moon_ra, moon_dec = raDecFromAltAz(moon_alt, 0.0, obs_metadata)
    dist2moon = haversine(np.radians(moon_ra), np.radians(moon_dec),
                          obs_metadata._pointingRA, obs_metadata._pointingDec)

    obs_metadata.OpsimMetaData = {
        'moonra': moon_ra,
        'moondec': moon_dec,
        'moonalt': moon_alt,
        'sunalt': sun_alt,
        'dist2moon': dist2moon,
        'rottelpos': np.degrees(rotTel)
    }

    # Now begin building the database.
    # First create the tables.
    conn = sqlite3.connect(filename)
    c = conn.cursor()
    try:
        c.execute('''CREATE TABLE galaxy_bulge
                 (galtileid int, galid int, bra real, bdec real, ra real, dec real, magnorm_bulge real,
                 sedname_bulge text, a_b real, b_b real, pa_bulge real, bulge_n int,
                 ext_model_b text, av_b real, rv_b real, u_ab real, g_ab real, r_ab real, i_ab real,
                 z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real)'''
                  )
        conn.commit()
    except:
        raise RuntimeError("Error creating galaxy_bulge table.")

    try:
        c.execute('''CREATE TABLE galaxy
                     (galtileid int, galid int, ra real, dec real,
                      bra real, bdec real, dra real, ddec real,
                      agnra real, agndec real,
                      magnorm_bulge, magnorm_disk, magnorm_agn,
                      sedname_bulge text, sedname_disk text, sedname_agn text,
                      varParamStr text,
                      a_b real, b_b real, pa_bulge real, bulge_n int,
                      a_d real, b_d real, pa_disk real, disk_n int,
                      ext_model_b text, av_b real, rv_b real,
                      ext_model_d text, av_d real, rv_d real,
                      u_ab real, g_ab real, r_ab real, i_ab real,
                      z_ab real, y_ab real,
                      redshift real, BulgeHalfLightRadius real, DiskHalfLightRadius real)'''
                  )

        conn.commit()
    except:
        raise RuntimeError("Error creating galaxy table.")

    try:
        c.execute('''CREATE TABLE galaxy_agn
                  (galtileid int, galid int, agnra real, agndec real, ra real, dec real,
                  magnorm_agn real, sedname_agn text, varParamStr text, u_ab real,
                  g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real)'''
                  )
    except:
        raise RuntimeError("Error creating galaxy_agn table.")

    try:
        c.execute('''CREATE TABLE StarAllForceseek
                  (simobjid int, ra real, decl real, magNorm real,
                  mudecl real, mura real, galacticAv real, vrad real, varParamStr text,
                  sedFilename text, parallax real, ebv real)''')
    except:
        raise RuntimeError("Error creating StarAllForceseek table.")

    # Now generate the data to be stored in the tables.

    rr = rng.random_sample(size) * np.radians(radius)
    theta = rng.random_sample(size) * 2.0 * np.pi

    if deltaRA is None:
        ra = np.degrees(centerRA + rr * np.cos(theta))
    else:
        ra = np.degrees(centerRA) + deltaRA

    if deltaDec is None:
        dec = np.degrees(centerDec + rr * np.sin(theta))
    else:
        dec = np.degrees(centerDec) + deltaDec

    bra = np.radians(ra + rng.random_sample(size) * 0.01 * radius)
    bdec = np.radians(dec + rng.random_sample(size) * 0.01 * radius)
    dra = np.radians(ra + rng.random_sample(size) * 0.01 * radius)
    ddec = np.radians(dec + rng.random_sample(size) * 0.01 * radius)
    agnra = np.radians(ra + rng.random_sample(size) * 0.01 * radius)
    agndec = np.radians(dec + rng.random_sample(size) * 0.01 * radius)

    magnorm_bulge = rng.random_sample(size) * delta_magnorm + magnorm_min
    magnorm_disk = rng.random_sample(size) * delta_magnorm + magnorm_min
    magnorm_agn = rng.random_sample(size) * delta_magnorm + magnorm_min
    b_b = rng.random_sample(size) * 0.2
    a_b = b_b + rng.random_sample(size) * 0.05
    b_d = rng.random_sample(size) * 0.5
    a_d = b_d + rng.random_sample(size) * 0.1

    BulgeHalfLightRadius = rng.random_sample(size) * 0.2
    DiskHalfLightRadius = rng.random_sample(size) * 0.5

    pa_bulge = rng.random_sample(size) * 360.0
    pa_disk = rng.random_sample(size) * 360.0

    av_b = rng.random_sample(size) * 0.3
    av_d = rng.random_sample(size) * 0.3
    rv_b = rng.random_sample(size) * 0.1 + 2.0
    rv_d = rng.random_sample(size) * 0.1 + 2.0

    u_ab = rng.random_sample(size) * 4.0 + 17.0
    g_ab = rng.random_sample(size) * 4.0 + 17.0
    r_ab = rng.random_sample(size) * 4.0 + 17.0
    i_ab = rng.random_sample(size) * 4.0 + 17.0
    z_ab = rng.random_sample(size) * 4.0 + 17.0
    y_ab = rng.random_sample(size) * 4.0 + 17.0
    redshift = rng.random_sample(size) * 2.0

    t0_mjd = mjd - rng.random_sample(size) * 1000.0
    agn_tau = rng.random_sample(size) * 1000.0 + 1000.0
    agnSeed = rng.randint(2, 4001, size=size)
    agn_sfu = rng.random_sample(size)
    agn_sfg = rng.random_sample(size)
    agn_sfr = rng.random_sample(size)
    agn_sfi = rng.random_sample(size)
    agn_sfz = rng.random_sample(size)
    agn_sfy = rng.random_sample(size)

    rrStar = rng.random_sample(size) * np.radians(radius)
    thetaStar = rng.random_sample(size) * 2.0 * np.pi

    if deltaRA is None:
        raStar = centerRA + rrStar * np.cos(thetaStar)
    else:
        raStar = centerRA + np.radians(deltaRA)

    if deltaDec is None:
        decStar = centerDec + rrStar * np.sin(thetaStar)
    else:
        decStar = centerDec + np.radians(deltaDec)

    raStar = np.degrees(raStar)
    decStar = np.degrees(decStar)

    magnormStar = rng.random_sample(size) * delta_magnorm + magnorm_min
    mudecl = rng.random_sample(size) * 0.0001
    mura = rng.random_sample(size) * 0.0001
    galacticAv = rng.random_sample(size) * 0.05 * 3.1
    vrad = rng.random_sample(size) * 1.0
    parallax = 0.00045 + rng.random_sample(size) * 0.00001
    period = rng.random_sample(size) * 20.0
    amp = rng.random_sample(size) * 5.0

    # write the data to the tables.
    for i in range(size):

        cmd = '''INSERT INTO galaxy_bulge VALUES (%i, %i, %f, %f, %f, %f, %f,
              '%s', %f, %f, %f, %i, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' % \
              (i, i, bra[i], bdec[i], ra[i], dec[i], magnorm_bulge[i], galaxy_seds[i%len(galaxy_seds)],
               a_b[i], b_b[i], pa_bulge[i], 4, 'CCM', av_b[i], rv_b[i], u_ab[i], g_ab[i],
               r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i])

        c.execute(cmd)

        varParam = {
            'varMethodName': 'applyAgn',
            'pars': {
                'agn_tau': round(agn_tau[i], 4),
                't0_mjd': round(t0_mjd[i], 4),
                'agn_sfu': round(agn_sfu[i], 4),
                'agn_sfg': round(agn_sfg[i], 4),
                'agn_sfr': round(agn_sfr[i], 4),
                'agn_sfi': round(agn_sfi[i], 4),
                'agn_sfz': round(agn_sfz[i], 4),
                'agn_sfy': round(agn_sfy[i], 4),
                'seed': int(agnSeed[i])
            }
        }

        paramStr = json.dumps(varParam)

        cmd = '''INSERT INTO galaxy VALUES (%i, %i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f,
                                            '%s', '%s', '%s', '%s',
                                            %f, %f, %f, %i,
                                            %f, %f, %f, %i,
                                            '%s', %f, %f,
                                            '%s', %f, %f,
                                            %f, %f, %f, %f, %f, %f,
                                            %f, %f, %f)''' % \
              (i, i, ra[i], dec[i], bra[i], bdec[i], dra[i], ddec[i], agnra[i], agndec[i],
               magnorm_bulge[i], magnorm_disk[i], magnorm_agn[i],
               galaxy_seds[i%len(galaxy_seds)], galaxy_seds[i%len(galaxy_seds)], agn_sed,
               paramStr,
               a_b[i], b_b[i], pa_bulge[i], 4,
               a_d[i], b_d[i], pa_disk[i], 1,
               'CCM', av_b[i], rv_b[i],
               'CCM', av_d[i], rv_d[i],
               u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i],
               BulgeHalfLightRadius[i], DiskHalfLightRadius[i])
        c.execute(cmd)

        cmd = '''INSERT INTO galaxy_agn VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', '%s',
              %f, %f, %f, %f, %f, %f, %f)''' % \
              (i, i, agnra[i], agndec[i], ra[i], dec[i],
               magnorm_agn[i], agn_sed, paramStr,
               u_ab[i], g_ab[i], r_ab[i], i_ab[i],
               z_ab[i], y_ab[i], redshift[i])

        c.execute(cmd)

        varParam = {
            'varMethodName': 'testVar',
            'pars': {
                'period': period[i],
                'amplitude': amp[i]
            }
        }
        paramStr = json.dumps(varParam)
        cmd = '''INSERT INTO StarAllForceseek VALUES (%i, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', %f, %f)''' %\
              (i, raStar[i], decStar[i], magnormStar[i], mudecl[i], mura[i],
               galacticAv[i], vrad[i], paramStr, star_seds[i%len(star_seds)], parallax[i],
               galacticAv[i]/3.1)

        c.execute(cmd)

    conn.commit()
    conn.close()
    return obs_metadata
Esempio n. 16
0
def createTestSNDB():
    """
    Create a CatalogDBObject-like database that contains everything needed to create
    a supernova catalog.  Return the CatalogDBObject and an ObservationMetaData pointing
    to its center.

    Note: the OpsimMetaData for the returned ObservationMetaData will be inconsistent.
    It is just there so that PhoSim InstanceCatalog classes can write out a header.
    """

    raCenter = 23.0
    decCenter = -19.0
    radius = 0.1
    obs = ObservationMetaData(pointingRA=raCenter,
                              pointingDec=decCenter,
                              boundType='circle',
                              boundLength=radius,
                              rotSkyPos=33.0,
                              mjd=59580.0,
                              bandpassName='r')

    # these will be totally inconsistent; just need something to put in the header
    obs.OpsimMetaData = {
        'dist2Moon': 0.1,
        'moonalt': -0.2,
        'moonra': 1.1,
        'moondec': 0.5,
        'rottelpos': 0.4,
        'sunalt': -1.1
    }

    rng = np.random.RandomState(88)
    n_obj = 10
    rr = rng.random_sample(n_obj) * radius
    theta = rng.random_sample(n_obj) * 2.0 * np.pi
    ra_list = raCenter + rr * np.cos(theta)
    dec_list = decCenter + rr * np.sin(theta)
    t0_list = 0.5 * rng.random_sample(n_obj)
    x0_list = rng.random_sample(n_obj) * 1.0e-4
    x1_list = rng.random_sample(n_obj)
    c_list = rng.random_sample(n_obj) * 2.0
    z_list = rng.random_sample(n_obj) * 1.1 + 0.1

    txt_file_name = tempfile.mktemp(dir=ROOT,
                                    prefix='test_phosim_sn_source',
                                    suffix='.txt')

    dtype = np.dtype([('id', int), ('snra', float), ('sndec', float),
                      ('t0', float), ('x0', float), ('x1', float),
                      ('c', float), ('redshift', float), ('galtileid', int)])

    with open(txt_file_name, 'w') as output_file:
        for ix, (ra, dec, t0, x0, x1, c, z) in enumerate(
                zip(ra_list, dec_list, t0_list, x0_list, x1_list, c_list,
                    z_list)):
            output_file.write('%d %e %e %e %e %e %e %e %d\n' %
                              (ix, ra, dec, t0, x0, x1, c, z, ix))

    class testSNDBObj(SNDBObj, fileDBObject):
        tableid = 'test'

        def query_columns(self, *args, **kwargs):
            return fileDBObject.query_columns(self, *args, **kwargs)

    dbobj = testSNDBObj(txt_file_name, runtable='test', dtype=dtype)

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

    return dbobj, obs
Esempio n. 17
0
    def ObservationMetaDataFromPointing(self,
                                        OpSimPointingRecord,
                                        OpSimColumns=None,
                                        boundLength=1.75,
                                        boundType='circle'):
        """
        Return instance of ObservationMetaData for an OpSim Pointing record
        from OpSim.

        Parameters
        ----------
        OpSimPointingRecord : Dictionary, mandatory
            Dictionary of values with keys corresponding to certain columns of
            the Summary table in the OpSim database. The minimal list of keys
            required for catsim to work is 'fiveSigmaDepth',
            'filtSkyBrightness', and at least one of ('finSeeing', 'FWHMeff').
            More keys defined in columnMap may be necessary for PhoSim to work.
        OpSimColumns : tuple of strings, optional, defaults to None
            The columns corresponding to the OpSim records. If None, attempts
            to obtain these from the OpSimRecord as OpSimRecord.dtype.names
        boundType : {'circle', 'box'}, optional, defaults to 'circle'
            Shape of the observation
        boundLength : scalar float, optional, defaults to 1.75
            'characteristic size' of observation field, in units of degrees.
            For boundType='circle', this is a radius, for boundType='box', this
            is a size of the box
        """

        pointing = OpSimPointingRecord
        pointing_column_names = pointing.dtype.names
        # Decide what is the name of the column in the OpSim database
        # corresponding to the Seeing. For older OpSim outputs, this is
        # 'finSeeing'. For later OpSim outputs this is 'FWHMeff'
        if OpSimColumns is None:
            OpSimColumns = pointing_column_names

        self._set_seeing_column(OpSimColumns)

        # get the names of the columns that contain the minimal schema
        # for an ObservationMetaData
        mjd_name = self.user_interface_to_opsim['expMJD'][0]
        ra_name = self.user_interface_to_opsim['fieldRA'][0]
        dec_name = self.user_interface_to_opsim['fieldDec'][0]
        filter_name = self.user_interface_to_opsim['telescopeFilter'][0]

        # check to see if angles are in degrees or radians
        if self.user_interface_to_opsim['fieldRA'][1] is None:
            in_degrees = True
        else:
            in_degrees = False

        # check to make sure the OpSim pointings being supplied contain
        # the minimum required information

        for required_column in (ra_name, dec_name, mjd_name, filter_name):
            if required_column not in OpSimColumns:
                raise RuntimeError(
                    "ObservationMetaDataGenerator requires that the database of "
                    "pointings include data for:\nfieldRA"
                    "\nfieldDec\nexpMJD\nfilter")

        # construct a raw dict of all of the OpSim columns associated with this pointing
        raw_dict = dict([(col, pointing[col])
                         for col in pointing_column_names])
        raw_dict['opsim_version'] = self.opsim_version

        if in_degrees:
            ra_val = pointing[ra_name]
            dec_val = pointing[dec_name]
        else:
            ra_val = np.degrees(pointing[ra_name])
            dec_val = np.degrees(pointing[dec_name])
        mjd_val = pointing[mjd_name]
        filter_val = pointing[filter_name]

        obs = ObservationMetaData(pointingRA=ra_val,
                                  pointingDec=dec_val,
                                  mjd=mjd_val,
                                  bandpassName=filter_val,
                                  boundType=boundType,
                                  boundLength=boundLength)

        m5_name = self.user_interface_to_opsim['m5'][0]
        rotSky_name = self.user_interface_to_opsim['rotSkyPos'][0]

        if m5_name in pointing_column_names:
            obs.m5 = pointing[m5_name]
        if 'filtSkyBrightness' in pointing_column_names:
            obs.skyBrightness = pointing['filtSkyBrightness']
        if self._seeing_column in pointing_column_names:
            obs.seeing = pointing[self._seeing_column]
        if rotSky_name in pointing_column_names:
            if in_degrees:
                obs.rotSkyPos = pointing[rotSky_name]
            else:
                obs.rotSkyPos = np.degrees(pointing[rotSky_name])

        obs.OpsimMetaData = raw_dict

        return obs
    def ObservationMetaDataFromPointing(self, OpSimPointingRecord, OpSimColumns=None,
                                        boundLength=1.75, boundType='circle'):
        """
        Return instance of ObservationMetaData for an OpSim Pointing record
        from OpSim.

        Parameters
        ----------
        OpSimPointingRecord : Dictionary, mandatory
            Dictionary of values with keys corresponding to certain columns of
            the Summary table in the OpSim database. The minimal list of keys
            required for catsim to work is 'fiveSigmaDepth',
            'filtSkyBrightness', and at least one of ('finSeeing', 'FWHMeff').
            More keys defined in columnMap may be necessary for PhoSim to work.
        OpSimColumns : tuple of strings, optional, defaults to None
            The columns corresponding to the OpSim records. If None, attempts
            to obtain these from the OpSimRecord as OpSimRecord.dtype.names
        boundType : {'circle', 'box'}, optional, defaults to 'circle'
            Shape of the observation
        boundLength : scalar float, optional, defaults to 1.75
            'characteristic size' of observation field, in units of degrees.
            For boundType='circle', this is a radius, for boundType='box', this
            is a size of the box
        """

        pointing = OpSimPointingRecord
        pointing_column_names = pointing.dtype.names
        # Decide what is the name of the column in the OpSim database
        # corresponding to the Seeing. For older OpSim outputs, this is
        # 'finSeeing'. For later OpSim outputs this is 'FWHMeff'
        if OpSimColumns is None:
            OpSimColumns = pointing_column_names

        self._set_seeing_column(OpSimColumns)

        # get the names of the columns that contain the minimal schema
        # for an ObservationMetaData
        mjd_name = self.user_interface_to_opsim['expMJD'][0]
        ra_name = self.user_interface_to_opsim['fieldRA'][0]
        dec_name = self.user_interface_to_opsim['fieldDec'][0]
        filter_name = self.user_interface_to_opsim['telescopeFilter'][0]

        # check to see if angles are in degrees or radians
        if self.user_interface_to_opsim['fieldRA'][1] is None:
            in_degrees = True
        else:
            in_degrees = False

        # check to make sure the OpSim pointings being supplied contain
        # the minimum required information

        for required_column in (ra_name, dec_name, mjd_name, filter_name):
            if required_column not in OpSimColumns:
                raise RuntimeError("ObservationMetaDataGenerator requires that the database of "
                                   "pointings include data for:\nfieldRA"
                                   "\nfieldDec\nexpMJD\nfilter")

        # construct a raw dict of all of the OpSim columns associated with this pointing
        raw_dict = dict([(col, pointing[col]) for col in pointing_column_names])
        raw_dict['opsim_version'] = self.opsim_version

        if in_degrees:
            ra_val = pointing[ra_name]
            dec_val = pointing[dec_name]
        else:
            ra_val = np.degrees(pointing[ra_name])
            dec_val = np.degrees(pointing[dec_name])
        mjd_val = pointing[mjd_name]
        filter_val = pointing[filter_name]

        obs = ObservationMetaData(pointingRA=ra_val,
                                  pointingDec=dec_val,
                                  mjd=mjd_val,
                                  bandpassName=filter_val,
                                  boundType=boundType,
                                  boundLength=boundLength)

        m5_name = self.user_interface_to_opsim['m5'][0]
        rotSky_name = self.user_interface_to_opsim['rotSkyPos'][0]

        if m5_name in pointing_column_names:
            obs.m5 = pointing[m5_name]
        if 'filtSkyBrightness' in pointing_column_names:
            obs.skyBrightness = pointing['filtSkyBrightness']
        if self._seeing_column in pointing_column_names:
            obs.seeing = pointing[self._seeing_column]
        if rotSky_name in pointing_column_names:
            if in_degrees:
                obs.rotSkyPos = pointing[rotSky_name]
            else:
                obs.rotSkyPos = np.degrees(pointing[rotSky_name])

        obs.OpsimMetaData = raw_dict

        return obs
Esempio n. 19
0
        mjd_good.append(mjd)
    if len(mjd_good) > 20:
        break

rot_sky_pos = rng.random_sample(len(mjd_good)) * 360.0
for i_mjd, mjd in enumerate(mjd_good):
    for i_filter in range(6):
        for all_same in (True, False):
            obs = ObservationMetaData(pointingRA=obs_root.pointingRA,
                                      pointingDec=obs_root.pointingDec,
                                      rotSkyPos=rot_sky_pos[i_mjd],
                                      mjd=mjd_good[i_mjd],
                                      site=obs_root.site,
                                      bandpassName='ugrizy'[i_filter])

            obs.OpsimMetaData = opsim_meta_data
            obs.OpsimMetaData['rawSeeing'] = rng.random_sample() * 0.15 + 0.6

            if all_same:
                out_name = os.path.join(
                    out_dir, 'instcat_%d_%d_same_sed.txt' % (i_mjd, i_filter))
            else:
                out_name = os.path.join(
                    out_dir, 'instcat_%d_%d_diff_sed.txt' % (i_mjd, i_filter))

            ref_name = os.path.join(out_dir, 'phosim_ref_cat.txt')

            if not ref_made:
                ref_handle = open(ref_name, 'w')

            if all_same:
Esempio n. 20
0
    def testFitsHeader(self):
        """
        Create a test image with the LSST camera and with the
        cartoon camera.  Verify that the image created with the LSST
        camera has the DM-required cards in its FITS header while the
        image created with the cartoon camera does not
        """

        cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'),
                                 'tests', 'cameraData')
        cartoonCamera = ReturnCamera(cameraDir)

        outputDir = tempfile.mkdtemp(dir=ROOT, prefix='testFitsHeader-')

        lsst_cat_name = os.path.join(outputDir, 'fits_test_lsst_cat.txt')
        lsst_cat_root = os.path.join(outputDir, 'fits_test_lsst_image')

        cartoon_cat_name = os.path.join(outputDir, 'fits_test_cartoon_cat.txt')
        cartoon_cat_root = os.path.join(outputDir, 'fits_test_cartoon_image')

        obs = ObservationMetaData(pointingRA=32.0,
                                  pointingDec=22.0,
                                  boundLength=0.1,
                                  boundType='circle',
                                  mjd=58000.0,
                                  rotSkyPos=14.0,
                                  bandpassName='u')

        obs.OpsimMetaData = {'obshistID': 112}

        dbFileName = os.path.join(outputDir, 'fits_test_db.dat')
        create_text_catalog(obs, dbFileName, np.array([30.0]),
                            np.array([30.0]), [22.0])
        db = fitsHeaderFileDBObj(dbFileName, runtable='test')

        # first test the lsst camera
        lsstCat = fitsHeaderCatalog(db, obs_metadata=obs)
        lsstCat.camera_wrapper = LSSTCameraWrapper()
        lsstCat.PSF = SNRdocumentPSF()
        lsstCat.write_catalog(lsst_cat_name)
        lsstCat.write_images(nameRoot=lsst_cat_root)

        list_of_files = os.listdir(outputDir)
        ct = 0
        for file_name in list_of_files:
            true_name = os.path.join(outputDir, file_name)
            if lsst_cat_root in true_name:
                ct += 1
                with fits.open(true_name) as fitsTest:
                    header = fitsTest[0].header
                    self.assertIn('CHIPID', header)
                    self.assertIn('OBSID', header)
                    self.assertIn('OUTFILE', header)
                    self.assertEqual(header['OBSID'], 112)
                    self.assertEqual(header['CHIPID'], 'R22_S11')
                    self.assertEqual(header['OUTFILE'],
                                     'lsst_e_112_f0_R22_S11_E000')
                os.unlink(true_name)

        self.assertGreater(ct, 0)
        if os.path.exists(lsst_cat_name):
            os.unlink(lsst_cat_name)

        # now test with the cartoon camera
        cartoonCat = fitsHeaderCatalog(db, obs_metadata=obs)
        cartoonCat.camera_wrapper = GalSimCameraWrapper(cartoonCamera)
        cartoonCat.PSF = SNRdocumentPSF()
        cartoonCat.write_catalog(cartoon_cat_name)
        cartoonCat.write_images(nameRoot=cartoon_cat_root)
        list_of_files = os.listdir(outputDir)
        ct = 0
        for file_name in list_of_files:
            true_name = os.path.join(outputDir, file_name)
            if cartoon_cat_root in true_name:
                ct += 1
                with fits.open(true_name) as fitsTest:
                    header = fitsTest[0].header
                    self.assertNotIn('CHIPID', header)
                    self.assertNotIn('OBSID', header)
                    self.assertNotIn('OUTFILE', header)
                os.unlink(true_name)

        self.assertGreater(ct, 0)
        if os.path.exists(cartoon_cat_name):
            os.unlink(cartoon_cat_name)

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

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