Exemplo n.º 1
0
 def _calcColors(self, sedname='C.dat'):
     """
     Calculate the colors for a moving object with sed 'sedname'.
     """
     # Do we need to read in the LSST bandpasses?
     try:
         self.lsst
     except AttributeError:
         filterdir = os.getenv('LSST_THROUGHPUTS_BASELINE')
         filterlist = ('u', 'g', 'r', 'i', 'z', 'y')
         self.lsst ={}
         for f in filterlist:
             self.lsst[f] = Bandpass()
             self.lsst[f].readThroughput(os.path.join(filterdir, 'total_'+f+'.dat'))
         self.vband = Bandpass()
         self.vband.readThroughput('harris_V.dat')
         self.colors = {}
     # See if the sed's colors are in memory already.
     if sedname not in self.colors:
         moSed = Sed()
         moSed.readSED_flambda(sedname)
         vmag = moSed.calcMag(self.vband)
         self.colors[sedname] = {}
         for f in filterlist:
             self.colors[sedname][f] = moSed.calcMag(self.lsst[f]) - vmag
     return self.colors[sedname]
Exemplo n.º 2
0
def calcEffWavelen(hardware, title, throughputDir=None):
    photParams = PhotometricParameters()
    lsstDefaults = LSSTdefaults()
    atmos = {}
    stdAtmoFile = os.path.join(os.getenv('SYSENG_THROUGHPUTS_DIR'),
                               'siteProperties/pachonModtranAtm_12.dat')
    atmos['std'] = Bandpass()
    atmos['std'].readThroughput(stdAtmoFile)
    multiAtmos = False
    if throughputDir is None:
        throughputDir = os.getenv('THROUGHPUTS_DIR')
    if throughputDir is not None:
        multiAtmos = True
        Xarr = np.arange(1.0, 2.55, 0.1)
        for X in Xarr:
            atmos['%.1f' % X] = Bandpass()
            atmos['%.1f' % X].readThroughput(
                os.path.join(throughputDir,
                             'atmos/atmos_%d.dat' % (int(X * 10))))

    atmoskeys = sorted(atmos.keys())
    print title
    print '    %s' % ('    '.join(atmoskeys))
    system = Bandpass()
    effsb = {}
    for f in ['u', 'g', 'r', 'i', 'z', 'y']:
        writestring = '%s ' % f
        for k in atmoskeys:
            system.wavelen, system.sb = hardware[f].multiplyThroughputs(
                atmos[k].wavelen, atmos[k].sb)
            effphi, effsb[k] = system.calcEffWavelen()
            writestring += '%.2f ' % (effsb[k])
        print writestring
Exemplo n.º 3
0
def _get_sed_mags_and_cosmology(catalog_name):
    """
    Returns 3 numpy arrays: sed_names, sed_mag_list, sed_mag_norm
    and a dictionary for cosmology
    sed_names is 1d str array, with length = number of SED files in the library
    sed_mag_list is MxN float array, with M = number of SED files in the library,
        and N = number of top hat filters in the catalog
    sed_mag_norm is 1d float array, with length = number of SED files in the library
    cosmology = {'H0': H0, 'Om0': Om0}
    """
    gc = GCRCatalogs.load_catalog(catalog_name,
                                  config_overwrite=dict(md5=False))
    cosmo = dict(H0=gc.cosmology.H0.value, Om0=gc.cosmology.Om0)

    bp_params_raw = {'disk': set(), 'bulge': set()}
    for q in gc.list_all_quantities():
        m = _gcr_sed_re.match(q)
        if m:
            wav0, width, tag = m.groups()
            bp_params_raw[tag].add((int(wav0), int(width)))
    assert bp_params_raw['disk'] == bp_params_raw[
        'bulge'], 'SEDs for disk and bulge do not match'
    assert bp_params_raw['disk'], 'No SED found'

    bp_params_sorted = sorted(bp_params_raw['disk'], key=lambda p: p[0])

    # SED labels in GCR specify the band pass in angstrom, but CatSim uses nm
    # Hence the conversion factor 0.1 in the code below
    wav_min = bp_params_sorted[0][0] * 0.1
    wav_max = max((0.1 * (wav0 + width) for wav0, width in bp_params_sorted))
    wav_grid = np.arange(wav_min, wav_max, 0.1)

    bp_name_list = list()
    bp_list = list()
    for wav0, width in bp_params_sorted:
        sb_grid = ((wav_grid >= wav0 * 0.1) &
                   (wav_grid <= (wav0 + width) * 0.1)).astype(float)
        bp_list.append(Bandpass(wavelen=wav_grid, sb=sb_grid))
        bp_name_list.append('%d_%d' % (wav0, width))

    bandpass_dict = BandpassDict(bp_list, bp_name_list)

    sed_names = list()
    sed_mag_list = list()
    sed_mag_norm = list()

    imsim_bp = Bandpass()
    imsim_bp.imsimBandpass()

    for sed_file_name in os.listdir(_galaxy_sed_dir):
        spec = Sed()
        spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name))
        sed_names.append(sed_file_name)
        sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec)))
        sed_mag_norm.append(spec.calcMag(imsim_bp))

    return np.array(sed_names), np.array(sed_mag_list), np.array(
        sed_mag_norm), cosmo
