Exemplo n.º 1
0
    def testDmagExceptions(self):
        """
        Test that the dmagTrailing and dmagDetection getters raise expected
        exceptions
        """
        catName = os.path.join(getPackageDir('sims_catUtils'), 'tests',
                               'scratchSpace', 'ssmDmagCatExceptions.txt')

        obs = ObservationMetaData()
        with self.assertRaises(RuntimeError) as context:
            cat = SSM_dmagCat(self.photDB, obs_metadata=obs)
        self.assertIn("does not specify seeing", context.exception.args[0])

        obs = ObservationMetaData(bandpassName=['u', 'g'], seeing=[0.6, 0.5])

        with self.assertRaises(RuntimeError) as context:
            cat = SSM_dmagCat(self.photDB, obs_metadata=obs)
        self.assertIn("multiple seeing values", context.exception.args[0])

        obs = ObservationMetaData(bandpassName='u', seeing=0.7)
        with self.assertRaises(RuntimeError) as context:
            cat = SSM_dmagCat(self.photDB, obs_metadata=obs)
            cat.photParams = None
            cat.write_catalog(catName)
        self.assertIn("does not have an associated PhotometricParameters",
                      context.exception.args[0])

        if os.path.exists(catName):
            os.unlink(catName)
Exemplo n.º 2
0
    def test_mlt_clean_up(self):
        """
        Test that the MLT cache is correctly loaded after sims_clean_up is
        called.
        """
        db = MLT_test_DB(database=self.db_name, driver='sqlite')
        obs = ObservationMetaData(mjd=60000.0)
        cat = FlaringCatalog(db, obs_metadata=obs)
        cat._mlt_lc_file = self.mlt_lc_name
        cat_name_1 = os.path.join(self.scratch_dir,'mlt_clean_test_cat_1.txt')
        cat.write_catalog(cat_name_1)
        sims_clean_up()

        # re-generate the same catalog and verify that its
        # contents are unchanged
        db = MLT_test_DB(database=self.db_name, driver='sqlite')
        obs = ObservationMetaData(mjd=60000.0)
        cat = FlaringCatalog(db, obs_metadata=obs)
        cat._mlt_lc_file = self.mlt_lc_name
        cat_name_2 = os.path.join(self.scratch_dir,'mlt_clean_test_cat_2.txt')
        cat.write_catalog(cat_name_2)
        with open(cat_name_1, 'r') as in_file_1:
            lines_1 = in_file_1.readlines()
        with open(cat_name_2, 'r') as in_file_2:
            lines_2 = in_file_2.readlines()
        self.assertGreater(len(lines_1), 1)
        self.assertEqual(len(lines_1), len(lines_2))
        for line in lines_1:
            self.assertIn(line, lines_2)

        if os.path.exists(cat_name_1):
            os.unlink(cat_name_1)
        if os.path.exists(cat_name_2):
            os.unlink(cat_name_2)
Exemplo n.º 3
0
    def setUp(self):
        self.driver = 'sqlite'
        self.StarDBName = os.path.join(self.scratch_dir,
                                       'testSetup_setupTestStars.db')

        self.GalaxyDBName = os.path.join(self.scratch_dir,
                                         'testSetup_setupTestGalaxies.db')

        self.pointingRA = 50.0
        self.pointingDec = -5.0
        self.radius = 1.0
        makeStarDatabase(filename=self.StarDBName, size=100,
                         pointingRA=self.pointingRA,
                         pointingDec=self.pointingDec,
                         radius=self.radius)

        makeGalaxyDatabase(filename=self.GalaxyDBName, size=100,
                           pointingRA=self.pointingRA,
                           pointingDec=self.pointingDec,
                           radius=self.radius)

        self.starDBObj = testStarDBObject(driver=self.driver, database= self.StarDBName)
        self.galaxyDBObj = testGalaxyDBObject(driver=self.driver, database=self.GalaxyDBName)

        self.obs_metadata = ObservationMetaData(pointingRA=self.pointingRA,
                                                pointingDec=self.pointingDec,
                                                boundType='circle', boundLength=self.radius,
                                                bandpassName='g', mjd=57000.0,
                                                m5=24.5)

        self.obs_metadata_compound = ObservationMetaData(pointingRA=self.pointingRA,
                                                         pointingDec=self.pointingDec,
                                                         boundType='circle', boundLength=self.radius,
                                                         bandpassName=['g', 'i'], mjd=57000.0,
                                                         m5=[24.5, 17.5])
Exemplo n.º 4
0
    def testNativeLonLat(self):
        """
        Test that nativeLonLatFromRaDec works by considering stars and pointings
        at intuitive locations
        """

        mjd = 53855.0

        raList_obs = [0.0, 0.0, 0.0, 270.0]
        decList_obs = [90.0, 90.0, 0.0, 0.0]

        raPointList_obs = [0.0, 270.0, 270.0, 0.0]
        decPointList_obs = [0.0, 0.0, 0.0, 0.0]

        lonControlList = [180.0, 180.0, 90.0, 270.0]
        latControlList = [0.0, 0.0, 0.0, 0.0]

        for rr_obs, dd_obs, rp_obs, dp_obs, lonc, latc in \
                zip(raList_obs, decList_obs, raPointList_obs, decPointList_obs,
                    lonControlList, latControlList):

            obsTemp = ObservationMetaData(mjd=mjd)

            rr, dd = icrsFromObserved(np.array([rr_obs, rp_obs]),
                                      np.array([dd_obs, dp_obs]),
                                      obs_metadata=obsTemp,
                                      epoch=2000.0,
                                      includeRefraction=True)

            obs = ObservationMetaData(pointingRA=rr[1],
                                      pointingDec=dd[1],
                                      mjd=mjd)
            lon, lat = nativeLonLatFromRaDec(rr[0], dd[0], obs)
            distance = arcsecFromRadians(haversine(lon, lat, lonc, latc))
            self.assertLess(distance, 1.0)
Exemplo n.º 5
0
    def testSeeing(self):
        """
        Test behavior of ObservationMetaData's seeing member variable
        """

        self.assertRaises(RuntimeError,
                          ObservationMetaData,
                          bandpassName='u',
                          seeing=[0.7, 0.6])
        self.assertRaises(RuntimeError,
                          ObservationMetaData,
                          bandpassName=['u', 'g'],
                          seeing=0.7)
        self.assertRaises(RuntimeError,
                          ObservationMetaData,
                          bandpassName=['u', 'g'],
                          seeing=[0.8, 0.7, 0.6])

        obsMD = ObservationMetaData()
        self.assertIsNone(obsMD.seeing)

        obsMD = ObservationMetaData(bandpassName='g', seeing=0.7)
        self.assertAlmostEqual(obsMD.seeing['g'], 0.7, 10)

        obsMD = ObservationMetaData(bandpassName=['u', 'g', 'r'],
                                    seeing=[0.7, 0.6, 0.5])
        self.assertEqual(obsMD.seeing['u'], 0.7)
        self.assertEqual(obsMD.seeing['g'], 0.6)
        self.assertEqual(obsMD.seeing['r'], 0.5)
