def testGetRotSkyPos(self):
        rotTelList = np.random.random_sample(len(self.raList))*2.0*np.pi
        mjd = 56321.8

        obsTemp = ObservationMetaData(mjd=mjd, site=Site(longitude=self.lon, latitude=self.lat, name='LSST'))

        rotSkyRad = utils._getRotSkyPos(self.raList, self.decList,
                                        obsTemp, rotTelList)

        rotSkyDeg = utils.getRotSkyPos(np.degrees(self.raList),
                                       np.degrees(self.decList),
                                       obsTemp, np.degrees(rotTelList))

        np.testing.assert_array_almost_equal(rotSkyRad, np.radians(rotSkyDeg), 10)

        rotSkyRad = utils._getRotSkyPos(self.raList, self.decList,
                                        obsTemp, rotTelList[0])

        rotSkyDeg = utils.getRotSkyPos(np.degrees(self.raList),
                                       np.degrees(self.decList),
                                       obsTemp, np.degrees(rotTelList[0]))

        np.testing.assert_array_almost_equal(rotSkyRad, np.radians(rotSkyDeg), 10)


        for ra, dec, rotTel in \
        zip(self.raList, self.decList, rotTelList):

            rotSkyRad = utils._getRotSkyPos(ra, dec, obsTemp, rotTel)

            rotSkyDeg = utils.getRotSkyPos(np.degrees(ra), np.degrees(dec),
                                           obsTemp, np.degrees(rotTel))

            self.assertAlmostEqual(rotSkyRad, np.radians(rotSkyDeg), 10)
def get_obs_md(obs_gen, obsHistID, fov=2, dither=True):
    """
    Get the ObservationMetaData object for the specified obsHistID.

    Parameters
    ----------
    obs_gen: lsst.sims.catUtils.utils.ObservationMetaDataGenerator
        Object that reads the opsim db file and generates obs_md objects.
    obsHistID: int
        The ID number of the desired visit.
    fov: float [2]
        Field-of-view angular radius in degrees.  2 degrees will cover
        the LSST focal plane.
    dither: bool [True]
        Flag to apply dithering in the opsim db file.

    Returns
    -------
    lsst.sims.utils.ObservationMetaData object
    """
    obs_md = obs_gen.getObservationMetaData(obsHistID=obsHistID,
                                            boundType='circle',
                                            boundLength=fov)[0]
    if dither:
        obs_md.pointingRA \
            = np.degrees(obs_md.OpsimMetaData['descDitheredRA'])
        obs_md.pointingDec \
            = np.degrees(obs_md.OpsimMetaData['descDitheredDec'])
        obs_md.OpsimMetaData['rotTelPos'] \
            = obs_md.OpsimMetaData['descDitheredRotTelPos']
        obs_md.rotSkyPos \
            = np.degrees(_getRotSkyPos(obs_md._pointingRA, obs_md._pointingDec,
                                       obs_md, obs_md.OpsimMetaData['rotTelPos']))
    return obs_md
