Example #1
0
# the radius of the holes (in arcminutes)
source_mask_radius = 30
# the apodisation lengh for the point source mask (in degree)
apo_radius_degree_mask = 1
# the type of power spectrum (Cl or Dl)
type = "Cl"

test_dir = "result_pspyVSnamasterCAR_spin0and2"
try:
    os.makedirs(test_dir)
except:
    pass

# create a binningfile with format, lmin,lmax,lmean
pspy_utils.create_binning_file(bin_size=40,
                               n_bins=300,
                               file_name="%s/binning.dat" % test_dir)
binning_file = "%s/binning.dat" % test_dir

# the templates for the CMB splits
template = so_map.car_template(ncomp, ra0, ra1, dec0, dec1, res)
# the templates for the binary mask
binary = so_map.car_template(1, ra0, ra1, dec0, dec1, res)
# for CAR we set pixels inside the survey at 1 and  at the border to be zero
binary.data[:] = 0
binary.data[1:-1, 1:-1] = 1

print("Generate CMB realisation")

#First let's generate a CMB realisation
cmb = template.synfast(clfile)
patch = {"patch_type": "Disk", "center": [0, 0], "radius": 8}
apo_radius_survey = 1

# Spectra parameters
ps_method = "master"
error_method = "master"
bin_size = 40
compute_T_only = False
lmax = 2000
type = "Dl"
master_threshold = None
lmax_pad = None
beam = None  # Not ready yet

pspy_utils.create_binning_file(bin_size=bin_size,
                               n_bins=1000,
                               file_name="binning.dat")
binning_file = "binning.dat"

all_dict = []
for iii in range(n_sims):
    t = time.time()

    cmb = template_car.synfast(clfile)

    for i, info in enumerate(maps_info_list):
        split = cmb.copy()
        noise = so_map.white_noise(split, rms_uKarcmin_T=rms_uKarcmin_T)
        split.data += noise.data
        split.write_map(info["name"])
Example #3
0
def compute_ps(patch,
               maps_info_list,
               ps_method="master",
               error_method="master",
               type="Dl",
               binning_file=None,
               bin_size=None,
               beam=None,
               galactic_mask=None,
               source_mask=None,
               apo_radius_survey=1,
               compute_T_only=False,
               lmax=1000,
               lmax_pad=None,
               l_thres=None,
               l_toep=None):
    """Compute spectra

    Parameters
    ----------
    patch: dict
      a dict containing the patch type and coordinates
      if patch_type is "Rectangle" the coordinate are expected to be the 4 corners
      if patch_type is "Disk" we expect the coordinate of the center and the radius in degree
    maps_info_list: list of dicts describing the data maps
      dictionnary should contain the name, the data type ("IQU" or "I") and optionally a calibration factor to apply to the map
      note that all map in the list should have the same data type
    beam: text file
      file describing the beam of the map, expect bl to be the second column and start at l=0 (standard is : l,bl, ...)
    binning_file: text file
      a binning file with three columns bin low, bin high, bin mean
      note that either binning_file or bin_size should be provided
    bin_size: integer
      the bin size
      note that either binning_file or bin_size should be provided
    type: string
      the type of binning, either bin Cl or bin Dl
    source_mask: dict
      a dict containing an optional source mask and its properties
      the dictionnary should contain the name, the type of apodisation and the radius of apodisation
    galactic_mask: fits file
      an optional galactic mask to apply
    apo_radius_survey: float
      the apodisation radius in degree
    ps_method: string
      the method for the computation of the power spectrum
      can be "master", "pseudo", or "2dflat" for now
    error_method: string
      the method for the computation of error
      can be "master" or "knox" for now
    compute_T_only: boolean
      True to compute only T spectra, should always be true for data_type= "I"
    lmax : integer
      the maximum multipole to consider for the spectra computation
    lmax_pad: integer
      the maximum multipole to consider for the mcm computation (optional)
      lmax_pad should always be greater than lmax
    """

    if binning_file is None:
        pspy_utils.create_binning_file(bin_size=bin_size,
                                       n_bins=1000,
                                       file_name="binning.dat")
        binning_file = "binning.dat"

    car_box, window = create_window(patch,
                                    maps_info_list,
                                    apo_radius_survey,
                                    galactic_mask=galactic_mask,
                                    source_mask=source_mask,
                                    compute_T_only=compute_T_only)

    mbb_inv = compute_mode_coupling(window,
                                    type,
                                    lmax,
                                    binning_file,
                                    ps_method=ps_method,
                                    beam=beam,
                                    lmax_pad=lmax_pad,
                                    l_thres=l_thres,
                                    l_toep=l_toep,
                                    compute_T_only=compute_T_only)

    spectra, spec_name_list, ells, ps_dict = get_spectra(
        window,
        maps_info_list,
        car_box,
        type,
        lmax,
        binning_file,
        ps_method=ps_method,
        mbb_inv=mbb_inv,
        compute_T_only=compute_T_only)

    if ps_method == "2dflat" or error_method is None:
        return spectra, spec_name_list, ells, ps_dict, None

    cov_dict = get_covariance(window,
                              lmax,
                              spec_name_list,
                              ps_dict,
                              binning_file,
                              error_method=error_method,
                              l_thres=l_thres,
                              l_toep=l_toep,
                              spectra=spectra,
                              mbb_inv=mbb_inv,
                              compute_T_only=compute_T_only)

    return spectra, spec_name_list, ells, ps_dict, cov_dict
Example #4
0
survey = so_map.healpix_template(ncomp=1, nside=nside)
survey.data = mask

from pspy import so_window
survey = so_window.create_apodization(survey,
                                      apo_type="C1",
                                      apo_radius_degree=5)

