Exemple #1
0
def eval_scaled_dust_dbmmb_map(model, nu_ref, nu_test, beta0, beta1, nubreak,
                               nside, fsky, radec_center, T):
    #def double-beta dust model
    if model == 'd0':
        print('one beta model')
        analytic_expr = ('(exp(nu0 / temp * h_over_k) -1)'
                         '/ (exp(nu / temp * h_over_k) - 1)'
                         '* (nu / nu0)**(1 + beta_d)')
        dust = AnalyticComponent(analytic_expr,
                                 nu0=nu_ref,
                                 h_over_k=constants.h * 1e9 / constants.k,
                                 temp=T)
        #print(dust)
        scaling_factor = dust.eval(nu_test, beta0)
    else:
        print('double beta model')
        analytic_expr = double_beta_dust_FGB_Model(units='K_CMB')
        dbdust = AnalyticComponent(analytic_expr,
                                   nu0=nu_ref,
                                   h_over_k=constants.h * 1e9 / constants.k,
                                   temp=T)
        scaling_factor = dbdust.eval(nu_test, beta0, beta1, nubreak)

    sky = pysm3.Sky(nside=nside, preset_strings=['d0'])
    dust_map_ref = np.zeros(
        (3, 12 * nside**2))  #this way the output is w/0 units!!
    dust_map_ref[0:3, :] = sky.get_emission(
        nu_ref * u.GHz, None) * utils.bandpass_unit_conversion(
            nu_ref * u.GHz, None, u.uK_CMB)

    map_test = dust_map_ref * scaling_factor

    #mask = s4bi.get_coverage(fsky, nside, center_radec=radec_center)

    return map_test
Exemple #2
0
    def compute_fg(self, dust_model, presets_dust=None, NSIDE_PATCH=4):

        #print("\nYou're computing thermal dust with model {}\n".format(dust_model))

        dustmaps = np.zeros((self.nfreqs, 3, self.npix))
        if dust_model == 'd0':
            sync_model = 's0'
        else:
            sync_model = 's1'
        settings = [dust_model, sync_model]
        self.preset_fg(dustmodel=dust_model, dict_dust=presets_dust)
        sky = pysm3.Sky(nside=self.nside, preset_strings=settings)

        if dust_model == 'd0' or dust_model == 'd1' or dust_model == 'd2' or dust_model == 'd3' or dust_model == 'd6':
            sky.components[0].mbb_temperature = 20 * sky.components[
                0].mbb_temperature.unit  # fix temp at 20 K across the sky
            #print('Downgrade pixelization of spectral indices at nside = {}'.format(NSIDE_PATCH))
            if dust_model != 'd0':
                #print('Downgrade pixelization of spectral indices at nside = {}'.format(NSIDE_PATCH))
                for spectral_param in [
                        sky.components[0].mbb_index, sky.components[1].pl_index
                ]:
                    spectral_param[:] = hp.ud_grade(
                        hp.ud_grade(spectral_param.value, NSIDE_PATCH),
                        self.nside) * spectral_param.unit

        for j in range(self.nfreqs):
            dustmaps[j] = np.array(
                sky.get_emission(self.nus[j] * u.GHz) *
                utils.bandpass_unit_conversion(self.nus[j] * u.GHz, None,
                                               u.uK_CMB))

        return dustmaps
Exemple #3
0
def get_fg_notconvolved(model, nu, nside=256):

    sky = pysm3.Sky(nside=nside, preset_strings=[model])
    maps = np.zeros(((len(nu), 3, 12 * nside**2)))
    for indi, i in enumerate(nu):
        maps[indi] = sky.get_emission(i * u.GHz,
                                      None) * utils.bandpass_unit_conversion(
                                          i * u.GHz, None, u.uK_CMB)

    return maps
Exemple #4
0
def get_dust_sky(nus, model, nside):
    sky = pysm3.Sky(nside=nside, preset_strings=[model])
    maps_dust = np.zeros(((len(nus), 3, 12 * nside**2)))

    for i_freq, freq in enumerate(nus):
        maps_XXXGHz = sky.get_emission(freq * u.GHz,
                                       None) * utils.bandpass_unit_conversion(
                                           freq * u.GHz, None, u.uK_CMB)
        maps_dust[i_freq] = maps_XXXGHz.copy()

    return maps_dust