Пример #3
0
    def testGetRotSkyPos(self):
        rotTelList = self.rng.random_sample(len(self.raList)) * 2.0 * np.pi
        mjd = 56321.8

        obsTemp = ObservationMetaData(mjd=mjd,
                                      site=Site(longitude=self.lon,
                                                latitude=self.lat,
                                                name='LSST'))

        rotSkyRad = utils._getRotSkyPos(self.raList, self.decList, obsTemp,
                                        rotTelList)

        rotSkyDeg = utils.getRotSkyPos(np.degrees(self.raList),
                                       np.degrees(self.decList), obsTemp,
                                       np.degrees(rotTelList))

        np.testing.assert_array_almost_equal(rotSkyRad, np.radians(rotSkyDeg),
                                             10)

        rotSkyRad = utils._getRotSkyPos(self.raList, self.decList, obsTemp,
                                        rotTelList[0])

        rotSkyDeg = utils.getRotSkyPos(np.degrees(self.raList),
                                       np.degrees(self.decList), obsTemp,
                                       np.degrees(rotTelList[0]))

        np.testing.assert_array_almost_equal(rotSkyRad, np.radians(rotSkyDeg),
                                             10)

        for ra, dec, rotTel in \
                zip(self.raList, self.decList, rotTelList):

            rotSkyRad = utils._getRotSkyPos(ra, dec, obsTemp, rotTel)

            rotSkyDeg = utils.getRotSkyPos(np.degrees(ra), np.degrees(dec),
                                           obsTemp, np.degrees(rotTel))

            self.assertAlmostEqual(rotSkyRad, np.radians(rotSkyDeg), 10)
    phosim_header_map = copy.deepcopy(DefaultPhoSimHeaderMap)
    phosim_header_map['nsnap'] = 1
    phosim_header_map['vistime'] = 30.0
    phosim_header_map['camconfig'] = 1
    for obshistid in obshistid_list:

        obs_list = obs_generator.getObservationMetaData(obsHistID=obshistid,
                                                        boundType='circle',
                                                        boundLength=args.fov)

        obs = obs_list[0]
        if dither_switch:
            print 'dithering'
            obs.pointingRA = np.degrees(obs.OpsimMetaData['randomDitherFieldPerVisitRA'])
            obs.pointingDec = np.degrees(obs.OpsimMetaData['randomDitherFieldPerVisitDec'])
            rotSky = _getRotSkyPos(obs._pointingRA, obs._pointingDec, obs,
                                   obs.OpsimMetaData['ditheredRotTelPos'])

            obs.rotSkyPos = np.degrees(rotSky)
            obs.OpsimMetaData['rotTelPos'] = obs.OpsimMetaData['ditheredRotTelPos']

        cat_name = os.path.join(out_dir,'phosim_cat_%d.txt' % obshistid)
        star_name = 'star_cat_%d.txt' % obshistid
        gal_name = 'gal_cat_%d.txt' % obshistid
        agn_name = 'agn_cat_%d.txt' % obshistid

        cat = PhoSimCatalogPoint(star_db, obs_metadata=obs)
        cat.phoSimHeaderMap = phosim_header_map
        with open(cat_name, 'w') as output:
            cat.write_header(output)
            output.write('includeobj %s.gz\n' % star_name)
            output.write('includeobj %s.gz\n' % gal_name)
Пример #5
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
Пример #6
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
Пример #7
0
    phosim_header_map['vistime'] = 30.0
    phosim_header_map['camconfig'] = 1
    for obshistid in obshistid_list:

        obs_list = obs_generator.getObservationMetaData(obsHistID=obshistid,
                                                        boundType='circle',
                                                        boundLength=args.fov)

        obs = obs_list[0]
        if dither_switch:
            print 'dithering'
            obs.pointingRA = np.degrees(
                obs.OpsimMetaData['randomDitherFieldPerVisitRA'])
            obs.pointingDec = np.degrees(
                obs.OpsimMetaData['randomDitherFieldPerVisitDec'])
            rotSky = _getRotSkyPos(obs._pointingRA, obs._pointingDec, obs,
                                   obs.OpsimMetaData['ditheredRotTelPos'])

            obs.rotSkyPos = np.degrees(rotSky)
            obs.OpsimMetaData['rotTelPos'] = obs.OpsimMetaData[
                'ditheredRotTelPos']

        cat_name = os.path.join(out_dir, 'phosim_cat_%d.txt' % obshistid)
        star_name = 'star_cat_%d.txt' % obshistid
        gal_name = 'gal_cat_%d.txt' % obshistid
        agn_name = 'agn_cat_%d.txt' % obshistid

        cat = PhoSimCatalogPoint(star_db, obs_metadata=obs)
        cat.phoSimHeaderMap = phosim_header_map
        with open(cat_name, 'w') as output:
            cat.write_header(output)
            output.write('includeobj %s.gz\n' % star_name)