Exemplo n.º 4
0
 def setUp(self):
     self.normband = Bandpass()
     self.normband.imsimBandpass()
     self.uband = Bandpass()
     self.uband.readThroughput(os.path.join(getPackageDir('sims_photUtils'), 'tests',
                               'cartoonSedTestData', 'test_bandpass_u.dat'))
     self.gband = Bandpass()
     self.gband.readThroughput(os.path.join(getPackageDir('sims_photUtils'), 'tests',
                               'cartoonSedTestData', 'test_bandpass_g.dat'))
Exemplo n.º 5
0
    def testLoadBandpassesFromFiles(self):
        """
        Test that running the classmethod loadBandpassesFromFiles produces
        expected result
        """

        fileDir = os.path.join(getPackageDir('sims_photUtils'),
                               'tests', 'cartoonSedTestData')
        bandpassNames = ['g', 'z', 'i']
        bandpassRoot = 'test_bandpass_'
        componentList = ['toy_mirror.dat']
        atmo = os.path.join(fileDir, 'toy_atmo.dat')

        bandpassDict, hardwareDict = BandpassDict.loadBandpassesFromFiles(bandpassNames=bandpassNames,
                                                                          filedir=fileDir,
                                                                          bandpassRoot=bandpassRoot,
                                                                          componentList=componentList,
                                                                          atmoTransmission=atmo)

        controlBandpassList = []
        controlHardwareList = []

        for bpn in bandpassNames:
            componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'),
                             os.path.join(fileDir, 'toy_mirror.dat')]

            dummyBp = Bandpass()
            dummyBp.readThroughputList(componentList)
            controlHardwareList.append(dummyBp)

            componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'),
                             os.path.join(fileDir, 'toy_mirror.dat'),
                             os.path.join(fileDir, 'toy_atmo.dat')]

            dummyBp = Bandpass()
            dummyBp.readThroughputList(componentList)
            controlBandpassList.append(dummyBp)

        wMin = controlBandpassList[0].wavelen[0]
        wMax = controlBandpassList[0].wavelen[-1]
        wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0]

        for bp, hh in zip(controlBandpassList, controlHardwareList):
            bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax,
                                wavelen_step=wStep)
            hh.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax,
                                wavelen_step=wStep)

        for test, control in zip(bandpassDict.values(), controlBandpassList):
            np.testing.assert_array_almost_equal(test.wavelen,
                                                 control.wavelen, 19)
            np.testing.assert_array_almost_equal(test.sb, control.sb, 19)

        for test, control in zip(hardwareDict.values(), controlHardwareList):
            np.testing.assert_array_almost_equal(test.wavelen,
                                                 control.wavelen, 19)
            np.testing.assert_array_almost_equal(test.sb, control.sb, 19)
Exemplo n.º 6
0
    def Load_System(self):

        for f in self.filterlist:
            self.lsst_std[f] = Bandpass()
            #self.lsst_std[f].readThroughput(os.path.join(self.throughputsDir, 'total_'+f+'.dat'))
            self.lsst_system[f] = Bandpass()
            self.lsst_system[f].readThroughputList([
                'detector.dat', 'lens1.dat', 'lens2.dat', 'lens3.dat',
                'm1.dat', 'm2.dat', 'm3.dat', 'filter_' + f + '.dat'
            ],
                                                   rootDir=self.throughputsDir)
Exemplo n.º 7
0
    def readFilters(self,
                    filterDir=None,
                    bandpassRoot='total_',
                    bandpassSuffix='.dat',
                    filterlist=('u', 'g', 'r', 'i', 'z', 'y'),
                    vDir=None,
                    vFilter='harris_V.dat'):
        """
        Read (LSST) and Harris (V) filters. 
        
        Only the defaults are LSST specific; this can easily be adapted for any survey. 

        Parameters
        ----------
        filterDir : str, opt
            Directory containing the filter throughput curves ('total_*.dat')
            Default set by 'LSST_THROUGHPUTS_BASELINE' env variable.
        bandpassRoot : str, opt
            Rootname of the throughput curves in filterlist. 
            E.g. throughput curve names are bandpassRoot + filterlist[i] + bandpassSuffix
            Default 'total_' (appropriate for LSST throughput repo).
        bandpassSuffix : str, opt
            Suffix for the throughput curves in filterlist.
            Default '.dat' (appropriate for LSST throughput repo).
        filterlist : list, opt
            List containing the filter names to use to calculate colors.
            Default ('u', 'g', 'r', 'i', 'z', 'y')
        vDir : str, opt
            Directory containing the V band throughput curve.
            Default None = $SIMS_MOVINGOBJECTS_DIR/data.
        vFilter : str, opt
            Name of the V band filter curve. 
            Default harris_V.dat.
        """
        if filterDir is None:
            filterDir = os.getenv('LSST_THROUGHPUTS_BASELINE')
        if filterDir is None:
            raise ValueError(
                'Please set filterDir or env variable LSST_THROUGHPUTS_BASELINE'
            )
        if vDir is None:
            vDir = os.path.join(getPackageDir('SIMS_MOVINGOBJECTS'), 'data')
        self.filterlist = filterlist
        # Read filter throughput curves from disk.
        self.lsst = {}
        for f in self.filterlist:
            self.lsst[f] = Bandpass()
            self.lsst[f].readThroughput(
                os.path.join(filterDir, bandpassRoot + f + bandpassSuffix))
        self.vband = Bandpass()
        self.vband.readThroughput(os.path.join(vDir, vFilter))