Exemplo n.º 6
0
    def ssoInCameraFov(self, ephems, obsData):
        """Determine which observations are within the actual camera footprint for a series of observations.
        Note that ephems and obsData must be the same length.

        Parameters
        ----------
        ephems : np.recarray
            Ephemerides for the objects.
        obsData : np.recarray
            Observation pointings.

        Returns
        -------
        np.ndarray
            Returns the indexes of the numpy array of the object observations which are inside the fov.
        """
        if not hasattr(self, 'camera'):
            self._setupCamera()
        epoch = 2000.0
        # See if the object is within 'rFov' of the center of the boresight.
        idxObsRough = self._ssoInCircleFov(ephems, obsData, rFov=2.1)
        # Then test for the camera footprint exactly.
        idxObs = []
        for idx in idxObsRough:
            mjd_date = obsData[idx][self.obsTimeCol]
            if self.obsTimeScale == 'TAI':
                mjd = ModifiedJulianDate(TAI=mjd_date)
            elif self.obsTimeScale == 'UTC':
                mjd = ModifiedJulianDate(UTC=mjd_date)
            else:
                warnings.warn(
                    'Expected timescale of TAI or UTC, but did not match. Using TAI.'
                )
                mjd = ModifiedJulianDate(TAI=mjd_date)
            if not self.obsDegrees:
                obs_metadata = ObservationMetaData(
                    pointingRA=np.degrees(obsData[idx][self.obsRA]),
                    pointingDec=np.degrees(obsData[idx][self.obsDec]),
                    rotSkyPos=np.degrees(obsData[idx][self.obsRotSkyPos]),
                    mjd=mjd)
            else:
                obs_metadata = ObservationMetaData(
                    pointingRA=obsData[idx][self.obsRA],
                    pointingDec=obsData[idx][self.obsDec],
                    rotSkyPos=obsData[idx][self.obsRotSkyPos],
                    mjd=mjd)
            # Catch the warnings from astropy about the time being in the future.
            with warnings.catch_warnings(record=False):
                warnings.simplefilter('ignore')
                chipName = chipNameFromRaDec(ra=ephems['ra'][idx],
                                             dec=ephems['dec'][idx],
                                             epoch=epoch,
                                             obs_metadata=obs_metadata,
                                             camera=self.camera)
            if chipName is not None:
                tt = self.ccd_type_dict[self.camera[chipName].getType()]
                if tt == 'science':
                    idxObs.append(idx)
        idxObs = np.array(idxObs, int)
        return idxObs
Exemplo n.º 7
0
    def testBounds(self):
        """
        Test if ObservationMetaData correctly assigns the pointing[RA,Dec]
        when circle and box bounds are specified
        """

        circRA = 25.0
        circDec = 50.0
        radius = 5.0

        boxRA = 15.0
        boxDec = 0.0
        boxLength = np.array([5.0, 10.0])

        testObsMD = ObservationMetaData(boundType='circle',
                                        pointingRA=circRA,
                                        pointingDec=circDec,
                                        boundLength=radius,
                                        mjd=53580.0)
        self.assertAlmostEqual(testObsMD.pointingRA, 25.0, 10)
        self.assertAlmostEqual(testObsMD.pointingDec, 50.0, 10)

        testObsMD = ObservationMetaData(boundType='box',
                                        pointingRA=boxRA,
                                        pointingDec=boxDec,
                                        boundLength=boxLength,
                                        mjd=53580.0)
        self.assertAlmostEqual(testObsMD.pointingRA, 15.0, 10)
        self.assertAlmostEqual(testObsMD.pointingDec, 0.0, 10)
Exemplo n.º 8
0
    def testBoundBuilding(self):
        """
        Make sure ObservationMetaData can build bounds
        """
        boxBounds = [0.1, 0.3]
        circObs = ObservationMetaData(boundType='circle',
                                      pointingRA=0.0,
                                      pointingDec=0.0,
                                      boundLength=1.0,
                                      mjd=53580.0)
        boundControl = CircleBounds(0.0, 0.0, np.radians(1.0))
        self.assertEqual(circObs.bounds, boundControl)

        squareObs = ObservationMetaData(boundType='box',
                                        pointingRA=0.0,
                                        pointingDec=0.0,
                                        boundLength=1.0,
                                        mjd=53580.0)
        boundControl = BoxBounds(0.0, 0.0, np.radians(1.0))
        self.assertEqual(squareObs.bounds, boundControl)

        boxObs = ObservationMetaData(boundType='box',
                                     pointingRA=0.0,
                                     pointingDec=0.0,
                                     boundLength=boxBounds,
                                     mjd=53580.0)
        boundControl = BoxBounds(0.0, 0.0, np.radians([0.1, 0.3]))
        self.assertEqual(boxObs.bounds, boundControl)
Exemplo n.º 9
0
    def testM5(self):
        """
        Test behavior of ObservationMetaData's m5 member variable
        """

        self.assertRaises(RuntimeError,
                          ObservationMetaData,
                          bandpassName='u',
                          m5=[12.0, 13.0])
        self.assertRaises(RuntimeError,
                          ObservationMetaData,
                          bandpassName=['u', 'g'],
                          m5=15.0)
        self.assertRaises(RuntimeError,
                          ObservationMetaData,
                          bandpassName=['u', 'g'],
                          m5=[12.0, 13.0, 15.0])

        obsMD = ObservationMetaData()
        self.assertIsNone(obsMD.m5)

        obsMD = ObservationMetaData(bandpassName='g', m5=12.0)
        self.assertAlmostEqual(obsMD.m5['g'], 12.0, 10)

        obsMD = ObservationMetaData(bandpassName=['u', 'g', 'r'],
                                    m5=[10, 11, 12])
        self.assertEqual(obsMD.m5['u'], 10)
        self.assertEqual(obsMD.m5['g'], 11)
        self.assertEqual(obsMD.m5['r'], 12)
Exemplo n.º 10
0
    def setUp(self):

        self.RAmin = 190.
        self.RAmax = 210.
        self.DECmin = -70.
        self.DECmax = -50.

        self.RAcenter = 200.
        self.DECcenter = -60.
        self.radius = 10.0

        self.obsMdCirc = ObservationMetaData(boundType='circle',
                                             pointingRA=self.RAcenter,
                                             pointingDec=self.DECcenter,
                                             boundLength=self.radius,
                                             mjd=52000.,
                                             bandpassName='r')

        self.obsMdBox = ObservationMetaData(
            boundType='box',
            pointingRA=0.5 * (self.RAmax + self.RAmin),
            pointingDec=0.5 * (self.DECmin + self.DECmax),
            boundLength=np.array([
                0.5 * (self.RAmax - self.RAmin),
                0.5 * (self.DECmax - self.DECmin)
            ]),
            mjd=52000.,
            bandpassName='r')
Exemplo n.º 11
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
Exemplo n.º 12
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
Exemplo n.º 13
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)
    def testExceptions(self):
        """
        Test that errors are produced whenever ObservationMetaData
        parameters are overwritten in an unintentional way
        """

        metadata = {'pointingRA':[1.5], 'pointingDec':[0.5],
                    'Opsim_expmjd':[52000.0],
                    'Opsim_rotskypos':[1.3],
                    'Opsim_filter':[2],
                    'Opsim_rawseeing':[0.7]}

        obs_metadata = ObservationMetaData(phoSimMetaData=metadata,
                                           boundType='circle',
                                           boundLength=0.1)

        with self.assertRaises(RuntimeError):
            obs_metadata.pointingRA=1.2

        with self.assertRaises(RuntimeError):
            obs_metadata.pointingDec=1.2

        with self.assertRaises(RuntimeError):
            obs_metadata.rotSkyPos=1.5

        with self.assertRaises(RuntimeError):
            obs_metadata.seeing=0.5

        with self.assertRaises(RuntimeError):
            obs_metadata.setBandpassM5andSeeing()

        obs_metadata = ObservationMetaData(pointingRA=1.5,
                                           pointingDec=1.5)
