Exemplo n.º 1
0
    def Calc_m5(self, filtre):

        filtre_trans = self.system[filtre]
        wavelen_min, wavelen_max, wavelen_step = filtre_trans.getWavelenLimits(
            None, None, None)

        bandpass = Bandpass(wavelen=filtre_trans.wavelen, sb=filtre_trans.sb)

        flatSedb = Sed()
        flatSedb.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
        flux0b = np.power(10., -0.4 * self.mag_sky[filtre])
        flatSedb.multiplyFluxNorm(flux0b)
        photParams = PhotometricParameters(bandpass=filtre)
        norm = photParams.platescale**2 / 2. * photParams.exptime / photParams.gain
        if self.atmos:
            self.data['m5'][filtre] = SignalToNoise.calcM5(
                flatSedb,
                self.atmosphere[filtre],
                self.system[filtre],
                photParams=photParams,
                FWHMeff=self.FWHMeff[filtre])
            adu_int = flatSedb.calcADU(bandpass=self.atmosphere[filtre],
                                       photParams=photParams)
            self.data['flux_sky'][filtre] = adu_int * norm
        else:
            self.data['m5'][filtre] = SignalToNoise.calcM5(
                flatSedb,
                self.system[filtre],
                self.system[filtre],
                photParams=photParams,
                FWHMeff=self.FWHMeff[filtre])
            adu_int = flatSedb.calcADU(bandpass=self.system[filtre],
                                       photParams=photParams)
            self.data['flux_sky'][filtre] = adu_int * norm
Exemplo n.º 2
0
 def testAstrometricError(self):
     fwhmGeom = 0.7
     m5 = 24.5
     # For bright objects, error should be systematic floor
     mag = 10
     astrometricErr = snr.calcAstrometricError(mag,
                                               m5,
                                               fwhmGeom=fwhmGeom,
                                               nvisit=1,
                                               systematicFloor=10)
     self.assertAlmostEqual(astrometricErr, 10, 3)
     # Even if you increase the number of visits, the systemic floor doesn't change
     astrometricErr = snr.calcAstrometricError(mag,
                                               m5,
                                               fwhmGeom=fwhmGeom,
                                               nvisit=100)
     self.assertAlmostEqual(astrometricErr, 10, 3)
     # For a single visit, fainter source, larger error and nvisits matters
     mag = 24.5
     astrometricErr1 = snr.calcAstrometricError(mag,
                                                m5,
                                                fwhmGeom=fwhmGeom,
                                                nvisit=1,
                                                systematicFloor=10)
     astrometricErr100 = snr.calcAstrometricError(mag,
                                                  m5,
                                                  fwhmGeom=fwhmGeom,
                                                  nvisit=100,
                                                  systematicFloor=10)
     self.assertGreater(astrometricErr1, astrometricErr100)
     self.assertAlmostEqual(astrometricErr1, 140.357, 3)
Exemplo n.º 3
0
    def testNoSystematicUncertainty(self):
        """
        Test that systematic uncertainty is handled correctly when set to None.
        """
        m5 = [23.5, 24.3, 22.1, 20.0, 19.5, 21.7]
        photParams = PhotometricParameters(sigmaSys=0.0)

        obs_metadata = ObservationMetaData(
            unrefractedRA=23.0, unrefractedDec=45.0, m5=m5, bandpassName=self.filterNameList
        )

        magnitudes = []
        for bp in self.bpList:
            mag = self.starSED.calcMag(bp)
            magnitudes.append(mag)

        skySedList = []

        for bp, hardware, filterName in zip(self.bpList, self.hardwareList, self.filterNameList):
            skyDummy = Sed()
            skyDummy.readSED_flambda(os.path.join(lsst.utils.getPackageDir("throughputs"), "baseline", "darksky.dat"))
            normalizedSkyDummy = setM5(
                obs_metadata.m5[filterName],
                skyDummy,
                bp,
                hardware,
                seeing=LSSTdefaults().seeing(filterName),
                photParams=photParams,
            )

            skySedList.append(normalizedSkyDummy)

        sigmaList = snr.calcMagError_m5(numpy.array(magnitudes), numpy.array(self.bpList), numpy.array(m5), photParams)

        for i in range(len(self.bpList)):
            snrat = snr.calcSNR_sed(
                self.starSED,
                self.bpList[i],
                skySedList[i],
                self.hardwareList[i],
                seeing=LSSTdefaults().seeing(self.filterNameList[i]),
                photParams=PhotometricParameters(),
            )

            testSNR, gamma = snr.calcSNR_m5(
                numpy.array([magnitudes[i]]),
                [self.bpList[i]],
                numpy.array([m5[i]]),
                photParams=PhotometricParameters(sigmaSys=0.0),
            )

            self.assertAlmostEqual(
                snrat, testSNR[0], 10, msg="failed on calcSNR_m5 test %e != %e " % (snrat, testSNR[0])
            )

            control = snr.magErrorFromSNR(testSNR)

            msg = "%e is not %e; failed" % (sigmaList[i], control)

            self.assertAlmostEqual(sigmaList[i], control, 10, msg=msg)
Exemplo n.º 4
0
 def testFWHMconversions(self):
     FWHMeff = 0.8
     FWHMgeom = snr.FWHMeff2FWHMgeom(FWHMeff)
     self.assertEqual(FWHMgeom, (0.822 * FWHMeff + 0.052))
     FWHMgeom = 0.8
     FWHMeff = snr.FWHMgeom2FWHMeff(FWHMgeom)
     self.assertEqual(FWHMeff, (FWHMgeom - 0.052) / 0.822)
Exemplo n.º 5
0
    def testSystematicUncertainty(self):
        """
        Test that systematic uncertainty is added correctly.
        """
        sigmaSys = 0.002
        m5_list = [23.5, 24.3, 22.1, 20.0, 19.5, 21.7]
        photParams = PhotometricParameters(sigmaSys=sigmaSys)

        obs_metadata = ObservationMetaData(pointingRA=23.0,
                                           pointingDec=45.0,
                                           m5=m5_list,
                                           bandpassName=self.filterNameList)
        magnitude_list = []
        for bp in self.bpList:
            mag = self.starSED.calcMag(bp)
            magnitude_list.append(mag)

        for bp, hardware, filterName, mm, m5 in \
            zip(self.bpList, self.hardwareList, self.filterNameList, magnitude_list, m5_list):

            skyDummy = Sed()
            skyDummy.readSED_flambda(
                os.path.join(lsst.utils.getPackageDir('throughputs'),
                             'baseline', 'darksky.dat'))

            normalizedSkyDummy = setM5(
                obs_metadata.m5[filterName],
                skyDummy,
                bp,
                hardware,
                FWHMeff=LSSTdefaults().FWHMeff(filterName),
                photParams=photParams)

            sigma, gamma = snr.calcMagError_m5(mm, bp, m5, photParams)

            snrat = snr.calcSNR_sed(self.starSED,
                                    bp,
                                    normalizedSkyDummy,
                                    hardware,
                                    FWHMeff=LSSTdefaults().FWHMeff(filterName),
                                    photParams=PhotometricParameters())

            testSNR, gamma = snr.calcSNR_m5(
                mm, bp, m5, photParams=PhotometricParameters(sigmaSys=0.0))

            self.assertAlmostEqual(snrat,
                                   testSNR,
                                   10,
                                   msg='failed on calcSNR_m5 test %e != %e ' %
                                   (snrat, testSNR))

            control = np.sqrt(
                np.power(snr.magErrorFromSNR(testSNR), 2) +
                np.power(sigmaSys, 2))

            msg = '%e is not %e; failed' % (sigma, control)

            self.assertAlmostEqual(sigma, control, 10, msg=msg)
Exemplo n.º 6
0
    def testSignalToNoise(self):
        """
        Test that calcSNR_m5 and calcSNR_sed give similar results
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        m5 = []
        for i in range(len(self.hardwareList)):
            m5.append(
                snr.calcM5(
                    self.skySed,
                    self.bpList[i],
                    self.hardwareList[i],
                    photParams,
                    seeing=defaults.seeing(self.filterNameList[i]),
                )
            )

        sedDir = lsst.utils.getPackageDir("sims_sed_library")
        sedDir = os.path.join(sedDir, "starSED", "kurucz")
        fileNameList = os.listdir(sedDir)

        numpy.random.seed(42)
        offset = numpy.random.random_sample(len(fileNameList)) * 2.0

        for ix, name in enumerate(fileNameList):
            if ix > 100:
                break
            spectrum = Sed()
            spectrum.readSED_flambda(os.path.join(sedDir, name))
            ff = spectrum.calcFluxNorm(m5[2] - offset[ix], self.bpList[2])
            spectrum.multiplyFluxNorm(ff)
            magList = []
            controlList = []
            magList = []
            for i in range(len(self.bpList)):
                controlList.append(
                    snr.calcSNR_sed(
                        spectrum,
                        self.bpList[i],
                        self.skySed,
                        self.hardwareList[i],
                        photParams,
                        defaults.seeing(self.filterNameList[i]),
                    )
                )

                magList.append(spectrum.calcMag(self.bpList[i]))

            testList, gammaList = snr.calcSNR_m5(
                numpy.array(magList), numpy.array(self.bpList), numpy.array(m5), photParams
            )

            for tt, cc in zip(controlList, testList):
                msg = "%e != %e " % (tt, cc)
                self.assertTrue(numpy.abs(tt / cc - 1.0) < 0.001, msg=msg)
Exemplo n.º 7
0
 def Get_m5(self,filtre,mag_SN,msky,photParams,FWHMeff):
     
     wavelen_min, wavelen_max, wavelen_step=self.transmission.lsst_system[filtre].getWavelenLimits(None,None,None)
             
     flatSed = Sed()
     flatSed.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
     flux0=np.power(10.,-0.4*msky)
     flatSed.multiplyFluxNorm(flux0)
     m5_calc=SignalToNoise.calcM5(flatSed,self.transmission.lsst_atmos_aerosol[filtre],self.transmission.lsst_system[filtre],photParams=photParams,FWHMeff=FWHMeff)
     snr_m5_through,gamma_through=SignalToNoise.calcSNR_m5(mag_SN,self.transmission.lsst_atmos_aerosol[filtre],m5_calc,photParams)
     
     return m5_calc,snr_m5_through
Exemplo n.º 8
0
    def testVerboseSNR(self):
        """
        Make sure that calcSNR_sed has everything it needs to run in verbose mode
        """
        photParams = PhotometricParameters()

        # create a cartoon spectrum to test on
        spectrum = Sed()
        spectrum.setFlatSED()
        spectrum.multiplyFluxNorm(1.0e-9)

        snr.calcSNR_sed(spectrum, self.bpList[0], self.skySed,
                        self.hardwareList[0], photParams, FWHMeff=0.7, verbose=True)
Exemplo n.º 9
0
    def Calc_Sky(self, paper, infos, transmission):

        Diameter = 6.5  #m
        Deltat = 30  #s
        platescale = 0.2  #arsec
        gain = 2.3

        for filtre in self.filters:

            filtre_trans = transmission.lsst_system[filtre]
            wavelen_min, wavelen_max, wavelen_step = filtre_trans.getWavelenLimits(
                None, None, None)
            photParams = PhotometricParameters()
            #photParams._exptime=30.

            bandpass = Bandpass(wavelen=filtre_trans.wavelen,
                                sb=filtre_trans.sb)

            infos['Skyb'][filtre] = 5455 * np.power(
                Diameter / 6.5, 2.) * np.power(Deltat / 30., 2.) * np.power(
                    platescale, 2.) * np.power(
                        10., 0.4 *
                        (25. -
                         infos['mbsky'][filtre])) * infos['Sigmab'][filtre]

            Zb = 181.8 * np.power(Diameter / 6.5, 2.) * infos['Tb'][filtre]
            mbZ = 25. + 2.5 * np.log10(Zb)
            flatSed = Sed()
            flatSed.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
            flux0 = np.power(10., -0.4 * mbZ)
            flatSed.multiplyFluxNorm(flux0)
            counts = flatSed.calcADU(
                bandpass, photParams=photParams)  #number of counts for exptime
            infos['mb_Z'][filtre] = mbZ
            infos['counts_mb_Z'][filtre] = counts / photParams.exptime

            flatSedb = Sed()
            flatSedb.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
            flux0b = np.power(10., -0.4 * infos['mbsky'][filtre])
            flatSedb.multiplyFluxNorm(flux0b)
            FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(paper['Seeing'][filtre])
            #FWHMeff = paper['Seeing'][filtre]
            #m5_calc=SignalToNoise.calcM5(flatSedb,transmission.lsst_atmos[filtre],transmission.lsst_system[filtre],photParams=photParams,FWHMeff=FWHMeff)
            m5_calc = SignalToNoise.calcM5(
                flatSedb,
                transmission.lsst_atmos[filtre],
                transmission.lsst_system[filtre],
                photParams=photParams,
                FWHMeff=self.paper['FWHMeff'][filtre])
            infos['fiveSigmaDepth'][filtre] = m5_calc
Exemplo n.º 10
0
    def testVerboseSNR(self):
        """
        Make sure that calcSNR_sed has everything it needs to run in verbose mode
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        #create a cartoon spectrum to test on
        spectrum = Sed()
        spectrum.setFlatSED()
        spectrum.multiplyFluxNorm(1.0e-9)

        snr.calcSNR_sed(spectrum, self.bpList[0], self.skySed,
                        self.hardwareList[0], photParams, seeing=0.7, verbose=True)