Exemple #5
0
    def get_simple_sky_map(self):
        """
        Create as many skies as the number of input frequencies.
        Instrumental effects are not considered.
        Return a vector of shape (number_of_input_subfrequencies, npix, 3)
        """
        _, nus_edge, nus_in, _, _, Nbbands_in = qubic.compute_freq(self.filter_nu, self.Nfin,
                                                                   self.filter_relative_bandwidth)

        sky = np.zeros((self.Nfin, self.npix, 3))

        ##### This is the old code by JCH - It has been replaced by what Edgar Jaber has proposed
        ##### see his presentation on Git: qubic/scripts/ComponentSeparation/InternshipJaber/teleconf_03122020.pdf
        # for i in range(Nf):
        #     # ###################### This is puzzling part here: ############################
        #     # # See Issue on PySM Git: https://github.com/healpy/pysm/issues/49
        #     # ###############################################################################
        #     # # #### THIS IS WHAT WOULD MAKE SENSE BUT DOES NOT WORK ~ 5% on maps w.r.t. input
        #     # # nfreqinteg = 5
        #     # # nus = np.linspace(nus_edge[i], nus_edge[i + 1], nfreqinteg)
        #     # # freqs = utils.check_freq_input(nus)
        #     # # convert_to_uK_RJ = (np.ones(len(freqs), dtype=np.double) * u.uK_CMB).to_value(
        #     # # u.uK_RJ, equivalencies=u.cmb_equivalencies(freqs))
        #     # # #print('Convert_to_uK_RJ :',convert_to_uK_RJ)
        #     # # weights = np.ones(nfreqinteg) * convert_to_uK_RJ
        #     # ###############################################################################
        #     # ###### Works OK but not clear why...
        #     # ###############################################################################
        #     # nfreqinteg = 5
        #     # nus = np.linspace(nus_edge[i], nus_edge[i + 1], nfreqinteg)
        #     # filter_uK_CMB = np.ones(len(nus), dtype=np.double)
        #     # filter_uK_CMB_normalized = utils.normalize_weights(nus, filter_uK_CMB)
        #     # weights = 1. / filter_uK_CMB_normalized
        #     # ###############################################################################

        #     # ### Integrate through band using filter shape defined in weights
        #     # themaps_iqu = self.sky.get_emission(nus * u.GHz, weights=weights)
        #     # sky[i, :, :] = np.array(themaps_iqu.to(u.uK_CMB, equivalencies=u.cmb_equivalencies(nus_in[i] * u.GHz))).T
        #     # ratio = np.mean(self.input_cmb_maps[0,:]/sky[i,:,0])
        #     # print('Ratio to initial: ',ratio)

        # #### Here is the new code from Edgar Jaber
        for i in range(self.Nfin):
            nfreqinteg = 5
            freqs = np.linspace(nus_edge[i], nus_edge[i + 1], nfreqinteg)
            weights = np.ones(nfreqinteg)
            sky[i, :, :] = (
                    self.sky.get_emission(freqs * u.GHz, weights) * utils.bandpass_unit_conversion(freqs * u.GHz,
                                                                                                   weights,
                                                                                                   u.uK_CMB)).T

        return sky