Exemplo n.º 15
0
    def testDegreesVersusRadians(self):
        """
        Test that the radian and degree versions of nativeLonLatFromRaDec
        and raDecFromNativeLonLat are consistent with each other
        """

        rng = np.random.RandomState(873)
        nSamples = 1000
        obs = ObservationMetaData(pointingRA=45.0,
                                  pointingDec=-34.5,
                                  mjd=54656.76)
        raList = rng.random_sample(nSamples) * 360.0
        decList = rng.random_sample(nSamples) * 180.0 - 90.0

        lonDeg, latDeg = nativeLonLatFromRaDec(raList, decList, obs)
        lonRad, latRad = _nativeLonLatFromRaDec(np.radians(raList),
                                                np.radians(decList), obs)
        np.testing.assert_array_almost_equal(np.radians(lonDeg), lonRad, 15)
        np.testing.assert_array_almost_equal(np.radians(latDeg), latRad, 15)

        raDeg, decDeg = raDecFromNativeLonLat(raList, decList, obs)
        raRad, decRad = _raDecFromNativeLonLat(np.radians(raList),
                                               np.radians(decList), obs)
        np.testing.assert_array_almost_equal(np.radians(raDeg), raRad, 15)
        np.testing.assert_array_almost_equal(np.radians(decDeg), decRad, 15)
Exemplo n.º 16
0
    def testNativeLonLatVector(self):
        """
        Test that nativeLonLatFromRaDec works in a vectorized way; we do this
        by performing a bunch of tansformations passing in ra and dec as numpy arrays
        and then comparing them to results computed in an element-wise way
        """

        obs = ObservationMetaData(pointingRA=123.0,
                                  pointingDec=43.0,
                                  mjd=53467.2)

        nSamples = 100
        rng = np.random.RandomState(42)
        raList = rng.random_sample(nSamples) * 360.0
        decList = rng.random_sample(nSamples) * 180.0 - 90.0

        lonList, latList = nativeLonLatFromRaDec(raList, decList, obs)

        for rr, dd, lon, lat in zip(raList, decList, lonList, latList):
            lonControl, latControl = nativeLonLatFromRaDec(rr, dd, obs)
            distance = arcsecFromRadians(
                haversine(np.radians(lon), np.radians(lat),
                          np.radians(lonControl), np.radians(latControl)))

            self.assertLess(distance, 0.0001)
Exemplo n.º 17
0
    def testPAStacker(self):
        """ Test the parallacticAngleStacker"""
        data = np.zeros(100,
                        dtype=list(
                            zip([
                                'observationStartMJD', 'fieldDec', 'fieldRA',
                                'observationStartLST'
                            ], [float] * 4)))
        data['observationStartMJD'] = np.arange(100) * .2 + 50000
        site = Site(name='LSST')
        data['observationStartLST'], last = calcLmstLast(
            data['observationStartMJD'], site.longitude_rad)
        data['observationStartLST'] = data['observationStartLST'] * 180. / 12.
        stacker = stackers.ParallacticAngleStacker(degrees=True)
        data = stacker.run(data)
        # Check values are in good range
        assert (data['PA'].max() <= 180)
        assert (data['PA'].min() >= -180)

        # Check compared to the util
        check_pa = []
        ras = np.radians(data['fieldRA'])
        decs = np.radians(data['fieldDec'])
        for ra, dec, mjd in zip(ras, decs, data['observationStartMJD']):
            alt, az, pa = _altAzPaFromRaDec(
                ra, dec, ObservationMetaData(mjd=mjd, site=site))

            check_pa.append(pa)
        check_pa = np.degrees(check_pa)
        np.testing.assert_array_almost_equal(data['PA'], check_pa, decimal=0)
Exemplo n.º 18
0
    def setUpClass(cls):

        cls.obs = ObservationMetaData(
            bandpassName=['u', 'g', 'r', 'i', 'z', 'y'],
            m5=[22.0, 23.0, 24.0, 25.0, 26.0, 27.0])

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

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

        inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests',
                                'testData')
        inputFile = os.path.join(inputDir, 'IndicesTestCatalogStars.txt')

        cls.db = fileDBObject(inputFile,
                              runtable='test',
                              idColKey='id',
                              dtype=dbdtype)

        cat = baselineStarCatalog(cls.db, obs_metadata=cls.obs)
        cls.catName = tempfile.mktemp(dir=ROOT,
                                      prefix='indicesStarControlCat-',
                                      suffix='.txt')
        cat.write_catalog(cls.catName)
        cls.controlData = np.genfromtxt(cls.catName,
                                        dtype=baselineDtype,
                                        delimiter=',')
        os.unlink(cls.catName)
Exemplo n.º 19
0
    def setUpClass(cls):

        cls.obs = ObservationMetaData(
            bandpassName=['u', 'g', 'r', 'i', 'z', 'y'],
            m5=[24.0, 25.0, 26.0, 27.0, 28.0, 29.0])

        dtype = np.dtype([('id', np.int), ('sedFilenameBulge', str, 100),
                          ('magNormBulge', np.float),
                          ('sedFilenameDisk', str, 100),
                          ('magNormDisk', np.float),
                          ('sedFilenameAgn', str, 100),
                          ('magNormAgn', np.float),
                          ('internalAvBulge', np.float),
                          ('internalAvDisk', np.float),
                          ('galacticAv', np.float), ('redshift', np.float)])

        inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests',
                                'testData')
        inputFile = os.path.join(inputDir, 'IndicesTestCatalogGalaxies.txt')
        cls.db = fileDBObject(inputFile,
                              dtype=dtype,
                              runtable='test',
                              idColKey='id')

        cls.db.objectTypeId = 44

        cat = baselineGalaxyCatalog(cls.db, obs_metadata=cls.obs)
        dtype = np.dtype([(name, np.float) for name in cat.column_outputs])
        catName = tempfile.mktemp(dir=ROOT, prefix='', suffix='.txt')
        cat.write_catalog(catName)
        cls.controlData = np.genfromtxt(catName, dtype=dtype, delimiter=',')
        os.remove(catName)
Exemplo n.º 20
0
    def setUpClass(cls):

        cls.obs = ObservationMetaData(
            bandpassName=['u', 'g', 'r', 'i', 'z', 'y'],
            m5=[22.0, 23.0, 24.0, 25.0, 26.0, 27.0])

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

        dbdtype = np.dtype([('id', np.int), ('sedFilename', str, 100),
                            ('magNorm', np.float), ('velRA', np.float),
                            ('velDec', np.float)])

        inputDir = os.path.join(getPackageDir('sims_catUtils'), 'tests',
                                'testData')
        inputFile = os.path.join(inputDir, 'SSMphotometryCatalog.txt')

        cls.db = fileDBObject(inputFile,
                              runtable='test',
                              idColKey='id',
                              dtype=dbdtype)

        cat = baselineSSMCatalog(cls.db, obs_metadata=cls.obs)
        catName = tempfile.mktemp(prefix='IndexTestCase_setUpClass')
        cat.write_catalog(catName)
        cls.controlData = np.genfromtxt(catName,
                                        dtype=baselineDtype,
                                        delimiter=',')
        os.unlink(catName)