def _create_sed_library_mags(wav_min, wav_width):
    """
    Calculate the magnitudes of the SEDs in sims_sed_library dir in the
    tophat filters specified by wav_min, wav_width

    Parameters
    ----------
    wav_min is a numpy array of the minimum wavelengths of the tophat
    filters (in nm)

    wav_width is a numpy array of the widths of the tophat filters (in nm)

    Returns
    -------
    sed_names is an array containing the names of the SED files

    sed_mag_list is MxN float array, with M = number of SED files in the library,
        and N = number of top hat filters in the catalog

    sed_mag_norm is 1d float array, with length = number of SED files in the library
    """

    wav_max = max((wav0 + width for wav0, width in zip(wav_min, wav_width)))
    wav_grid = np.arange(wav_min.min(), wav_max, 0.1)

    bp_name_list = list()
    bp_list = list()
    for wav0, width in zip(wav_min, wav_width):
        sb_grid = ((wav_grid >= wav0) & (wav_grid <=
                                         (wav0 + width))).astype(float)
        bp_list.append(Bandpass(wavelen=wav_grid, sb=sb_grid))
        bp_name_list.append('%d_%d' % (wav0, width))

    bandpass_dict = BandpassDict(bp_list, bp_name_list)

    sed_names = list()
    sed_mag_list = list()
    sed_mag_norm = list()

    imsim_bp = Bandpass()
    imsim_bp.imsimBandpass()

    for sed_file_name in os.listdir(_galaxy_sed_dir):
        spec = Sed()
        spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name))
        sed_names.append(defaultSpecMap[sed_file_name])
        sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec)))
        sed_mag_norm.append(spec.calcMag(imsim_bp))

    return np.array(sed_names), np.array(sed_mag_list), np.array(sed_mag_norm)
Exemplo n.º 9
0
    def Load_System(self):

        for f in self.filterlist:
            self.lsst_std[f] = Bandpass()
            #self.lsst_std[f].readThroughput(os.path.join(self.throughputsDir, 'total_'+f+'.dat'))
            self.lsst_system[f] = Bandpass()
            index = [
                i for i, x in enumerate(self.filter_files) if f + '.dat' in x
            ]
            self.lsst_system[f].readThroughputList(
                self.telescope_files + [self.filter_files[index[0]]],
                rootDir=self.throughputsDir,
                wavelen_min=self.wave_min,
                wavelen_max=self.wave_max)
Exemplo n.º 10
0
def buildHardwareAndSystem(defaultDirs, addLosses=True, atmosphereOverride=None, shiftFilters=None):
    """
    Using directories for each component set by 'defaultDirs',
     builds the system (including atmosphere) and hardware throughput curves for each filter.
     Allows optional override of the default atmosphere by one passed in atmosphereOverride (a bandpass object).
    Returns dictionaries of the hardware and system in bandpass objects, keyed per filtername.

    defaultDirs is a dictionary containing the directories of each throughput component:
      it can be built automatically using the setDefaultDirs function.
    For each component, the relevant method above is called - to build the 'lens1' component, buildLens is called, etc.
    The detector is built using 'buildDetector':
      if the directory defaultDirs['detector'] points to multiple subdirectories
       (one per vendor) then the detector response curve corresponds to the minimum
       of all curves at each wavelength.
      if defaultsDirs['detector'] points to a single vendor directory
        (with a *QE.dat curve present), then this is treated a single vendor directory.
    The value of addLosses is passed to each component as it is built (i.e. losses will be multiplied into each throughput curve).
    """
    # Build each component.
    detector = buildDetector(defaultDirs['detector'], addLosses)
    lens1 = buildLens(defaultDirs['lens1'], addLosses)
    lens2 = buildLens(defaultDirs['lens2'], addLosses)
    lens3 = buildLens(defaultDirs['lens3'], addLosses)
    filters = buildFilters(defaultDirs['filters'], addLosses, shiftFilters=shiftFilters)
    mirror1 = buildMirror(defaultDirs['mirror1'], addLosses)
    mirror2 = buildMirror(defaultDirs['mirror2'], addLosses)
    mirror3 = buildMirror(defaultDirs['mirror3'], addLosses)
    if atmosphereOverride is None:
        atmosphere = readAtmosphere(defaultDirs['atmosphere'])
    else:
        atmosphere = atmosphereOverride
        if np.all(atmosphere.wavelen != detector.wavelen):
            atmosphere.resampleBandpass(wavelen_min = detector.wavelen_min,
                                        wavelen_max = detector.wavelen_max,
                                        wavelen_step = detector.wavelen_step)
    # Combine the individual components.
    # Note that the process of reading in the files above would have put them onto the same wavelength grid.
    core_sb = (detector.sb * lens1.sb * lens2.sb * lens3.sb
               * mirror1.sb * mirror2.sb * mirror3.sb)
    wavelen = detector.wavelen
    hardware = {}
    system = {}
    for f in filters:
        hardware[f] = Bandpass()
        system[f] = Bandpass()
        hw_sb = core_sb * filters[f].sb
        hardware[f].setBandpass(wavelen, hw_sb)
        system[f].setBandpass(wavelen, hw_sb*atmosphere.sb)
    return hardware, system