Exemplo n.º 11
0
    def testSignalToNoise(self):
        """
        Test that calcSNR_m5 and calcSNR_sed give similar results
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        m5 = []
        for i in range(len(self.hardwareList)):
            m5.append(snr.calcM5(self.skySed, self.bpList[i],
                      self.hardwareList[i],
                      photParams, seeing=defaults.seeing(self.filterNameList[i])))


        sedDir = lsst.utils.getPackageDir('sims_sed_library')
        sedDir = os.path.join(sedDir, 'starSED', 'kurucz')
        fileNameList = os.listdir(sedDir)

        numpy.random.seed(42)
        offset = numpy.random.random_sample(len(fileNameList))*2.0

        for ix, name in enumerate(fileNameList):
            if ix>100:
                break
            spectrum = Sed()
            spectrum.readSED_flambda(os.path.join(sedDir, name))
            ff = spectrum.calcFluxNorm(m5[2]-offset[ix], self.bpList[2])
            spectrum.multiplyFluxNorm(ff)
            magList = []
            controlList = []
            magList = []
            for i in range(len(self.bpList)):
                controlList.append(snr.calcSNR_sed(spectrum, self.bpList[i],
                                               self.skySed,
                                               self.hardwareList[i],
                                               photParams, defaults.seeing(self.filterNameList[i])))

                magList.append(spectrum.calcMag(self.bpList[i]))

            testList, gammaList = snr.calcSNR_m5(numpy.array(magList),
                                        numpy.array(self.bpList),
                                        numpy.array(m5),
                                        photParams)

            for tt, cc in zip(controlList, testList):
                msg = '%e != %e ' % (tt, cc)
                self.assertTrue(numpy.abs(tt/cc - 1.0) < 0.001, msg=msg)
Exemplo n.º 12
0
    def testNoSystematicUncertainty(self):
        """
        Test that systematic uncertainty is handled correctly when set to None.
        """
        m5_list = [23.5, 24.3, 22.1, 20.0, 19.5, 21.7]
        photParams= PhotometricParameters(sigmaSys=0.0)

        obs_metadata = ObservationMetaData(pointingRA=23.0, pointingDec=45.0,
                                           m5=m5_list, bandpassName=self.filterNameList)

        magnitude_list = []
        for bp in self.bpList:
            mag = self.starSED.calcMag(bp)
            magnitude_list.append(mag)

        skySedList = []

        for bp, hardware, filterName, mm, m5 in \
            zip(self.bpList, self.hardwareList, self.filterNameList, magnitude_list, m5_list):

            skyDummy = Sed()
            skyDummy.readSED_flambda(os.path.join(lsst.utils.getPackageDir('throughputs'),
                                     'baseline', 'darksky.dat'))

            normalizedSkyDummy = setM5(obs_metadata.m5[filterName], skyDummy,
                                       bp, hardware,
                                       FWHMeff=LSSTdefaults().FWHMeff(filterName),
                                       photParams=photParams)

            sigma, gamma = snr.calcMagError_m5(mm, bp, m5, photParams)


            snrat = snr.calcSNR_sed(self.starSED, bp, normalizedSkyDummy, hardware,
                              FWHMeff=LSSTdefaults().FWHMeff(filterName),
                              photParams=PhotometricParameters())

            testSNR, gamma = snr.calcSNR_m5(mm, bp, m5, photParams=PhotometricParameters(sigmaSys=0.0))

            self.assertAlmostEqual(snrat, testSNR, 10, msg = 'failed on calcSNR_m5 test %e != %e ' \
                                                               % (snrat, testSNR))

            control = snr.magErrorFromSNR(testSNR)

            msg = '%e is not %e; failed' % (sigma, control)

            self.assertAlmostEqual(sigma, control, 10, msg=msg)
Exemplo n.º 13
0
    def testNoSystematicUncertainty(self):
        """
        Test that systematic uncertainty is handled correctly when set to None.
        """
        m5 = [23.5, 24.3, 22.1, 20.0, 19.5, 21.7]
        photParams= PhotometricParameters(sigmaSys=0.0)

        obs_metadata = ObservationMetaData(unrefractedRA=23.0, unrefractedDec=45.0, m5=m5, bandpassName=self.filterNameList)

        magnitudes = []
        for bp in self.bpList:
            mag = self.starSED.calcMag(bp)
            magnitudes.append(mag)

        skySedList = []

        for bp, hardware, filterName in zip(self.bpList, self.hardwareList, self.filterNameList):
            skyDummy = Sed()
            skyDummy.readSED_flambda(os.path.join(lsst.utils.getPackageDir('throughputs'), 'baseline', 'darksky.dat'))
            normalizedSkyDummy = setM5(obs_metadata.m5[filterName], skyDummy,
                                       bp, hardware,
                                       seeing=LSSTdefaults().seeing(filterName),
                                       photParams=photParams)

            skySedList.append(normalizedSkyDummy)

        sigmaList = snr.calcMagError_m5(numpy.array(magnitudes), numpy.array(self.bpList), \
                                        numpy.array(m5), photParams)


        for i in range(len(self.bpList)):
            snrat = snr.calcSNR_sed(self.starSED, self.bpList[i], skySedList[i], self.hardwareList[i],
                              seeing=LSSTdefaults().seeing(self.filterNameList[i]),
                              photParams=PhotometricParameters())

            testSNR, gamma = snr.calcSNR_m5(numpy.array([magnitudes[i]]), [self.bpList[i]],
                                           numpy.array([m5[i]]), photParams=PhotometricParameters(sigmaSys=0.0))

            self.assertAlmostEqual(snrat, testSNR[0], 10, msg = 'failed on calcSNR_m5 test %e != %e ' \
                                                               % (snrat, testSNR[0]))

            control = snr.magErrorFromSNR(testSNR)

            msg = '%e is not %e; failed' % (sigmaList[i], control)

            self.assertAlmostEqual(sigmaList[i], control, 10, msg=msg)
Exemplo n.º 14
0
    def Calc_m5(self, filtre):
        """
        Calc_m5(filtre):
        
        Compute m5 or SNR at five sigma
        Tool function implemented by Phillie Gris (IN2P3)
        
        """
        # get telescope passband (no atmosphere)
        filtre_trans = self.system[filtre]
        wavelen_min, wavelen_max, wavelen_step = filtre_trans.getWavelenLimits(
            None, None, None)

        bandpass = Bandpass(wavelen=filtre_trans.wavelen, sb=filtre_trans.sb)
        # create a Flat sed S_nu from the sky brightness magnitude
        flatSedb = Sed()
        flatSedb.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
        flux0b = np.power(10., -0.4 * self.mag_sky[filtre])
        flatSedb.multiplyFluxNorm(flux0b)

        # Get LSST photometric parameters
        photParams = PhotometricParameters(bandpass=filtre)
        norm = photParams.platescale**2 / 2. * photParams.exptime / photParams.gain

        # Use LSST sims (SignalToNoise) to calculate M5 with atmosphere or without atmosphere
        if self.atmos:
            self.data['m5'][filtre] = SignalToNoise.calcM5(
                flatSedb,
                self.atmosphere[filtre],
                self.system[filtre],
                photParams=photParams,
                FWHMeff=self.FWHMeff[filtre])
            adu_int = flatSedb.calcADU(bandpass=self.atmosphere[filtre],
                                       photParams=photParams)
            self.data['flux_sky'][filtre] = adu_int * norm
        else:
            self.data['m5'][filtre] = SignalToNoise.calcM5(
                flatSedb,
                self.system[filtre],
                self.system[filtre],
                photParams=photParams,
                FWHMeff=self.FWHMeff[filtre])
            adu_int = flatSedb.calcADU(bandpass=self.system[filtre],
                                       photParams=photParams)
            self.data['flux_sky'][filtre] = adu_int * norm
Exemplo n.º 15
0
    def testMagError(self):
        """
        Make sure that calcMagError_sed and calcMagError_m5
        agree to within 0.001
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        # create a cartoon spectrum to test on
        spectrum = Sed()
        spectrum.setFlatSED()
        spectrum.multiplyFluxNorm(1.0e-9)

        # find the magnitudes of that spectrum in our bandpasses
        magList = []
        for total in self.bpList:
            magList.append(spectrum.calcMag(total))
        magList = np.array(magList)

        # try for different normalizations of the skySED
        for fNorm in np.arange(1.0, 5.0, 1.0):
            self.skySed.multiplyFluxNorm(fNorm)

            for total, hardware, filterName, mm in \
                zip(self.bpList, self.hardwareList, self.filterNameList, magList):

                FWHMeff = defaults.FWHMeff(filterName)

                m5 = snr.calcM5(self.skySed,
                                total,
                                hardware,
                                photParams,
                                FWHMeff=FWHMeff)

                sigma_sed = snr.calcMagError_sed(spectrum,
                                                 total,
                                                 self.skySed,
                                                 hardware,
                                                 photParams,
                                                 FWHMeff=FWHMeff)

                sigma_m5, gamma = snr.calcMagError_m5(mm, total, m5,
                                                      photParams)

                self.assertAlmostEqual(sigma_m5, sigma_sed, 3)
Exemplo n.º 16
0
    def CalcMyABMagnitudesError_filter(self, band, SkyBrightnessMag, FWHMGeom):
        """
        CalcMyABMagnitudesError_filter(self,band,SkyBrightnessMag,FWHMGeom)
        
        - author : Sylvie Dagoret-Campagne
        - affiliation : LAL/IN2P3/CNRS/FRANCE
        - date   : July 5th 2018
        
        Calculate magnitude errors for one band.
        
        Input args:
        - band : filter band
        - SkyBrighnessMag : Sky Brighness Magnitude in the band
        - FWHMGeom : Geometrical PSF in the band
        
        """

        filtre_atm = self.lsst_atmos[band]
        filtre_syst = self.lsst_system[band]

        wavelen_min, wavelen_max, wavelen_step = filtre_syst.getWavelenLimits(
            None, None, None)

        #calculation of effective PSF
        FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(FWHMGeom)

        photParams = PhotometricParameters(bandpass=band)

        # create a Flat sed S_nu from the sky brightness magnitude
        skysed = Sed()
        skysed.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
        flux0b = np.power(10., -0.4 * SkyBrightnessMag)
        skysed.multiplyFluxNorm(flux0b)

        #calcMagError filled according doc
        mag_err = SignalToNoise.calcMagError_sed(self.sed,
                                                 filtre_atm,
                                                 skysed,
                                                 filtre_syst,
                                                 photParams,
                                                 FWHMeff,
                                                 verbose=False)

        return mag_err
Exemplo n.º 17
0
    def testError_arr(self):
        """
        Test that calcMagError_m5 works on numpy arrays of magnitudes
        """
        rng = np.random.RandomState(17)
        mag_list = rng.random_sample(100)*5.0 + 15.0

        photParams = PhotometricParameters()
        bp = self.bpList[0]
        m5 = 24.0
        control_list = []
        for mm in mag_list:
            sig, gamma = snr.calcMagError_m5(mm, bp, m5, photParams)
            control_list.append(sig)
        control_list = np.array(control_list)

        test_list, gamma = snr.calcMagError_m5(mag_list, bp, m5, photParams)

        np.testing.assert_array_equal(control_list, test_list)
Exemplo n.º 18
0
    def testSNR_arr(self):
        """
        Test that calcSNR_m5 works on numpy arrays of magnitudes
        """
        numpy.random.seed(17)
        mag_list = numpy.random.random_sample(100)*5.0 + 15.0

        photParams = PhotometricParameters()
        bp = self.bpList[0]
        m5 = 24.0
        control_list = []
        for mm in mag_list:
            ratio, gamma = snr.calcSNR_m5(mm, bp, m5, photParams)
            control_list.append(ratio)
        control_list = numpy.array(control_list)

        test_list, gamma = snr.calcSNR_m5(mag_list, bp, m5, photParams)

        numpy.testing.assert_array_equal(control_list, test_list)
Exemplo n.º 19
0
    def testError_arr(self):
        """
        Test that calcMagError_m5 works on numpy arrays of magnitudes
        """
        rng = np.random.RandomState(17)
        mag_list = rng.random_sample(100) * 5.0 + 15.0

        photParams = PhotometricParameters()
        bp = self.bpList[0]
        m5 = 24.0
        control_list = []
        for mm in mag_list:
            sig, gamma = snr.calcMagError_m5(mm, bp, m5, photParams)
            control_list.append(sig)
        control_list = np.array(control_list)

        test_list, gamma = snr.calcMagError_m5(mag_list, bp, m5, photParams)

        np.testing.assert_array_equal(control_list, test_list)
Exemplo n.º 20
0
    def testSignalToNoise(self):
        """
        Test that calcSNR_m5 and calcSNR_sed give similar results
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        m5 = []
        for i in range(len(self.hardwareList)):
            m5.append(
                snr.calcM5(self.skySed,
                           self.bpList[i],
                           self.hardwareList[i],
                           photParams,
                           FWHMeff=defaults.FWHMeff(self.filterNameList[i])))

        sedDir = os.path.join(lsst.utils.getPackageDir('sims_photUtils'),
                              'tests/cartoonSedTestData/starSed/')
        sedDir = os.path.join(sedDir, 'kurucz')
        fileNameList = os.listdir(sedDir)

        rng = np.random.RandomState(42)
        offset = rng.random_sample(len(fileNameList)) * 2.0

        for ix, name in enumerate(fileNameList):
            if ix > 100:
                break
            spectrum = Sed()
            spectrum.readSED_flambda(os.path.join(sedDir, name))
            ff = spectrum.calcFluxNorm(m5[2] - offset[ix], self.bpList[2])
            spectrum.multiplyFluxNorm(ff)
            for i in range(len(self.bpList)):
                control_snr = snr.calcSNR_sed(
                    spectrum, self.bpList[i], self.skySed,
                    self.hardwareList[i], photParams,
                    defaults.FWHMeff(self.filterNameList[i]))

                mag = spectrum.calcMag(self.bpList[i])

                test_snr, gamma = snr.calcSNR_m5(mag, self.bpList[i], m5[i],
                                                 photParams)
                self.assertLess((test_snr - control_snr) / control_snr, 0.001)
Exemplo n.º 21
0
    def testMagError(self):
        """
        Make sure that calcMagError_sed and calcMagError_m5
        agree to within 0.001
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        #create a cartoon spectrum to test on
        spectrum = Sed()
        spectrum.setFlatSED()
        spectrum.multiplyFluxNorm(1.0e-9)

        #find the magnitudes of that spectrum in our bandpasses
        magList = []
        for total in self.bpList:
            magList.append(spectrum.calcMag(total))
        magList = numpy.array(magList)

        #try for different normalizations of the skySED
        for fNorm in numpy.arange(1.0, 5.0, 1.0):
            self.skySed.multiplyFluxNorm(fNorm)
            m5List = []
            magSed = []
            for total, hardware, filterName in \
            zip(self.bpList, self.hardwareList, self.filterNameList):

                seeing = defaults.seeing(filterName)

                m5List.append(snr.calcM5(self.skySed, total, hardware, photParams,seeing=seeing))

                magSed.append(snr.calcMagError_sed(spectrum, total, self.skySed,
                                                   hardware, photParams, seeing=seeing))

            magSed = numpy.array(magSed)

            magM5 = snr.calcMagError_m5(magList, self.bpList,
                                        numpy.array(m5List), photParams)


            numpy.testing.assert_array_almost_equal(magM5, magSed, decimal=3)
Exemplo n.º 22
0
def calc_m5_photUtils(hardware, system, darksky, visitFilter, filtsky, FWHMeff, expTime, airmass, tauCloud=0):
    m5 = np.zeros(len(expTime))
    for i in range(len(m5)):
        photParams = PhotometricParameters(exptime=expTime[i] / 2.0, nexp=2)
        skysed = copy.deepcopy(darksky)
        fluxnorm = skysed.calcFluxNorm(filtsky[i], system[visitFilter[i]])
        skysed.multiplyFluxNorm(fluxnorm)
        # Calculate the m5 value (this is for x=1.0 because we used x=1.0 in the atmosphere for system)
        m5[i] = SignalToNoise.calcM5(
            skysed, system[visitFilter[i]], hardware[visitFilter[i]], photParams, FWHMeff=FWHMeff[i]
        )
    return m5
Exemplo n.º 23
0
    def testSNRexceptions(self):
        """
        test that calcSNR_m5 raises an exception when arguments are not of the right shape.
        """

        photParams = PhotometricParameters()
        shortGamma = numpy.array([1.0, 1.0])
        shortMagnitudes = numpy.array([22.0, 23.0])
        magnitudes = 22.0*numpy.ones(6)
        self.assertRaises(RuntimeError, snr.calcSNR_m5, magnitudes, self.bpList, shortMagnitudes, photParams)
        self.assertRaises(RuntimeError, snr.calcSNR_m5, shortMagnitudes, self.bpList, magnitudes, photParams)
        self.assertRaises(RuntimeError, snr.calcSNR_m5, magnitudes, self.bpList, magnitudes, photParams, gamma=shortGamma)
        signalToNoise, gg = snr.calcSNR_m5(magnitudes, self.bpList, magnitudes, photParams)
Exemplo n.º 24
0
    def testSignalToNoise(self):
        """
        Test that calcSNR_m5 and calcSNR_sed give similar results
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        m5 = []
        for i in range(len(self.hardwareList)):
            m5.append(snr.calcM5(self.skySed, self.bpList[i],
                      self.hardwareList[i],
                      photParams, FWHMeff=defaults.FWHMeff(self.filterNameList[i])))

        sedDir = os.path.join(lsst.utils.getPackageDir('sims_photUtils'),
                              'tests/cartoonSedTestData/starSed/')
        sedDir = os.path.join(sedDir, 'kurucz')
        fileNameList = os.listdir(sedDir)

        rng = np.random.RandomState(42)
        offset = rng.random_sample(len(fileNameList))*2.0

        for ix, name in enumerate(fileNameList):
            if ix > 100:
                break
            spectrum = Sed()
            spectrum.readSED_flambda(os.path.join(sedDir, name))
            ff = spectrum.calcFluxNorm(m5[2]-offset[ix], self.bpList[2])
            spectrum.multiplyFluxNorm(ff)
            for i in range(len(self.bpList)):
                control_snr = snr.calcSNR_sed(spectrum, self.bpList[i],
                                              self.skySed,
                                              self.hardwareList[i],
                                              photParams, defaults.FWHMeff(self.filterNameList[i]))

                mag = spectrum.calcMag(self.bpList[i])

                test_snr, gamma = snr.calcSNR_m5(mag, self.bpList[i], m5[i], photParams)
                self.assertLess((test_snr-control_snr)/control_snr, 0.001)
Exemplo n.º 25
0
    def Calc_m5(self, filtre):

        filtre_trans = self.throughputs.system[filtre]
        wavelen_min, wavelen_max, wavelen_step = filtre_trans.getWavelenLimits(
            None, None, None)

        bandpass = Bandpass(wavelen=filtre_trans.wavelen, sb=filtre_trans.sb)

        flatSedb = Sed()
        flatSedb.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
        flux0b = np.power(10., -0.4 * self.mag_sky[filtre])
        flatSedb.multiplyFluxNorm(flux0b)
        if self.atmos:
            self.data['m5'][filtre] = SignalToNoise.calcM5(
                flatSedb,
                self.throughputs.atmosphere[filtre],
                self.throughputs.system[filtre],
                photParams=self.photParams,
                FWHMeff=self.FWHMeff[filtre])
            adu_int = flatSedb.calcADU(
                bandpass=self.throughputs.atmosphere[filtre],
                photParams=self.photParams)
            self.data['flux_sky'][
                filtre] = adu_int * self.pixel_area / self.expTime[
                    filtre] / self.gain
        else:
            self.data['m5'][filtre] = SignalToNoise.calcM5(
                flatSedb,
                self.throughputs.system[filtre],
                self.throughputs.system[filtre],
                photParams=self.photParams,
                FWHMeff=self.FWHMeff[filtre])
            adu_int = flatSedb.calcADU(
                bandpass=self.throughputs.system[filtre],
                photParams=self.photParams)
            self.data['flux_sky'][
                filtre] = adu_int * self.pixel_area / self.expTime[
                    filtre] / self.gain
Exemplo n.º 26
0
    def testMagError(self):
        """
        Make sure that calcMagError_sed and calcMagError_m5
        agree to within 0.001
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        # create a cartoon spectrum to test on
        spectrum = Sed()
        spectrum.setFlatSED()
        spectrum.multiplyFluxNorm(1.0e-9)

        # find the magnitudes of that spectrum in our bandpasses
        magList = []
        for total in self.bpList:
            magList.append(spectrum.calcMag(total))
        magList = numpy.array(magList)

        # try for different normalizations of the skySED
        for fNorm in numpy.arange(1.0, 5.0, 1.0):
            self.skySed.multiplyFluxNorm(fNorm)
            m5List = []
            magSed = []
            for total, hardware, filterName in zip(self.bpList, self.hardwareList, self.filterNameList):

                seeing = defaults.seeing(filterName)

                m5List.append(snr.calcM5(self.skySed, total, hardware, photParams, seeing=seeing))

                magSed.append(snr.calcMagError_sed(spectrum, total, self.skySed, hardware, photParams, seeing=seeing))

            magSed = numpy.array(magSed)

            magM5 = snr.calcMagError_m5(magList, self.bpList, numpy.array(m5List), photParams)

            numpy.testing.assert_array_almost_equal(magM5, magSed, decimal=3)