Exemplo n.º 21
0
class LensGalaxyObj(BaseCatalogObj):
    objid = 'lensgalaxy'
    tableid = 'lens'
    idColKey = 'galid'
    raColName = 'ra'
    decColName = 'dec'
    objectTypeId = 30
    doRunTest = True
    #: all sky since this is a small set.
    testObservationMetaData = ObservationMetaData(mjd=53000., bandpassName='r', m5=22.0)

    dbDefaultValues = {'varsimobjid': -1, 'myid': -1, 'variabilityParameters': None}
    #: The following maps column names to database schema.  The tuples
    #: must be at least length 2.  If column name is the same as the name
    #: in the DB the mapping element may be None.  The rest of the tuple
    #: should be formatted like a numpy.dtype.  If ommitted, the dtype
    #: is assumed to be float.
    columns = [('galid', 'id', int),
               ('raJ2000', 'ra_bulge*PI()/180.'),
               ('decJ2000', 'dec_bulge*PI()/180.'),
               ('magNorm', 'magnorm_bulge'),
               ('sedFilename', 'sedname_bulge', str, 40),
               ('lsst_u', 'u_ab'),
               ('lsst_g', 'g_ab'),
               ('lsst_r', 'r_ab'),
               ('lsst_i', 'i_ab'),
               ('lsst_z', 'z_ab'),
               ('lsst_y', 'y_ab')]
Exemplo n.º 22
0
    def testNonsenseArbitraryConstraints(self):
        """
        Test a query with a user-specified constraint on the magnitude column
        """

        myNonsense = CatalogDBObject.from_objid('Nonsense')

        raMin = 50.0
        raMax = 150.0
        decMax = 30.0
        decMin = -20.0
        raCenter=0.5*(raMin+raMax)
        decCenter=0.5*(decMin+decMax)

        mycolumns = ['NonsenseId', 'NonsenseRaJ2000', 'NonsenseDecJ2000', 'NonsenseMag']

        boxObsMd = ObservationMetaData(boundType='box', pointingRA=raCenter, pointingDec=decCenter,
                    boundLength=numpy.array([0.5*(raMax-raMin), 0.5*(decMax-decMin)]), mjd=52000., bandpassName='r')

        boxQuery = myNonsense.query_columns(colnames = mycolumns,
                      obs_metadata=boxObsMd, chunk_size=100, constraint = 'mag > 11.0')

        raMin = numpy.radians(raMin)
        raMax = numpy.radians(raMax)
        decMin = numpy.radians(decMin)
        decMax = numpy.radians(decMax)

        goodPoints = []

        ct = 0
        for chunk in boxQuery:
            for row in chunk:
                ct += 1

                self.assertLess(row[1], raMax)
                self.assertGreater(row[1], raMin)
                self.assertLess(row[2], decMax)
                self.assertGreater(row[2], decMin)
                self.assertGreater(row[3], 11.0)

                dex = numpy.where(self.baselineData['id'] == row[0])[0][0]

                #keep a list of the points returned by the query
                goodPoints.append(row[0])

                self.assertAlmostEqual(numpy.radians(self.baselineData['ra'][dex]), row[1], 3)
                self.assertAlmostEqual(numpy.radians(self.baselineData['dec'][dex]), row[2], 3)
                self.assertAlmostEqual(self.baselineData['mag'][dex], row[3], 3)

        self.assertGreater(ct, 0)

        ct = 0
        for entry in [xx for xx in self.baselineData if xx[0] not in goodPoints]:
            #make sure that the points not returned by the query did, in fact, violate one of the
            #constraints of the query (either the box bound or the magnitude cut off)
            switch = (entry[1] > raMax or entry[1] < raMin or entry[2] >decMax or entry[2] < decMin or entry[3]<11.0)

            self.assertTrue(switch)
            ct += 1
        self.assertGreater(ct, 0)
    def test_disk_phosim_catalog(self):
        """
        Just try producing a PhoSim InstanceCatalog from a fake
        ObservationMetaData, using protoDC2 (to make sure we don't
        break the whole interface)
        """
        db = diskDESCQAObject_protoDC2(yaml_file_name='protoDC2')
        db.field_ra = self.field_ra
        db.field_dec = self.field_dec
        obs = ObservationMetaData(pointingRA=self.field_ra + 0.2,
                                  pointingDec=self.field_dec - 0.2,
                                  mjd=59580.0,
                                  rotSkyPos=112.0,
                                  bandpassName='z',
                                  boundType='circle',
                                  boundLength=0.01)

        cat = PhoSimDESCQA(db, obs_metadata=obs, cannot_be_null=['hasDisk'])
        cat.phoSimHeaderMap = {}
        cat_name = os.path.join(self.out_dir, 'disk_phosim_cat.txt')
        self.assertTrue(os.path.exists(self.out_dir))
        cat.write_catalog(cat_name)
        with open(cat_name, 'r') as in_file:
            cat_lines = in_file.readlines()

        self.assertGreater(len(cat_lines), 50)

        if os.path.exists(cat_name):
            os.unlink(cat_name)
Exemplo n.º 24
0
    def test_m5_exceptions(self):
        """
        Test that the correct exception is raised when you ask for a photometric
        uncertainty but do not define the required m5 value
        """

        catName = os.path.join(getPackageDir('sims_catUtils'), 'tests',
                               'scratchSpace', 'm5_exception_cat.txt')

        obs = ObservationMetaData(pointingRA=25.0,
                                  pointingDec=-14.0,
                                  boundType='circle',
                                  boundLength=0.1,
                                  bandpassName=['u', 'g', 'r', 'z', 'y'],
                                  m5=[24.0] * 5,
                                  mjd=57388.0)

        with self.assertRaises(KeyError) as context:
            cat = testStars(self.star, obs_metadata=obs)
            cat.write_catalog(catName)

        self.assertIn(
            'Is it possible your ObservationMetaData does not have the proper\nm5 values defined?',
            context.exception.args[0])

        with self.assertRaises(KeyError) as context:
            cat = testGalaxies(self.galaxy, obs_metadata=obs)
            cat.write_catalog(catName)

        self.assertIn(
            'Is it possible your ObservationMetaData does not have the proper\nm5 values defined?',
            context.exception.args[0])

        if os.path.exists(catName):
            os.unlink(catName)
Exemplo n.º 25
0
 def ssoInFov(self, interpfuncs, simdata, rFov=np.radians(1.75),
              useCamera=True,
              simdataRaCol = 'fieldRA', simdataDecCol='fieldDec'):
     """
     Return the indexes of the simdata observations where the object was inside the fov.
     """
     # See if the object is within 'rFov' of the center of the boresight.
     raSso = np.radians(interpfuncs['ra'](simdata['expMJD']))
     decSso = np.radians(interpfuncs['dec'](simdata['expMJD']))
     sep = haversine(raSso, decSso, simdata[simdataRaCol], simdata[simdataDecCol])
     if not useCamera:
         idxObsRough = np.where(sep<rFov)[0]
         return idxObsRough
     # Or go on and use the camera footprint.
     try:
         self.camera
     except AttributeError:
         self._setupCamera()
     idxObs = []
     idxObsRough = np.where(sep<self.cameraFov)[0]
     for idx in idxObsRough:
         mjd = simdata[idx]['expMJD']
         obs_metadata = ObservationMetaData(unrefractedRA=np.degrees(simdata[idx][simdataRaCol]),
                                            unrefractedDec=np.degrees(simdata[idx][simdataDecCol]),
                                            rotSkyPos=np.degrees(simdata[idx]['rotSkyPos']),
                                            mjd=simdata[idx]['expMJD'])
         raObj = np.radians(np.array([interpfuncs['ra'](simdata[idx]['expMJD'])]))
         decObj = np.radians(np.array([interpfuncs['dec'](simdata[idx]['expMJD'])]))
         raObj, decObj = observedFromICRS(raObj, decObj, obs_metadata=obs_metadata, epoch=self.epoch)
         chipNames = findChipName(ra=raObj,dec=decObj, epoch=self.epoch, camera=self.camera, obs_metadata=obs_metadata)
         if chipNames != [None]:
             idxObs.append(idx)
     idxObs = np.array(idxObs)
     return idxObs