Exemplo n.º 11
0
    def testWavelenMatch(self):
        """
        Test that when you load bandpasses sampled over different
        wavelength grids, they all get sampled to the same wavelength
        grid.
        """
        dwavList = numpy.arange(5.0,25.0,5.0)
        bpList = []
        bpNameList = []
        for ix, dwav in enumerate(dwavList):
            name = 'bp_%d' % ix
            wavelen = numpy.arange(10.0, 1500.0, dwav)
            sb = numpy.exp(-0.5*(numpy.power((wavelen-100.0*ix)/100.0,2)))
            bp = Bandpass(wavelen=wavelen, sb=sb)
            bpList.append(bp)
            bpNameList.append(name)

        # First make sure that we have created distinct wavelength grids
        for ix in range(len(bpList)):
            for iy in range(ix+1,len(bpList)):
                self.assertTrue(len(bpList[ix].wavelen)!=len(bpList[iy].wavelen))

        testDict = BandpassDict(bpList, bpNameList)

        # Now make sure that the wavelength grids in the dict were resampled, but that
        # the original wavelength grids were not changed
        for ix in range(len(bpList)):
            numpy.testing.assert_array_almost_equal(testDict.values()[ix].wavelen, testDict.wavelenMatch, 19)
            if ix!=0:
                self.assertTrue(len(testDict.wavelenMatch)!=len(bpList[ix].wavelen))
Exemplo n.º 12
0
def importBandpass(filename, name):
    '''Given the name of a bandpass read it from file into Bandpass class
    assumes filters are differentiated by #NAME filter_name filter comments'''
    filter = Bandpass()
    data = []
    with open(filename, 'r') as input:
        for line in input:
            line = line.strip().split()
            if (line[0].startswith("#NAME") and (line[1] == name)):
                for line in input:
                    if line.startswith("#NAME"):
                        break
                    else:
                        data.append(map(float, line.split()))
    data = np.asarray(data).T
    # convert to nanometers and set limits for wavelength
    filter.setWavelenLimits(data[0].min() / 10., data[0].max() / 10., 1.)
    try:
        filter.setBandpass(data[0] / 10., data[1])
    except IndexError:
        print "Unable to find data values for ", name
        exit()
    #resample to
    filter.resampleBandpass(wavelen_min=300.0,
                            wavelen_max=1150.0,
                            wavelen_step=0.1)
    return filter
Exemplo n.º 13
0
def mCalcs(airmass, bandName, ra, dec, expMJD,  FWHMeff, hwbpdict, photparams=None, sm=None):
    """
    sm : 
    """
    if photparams is None:
        photparams = PhotometricParameters()
    if sm is None:
        sm = SkyModel(observatory='LSST', mags=False, preciseAltAz=True)
    # Obtain full sky transmission at airmass
    # Note that this method is not interpolating but choosing the atmospheric transmission from
    # Modtran simulations of the closest airmass in a sequence of np.arange(1., 2.51, 0.1)
    fname = atmTransName(airmass)
    print(fname)
    atmTrans = np.loadtxt(fname)
    wave, trans = hwbpdict[bandName].multiplyThroughputs(atmTrans[:, 0], atmTrans[:, 1])
    bp = Bandpass(wavelen=wave, sb=trans)
    # Set the observing condition
    sm.setRaDecMjd(lon=[ra], lat=[dec], filterNames=[bandName],
                   mjd=expMJD, degrees=False, azAlt=False)
    # Get the sky sed
    wave, spec = sm.returnWaveSpec()
    sed = Sed(wavelen=wave, flambda=spec[0])
    sed.writeSED('skySED_laptop.csv')
    m5 = calcM5(sed, bp, hwbpdict[bandName], photparams, FWHMeff)
    # Get the sky magnitude only in the band concerned
    m = sm.returnMags(bandpasses=hwbpdict)[bandName][0]
    return m5, m