Exemple #6
0
def give_me_maps_d1_modified(nus,
                             nubreak,
                             covmap,
                             delta_b,
                             nside,
                             fix_temp=None,
                             nside_index=256):

    maps_dust = np.ones(((len(nus), 3, 12 * nside**2))) * hp.UNSEEN
    ind = np.where(covmap > 0)[0]
    sky = pysm3.Sky(nside=nside, preset_strings=['d1'])

    maps_dust = sky.get_emission(353 * u.GHz,
                                 None) * utils.bandpass_unit_conversion(
                                     353 * u.GHz, None, u.uK_CMB)
    map_index = np.array(sky.components[0].mbb_index)
    if fix_temp is not None:
        sky.components[0].mbb_temperature = fix_temp
        map_temperature = np.array(
            np.ones(12 * nside**2) * sky.components[0].mbb_temperature)
    else:
        map_temperature = np.array(sky.components[0].mbb_temperature)

    if nside_index != 256:
        map_temperature = hp.pixelfunc.ud_grade(map_temperature, nside_index)
        map_index = hp.pixelfunc.ud_grade(map_index, nside_index)
        map_temperature = hp.pixelfunc.ud_grade(map_temperature, 256)
        map_index = hp.pixelfunc.ud_grade(map_index, 256)

    #hp.mollview(map_temperature, sub=(1, 2, 1))
    #hp.mollview(map_index, sub=(1, 2, 2))
    #print(map_index.shape)

    # Evaluation of Mixing Matrix for 2 beta model
    comp2b = [fgbuster.component_model.Dust_2b(nu0=353)]
    A2b = fgbuster.MixingMatrix(*comp2b)
    A2b_ev = A2b.evaluator(nus)

    new_dust_map = np.ones(((len(nus), 3, 12 * nside**2))) * hp.UNSEEN
    for i in ind:

        A2b_maxL = A2b_ev([
            np.array(map_index)[i] - delta_b,
            np.array(map_index)[i] + delta_b, nubreak,
            np.array(map_temperature)[i]
        ])

        for j in range(len(nus)):
            new_dust_map[j, :, i] = A2b_maxL[j, 0] * maps_dust[:, i]

    return new_dust_map, [map_index, map_temperature]
Exemple #7
0
def eval_scaled_sync_map(nu_ref, nu_test, betapl, nside, fsky, radec_center):
    #def double-beta dust model
    analytic_expr = '(nu / nu0)**(beta_pl)'
    sync = AnalyticComponent(analytic_expr, nu0=nu_ref)
    scaling_factor = sync.eval(nu_test, betapl)

    sky = pysm3.Sky(nside=nside, preset_strings=['s0'])
    sync_map_ref = np.zeros(
        (3, 12 * nside**2))  #this way the output is w/0 units!!
    sync_map_ref[0:3, :] = sky.get_emission(
        nu_ref * u.GHz, None) * utils.bandpass_unit_conversion(
            nu_ref * u.GHz, None, u.uK_CMB)

    map_test = sync_map_ref * scaling_factor

    #mask = s4bi.get_coverage(fsky, nside, center_radec=radec_center)

    return map_test
Exemple #8
0
def create_dustd0(nside, nus):

    # Create 353 GHz dust maps
    sky = pysm3.Sky(nside=nside, preset_strings=['d0'])

    #comp=[fgbuster.component_model.Dust(nu0=353, beta_d=1.54, temp=20)]

    #A = fgbuster.MixingMatrix(*comp)
    #A_ev = A.evaluator(nus)
    #A_maxL = A_ev()

    new_dust_map = np.zeros(((len(nus), 3, 12 * nside**2)))
    for i in range(len(nus)):
        maps_XXXGHz = sky.get_emission(nus[i] * u.GHz,
                                       None) * utils.bandpass_unit_conversion(
                                           nus[i] * u.GHz, None, u.uK_CMB)
        new_dust_map[i] = maps_XXXGHz.copy()

    return new_dust_map
Exemple #9
0
def create_sync(nside, nus):

    # Create 353 GHz dust maps
    sky = pysm3.Sky(nside=nside, preset_strings=['s0'])
    maps_70GHz = sky.get_emission(70 * u.GHz,
                                  None) * utils.bandpass_unit_conversion(
                                      70 * u.GHz, None, u.uK_CMB)

    comp = [fgbuster.component_model.Synchrotron(nu0=70)]

    A = fgbuster.MixingMatrix(*comp)
    A_ev = A.evaluator(nus)
    A_maxL = A_ev(np.array([-3]))

    new_sync_map = np.zeros(((len(nus), 3, 12 * nside**2)))
    for i in range(len(nus)):
        new_sync_map[i] = A_maxL[i, 0] * maps_70GHz

    return new_sync_map