Exemplo n.º 26
0
    def test_m5_exceptions(self):
        """
        Test that the correct exception is raised when you ask for a photometric
        uncertainty but do not define the required m5 value
        """
        obs = ObservationMetaData(pointingRA=25.0,
                                  pointingDec=-14.0,
                                  boundType='circle',
                                  boundLength=0.1,
                                  bandpassName=['u', 'g', 'r', 'z', 'y'],
                                  m5=[24.0] * 5,
                                  mjd=57388.0)

        with self.assertRaises(KeyError) as context:
            cat = testStars(self.star, obs_metadata=obs)
            with lsst.utils.tests.getTempFilePath('.txt') as catName:
                cat.write_catalog(catName)

        self.assertIn(
            'Is it possible your ObservationMetaData does not have the proper\nm5 values defined?',
            context.exception.args[0])

        with self.assertRaises(KeyError) as context:
            cat = testGalaxies(self.galaxy, obs_metadata=obs)
            with lsst.utils.tests.getTempFilePath('.txt') as catName:
                cat.write_catalog(catName)

        self.assertIn(
            'Is it possible your ObservationMetaData does not have the proper\nm5 values defined?',
            context.exception.args[0])
    def testObsMetaData(self):
        """
        Test that CompoundCatalogDBObject can handle ObservationMetaData
        properly
        """

        obs = ObservationMetaData(pointingRA = 180.0,
                                  pointingDec = 0.0,
                                  boundType = 'box',
                                  boundLength = (80.0, 25.0),
                                  mjd=53580.0)


        class testDbClass22(testStarDB1):
            database = self.dbName
            driver = 'sqlite'

        class testDbClass23(testStarDB2):
            database = self.dbName
            driver = 'sqlite'

        db1 = testDbClass22
        db2 = testDbClass23

        compoundDb = CompoundCatalogDBObject([db1, db2])

        colnames = ['%s_id' % db1.objid,
                    '%s_raJ2000' % db1.objid, '%s_decJ2000' % db1.objid,
                    '%s_magMod' % db1.objid,
                    '%s_raJ2000' % db2.objid, '%s_decJ2000' % db2.objid,
                    '%s_magMod' % db2.objid]

        results = compoundDb.query_columns(colnames=colnames,
                                           obs_metadata=obs)

        good_rows = []
        for chunk in results:
            for line in chunk:
                ix = line['id']
                good_rows.append(ix)
                self.assertAlmostEqual(line['%s_raJ2000' % db1.objid], self.controlArray['ra'][ix], 10)
                self.assertAlmostEqual(line['%s_decJ2000' % db1.objid], self.controlArray['dec'][ix], 10)
                self.assertAlmostEqual(line['%s_magMod' % db1.objid], self.controlArray['mag'][ix], 10)
                self.assertAlmostEqual(line['%s_raJ2000' % db2.objid], 2.0*self.controlArray['ra'][ix], 10)
                self.assertAlmostEqual(line['%s_decJ2000' % db2.objid], 2.0*self.controlArray['dec'][ix], 10)
                self.assertAlmostEqual(line['%s_magMod' % db2.objid], 2.0*self.controlArray['mag'][ix], 10)
                self.assertTrue(self.controlArray['ra'][ix]>100.0)
                self.assertTrue(self.controlArray['ra'][ix]<260.0)
                self.assertTrue(self.controlArray['dec'][ix]>-25.0)
                self.assertTrue(self.controlArray['dec'][ix]<25.0)

        bad_rows = [ix for ix in range(self.controlArray.shape[0]) if ix not in good_rows]

        in_bounds = [rr>100.0 and rr<260.0 and dd>-25.0 and dd<25.0 \
                      for (rr, dd) in \
                      zip(self.controlArray['ra'][bad_rows], self.controlArray['dec'][bad_rows])]

        self.assertFalse(True in in_bounds)
        self.assertTrue(len(good_rows)>0)
        self.assertTrue(len(bad_rows)>0)