Exemplo n.º 14
0
    def __init__(self, m5Col='fiveSigmaDepth', metricName='ExgalM5', units='mag',
                 lsstFilter='r', wavelen_min=None , wavelen_max=None , **kwargs):
        # Set the name for the dust map to use. This is gathered into the MetricBundle.
        maps = ['DustMap']
        # Set the default wavelength limits for the lsst filters. These are approximately correct.
        waveMins = {'u':330.,'g':403.,'r':552.,'i':691.,'z':818.,'y':950.}
        waveMaxes = {'u':403.,'g':552.,'r':691.,'i':818.,'z':922.,'y':1070.}
        if lsstFilter is not None:
            wavelen_min = waveMins[lsstFilter]
            wavelen_max = waveMaxes[lsstFilter]

        self.m5Col = m5Col
        super().__init__(col=[self.m5Col], maps=maps, metricName=metricName, units=units, **kwargs)

        # Set up internal values for the dust extinction.
        testsed = Sed()
        testsed.setFlatSED(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=1.0)
        testbandpass = Bandpass(wavelen_min=wavelen_min, wavelen_max=wavelen_max, wavelen_step=1.0)
        testbandpass.setBandpass(wavelen=testsed.wavelen,
                                 sb=np.ones(len(testsed.wavelen)))
        self.R_v = 3.1
        self.ref_ebv = 1.0
        # Calculate non-dust-extincted magnitude
        flatmag = testsed.calcMag(testbandpass)
        # Add dust
        self.a, self.b = testsed.setupCCM_ab()
        testsed.addDust(self.a, self.b, ebv=self.ref_ebv, R_v=self.R_v)
        # Calculate difference due to dust when EBV=1.0 (m_dust = m_nodust - Ax, Ax > 0)
        self.Ax1 = testsed.calcMag(testbandpass) - flatmag
        # We will call Coaddm5Metric to calculate the coadded depth. Set it up here.
        self.Coaddm5Metric = Coaddm5Metric(m5Col=m5Col)
Exemplo n.º 15
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.º 16
0
    def ZP_filtre(self, filtre):

        photParams = PhotometricParameters(bandpass=filtre)
        Diameter = 2. * np.sqrt(
            photParams.effarea * 1.e-4 / np.pi)  # diameter in meter
        Cte = 3631. * np.pi * Diameter**2 * 2. * photParams.exptime / 4 / h / 1.e36
        #print('hello Cte',Cte,Diameter,h,photParams.exptime)

        self.data['Skyb'][filtre] = Cte * np.power(
            Diameter / 6.5, 2.) * np.power(
                2. * photParams.exptime / 30., 2.) * np.power(
                    photParams.platescale, 2.) * np.power(
                        10., 0.4 *
                        (25. - self.mag_sky[filtre])) * self.Sigmab[filtre]

        Zb = 181.8 * np.power(Diameter / 6.5, 2.) * self.Tb[filtre]
        mbZ = 25. + 2.5 * np.log10(Zb)
        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)
        flatSed = Sed()
        flatSed.setFlatSED(wavelen_min, wavelen_max, wavelen_step)
        flux0 = np.power(10., -0.4 * mbZ)
        flatSed.multiplyFluxNorm(flux0)
        photParams = PhotometricParameters(bandpass=filtre)
        counts = flatSed.calcADU(
            bandpass, photParams=photParams)  #number of counts for exptime
        self.data['zp'][filtre] = mbZ
        #print 'hello',counts/self.photParams.exptime
        self.data['counts_zp'][filtre] = counts / 2. * photParams.exptime
Exemplo n.º 17
0
 def setUp(self):
     self.wmin = 500
     self.wmax = 1500
     self.bandpasswavelen = np.arange(self.wmin, self.wmax + .5, 1)
     self.bandpasssb = np.ones(len(self.bandpasswavelen))
     self.testbandpass = Bandpass(wavelen=self.bandpasswavelen,
                                  sb=self.bandpasssb)
