|
| 1 | +from pspipe_utils import consistency, external_data, pspipe_list, covariance |
| 2 | +from pspy import so_spectra, so_cov, pspy_utils |
| 3 | +import numpy as np |
| 4 | +import pylab as plt |
| 5 | + |
| 6 | +def get_residual_and_cov(map_set_list, spec_dir, cov_dir, mode, spectra_order, op="aa+bb-2ab", mc_cov=False): |
| 7 | + """ |
| 8 | + get the corresponding residual and its associated covariance for the given map_set_list, |
| 9 | + the given mode and the given operation |
| 10 | + |
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + map_set_list : list of string |
| 14 | + the map_set we want to compare, for example ["Planck_f143", "Planck_f353"] |
| 15 | + spec_dir: string |
| 16 | + the location of the spectra |
| 17 | + cov_dir: string |
| 18 | + the location of the covariances |
| 19 | + mode: string |
| 20 | + the spectrum we want to look at, e.g "TT" |
| 21 | + spectra_order: string |
| 22 | + the order of the spectra, e.g ["TT", "TE", "TB", ..., "BB"] |
| 23 | + op: string |
| 24 | + the operation we want to perform |
| 25 | + mc_cov: boolean |
| 26 | + wether to use a monte carlo correction for the covariance |
| 27 | + |
| 28 | + """ |
| 29 | + |
| 30 | + ps_template = spec_dir + "/Dl_{}x{}_cross.dat" |
| 31 | + cov_template = cov_dir + "/analytic_cov_{}x{}_{}x{}.npy" |
| 32 | + ps_dict, cov_dict = consistency.get_ps_and_cov_dict(map_set_list, ps_template, cov_template, spectra_order=spectra_order) |
| 33 | + lb, res, cov_res, _, _ = consistency.compare_spectra(map_set_list, op, ps_dict, cov_dict, mode=mode) |
| 34 | + |
| 35 | + if mc_cov: |
| 36 | + mc_cov_template = cov_dir + "/mc_cov_{}x{}_{}x{}.npy" |
| 37 | + _, mc_cov_dict = consistency.get_ps_and_cov_dict(map_set_list, ps_template, mc_cov_template, spectra_order=spectra_order) |
| 38 | + _, _, mc_cov_res, _, _ = consistency.compare_spectra(map_set_list, op, ps_dict, mc_cov_dict, mode=mode) |
| 39 | + cov_res = covariance.correct_analytical_cov(cov_res, |
| 40 | + mc_cov_res, |
| 41 | + only_diag_corrections=True) |
| 42 | + |
| 43 | + return lb, res, cov_res |
| 44 | + |
| 45 | + |
| 46 | +def get_spectra_and_cov(spec_dir, cov_dir, spec_name, mode, spectra_order, mc_cov=False): |
| 47 | + """ |
| 48 | + get the corresponding spectrum and its associated covariance for the given spec_name, |
| 49 | + the given mode |
| 50 | + |
| 51 | + Parameters |
| 52 | + ---------- |
| 53 | + spec_dir: string |
| 54 | + the location of the spectra |
| 55 | + cov_dir: string |
| 56 | + the location of the covariances |
| 57 | + spec_name: string |
| 58 | + the name of the spectrum to look at, e.g "dr6_pa4_f220xdr6_pa4_f220" |
| 59 | + mode: string |
| 60 | + the spectrum we want to look at, e.g "TT" |
| 61 | + spectra_order: string |
| 62 | + the order of the spectra, e.g ["TT", "TE", "TB", ..., "BB"] |
| 63 | + mc_cov: boolean |
| 64 | + wether to use a monte carlo correction for the covariance |
| 65 | + |
| 66 | + """ |
| 67 | + |
| 68 | + |
| 69 | + lb, ps = so_spectra.read_ps(f"{spec_dir}/Dl_{spec_name}_cross.dat", spectra=spectra_order) |
| 70 | + cov = np.load(f"{cov_dir}/analytic_cov_{spec_name}_{spec_name}.npy") |
| 71 | + cov = so_cov.selectblock(cov, spectra_order, n_bins=len(lb), block=mode+mode) |
| 72 | + if mc_cov: |
| 73 | + mc_cov = np.load(f"{cov_dir}/mc_cov_{spec_name}_{spec_name}.npy") |
| 74 | + mc_cov = so_cov.selectblock(cov, spectra_order, n_bins=len(lb), block=mode+mode) |
| 75 | + cov = covariance.correct_analytical_cov(cov, |
| 76 | + mc_cov, |
| 77 | + only_diag_corrections=True) |
| 78 | + |
| 79 | + |
| 80 | + return lb, ps[mode], cov |
| 81 | + |
| 82 | + |
| 83 | +def load_band_pass(dict, use_220=False): |
| 84 | + "load the bandpass corresponding to the dictionnary, optionnaly also read the 220 GHz bandpass" |
| 85 | + |
| 86 | + narrays, sv_list, ar_list = pspipe_list.get_arrays_list(dict) |
| 87 | + |
| 88 | + passbands = {} |
| 89 | + for sv, ar in zip(sv_list, ar_list): |
| 90 | + freq_info = dict[f"freq_info_{sv}_{ar}"] |
| 91 | + |
| 92 | + if dict["do_bandpass_integration"]: |
| 93 | + nu_ghz, pb = np.loadtxt(freq_info["passband"]).T |
| 94 | + else: |
| 95 | + nu_ghz, pb = np.array([freq_info["freq_tag"]]), np.array([1.]) |
| 96 | + |
| 97 | + passbands[f"{sv}_{ar}"] = [nu_ghz, pb] |
| 98 | + |
| 99 | + if use_220: |
| 100 | + if dict["do_bandpass_integration"]: |
| 101 | + passbands[f"dr6_pa4_f220"] = external_data.get_passband_dict_dr6(["pa4_f220"])["pa4_f220"] |
| 102 | + else: |
| 103 | + passbands[f"dr6_pa4_f220"] = [[220], [1.0]] |
| 104 | + |
| 105 | + return passbands |
| 106 | + |
| 107 | + |
| 108 | + |
0 commit comments