Exemplo n.º 27
0
    def testSNRexceptions(self):
        """
        test that calcSNR_m5 raises an exception when arguments are not of the right shape.
        """

        photParams = PhotometricParameters()
        shortGamma = numpy.array([1.0, 1.0])
        shortMagnitudes = numpy.array([22.0, 23.0])
        magnitudes = 22.0 * numpy.ones(6)
        self.assertRaises(RuntimeError, snr.calcSNR_m5, magnitudes, self.bpList, shortMagnitudes, photParams)
        self.assertRaises(RuntimeError, snr.calcSNR_m5, shortMagnitudes, self.bpList, magnitudes, photParams)
        self.assertRaises(
            RuntimeError, snr.calcSNR_m5, magnitudes, self.bpList, magnitudes, photParams, gamma=shortGamma
        )
        signalToNoise, gg = snr.calcSNR_m5(magnitudes, self.bpList, magnitudes, photParams)
Exemplo n.º 28
0
    def testMagError(self):
        """
        Make sure that calcMagError_sed and calcMagError_m5
        agree to within 0.001
        """
        defaults = LSSTdefaults()
        photParams = PhotometricParameters()

        # create a cartoon spectrum to test on
        spectrum = Sed()
        spectrum.setFlatSED()
        spectrum.multiplyFluxNorm(1.0e-9)

        # find the magnitudes of that spectrum in our bandpasses
        magList = []
        for total in self.bpList:
            magList.append(spectrum.calcMag(total))
        magList = np.array(magList)

        # try for different normalizations of the skySED
        for fNorm in np.arange(1.0, 5.0, 1.0):
            self.skySed.multiplyFluxNorm(fNorm)

            for total, hardware, filterName, mm in \
                zip(self.bpList, self.hardwareList, self.filterNameList, magList):

                FWHMeff = defaults.FWHMeff(filterName)

                m5 = snr.calcM5(self.skySed, total, hardware, photParams, FWHMeff=FWHMeff)

                sigma_sed = snr.calcMagError_sed(spectrum, total, self.skySed,
                                                 hardware, photParams, FWHMeff=FWHMeff)

                sigma_m5, gamma = snr.calcMagError_m5(mm, total, m5, photParams)

                self.assertAlmostEqual(sigma_m5, sigma_sed, 3)
Exemplo n.º 29
0
    def calcSNR_Flux(self, df, transm):
        """
        Method to estimate SNRs and fluxes (in e.sec)
        using lsst sims estimators

        Parameters
        ---------------
        df: pandas df
           data to process
        transm : array
           throughputs

        Returns
        ----------
        original df plus the following cols:
        gamma: gamma values
        snr_m5: snr values
        flux_e_sec: flux in pe/sec

        """

        # estimate SNR
        # Get photometric parameters to estimate SNR
        photParams = [
            PhotometricParameters(
                exptime=vv[self.exptimeCol] / vv[self.nexpCol],
                nexp=vv[self.nexpCol]) for index, vv in df.iterrows()
        ]

        nvals = range(len(df))
        calc = [
            SignalToNoise.calcSNR_m5(df.iloc[i]['mag'], transm[i],
                                     df.iloc[i][self.m5Col], photParams[i])
            for i in nvals
        ]

        df['snr_m5'] = [calc[i][0] for i in nvals]
        df['gamma'] = [calc[i][1] for i in nvals]
        # estimate the flux in elec.sec-1
        df['flux_e_sec'] = self.telescope.mag_to_flux_e_sec(
            df['mag'].values, df[self.filterCol].values,
            df[self.exptimeCol] / df[self.nexpCol], df[self.nexpCol])[:, 1]

        return df
Exemplo n.º 30
0
def calc_m5_photUtils(hardware,
                      system,
                      darksky,
                      visitFilter,
                      filtsky,
                      FWHMeff,
                      expTime,
                      airmass,
                      tauCloud=0):
    m5 = np.zeros(len(expTime))
    for i in range(len(m5)):
        photParams = PhotometricParameters(exptime=expTime[i] / 2.0, nexp=2)
        skysed = copy.deepcopy(darksky)
        fluxnorm = skysed.calcFluxNorm(filtsky[i], system[visitFilter[i]])
        skysed.multiplyFluxNorm(fluxnorm)
        # Calculate the m5 value (this is for x=1.0 because we used x=1.0 in the atmosphere for system)
        m5[i] = SignalToNoise.calcM5(skysed,
                                     system[visitFilter[i]],
                                     hardware[visitFilter[i]],
                                     photParams,
                                     FWHMeff=FWHMeff[i])
    return m5
Exemplo n.º 31
0
    def CalcMyABMagnitudesErrors(self):
        """
        CalcMyABMagnitudesErrors(self)
        
        - author : Sylvie Dagoret-Campagne
        - affiliation : LAL/IN2P3/CNRS/FRANCE
        - date   : July 4th 2018
        
        Calculate magnitude errors for all bands
        
        """
        all_magABErr = []

        for i, band in enumerate(self.filterlist):

            filtre_atm = self.lsst_atmos[band]
            filtre_syst = self.lsst_system[band]

            wavelen_min, wavelen_max, wavelen_step = filtre_syst.getWavelenLimits(
                None, None, None)

            photParams = PhotometricParameters(bandpass=band)
            FWHMeff = self.data['FWHMeff'][band]

            # create a Flat sed S_nu from the sky brightness magnitude
            skysed = Sed()
            skysed.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
            flux0b = np.power(10., -0.4 * self.mag_sky[band])
            skysed.multiplyFluxNorm(flux0b)

            #calcMagError filled according doc
            magerr=SignalToNoise.calcMagError_sed( \
                self.sed,filtre_atm,skysed,filtre_syst,photParams,FWHMeff,verbose=False)

            all_magABErr.append(magerr)
        return np.array(all_magABErr)
Exemplo n.º 32
0
    def get(self, what, band):
        """
        Decorator to access quantities

        Parameters
        ---------------
        what: str
          parameter to estimate
        band: str
          filter

        """
        filter_trans = self.system[band]
        wavelen_min, wavelen_max, wavelen_step = filter_trans.getWavelenLimits(
            None, None, None)

        bandpass = Bandpass(wavelen=filter_trans.wavelen, sb=filter_trans.sb)

        flatSedb = Sed()
        flatSedb.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
        flux0b = np.power(10., -0.4 * self.mag_sky(band))
        flatSedb.multiplyFluxNorm(flux0b)
        photParams = PhotometricParameters(bandpass=band)
        norm = photParams.platescale**2 / 2. * photParams.exptime / photParams.gain
        trans = filter_trans

        if self.atmos:
            trans = self.atmosphere[band]
        self.data['m5'][band] = SignalToNoise.calcM5(
            flatSedb,
            trans,
            filter_trans,
            photParams=photParams,
            FWHMeff=self.FWHMeff(band))
        adu_int = flatSedb.calcADU(bandpass=trans, photParams=photParams)
        self.data['flux_sky'][band] = adu_int * norm
Exemplo n.º 33
0
def calcM5(hardware, system, atmos, title='m5'):
    """
    Calculate m5 values for all filters in hardware and system.
    Prints all values that go into "table 2" of the overview paper.
    Returns dictionary of m5 values.
    """
    # photParams stores default values for the exposure time, nexp, size of the primary,
    #  readnoise, gain, platescale, etc.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/PhotometricParameters.py
    effarea = np.pi * (6.423 / 2. * 100.)**2
    photParams_zp = PhotometricParameters(exptime=1,
                                          nexp=1,
                                          gain=1,
                                          effarea=effarea,
                                          readnoise=8.8,
                                          othernoise=0,
                                          darkcurrent=0.2)
    photParams = PhotometricParameters(gain=1.0,
                                       effarea=effarea,
                                       readnoise=8.8,
                                       othernoise=0,
                                       darkcurrent=0.2)
    photParams_infinity = PhotometricParameters(gain=1.0,
                                                readnoise=0,
                                                darkcurrent=0,
                                                othernoise=0,
                                                effarea=effarea)
    # lsstDefaults stores default values for the FWHMeff.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/LSSTdefaults.py
    lsstDefaults = LSSTdefaults()
    darksky = Sed()
    darksky.readSED_flambda(os.path.join('../siteProperties', 'darksky.dat'))
    flatSed = Sed()
    flatSed.setFlatSED()
    m5 = {}
    Tb = {}
    Sb = {}
    kAtm = {}
    Cm = {}
    dCm_infinity = {}
    sourceCounts = {}
    skyCounts = {}
    skyMag = {}
    gamma = {}
    for f in system:
        m5[f] = SignalToNoise.calcM5(darksky,
                                     system[f],
                                     hardware[f],
                                     photParams,
                                     FWHMeff=lsstDefaults.FWHMeff(f))
        fNorm = flatSed.calcFluxNorm(m5[f], system[f])
        flatSed.multiplyFluxNorm(fNorm)
        sourceCounts[f] = flatSed.calcADU(system[f], photParams=photParams)
        # Calculate the Skycounts expected in this bandpass.
        skyCounts[f] = (darksky.calcADU(hardware[f], photParams=photParams) *
                        photParams.platescale**2)
        # Calculate the sky surface brightness.
        skyMag[f] = darksky.calcMag(hardware[f])
        # Calculate the gamma value.
        gamma[f] = SignalToNoise.calcGamma(system[f], m5[f], photParams)
        # Calculate the "Throughput Integral" (this is the hardware + atmosphere)
        dwavelen = np.mean(np.diff(system[f].wavelen))
        Tb[f] = np.sum(system[f].sb / system[f].wavelen) * dwavelen
        # Calculate the "Sigma" 'system integral' (this is the hardware only)
        Sb[f] = np.sum(hardware[f].sb / hardware[f].wavelen) * dwavelen
        # Calculate km - atmospheric extinction in a particular bandpass
        kAtm[f] = -2.5 * np.log10(Tb[f] / Sb[f])
        # Calculate the Cm and Cm_Infinity values.
        # m5 = Cm + 0.5*(msky - 21) + 2.5log10(0.7/FWHMeff) + 1.25log10(t/30) - km(X-1.0)
        # Exptime should be 30 seconds and X=1.0
        exptime = photParams.exptime * photParams.nexp
        if exptime != 30.0:
            print "Whoa, exposure time was not as expected - got %s not 30 seconds. Please edit Cm calculation." % (
                exptime)
        # Assumes atmosphere used in system throughput is X=1.0
        X = 1.0
        Cm[f] = (m5[f] - 0.5 * (skyMag[f] - 21) -
                 2.5 * np.log10(0.7 / lsstDefaults.FWHMeff(f)))
        # Calculate Cm_Infinity by setting readout noise to zero.
        m5inf = SignalToNoise.calcM5(darksky,
                                     system[f],
                                     hardware[f],
                                     photParams_infinity,
                                     FWHMeff=lsstDefaults.FWHMeff(f))
        Cm_infinity = (m5inf - 0.5 * (skyMag[f] - 21) -
                       2.5 * np.log10(0.7 / lsstDefaults.FWHMeff(f)))
        dCm_infinity[f] = Cm_infinity - Cm[f]
    print title
    print 'Filter FWHMeff FWHMgeom SkyMag SkyCounts Tb Sb kAtm Gamma Cm dCm_infinity m5 SourceCounts'
    for f in ('u', 'g', 'r', 'i', 'z', 'y'):
        print '%s %.2f %.2f %.2f %.1f %.3f %.3f %.4f %.6f %.2f %.2f %.2f %.2f'\
           %(f, lsstDefaults.FWHMeff(f),
             SignalToNoise.FWHMeff2FWHMgeom(lsstDefaults.FWHMeff(f)),
             skyMag[f], skyCounts[f], Tb[f], Sb[f], kAtm[f],
             gamma[f], Cm[f], dCm_infinity[f], m5[f], sourceCounts[f])

    # Show what these look like individually (add sky & m5 limits on throughput curves)
    plt.figure()
    for f in filterlist:
        plt.plot(system[f].wavelen,
                 system[f].sb,
                 color=filtercolors[f],
                 linewidth=2,
                 label=f)
    plt.plot(atmosphere.wavelen, atmosphere.sb, 'k:', label='X=1.0')
    plt.legend(loc='center right', fontsize='smaller')
    plt.xlim(300, 1100)
    plt.ylim(0, 1)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Throughput')
    plt.title('System Throughputs')
    plt.grid(True)

    plt.figure()
    ax = plt.gca()
    # Add dark sky
    ax2 = ax.twinx()
    plt.sca(ax2)
    skyab = -2.5 * np.log10(darksky.fnu) - darksky.zp
    ax2.plot(darksky.wavelen,
             skyab,
             'k-',
             linewidth=0.8,
             label='Dark sky mags')
    ax2.set_ylabel('AB mags')
    ax2.set_ylim(24, 14)
    plt.sca(ax)
    # end of dark sky
    handles = []
    for f in filterlist:
        plt.plot(system[f].wavelen,
                 system[f].sb,
                 color=filtercolors[f],
                 linewidth=2)
        myline = mlines.Line2D([], [],
                               color=filtercolors[f],
                               linestyle='-',
                               linewidth=2,
                               label='%s: m5 %.1f (sky %.1f)' %
                               (f, m5[f], skyMag[f]))
        handles.append(myline)
    plt.plot(atmos.wavelen, atmos.sb, 'k:', label='Atmosphere, X=1.0')
    # Add legend for dark sky.
    myline = mlines.Line2D([], [],
                           color='k',
                           linestyle='-',
                           label='Dark sky AB mags/arcsec^2')
    handles.append(myline)
    # end of dark sky legend line
    plt.legend(loc=(0.01, 0.69),
               handles=handles,
               fancybox=True,
               numpoints=1,
               fontsize='small')
    plt.ylim(0, 1)
    plt.xlim(300, 1100)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Fractional Throughput Response')
    plt.title('System total response curves %s' % (title))
    return m5