Exemplo n.º 18
0
def buildVendorDetector(vendorDir, addLosses=True):
    """
    Builds a detector response from the files in vendorDir, by reading the *_QE.dat
      and *_Losses subdirectory for a single version of the detector.
    Returns a Bandpass object.
    If addLosses is True, the QE curve is multiplied by the losses in the *Losses.dat files.
    If addLosses is False, the QE curve does not have any losses included.
    """
    # Read the QE file.
    qefile = glob(os.path.join(vendorDir, '*_QE.dat'))
    if len(qefile) != 1:
        raise ValueError('Expected a single QE file in this directory, found: ', qefile)
    qefile = qefile[0]
    qe = Bandpass()
    qe.readThroughput(qefile)
    if addLosses:
        loss = _readLosses(vendorDir)
        wavelength, sb = qe.multiplyThroughputs(loss.wavelen, loss.sb)
        qe.setBandpass(wavelength, sb)
    # Verify that no values go significantly below zero.
    belowzero = np.where(qe.sb < 0)
    # If there are QE values significantly < 0, raise an exception.
    if qe.sb[belowzero] < belowZeroThreshhold:
        raise ValueError('Found values in QE response significantly below zero.')
    # If they are just small errors in interpolation, set to zero.
    qe.sb[belowzero] = 0
    return qe
Exemplo n.º 19
0
    def testLoadTotalBandpassesFromFiles(self):
        """
        Test that the class method loadTotalBandpassesFromFiles produces the
        expected result
        """

        bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData')
        bandpassNames = ['g', 'r', 'u']
        bandpassRoot = 'test_bandpass_'

        bandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames=bandpassNames,
                                                                 bandpassDir=bandpassDir,
                                                                 bandpassRoot = bandpassRoot)

        controlBandpassList = []
        for bpn in bandpassNames:
            dummyBp = Bandpass()
            dummyBp.readThroughput(os.path.join(bandpassDir,bandpassRoot+bpn+'.dat'))
            controlBandpassList.append(dummyBp)

        wMin = controlBandpassList[0].wavelen[0]
        wMax = controlBandpassList[0].wavelen[-1]
        wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0]

        for bp in controlBandpassList:
            bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax, wavelen_step=wStep)

        for test, control in zip(bandpassDict.values(), controlBandpassList):
            numpy.testing.assert_array_almost_equal(test.wavelen, control.wavelen, 19)
            numpy.testing.assert_array_almost_equal(test.sb, control.sb, 19)
Exemplo n.º 20
0
    def ZP_filtre(self, filtre):

        self.data['Skyb'][filtre] = self.Cte * np.power(
            self.Diameter / 6.5,
            2.) * np.power(self.DeltaT / 30., 2.) * np.power(
                self.platescale, 2.) * np.power(
                    10., 0.4 *
                    (25. - self.mag_sky[filtre])) * self.Sigmab[filtre]

        Zb = 181.8 * np.power(self.Diameter / 6.5, 2.) * self.Tb[filtre]
        mbZ = 25. + 2.5 * np.log10(Zb)
        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)
        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=self.photParams)  #number of counts for exptime
        self.data['zp'][filtre] = mbZ
        #print 'hello',counts/self.photParams.exptime
        self.data['counts_zp'][filtre] = counts / self.photParams.exptime
Exemplo n.º 21
0
def buildMirror(mirrorDir, addLosses=True):
    """
    Build a mirror throughput curve.
    Assumes there are *Losses.dat subdirectory with loss files
       and a m*_Ideal.dat file with the mirror throughput.
    Returns a bandpass object.
    If addLosses is True, the *_Ideal.dat file is multiplied by the *_Losses/*.dat files.
    """
    # Read the mirror reflectance curve.
    mirrorfile = glob(os.path.join(mirrorDir, 'm*Ideal.dat'))
    if len(mirrorfile) != 1:
        raise ValueError('Expected a single mirror file in directory %s, found: ' %mirrorDir, mirrorfile)
    mirrorfile = mirrorfile[0]
    mirror = Bandpass()
    mirror.readThroughput(mirrorfile)
    if addLosses:
        loss = _readLosses(mirrorDir)
        wavelen, sb = mirror.multiplyThroughputs(loss.wavelen, loss.sb)
        mirror.setBandpass(wavelen, sb)
    # Verify that no values go significantly below zero.
    belowzero = np.where(mirror.sb < 0)
    # If there are QE values significantly < 0, raise an exception.
    if mirror.sb[belowzero] < belowZeroThreshhold:
        raise ValueError('Found values in mirror response significantly below zero')
    # If they are just small errors in interpolation, set to zero.
    mirror.sb[belowzero] = 0
    return mirror
Exemplo n.º 22
0
    def test_norm(self):
        """
        Test that the special test case getImsimFluxNorm
        returns the same value as calling calcFluxNorm actually
        passing in the imsim Bandpass
        """

        bp = Bandpass()
        bp.imsimBandpass()

        rng = np.random.RandomState(1123)
        wavelen = np.arange(300.0, 2000.0, 0.17)

        for ix in range(10):
            flux = rng.random_sample(len(wavelen)) * 100.0
            sed = Sed()
            sed.setSED(wavelen=wavelen, flambda=flux)
            magmatch = rng.random_sample() * 5.0 + 10.0

            control = sed.calcFluxNorm(magmatch, bp)
            test = getImsimFluxNorm(sed, magmatch)

            # something about how interpolation is done in Sed means
            # that the values don't come out exactly equal.  They come
            # out equal to 8 seignificant digits, though.
            self.assertEqual(control, test)