print("Generating binning file")
import os
output_dir = "/tmp/pysm"
os.makedirs(output_dir, exist_ok=True)
binning_file = os.path.join(output_dir, "binning.dat")
from pspy import pspy_utils
pspy_utils.create_binning_file(bin_size=40,
                               n_bins=1000,
                               file_name=binning_file)

print("Computing MCM")
window = (survey, survey)
niter = 3
from pspy import so_mcm
mbb_inv, bbl = so_mcm.mcm_and_bbl_spin0and2(window,
                                            binning_file,
                                            lmax=lmax,
                                            type="Dl",
                                            niter=niter)

dust = ["d0", "d1", "d2", "d4", "d6"]  # "d5"
synchrotron = ["s1", "s2", "s3"]
ame = ["a1", "a2"]
Example #5
0
def compute_ps(
    patch,
    maps_info_list,
    ps_method="master",
    error_method="master",
    type="Dl",
    binning_file=None,
    bin_size=None,
    beam_file=None,
    galactic_mask=None,
    source_mask=None,
    apo_radius_survey=1,
    compute_T_only=False,
    lmax=1000,
    l_exact=None,
    l_band=None,
    l_toep=None,
    vk_mask=None,
    hk_mask=None,
    transfer_function=None,
):
    """Compute spectra

    Parameters
    ----------
    patch: dict
      a dict containing the patch type and coordinates
      if patch_type is "Rectangle" the coordinate are expected to be the 4 corners
      if patch_type is "Disk" we expect the coordinate of the center and the radius in degree
    maps_info_list: list of dicts describing the data maps
      dictionnary should contain the name, the data type ("IQU" or "I") and optionally a calibration factor to apply to the map
      note that all map in the list should have the same data type
    beam_file: text file
      file describing the beam of the map, expect l,bl
    binning_file: text file
      a binning file with three columns bin low, bin high, bin mean
      note that either binning_file or bin_size should be provided
    bin_size: integer
      the bin size
      note that either binning_file or bin_size should be provided
    type: string
      the type of binning, either bin Cl or bin Dl
    source_mask: dict
      a dict containing an optional source mask and its properties
      the dictionnary should contain the name, the type of apodisation and the radius of apodisation
    galactic_mask: fits file
      an optional galactic mask to apply
    apo_radius_survey: float
      the apodisation radius in degree
    ps_method: string
      the method for the computation of the power spectrum
      can be "master", "pseudo", or "2dflat" for now
    error_method: string
      the method for the computation of error
      can be "master" or "knox" for now
    compute_T_only: boolean
      True to compute only T spectra, should always be true for data_type= "I"
    lmax: integer
      the maximum multipole to consider for the spectra computation
    vk_mask: list
      the vertical band to filter out from 2D FFT (format is [-lx, +lx])
    hk_mask: list
      the horizontal band to filter out from 2D FFT (format is [-ly, +ly])
    transfer_function: str
      the path to the transfer function
    """

    # Check computation mode
    for map_info in maps_info_list:
        if not compute_T_only and map_info["data_type"] == "I":
            raise ValueError(
                "Only temperature computation can be done given data type! Check your configuration."
            )

    # Check file path
    if binning_file is not None:
        if not os.path.exists(binning_file):
            raise ValueError("No binning file at '{}'".format(binning_file))
    if beam_file is not None:
        if not os.path.exists(beam_file):
            raise ValueError("No beam file at '{}'".format(beam_file))

    if binning_file is None and ps_method != "2dflat":
        if bin_size is None:
            raise ValueError("Missing binning size!")
        pspy_utils.create_binning_file(bin_size=bin_size,
                                       n_bins=1000,
                                       file_name="binning.dat")
        binning_file = "binning.dat"

    use_kspace_filter = False
    if vk_mask is not None or hk_mask is not None:
        if transfer_function is None:
            raise ValueError(
                "Missing transfer function to correct for kpsace filter")
        use_kspace_filter = True

    car_box, window, binary = create_window(
        patch,
        maps_info_list,
        apo_radius_survey,
        galactic_mask=galactic_mask,
        source_mask=source_mask,
        compute_T_only=compute_T_only,
        use_kspace_filter=use_kspace_filter,
    )

    mbb_inv = compute_mode_coupling(
        window,
        type,
        lmax,
        binning_file,
        ps_method=ps_method,
        beam_file=beam_file,
        l_exact=l_exact,
        l_band=l_band,
        l_toep=l_toep,
        compute_T_only=compute_T_only,
    )

    spectra, spec_name_list, ells, ps_dict = get_spectra(
        window,
        maps_info_list,
        car_box,
        type,
        lmax,
        binning_file,
        ps_method=ps_method,
        mbb_inv=mbb_inv,
        compute_T_only=compute_T_only,
        vk_mask=vk_mask,
        hk_mask=hk_mask,
        transfer_function=transfer_function,
        binary=binary,
    )

    if ps_method == "2dflat" or error_method is None:
        return spectra, spec_name_list, ells, ps_dict, None

    ps_dict_for_cov = theory_for_covariance(ps_dict,
                                            spec_name_list,
                                            spectra,
                                            lmax,
                                            beam_file=beam_file,
                                            binning_file=binning_file)

    cov_dict = get_covariance(
        window,
        lmax,
        spec_name_list,
        ps_dict_for_cov,
        binning_file,
        error_method=error_method,
        l_exact=l_exact,
        l_band=l_band,
        l_toep=l_toep,
        spectra=spectra,
        mbb_inv=mbb_inv,
        compute_T_only=compute_T_only,
        transfer_function=transfer_function,
    )

    return spectra, spec_name_list, ells, ps_dict, cov_dict