def calcM5(hardware, system, atmos, title='m5'):
    """
    Calculate m5 values for all filters in hardware and system.
    Prints all values that go into "table 2" of the overview paper.
    Returns dictionary of m5 values.
    """
    # photParams stores default values for the exposure time, nexp, size of the primary,
    #  readnoise, gain, platescale, etc.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/PhotometricParameters.py
    photParams = PhotometricParameters(gain=1)
    photParams_infinity = PhotometricParameters(readnoise=0, darkcurrent=0,
                                                othernoise=0, gain=1)
    # lsstDefaults stores default values for the FWHMeff.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/LSSTdefaults.py
    lsstDefaults = LSSTdefaults()
    darksky = Sed()
    darksky.readSED_flambda(os.path.join('../siteProperties', 'darksky.dat'))
    flatSed = Sed()
    flatSed.setFlatSED()
    m5 = {}
    Tb = {}
    Sb = {}
    kAtm = {}
    Cm = {}
    dCm_infinity = {}
    sourceCounts = {}
    skyCounts = {}
    skyMag = {}
    gamma = {}
    for f in system:
        m5[f] = SignalToNoise.calcM5(darksky, system[f], hardware[f], photParams,
                                     FWHMeff=lsstDefaults.FWHMeff(f))
        fNorm = flatSed.calcFluxNorm(m5[f], system[f])
        flatSed.multiplyFluxNorm(fNorm)
        sourceCounts[f] = flatSed.calcADU(system[f], photParams=photParams)
        # Calculate the Skycounts expected in this bandpass.
        skyCounts[f] = (darksky.calcADU(hardware[f], photParams=photParams)
                        * photParams.platescale**2)
        # Calculate the sky surface brightness.
        skyMag[f] = darksky.calcMag(hardware[f])
        # Calculate the gamma value.
        gamma[f] = SignalToNoise.calcGamma(system[f], m5[f], photParams)
        # Calculate the "Throughput Integral" (this is the hardware + atmosphere)
        dwavelen = np.mean(np.diff(system[f].wavelen))
        Tb[f] = np.sum(system[f].sb / system[f].wavelen) * dwavelen
        # Calculate the "Sigma" 'system integral' (this is the hardware only)
        Sb[f] = np.sum(hardware[f].sb / hardware[f].wavelen) * dwavelen
        # Calculate km - atmospheric extinction in a particular bandpass
        kAtm[f] = -2.5*np.log10(Tb[f] / Sb[f])
        # Calculate the Cm and Cm_Infinity values.
        # m5 = Cm + 0.5*(msky - 21) + 2.5log10(0.7/FWHMeff) + 1.25log10(t/30) - km(X-1.0)
        # Exptime should be 30 seconds and X=1.0
        exptime = photParams.exptime * photParams.nexp
        if exptime != 30.0:
            print "Whoa, exposure time was not as expected - got %s not 30 seconds. Please edit Cm calculation." %(exptime)
        # Assumes atmosphere used in system throughput is X=1.0
        X = 1.0
        Cm[f] = (m5[f] - 0.5*(skyMag[f] - 21) - 2.5*np.log10(0.7/lsstDefaults.FWHMeff(f)))
        # Calculate Cm_Infinity by setting readout noise to zero.
        m5inf = SignalToNoise.calcM5(darksky, system[f], hardware[f],  photParams_infinity,
                                     FWHMeff=lsstDefaults.FWHMeff(f))
        Cm_infinity = (m5inf - 0.5*(skyMag[f] - 21)
                       - 2.5*np.log10(0.7/lsstDefaults.FWHMeff(f)))
        dCm_infinity[f] = Cm_infinity - Cm[f]
    print title
    print 'Filter FWHMeff FWHMgeom SkyMag SkyCounts Tb Sb kAtm Gamma Cm dCm_infinity m5 SourceCounts'
    for f in ('u', 'g' ,'r', 'i', 'z', 'y'):
        print '%s %.2f %.2f %.2f %.1f %.3f %.3f %.4f %.6f %.2f %.2f %.2f %.2f'\
           %(f, lsstDefaults.FWHMeff(f),
             SignalToNoise.FWHMeff2FWHMgeom(lsstDefaults.FWHMeff(f)),
             skyMag[f], skyCounts[f], Tb[f], Sb[f], kAtm[f],
             gamma[f], Cm[f], dCm_infinity[f], m5[f], sourceCounts[f])

    # Show what these look like individually (add sky & m5 limits on throughput curves)
    plt.figure()
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2, label=f)
    plt.plot(atmosphere.wavelen, atmosphere.sb, 'k:', label='X=1.0')
    plt.legend(loc='center right', fontsize='smaller')
    plt.xlim(300, 1100)
    plt.ylim(0, 1)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Throughput')
    plt.title('System Throughputs')
    plt.grid(True)

    plt.figure()
    ax = plt.gca()
    # Add dark sky
    ax2 = ax.twinx()
    plt.sca(ax2)
    skyab = -2.5*np.log10(darksky.fnu) - darksky.zp
    ax2.plot(darksky.wavelen, skyab,
             'k-', linewidth=0.8, label='Dark sky mags')
    ax2.set_ylabel('AB mags')
    ax2.set_ylim(24, 14)
    plt.sca(ax)
    # end of dark sky
    handles = []
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2)
        myline = mlines.Line2D([], [], color=filtercolors[f], linestyle='-', linewidth=2,
                               label = '%s: m5 %.1f (sky %.1f)' %(f, m5[f], skyMag[f]))
        handles.append(myline)
    plt.plot(atmos.wavelen, atmos.sb, 'k:', label='Atmosphere, X=1.0')
    # Add legend for dark sky.
    myline = mlines.Line2D([], [], color='k', linestyle='-', label='Dark sky AB mags/arcsec^2')
    handles.append(myline)
    # end of dark sky legend line
    plt.legend(loc=(0.01, 0.69), handles=handles, fancybox=True, numpoints=1, fontsize='small')
    plt.ylim(0, 1)
    plt.xlim(300, 1100)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Fractional Throughput Response')
    plt.title('System total response curves %s' %(title))
    return m5
Exemplo n.º 35
0
    def Simulate_and_Fit_LC(self, observations, transmission, zmin, zmax):

        #print 'time',observations['expMJD'],observations['filter']

        #print 'simulate and fit'
        ra = observations[self.fieldRA][0]
        dec = observations[self.fieldDec][0]

        if self.SN.sn_type == 'Ia':
            mbsim = self.SN.SN._source.peakmag('bessellb', 'vega')
        else:
            mbsim = -1

        #This will be the data for sncosmo fitting
        table_for_fit = {}
        table_for_fit['error_calc'] = Table(names=('time', 'flux', 'fluxerr',
                                                   'band', 'zp', 'zpsys'),
                                            dtype=('f8', 'f8', 'f8', 'S7',
                                                   'f4', 'S4'))
        table_for_fit['error_coadd_calc'] = Table(
            names=('time', 'flux', 'fluxerr', 'band', 'zp', 'zpsys'),
            dtype=('f8', 'f8', 'f8', 'S7', 'f4', 'S4'))
        table_for_fit['error_opsim'] = Table(names=('time', 'flux', 'fluxerr',
                                                    'band', 'zp', 'zpsys'),
                                             dtype=('f8', 'f8', 'f8', 'S7',
                                                    'f4', 'S4'))
        table_for_fit['error_coadd_opsim'] = Table(
            names=('time', 'flux', 'fluxerr', 'band', 'zp', 'zpsys'),
            dtype=('f8', 'f8', 'f8', 'S7', 'f4', 'S4'))
        table_for_fit['error_through'] = Table(
            names=('time', 'flux', 'fluxerr', 'band', 'zp', 'zpsys'),
            dtype=('f8', 'f8', 'f8', 'S7', 'f4', 'S4'))
        table_for_fit['error_coadd_through'] = Table(
            names=('time', 'flux', 'fluxerr', 'band', 'zp', 'zpsys'),
            dtype=('f8', 'f8', 'f8', 'S7', 'f4', 'S4'))

        mytype = [('obsHistID', np.int), ('filtSkyBrightness', np.float),
                  ('airmass', np.float), ('moonPhase', np.float),
                  ('fieldRA', np.float), ('fieldDec', np.float),
                  ('visitExpTime', np.float), ('expDate', np.int),
                  ('filter', np.dtype('a15')), ('fieldID', np.int),
                  ('fiveSigmaDepth', np.float), ('ditheredDec', np.float),
                  ('expMJD', np.float), ('ditheredRA', np.float),
                  ('rawSeeing', np.float), ('flux', np.float),
                  ('err_flux', np.float), ('err_flux_opsim', np.float),
                  ('err_flux_through', np.float), ('finSeeing', np.float),
                  ('katm_opsim', np.float), ('katm_calc', np.float),
                  ('m5_calc', np.float), ('Tb', np.float),
                  ('Sigmab', np.float), ('Cm', np.float), ('dCm', np.float),
                  ('mag_SN', np.float), ('snr_m5_through', np.float),
                  ('snr_m5_opsim', np.float), ('gamma_through', np.float),
                  ('gamma_opsim', np.float), ('snr_SED', np.float)]

        myobservations = np.zeros((60, 1), dtype=mytype)

        #print 'Nobservations',len(observations)
        nobs = -1
        for filtre in self.filterNames:
            obs_filtre = observations[np.where(
                observations['filter'] == filtre)]
            #print 'ehehe',obs_filtre

            for obs in obs_filtre:

                nobs += 1

                if len(myobservations) <= nobs:
                    myobservations = np.resize(myobservations,
                                               (len(myobservations) + 100, 1))

                for name in observations.dtype.names:
                    myobservations[name][nobs] = obs[name]

                #print 'time uu',obs['expMJD']
                seeing = obs['rawSeeing']
                #seeing=obs['finSeeing']
                time_obs = obs['expMJD']
                m5_opsim = obs['fiveSigmaDepth']

                #print 'getting SED'
                sed_SN = self.SN.get_SED(time_obs)
                #print 'got SED',sed_SN.wavelen,sed_SN.flambda,obs['expMJD']
                """
                outf = open('SN_'+str(time)+'.dat', 'wb')
                for i,wave in enumerate(sn.SEDfromSNcosmo.wavelen):
                    print >> outf,wave,sn.SEDfromSNcosmo.flambda[i]
                outf.close()
                """
                #print 'loading transmission airmass'
                transmission.Load_Atmosphere(obs['airmass'])
                flux_SN = sed_SN.calcFlux(
                    bandpass=transmission.lsst_atmos_aerosol[filtre])
                #print 'this is my flux',flux_SN
                #flux_SN=sed_SN.calcFlux(bandpass=transmission.lsst_system[filtre]) / 3631.0

                myup = transmission.darksky.calcInteg(
                    transmission.lsst_system[filtre])
                """
                wavelen, sb = transmission.lsst_system[filtre].multiplyThroughputs(transmission.lsst_atmos[filtre].wavelen, transmission.lsst_atmos[filtre].sb)
                lsst_total= Bandpass(wavelen=wavelen, sb=sb)
                """
                Tb = self.Calc_Integ(transmission.lsst_atmos[filtre])
                Sigmab = self.Calc_Integ(transmission.lsst_system[filtre])
                katm = -2.5 * np.log10(Tb / Sigmab)

                mbsky_through = -2.5 * np.log10(myup / (3631. * Sigmab))

                #print 'there mbsky',filtre,mbsky_through,obs['filtSkyBrightness'],katm,self.kAtm[filtre],Tb,Sigmab,obs['airmass']

                Filter_Wavelength_Correction = np.power(
                    500.0 / self.params.filterWave[filtre], 0.3)
                Airmass_Correction = math.pow(obs['airmass'], 0.6)
                FWHM_Sys = self.params.FWHM_Sys_Zenith * Airmass_Correction
                FWHM_Atm = seeing * Filter_Wavelength_Correction * Airmass_Correction
                finSeeing = self.params.scaleToNeff * math.sqrt(
                    np.power(FWHM_Sys, 2) +
                    self.params.atmNeffFactor * np.power(FWHM_Atm, 2))

                #print 'hello pal',filtre,finSeeing,obs['visitExpTime']
                Tscale = obs['visitExpTime'] / 30.0 * np.power(
                    10.0, -0.4 *
                    (obs['filtSkyBrightness'] - self.params.msky[filtre]))
                dCm = self.params.dCm_infinity[filtre] - 1.25 * np.log10(
                    1 + np.power(10., 0.8 * self.params.dCm_infinity[filtre] -
                                 1.) / Tscale)

                m5_recalc = dCm + self.params.Cm[filtre] + 0.5 * (
                    obs['filtSkyBrightness'] - 21.) + 2.5 * np.log10(
                        0.7 / finSeeing) - self.params.kAtm[filtre] * (
                            obs['airmass'] - 1.) + 1.25 * np.log10(
                                obs['visitExpTime'] / 30.)

                myobservations['Cm'][nobs] = self.params.Cm[filtre]
                myobservations['dCm'][nobs] = dCm
                myobservations['finSeeing'][nobs] = finSeeing
                myobservations['Tb'][nobs] = Tb
                myobservations['Sigmab'][nobs] = Sigmab
                myobservations['katm_calc'][nobs] = katm
                myobservations['katm_opsim'][nobs] = self.params.kAtm[filtre]
                wavelen_min, wavelen_max, wavelen_step = transmission.lsst_system[
                    filtre].getWavelenLimits(None, None, None)
                flatSed = Sed()
                flatSed.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
                flux0 = np.power(10., -0.4 * obs['filtSkyBrightness'])
                flatSed.multiplyFluxNorm(flux0)

                if flux_SN > 0:

                    #print 'positive flux',flux_SN
                    mag_SN = -2.5 * np.log10(flux_SN / 3631.0)

                    FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(finSeeing)
                    #FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(seeing)
                    photParams = PhotometricParameters()
                    snr_SN = SignalToNoise.calcSNR_sed(
                        sed_SN,
                        transmission.lsst_atmos_aerosol[filtre],
                        transmission.darksky,
                        transmission.lsst_system[filtre],
                        photParams,
                        FWHMeff=FWHMeff,
                        verbose=False)
                    #m5_calc=SignalToNoise.calcM5(transmission.darksky,transmission.lsst_atmos_aerosol[filtre],transmission.lsst_system[filtre],photParams=photParams,FWHMeff=FWHMeff)
                    m5_calc = SignalToNoise.calcM5(
                        flatSed,
                        transmission.lsst_atmos_aerosol[filtre],
                        transmission.lsst_system[filtre],
                        photParams=photParams,
                        FWHMeff=FWHMeff)
                    snr_m5_through, gamma_through = SignalToNoise.calcSNR_m5(
                        mag_SN, transmission.lsst_atmos_aerosol[filtre],
                        m5_calc, photParams)
                    snr_m5_opsim, gamma_opsim = SignalToNoise.calcSNR_m5(
                        mag_SN, transmission.lsst_atmos_aerosol[filtre],
                        m5_opsim, photParams)

                    #print 'm5 diff',filtre,m5_calc,m5_opsim,m5_calc/m5_opsim,m5_recalc,(m5_opsim/m5_recalc)
                    err_flux_SN = flux_SN / snr_SN
                    err_flux_SN_opsim = flux_SN / snr_m5_opsim
                    err_flux_SN_through = flux_SN / snr_m5_through

                    #print 'test errors',flux_SN,err_flux_SN,err_flux_SN_opsim,err_flux_SN_through,err_flux_SN_through/err_flux_SN_opsim,m5_opsim-m5_calc
                    myobservations['mag_SN'][nobs] = mag_SN
                    myobservations['flux'][nobs] = flux_SN
                    myobservations['err_flux'][nobs] = err_flux_SN
                    myobservations['err_flux_opsim'][nobs] = err_flux_SN_opsim
                    myobservations['err_flux_through'][
                        nobs] = err_flux_SN_through
                    myobservations['m5_calc'][nobs] = m5_calc
                    myobservations['snr_m5_through'][nobs] = snr_m5_through
                    myobservations['snr_m5_opsim'][nobs] = snr_m5_opsim
                    myobservations['gamma_through'][nobs] = gamma_through
                    myobservations['gamma_opsim'][nobs] = gamma_opsim
                    myobservations['snr_SED'][nobs] = snr_SN

                    #print 'SNR',flux_SN,flux_SN/err_flux_SN,flux_SN/err_flux_SN_opsim
                    #if flux_SN/err_flux_SN >=5:
                    #table_for_fit['error_calc'].add_row((time_obs,flux_SN,err_flux_SN,'LSST::'+filtre,25,'ab'))
                    #if flux_SN/err_flux_SN_opsim >=5.:
                    table_for_fit['error_opsim'].add_row(
                        (time_obs, flux_SN, err_flux_SN_opsim,
                         'LSST::' + filtre, 25, 'ab'))
                    table_for_fit['error_through'].add_row(
                        (time_obs, flux_SN, err_flux_SN_through,
                         'LSST::' + filtre, 25, 'ab'))
                    #print 'Getting fluxes and errors',time.time()-self.thetime,filtre,nobs
                else:
                    err_flux_SN = -999.
                    err_flux_SN_opsim = -999.
                    myobservations['mag_SN'][nobs] = -999
                    myobservations['flux'][nobs] = flux_SN
                    myobservations['err_flux'][nobs] = -999.
                    myobservations['err_flux_opsim'][nobs] = -999.
                    myobservations['err_flux_through'][nobs] = -999.
                    myobservations['m5_calc'][nobs] = -999.
                    myobservations['snr_m5_through'][nobs] = -999
                    myobservations['snr_m5_opsim'][nobs] = -999
                    myobservations['gamma_through'][nobs] = -999
                    myobservations['gamma_opsim'][nobs] = -999
                    myobservations['snr_SED'][nobs] = -999

                #print 'flux SN',flux_SN,err_flux_SN,mag_SN,snr_SN,snr_m5_through,snr_m5_opsim

                #t.add_row((time,flux_SN,err_flux_SN,'LSST::'+filtre,0.,'vega'))

                #break

        #t = Table([list(data['time']), list(data['band']),data['flux'],data['fluxerr'],data['flux_aero'],data['fluxerr_aero'],data['fluxerr_new'],data['zp'],data['zpsys']], names=('time','band','flux','fluxerr','flux_aero','fluxerr_aero','fluxerr_new','zp','zpsys'), meta={'name': 'first table'})

        #model.set(z=0.5)
        #print SN.SN

        myobservations = np.resize(myobservations, (nobs + 1, 1))
        #print 'there obs',myobservations
        #print 'Getting coadds',time.time()-self.thetime
        for band in ['u', 'g', 'r', 'i', 'z', 'y']:
            #sela=table_for_fit['error_calc'][np.where(table_for_fit['error_calc']['band']=='LSST::'+band)]
            #sela=sela[np.where(np.logical_and(sela['flux']/sela['fluxerr']>5.,sela['flux']>0.))]
            selb = table_for_fit['error_opsim'][np.where(
                table_for_fit['error_opsim']['band'] == 'LSST::' + band)]
            #selb=selb[np.where(np.logical_and(selb['flux']/selb['fluxerr']>5.,selb['flux']>0.))]
            selc = table_for_fit['error_through'][np.where(
                table_for_fit['error_through']['band'] == 'LSST::' + band)]

            #table_for_fit['error_coadd_calc']=vstack([table_for_fit['error_coadd_calc'],self.Get_coadd(sela)])
            table_for_fit['error_coadd_opsim'] = vstack(
                [table_for_fit['error_coadd_opsim'],
                 self.Get_coadd(selb)])
            table_for_fit['error_coadd_through'] = vstack(
                [table_for_fit['error_coadd_through'],
                 self.Get_coadd(selc)])

        #print 'There we go fitting',time.time()-self.thetime
        dict_fit = {}
        #for val in ['error_calc','error_coadd_calc','error_opsim','error_coadd_opsim']:
        for val in ['error_coadd_opsim', 'error_coadd_through']:
            dict_fit[val] = {}
            dict_fit[val]['sncosmo_fitted'] = {}
            dict_fit[val]['table_for_fit'] = table_for_fit[val]
            #print 'fit',val,time.time()-self.thetime
            res, fitted_model, mbfit, fit_status = self.Fit_SN(
                table_for_fit[val], zmin, zmax)
            if res is not None:
                dict_fit[val]['sncosmo_res'] = res
                #self.dict_fit[val]['fitted_model']=fitted_model
                for i, par in enumerate(fitted_model.param_names):
                    dict_fit[val]['sncosmo_fitted'][
                        par] = fitted_model.parameters[i]
                dict_fit[val]['mbfit'] = mbfit
            dict_fit[val]['fit_status'] = fit_status

        return dict_fit, mbsim, myobservations