Exemplo n.º 23
0
    def testMags(self):
        """
        Test that the interpolated mags are similar to mags computed from interpolated spectra
        """

        throughPath = os.path.join(getPackageDir('throughputs'),'baseline')
        filters = ['u','g','r','i','z','y']

        bps = []
        for filterName in filters:
            bp = np.loadtxt(os.path.join(throughPath, 'filter_%s.dat' % filterName),
                            dtype=zip(['wave','trans'],[float]*2 ))
            lsst_bp = Bandpass()
            lsst_bp.setBandpass(bp['wave'], bp['trans'])
            bps.append(lsst_bp)

        sm1 = sb.SkyModel()
        sm1.setRaDecMjd([36.],[-68.],49353.18, degrees=True)
        sm1.computeSpec()
        mags1 = []
        for bp in bps:
            mags1.append(sm1.computeMags(bandpass=bp))
        mags1 = np.array(mags1)

        sm2 = sb.SkyModel(mags=True)
        sm2.setRaDecMjd([36.],[-68.],49353.18, degrees=True)
        sm2.computeSpec()
        mag2 = sm2.computeMags()
        np.testing.assert_allclose(mags1,mag2.T, rtol=1e-4)
Exemplo n.º 24
0
def get_sed(name, magnorm, redshift, av, rv):
    if not hasattr(get_sed, '_rest_dict'):
        get_sed._rest_dict = {}
        get_sed._imsim_bp = Bandpass()
        get_sed._imsim_bp.imsimBandpass()
        get_sed._sed_dir = os.environ['SIMS_SED_LIBRARY_DIR']
        get_sed._ccm_w = None

    tag = '%s_%.2f_%.2f' % (name, av, rv)
    if tag not in get_sed._rest_dict:
        ss = Sed()
        ss.readSED_flambda(os.path.join(get_sed._sed_dir, name))
        if get_sed._ccm_w is None or not np.array_equal(
                ss.wavelen, get_sed._ccm_w):
            get_sed._ccm_w = np.copy(ss.wavelen)
            get_sed._ax, get_sed._bx = ss.setupCCM_ab()

        mn = ss.calcMag(get_sed._imsim_bp)
        ss.addDust(get_sed._ax, get_sed._bx, A_v=av, R_v=rv)
        get_sed._rest_dict[tag] = (ss, mn)

    base_sed = get_sed._rest_dict[tag][0]
    ss = Sed(wavelen=base_sed.wavelen, flambda=base_sed.flambda)
    dmag = magnorm - get_sed._rest_dict[tag][1]
    fnorm = np.power(10.0, -0.4 * dmag)
    ss.multiplyFluxNorm(fnorm)
    ss.redshiftSED(redshift, dimming=True)
    return ss
Exemplo n.º 25
0
    def testMags(self):
        """
        Test that the interpolated mags are similar to mags computed from interpolated spectra
        """

        throughPath = os.path.join(getPackageDir('throughputs'), 'baseline')
        filters = ['u', 'g', 'r', 'i', 'z', 'y']

        bps = {}
        for filterName in filters:
            bp = np.loadtxt(os.path.join(throughPath,
                                         'filter_%s.dat' % filterName),
                            dtype=list(zip(['wave', 'trans'], [float] * 2)))
            lsst_bp = Bandpass()
            lsst_bp.setBandpass(bp['wave'], bp['trans'])
            bps[filterName] = lsst_bp

        sm1 = self.sm_spec
        sm1.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
        mags1 = sm1.returnMags(bandpasses=bps)

        sm2 = self.sm_mags
        sm2.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
        mag2 = sm2.returnMags()

        for i, filtername in enumerate(filters):
            np.testing.assert_allclose(mags1[filtername],
                                       mag2[filtername],
                                       rtol=1e-4)
