Beispiel #1
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)
Beispiel #2
0
    def calc_sne_mags(self, obs_mjd, obs_filter):

        wavelen_max = 1800.
        wavelen_min = 30.
        wavelen_step = 0.1

        sn_magnorm_list = []
        sn_sed_names = []
        add_to_cat_list = []

        for idx in range(len(self.truth_cat)):

            sed_mjd = obs_mjd - self.truth_cat['t_delay'].iloc[idx]

            current_sn_obj = SNObject(ra=self.truth_cat['ra'].iloc[idx],
                                      dec=self.truth_cat['dec'].iloc[idx])
            current_sn_obj.set(z=self.truth_cat['redshift'].iloc[idx],
                               t0=self.truth_cat['t0'].iloc[idx],
                               x0=self.truth_cat['x0'].iloc[idx],
                               x1=self.truth_cat['x1'].iloc[idx],
                               c=self.truth_cat['c'].iloc[idx])

            # Following follows from
            # https://github.com/lsst/sims_catUtils/blob/master/python/lsst/sims/catUtils/mixins/sncat.py

            sn_sed_obj = current_sn_obj.SNObjectSourceSED(
                time=sed_mjd,
                wavelen=np.arange(wavelen_min, wavelen_max, wavelen_step))
            flux_500 = sn_sed_obj.flambda[np.where(
                sn_sed_obj.wavelen >= 499.99)][0]

            if flux_500 > 0.:
                sn_magnorm = sn_sed_obj.calcMag(bandpass=self.imSimBand)
                sn_name = None
                if self.write_sn_sed:
                    sn_name = '%s/specFileGLSN_%s_%s_%.4f.txt' % (
                        self.sed_folder_name,
                        self.truth_cat['dc2_sys_id'].iloc[idx],
                        self.truth_cat['image_number'].iloc[idx], obs_mjd)
                    sed_filename = '%s/%s' % (self.out_dir, sn_name)
                    sn_sed_obj.writeSED(sed_filename)
                    with open(sed_filename, 'rb') as f_in, gzip.open(
                            str(sed_filename + '.gz'), 'wb') as f_out:
                        shutil.copyfileobj(f_in, f_out)
                    os.remove(sed_filename)

                sn_magnorm_list.append(sn_magnorm)
                sn_sed_names.append(sn_name + '.gz')
                add_to_cat_list.append(idx)

        return add_to_cat_list, np.array(sn_magnorm_list), sn_sed_names