def test_create_gaintable_from_pointingtable(self):
        s3_components = create_test_skycomponents_from_s3(
            flux_limit=0.3,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=1.5 * numpy.pi / 180.0)
        s3_components = filter_skycomponents_by_flux(s3_components, 0.0, 10.0)

        pt = create_pointingtable_from_blockvisibility(self.vis)
        pt = simulate_pointingtable(pt,
                                    pointing_error=0.01,
                                    static_pointing_error=[0.001, 0.0001])
        vp = create_vp(self.model, 'MID')
        gt = simulate_gaintable_from_pointingtable(self.vis, s3_components, pt,
                                                   vp)
        if self.doplot:
            import matplotlib.pyplot as plt
            plt.clf()
            plt.plot(gt[0].time, numpy.real(1.0 / gt[0].gain[:, 0, 0, 0, 0]),
                     '.')
            plt.plot(gt[0].time, numpy.imag(1.0 / gt[0].gain[:, 0, 0, 0, 0]),
                     '.')
            plt.title('test_create_gaintable_from_pointingtable')
            plt.show(block=False)
        assert gt[0].gain.shape == (self.ntimes, self.nants, 1, 1,
                                    1), gt[0].gain.shape
예제 #2
0
 def test_create_test_skycomponents_from_s3(self):
     self.frequency = numpy.linspace(0.8e9, 1.2e9, 5)
     sc = create_test_skycomponents_from_s3(
         flux_limit=3.0,
         phasecentre=self.phasecentre,
         polarisation_frame=PolarisationFrame("stokesI"),
         frequency=self.frequency,
         radius=0.1)
     assert len(
         sc) == 9, "Only expected nine sources, actually found %d" % len(sc)
     assert sc[0].name == 'S3_36133023'
     self.assertAlmostEqual(sc[0].flux[0, 0], 4.54336938, 7)
예제 #3
0
    def test_create_gaintable_from_pointingtable(self):
        s3_components = create_test_skycomponents_from_s3(
            flux_limit=5.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.2)

        pt = create_pointingtable_from_blockvisibility(self.vis)
        pt = simulate_pointingtable(pt,
                                    pointing_error=0.01,
                                    static_pointing_error=[0.001, 0.0001])
        vp = create_vp(self.model, 'MID')
        gt = simulate_gaintable_from_pointingtable(self.vis, s3_components, pt,
                                                   vp)
        assert gt[0].gain.shape == (self.ntimes, self.nants, 1, 1,
                                    1), gt[0].gain.shape
예제 #4
0
    def test_create_gaintable_from_screen_troposphere(self):
        self.actualSetup("troposphere")
        screen = import_image_from_fits(
            rascil_data_path('models/test_mpc_screen.fits'))
        beam = create_test_image(cellsize=0.00015,
                                 phasecentre=self.vis.phasecentre,
                                 frequency=self.frequency)

        beam = create_low_test_beam(beam, use_local=False)

        s3_components = create_test_skycomponents_from_s3(
            flux_limit=0.3,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=1.5 * numpy.pi / 180.0)

        assert len(s3_components) > 0, "No S3 components selected"

        pb_s3_components = apply_beam_to_skycomponent(s3_components, beam)

        actual_components = filter_skycomponents_by_flux(pb_s3_components,
                                                         flux_max=10.0)

        assert len(
            actual_components) > 0, "No components after applying primary beam"

        gaintables = create_gaintable_from_screen(
            self.vis,
            actual_components,
            screen,
            height=3e3,
            type_atmosphere="troposphere")
        assert len(gaintables) == len(actual_components), len(gaintables)
        assert gaintables[0].gain.shape == (3, 63, 1, 1,
                                            1), gaintables[0].gain.shape