Exemplo n.º 36
0
    def __call__(self, obs, out_q=None):

        #print 'hello here',len(mjds),filtre
        #time_begin=time.time()
        """
        mjds=obs['mjd']
        airmass=obs['airmass']
        m5=obs['m5sigmadepth']
        filtre=obs['band']
        expTime=obs['exptime']
        """

        fluxes = 10. * self.SN.flux(obs['mjd'], self.wave)

        wavelength = self.wave / 10.

        wavelength = np.repeat(wavelength[np.newaxis, :], len(fluxes), 0)
        SED_time = Sed(wavelen=wavelength, flambda=fluxes)
        #print 'total elapse time seds',time.time()-time_begin
        """
        time_begin=time.time()
        for i in range(len(SED_time.wavelen)):
            

            photParams=PhotometricParameters(nexp=expTime[i]/15.)
            sed=Sed(wavelen=SED_time.wavelen[i],flambda=SED_time.flambda[i])
            
            self.transmission.Load_Atmosphere(airmass[i])
            trans=self.transmission.atmosphere[filtre]
            
            flux_SN=sed.calcFlux(bandpass=trans)
            
            if flux_SN >0:
                mag_SN=-2.5 * np.log10(flux_SN / 3631.0)  
                snr_m5_opsim,gamma_opsim=SignalToNoise.calcSNR_m5(mag_SN,trans,m5[i],photParams)
                err_flux_SN=flux_SN/snr_m5_opsim
                e_per_sec = sed.calcADU(bandpass=trans, photParams=photParams) #number of ADU counts for expTime
                    #e_per_sec = sed.calcADU(bandpass=self.transmission.lsst_atmos[filtre], photParams=photParams)
                #print 'alors',e_per_sec,expTime[i],photParams.gain
                e_per_sec/=expTime[i]/photParams.gain
                #print 'alors b',e_per_sec
                #print 'ref',filtre,i,mjds[i],e_per_sec
                    #self.lc[filtre].append(e_per_sec)
                r.append((e_per_sec,mjds[i],flux_SN))
                
                #self.table_for_fit.add_row((mjds[i],flux_SN,err_flux_SN,'LSST::'+filtre,25,'ab'))
                #self.table_for_fit.add_row((mjds[i],mag_SN,mag_SN/snr_m5_opsim,'LSST::'+filtre,25,'ab'))
                self.table_obs.add_row((mjds[i],flux_SN,err_flux_SN,'LSST::'+filtre,2.5*np.log10(3631),'ab',airmass[i],m5[i],expTime[i],e_per_sec,self.telescope.mag_to_flux(m5[i],filtre)))
                #print 'there we go',filtre,e_per_sec,self.telescope.mag_to_flux(m5[i],filtre)
            
        #print 'total elapse time GEN LC',time.time()-time_begin
        """
        #time_begin=time.time()
        fluxes = []
        transes = []
        seds = [
            Sed(wavelen=SED_time.wavelen[i], flambda=SED_time.flambda[i])
            for i in range(len(SED_time.wavelen))
        ]

        #photParams=[]
        #time_begin=time.time()
        """
        for i in range(len(SED_time.wavelen)):
            
            #photParams=PhotometricParameters(nexp=expTime[i]/15.)
            #sed=Sed(wavelen=SED_time.wavelen[i],flambda=SED_time.flambda[i])
            
            self.transmission.Load_Atmosphere(airmass[i])
            trans=self.transmission.atmosphere[filtre]
            
            #flux_SN=sed.calcFlux(bandpass=trans)
            
            #fluxes.append(flux_SN)
            transes.append(trans)
            #seds.append(sed)
        """

        transes = [
            self.transmission.atmosphere[obs['band'][i][-1]]
            for i in range(len(SED_time.wavelen))
        ]
        #print 'total elapse time sed',time.time()-time_begin,len(transes)
        fluxes = [
            seds[i].calcFlux(bandpass=transes[i])
            for i in range(len(SED_time.wavelen))
        ]
        #print 'total elapse time sed',time.time()-time_begin
        #print 'before',len(fluxes),len(seds),len(transes),len(m5),len(expTime),len(mjds),self.param['X1'],self.param['Color']
        #print fluxes,mjds
        #time_begin=time.time()

        table_obs = Table(obs)
        snr_m5 = []
        e_per_sec_list = []
        for i in range(len(SED_time.wavelen)):
            photParams = PhotometricParameters(nexp=table_obs['exptime'][i] /
                                               15.)
            flux_SN = fluxes[i]
            if flux_SN > 0:
                trans = self.transmission.atmosphere[table_obs['band'][i][-1]]
                mag_SN = -2.5 * np.log10(flux_SN / 3631.0)
                snr_m5_opsim, gamma_opsim = SignalToNoise.calcSNR_m5(
                    mag_SN, trans, table_obs['m5sigmadepth'][i], photParams)
                err_flux_SN = flux_SN / snr_m5_opsim
                e_per_sec = seds[i].calcADU(
                    bandpass=trans,
                    photParams=photParams)  #number of ADU counts for expTime
                #e_per_sec = sed.calcADU(bandpass=self.transmission.lsst_atmos[filtre], photParams=photParams)
                #print 'alors',e_per_sec,expTime[i],photParams.gain
                e_per_sec /= table_obs['exptime'][i] / photParams.gain
                #print('hello',mag_SN,flux_SN,e_per_sec,snr_m5_opsim)
                snr_m5.append(snr_m5_opsim)
                e_per_sec_list.append(e_per_sec)
                #print fluxes,mags
            else:
                snr_m5.append(1)
                e_per_sec_list.append(1)

        #print('passed')

        table_obs.add_column(Column(fluxes, name='flux'))
        table_obs.add_column(Column(snr_m5, name='snr_m5'))
        table_obs.add_column(Column(e_per_sec_list, name='flux_e'))
        idx = table_obs['flux'] >= 0.
        table_obs = table_obs[idx]
        """
        mags=-2.5 * np.log10(table_obs['flux'] / 3631.0)
        def snr(bands,mags,sky,seeing,expTime,ron):
            pixel_scale=0.2
            mag_sky=dict(zip('ugrizy',[22.95,22.24,21.20,20.47,19.60,18.63]))
            FWHMeff = {'u':0.92, 'g':0.87, 'r':0.83, 'i':0.80, 'z':0.78, 'y':0.76}
            neff=np.array(2.266*(seeing/pixel_scale)**2)
            flux_e=self.telescope.mag_to_flux(mags,[band[-1] for band in bands])*expTime
            #flux_sky=self.telescope.mag_to_flux([mag_sky[band[-1]] for band in bands],[band[-1] for band in bands])*expTime
            flux_sky=self.telescope.mag_to_flux(sky,[band[-1] for band in bands])*expTime
            #res=flux_e/np.sqrt((flux_sky*pixel_scale**2+ron**2)*neff)
            res=flux_e/np.sqrt(flux_e+(flux_sky*pixel_scale**2+ron**2)*neff)
            #for i in range(len(mags)):
            #print mags[i],flux_e[i],res[i]
            return res,flux_e/expTime

        snrs,flux_e=snr(table_obs['band'],mags,table_obs['sky'],table_obs['seeing'],table_obs['exptime'],5.)
        print('ici',flux_e,snrs)
        """
        table_obs.add_column(
            Column(table_obs['flux'] / table_obs['snr_m5'], name='fluxerr'))
        #table_obs.add_column(Column(flux_e, name='flux_e'))
        #table_obs.add_column(Column(flux_e/snrs, name='flux_e_err'))
        table_obs.add_column(
            Column(table_obs['flux_e'] / table_obs['snr_m5'],
                   name='flux_e_err'))
        table_obs.add_column(
            Column([2.5 * np.log10(3631)] * len(table_obs), name='zp'))
        table_obs.add_column(Column(['ab'] * len(table_obs), name='zpsys'))
        #table_obs.add_column(Column(self.telescope.mag_to_flux(mags,[band[-1] for band in table_obs['band']]),name='flux_e_sec'))
        #table_obs.add_column(Column(self.telescope.mag_to_flux(obs['m5sigmadepth'],[band[-1] for band in table_obs['band']]),'flux_5sigma_e_sec'))
        table_obs['band'][:] = np.array(
            [b.replace('LSSTPG', 'LSST') for b in table_obs['band']])

        table_obs.rename_column('mjd', 'time')
        table_obs.rename_column('m5sigmadepth', 'm5')

        for colname in [
                'exptime', 'rawSeeing', 'seeing', 'moon_frac', 'sky', 'kAtm',
                'airmass', 'm5', 'Nexp', 'Ra', 'Dec'
        ]:
            if colname in table_obs.colnames:
                table_obs.remove_column(colname)

        #print(table_obs[['flux_e','snr_m5']])
        """
        table_obs.remove_column('exptime')
        table_obs.remove_column('rawSeeing')
        table_obs.remove_column('seeing')
        table_obs.remove_column('moon_frac')
        table_obs.remove_column('sky')
        table_obs.remove_column('kAtm')
        table_obs.remove_column('airmass')
        table_obs.remove_column('m5')
        table_obs.remove_column('Nexp')
        table_obs.remove_column('Ra')
        table_obs.remove_column('Dec')
        """
        #table_obs.remove_column('flux_e_sec')
        #table_obs.remove_column('flux_5sigma_e_sec')

        #print len(table_obs)
        #print table_obs
        #print len(table_obs),table_obs.dtype
        #print np.array(np.sort(table_obs,order='band'))
        """
        idx=fluxes > 0.
        fluxes=fluxes[idx]
        seds=np.array(seds)[idx]
        transes=np.array(transes)[idx]
        m5=m5[idx]
        expTime=expTime[idx]
        photParams=[PhotometricParameters(nexp=expTime[i]/15.) for i in range(len(expTime))]
        airmass=airmass[idx]
        mjds=mjds[idx]
        filtre=filtre[idx]

        #print 'allors',len(fluxes),len(seds),len(transes),len(m5),len(expTime),len(photParams),len(mjds)
        #print fluxes,mjds
        mags=-2.5 * np.log10(fluxes / 3631.0)  
        gamma = np.asarray([SignalToNoise.calcGamma(transes[i],m5[i],photParams[i]) for i in range(len(mags))])
        x=np.power(10.,0.4*(mags-m5))
        snr_m5_gamma=1./np.asarray(np.sqrt((0.04-gamma)*x+gamma*(x**2)))
        #snr_m5_gamma_orig=[SignalToNoise.calcSNR_m5(mags[i],transes[i],m5[i],photParams[i]) for i in range(len(mags))]
        #print 'hhh',snr_m5_gamma,snr_m5_gamma_orig
        #snr_m5_opsim=[SignalToNoise.calcSNR_m5(mags[i],transes[i],m5[i],photParams[i]) for i in range(len(mags))]
        err_fluxes=fluxes/snr_m5_gamma
        #err_fluxes_orig=[fluxes[i]/snr_m5_gamma_orig[i][0] for i in range(len(fluxes))]
        
        e_per_sec = [seds[i].calcADU(bandpass=transes[i], photParams=photParams[i]) for i in range(len(transes))] #number of ADU counts for expTime
        
        e_per_sec=[e_per_sec[i]/(expTime[i]/photParams[i].gain) for i in range(len(e_per_sec))]
        #mags_new=[self.telescope.flux_to_mag(e_per_sec[i],filtre[i][-1])[0] for i in range(len(e_per_sec))]

        #print 10**(-0.4*(mags-mags_new))
        print filtre[:]
        print 'test',e_per_sec,self.telescope.mag_to_flux(mags,[band[-1] for band in filtre]),e_per_sec/self.telescope.mag_to_flux(mags,[band[-1] for band in filtre])

        

        #snrs=snr(filtre,mags,obs['sky'],np.array([0.8]*len(mags)),obs['exptime'],5.)
        print snr_m5_gamma,snrs,np.median(snr_m5_gamma/snrs),np.mean(snr_m5_gamma/snrs),np.min(snr_m5_gamma/snrs),np.max(snr_m5_gamma/snrs)

        table_obs=Table()
        table_obs.add_column(Column(mjds, name='time'))
        table_obs.add_column(Column(fluxes, name='flux'))
        table_obs.add_column(Column(err_fluxes, name='fluxerr'))
        table_obs.add_column(Column(['LSST::'+filtre[i][-1] for i in range(len(filtre))], name='band'))
        table_obs.add_column(Column([2.5*np.log10(3631)]*len(mjds),name='zp'))
        table_obs.add_column(Column(['ab']*len(mjds),name='zpsys'))
        table_obs.add_column(Column(airmass,name='airmass'))
        table_obs.add_column(Column(m5,name='m5'))
        table_obs.add_column(Column(expTime,name='expTime'))
        table_obs.add_column(Column(e_per_sec,name='flux_e_sec'))
        table_obs.add_column(Column(self.telescope.mag_to_flux(m5,[filtre[i][-1] for i in range(len(filtre))]),'flux_5sigma_e_sec'))
        #print 'total elapse time GEN LC b',time.time()-time_begin
        #print table_obs
        """
        if out_q is not None:
            out_q.put({filtre[0][-1]: table_obs})

        return table_obs
