|
| 1 | +""" |
| 2 | +This script compute all alms write them to disk. |
| 3 | +It uses the window function provided in the dictionnary file. |
| 4 | +Optionally, it applies a calibration to the maps, a kspace filter and deconvolve the CAR pixel window function. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import time |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import healpy as hp |
| 12 | +from pixell import enmap |
| 13 | +from pspipe_utils import kspace, log, misc, pspipe_list |
| 14 | +from pspy import pspy_utils, so_dict, so_mpi, sph_tools, so_map |
| 15 | + |
| 16 | + |
| 17 | +d = so_dict.so_dict() |
| 18 | +d.read_from_file(sys.argv[1]) |
| 19 | +log = log.get_logger(**d) |
| 20 | + |
| 21 | +surveys = d["surveys"] |
| 22 | +lmax = d["lmax"] |
| 23 | +deconvolve_pixwin = d["deconvolve_pixwin"] |
| 24 | +niter = d["niter"] |
| 25 | +apply_kspace_filter = d["apply_kspace_filter"] |
| 26 | +apply_alm_mask = d["apply_alm_mask"] |
| 27 | + |
| 28 | +alms_dir = "alms" |
| 29 | +pspy_utils.create_directory(alms_dir) |
| 30 | + |
| 31 | +n_ar, sv_list, ar_list = pspipe_list.get_arrays_list(d) |
| 32 | + |
| 33 | +log.info(f"number of arrays for the mpi loop : {n_ar}") |
| 34 | +so_mpi.init(True) |
| 35 | +subtasks = so_mpi.taskrange(imin=0, imax=n_ar-1) |
| 36 | + |
| 37 | +for task in subtasks: |
| 38 | + task = int(task) |
| 39 | + sv, ar = sv_list[task], ar_list[task] |
| 40 | + alm_conv = d[f"alm_conv_{sv}"] |
| 41 | + |
| 42 | + log.info(f"[{task}] Computing alm for '{sv}' survey and '{ar}' array") |
| 43 | + |
| 44 | + win_T = so_map.read_map(d[f"window_T_{sv}_{ar}"]) |
| 45 | + win_pol = so_map.read_map(d[f"window_pol_{sv}_{ar}"]) |
| 46 | + |
| 47 | + window_tuple = (win_T, win_pol) |
| 48 | + |
| 49 | + |
| 50 | + if win_T.pixel == "CAR": |
| 51 | + win_kspace = so_map.read_map(d[f"window_kspace_{sv}_{ar}"]) |
| 52 | + |
| 53 | + if apply_kspace_filter: |
| 54 | + ks_f = d[f"k_filter_{sv}"] |
| 55 | + filter = kspace.get_kspace_filter(win_T, ks_f) |
| 56 | + |
| 57 | + inv_pixwin_lxly = None |
| 58 | + if deconvolve_pixwin: |
| 59 | + if d[f"pixwin_{sv}"]["pix"] == "CAR": |
| 60 | + # compute the CAR pixel function in fourier space |
| 61 | + wy, wx = enmap.calc_window(win_T.data.shape, order=d[f"pixwin_{sv}"]["order"]) |
| 62 | + inv_pixwin_lxly = (wy[:,None] * wx[None,:]) ** (-1) |
| 63 | + |
| 64 | + |
| 65 | + maps = d[f"maps_{sv}_{ar}"] |
| 66 | + cal, pol_eff = d[f"cal_{sv}_{ar}"], d[f"pol_eff_{sv}_{ar}"] |
| 67 | + |
| 68 | + t0 = time.time() |
| 69 | + for k, map in enumerate(maps): |
| 70 | + |
| 71 | + if win_T.pixel == "CAR": |
| 72 | + split = so_map.read_map(map, geometry=win_T.data.geometry) |
| 73 | + |
| 74 | + if d[f"src_free_maps_{sv}"] == True: |
| 75 | + ps_map_name = map.replace("_srcfree.fits", ".fits") |
| 76 | + if ps_map_name == map: |
| 77 | + raise ValueError("name should contain srcfree, check map names!") |
| 78 | + ps_map = so_map.read_map(ps_map_name, geometry=win_T.data.geometry) |
| 79 | + ps_map.data -= split.data |
| 80 | + |
| 81 | + |
| 82 | + ps_mask = so_map.read_map(d[f"ps_mask_{sv}_{ar}"]) |
| 83 | + ps_map.data *= ps_mask.data |
| 84 | + split.data += ps_map.data |
| 85 | + |
| 86 | + if apply_kspace_filter: |
| 87 | + log.info(f"[{task}] apply kspace filter on {map}") |
| 88 | + split = kspace.filter_map(split, |
| 89 | + filter, |
| 90 | + win_kspace, |
| 91 | + inv_pixwin=inv_pixwin_lxly, |
| 92 | + weighted_filter=ks_f["weighted"], |
| 93 | + use_ducc_rfft=True) |
| 94 | + |
| 95 | + else: |
| 96 | + log.info(f"[{task}] WARNING: no kspace filter is applied") |
| 97 | + if (deconvolve_pixwin) & (inv_pixwin_lxly is not None): |
| 98 | + split = so_map.fourier_convolution(split, |
| 99 | + inv_pixwin_lxly, |
| 100 | + window=win_kspace, |
| 101 | + use_ducc_rfft=True) |
| 102 | + |
| 103 | + elif win_T.pixel == "HEALPIX": |
| 104 | + split = so_map.read_map(map) |
| 105 | + |
| 106 | + split = split.calibrate(cal=cal, pol_eff=pol_eff) |
| 107 | + |
| 108 | + if d["remove_mean"] == True: |
| 109 | + split = split.subtract_mean(window_tuple) |
| 110 | + |
| 111 | + master_alms = sph_tools.get_alms(split, window_tuple, niter, lmax, alm_conv=alm_conv) |
| 112 | + |
| 113 | + if apply_alm_mask == True: |
| 114 | + alm_mask = hp.read_alm(d[f"alm_mask_{sv}_{ar}"], hdu=1) |
| 115 | + alm_mask = hp.sphtfunc.resize_alm(alm_mask, d["lmax_mask"], d["lmax_mask"], lmax, lmax) |
| 116 | + master_alms *= alm_mask |
| 117 | + |
| 118 | + np.save(f"{alms_dir}/alms_{sv}_{ar}_{k}.npy", master_alms) |
| 119 | + |
| 120 | + log.info(f"[{task}] execution time {time.time() - t0} seconds") |
0 commit comments