Exemplo n.º 1
0
    def test_spec_kcorr_with_curve(self):
        nf, start, end = 100, 10., 1e5
        wl = np.exp(np.linspace(np.log(end), np.log(start), nf))
        freq = rf.val_to_hz(wl, inp="A")
        Tcolor = 7e3
        sp = spectrum.SpectrumPlanck(freq, Tcolor, 'test')

        z = 0.1
        distance = 1e6  # phys.cosmology_D_by_z(z)
        bn_rest = 'g'
        # bn_obs = 'V'
        bn_obs = bn_rest
        br = band_by_name(bn_rest)
        bo = band_by_name(bn_obs)

        M_Q = sp.to_mag(br, z=0., d=phys.pc2cm(10.))
        m_r = sp.to_mag(bo, z=z, d=phys.pc2cm(distance))
        DM = phys.dist2MD(distance)
        K_QR = m_r - M_Q - DM

        kcorr = kcorrection_spec(sp, z, br, bo)

        print("{}: z= {} K_QR= {:.5f} kcorr= {:.5f}".format(bn_rest, z, K_QR, kcorr))

        self.assertAlmostEqual(K_QR, kcorr, 8, "{}: For z={} kcorr should be like K_QR. But kcorr={:.5f} K_QR= {:.5f}.".
                               format(bn_rest, z, kcorr, K_QR))
Exemplo n.º 2
0
    def flux_to_curves(self, bands, z=0., d=phys.pc2cm(10.), magnification=1.):
        """

        :param bands:
        :param z:
        :param d:
        :param magnification:
        :return:
        """
        from pystella.rf.lc import SetLightCurve
        from pystella.rf import band

        curves = SetLightCurve(self.Name)
        for bname in bands:
            if isinstance(bname, str):
                b = band.band_by_name(bname)
            else:
                b = bname
            try:
                lc = self.flux_to_curve(b, z=z, d=d, magnification=magnification)
                curves.add(lc)
            except ValueError as ex:
                logger.error("Could not compute light curve {} for band {} due to {}.".
                             format(self.Name, bname, ex))
        return curves
Exemplo n.º 3
0
    def to_mag(self, b, z=0., d=phys.pc2cm(10.), magnification=1.):
        """
        Compute the magnitude for the band
        :param b: photometric band
        :param z: redshift,  default: 0
        :param d: distance [cm], default: 10 pc
        :param magnification: , default: 1
        :return:
        """
        if b is None:
            raise ValueError("Band must be defined.")

        from pystella.rf.star import Star
        from pystella.rf import band

        star = Star('', self, is_flux_eq_luminosity=True)
        star.set_distance(d)
        star.set_redshift(z)
        star.set_magnification(magnification)
        # mag = star.flux_to_mag(b)
        if b.Name in [band.Band.NameBol, band.Band.NameBolQuasi, band.Band.NameUBVRI]:
            mag = star.magBol(b)
        else:
            mag = star.magAB(b)
        return mag
Exemplo n.º 4
0
def compute_mag(name,
                path,
                bands,
                ext=None,
                z=0.,
                distance=10.,
                magnification=1.,
                t_diff=1.05,
                is_show_info=True,
                is_save=False):
    """
        Compute magnitude in bands for the 'name' model.
    :param name: the name of a model and data files
    :param path: the directory with data-files
    :param bands: photometric bands
    :param ext: extinction
    :param z: redshift, default 0
    :param distance: distance to star in parsec, default 10 pc
    :param magnification: gravitational lensing magnification
    :param t_diff:  depression between time points
    :param is_show_info: flag to write some information, default True
    :param is_save: flag to save result in file, default False
    :return: dictionary with keys = bands, value = star's magnitudes
    """
    model = Stella(name, path=path)
    if is_show_info:
        print('')
        model.info()

    if not model.is_ph:
        model.info()
        print("Error: No data for: " + str(model))
        return None

    serial_spec = model.get_ph(t_diff=t_diff)
    mags = serial_spec.mags_bands(bands,
                                  z=z,
                                  d=phys.pc2cm(distance),
                                  magnification=magnification)

    if mags is not None:
        fname = os.path.join(path, name + '.ubv')
        if is_save:
            mags_save(mags, bands, fname)
            print("Magnitudes have been saved to " + fname)

    if is_show_info:
        # print the time of maximum LC
        tmin = 2.0
        t = mags['time']
        for n in bands:
            t_min = t[t > tmin][mags[n][t > tmin].argmin()]
            print("t_max(%s) = %f" % (n, t_min))

    if ext is not None:  # add extinction
        for n in bands:
            mags[n] = mags[n] + ext[n]

    return mags