예제 #5
0
def create_simulation_components(context,
                                 phasecentre,
                                 frequency,
                                 pbtype,
                                 offset_dir,
                                 flux_limit,
                                 pbradius,
                                 pb_npixel,
                                 pb_cellsize,
                                 show=False):
    """ Construct components for simulation
    
    :param context:
    :param phasecentre:
    :param frequency:
    :param pbtype:
    :param offset_dir:
    :param flux_limit:
    :param pbradius:
    :param pb_npixel:
    :param pb_cellsize:
    :return:
    """

    HWHM_deg, null_az_deg, null_el_deg = find_pb_width_null(pbtype, frequency)

    dec = phasecentre.dec.deg
    ra = phasecentre.ra.deg

    if context == 'singlesource':
        log.info("create_simulation_components: Constructing single component")
        offset = [HWHM_deg * offset_dir[0], HWHM_deg * offset_dir[1]]
        log.info(
            "create_simulation_components: Offset from pointing centre = %.3f, %.3f deg"
            % (offset[0], offset[1]))

        # The point source is offset to approximately the halfpower point
        offset_direction = SkyCoord(
            ra=(ra + offset[0] / numpy.cos(numpy.pi * dec / 180.0)) *
            units.deg,
            dec=(dec + offset[1]) * units.deg,
            frame='icrs',
            equinox='J2000')

        original_components = [
            Skycomponent(flux=[[1.0]],
                         direction=offset_direction,
                         frequency=frequency,
                         polarisation_frame=PolarisationFrame('stokesI'))
        ]

    elif context == 'null':
        log.info(
            "create_simulation_components: Constructing single component at the null"
        )

        offset = [null_az_deg * offset_dir[0], null_el_deg * offset_dir[1]]
        HWHM = HWHM_deg * numpy.pi / 180.0

        log.info(
            "create_simulation_components: Offset from pointing centre = %.3f, %.3f deg"
            % (offset[0], offset[1]))

        # The point source is offset to approximately the null point
        offset_direction = SkyCoord(
            ra=(ra + offset[0] / numpy.cos(numpy.pi * dec / 180.0)) *
            units.deg,
            dec=(dec + offset[1]) * units.deg,
            frame='icrs',
            equinox='J2000')

        original_components = [
            Skycomponent(flux=[[1.0]],
                         direction=offset_direction,
                         frequency=frequency,
                         polarisation_frame=PolarisationFrame('stokesI'))
        ]

    else:
        offset = [0.0, 0.0]
        # Make a skymodel from S3
        max_flux = 0.0
        total_flux = 0.0
        log.info("create_simulation_components: Constructing s3sky components")
        from rascil.processing_components.simulation import create_test_skycomponents_from_s3

        original_components = create_test_skycomponents_from_s3(
            flux_limit=flux_limit / 100.0,
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame("stokesI"),
            frequency=numpy.array(frequency),
            radius=pbradius)
        log.info(
            "create_simulation_components: %d components before application of primary beam"
            % (len(original_components)))

        pbmodel = create_image(npixel=pb_npixel,
                               cellsize=pb_cellsize,
                               phasecentre=phasecentre,
                               frequency=frequency,
                               polarisation_frame=PolarisationFrame("stokesI"))
        pb = create_pb(pbmodel,
                       "MID_GAUSS",
                       pointingcentre=phasecentre,
                       use_local=False)
        pb_feko = create_pb(pbmodel,
                            pbtype,
                            pointingcentre=phasecentre,
                            use_local=True)
        pb.data = pb_feko.data[:, 0, ...][:, numpy.newaxis, ...]
        pb_applied_components = [
            copy_skycomponent(c) for c in original_components
        ]
        pb_applied_components = apply_beam_to_skycomponent(
            pb_applied_components, pb)
        filtered_components = []
        for icomp, comp in enumerate(pb_applied_components):
            if comp.flux[0, 0] > flux_limit:
                total_flux += comp.flux[0, 0]
                if abs(comp.flux[0, 0]) > max_flux:
                    max_flux = abs(comp.flux[0, 0])
                filtered_components.append(original_components[icomp])
        log.info(
            "create_simulation_components: %d components > %.3f Jy after application of primary beam"
            % (len(filtered_components), flux_limit))
        log.info(
            "create_simulation_components: Strongest components is %g (Jy)" %
            max_flux)
        log.info(
            "create_simulation_components: Total flux in components is %g (Jy)"
            % total_flux)
        original_components = [
            copy_skycomponent(c) for c in filtered_components
        ]
        if show:
            plt.clf()
            show_image(pb, components=original_components)
            plt.show(block=False)

        log.info("create_simulation_components: Created %d components" %
                 len(original_components))
        # Primary beam points to the phasecentre
        offset_direction = SkyCoord(ra=ra * units.deg,
                                    dec=dec * units.deg,
                                    frame='icrs',
                                    equinox='J2000')

    return original_components, offset_direction