Exemplo n.º 28
0
    def test_outside_radius(self):
        """
        Test that methods can gracefully handle points
        outside of the focal plane
        """
        rng = np.random.RandomState(7123)
        ra = 145.0
        dec = -25.0
        obs = ObservationMetaData(pointingRA=ra,
                                  pointingDec=dec,
                                  mjd=59580.0,
                                  rotSkyPos=113.0)

        rr = rng.random_sample(100) * 5.0
        self.assertGreater(rr.max(), 4.5)
        theta = rng.random_sample(100) * 2.0 * np.pi
        ra_vec = ra + rr * np.cos(theta)
        dec_vec = dec + rr * np.sin(theta)
        chip_name_list = chipNameFromRaDecLSST(ra_vec,
                                               dec_vec,
                                               obs_metadata=obs,
                                               band='u')

        rr = angularSeparation(ra, dec, ra_vec, dec_vec)

        ct_none = 0
        for rr, name in zip(rr, chip_name_list):
            if rr > 2.0:
                self.assertIsNone(name)
                ct_none += 1
        self.assertGreater(ct_none, 0)
 def set_data(self, seed):
     """
     Accept a seed integer.  Return an ObservationMetaData
     and numpy arrays of RA, Dec (in degrees),
     pm_ra, pm_dec, parallax (in arcsec) and v_rad (in km/s)
     centered on that bore site.
     """
     rng = np.random.RandomState(seed)
     n_obj = 30
     ra = 23.1
     dec = -15.6
     rotSkyPos = 23.56
     mjd = 59723.2
     obs = ObservationMetaData(pointingRA=ra,
                               pointingDec=dec,
                               rotSkyPos=rotSkyPos,
                               mjd=mjd)
     rr = rng.random_sample(n_obj) * 1.75
     theta = rng.random_sample(n_obj) * 2.0 * np.pi
     ra_list = ra + rr * np.cos(theta)
     dec_list = dec + rr * np.sin(theta)
     pm_ra = rng.random_sample(n_obj) * 20.0 - 10.0
     pm_dec = rng.random_sample(n_obj) * 20.0 - 10.0
     parallax = rng.random_sample(n_obj) * 1.0 - 0.5
     v_rad = rng.random_sample(n_obj) * 600.0 - 300.0
     return obs, ra_list, dec_list, pm_ra, pm_dec, parallax, v_rad
    def test_one_by_one(self):
        """
        test that running RA, Dec pairs in one at a time gives the same
        results as running them in in batches
        """

        ra = 145.0
        dec = -25.0
        obs = ObservationMetaData(pointingRA=ra,
                                  pointingDec=dec,
                                  mjd=59580.0,
                                  rotSkyPos=113.0)
        rng = np.random.RandomState(100)
        theta = rng.random_sample(100) * 2.0 * np.pi
        rr = rng.random_sample(len(theta)) * 2.0
        ra_list = ra + rr * np.cos(theta)
        dec_list = dec + rr * np.sin(theta)
        name_control = chipNameFromRaDecLSST(ra_list,
                                             dec_list,
                                             obs_metadata=obs)
        is_none = 0
        for ra, dec, name in zip(ra_list, dec_list, name_control):
            test_name = chipNameFromRaDecLSST(ra, dec, obs_metadata=obs)
            self.assertEqual(test_name, name)
            if test_name is None:
                is_none += 1

        self.assertGreater(is_none, 0)
        self.assertLess(is_none, (3 * len(ra_list)) // 4)
Exemplo n.º 31
0
class SNDBObj(BaseCatalogObj):
    objid = 'TwinkUnlensedSN'
    # From now on the tableid should be specified in instantiating the class
    # table = 'TwinkSN' or 'TwinkSNKraken'
    idColKey = 'id'
    raColName = 'snra'
    decColName = 'sndec'
    objectTypeId = 42
    #Don't run test on base class
    doRunTest = False
    #default observation metadata
    testObservationMetaData = ObservationMetaData(boundType='circle',
                                                  pointingRA=53.125,
                                                  pointingDec=-27.9,
                                                  boundLength=0.1,
                                                  mjd=52000.,
                                                  bandpassName='r',
                                                  m5=22.0)

    dbDefaultValues = {
        'varsimobjid': -1,
        'runid': -1,
        'ismultiple': -1,
        'run': -1,
        'runobjid': -1
    }

    # These types should be matched to the database.
    #: Default map is float.  If the column mapping is the same as the
    # column name, None can be specified

    columns = [('raJ2000', 'snra*PI()/180.'), ('decJ2000', 'sndec*PI()/180.'),
               ('Tt0', 't0'), ('Tx0', 'x0'), ('Tx1', 'x1'), ('Tc', 'c'),
               ('Tsnid', 'id'), ('Tredshift', 'redshift'),
               ('Tgaltileid', 'galtileid')]
    def test_default_varParamStr(self):
        """
        Test that DESCQAObjects now return varParamStr='None' by default
        """
        db = diskDESCQAObject_protoDC2(yaml_file_name='protoDC2')
        db.field_ra = self.field_ra
        db.field_dec = self.field_dec

        obs = ObservationMetaData(pointingRA=self.field_ra - 0.7,
                                  pointingDec=self.field_dec + 1.0)

        class VarParamStrTestClass(InstanceCatalog):
            column_outputs = ['raJ2000', 'decJ2000', 'varParamStr']

        cat = VarParamStrTestClass(db, obs_metadata=obs)
        cat_name = os.path.join(self.out_dir, 'varParamStr_cat.txt')
        cat.write_catalog(cat_name)
        line_ct = 0
        with open(cat_name, 'r') as in_file:
            for line in in_file:
                if line[0] == '#':
                    continue
                cols = line.strip().split()
                self.assertEqual(cols[2], 'None')
                line_ct += 1
        self.assertGreater(line_ct, 100)

        if os.path.exists(cat_name):
            os.unlink(cat_name)
    def testM5andSeeingAssignment(self):
        """
        Test assignment of m5 and seeing seeing and bandpass in ObservationMetaData
        """
        obsMD = ObservationMetaData(bandpassName=['u','g'], m5=[15.0, 16.0], seeing=[0.7, 0.6])
        self.assertAlmostEqual(obsMD.m5['u'], 15.0, 10)
        self.assertAlmostEqual(obsMD.m5['g'], 16.0, 10)
        self.assertAlmostEqual(obsMD.seeing['u'], 0.7, 10)
        self.assertAlmostEqual(obsMD.seeing['g'], 0.6, 10)

        obsMD.setBandpassM5andSeeing(bandpassName=['i','z'], m5=[25.0, 22.0], seeing=[0.5, 0.4])
        self.assertAlmostEqual(obsMD.m5['i'], 25.0, 10)
        self.assertAlmostEqual(obsMD.m5['z'], 22.0, 10)
        self.assertAlmostEqual(obsMD.seeing['i'], 0.5, 10)
        self.assertAlmostEqual(obsMD.seeing['z'], 0.4, 10)

        with self.assertRaises(KeyError):
            obsMD.m5['u']

        with self.assertRaises(KeyError):
            obsMD.m5['g']

        obsMD.m5 = [13.0, 14.0]
        obsMD.seeing = [0.2, 0.3]
        self.assertAlmostEqual(obsMD.m5['i'], 13.0, 10)
        self.assertAlmostEqual(obsMD.m5['z'], 14.0, 10)
        self.assertAlmostEqual(obsMD.seeing['i'], 0.2, 10)
        self.assertAlmostEqual(obsMD.seeing['z'], 0.3, 10)

        obsMD.setBandpassM5andSeeing(bandpassName=['k', 'j'], m5=[21.0, 23.0])
        self.assertAlmostEqual(obsMD.m5['k'], 21.0, 10)
        self.assertAlmostEqual(obsMD.m5['j'], 23.0, 10)
        self.assertIsNone(obsMD.seeing)

        obsMD.setBandpassM5andSeeing(bandpassName=['w', 'x'], seeing=[0.9, 1.1])
        self.assertAlmostEqual(obsMD.seeing['w'], 0.9, 10)
        self.assertAlmostEqual(obsMD.seeing['x'], 1.1, 10)

        phoSimMD = {'Opsim_filter':[4]}
        obsMD.phoSimMetaData = phoSimMD
        self.assertEqual(obsMD.bandpass, 4)
        self.assertTrue(obsMD.m5 is None)
        self.assertTrue(obsMD.seeing is None)
Exemplo n.º 34
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}
Exemplo n.º 35
0
        if not hasattr(self, 'sdssBandpassDict'):
            bandpassNames = ['u','g','r','i','z']
            bandpassDir = os.path.join(getPackageDir('throughputs'),'sdss')
            bandpassRoot = 'sdss_'

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

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