def get_pointing_htmid(pointing_dir,
                       opsim_db_name,
                       ra_colname='descDitheredRA',
                       dec_colname='descDitheredDec',
                       rottel_colname='descDitheredRotTelPos'):
    """
    For a list of OpSim pointings, find dicts mapping those pointings to:
    - The trixels filling the pointings
    - The MJDs of the pointings
    - The telescope filters of the pointings

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

    opsim_db_name is the path to the OpSim database from which to take those pointings

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

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

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

    Returns
    -------
    htmid_bound_dict -- a dict keyed on ObsHistID.  Values are the list of trixels filling
    the OpSim pointing, as returned by lsst.sims.utils.HalfSpace.findAllTrixels

    mjd_dict -- a dict keyed on ObsHistID.  Values are the MJD(TAI) of the OpSim pointings.

    filter_dict -- a dict keyed on ObsHistID.  Values are the 'ugrizy' filter of the
    OpSim pointings.

    obsmd_dict -- a dict keyed on ObsHistID.  The values are ObservationMetaData with the
    RA, Dec, MJD, and rotSkyPos of the pointings (for use in focal plane geometry calculations)
    """

    radius = 2.0  # field of view of a pointing in degrees

    if not os.path.isfile(opsim_db_name):
        raise RuntimeError("%s is not a valid file name" % opsim_db_name)

    if not os.path.isdir(pointing_dir):
        raise RuntimeError("%s is not a valid dir name" % pointing_dir)

    dtype = np.dtype([('obshistid', int), ('mjd', float)])
    obs_data = None
    for file_name in os.listdir(pointing_dir):
        if 'visits' in file_name:
            full_name = os.path.join(pointing_dir, file_name)
            data = np.genfromtxt(full_name, dtype=dtype)
            if obs_data is None:
                obs_data = data['obshistid']
            else:
                obs_data = np.concatenate((obs_data, data['obshistid']),
                                          axis=0)

    obs_data = np.sort(obs_data)

    db = DBObject(opsim_db_name, driver='sqlite')
    dtype = np.dtype([('obshistid', int), ('mjd', float), ('band', str, 1),
                      ('ra', float), ('dec', float), ('rotTelPos', float)])

    htmid_bound_dict = {}
    mjd_dict = {}
    filter_dict = {}
    obsmd_dict = {}

    d_obs = len(obs_data) // 5
    for i_start in range(0, len(obs_data), d_obs):
        i_end = i_start + d_obs
        if len(obs_data) - i_start < d_obs:
            i_end = len(obs_data)

        subset = obs_data[i_start:i_end]

        query = 'SELECT obsHistId, expMJD, filter,'
        query += ' %s, %s, %s FROM Summary' % (ra_colname, dec_colname,
                                               rottel_colname)
        query += ' WHERE obsHistID BETWEEN %d and %e' % (subset.min(),
                                                         subset.max())
        query += ' GROUP BY obsHistID'

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

        for ii in range(len(results)):
            obshistid = results['obshistid'][ii]
            if obshistid not in obs_data:
                continue

            hs = halfSpaceFromRaDec(np.degrees(results['ra'][ii]),
                                    np.degrees(results['dec'][ii]), radius)

            trixel_bounds = hs.findAllTrixels(_truth_trixel_level)
            htmid_bound_dict[obshistid] = trixel_bounds
            mjd_dict[obshistid] = results['mjd'][ii]
            filter_dict[obshistid] = results['band'][ii]
            obs_md = ObservationMetaData(
                pointingRA=np.degrees(results['ra'][ii]),
                pointingDec=np.degrees(results['dec'][ii]),
                mjd=results['mjd'][ii])

            rotsky_rad = _getRotSkyPos(results['ra'][ii], results['dec'][ii],
                                       obs_md, results['rotTelPos'][ii])
            obsmd_dict[obshistid] = ObservationMetaData(
                pointingRA=np.degrees(results['ra'][ii]),
                pointingDec=np.degrees(results['dec'][ii]),
                mjd=results['mjd'][ii],
                rotSkyPos=np.degrees(rotsky_rad))
    assert len(obs_data) == len(htmid_bound_dict)

    return htmid_bound_dict, mjd_dict, filter_dict, obsmd_dict