def create_simulation_components(
        context,
        phasecentre,
        frequency,
        pbtype,
        offset_dir,
        flux_limit,
        pbradius,
        pb_npixel,
        pb_cellsize,
        show=False,
        fov=10,
        polarisation_frame=PolarisationFrame("stokesI"),
        filter_by_primary_beam=True,
        flux_max=10.0):
    """ Construct components for simulation
    
    :param context: singlesource or null or s3sky
    :param phasecentre: Centre of components
    :param frequency: Frequency
    :param pbtype: Type of primary beam
    :param offset_dir: Offset in ra, dec degrees
    :param flux_limit: Lower limit flux
    :param pbradius: Radius of components in radians
    :param pb_npixel: Number of pixels in the primary beam model
    :param pb_cellsize: Cellsize in primary beam model
    :param fov: FOV in degrees (used to select catalog)
    :param flux_max: Maximum flux in model before application of primary beam
    :param filter_by_primary_beam: Filter components by primary beam
    :param polarisation_frame:
    :param show:

    :return:
    """

    HWHM_deg, null_az_deg, null_el_deg = find_pb_width_null(pbtype, frequency)

    dec = phasecentre.dec.deg
    ra = phasecentre.ra.deg

    if context == 'singlesource':
        log.info("create_simulation_components: Constructing single component")
        offset = [HWHM_deg * offset_dir[0], HWHM_deg * offset_dir[1]]
        log.info(
            "create_simulation_components: Offset from pointing centre = %.3f, %.3f deg"
            % (offset[0], offset[1]))

        # The point source is offset to approximately the halfpower point
        odirection = SkyCoord(
            ra=(ra + offset[0] / numpy.cos(numpy.pi * dec / 180.0)) *
            units.deg,
            dec=(dec + offset[1]) * units.deg,
            frame='icrs',
            equinox='J2000')

        if polarisation_frame.type == "stokesIQUV":
            original_components = [
                Skycomponent(
                    flux=[[1.0, 0.0, 0.0, 0.0]],
                    direction=odirection,
                    frequency=frequency,
                    polarisation_frame=PolarisationFrame('stokesIQUV'))
            ]
        else:
            original_components = [
                Skycomponent(flux=[[1.0]],
                             direction=odirection,
                             frequency=frequency,
                             polarisation_frame=PolarisationFrame('stokesI'))
            ]

        offset_direction = odirection

    elif context == 'doublesource':

        original_components = []

        log.info(
            "create_simulation_components: Constructing double components")

        for sign_offset in [(-1, 0), (1, 0)]:
            offset = [HWHM_deg * sign_offset[0], HWHM_deg * sign_offset[1]]

            log.info(
                "create_simulation_components: Offset from pointing centre = %.3f, %.3f deg"
                % (offset[0], offset[1]))

            odirection = SkyCoord(
                ra=(ra + offset[0] / numpy.cos(numpy.pi * dec / 180.0)) *
                units.deg,
                dec=(dec + offset[1]) * units.deg,
                frame='icrs',
                equinox='J2000')
            if polarisation_frame.type == "stokesIQUV":
                original_components.append(
                    Skycomponent(
                        flux=[[1.0, 0.0, 0.0, 0.0]],
                        direction=odirection,
                        frequency=frequency,
                        polarisation_frame=PolarisationFrame('stokesIQUV')))
            else:
                original_components.append(
                    Skycomponent(
                        flux=[[1.0]],
                        direction=odirection,
                        frequency=frequency,
                        polarisation_frame=PolarisationFrame('stokesI')))

        for o in original_components:
            print(o)

        offset_direction = odirection

    elif context == 'null':
        log.info(
            "create_simulation_components: Constructing single component at the null"
        )

        offset = [null_az_deg * offset_dir[0], null_el_deg * offset_dir[1]]
        HWHM = HWHM_deg * numpy.pi / 180.0

        log.info(
            "create_simulation_components: Offset from pointing centre = %.3f, %.3f deg"
            % (offset[0], offset[1]))

        # The point source is offset to approximately the null point
        offset_direction = SkyCoord(
            ra=(ra + offset[0] / numpy.cos(numpy.pi * dec / 180.0)) *
            units.deg,
            dec=(dec + offset[1]) * units.deg,
            frame='icrs',
            equinox='J2000')

        if polarisation_frame.type == "stokesIQUV":
            original_components = [
                Skycomponent(
                    flux=[[1.0, 0.0, 0.0, 0.0]],
                    direction=offset_direction,
                    frequency=frequency,
                    polarisation_frame=PolarisationFrame('stokesIQUV'))
            ]
        else:
            original_components = [
                Skycomponent(flux=[[1.0]],
                             direction=offset_direction,
                             frequency=frequency,
                             polarisation_frame=PolarisationFrame('stokesI'))
            ]

    else:
        offset = [0.0, 0.0]
        # Make a skymodel from S3
        max_flux = 0.0
        total_flux = 0.0
        log.info("create_simulation_components: Constructing s3sky components")
        from rascil.processing_components.simulation import create_test_skycomponents_from_s3

        all_components = create_test_skycomponents_from_s3(
            flux_limit=flux_limit / 100.0,
            phasecentre=phasecentre,
            polarisation_frame=polarisation_frame,
            frequency=numpy.array(frequency),
            radius=pbradius,
            fov=fov)
        original_components = filter_skycomponents_by_flux(all_components,
                                                           flux_max=flux_max)
        log.info(
            "create_simulation_components: %d components before application of primary beam"
            % (len(original_components)))

        if filter_by_primary_beam:
            pbmodel = create_image(
                npixel=pb_npixel,
                cellsize=pb_cellsize,
                phasecentre=phasecentre,
                frequency=frequency,
                polarisation_frame=PolarisationFrame("stokesI"))
            stokesi_components = [
                copy_skycomponent(o) for o in original_components
            ]
            for s in stokesi_components:
                s.flux = numpy.array([[s.flux[0, 0]]])
                s.polarisation_frame = PolarisationFrame("stokesI")

            pb = create_pb(pbmodel,
                           "MID_GAUSS",
                           pointingcentre=phasecentre,
                           use_local=False)
            pb_applied_components = [
                copy_skycomponent(c) for c in stokesi_components
            ]
            pb_applied_components = apply_beam_to_skycomponent(
                pb_applied_components, pb)
            filtered_components = []
            for icomp, comp in enumerate(pb_applied_components):
                if comp.flux[0, 0] > flux_limit:
                    total_flux += comp.flux[0, 0]
                    if abs(comp.flux[0, 0]) > max_flux:
                        max_flux = abs(comp.flux[0, 0])
                    filtered_components.append(original_components[icomp])
            log.info(
                "create_simulation_components: %d components > %.3f Jy after filtering with primary beam"
                % (len(filtered_components), flux_limit))
            log.info(
                "create_simulation_components: Strongest components is %g (Jy)"
                % max_flux)
            log.info(
                "create_simulation_components: Total flux in components is %g (Jy)"
                % total_flux)
            original_components = [
                copy_skycomponent(c) for c in filtered_components
            ]
            if show:
                plt.clf()
                show_image(pb, components=original_components)
                plt.show(block=False)

        log.info("create_simulation_components: Created %d components" %
                 len(original_components))
        # Primary beam points to the phasecentre
        offset_direction = SkyCoord(ra=ra * units.deg,
                                    dec=dec * units.deg,
                                    frame='icrs',
                                    equinox='J2000')

    return original_components, offset_direction