if __name__ == "__main__":

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

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

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

    obs_metadata_pointed.metadata = {}
    obs_metadata_pointed.metadata['Opsim_filter'] = 'i'
    dbObj = CatalogDBObject.from_objid('galaxyBase')
    sdssGalaxies = sdssGalaxies(dbObj, obs_metadata = obs_metadata_pointed)
    sdssGalaxies.write_catalog("example_sdss_galaxies.txt")
    def testAssignment(self):
        """
        Test that ObservationMetaData member variables get passed correctly
        """

        mjd = 5120.0
        RA = 1.5
        Dec = -1.1
        rotSkyPos = -10.0
        skyBrightness = 25.0

        testObsMD = ObservationMetaData()
        testObsMD.pointingRA = RA
        testObsMD.pointingDec = Dec
        testObsMD.rotSkyPos = rotSkyPos
        testObsMD.skyBrightness = skyBrightness
        testObsMD.mjd = mjd
        testObsMD.boundType = 'box'
        testObsMD.boundLength = [1.2, 3.0]

        self.assertAlmostEqual(testObsMD.pointingRA, RA, 10)
        self.assertAlmostEqual(testObsMD.pointingDec, Dec, 10)
        self.assertAlmostEqual(testObsMD.rotSkyPos, rotSkyPos, 10)
        self.assertAlmostEqual(testObsMD.skyBrightness, skyBrightness, 10)
        self.assertEqual(testObsMD.boundType, 'box')
        self.assertAlmostEqual(testObsMD.boundLength[0], 1.2, 10)
        self.assertAlmostEqual(testObsMD.boundLength[1], 3.0, 10)
        self.assertAlmostEqual(testObsMD.mjd.TAI, mjd, 10)

        #test reassignment

        testObsMD.pointingRA = RA+1.0
        testObsMD.pointingDec = Dec+1.0
        testObsMD.rotSkyPos = rotSkyPos+1.0
        testObsMD.skyBrightness = skyBrightness+1.0
        testObsMD.boundLength = 2.2
        testObsMD.boundType = 'circle'
        testObsMD.mjd = mjd + 10.0

        self.assertAlmostEqual(testObsMD.pointingRA, RA+1.0, 10)
        self.assertAlmostEqual(testObsMD.pointingDec, Dec+1.0, 10)
        self.assertAlmostEqual(testObsMD.rotSkyPos, rotSkyPos+1.0, 10)
        self.assertAlmostEqual(testObsMD.skyBrightness, skyBrightness+1.0, 10)
        self.assertEqual(testObsMD.boundType, 'circle')
        self.assertAlmostEqual(testObsMD.boundLength,2.2, 10)
        self.assertAlmostEqual(testObsMD.mjd.TAI, mjd+10.0, 10)

        phosimMD = OrderedDict([('pointingRA', (-2.0,float)),
                                ('pointingDec', (0.9,float)),
                                ('Opsim_rotskypos', (1.1,float)),
                                ('Opsim_expmjd',(4000.0,float)),
                                ('Opsim_filter',('g',str))])


        testObsMD.phoSimMetaData = phosimMD
        self.assertAlmostEqual(testObsMD.pointingRA, numpy.degrees(-2.0), 10)
        self.assertAlmostEqual(testObsMD.pointingDec, numpy.degrees(0.9), 10)
        self.assertAlmostEqual(testObsMD.rotSkyPos, numpy.degrees(1.1))
        self.assertAlmostEqual(testObsMD.mjd.TAI, 4000.0, 10)
        self.assertAlmostEqual(testObsMD.bandpass, 'g')

        testObsMD = ObservationMetaData(mjd=mjd, pointingRA=RA,
            pointingDec=Dec, rotSkyPos=rotSkyPos, bandpassName='z',
            skyBrightness=skyBrightness)

        self.assertAlmostEqual(testObsMD.mjd.TAI,5120.0,10)
        self.assertAlmostEqual(testObsMD.pointingRA,1.5,10)
        self.assertAlmostEqual(testObsMD.pointingDec,-1.1,10)
        self.assertAlmostEqual(testObsMD.rotSkyPos,-10.0,10)
        self.assertEqual(testObsMD.bandpass,'z')
        self.assertAlmostEqual(testObsMD.skyBrightness, skyBrightness, 10)

        testObsMD = ObservationMetaData()
        testObsMD.phoSimMetaData = phosimMD

        self.assertAlmostEqual(testObsMD.mjd.TAI,4000.0,10)

        #recall that pointingRA/Dec are stored as radians in phoSim metadata
        self.assertAlmostEqual(testObsMD.pointingRA,numpy.degrees(-2.0),10)
        self.assertAlmostEqual(testObsMD.pointingDec,numpy.degrees(0.9),10)
        self.assertAlmostEqual(testObsMD.rotSkyPos,numpy.degrees(1.1),10)
        self.assertEqual(testObsMD.bandpass,'g')

        testObsMD = ObservationMetaData()
        testObsMD.phoSimMetaData = phosimMD

        self.assertAlmostEqual(testObsMD.mjd.TAI,4000.0,10)

        #recall that pointingRA/Dec are stored as radians in phoSim metadata
        self.assertAlmostEqual(testObsMD.pointingRA,numpy.degrees(-2.0),10)
        self.assertAlmostEqual(testObsMD.pointingDec,numpy.degrees(0.9),10)
        self.assertAlmostEqual(testObsMD.rotSkyPos,numpy.degrees(1.1),10)
        self.assertEqual(testObsMD.bandpass,'g')


        # test assigning ModifiedJulianDate
        obs = ObservationMetaData()
        mjd = ModifiedJulianDate(TAI=57388.0)
        obs.mjd = mjd
        self.assertEqual(obs.mjd, mjd)

        mjd2 = ModifiedJulianDate(TAI=45000.0)
        obs.mjd = mjd2
        self.assertEqual(obs.mjd, mjd2)
        self.assertNotEqual(obs.mjd, mjd)
Exemplo n.º 37
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
Exemplo n.º 38
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
Exemplo n.º 39
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)
Exemplo n.º 40
0
    def test_ssm_catalog_creation(self):

        t = time.time()
        # Fake opsim data.
        database = os.path.join(getPackageDir('SIMS_DATA'), 'OpSimData/opsimblitz1_1133_sqlite.db')
        generator = ObservationMetaDataGenerator(database=database, driver='sqlite')

        night = 20
        query = 'select min(expMJD), max(expMJD) from summary where night=%d' % (night)
        res = generator.opsimdb.execute_arbitrary(query)
        expMJD_min = res[0][0]
        expMJD_max = res[0][1]

        obsMetaDataResults = generator.getObservationMetaData(expMJD=(expMJD_min, expMJD_max),
                                                              limit=3, boundLength=2.2)

        dt, t = dtime(t)
        print('To query opsim database: %f seconds' % (dt))

        write_header = True
        write_mode = 'w'

        try:
            ssmObj = SolarSystemObj()

            for obsMeta in obsMetaDataResults:
                # But moving objects databases are not currently complete for all years.
                # Push forward to night=747.
                # (note that we need the phosim dictionary as well)

                newMJD = 59590.2  # this MJD is artificially chosen to be in the
                                  # time span of the new baseline simulated survey

                obs = ObservationMetaData(mjd=newMJD,
                                          pointingRA=obsMeta.pointingRA,
                                          pointingDec=obsMeta.pointingDec,
                                          bandpassName=obsMeta.bandpass,
                                          rotSkyPos=obsMeta.rotSkyPos,
                                          m5=obsMeta.m5[obsMeta.bandpass],
                                          seeing=obsMeta.seeing[obsMeta.bandpass],
                                          boundLength=obsMeta.boundLength,
                                          boundType=obsMeta.boundType)

                obs._OpsimMetaData = {'visitExpTime': 30}

                mySsmDb = ssmCatCamera(ssmObj, obs_metadata = obs)
                photParams = PhotometricParameters(exptime = obs.OpsimMetaData['visitExpTime'],
                                                   nexp=1, bandpass=obs.bandpass)
                mySsmDb.photParams = photParams

                try:
                    with lsst.utils.tests.getTempFilePath('.txt') as output_cat:
                        mySsmDb.write_catalog(output_cat, write_header=write_header, write_mode=write_mode)

                        # verify that we did not write an empty catalog
                        with open(output_cat, 'r') as input_file:
                            lines = input_file.readlines()
                        msg = 'MJD is %.3f' % obs.mjd.TAI
                        self.assertGreater(len(lines), 1, msg=msg)
                except:
                    # This is because the solar system object 'tables'
                    # don't actually connect to tables on fatboy; they just
                    # call methods stored on fatboy.  Therefore, the connection
                    # failure will not be noticed until this part of the test
                    msg = sys.exc_info()[1].args[0]
                    if 'DB-Lib error' in msg:
                        reassure()
                        continue
                    else:
                        raise

                write_mode = 'a'
                write_header = False

                dt, t = dtime(t)
                print('To query solar system objects: %f seconds (obs MJD time %f)' % (dt, obs.mjd.TAI))

        except:
            trace = traceback.extract_tb(sys.exc_info()[2], limit=20)
            msg = sys.exc_info()[1].args[0]
            if 'Failed to connect' in msg or failedOnFatboy(trace):
                # if the exception was because of a failed connection
                # to fatboy, ignore it.
                reassure()

                pass
            else:
                raise