Exemplo n.º 26
0
def _create_library_one_sed(_galaxy_sed_dir, sed_file_name_list, av_grid,
                            rv_grid, bandpass_dict, out_dict, out_lock):

    n_obj = 0
    for i_av, av in enumerate(av_grid):
        for i_rv, rv in enumerate(rv_grid):
            if av < 0.01 and i_rv > 0:
                continue
            n_obj += 1

    imsim_bp = Bandpass()
    imsim_bp.imsimBandpass()

    t_start = time.time()

    sed_names_out = []
    rv_out_list_out = []
    av_out_list_out = []
    sed_mag_norm_out = []
    sed_mag_list_out = []

    for i_sed, sed_file_name in enumerate(sed_file_name_list):
        base_spec = Sed()
        base_spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name))
        ax, bx = base_spec.setupCCM_ab()

        mag_norm = base_spec.calcMag(imsim_bp)

        sed_names = np.array([defaultSpecMap[sed_file_name]] * n_obj)
        rv_out_list = np.zeros(n_obj, dtype=float)
        av_out_list = np.zeros(n_obj, dtype=float)
        sed_mag_norm = mag_norm * np.ones(n_obj, dtype=float)
        sed_mag_list = np.zeros((n_obj, len(bandpass_dict)), dtype=float)

        i_obj = 0
        for i_av, av in enumerate(av_grid):
            for i_rv, rv in enumerate(rv_grid):
                if av < 0.01 and i_rv > 0:
                    continue
                spec = Sed(wavelen=base_spec.wavelen,
                           flambda=base_spec.flambda)
                spec.addDust(ax, bx, A_v=av, R_v=rv)
                av_out_list[i_obj] = av
                rv_out_list[i_obj] = rv
                sed_mag_list[i_obj][:] = bandpass_dict.magListForSed(spec)
                i_obj += 1

        sed_names_out.append(sed_names)
        sed_mag_norm_out.append(sed_mag_norm)
        sed_mag_list_out.append(sed_mag_list)
        av_out_list_out.append(av_out_list)
        rv_out_list_out.append(rv_out_list)

    with out_lock:
        out_dict['sed_names'] += sed_names_out
        out_dict['sed_mag_norm'] += sed_mag_norm_out
        out_dict['sed_mag_list'] += sed_mag_list_out
        out_dict['av_out_list'] += av_out_list_out
        out_dict['rv_out_list'] += rv_out_list_out
Exemplo n.º 27
0
    def get_phosimVars(self):
        """
        Obtain variables sedFilepath to be used to obtain unique filenames
        for each SED for phoSim and MagNorm which is also used. Note that aside
        from acting as a getter, this also writes spectra to 
        `self.sn_sedfile_prefix`snid_mjd_band.dat for each observation of
        interest
        """
        # construct the unique filename
        # method: snid_mjd(to 4 places of decimal)_bandpassname
        mjd = "_{:0.4f}_".format(self.mjdobs)
        mjd += self.obs_metadata.bandpass + '.dat'
        fnames = np.array([
            self.sn_sedfile_prefix + str(int(elem)) + mjd if isinstance(
                elem, numbers.Number) else self.sn_sedfile_prefix + str(elem) +
            mjd for elem in self.column_by_name('snid')
        ],
                          dtype='str')

        c, x1, x0, t0, z = self.column_by_name('c'),\
            self.column_by_name('x1'),\
            self.column_by_name('x0'),\
            self.column_by_name('t0'),\
            self.column_by_name('redshift')

        bp = Bandpass()
        bp.imsimBandpass()

        magNorms = np.zeros(len(fnames))

        snobject = SNObject()
        snobject.rectifySED = True
        for i in range(len(self.column_by_name('snid'))):
            # if t0 is nan, this was set by the catalog for dim SN, or SN
            #   outside redshift range, We will not provide a SED file for these
            if np.isnan(t0[i]):
                magNorms[i] = np.nan
                fnames[i] = None

            else:
                snobject.set(c=c[i], x1=x1[i], x0=x0[i], t0=t0[i], z=z[i])
                if snobject.modelOutSideTemporalRange == 'zero':
                    if self.mjdobs > snobject.maxtime(
                    ) or self.mjdobs < snobject.mintime():
                        magNorms[i] = np.nan
                        fnames[i] = None

                # SED in rest frame
                sed = snobject.SNObjectSourceSED(time=self.mjdobs)
                try:
                    magNorms[i] = sed.calcMag(bandpass=bp)
                except:
                    # sed.flambda = 1.0e-20
                    magNorms[i] = 1000.  # sed.calcMag(bandpass=bp)

                if self.writeSedFile:
                    sed.writeSED(fnames[i])

        return (fnames, magNorms)
Exemplo n.º 28
0
 def setUp(self):
     warnings.simplefilter('always')
     self.wmin = 500
     self.wmax = 1500
     self.bandpasswavelen = np.arange(self.wmin, self.wmax + .5, 1)
     self.bandpasssb = np.ones(len(self.bandpasswavelen))
     self.testbandpass = Bandpass(wavelen=self.bandpasswavelen,
                                  sb=self.bandpasssb)
Exemplo n.º 29
0
def getDesThroughput(f):
    if f not in ["u", "g", "r", "i", "z", "y"]:
        raise ValueError("DES does not have a {} filter".format(f))
    throughputsDir = "DES_throughputs"
    throughputsFile = os.path.join(throughputsDir, "total_{}.dat".format(f))
    desBand = Bandpass()
    desBand.readThroughput(throughputsFile)
    return desBand
Exemplo n.º 30
0
def getHscThroughput(f):
    if f not in ["Y"]:
        raise ValueError("HSC does not have a {} filter".format(f))
    throughputsDir = "HSC_throughputs"
    throughputsFile = os.path.join(throughputsDir, "total_{}.dat".format(f))
    hscBand = Bandpass()
    hscBand.readThroughput(throughputsFile)
    return hscBand