Exemplo n.º 37
0
def calcM5s(hardware, system, atmos, title='m5'):
    photParams = PhotometricParameters()
    lsstDefaults = LSSTdefaults()
    darksky = Sed()
    darksky.readSED_flambda(os.path.join(os.getenv('SYSENG_THROUGHPUTS_DIR'), 'siteProperties', 'darksky.dat'))
    flatSed = Sed()
    flatSed.setFlatSED()
    m5 = {}
    sourceCounts = {}
    skyCounts = {}
    skyMag = {}
    gamma = {}
    for f in system:
        m5[f] = SignalToNoise.calcM5(darksky, system[f], hardware[f], photParams, seeing=lsstDefaults.seeing(f))
        fNorm = flatSed.calcFluxNorm(m5[f], system[f])
        flatSed.multiplyFluxNorm(fNorm)
        sourceCounts[f] = flatSed.calcADU(system[f], photParams=photParams)
        # Calculate the Skycounts expected in this bandpass.
        skyCounts[f] = darksky.calcADU(hardware[f], photParams=photParams) * photParams.platescale**2
        # Calculate the sky surface brightness.
        skyMag[f] = darksky.calcMag(hardware[f])
        # Calculate the gamma value.
        gamma[f] = SignalToNoise.calcGamma(system[f], m5[f], photParams)
    print title
    print 'Filter m5 SourceCounts SkyCounts SkyMag Gamma'
    for f in ('u', 'g' ,'r', 'i', 'z', 'y'):
        print '%s %.2f %.1f %.2f %.2f %.6f' %(f, m5[f], sourceCounts[f], skyCounts[f], skyMag[f], gamma[f])

    # Show what these look like individually (add sky & m5 limits on throughput curves)
    plt.figure()
    ax = plt.gca()
    # Add dark sky
    ax2 = ax.twinx()
    plt.sca(ax2)
    skyab = -2.5*np.log10(darksky.fnu) - darksky.zp
    ax2.plot(darksky.wavelen, skyab,
             'k-', linewidth=0.8, label='Dark sky mags')
    ax2.set_ylabel('AB mags')
    ax2.set_ylim(24, 10)
    plt.sca(ax)
    # end of dark sky
    handles = []
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2)
        myline = mlines.Line2D([], [], color=filtercolors[f], linestyle='-', linewidth=2,
                               label = '%s: m5 %.1f (sky %.1f)' %(f, m5[f], skyMag[f]))
        handles.append(myline)
    plt.plot(atmos.wavelen, atmos.sb, 'k:', label='Atmosphere, X=1.2')
    # Add legend for dark sky.
    myline = mlines.Line2D([], [], color='k', linestyle='-', label='Dark sky AB mags')
    handles.append(myline)
    # end of dark sky legend line
    plt.legend(loc=(0.01, 0.69), handles=handles, fancybox=True, numpoints=1, fontsize='small')
    plt.ylim(0, 1)
    plt.xlim(300, 1100)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Fractional Throughput Response')
    if title == 'Vendor combo':
        title = ''
    plt.title('System total response curves %s' %(title))
    if title == '':
        plt.savefig('throughputs.pdf', format='pdf', dpi=600)
    return m5
def calcM5(hardware, system, atmos, title="m5"):
    effarea = np.pi * (6.423 / 2.0 * 100.0) ** 2
    photParams = PhotometricParameters(effarea=effarea)
    lsstDefaults = LSSTdefaults()
    darksky = Sed()
    darksky.readSED_flambda(os.path.join("../siteProperties", "darksky.dat"))
    flatSed = Sed()
    flatSed.setFlatSED()
    m5 = {}
    sourceCounts = {}
    skyCounts = {}
    skyMag = {}
    gamma = {}
    for f in system:
        m5[f] = SignalToNoise.calcM5(darksky, system[f], hardware[f], photParams, FWHMeff=lsstDefaults.FWHMeff(f))
        fNorm = flatSed.calcFluxNorm(m5[f], system[f])
        flatSed.multiplyFluxNorm(fNorm)
        sourceCounts[f] = flatSed.calcADU(system[f], photParams=photParams)
        # Calculate the Skycounts expected in this bandpass.
        skyCounts[f] = darksky.calcADU(hardware[f], photParams=photParams) * photParams.platescale ** 2
        # Calculate the sky surface brightness.
        skyMag[f] = darksky.calcMag(hardware[f])
        # Calculate the gamma value.
        gamma[f] = SignalToNoise.calcGamma(system[f], m5[f], photParams)
    print title
    print "Filter m5 SourceCounts SkyCounts SkyMag Gamma"
    for f in ("u", "g", "r", "i", "z", "y"):
        print "%s %.2f %.1f %.2f %.2f %.6f" % (f, m5[f], sourceCounts[f], skyCounts[f], skyMag[f], gamma[f])

    # Show what these look like individually (add sky & m5 limits on throughput curves)
    plt.figure()
    ax = plt.gca()
    # Add dark sky
    ax2 = ax.twinx()
    plt.sca(ax2)
    skyab = -2.5 * np.log10(darksky.fnu) - darksky.zp
    ax2.plot(darksky.wavelen, skyab, "k-", linewidth=0.8, label="Dark sky mags")
    ax2.set_ylabel("AB mags")
    ax2.set_ylim(24, 14)
    plt.sca(ax)
    # end of dark sky
    handles = []
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2)
        myline = mlines.Line2D(
            [],
            [],
            color=filtercolors[f],
            linestyle="-",
            linewidth=2,
            label="%s: m5 %.1f (sky %.1f)" % (f, m5[f], skyMag[f]),
        )
        handles.append(myline)
    plt.plot(atmos.wavelen, atmos.sb, "k:", label="Atmosphere, X=1.0 with aerosols")
    # Add legend for dark sky.
    myline = mlines.Line2D([], [], color="k", linestyle="-", label="Dark sky AB mags")
    handles.append(myline)
    # end of dark sky legend line
    plt.legend(loc=(0.01, 0.69), handles=handles, fancybox=True, numpoints=1, fontsize="small")
    plt.ylim(0, 1)
    plt.xlim(300, 1100)
    plt.xlabel("Wavelength (nm)")
    plt.ylabel("Fractional Throughput Response")
    if title == "Vendor combo":
        title = ""
    plt.title("System total response curves %s" % (title))
    plt.savefig("../plots/system+sky" + title + ".png", format="png", dpi=600)
    return m5
Exemplo n.º 39
0
    def Simulate_and_Fit_LC(self, observations, transmission, zmin, zmax):

        #print 'start Simulation',time.time()-self.start_time
        #print 'time',observations['expMJD'],observations['filter']

        #print 'simulate and fit'
        ra = observations[self.fieldRA][0]
        dec = observations[self.fieldDec][0]

        if self.SN.sn_type == 'Ia':
            mbsim = self.SN.SN._source.peakmag('bessellb', 'vega')
        else:
            mbsim = -1

        #This will be the data for sncosmo fitting
        table_for_fit = {}
        table_for_fit['error_coadd_opsim'] = Table(
            names=('time', 'flux', 'fluxerr', 'band', 'zp', 'zpsys'),
            dtype=('f8', 'f8', 'f8', 'S7', 'f4', 'S4'))
        table_for_fit['error_coadd_through'] = Table(
            names=('time', 'flux', 'fluxerr', 'band', 'zp', 'zpsys'),
            dtype=('f8', 'f8', 'f8', 'S7', 'f4', 'S4'))
        """
        table_for_fit['error_opsim'] = Table(names=('time','flux','fluxerr','band','zp','zpsys'), dtype=('f8', 'f8','f8','S7','f4','S4'))
        table_for_fit['error_through'] = Table(names=('time','flux','fluxerr','band','zp','zpsys'), dtype=('f8', 'f8','f8','S7','f4','S4'))
        """
        mytype = [('obsHistID', np.int), ('filtSkyBrightness', np.float),
                  ('airmass', np.float), ('moonPhase', np.float),
                  ('fieldRA', np.float), ('fieldDec', np.float),
                  ('visitExpTime', np.float), ('expDate', np.int),
                  ('filter', np.dtype('a15')), ('fieldID', np.int),
                  ('fiveSigmaDepth', np.float), ('ditheredDec', np.float),
                  ('expMJD', np.float), ('ditheredRA', np.float),
                  ('rawSeeing', np.float), ('flux', np.float),
                  ('err_flux', np.float), ('err_flux_opsim', np.float),
                  ('err_flux_through', np.float), ('finSeeing', np.float),
                  ('katm_opsim', np.float), ('katm_calc', np.float),
                  ('m5_calc', np.float), ('Tb', np.float),
                  ('Sigmab', np.float), ('Cm', np.float), ('dCm', np.float),
                  ('mag_SN', np.float), ('snr_m5_through', np.float),
                  ('snr_m5_opsim', np.float), ('gamma_through', np.float),
                  ('gamma_opsim', np.float), ('snr_SED', np.float)]

        myobservations = np.zeros((60, 1), dtype=mytype)

        #print 'Nobservations',len(observations)
        nobs = -1
        for obs in observations:

            nobs += 1
            filtre = obs['filter']
            if len(myobservations) <= nobs:
                myobservations = np.resize(myobservations,
                                           (len(myobservations) + 100, 1))

            for name in self.cols_restricted:
                myobservations[name][nobs] = obs[name]

            seeing = obs['rawSeeing']
            time_obs = obs['expMJD']
            m5_opsim = obs['fiveSigmaDepth']
            sed_SN = self.SN.get_SED(time_obs)

            transmission.Load_Atmosphere(obs['airmass'])
            flux_SN = sed_SN.calcFlux(
                bandpass=transmission.lsst_atmos_aerosol[filtre])

            myup = 0
            Tb = 0
            Sigmab = 0
            katm = 0

            mbsky_through = 0
            """
            Filter_Wavelength_Correction = np.power(500.0 / self.params.filterWave[filtre], 0.3)
            Airmass_Correction = math.pow(obs['airmass'],0.6)
            FWHM_Sys = self.params.FWHM_Sys_Zenith * Airmass_Correction
            FWHM_Atm = seeing * Filter_Wavelength_Correction * Airmass_Correction
            finSeeing = self.params.scaleToNeff * math.sqrt(np.power(FWHM_Sys,2) + self.params.atmNeffFactor * np.power(FWHM_Atm,2))
            

            Tscale = obs['visitExpTime']/ 30.0 * np.power(10.0, -0.4*(obs['filtSkyBrightness'] - self.params.msky[filtre]))
            dCm = self.params.dCm_infinity[filtre] - 1.25*np.log10(1 + np.power(10.,0.8*self.params.dCm_infinity[filtre]- 1.)/Tscale)

            m5_recalc=dCm+self.params.Cm[filtre]+0.5*(obs['filtSkyBrightness']-21.)+2.5*np.log10(0.7/finSeeing)-self.params.kAtm[filtre]*(obs['airmass']-1.)+1.25*np.log10(obs['visitExpTime']/30.)
            
            """
            """
            myobservations['Cm'][nobs]=self.params.Cm[filtre]
            myobservations['dCm'][nobs]=dCm
            myobservations['finSeeing'][nobs]=finSeeing
            myobservations['Tb'][nobs]=Tb
            myobservations['Sigmab'][nobs]=Sigmab
            myobservations['katm_calc'][nobs]=katm
            myobservations['katm_opsim'][nobs]=self.params.kAtm[filtre] 
            """
            #print 'Flux',time.time()-self.start_time
            if flux_SN > 0:

                wavelen_min, wavelen_max, wavelen_step = transmission.lsst_system[
                    filtre].getWavelenLimits(None, None, None)
                flatSed = Sed()
                flatSed.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
                flux0 = np.power(10., -0.4 * obs['filtSkyBrightness'])
                flatSed.multiplyFluxNorm(flux0)
                mag_SN = -2.5 * np.log10(flux_SN / 3631.0)

                #FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(finSeeing)
                FWHMeff = obs['FWHMeff']
                photParams = PhotometricParameters(nexp=obs['visitExpTime'] /
                                                   15.)

                m5_calc = SignalToNoise.calcM5(
                    flatSed,
                    transmission.lsst_atmos_aerosol[filtre],
                    transmission.lsst_system[filtre],
                    photParams=photParams,
                    FWHMeff=FWHMeff)
                snr_m5_through, gamma_through = SignalToNoise.calcSNR_m5(
                    mag_SN, transmission.lsst_atmos_aerosol[filtre], m5_calc,
                    photParams)
                m5_opsim += 1.25 * np.log10(obs['visitExpTime'] / 30.)
                snr_m5_opsim, gamma_opsim = SignalToNoise.calcSNR_m5(
                    mag_SN, transmission.lsst_atmos_aerosol[filtre], m5_opsim,
                    photParams)

                err_flux_SN = 0
                err_flux_SN_opsim = flux_SN / snr_m5_opsim
                err_flux_SN_through = flux_SN / snr_m5_through

                myobservations['mag_SN'][nobs] = mag_SN
                myobservations['flux'][nobs] = flux_SN
                myobservations['err_flux'][nobs] = err_flux_SN
                myobservations['err_flux_opsim'][nobs] = err_flux_SN_opsim
                myobservations['err_flux_through'][nobs] = err_flux_SN_through
                myobservations['m5_calc'][nobs] = m5_calc
                myobservations['snr_m5_through'][nobs] = snr_m5_through
                myobservations['snr_m5_opsim'][nobs] = snr_m5_opsim
                myobservations['gamma_through'][nobs] = gamma_through
                myobservations['gamma_opsim'][nobs] = gamma_opsim
                #myobservations['snr_SED'][nobs]=snr_SN

                #print 'SNR',flux_SN,flux_SN/err_flux_SN,flux_SN/err_flux_SN_opsim
                #if flux_SN/err_flux_SN >=5:
                #table_for_fit['error_calc'].add_row((time_obs,flux_SN,err_flux_SN,'LSST::'+filtre,25,'ab'))
                #if flux_SN/err_flux_SN_opsim >=5.:
                table_for_fit['error_coadd_opsim'].add_row(
                    (time_obs, flux_SN, err_flux_SN_opsim, 'LSST::' + filtre,
                     25, 'ab'))
                table_for_fit['error_coadd_through'].add_row(
                    (time_obs, flux_SN, err_flux_SN_through, 'LSST::' + filtre,
                     25, 'ab'))
                #print 'Getting fluxes and errors',time.time()-self.thetime,filtre,nobs
            else:
                err_flux_SN = -999.
                err_flux_SN_opsim = -999.
                myobservations['mag_SN'][nobs] = -999
                myobservations['flux'][nobs] = flux_SN
                myobservations['err_flux'][nobs] = -999.
                myobservations['err_flux_opsim'][nobs] = -999.
                myobservations['err_flux_through'][nobs] = -999.
                myobservations['m5_calc'][nobs] = -999.
                myobservations['snr_m5_through'][nobs] = -999
                myobservations['snr_m5_opsim'][nobs] = -999
                myobservations['gamma_through'][nobs] = -999
                myobservations['gamma_opsim'][nobs] = -999
                myobservations['snr_SED'][nobs] = -999

        myobservations = np.resize(myobservations, (nobs + 1, 1))
        """
        print 'Preparing table_for_fit',time.time()-self.start_time
        for band in ['u','g','r','i','z','y']:
            selb=table_for_fit['error_opsim'][np.where(table_for_fit['error_opsim']['band']=='LSST::'+band)]
            selc=table_for_fit['error_through'][np.where(table_for_fit['error_through']['band']=='LSST::'+band)]

            table_for_fit['error_coadd_opsim']=vstack([table_for_fit['error_coadd_opsim'],self.Get_coadd(selb)])
            table_for_fit['error_coadd_through']=vstack([table_for_fit['error_coadd_through'],self.Get_coadd(selc)])
        """

        #print 'There we go fitting',time.time()-self.thetime
        dict_fit = {}
        #for val in ['error_calc','error_coadd_calc','error_opsim','error_coadd_opsim']:

        for val in ['error_coadd_opsim', 'error_coadd_through']:
            #print 'Go for fit',time.time()-self.start_time
            dict_fit[val] = {}
            dict_fit[val]['sncosmo_fitted'] = {}
            dict_fit[val]['table_for_fit'] = table_for_fit[val]
            #print 'fit',val,time.time()-self.thetime
            res, fitted_model, mbfit, fit_status = self.Fit_SN(
                table_for_fit[val], zmin, zmax)
            if res is not None:
                dict_fit[val]['sncosmo_res'] = res
                #self.dict_fit[val]['fitted_model']=fitted_model
                for i, par in enumerate(fitted_model.param_names):
                    dict_fit[val]['sncosmo_fitted'][
                        par] = fitted_model.parameters[i]
                dict_fit[val]['mbfit'] = mbfit
            dict_fit[val]['fit_status'] = fit_status
            #print 'end of Fit',time.time()-self.start_time

        return dict_fit, mbsim, myobservations