Exemplo n.º 41
0
    def test_eq(self):
        """
        Test that we implemented __eq__ and __ne__ correctly
        """
        empty_obs = ObservationMetaData()
        other_empty_obs = ObservationMetaData()
        self.assertEqual(empty_obs, other_empty_obs)
        self.assertTrue(empty_obs == other_empty_obs)
        self.assertFalse(empty_obs != other_empty_obs)

        dummy_site = Site(longitude=23.1, latitude=-11.1, temperature=11.0,
                          height=8921.01, pressure=734.1, humidity=0.1,
                          lapseRate=0.006)

        ref_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                      mjd=59580.1, rotSkyPos=91.2,
                                      bandpassName = 'u', m5=24.3,
                                      skyBrightness=22.1, seeing=0.8,
                                      site=dummy_site)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.1, seeing=0.8,
                                        site=dummy_site)

        self.assertEqual(ref_obs, other_obs)
        self.assertTrue(ref_obs == other_obs)
        self.assertFalse(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.41, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.1, seeing=0.8,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.2,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.1, seeing=0.8,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.2, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.1, seeing=0.8,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.1,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.1, seeing=0.8,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'g', m5=24.3,
                                        skyBrightness=22.1, seeing=0.8,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.1,
                                        skyBrightness=22.1, seeing=0.8,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.2, seeing=0.8,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.1, seeing=0.81,
                                        site=dummy_site)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs = ObservationMetaData(pointingRA=23.44, pointingDec=-19.1,
                                        mjd=59580.1, rotSkyPos=91.2,
                                        bandpassName = 'u', m5=24.3,
                                        skyBrightness=22.1, seeing=0.8)

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        # use assignment to bring other_obs back into agreement with
        # ref_obs
        other_obs.site = dummy_site
        self.assertEqual(ref_obs, other_obs)
        self.assertTrue(ref_obs == other_obs)
        self.assertFalse(ref_obs != other_obs)

        # now try cases of m5, bandpass, and seeing being lists
        ref_obs.setBandpassM5andSeeing(bandpassName=['u', 'r', 'z'],
                                       m5=[22.1, 23.5, 24.2],
                                       seeing=[0.6, 0.7, 0.8])

        other_obs.setBandpassM5andSeeing(bandpassName=['u', 'r', 'z'],
                                         m5=[22.1, 23.5, 24.2],
                                         seeing=[0.6, 0.7, 0.8])

        self.assertEqual(ref_obs, other_obs)
        self.assertTrue(ref_obs == other_obs)
        self.assertFalse(ref_obs != other_obs)

        other_obs.setBandpassM5andSeeing(bandpassName=['u', 'i', 'z'],
                                         m5=[22.1, 23.5, 24.2],
                                         seeing=[0.6, 0.7, 0.8])

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs.setBandpassM5andSeeing(bandpassName=['u', 'r', 'z'],
                                         m5=[22.1, 23.4, 24.2],
                                         seeing=[0.6, 0.7, 0.8])

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs.setBandpassM5andSeeing(bandpassName=['u', 'r', 'z'],
                                         m5=[22.1, 23.5, 24.2],
                                         seeing=[0.2, 0.7, 0.8])

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)

        other_obs.setBandpassM5andSeeing(bandpassName=['u', 'z'],
                                         m5=[22.1, 24.2],
                                         seeing=[0.2, 0.8])

        self.assertNotEqual(ref_obs, other_obs)
        self.assertFalse(ref_obs == other_obs)
        self.assertTrue(ref_obs != other_obs)
Exemplo n.º 42
0
    def testAssignment(self):
        """
        Test that ObservationMetaData member variables get passed correctly
        """

        mjd = 5120.0
        RA = 1.5
        Dec = -1.1
        rotSkyPos = -10.0
        skyBrightness = 25.0

        testObsMD = ObservationMetaData()
        testObsMD.pointingRA = RA
        testObsMD.pointingDec = Dec
        testObsMD.rotSkyPos = rotSkyPos
        testObsMD.skyBrightness = skyBrightness
        testObsMD.mjd = mjd
        testObsMD.boundType = 'box'
        testObsMD.boundLength = [1.2, 3.0]

        self.assertAlmostEqual(testObsMD.pointingRA, RA, 10)
        self.assertAlmostEqual(testObsMD.pointingDec, Dec, 10)
        self.assertAlmostEqual(testObsMD.rotSkyPos, rotSkyPos, 10)
        self.assertAlmostEqual(testObsMD.skyBrightness, skyBrightness, 10)
        self.assertEqual(testObsMD.boundType, 'box')
        self.assertAlmostEqual(testObsMD.boundLength[0], 1.2, 10)
        self.assertAlmostEqual(testObsMD.boundLength[1], 3.0, 10)
        self.assertAlmostEqual(testObsMD.mjd.TAI, mjd, 10)

        # test reassignment

        testObsMD.pointingRA = RA+1.0
        testObsMD.pointingDec = Dec+1.0
        testObsMD.rotSkyPos = rotSkyPos+1.0
        testObsMD.skyBrightness = skyBrightness+1.0
        testObsMD.boundLength = 2.2
        testObsMD.boundType = 'circle'
        testObsMD.mjd = mjd + 10.0

        self.assertAlmostEqual(testObsMD.pointingRA, RA+1.0, 10)
        self.assertAlmostEqual(testObsMD.pointingDec, Dec+1.0, 10)
        self.assertAlmostEqual(testObsMD.rotSkyPos, rotSkyPos+1.0, 10)
        self.assertAlmostEqual(testObsMD.skyBrightness, skyBrightness+1.0, 10)
        self.assertEqual(testObsMD.boundType, 'circle')
        self.assertAlmostEqual(testObsMD.boundLength, 2.2, 10)
        self.assertAlmostEqual(testObsMD.mjd.TAI, mjd+10.0, 10)

        testObsMD = ObservationMetaData(mjd=mjd, pointingRA=RA,
                                        pointingDec=Dec, rotSkyPos=rotSkyPos, bandpassName='z',
                                        skyBrightness=skyBrightness)

        self.assertAlmostEqual(testObsMD.mjd.TAI, 5120.0, 10)
        self.assertAlmostEqual(testObsMD.pointingRA, 1.5, 10)
        self.assertAlmostEqual(testObsMD.pointingDec, -1.1, 10)
        self.assertAlmostEqual(testObsMD.rotSkyPos, -10.0, 10)
        self.assertEqual(testObsMD.bandpass, 'z')
        self.assertAlmostEqual(testObsMD.skyBrightness, skyBrightness, 10)

        # test assigning ModifiedJulianDate
        obs = ObservationMetaData()
        mjd = ModifiedJulianDate(TAI=57388.0)
        obs.mjd = mjd
        self.assertEqual(obs.mjd, mjd)

        mjd2 = ModifiedJulianDate(TAI=45000.0)
        obs.mjd = mjd2
        self.assertEqual(obs.mjd, mjd2)
        self.assertNotEqual(obs.mjd, mjd)
    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