예제 #7
0
    def test_apply_voltage_pattern_image_pointsource(self):
        self.createVis(rmax=1e3)
        telescope = 'MID_FEKO_B2'
        vpol = PolarisationFrame("linear")
        self.times = numpy.linspace(-4, +4, 8) * numpy.pi / 12.0
        bvis = create_blockvisibility(self.config,
                                      self.times,
                                      self.frequency,
                                      channel_bandwidth=self.channel_bandwidth,
                                      phasecentre=self.phasecentre,
                                      weight=1.0,
                                      polarisation_frame=vpol,
                                      zerow=True)
        cellsize = advise_wide_field(bvis)['cellsize']

        pbmodel = create_image_from_visibility(
            bvis,
            cellsize=self.cellsize,
            npixel=self.npixel,
            override_cellsize=False,
            polarisation_frame=PolarisationFrame("stokesIQUV"))
        vpbeam = create_vp(pbmodel, telescope=telescope, use_local=False)
        vpbeam.wcs.wcs.ctype[0] = 'RA---SIN'
        vpbeam.wcs.wcs.ctype[1] = 'DEC--SIN'
        vpbeam.wcs.wcs.crval[0] = pbmodel.wcs.wcs.crval[0]
        vpbeam.wcs.wcs.crval[1] = pbmodel.wcs.wcs.crval[1]

        s3_components = create_test_skycomponents_from_s3(
            flux_limit=0.1,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=1.5 * numpy.pi / 180.0)

        for comp in s3_components:
            comp.polarisation_frame = PolarisationFrame('stokesIQUV')
            comp.flux = numpy.array([[comp.flux[0, 0], 0.0, 0.0, 0.0]])

        s3_components = filter_skycomponents_by_flux(s3_components, 0.0, 10.0)

        from rascil.processing_components.image import show_image
        import matplotlib.pyplot as plt
        plt.clf()
        show_image(vpbeam, components=s3_components)
        plt.show(block=False)

        vpcomp = apply_voltage_pattern_to_skycomponent(s3_components, vpbeam)
        bvis.data['vis'][...] = 0.0 + 0.0j
        bvis = dft_skycomponent_visibility(bvis, vpcomp)

        rec_comp = idft_visibility_skycomponent(bvis, vpcomp)[0]

        stokes_comp = list()
        for comp in rec_comp:
            stokes_comp.append(
                convert_pol_frame(comp.flux[0], PolarisationFrame("linear"),
                                  PolarisationFrame("stokesIQUV")))

        stokesI = numpy.abs(
            numpy.array([comp_flux[0] for comp_flux in stokes_comp]).real)
        stokesQ = numpy.abs(
            numpy.array([comp_flux[1] for comp_flux in stokes_comp]).real)
        stokesU = numpy.abs(
            numpy.array([comp_flux[2] for comp_flux in stokes_comp]).real)
        stokesV = numpy.abs(
            numpy.array([comp_flux[3] for comp_flux in stokes_comp]).real)
        plt.clf()
        plt.loglog(stokesI, stokesQ, '.', label='Q')
        plt.loglog(stokesI, stokesU, '.', label='U')
        plt.loglog(stokesI, stokesV, '.', label='V')
        plt.xlabel("Stokes Flux I (Jy)")
        plt.ylabel("Flux (Jy)")
        plt.legend()
        plt.savefig('%s/test_primary_beams_pol_rsexecute_stokes_errors.png' %
                    self.dir)
        plt.show(block=False)

        split_times = False
        if split_times:
            bvis_list = list()
            for rows in vis_timeslice_iter(bvis, vis_slices=8):
                bvis_list.append(create_visibility_from_rows(bvis, rows))
        else:
            bvis_list = [bvis]

        bvis_list = rsexecute.scatter(bvis_list)

        model_list = \
            [rsexecute.execute(create_image_from_visibility, nout=1)(bv, cellsize=cellsize, npixel=4096,
                                                                     phasecentre=self.phasecentre,
                                                                     override_cellsize=False,
                                                                     polarisation_frame=PolarisationFrame("stokesIQUV"))
             for bv in bvis_list]

        model_list = rsexecute.persist(model_list)
        bvis_list = weight_list_rsexecute_workflow(bvis_list, model_list)

        continuum_imaging_list = \
            continuum_imaging_list_rsexecute_workflow(bvis_list, model_list,
                                                      context='2d',
                                                      algorithm='hogbom',
                                                      facets=1,
                                                      niter=1000,
                                                      fractional_threshold=0.1,
                                                      threshold=1e-4,
                                                      nmajor=5, gain=0.1,
                                                      deconvolve_facets=4,
                                                      deconvolve_overlap=32,
                                                      deconvolve_taper='tukey',
                                                      psf_support=64,
                                                      restore_facets=4, psfwidth=1.0)
        clean, residual, restored = rsexecute.compute(continuum_imaging_list,
                                                      sync=True)
        centre = 0
        if self.persist:
            export_image_to_fits(
                clean[centre],
                '%s/test_primary_beams_pol_rsexecute_clean.fits' % self.dir)
            export_image_to_fits(
                residual[centre][0],
                '%s/test_primary_beams_pol_rsexecute_residual.fits' % self.dir)
            export_image_to_fits(
                restored[centre],
                '%s/test_primary_beams_pol_rsexecute_restored.fits' % self.dir)

        plt.clf()
        show_image(restored[centre])
        plt.show(block=False)

        qa = qa_image(restored[centre])
        assert numpy.abs(qa.data['max'] - 0.9953017707113947) < 1.0e-7, str(qa)
        assert numpy.abs(qa.data['min'] +
                         0.0036396480874570846) < 1.0e-7, str(qa)