Exemplo n.º 40
0
def process_stellar_chunk(chunk, filter_obs, mjd_obs, m5_obs, coadd_m5,
                          obs_md_list, proper_chip, variability_cache,
                          out_data):

    t_start_chunk = time.time()
    #print('processing %d' % len(chunk))
    ct_first = 0
    ct_at_all = 0
    ct_tot = 0

    n_t = len(filter_obs)
    n_obj = len(chunk)

    coadd_visits = {}
    coadd_visits['u'] = 6
    coadd_visits['g'] = 8
    coadd_visits['r'] = 18
    coadd_visits['i'] = 18
    coadd_visits['z'] = 16
    coadd_visits['y'] = 16

    # from the overview paper
    # table 2; take m5 row and add Delta m5 row
    # to get down to airmass 1.2
    m5_single = {}
    m5_single['u'] = 23.57
    m5_single['g'] = 24.65
    m5_single['r'] = 24.21
    m5_single['i'] = 23.79
    m5_single['z'] = 23.21
    m5_single['y'] = 22.31

    gamma_coadd = {}
    for bp in 'ugrizy':
        gamma_coadd[bp] = None

    gamma_single = {}
    for bp in 'ugrizy':
        gamma_single[bp] = [None] * n_t

    dflux = np.zeros((n_obj, n_t), dtype=float)
    dflux_for_mlt(chunk, filter_obs, mjd_obs, variability_cache, dflux)
    dflux_for_kepler(chunk, filter_obs, mjd_obs, variability_cache, dflux)
    dflux_for_rrly(chunk, filter_obs, mjd_obs, variability_cache, dflux)

    dummy_sed = Sed()
    lsst_bp = BandpassDict.loadTotalBandpassesFromFiles()
    flux_q = np.zeros((6, n_obj), dtype=float)
    flux_coadd = np.zeros((6, n_obj), dtype=float)
    mag_coadd = np.zeros((6, n_obj), dtype=float)
    snr_coadd = np.zeros((6, n_obj), dtype=float)
    snr_single = {}
    snr_single_mag_grid = np.arange(14.0, 30.0, 0.05)

    phot_params_single = PhotometricParameters(nexp=1, exptime=30.0)

    t_start_snr = time.time()
    photometry_mask = np.zeros((n_obj, n_t), dtype=bool)
    photometry_mask_1d = np.zeros(n_obj, dtype=bool)

    for i_bp, bp in enumerate('ugrizy'):
        valid_obs = np.where(filter_obs == i_bp)
        phot_params_coadd = PhotometricParameters(nexp=1,
                                                  exptime=30.0 *
                                                  coadd_visits[bp])

        flux_q[i_bp] = dummy_sed.fluxFromMag(chunk['%smag' % bp])
        dflux_sub = dflux[:, valid_obs[0]]
        assert dflux_sub.shape == (n_obj, len(valid_obs[0]))
        dflux_mean = np.mean(dflux_sub, axis=1)
        assert dflux_mean.shape == (n_obj, )
        flux_coadd[i_bp] = flux_q[i_bp] + dflux_mean
        mag_coadd[i_bp] = dummy_sed.magFromFlux(flux_coadd[i_bp])

        (snr_coadd[i_bp], gamma) = SNR.calcSNR_m5(mag_coadd[i_bp], lsst_bp[bp],
                                                  coadd_m5[bp],
                                                  phot_params_coadd)

        (snr_single[bp], gamma) = SNR.calcSNR_m5(snr_single_mag_grid,
                                                 lsst_bp[bp], m5_single[bp],
                                                 phot_params_single)

    #print('got all snr in %e' % (time.time()-t_start_snr))

    t_start_obj = time.time()
    snr_arr = np.zeros((n_obj, n_t), dtype=float)

    for i_bp, bp in enumerate('ugrizy'):
        valid_obs = np.where(filter_obs == i_bp)
        if len(valid_obs[0]) == 0:
            continue
        n_bp = len(valid_obs[0])
        dflux_bp = dflux[:, valid_obs[0]]
        flux0_arr = flux_q[i_bp]
        flux_tot = flux0_arr[:, None] + dflux_bp
        mag_tot = dummy_sed.magFromFlux(flux_tot)
        snr_single_val = np.interp(mag_tot, snr_single_mag_grid,
                                   snr_single[bp])

        noise_coadd = flux_coadd[i_bp] / snr_coadd[i_bp]
        noise_single = flux_tot / snr_single_val
        noise = np.sqrt(noise_coadd[:, None]**2 + noise_single**2)
        dflux_thresh = 5.0 * noise
        dflux_bp = np.abs(dflux_bp)
        detected = (dflux_bp >= dflux_thresh)
        snr_arr[:, valid_obs[0]] = dflux_bp / noise
        for i_obj in range(n_obj):
            if detected[i_obj].any():
                photometry_mask_1d[i_obj] = True
                photometry_mask[i_obj, valid_obs[0]] = detected[i_obj]

    t_before_chip = time.time()
    chip_mask = apply_focal_plane(chunk['ra'], chunk['decl'],
                                  photometry_mask_1d, obs_md_list, filter_obs,
                                  proper_chip)
    duration = (time.time() - t_before_chip) / 3600.0

    for i_obj in range(n_obj):
        if photometry_mask_1d[i_obj]:
            detected = photometry_mask[i_obj, :] & chip_mask[i_obj, :]
            if detected.any():
                unq = chunk['simobjid'][i_obj]
                first_dex = np.where(detected)[0].min()
                out_data[unq] = (mjd_obs[first_dex], snr_arr[i_obj, first_dex],
                                 chunk['var_type'][i_obj])
                if detected[0]:
                    ct_first += 1
                else:
                    ct_at_all += 1
Exemplo n.º 41
0
    def Simulate_LC(self):

        for obs in self.obs:
            filtre = obs['band'].split('::')[1]

            #seeing=obs['rawSeeing']
            time_obs = obs['mjd']
            #m5_opsim=obs['fiveSigmaDepth']
            m5_opsim = obs['m5sigmadepth']
            sed_SN = self.SN.get_SED(time_obs)

            self.transmission.Load_Atmosphere(obs['airmass'])
            flux_SN = sed_SN.calcFlux(
                bandpass=self.transmission.lsst_atmos_aerosol[filtre])

            #visittime=obs['visitExpTime']
            visittime = obs['exptime']

            photParams = PhotometricParameters(nexp=visittime / 15.)
            e_per_sec = sed_SN.calcADU(
                bandpass=self.transmission.lsst_atmos_aerosol[filtre],
                photParams=photParams)  #number of ADU counts for expTime
            e_per_sec /= visittime / photParams.gain
            """
             Filter_Wavelength_Correction = np.power(500.0 / self.params.filterWave[filtre], 0.3)
             Airmass_Correction = math.pow(obs['airmass'],0.6)
             FWHM_Sys = self.params.FWHM_Sys_Zenith * Airmass_Correction
             FWHM_Atm = seeing * Filter_Wavelength_Correction * Airmass_Correction
             finSeeing = self.params.scaleToNeff * math.sqrt(np.power(FWHM_Sys,2) + self.params.atmNeffFactor * np.power(FWHM_Atm,2))
             
             FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(finSeeing)
             """
            #print 'alors pal',finSeeing,FWHMeff,obs['FWHMgeom'],obs['FWHMeff'],SignalToNoise.FWHMgeom2FWHMeff(obs['FWHMgeom'])
            FWHMeff = obs['FWHMeff']
            if flux_SN > 0:

                mag_SN = -2.5 * np.log10(flux_SN / 3631.0)

                #print 'hello',finSeeing,FWHMeff

                m5_calc, snr_m5_through = self.Get_m5(filtre, mag_SN,
                                                      obs['sky'], photParams,
                                                      FWHMeff)

                m5_opsim += 1.25 * np.log10(visittime / 30.)
                snr_m5_opsim, gamma_opsim = SignalToNoise.calcSNR_m5(
                    mag_SN, self.transmission.lsst_atmos_aerosol[filtre],
                    m5_opsim, photParams)

                err_flux_SN_opsim = flux_SN / snr_m5_opsim
                err_flux_SN_through = flux_SN / snr_m5_through

                self.table_for_fit['error_coadd_opsim'].add_row(
                    (time_obs, flux_SN, err_flux_SN_opsim, 'LSST::' + filtre,
                     25, 'ab'))
                self.table_for_fit['error_coadd_through'].add_row(
                    (time_obs, flux_SN, err_flux_SN_through, 'LSST::' + filtre,
                     25, 'ab'))
                """
                 flatSed_err = Sed()
                 flatSed_err.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
                 #flatSed_err.multiplyFluxNorm(err_flux_SN_through)
                 err_mag_SN=-2.5 * np.log10( err_flux_SN_through/ 3631.0)
                 flux0_err=np.power(10.,-0.4*err_mag_SN)
                 flatSed_err.multiplyFluxNorm(flux0_err)
                 e_per_sec_err = flatSed_err.calcADU(bandpass=self.transmission.lsst_atmos_aerosol[filtre], photParams=photParams) #number of ADU counts for expTime
                 e_per_sec_err/=obs['visitExpTime']/2.3
                 """
                #print 'test',e_per_sec_err,e_per_sec/snr_m5_through
                #print 'hello',filtre,obs['expMJD'],obs['visitExpTime'],obs['rawSeeing'],obs['moon_frac'],obs['filtSkyBrightness'],obs['kAtm'],obs['airmass'],obs['fiveSigmaDepth'],obs['Nexp'],e_per_sec,e_per_sec_err,flux_SN,err_flux_SN_through
                self.table_LC.add_row(
                    ('LSST::' + filtre, obs['mjd'], visittime, FWHMeff,
                     obs['moon_frac'], obs['sky'], obs['kAtm'], obs['airmass'],
                     obs['m5sigmadepth'], obs['Nexp'], e_per_sec,
                     e_per_sec / snr_m5_through))
                if self.syste:
                    resu = [
                        'LSST::' + filtre, obs['mjd'], visittime, FWHMeff,
                        obs['moon_frac'], obs['sky'], obs['kAtm'],
                        obs['airmass'], m5_opsim, obs['Nexp'], e_per_sec,
                        e_per_sec / snr_m5_through, mag_SN,
                        mag_SN / snr_m5_through, m5_calc
                    ]

                    for i in range(1, 6, 1):
                        m5_calc_plus, snr_m5_plus = self.Get_m5(
                            filtre, mag_SN, obs['sky'] + float(i) / 10.,
                            photParams, FWHMeff)
                        m5_calc_minus, snr_m5_minus = self.Get_m5(
                            filtre, mag_SN, obs['sky'] - float(i) / 10.,
                            photParams, FWHMeff)
                        resu.append(mag_SN / snr_m5_plus)
                        resu.append(mag_SN / snr_m5_minus)
                        resu.append(m5_calc_plus)
                        resu.append(m5_calc_minus)
                    resu.append(self.z)
                    self.table_LC_syste.add_row(tuple(resu))
            else:
                self.table_LC.add_row(
                    ('LSST::' + filtre, obs['mjd'], visittime, FWHMeff,
                     obs['moon_frac'], obs['sky'], obs['kAtm'], obs['airmass'],
                     obs['m5sigmadepth'], obs['Nexp'], -1., -1.))

        self.table_LC.sort(['filter', 'expMJD'])
Exemplo n.º 42
0
def calcM5(hardware, system, atmos, title='m5', return_t2_values=False):
    """
    Calculate m5 values for all filters in hardware and system.
    Prints all values that go into "table 2" of the overview paper.
    Returns dictionary of m5 values.
    """
    # photParams stores default values for the exposure time, nexp, size of the primary,
    #  readnoise, gain, platescale, etc.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/PhotometricParameters.py
    effarea = np.pi * (6.423/2.*100.)**2
    photParams_zp = PhotometricParameters(exptime=1, nexp=1, gain=1, effarea=effarea,
                                          readnoise=8.8, othernoise=0, darkcurrent=0.2)
    photParams = PhotometricParameters(gain=1.0, effarea=effarea, readnoise=8.8, othernoise=0, darkcurrent=0.2)
    photParams_infinity = PhotometricParameters(gain=1.0, readnoise=0, darkcurrent=0,
                                                othernoise=0, effarea=effarea)
    # lsstDefaults stores default values for the FWHMeff.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/LSSTdefaults.py
    lsstDefaults = LSSTdefaults()
    darksky = Sed()
    darksky.readSED_flambda(os.path.join(getPackageDir('syseng_throughputs'),
                                         'siteProperties', 'darksky.dat'))
    flatSed = Sed()
    flatSed.setFlatSED()
    m5 = {}
    Tb = {}
    Sb = {}
    kAtm = {}
    Cm = {}
    dCm_infinity = {}
    sourceCounts = {}
    skyCounts = {}
    skyMag = {}
    gamma = {}
    zpT = {}
    FWHMgeom = {}
    FWHMeff = {}
    for f in system:
        zpT[f] = system[f].calcZP_t(photParams_zp)
        m5[f] = SignalToNoise.calcM5(darksky, system[f], hardware[f], photParams, FWHMeff=lsstDefaults.FWHMeff(f))
        fNorm = flatSed.calcFluxNorm(m5[f], system[f])
        flatSed.multiplyFluxNorm(fNorm)
        sourceCounts[f] = flatSed.calcADU(system[f], photParams=photParams)
        # Calculate the Skycounts expected in this bandpass.
        skyCounts[f] = (darksky.calcADU(hardware[f], photParams=photParams)
                        * photParams.platescale**2)
        # Calculate the sky surface brightness.
        skyMag[f] = darksky.calcMag(hardware[f])
        # Calculate the gamma value.
        gamma[f] = SignalToNoise.calcGamma(system[f], m5[f], photParams)
        # Calculate the "Throughput Integral" (this is the hardware + atmosphere)
        dwavelen = np.mean(np.diff(system[f].wavelen))
        Tb[f] = np.sum(system[f].sb / system[f].wavelen) * dwavelen
        # Calculate the "Sigma" 'system integral' (this is the hardware only)
        Sb[f] = np.sum(hardware[f].sb / hardware[f].wavelen) * dwavelen
        # Calculate km - atmospheric extinction in a particular bandpass
        kAtm[f] = -2.5*np.log10(Tb[f] / Sb[f])
        # Calculate the Cm and Cm_Infinity values.
        # m5 = Cm + 0.5*(msky - 21) + 2.5log10(0.7/FWHMeff) + 1.25log10(t/30) - km(X-1.0)
        # Assumes atmosphere used in system throughput is X=1.0
        X = 1.0
        Cm[f] = (m5[f] - 0.5*(skyMag[f] - 21) - 2.5*np.log10(0.7/lsstDefaults.FWHMeff(f))
                 - 1.25*np.log10((photParams.exptime*photParams.nexp)/30.0) + kAtm[f]*(X-1.0))
        # Calculate Cm_Infinity by setting readout noise to zero.
        m5inf = SignalToNoise.calcM5(darksky, system[f], hardware[f],  photParams_infinity,
                                     FWHMeff=lsstDefaults.FWHMeff(f))
        Cm_infinity = (m5inf - 0.5*(skyMag[f] - 21) - 2.5*np.log10(0.7/lsstDefaults.FWHMeff(f))
                       - 1.25*np.log10((photParams.exptime*photParams.nexp)/30.0) + kAtm[f]*(X-1.0))
        dCm_infinity[f] = Cm_infinity - Cm[f]
    print 'Filter FWHMeff FWHMgeom SkyMag SkyCounts Zp_t Tb Sb kAtm Gamma Cm dCm_infinity m5 SourceCounts'
    for f in ('u', 'g' ,'r', 'i', 'z', 'y'):
        FWHMeff[f] = lsstDefaults.FWHMeff(f)
        FWHMgeom[f] = SignalToNoise.FWHMeff2FWHMgeom(lsstDefaults.FWHMeff(f))
        print '%s %.2f %.2f %.2f %.1f %.2f %.3f %.3f %.4f %.6f %.2f %.2f %.2f %.2f'\
           % (f, FWHMeff[f], FWHMgeom[f],
              skyMag[f], skyCounts[f], zpT[f], Tb[f], Sb[f], kAtm[f],
              gamma[f], Cm[f], dCm_infinity[f], m5[f], sourceCounts[f])
    if return_t2_values:
        return {'FHWMeff': FWHMeff, 'FWHMgeom': FWHMgeom, 'skyMag': skyMag, 'skycounts': skyCounts,
                'zpT': zpT, 'Tb': Tb, 'Sb': Sb, 'kAtm': kAtm,
                'gamma': gamma, 'Cm': Cm, 'dCm_infinity': dCm_infinity,
                'm5': m5, 'sourceCounts': sourceCounts}

    for f in filterlist:
        m5_cm = Cm[f] + 0.5*(skyMag[f] - 21.0) + 2.5*np.log10(0.7/lsstDefaults.FWHMeff(f))
        if m5_cm - m5[f] > 0.001:
            raise ValueError('Cm calculation for %s band is incorrect! m5_cm != m5_snr' %f)

    # Show what these look like individually (add sky & m5 limits on throughput curves)
    plt.figure()
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2, label=f)
    plt.plot(atmosphere.wavelen, atmosphere.sb, 'k:', label='X=1.0')
    plt.legend(loc='center right', fontsize='smaller')
    plt.xlim(300, 1100)
    plt.ylim(0, 1)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Throughput')
    plt.title('System Throughputs')
    plt.grid(True)
    plt.savefig('../plots/throughputs.png', format='png')

    plt.figure()
    ax = plt.gca()
    # Add dark sky
    ax2 = ax.twinx()
    plt.sca(ax2)
    skyab = np.zeros(len(darksky.fnu))
    condition = np.where(darksky.fnu > 0)
    skyab[condition] = -2.5*np.log10(darksky.fnu[condition]) - darksky.zp
    ax2.plot(darksky.wavelen, skyab,
             'k-', linewidth=0.8, label='Dark sky mags')
    ax2.set_ylabel('AB mags')
    ax2.set_ylim(24, 14)
    plt.sca(ax)
    # end of dark sky
    handles = []
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2)
        myline = mlines.Line2D([], [], color=filtercolors[f], linestyle='-', linewidth=2,
                               label = '%s: m5 %.1f (sky %.1f)' %(f, m5[f], skyMag[f]))
        handles.append(myline)
    plt.plot(atmos.wavelen, atmos.sb, 'k:', label='Atmosphere, X=1.0')
    # Add legend for dark sky.
    myline = mlines.Line2D([], [], color='k', linestyle='-', label='Dark sky AB mags/arcsec^2')
    handles.append(myline)
    # end of dark sky legend line
    plt.legend(loc=(0.01, 0.69), handles=handles, fancybox=True, numpoints=1, fontsize='small')
    plt.ylim(0, 1)
    plt.xlim(300, 1100)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Fractional Throughput Response')
    plt.title('System total response curves %s' %(title))
    plt.savefig('../plots/system+sky' + title + '.png', format='png', dpi=600)
    return m5