Exemple #10
0
    def getskymaps(self,
                   same_resol=None,
                   verbose=False,
                   coverage=None,
                   iib=1,
                   noise=False,
                   signoise=1.,
                   beta=[],
                   fix_temp=None,
                   nside_index=256):
        """

        """

        sky = self.get_sky(coverage)
        allmaps = np.zeros(((len(self.nus), 3, self.npix)))
        pixok = coverage > 0
        if same_resol is not None:
            self.fwhmdeg = np.ones(len(self.nus)) * np.max(same_resol)

        for i in self.skyconfig.keys():
            if i == 'cmb':
                cmbmap = self.get_cmb(coverage)
                for j in range(len(self.nus)):
                    allmaps[j] += cmbmap
                map_index = []
            elif i == 'dust':
                dustmaps = np.zeros(((len(self.nus), 3, self.npix)))
                if self.skyconfig[i] == 'd0':
                    if verbose:
                        print('Model : {}'.format(self.skyconfig[i]))

                    for i in range(len(self.nus)):
                        print(
                            'Integration into bands ({:.0f} bands) -> from {:.2f} to {:.2f} GHz'
                            .format(iib, self.edges[i][0], self.edges[i][1]))
                        #print(self.edges[i])
                        nus_inter = get_freqs_inter(self.edges[i], iib)
                        #print(nus_inter)
                        dustmaps_inter = create_dustd0(
                            self.nside, nus_inter
                        )  #get_fg_notconvolved('d0', self.nus, nside=self.nside)
                        mean_dust_maps = np.mean(dustmaps_inter, axis=0)
                        dustmaps[i] = mean_dust_maps.copy()
                    allmaps += dustmaps
                    map_index = []

                elif self.skyconfig[i] == 'd02b':
                    if verbose:
                        print(
                            'Model : d02b -> Twos spectral index beta ({:.2f} and {:.2f}) with nu_break = {:.2f}'
                            .format(beta[0], beta[1], beta[2]))

                    # Create 353 GHz dust maps
                    sky = pysm3.Sky(nside=self.nside, preset_strings=['d0'])
                    maps_353GHz = sky.get_emission(
                        353 * u.GHz, None) * utils.bandpass_unit_conversion(
                            353 * u.GHz, None, u.uK_CMB)
                    for i in range(len(self.nus)):
                        print(
                            'Integration into bands ({:.0f} bands) -> from {:.2f} to {:.2f} GHz'
                            .format(iib, self.edges[i][0], self.edges[i][1]))
                        #print(self.edges[i])
                        nus_inter = get_freqs_inter(self.edges[i], iib)
                        #print(nus_inter)

                        #print(nus_inter)
                        #add Elenia's definition
                        dustmaps_inter = create_dust_with_model2beta(
                            maps_353GHz,
                            self.nside,
                            nus_inter,
                            beta[0],
                            beta[1],
                            beta[2],
                            temp=20,
                            break_width=beta[3])
                        #print(dustmaps_inter.shape)
                        mean_dust_maps = np.mean(dustmaps_inter, axis=0)
                        dustmaps[i] = mean_dust_maps.copy()
                    allmaps += dustmaps
                    map_index = []

                elif self.skyconfig[i] == 'd1' or self.skyconfig[
                        i] == 'd2' or self.skyconfig[
                            i] == 'd3' or self.skyconfig[i] == 'd4':
                    skyconf = self.skyconfig[i]
                    #if verbose:
                    print('Model : {}'.format(skyconf))
                    if iib == 1:
                        dustmaps = get_dust_sky(self.nus, skyconf, self.nside)
                    else:
                        dustmaps = np.zeros(((len(self.nus), 3, self.npix)))
                        for i in range(len(self.nus)):
                            nus_inter = get_freqs_inter(self.edges[i], iib)
                            print(nus_inter)
                            dustmaps_inter = get_dust_sky(
                                nus_inter, skyconf, self.nside)
                            dust_maps_i = np.mean(dustmaps_inter, axis=0)
                            dustmaps[i] = dust_maps_i.copy()

                    allmaps += dustmaps

                else:
                    print('No dust')

            elif i == 'synchrotron':
                syncmaps = np.zeros(((len(self.nus), 3, self.npix)))
                print('Model : {}'.format(self.skyconfig[i]))

                for i in range(len(self.nus)):
                    nus_inter = get_freqs_inter(self.edges[i], iib)
                    #print(nus_inter)
                    sync_maps_inter = create_sync(self.nside, nus_inter)
                    mean_sync_maps = np.mean(sync_maps_inter, axis=0)
                    syncmaps[i] = mean_sync_maps.copy()
                allmaps += syncmaps
                map_index = []

            else:
                print('No more components')
                #pass

        #hp.mollview(allmaps[0, 1])

        if same_resol != 0:
            for j in range(len(self.fwhmdeg)):
                if verbose:
                    print('Convolution to {:.2f} deg'.format(self.fwhmdeg[j]))
                allmaps[j] = hp.sphtfunc.smoothing(allmaps[j, :, :],
                                                   fwhm=np.deg2rad(
                                                       self.fwhmdeg[j]),
                                                   verbose=False)

        if noise:
            noisemaps = create_noisemaps(signoise, self.nus, self.nside,
                                         self.depth_i, self.depth_p, self.npix)
            maps_noisy = allmaps + noisemaps

            if coverage is not None:
                pixok = coverage > 0
                maps_noisy[:, :, ~pixok] = hp.UNSEEN
                noisemaps[:, :, ~pixok] = hp.UNSEEN
                allmaps[:, :, ~pixok] = hp.UNSEEN

            return maps_noisy, allmaps, noisemaps
        return allmaps
