Exemplo n.º 1
0
def test_from_classes_custominstrument():

    cmb = mapsims.SOPrecomputedCMB(
        num=0,
        nside=NSIDE,
        lensed=False,
        aberrated=False,
        has_polarization=True,
        cmb_set=0,
        cmb_dir="mapsims/tests/data",
        input_units="uK_CMB",
    )

    # CIB is only at NSIDE 4096, too much memory for testing
    # cib = so_pysm_models.WebSkyCIB(
    #    websky_version="0.3", nside=NSIDE, interpolation_kind="linear"
    # )

    simulator = mapsims.MapSim(
        channels="100",
        nside=NSIDE,
        unit="uK_CMB",
        pysm_components_string="SO_d0",
        pysm_custom_components={"cmb": cmb},
        pysm_output_reference_frame="C",
        instrument_parameters="planck_deltabandpass",
    )
    output_map = simulator.execute(write_outputs=False)["100"]

    freq = 100.89 * u.GHz
    expected_map = cmb.get_emission(freq) + so_pysm_models.get_so_models(
        "SO_d0", nside=NSIDE
    ).get_emission(freq)
    fwhm = 9.682 * u.arcmin

    from mpi4py import MPI
    map_dist = pysm.MapDistribution(
        nside=NSIDE, smoothing_lmax=3 * NSIDE - 1, mpi_comm=MPI.COMM_WORLD
    )
    expected_map = pysm.mpi.mpi_smoothing(
        expected_map.to_value(u.uK_CMB, equivalencies=u.cmb_equivalencies(freq)),
        fwhm,
        map_dist,
    )

    assert_quantity_allclose(output_map, expected_map, rtol=1e-3)
Exemplo n.º 2
0
def simulate_sky_signal(args, comm, data, focalplanes, signalname=None, mc=0):
    """ Use PySM to simulate smoothed sky signal.

    """
    log = Logger.get()
    timer = Timer()
    timer.start()
    # Convolve a signal TOD from PySM
    if comm.world_rank == 0:
        log.info("Simulating sky signal with PySM")

    map_dist = (None if comm is None else pysm.MapDistribution(
        nside=args.nside, mpi_comm=comm.comm_rank))
    pysm_component_objects = []
    pysm_model = []
    for model_tag in args.pysm_model.split(","):

        if not model_tag.startswith("SO"):
            pysm_model.append(model_tag)
        else:
            if so_pysm_models is None:
                raise RuntimeError(
                    "{} requires so_pysm_models".format(model_tag))
            if model_tag == "SO_x1_cib":
                pysm_component_objects.append(
                    so_pysm_models.WebSkyCIB(
                        websky_version="0.3",
                        interpolation_kind="linear",
                        nside=args.nside,
                        map_dist=map_dist,
                    ))
            elif model_tag == "SO_x1_ksz":
                pysm_component_objects.append(
                    so_pysm_models.WebSkySZ(
                        version="0.3",
                        nside=args.nside,
                        map_dist=map_dist,
                        sz_type="kinetic",
                    ))
            elif model_tag == "SO_x1_tsz":
                pysm_component_objects.append(
                    so_pysm_models.WebSkySZ(
                        version="0.3",
                        nside=args.nside,
                        map_dist=map_dist,
                        sz_type="thermal",
                    ))
            elif model_tag.startswith("SO_x1_cmb"):
                lensed = "unlensed" not in model_tag
                include_solar_dipole = "solar" in model_tag
                pysm_component_objects.append(
                    so_pysm_models.WebSkyCMBMap(
                        websky_version="0.3",
                        lensed=lensed,
                        include_solar_dipole=include_solar_dipole,
                        seed=1,
                        nside=args.nside,
                        map_dist=map_dist,
                    ))
            else:
                if not model_tag.endswith("s") and args.nside > 512:
                    model_tag += "s"
                pysm_component_objects.append(
                    so_pysm_models.get_so_models(model_tag,
                                                 args.nside,
                                                 map_dist=map_dist))

    if signalname is None:
        signalname = "pysmsignal"
    op_sim_pysm = OpSimPySM(
        data,
        comm=comm.comm_rank,
        out=signalname,
        pysm_model=pysm_model,
        pysm_component_objects=pysm_component_objects,
        focalplanes=focalplanes,
        apply_beam=args.pysm_apply_beam,
        coord="G",  # setting G doesn't perform any rotation
        map_dist=map_dist,
    )
    assert args.coord in "CQ", "Input SO models are always in Equatorial coordinates"
    op_sim_pysm.exec(data)
    if comm.comm_world is not None:
        comm.comm_world.barrier()
    timer.stop()
    if comm.world_rank == 0:
        timer.report("PySM")

    return signalname
Exemplo n.º 3
0
memreport = MemReporter(map_dist.mpi_comm)
memreport.run("After imports")

timelines_size_GB = 10
GB_to_bytes = 1024**3
num_elements = timelines_size_GB * GB_to_bytes // np.dtype(np.double).itemsize

timelines = np.ones(num_elements, dtype=np.double)

memreport.run("Created fake timelines of {:.2f} GB per process".format(
    timelines.nbytes / GB_to_bytes))

