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)
    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)
Esempio n. 3
0
    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)
    def ObservationMetaDataFromPointing(self, OpSimPointingRecord, OpSimColumns=None,
                                        boundLength=1.75, boundType='circle'):
        """
        Return instance of ObservationMetaData for an OpSim Pointing record
        from OpSim.

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

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

        self._set_seeing_column(OpSimColumns)

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

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

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

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

        obs.OpsimMetaData = raw_dict

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

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

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

        self._set_seeing_column(OpSimColumns)

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

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

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

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

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

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

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

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

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

        obs.OpsimMetaData = raw_dict

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

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

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

        self._set_seeing_column(OpSimColumns)

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

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

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

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

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

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

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

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

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

        obs.OpsimMetaData = raw_dict

        return obs