Exemplo n.º 5
0
    def curves(self,
               bands,
               z=0.,
               distance=10.,
               ebv=0.,
               Rv=None,
               law=LawFitz,
               mode=ReddeningLaw.SMC,
               **kwargs):
        """
            Compute model magnitudes of the photometric bands.
        :param bands: photometric bands
        :param z: redshift, default 0
        :param distance: distance to the source in parsec, default 10 pc
        :param ebv: color excess, default: 0
        :param Rv:  R_V
        :param law: the law of extinction curve
        :param mode: type of extinction curves (MW, LMC, SMC)
        :param kwargs: dic, wl_ab: wave length interval for spectra [A]
        :return: light curves for the photometric bands
        """
        from pystella.rf.light_curve_func import series_spec_reddening
        from pystella.util.phys_var import phys

        t_beg = kwargs.get("t_beg", float('-inf'))
        t_end = kwargs.get("t_end", float('inf'))
        t_diff = kwargs.get("t_diff", 1.01)
        wl_ab = kwargs.get("wl_ab", None)
        is_nfrus = kwargs.get("is_nfrus", True)
        magnification = kwargs.get("magnification", 1.)

        if len(bands) == 0:
            raise ValueError("You have not set any bands for model: " +
                             str(self))
        if not self.is_ph:
            self.info()
            raise ValueError("Error: No spectral data for: " + str(self))

        # Get SED(time)
        serial_spec = self.get_ph(t_diff=t_diff,
                                  t_beg=t_beg,
                                  t_end=t_end,
                                  is_nfrus=is_nfrus)
        if wl_ab is not None:
            serial_spec = serial_spec.copy(wl_ab=wl_ab)
        # reddening
        if ebv > 0:
            ss = serial_spec.copy(wl_ab=law.LAMBDA_LIM)
            serial_spec = series_spec_reddening(ss,
                                                ebv=ebv,
                                                Rv=Rv,
                                                law=law,
                                                mode=mode)
        # light curves
        curves = serial_spec.flux_to_curves(bands,
                                            z=z,
                                            d=phys.pc2cm(distance),
                                            magnification=magnification)
        return curves
Exemplo n.º 6
0
    def mags_bands(self, bands, z=0., d=phys.pc2cm(10.), magnification=1.):
        from pystella.rf import band

        mags = dict((k, None) for k in bands)
        for bn in bands:
            b = band.band_by_name(bn)
            times, m = self.to_mags(b, z=z, d=d, magnification=magnification)
            mags[bn] = m

        mags['time'] = self.Time * (1. + z)
        return mags
Exemplo n.º 7
0
def curves_compute(name,
                   path,
                   bands,
                   z=0.,
                   distance=10.,
                   magnification=1.,
                   **kwargs):
    """
        Compute magnitude in bands for the 'name' model.
    :param name: the name of a model and data files
    :param path: the directory with data-files
    :param bands: photometric bands
    :param z: redshift, default 0
    :param distance: distance to star in parsec, default 10 pc
    :param magnification: gravitational lensing magnification
    :return: dictionary with keys = bands, value = star's magnitudes
    """
    t_beg = kwargs.get("t_beg", 0.)
    t_end = kwargs.get("t_end", float('inf'))
    is_show_info = kwargs.get("is_show_info", False)
    t_diff = kwargs.get("t_diff", 1.05)

    if len(bands) == 0:
        raise ValueError("You have not set any bands for model: " + str(name))

    model = Stella(name, path=path)
    if not model.is_ph:
        model.info()
        raise ValueError("Error: No spectral data for: " + str(model))

    if is_show_info:
        print('')
        model.info()

    serial_spec = model.get_ph(t_diff=t_diff, t_beg=t_beg, t_end=t_end)
    curves = serial_spec.flux_to_curves(bands,
                                        z=z,
                                        d=phys.pc2cm(distance),
                                        magnification=magnification)

    if is_show_info:
        # print the time of maximum LC
        tmin = 2.0
        t = curves.TimeCommon
        for n in bands:
            t_min = t[t > tmin][curves[n][t > tmin].argmin()]
            print("t_max(%s) = %f" % (n, t_min))

    return curves
Exemplo n.º 8
0
    def flux_to_curves(self,
                       bands,
                       z: float = 0.,
                       d: float = phys.pc2cm(10.),
                       magnification: float = 1.):
        """
        Compute the light curves for bands using the spectral data.

        Parameters
        ----------
            - bands: broadbands 
            - z: redshift. Default: 0
            - d: distance [cm]. Default: 10 pc in cm
            - magnification: lens magnification. Default: 1 
        
        Returns
        -------

            SetLightCurve
        """
        from pystella.rf.lc import SetLightCurve
        from pystella.rf import band

        curves = SetLightCurve(self.Name)
        for bname in bands:
            if isinstance(bname, str):
                b = band.band_by_name(bname)
            else:
                b = bname
            try:
                lc = self.flux_to_curve(b,
                                        z=z,
                                        d=d,
                                        magnification=magnification)
                curves.add(lc)
            except ValueError as ex:
                logger.error(
                    "Could not compute light curve {} for band {} due to {}.".
                    format(self.Name, bname, ex))
        return curves