components = []
for comp in ["SO_d0s", "SO_s0s", "SO_f0s", "SO_a0s"]:
    components.append(get_so_models(comp, nside, map_dist=map_dist))

components.append(
    so_pysm_models.WebSkyCIB(
        websky_version="0.3",
        interpolation_kind="linear",
        nside=nside,
        map_dist=map_dist,
    ))

components.append(
    so_pysm_models.WebSkySZ(
        version="0.3",
        nside=nside,
        map_dist=map_dist,
        sz_type="kinetic",
Exemplo n.º 4
0
    def execute(self, write_outputs=False):
        """Run map simulations

        Execute simulations for all channels and write to disk the maps,
        unless `write_outputs` is False, then return them.
        """

        if self.run_pysm:
            sky_config = []
            preset_strings = []
            if self.pysm_components_string is not None:
                for model in self.pysm_components_string.split(","):
                    if model.startswith("SO"):
                        sky_config.append(get_so_models(model, self.nside))
                    else:
                        preset_strings.append(model)

            if len(preset_strings) > 0:
                input_reference_frame = "G"
                assert (
                    len(sky_config) == 0
                ), "Cannot mix PySM and SO models, they are defined in G and C frames"
            else:
                input_reference_frame = "C"

            self.pysm_sky = pysm.Sky(
                nside=self.nside,
                preset_strings=preset_strings,
                component_objects=sky_config,
                output_unit=u.Unit(self.unit),
            )

            if self.pysm_custom_components is not None:
                for comp_name, comp in self.pysm_custom_components.items():
                    self.pysm_sky.components.append(comp)

        if not write_outputs:
            output = {}

        # ch can be single channel or tuple of 2 channels (tube dichroic)
        for ch in self.channels:
            if not isinstance(ch, tuple):
                ch = [ch]
            output_map_shape = (len(ch), 3) + self.shape
            output_map = np.zeros(output_map_shape, dtype=np.float64)
            if self.run_pysm:
                for each, channel_map in zip(ch, output_map):
                    bandpass_integrated_map = self.pysm_sky.get_emission(
                        *each.bandpass).value
                    beam_width_arcmin = each.beam
                    # smoothing and coordinate rotation with 1 spherical harmonics transform
                    smoothed_map = hp.ma(
                        pysm.apply_smoothing_and_coord_transform(
                            bandpass_integrated_map,
                            fwhm=beam_width_arcmin,
                            lmax=3 * self.nside - 1,
                            rot=None if input_reference_frame
                            == self.pysm_output_reference_frame else
                            hp.Rotator(coord=(
                                input_reference_frame,
                                self.pysm_output_reference_frame,
                            )),
                            map_dist=None
                            if COMM_WORLD is None else pysm.MapDistribution(
                                nside=self.nside,
                                smoothing_lmax=3 * self.nside - 1,
                                mpi_comm=COMM_WORLD,
                            ),
                        ))

                    if smoothed_map.shape[0] == 1:
                        channel_map[0] += smoothed_map
                    else:
                        channel_map += smoothed_map

            output_map = output_map.reshape((len(ch), 1, 3) + self.shape)

            if self.other_components is not None:
                for comp in self.other_components.values():
                    kwargs = dict(tube=ch[0].tube, output_units=self.unit)
                    if function_accepts_argument(comp.simulate, "ch"):
                        kwargs.pop("tube")
                        kwargs["ch"] = ch
                    if function_accepts_argument(comp.simulate, "nsplits"):
                        kwargs["nsplits"] = self.nsplits
                    if function_accepts_argument(comp.simulate, "seed"):
                        kwargs["seed"] = self.num
                    component_map = comp.simulate(**kwargs)
                    if self.nsplits == 1:
                        component_map = component_map.reshape((len(ch), 1, 3) +
                                                              self.shape)
                    component_map[hp.mask_bad(component_map)] = np.nan
                    output_map = output_map + component_map

            for each, channel_map in zip(ch, output_map):
                if write_outputs:
                    for split, each_split_channel_map in enumerate(
                            channel_map):
                        filename = self.output_filename_template.format(
                            telescope=each.telescope
                            if each.tube is None else each.tube,
                            band=each.band,
                            nside=self.nside,
                            tag=self.tag,
                            num=self.num,
                            nsplits=self.nsplits,
                            split=split + 1,
                        )
                        warnings.warn("Writing output map " + filename)
                        if self.car:
                            pixell.enmap.write_map(
                                os.path.join(self.output_folder, filename),
                                each_split_channel_map,
                                extra=dict(units=self.unit),
                            )
                        else:
                            each_split_channel_map[np.isnan(
                                each_split_channel_map)] = hp.UNSEEN
                            each_split_channel_map = hp.reorder(
                                each_split_channel_map, r2n=True)
                            hp.write_map(
                                os.path.join(self.output_folder, filename),
                                each_split_channel_map,
                                coord=self.pysm_output_reference_frame,
                                column_units=self.unit,
                                dtype=np.float32,
                                overwrite=True,
                                nest=True,
                            )
                else:
                    if self.nsplits == 1:
                        channel_map = channel_map[0]
                    if not self.car:
                        channel_map[np.isnan(channel_map)] = hp.UNSEEN
                    output[each.tag] = channel_map
        if not write_outputs:
            return output