if patch is not None: survey_mask.data *= patch.data dist = so_window.get_distance(survey_mask, rmax=apod_survey_degree * np.pi / 180) # so here we create a binary mask this will only be used in order to skip the edges before applying the kspace filter # this step is a bit arbitrary and preliminary, more work to be done here binary = survey_mask.copy() if d["src_free_maps"] == False: binary.data *= ps_mask.data # Note that we don't skip the edges as much for the binary mask # compared to what we will do with the final window, this should prevent some aliasing from the kspace filter to enter the data # We also include the point source mask in the binary, this should not be necessary at the end since point source removal can # happen at the map maker level binary.data[dist.data < skip_from_edges_degree / 2] = 0 binary.write_map("%s/binary_%s_%s.fits" % (window_dir, sv, ar)) binary = binary.downgrade(4) binary.plot(file_name="%s/binary_%s_%s" % (window_dir, sv, ar)) # Now we create the final window function that will be used in the analysis survey_mask.data[dist.data < skip_from_edges_degree] = 0 survey_mask = so_window.create_apodization(survey_mask, "C1", apod_survey_degree, use_rmax=True) ps_mask = so_window.create_apodization(ps_mask, "C1", apod_pts_source_degree, use_rmax=True) survey_mask.data *= ps_mask.data survey_mask.write_map("%s/window_%s_%s.fits" % (window_dir, sv, ar)) survey_mask = survey_mask.downgrade(4) survey_mask.plot(file_name="%s/window_%s_%s" % (window_dir, sv, ar))
split.data += noise.data #First let's generate a CMB realisation cmb = template.synfast(clfile) split = cmb.copy() #let's add noise to it with rms 20 uk.arcmin in T ans sqrt(2)xthat in pol noise = so_map.white_noise(split, rms_uKarcmin_T=rms_uKarcmin_T) split.data += noise.data split.plot(file_name="%s/noisy_cmb" % (test_dir), ticks_spacing_car=4) print("Generate window function") #we then apodize the survey mask window = so_window.create_apodization( binary, apo_type="Rectangle", apo_radius_degree=apo_radius_degree_survey) #we create a point source mask mask = so_map.simulate_source_mask(binary, n_holes=source_mask_nholes, hole_radius_arcmin=source_mask_radius) #... and we apodize it mask = so_window.create_apodization(mask, apo_type="C1", apo_radius_degree=apo_radius_degree_mask) #the window is given by the product of the survey window and the mask window window.data *= mask.data #let's look at it window.plot(file_name="%s/window" % (test_dir), ticks_spacing_car=4) #for spin0 and 2 the window need to be a tuple made of two objects
def create_window(patch, maps_info_list, apo_radius_survey=1, res_arcmin=0.5, galactic_mask=None, source_mask=None, compute_T_only=False): """Create a window function for a patch Parameters ---------- patch: dict a dict containing the patch type and coordinates if patch_type is "Rectangle" the coordinates are expected to be the 4 corners if patch_type is "Disk" we expect the coordinates 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 apo_radius_survey: float the apodisation radius in degree (default: 1 degree) res_arcmin: float the resolution in arcminutes (default: 0.5 arcmin) 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 """ if patch["patch_type"] == "Rectangle": car_box = patch["patch_coordinate"] window = so_map.read_map(maps_info_list[0]["name"], car_box=car_box) if maps_info_list[0]["data_type"] == "IQU": window.data = window.data[0] window.ncomp = 1 window.data[:] = 0 window.data[1:-1, 1:-1] = 1 apo_type_survey = "C1" elif patch["patch_type"] == "Disk": dec_c, ra_c = patch["center"] radius = patch["radius"] eps = 0.1 car_box = [[dec_c - radius - eps, ra_c - radius - eps], [dec_c + radius + eps, ra_c + radius + eps]] window = so_map.read_map(maps_info_list[0]["name"], car_box=car_box) if maps_info_list[0]["data_type"] == "IQU": window.data = window.data[0] window.ncomp = 1 window.data[:] = 1 y_c, x_c = enmap.sky2pix(window.data.shape, window.data.wcs, [dec_c * np.pi / 180, ra_c * np.pi / 180]) window.data[int(y_c), int(x_c)] = 0 dist = distance_transform_edt(window.data) * res_arcmin * 1 / 60 window.data[dist < radius] = 0 window.data = 1 - window.data apo_type_survey = "C1" else: raise ValueError("Patch type '{}' is not supported".format( patch["patch_type"])) if galactic_mask is not None: gal_mask = so_map.read_map(galactic_mask, car_box=car_box) window.data *= gal_mask.data del gal_mask for map_info in maps_info_list: split = so_map.read_map(map_info["name"], car_box=car_box) if compute_T_only and map_info["data_type"] == "IQU": split.data = split.data[0] split.ncomp = 1 if split.ncomp == 1: window.data[split.data == 0] = 0. else: for i in range(split.ncomp): window.data[split.data[i] == 0] = 0. window = so_window.create_apodization(window, apo_type=apo_type_survey, apo_radius_degree=apo_radius_survey) if source_mask is not None: ps_mask = so_map.read_map(source_mask["name"], car_box=car_box) ps_mask = so_window.create_apodization( ps_mask, apo_type=source_mask["apo_type"], apo_radius_degree=source_mask["apo_radius"]) window.data *= ps_mask.data del ps_mask return car_box, window
print("Loading masks for nside =", nside) galactic_mask = np.load("./masks/mask_galactic_1024.npz")["mask"] survey_mask = np.load("./masks/mask_survey_1024.npz")["mask"] mask = galactic_mask * survey_mask mask = hp.ud_grade(mask, nside_out=nside) assert np.all((mask == 1) | (mask == 0)), "Mask with values others than 0 or 1" from pspy import so_map 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
log10_CO_noise_ratio = so_map.read_map( "%s/HFI_BiasMap_%s-CO-noiseRatio_2048_R3.00_full.fits" % (EB_mask_dir, freq), fields_healpix=0) id = np.where(log10_CO_noise_ratio.data > -2) CO_mask[id] = 0 missing_pixel = np.ones(12 * nside**2) half_mission = [1, 2] for hm in half_mission: for c, field in enumerate(["I", "Q", "U"]): map = so_map.read_map( "%s/HFI_SkyMap_%s_2048_R3.01_halfmission-%s.fits" % (maps_dir, freq, hm), fields_healpix=c) id = np.where(map.data < -10**30) missing_pixel[id] = 0 cov = so_map.read_map( "%s/HFI_SkyMap_%s_2048_R3.01_halfmission-%s.fits" % (maps_dir, freq, hm), fields_healpix=4) id = np.where(cov.data < 0) missing_pixel[id] = 0 window.data[:] = missing_pixel * ps_mask["F%s" % freq] * CO_mask window = so_window.create_apodization(window, apo_type="C1", apo_radius_degree=2) window.write_map("%s/window_%s.fits" % (EB_mask_dir, freq)) window.plot(file_name="%s/window_%s" % (EB_mask_dir, freq))
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.healpix_template(ncomp, nside=nside) # the templates for the binary mask binary = so_map.healpix_template(ncomp=1, nside=nside) # we set pixel inside the disk at 1 and pixel outside at zero vec = hp.pixelfunc.ang2vec(lon, lat, lonlat=True) disc = hp.query_disc(nside, vec, radius=radius * np.pi / 180) binary.data[disc] = 1 print("Generate window function") # we then apodize the survey mask window = so_window.create_apodization( binary, apo_type="C1", apo_radius_degree=apo_radius_degree_survey) window.plot(file_name="%s/window" % (test_dir)) # generate theory power spectrum ps_theory = {} import camb camb_lmin = 0 camb_lmax = 10000 l_camb = np.arange(camb_lmin, camb_lmax) cosmo_params = { "H0": 67.5, "As": 1e-10 * np.exp(3.044), "ombh2": 0.02237, "omch2": 0.1200, "ns": 0.9649,
gal_mask = so_map.read_map(d["galactic_mask_%s_file_%s" % (exp, freq)]) gal_mask.plot(file_name="%s/gal_mask_%s_%s" % (plot_dir, exp, freq)) window.data[:] *= gal_mask.data[:] if d["survey_mask_%s" % exp] == True: survey_mask = so_map.read_map(d["survey_mask_%s_file_%s" % (exp, freq)]) survey_mask.plot(file_name="%s/survey_mask_mask_%s_%s" % (plot_dir, exp, freq)) window.data[:] *= survey_mask.data[:] apo_radius_degree = (d["apo_radius_survey_%s" % exp]) window = so_window.create_apodization( window, apo_type=d["apo_type_survey_%s" % exp], apo_radius_degree=apo_radius_degree) if d["pts_source_mask_%s" % exp] == True: hole_radius_arcmin = (d["source_mask_radius_%s" % exp]) mask = so_map.simulate_source_mask( binary, n_holes=d["source_mask_nholes_%s" % exp], hole_radius_arcmin=hole_radius_arcmin) mask = so_window.create_apodization( mask, apo_type=d["apo_type_mask_%s" % exp], apo_radius_degree=d["apo_radius_mask_%s" % exp]) window.data[:] *= mask.data[:] window.write_map("%s/window_%s_%s.fits" % (window_dir, exp, freq))
"data_type": "IQU", "id": name, "cal": None }] print("generate window") car_box, window = ps_tools.create_window(patch, maps_info_list, d["apo_radius_survey_degree"], d["res_arcmin"], compute_T_only=d["compute_T_only"]) # multiply by an additional source mask nholes_degsq = d["nholes_degsq"] hole_radius_arcmin = d["hole_radius_arcmin"] apo_radius_degree_pts_source = d["apo_radius_degree_pts_source"] n_holes = int((ra1 - ra0) * (dec1 - dec0) * nholes_degsq) print(n_holes) ps_mask = window.copy() ps_mask.data[:] = 1 ps_mask = so_map.simulate_source_mask(ps_mask, n_holes, hole_radius_arcmin) ps_mask = so_window.create_apodization( ps_mask, apo_type="C1", apo_radius_degree=apo_radius_degree_pts_source) window.data *= ps_mask.data window.write_map("%s/window.fits" % (window_dir)) window.plot(file_name="%s/window" % (plot_dir)) np.savetxt("%s/car_box.dat" % (window_dir), car_box)
def create_window( patch, maps_info_list, apo_radius_survey=1, res_arcmin=0.5, galactic_mask=None, source_mask=None, compute_T_only=False, use_rmax=True, use_kspace_filter=False, ): """Create a window function for a patch Parameters ---------- patch: dict a dict containing the patch type and coordinates if patch_type is "Rectangle" the coordinates are expected to be the 4 corners if patch_type is "Disk" we expect the coordinates 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 apo_radius_survey: float the apodisation radius in degree (default: 1 degree) res_arcmin: float the angular resolution of the map in arcminutes (default: 0.5 arcminutes) 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 compute_T_only: boolean only use temperature field use_rmax: boolean apply apodization up to the apodization radius use_kspace_filter: boolean create a binary mask to be only used when applying kspace filter to maps """ timer.start("Create window...") if patch["patch_type"] == "Rectangle": car_box = patch["patch_coordinate"] window = so_map.read_map(maps_info_list[0]["name"], car_box=car_box) if maps_info_list[0]["data_type"] == "IQU": window.data = window.data[0] window.ncomp = 1 window.data[:] = 0 window.data[1:-1, 1:-1] = 1 apo_type_survey = "C1" elif patch["patch_type"] == "Disk": dec_c, ra_c = patch["center"] radius = patch["radius"] eps = 0.1 car_box = [ [dec_c - radius - eps, ra_c - radius - eps], [dec_c + radius + eps, ra_c + radius + eps], ] window = so_map.read_map(maps_info_list[0]["name"], car_box=car_box) if maps_info_list[0]["data_type"] == "IQU": window.data = window.data[0] window.ncomp = 1 window.data[:] = 1 y_c, x_c = enmap.sky2pix(window.data.shape, window.data.wcs, [dec_c * np.pi / 180, ra_c * np.pi / 180]) window.data[int(y_c), int(x_c)] = 0 dist = distance_transform_edt(window.data) * res_arcmin * 1 / 60 window.data[dist < radius] = 0 window.data = 1 - window.data apo_type_survey = "C1" if galactic_mask is not None: gal_mask = so_map.read_map(galactic_mask["name"], car_box=car_box) window.data *= gal_mask.data del gal_mask for map_info in maps_info_list: split = so_map.read_map(map_info["name"], car_box=car_box) if compute_T_only and map_info["data_type"] == "IQU": split.data = split.data[0] split.ncomp = 1 if split.ncomp == 1: window.data[split.data == 0] = 0.0 else: for i in range(split.ncomp): window.data[split.data[i] == 0] = 0.0 # Binary mask for kspace filter binary = window.copy() if use_kspace_filter else None window = so_window.create_apodization(window, apo_type=apo_type_survey, apo_radius_degree=apo_radius_survey, use_rmax=use_rmax) if source_mask is not None: ps_mask = so_map.read_map(source_mask["name"], car_box=car_box) if use_kspace_filter: binary.data *= ps_mask.data ps_mask = so_window.create_apodization( ps_mask, apo_type=source_mask["apo_type"], apo_radius_degree=source_mask["apo_radius"]) window.data *= ps_mask.data del ps_mask timer.stop() return car_box, window, binary
# option to read an additional weight map if d['use_pixel_weight'] == True: weight = so_map.read_map(d['hitmaps_%s_%s' % (exp, f)]) else: weight = mask_T.copy() weight.data[:] = 1 mask_T.data *= survey_mask.data mask_P.data *= survey_mask.data # apodize the temperature window function and multiply it by the weight map, plot it and write it to disk window_T = so_window.create_apodization( mask_T, apo_type=d['apo_type_survey_%s' % exp], apo_radius_degree=d['apo_radius_survey_%s' % exp]) window_T.data *= weight.data window_T.write_map('%s/window_T_%s_%s.fits' % (window_dir, exp, f)) window_T.plot(file_name='%s/window_T_%s_%s' % (plot_dir, exp, f)) # apodize the polarisation window function and multiply it by the weight map, plot it and write it to disk window_P = so_window.create_apodization( mask_P, apo_type=d['apo_type_survey_%s' % exp], apo_radius_degree=d['apo_radius_survey_%s' % exp]) window_P.data *= weight.data window_P.write_map('%s/window_P_%s_%s.fits' % (window_dir, exp, f))
noise_map.data[:] *= K_to_muK * np.sqrt(2) noise_map.plot(file_name="%s/%s_map_%s" % (plot_dir, split, scan), color_range=vrange) if include_cmb == True: sim[split] = cmb.copy() sim[split].data[:] += noise_map.data[:] else: sim[split] = noise_map.copy() binary.plot(file_name="%s/binary_%s" % (plot_dir, scan)) for run in runs: window = so_window.create_apodization( binary, apo_type=apo_type, apo_radius_degree=apo_radius_degree) if run == "weighted": print("Use hits maps") hmap = so_map.read_map("%s/%s_telescope_all_time_all_hmap.fits" % (map_dir["split0"], scan)) window.data *= hmap.data else: print("Uniform weighting") window.write_map("%s/window_%s_%s.fits" % (window_dir, scan, run)) window.plot(file_name="%s/window_%s_%s" % (plot_dir, scan, run), title=scan) window = (window, window)