|
| 1 | +#TO BE TESTED MORE |
| 2 | + |
| 3 | +import matplotlib |
| 4 | + |
| 5 | +matplotlib.use("Agg") |
| 6 | +import sys |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pylab as plt |
| 10 | +import scipy.interpolate |
| 11 | +from pspipe_utils import log, misc |
| 12 | +from pspy import pspy_utils, so_dict, so_spectra |
| 13 | + |
| 14 | + |
| 15 | +def interpolate_dict(lb, cb, lth, spectra, force_positive=True, l_inf_lmin_equal_lmin=True, discard_cross=True): |
| 16 | + cl_dict = {} |
| 17 | + for spec in spectra: |
| 18 | + cl = scipy.interpolate.interp1d(lb, cb[spec], fill_value = "extrapolate") |
| 19 | + cl_dict[spec] = cl(lth) |
| 20 | + if l_inf_lmin_equal_lmin: |
| 21 | + id = np.where(lth <= np.min(lb)) |
| 22 | + cl_dict[spec][id]= cb[spec][0] |
| 23 | + if force_positive: |
| 24 | + cl_dict[spec] = np.abs(cl_dict[spec]) |
| 25 | + if discard_cross: |
| 26 | + if spec not in ["TT", "EE", "BB"]: |
| 27 | + cl_dict[spec] = np.zeros(len(lth)) |
| 28 | + return cl_dict |
| 29 | + |
| 30 | +d = so_dict.so_dict() |
| 31 | +d.read_from_file(sys.argv[1]) |
| 32 | +log = log.get_logger(**d) |
| 33 | + |
| 34 | +spectra_dir = d['spec_dir'] |
| 35 | +ps_model_dir = d['ps_model_dir'] |
| 36 | +plot_dir = d['plot_base_dir'] + "/noise_model/" |
| 37 | + |
| 38 | +pspy_utils.create_directory(ps_model_dir) |
| 39 | +pspy_utils.create_directory(plot_dir) |
| 40 | + |
| 41 | +spectra = ["TT", "TE", "TB", "ET", "BT", "EE", "EB", "BE", "BB"] |
| 42 | +surveys = d["surveys"] |
| 43 | +lmax = d["lmax"] |
| 44 | +type = d["type"] |
| 45 | +binning_file = d["binning_file"] |
| 46 | + |
| 47 | +lth = np.arange(2, lmax+2) |
| 48 | + |
| 49 | +for sv in surveys: |
| 50 | + arrays = d[f"arrays_{sv}"] |
| 51 | + for id_ar1, ar1 in enumerate(arrays): |
| 52 | + for id_ar2, ar2 in enumerate(arrays): |
| 53 | + if id_ar1 > id_ar2: continue |
| 54 | + |
| 55 | + log.info(f"Computing noise for '{sv}' survey and '{ar1}x{ar2}' arrays") |
| 56 | + |
| 57 | + l, bl_ar1 = misc.read_beams(d[f"beam_T_{sv}_{ar1}"], d[f"beam_pol_{sv}_{ar1}"]) |
| 58 | + l, bl_ar2 = misc.read_beams(d[f"beam_T_{sv}_{ar2}"], d[f"beam_pol_{sv}_{ar2}"]) |
| 59 | + |
| 60 | + lb, nbs_ar1xar1 = so_spectra.read_ps(f"{spectra_dir}/{type}_{sv}_{ar1}x{sv}_{ar1}_noise.dat", spectra=spectra) |
| 61 | + lb, nbs_ar1xar2 = so_spectra.read_ps(f"{spectra_dir}/{type}_{sv}_{ar1}x{sv}_{ar2}_noise.dat", spectra=spectra) |
| 62 | + lb, nbs_ar2xar2 = so_spectra.read_ps(f"{spectra_dir}/{type}_{sv}_{ar2}x{sv}_{ar2}_noise.dat", spectra=spectra) |
| 63 | + |
| 64 | + bb_ar1, bb_ar2 = {}, {} |
| 65 | + for field in ["T", "E", "B"]: |
| 66 | + lb, bb_ar1[field] = pspy_utils.naive_binning(l, bl_ar1[field], binning_file, lmax) |
| 67 | + lb, bb_ar2[field] = pspy_utils.naive_binning(l, bl_ar2[field], binning_file, lmax) |
| 68 | + |
| 69 | + Rb = {} |
| 70 | + for spec in spectra: |
| 71 | + X,Y = spec |
| 72 | + nbs_ar1xar1[spec] *= bb_ar1[X] * bb_ar1[Y] |
| 73 | + nbs_ar1xar2[spec] *= bb_ar1[X] * bb_ar2[Y] |
| 74 | + nbs_ar2xar2[spec] *= bb_ar2[X] * bb_ar2[Y] |
| 75 | + |
| 76 | + Rb[spec] = nbs_ar1xar2[spec] / np.sqrt(np.abs(nbs_ar1xar1[spec] * nbs_ar2xar2[spec])) |
| 77 | + |
| 78 | + if ar1 == ar2: |
| 79 | + nlth = interpolate_dict(lb, nbs_ar1xar1, lth, spectra) |
| 80 | + else: |
| 81 | + Rlth = interpolate_dict(lb, Rb, lth, spectra) |
| 82 | + nlth_ar1xar1 = interpolate_dict(lb, nbs_ar1xar1, lth, spectra) |
| 83 | + nlth_ar2xar2 = interpolate_dict(lb, nbs_ar2xar2, lth, spectra) |
| 84 | + nlth = {} |
| 85 | + for spec in spectra: |
| 86 | + nlth[spec] = Rlth[spec] * np.sqrt(nlth_ar1xar1[spec] * nlth_ar2xar2[spec]) |
| 87 | + |
| 88 | + |
| 89 | + for spec in spectra: |
| 90 | + plt.figure(figsize=(12,12)) |
| 91 | + plt.plot(lth, |
| 92 | + nlth[spec], |
| 93 | + label="interpolate", |
| 94 | + color="lightblue") |
| 95 | + if ar1 == ar2: |
| 96 | + nbs= nbs_ar1xar1[spec] |
| 97 | + else: |
| 98 | + nbs= nbs_ar1xar2[spec] |
| 99 | + |
| 100 | + plt.plot(lb, |
| 101 | + nbs, |
| 102 | + ".", |
| 103 | + label = f"{sv} {ar1}x{ar2}", |
| 104 | + color="red") |
| 105 | + plt.legend(fontsize=20) |
| 106 | + plt.savefig(f"{plot_dir}/noise_interpolate_{ar1}x{ar2}_{sv}_{spec}.png", bbox_inches="tight") |
| 107 | + plt.clf() |
| 108 | + plt.close() |
| 109 | + |
| 110 | + spec_name_noise_mean = f"mean_{ar1}x{ar2}_{sv}_noise" |
| 111 | + so_spectra.write_ps(ps_model_dir + f"/{spec_name_noise_mean}.dat", lth, nlth, type, spectra=spectra) |
| 112 | + |
| 113 | + if ar2 != ar1: |
| 114 | + spec_name_noise_mean = f"mean_{ar2}x{ar1}_{sv}_noise" |
| 115 | + TE, ET, TB, BT, EB, BE = nlth["ET"], nlth["TE"], nlth["BT"], nlth["TB"], nlth["BE"], nlth["EB"] |
| 116 | + nlth["TE"], nlth["ET"], nlth["TB"], nlth["BT"], nlth["EB"], nlth["BE"] = TE, ET, TB, BT, EB, BE |
| 117 | + so_spectra.write_ps(ps_model_dir + f"/{spec_name_noise_mean}.dat", lth, nlth, type, spectra=spectra) |
0 commit comments