Exemple #11
0
    def get_partial_sky_maps_withnoise(self, coverage=None, version_FastSim='01', sigma_sec=None,
                                       Nyears=4., FWHMdeg=None, seed=None,
                                       noise_profile=True, spatial_noise=True, nunu_correlation=True,
                                       noise_only=False, integrate_into_band=True,
                                       verbose=False):
        """
        This returns maps in the same way as with get_simple_sky_map but cut according to the coverage
        and with noise added according to this coverage and the RMS in muK.sqrt(sec) given by sigma_sec
        The default integration time is 4 years but can be modified with optional variable Nyears
        Note that the maps are convolved with the instrument beam by default, or with FWHMdeg (can be an array)
        if provided.
        If seed is provided, it will be used for the noise realization. If not it will be a new realization at
        each call.
        The optional effective_variance_invcov keyword is a modification law to be applied to the coverage in order to obtain
        more realistic noise profile. It is a law for effective RMS as a function of inverse coverage and is 2D array
        with the first one being (nx samples) inverse coverage and the second being the corresponding effective variance to be
        used through interpolation when generating the noise.
        
        Parameters
        ----------
        coverage: array
            Coverage map of the sky.
            By default, we load a coverage centered on the galactic center with 10000 pointings.
        version_FastSim: str
            Version of the FastSimulator files: 01, 02, 03... For now, only 01 exists.
        sigma_sec: float
        Nyears: float
            Integration time for observation to scale the noise, by default it is 4.
        FWHMdeg:
        seed:
        noise_profile:
        spatial_noise: bool
            If True, spatial noise correlations are added. True by default.
        nunu_correlation: bool
            If True, correlations between frequency sub-bands are added. True by default.
        noise_only: bool
            If True, only returns the noise maps and the coverage (without the sky signal).
        integrate_into_band: bool
            If True, averaging input sub-band maps into reconstruction sub-bands. True by default.
        verbose: bool

        Returns
        -------
        maps + noisemaps, maps, noisemaps, coverage

        """

        ### Input bands
        Nfreq_edges, nus_edge, nus, deltas, Delta, Nbbands = qubic.compute_freq(self.filter_nu,
                                                                                self.Nfin,
                                                                                self.filter_relative_bandwidth)
        ### Output bands
        # Check Nfout is between 1 and 8.
        if self.Nfout < 1 or self.Nfout > 8:
            raise NameError("Nfout should be contained between 1 and 8 for FastSimulation.")
        Nfreq_edges_out, nus_edge_out, nus_out, deltas_out, Delta_out, Nbbands_out = qubic.compute_freq(self.filter_nu,
                                                                                                        self.Nfout,
                                                                                                        self.filter_relative_bandwidth)

        # First get the convolved maps
        if noise_only is False:
            maps = np.zeros((self.Nfout, self.npix, 3))
            if integrate_into_band:
                if verbose:
                    print('Convolving each input frequency map')
                maps_all = self.get_fullsky_convolved_maps(FWHMdeg=FWHMdeg, verbose=verbose)

                # Now averaging maps into reconstruction sub-bands maps
                if verbose:
                    print('Averaging input maps from input sub-bands into reconstruction sub-bands:')
                for i in range(self.Nfout):
                    print('doing band {} {} {}'.format(i, nus_edge_out[i], nus_edge_out[i + 1]))
                    inband = (nus > nus_edge_out[i]) & (nus < nus_edge_out[i + 1])
                    maps[i, :, :] = np.mean(maps_all[inband, :, :], axis=0)
            else:
                for i in range(self.Nfout):
                    freq = nus_out[i]
                    maps[i, :, :] = (self.sky.get_emission(freq * u.GHz)
                                     * utils.bandpass_unit_conversion(freq * u.GHz,
                                                                      weights=None,
                                                                      output_unit=u.uK_CMB)).T
                _, maps = self.smoothing(maps, FWHMdeg, self.Nfout, nus_out, verbose=verbose)

        ##############################################################################################################
        # Restore data for FastSimulation ############################################################################
        ##############################################################################################################
        #### Directory for fast simulations
        dir_fast = os.path.join(os.path.dirname(__file__), 'data', f'FastSimulator_version{version_FastSim}')
        #### Integration time assumed in FastSim files
        fastsimfile_effective_duration = 2.

        with open(dir_fast + os.sep + 'DataFastSimulator_{}{}_nfsub_{}.pkl'.format(self.dictionary['config'],
                                                                          str(self.filter_nu),
                                                                          self.Nfout),
                  "rb") as file:
            DataFastSim = pickle.load(file)
            print(file)
        # Read Coverage map
        if coverage is None:
            DataFastSimCoverage = pickle.load(open(dir_fast + os.sep + 'DataFastSimulator_{}{}_coverage.pkl'.format(
                                                       self.dictionary['config'],
                                                       str(self.filter_nu)), "rb"))
            coverage = DataFastSimCoverage['coverage']
        # Read noise normalization
        if sigma_sec is None:
            #### Beware ! Initial End-To-End simulations that produced the first FastSimulator were done with
            #### Effective_duration = 4 years and this is the meaning of signoise
            #### New files were done with 2 years and as result the signoise needs to be multiplied by sqrt(effective_duration/4)
            sigma_sec = DataFastSim['signoise'] * np.sqrt(fastsimfile_effective_duration / 4.)

        # # Read Nyears
        # if Nyears is None:
        #     Nyears = DataFastSim['years']

        # Read Noise Profile
        if noise_profile is True:
            effective_variance_invcov = DataFastSim['effective_variance_invcov']
        else:
            effective_variance_invcov = None

        # Read Spatial noise correlation
        if spatial_noise is True:
            clnoise = DataFastSim['clnoise']
        else:
            clnoise = None

        # Read Noise Profile
        if nunu_correlation is True:
            covI = DataFastSim['CovI']
            covQ = DataFastSim['CovQ']
            covU = DataFastSim['CovU']
            sub_bands_cov = [covI, covQ, covU]
        else:
            sub_bands_cov = None
        ##############################################################################################################

        # Now pure noise maps
        if verbose:
            print('Making noise realizations')
        noisemaps = self.create_noise_maps(sigma_sec, coverage, nsub=self.Nfout,
                                           Nyears=Nyears, verbose=verbose, seed=seed,
                                           effective_variance_invcov=effective_variance_invcov,
                                           clnoise=clnoise,
                                           sub_bands_cov=sub_bands_cov)
        if self.Nfout == 1:
            noisemaps = np.reshape(noisemaps, (1, len(coverage), 3))
        seenpix = noisemaps[0, :, 0] != 0
        coverage[~seenpix] = 0

        if noise_only:
            return noisemaps, coverage
        else:
            maps[:, ~seenpix, :] = 0
            return maps + noisemaps, maps, noisemaps, coverage