Exemplo n.º 43
0
            #plt.plot(sed_SNb.wavelen*(1.+redshift)/(1.+redshiftb),ratio_cosmo*sed_SNb.flambda,linestyle='-',color='g')

        Filter_Wavelength_Correction = np.power(500.0 / filterWave[band], 0.3)
        Airmass_Correction = math.pow(obs['airmass'], 0.6)
        FWHM_Sys = FWHM_Sys_Zenith * Airmass_Correction
        FWHM_Atm = seeing * Filter_Wavelength_Correction * Airmass_Correction
        finSeeing = scaleToNeff * math.sqrt(
            np.power(FWHM_Sys, 2) + atmNeffFactor * np.power(FWHM_Atm, 2))

        #print 'flux_SN',band,flux_SN
        if flux_SN > 0:

            mag_SN = -2.5 * np.log10(flux_SN)

            plt.show()
            FWHMeff = SignalToNoise.FWHMgeom2FWHMeff(finSeeing)
            photParams = PhotometricParameters()
            snr_SN = SignalToNoise.calcSNR_sed(
                sed_SN,
                transmission.lsst_atmos_aerosol[band],
                transmission.darksky,
                transmission.lsst_system[band],
                photParams,
                FWHMeff=FWHMeff,
                verbose=False)
            m5_calc = SignalToNoise.calcM5(
                transmission.darksky,
                transmission.lsst_atmos_aerosol[band],
                transmission.lsst_system[band],
                photParams=photParams,
                FWHMeff=FWHMeff)
Exemplo n.º 44
0
def process_stellar_chunk(chunk, filter_obs, mjd_obs, m5_obs,
                          coadd_m5, m5_single, obs_md_list, proper_chip,
                          variability_cache, out_data, lock):

    t_start_chunk = time.time()
    #print('processing %d' % len(chunk))
    ct_first = 0
    ct_at_all = 0
    ct_tot = 0

    n_t = len(filter_obs)
    n_obj = len(chunk)

    coadd_visits = {}
    coadd_visits['u'] = 6
    coadd_visits['g'] = 8
    coadd_visits['r'] = 18
    coadd_visits['i'] = 18
    coadd_visits['z'] = 16
    coadd_visits['y'] = 16

    gamma_coadd = {}
    for bp in 'ugrizy':
        gamma_coadd[bp] = None

    gamma_single = {}
    for bp in 'ugrizy':
       gamma_single[bp] = [None]*n_t

    dflux = np.zeros((n_obj,n_t), dtype=float)
    dflux_for_mlt(chunk, filter_obs, mjd_obs, variability_cache, dflux)
    dflux_for_kepler(chunk, filter_obs, mjd_obs, variability_cache, dflux)
    dflux_for_rrly(chunk, filter_obs, mjd_obs, variability_cache, dflux)

    dummy_sed = Sed()
    lsst_bp = BandpassDict.loadTotalBandpassesFromFiles()
    flux_q = np.zeros((6,n_obj), dtype=float)
    flux_coadd = np.zeros((6,n_obj), dtype=float)
    mag_coadd = np.zeros((6,n_obj), dtype=float)
    snr_coadd = np.zeros((6,n_obj), dtype=float)
    snr_single = {}
    snr_single_mag_grid = np.arange(14.0, 30.0, 0.05)

    phot_params_single = PhotometricParameters(nexp=1,
                                               exptime=30.0)

    t_start_snr = time.time()
    photometry_mask = np.zeros((n_obj, n_t), dtype=bool)
    photometry_mask_1d = np.zeros(n_obj, dtype=bool)

    for i_bp, bp in enumerate('ugrizy'):
        valid_obs = np.where(filter_obs == i_bp)
        phot_params_coadd = PhotometricParameters(nexp=1,
                                                  exptime=30.0*coadd_visits[bp])

        flux_q[i_bp] = dummy_sed.fluxFromMag(chunk['%smag' % bp])
        dflux_sub = dflux[:,valid_obs[0]]
        assert dflux_sub.shape == (n_obj, len(valid_obs[0]))
        dflux_mean = np.mean(dflux_sub, axis=1)
        assert dflux_mean.shape==(n_obj,)
        flux_coadd[i_bp] = flux_q[i_bp]+dflux_mean
        mag_coadd[i_bp] = dummy_sed.magFromFlux(flux_coadd[i_bp])

        (snr_coadd[i_bp],
         gamma) = SNR.calcSNR_m5(mag_coadd[i_bp],
                                 lsst_bp[bp],
                                 coadd_m5[bp],
                                 phot_params_coadd)


        (snr_single[bp],
         gamma) = SNR.calcSNR_m5(snr_single_mag_grid,
                                 lsst_bp[bp],
                                 m5_single[bp],
                                 phot_params_single)

    #print('got all snr in %e' % (time.time()-t_start_snr))


    t_start_obj = time.time()
    snr_arr = np.zeros((n_obj, n_t), dtype=float)

    for i_bp, bp in enumerate('ugrizy'):
        valid_obs = np.where(filter_obs==i_bp)
        if len(valid_obs[0])==0:
            continue
        n_bp = len(valid_obs[0])
        dflux_bp = dflux[:,valid_obs[0]]
        flux0_arr = flux_q[i_bp]
        flux_tot = flux0_arr[:,None] + dflux_bp
        mag_tot = dummy_sed.magFromFlux(flux_tot)
        snr_single_val = np.interp(mag_tot,
                                  snr_single_mag_grid,
                                  snr_single[bp])

        noise_coadd = flux_coadd[i_bp]/snr_coadd[i_bp]
        noise_single = flux_tot/snr_single_val
        noise = np.sqrt(noise_coadd[:,None]**2+noise_single**2)
        dflux_thresh = 5.0*noise
        dflux_bp = np.abs(dflux_bp)
        detected = (dflux_bp>=dflux_thresh)
        snr_arr[:,valid_obs[0]] = dflux_bp/noise
        for i_obj in range(n_obj):
            if detected[i_obj].any():
                photometry_mask_1d[i_obj] = True
                photometry_mask[i_obj,valid_obs[0]] = detected[i_obj]

    t_before_chip = time.time()
    chip_mask = apply_focal_plane(chunk['ra'], chunk['decl'],
                                  photometry_mask_1d, obs_md_list,
                                  filter_obs, proper_chip)
    duration = (time.time()-t_before_chip)/3600.0

    unq_out = -1*np.ones(n_obj, dtype=int)
    mjd_out = -1.0*np.ones(n_obj, dtype=float)
    snr_out = -1.0*np.ones(n_obj, dtype=float)
    var_type_out = -1*np.ones(n_obj, dtype=int)
    for i_obj in range(n_obj):
        if photometry_mask_1d[i_obj]:
            detected = photometry_mask[i_obj,:] & chip_mask[i_obj,:]
            if detected.any():
                unq_out[i_obj] = chunk['simobjid'][i_obj]
                first_dex = np.where(detected)[0].min()
                mjd_out[i_obj] = mjd_obs[first_dex]
                snr_out[i_obj] = snr_arr[i_obj, first_dex]
                var_type_out[i_obj] = chunk['var_type'][i_obj]

    valid = np.where(unq_out>=0)
    unq_out = unq_out[valid]
    mjd_out = mjd_out[valid]
    var_type_out = var_type_out[valid]
    snr_out = snr_out[valid]

    with lock:
        existing_keys = list(out_data.keys())
        key_val = 0
        if len(existing_keys)>0:
            key_val = max(existing_keys)

        while key_val in existing_keys:
            key_val += 1

        out_data[key_val] = (None, None, None, None)

    out_data[key_val] = (unq_out, mjd_out,
                         snr_out, var_type_out)
Exemplo n.º 45
0
def calcM5(hardware, system, atmos, title='m5', X=1.0, return_t2_values=False):
    """
    Calculate m5 values for all filters in hardware and system.
    Prints all values that go into "table 2" of the overview paper.
    Returns dictionary of m5 values.
    """
    # photParams stores default values for the exposure time, nexp, size of the primary,
    #  readnoise, gain, platescale, etc.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/PhotometricParameters.py
    effarea = np.pi * (6.423/2.*100.)**2
    photParams_zp = PhotometricParameters(exptime=1, nexp=1, gain=1, effarea=effarea,
                                          readnoise=8.8, othernoise=0, darkcurrent=0.2)
    photParams = PhotometricParameters(gain=1.0, effarea=effarea, readnoise=8.8, othernoise=0, darkcurrent=0.2)
    photParams_infinity = PhotometricParameters(gain=1.0, readnoise=0, darkcurrent=0,
                                                othernoise=0, effarea=effarea)
    # lsstDefaults stores default values for the FWHMeff.
    # See https://github.com/lsst/sims_photUtils/blob/master/python/lsst/sims/photUtils/LSSTdefaults.py
    lsstDefaults = LSSTdefaults()
    darksky = Sed()
    darksky.readSED_flambda(os.path.join(getPackageDir('syseng_throughputs'),
                                         'siteProperties', 'darksky.dat'))
    flatSed = Sed()
    flatSed.setFlatSED()
    m5 = {}
    Tb = {}
    Sb = {}
    kAtm = {}
    Cm = {}
    dCm_infinity = {}
    sourceCounts = {}
    skyCounts = {}
    skyMag = {}
    gamma = {}
    zpT = {}
    FWHMgeom = {}
    FWHMeff = {}
    for f in system:
        zpT[f] = system[f].calcZP_t(photParams_zp)
        eff_wavelen = system[f].calcEffWavelen()[1]
        FWHMeff[f] = scale_seeing(0.62, eff_wavelen, X)[0]
        m5[f] = SignalToNoise.calcM5(darksky, system[f], hardware[f], photParams, FWHMeff=FWHMeff[f])
        fNorm = flatSed.calcFluxNorm(m5[f], system[f])
        flatSed.multiplyFluxNorm(fNorm)
        sourceCounts[f] = flatSed.calcADU(system[f], photParams=photParams)
        # Calculate the Skycounts expected in this bandpass.
        skyCounts[f] = (darksky.calcADU(hardware[f], photParams=photParams)
                        * photParams.platescale**2)
        # Calculate the sky surface brightness.
        skyMag[f] = darksky.calcMag(hardware[f])
        # Calculate the gamma value.
        gamma[f] = SignalToNoise.calcGamma(system[f], m5[f], photParams)
        # Calculate the "Throughput Integral" (this is the hardware + atmosphere)
        dwavelen = np.mean(np.diff(system[f].wavelen))
        Tb[f] = np.sum(system[f].sb / system[f].wavelen) * dwavelen
        # Calculate the "Sigma" 'system integral' (this is the hardware only)
        Sb[f] = np.sum(hardware[f].sb / hardware[f].wavelen) * dwavelen
        # Calculate km - atmospheric extinction in a particular bandpass
        kAtm[f] = -2.5*np.log10(Tb[f] / Sb[f])
        # Calculate the Cm and Cm_Infinity values.
        # m5 = Cm + 0.5*(msky - 21) + 2.5log10(0.7/FWHMeff) + 1.25log10(t/30) - km(X-1.0)
        # Assumes atmosphere used in system throughput is X=1.0
        Cm[f] = (m5[f] - 0.5*(skyMag[f] - 21) - 2.5*np.log10(0.7/FWHMeff[f])
                 - 1.25*np.log10((photParams.exptime*photParams.nexp)/30.0) + kAtm[f]*(X-1.0))
        # Calculate Cm_Infinity by setting readout noise to zero.
        m5inf = SignalToNoise.calcM5(darksky, system[f], hardware[f],  photParams_infinity,
                                     FWHMeff=FWHMeff[f])
        Cm_infinity = (m5inf - 0.5*(skyMag[f] - 21) - 2.5*np.log10(0.7/FWHMeff[f])
                       - 1.25*np.log10((photParams.exptime*photParams.nexp)/30.0) + kAtm[f]*(X-1.0))
        dCm_infinity[f] = Cm_infinity - Cm[f]
    print('Filter FWHMeff FWHMgeom SkyMag SkyCounts Zp_t Tb Sb kAtm Gamma Cm dCm_infinity m5 SourceCounts')
    for f in ('u', 'g' ,'r', 'i', 'z', 'y'):
        FWHMgeom[f] = SignalToNoise.FWHMeff2FWHMgeom(FWHMeff[f])
        print('%s %.2f %.2f %.2f %.1f %.2f %.3f %.3f %.4f %.6f %.2f %.2f %.2f %.2f'\
           % (f, FWHMeff[f], FWHMgeom[f],
              skyMag[f], skyCounts[f], zpT[f], Tb[f], Sb[f], kAtm[f],
              gamma[f], Cm[f], dCm_infinity[f], m5[f], sourceCounts[f]))

    for f in filterlist:
        m5_cm = Cm[f] + 0.5*(skyMag[f] - 21.0) + 2.5*np.log10(0.7/FWHMeff[f]) - kAtm[f]*(X-1.0)
        if m5_cm - m5[f] > 0.001:
            raise ValueError('Cm calculation for %s band is incorrect! m5_cm != m5_snr' %f)

    if return_t2_values:
        return {'FWHMeff': FWHMeff, 'FWHMgeom': FWHMgeom, 'skyMag': skyMag, 'skycounts': skyCounts,
                'zpT': zpT, 'Tb': Tb, 'Sb': Sb, 'kAtm': kAtm,
                'gamma': gamma, 'Cm': Cm, 'dCm_infinity': dCm_infinity,
                'm5': m5, 'sourceCounts': sourceCounts}

    # Show what these look like individually (add sky & m5 limits on throughput curves)
    plt.figure()
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2, label=f)
    plt.plot(atmos.wavelen, atmos.sb, 'k:', label='X=1.0')
    plt.legend(loc='center right', fontsize='smaller')
    plt.xlim(300, 1100)
    plt.ylim(0, 1)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Throughput')
    plt.title('System Throughputs')
    plt.grid(True)
    plt.savefig('../plots/throughputs.png', format='png')

    plt.figure()
    ax = plt.gca()
    # Add dark sky
    ax2 = ax.twinx()
    plt.sca(ax2)
    skyab = np.zeros(len(darksky.fnu))
    condition = np.where(darksky.fnu > 0)
    skyab[condition] = -2.5*np.log10(darksky.fnu[condition]) - darksky.zp
    ax2.plot(darksky.wavelen, skyab,
             'k-', linewidth=0.8, label='Dark sky mags')
    ax2.set_ylabel('AB mags')
    ax2.set_ylim(24, 14)
    plt.sca(ax)
    # end of dark sky
    handles = []
    for f in filterlist:
        plt.plot(system[f].wavelen, system[f].sb, color=filtercolors[f], linewidth=2)
        myline = mlines.Line2D([], [], color=filtercolors[f], linestyle='-', linewidth=2,
                               label = '%s: m5 %.1f (sky %.1f)' %(f, m5[f], skyMag[f]))
        handles.append(myline)
    plt.plot(atmos.wavelen, atmos.sb, 'k:', label='Atmosphere, X=1.0')
    # Add legend for dark sky.
    myline = mlines.Line2D([], [], color='k', linestyle='-', label='Dark sky AB mags/arcsec^2')
    handles.append(myline)
    # end of dark sky legend line
    plt.legend(loc=(0.01, 0.69), handles=handles, fancybox=True, numpoints=1, fontsize='small')
    plt.ylim(0, 1)
    plt.xlim(300, 1100)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Fractional Throughput Response')
    plt.title('System total response curves %s' %(title))
    plt.savefig('../plots/system+sky' + title + '.png', format='png', dpi=600)
    return m5