def load_invert_and_deconvolve(c):

        v1 = create_visibility_from_ms(input_vis[0], channum=[c])[0]
        v2 = create_visibility_from_ms(input_vis[1], channum=[c])[0]
        vf = append_visibility(v1, v2)
        vf = convert_visibility_to_stokes(vf)
        vf.configuration.diameter[...] = 35.0
        rows = vis_select_uvrange(vf, 0.0, uvmax=uvmax)
        v = create_visibility_from_rows(vf, rows)

        pol_frame = PolarisationFrame("stokesIQUV")

        m = create_image_from_visibility(v,
                                         npixel=npixel,
                                         cellsize=cellsize,
                                         polarisation_frame=pol_frame)

        if context == '2d':
            d, sumwt = invert_2d(v, m, dopsf=False)
            p, sumwt = invert_2d(v, m, dopsf=True)
        else:
            d, sumwt = invert_list_serial_workflow([v], [m],
                                                   context=context,
                                                   dopsf=False,
                                                   vis_slices=vis_slices)[0]
            p, sumwt = invert_list_serial_workflow([v], [m],
                                                   context=context,
                                                   dopsf=True,
                                                   vis_slices=vis_slices)[0]
        c, resid = deconvolve_cube(d,
                                   p,
                                   m,
                                   threshold=0.01,
                                   fracthresh=0.01,
                                   window_shape='quarter',
                                   niter=100,
                                   gain=0.1,
                                   algorithm='hogbom-complex')
        r = restore_cube(c, p, resid, psfwidth=psfwidth)
        return r
    def actualSetup(self, nsources=None, nvoronoi=None):

        n_workers = 8

        # Set up the observation: 10 minutes at transit, with 10s integration.
        # Skip 5/6 points to avoid outstation redundancy

        nfreqwin = 1
        ntimes = 3
        self.rmax = 2500.0
        dec = -40.0 * u.deg
        frequency = [1e8]
        channel_bandwidth = [0.1e8]
        times = numpy.linspace(-10.0, 10.0,
                               ntimes) * numpy.pi / (3600.0 * 12.0)

        phasecentre = SkyCoord(ra=+0.0 * u.deg,
                               dec=dec,
                               frame='icrs',
                               equinox='J2000')
        low = create_named_configuration('LOWBD2', rmax=self.rmax)

        centre = numpy.mean(low.xyz, axis=0)
        distance = numpy.hypot(low.xyz[:, 0] - centre[0],
                               low.xyz[:, 1] - centre[1],
                               low.xyz[:, 2] - centre[2])
        lowouter = low.data[distance > 1000.0][::6]
        lowcore = low.data[distance < 1000.0][::3]
        low.data = numpy.hstack((lowcore, lowouter))

        blockvis = create_blockvisibility(
            low,
            times,
            frequency=frequency,
            channel_bandwidth=channel_bandwidth,
            weight=1.0,
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame("stokesI"),
            zerow=True)

        vis = convert_blockvisibility_to_visibility(blockvis)
        advice = advise_wide_field(vis, guard_band_image=2.0, delA=0.02)

        cellsize = advice['cellsize']
        npixel = advice['npixels2']

        small_model = create_image_from_visibility(blockvis,
                                                   npixel=512,
                                                   frequency=frequency,
                                                   nchan=nfreqwin,
                                                   cellsize=cellsize,
                                                   phasecentre=phasecentre)

        vis.data['imaging_weight'][...] = vis.data['weight'][...]
        vis = weight_list_serial_workflow([vis], [small_model])[0]
        vis = taper_list_serial_workflow([vis], 3 * cellsize)[0]

        blockvis = convert_visibility_to_blockvisibility(vis)

        # ### Generate the model from the GLEAM catalog, including application of the primary beam.

        beam = create_image_from_visibility(blockvis,
                                            npixel=npixel,
                                            frequency=frequency,
                                            nchan=nfreqwin,
                                            cellsize=cellsize,
                                            phasecentre=phasecentre)
        beam = create_low_test_beam(beam, use_local=False)

        flux_limit = 0.5
        original_gleam_components = create_low_test_skycomponents_from_gleam(
            flux_limit=flux_limit,
            phasecentre=phasecentre,
            frequency=frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.15)

        all_components = apply_beam_to_skycomponent(original_gleam_components,
                                                    beam)
        all_components = filter_skycomponents_by_flux(all_components,
                                                      flux_min=flux_limit)
        voronoi_components = filter_skycomponents_by_flux(all_components,
                                                          flux_min=1.5)

        def max_flux(elem):
            return numpy.max(elem.flux)

        voronoi_components = sorted(voronoi_components,
                                    key=max_flux,
                                    reverse=True)

        if nsources is not None:
            all_components = [all_components[0]]

        if nvoronoi is not None:
            voronoi_components = [voronoi_components[0]]

        self.screen = import_image_from_fits(
            arl_path('data/models/test_mpc_screen.fits'))
        all_gaintables = create_gaintable_from_screen(blockvis, all_components,
                                                      self.screen)

        gleam_skymodel_noniso = [
            SkyModel(components=[all_components[i]],
                     gaintable=all_gaintables[i])
            for i, sm in enumerate(all_components)
        ]

        # ### Now predict the visibility for each skymodel and apply the gaintable for that skymodel,
        # returning a list of visibilities, one for each skymodel. We then sum these to obtain
        # the total predicted visibility. All images and skycomponents in the same skymodel
        # get the same gaintable applied which means that in this case each skycomponent has a separate gaintable.

        self.all_skymodel_noniso_vis = convert_blockvisibility_to_visibility(
            blockvis)

        ngroup = n_workers
        future_vis = arlexecute.scatter(self.all_skymodel_noniso_vis)
        chunks = [
            gleam_skymodel_noniso[i:i + ngroup]
            for i in range(0, len(gleam_skymodel_noniso), ngroup)
        ]
        for chunk in chunks:
            result = predict_skymodel_list_arlexecute_workflow(future_vis,
                                                               chunk,
                                                               context='2d',
                                                               docal=True)
            work_vis = arlexecute.compute(result, sync=True)
            for w in work_vis:
                self.all_skymodel_noniso_vis.data['vis'] += w.data['vis']
            assert numpy.max(
                numpy.abs(self.all_skymodel_noniso_vis.data['vis'])) > 0.0

        self.all_skymodel_noniso_blockvis = convert_visibility_to_blockvisibility(
            self.all_skymodel_noniso_vis)

        # ### Remove weaker of components that are too close (0.02 rad)
        idx, voronoi_components = remove_neighbouring_components(
            voronoi_components, 0.02)

        model = create_image_from_visibility(blockvis,
                                             npixel=npixel,
                                             frequency=frequency,
                                             nchan=nfreqwin,
                                             cellsize=cellsize,
                                             phasecentre=phasecentre)

        # Use the gaintable for the brightest component as the starting gaintable
        all_gaintables[0].gain[...] = numpy.conjugate(
            all_gaintables[0].gain[...])
        all_gaintables[0].gain[...] = 1.0 + 0.0j
        self.theta_list = initialize_skymodel_voronoi(model,
                                                      voronoi_components,
                                                      all_gaintables[0])
Esempio n. 3
0
                          local_dir=dask_dir)
    times = numpy.array([-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0]) * (numpy.pi / 12.0)
    frequency = numpy.array([4e8])
    channel_bandwidth = numpy.array([25e6])
    reffrequency = numpy.max(frequency)
    phasecentre = SkyCoord(ra=+5 * u.deg, dec=20 * u.deg, frame='icrs', equinox='J2000')
    vt = create_visibility(lowcore, times, frequency, channel_bandwidth=channel_bandwidth,
                        weight=1.0, phasecentre=phasecentre,
                        polarisation_frame=PolarisationFrame('stokesI'))

    advice = advise_wide_field(vt, wprojection_planes=1)

    vt.data['vis'] *= 0.0
    npixel=256

    model = create_image_from_visibility(vt, npixel=npixel, cellsize=6.8e-5, nchan=1,
                                        polarisation_frame=PolarisationFrame('stokesI'))
    centre = model.wcs.wcs.crpix-1
    spacing_pixels = npixel // 8
    log.info('Spacing in pixels = %s' % spacing_pixels)
    spacing = model.wcs.wcs.cdelt * spacing_pixels
    locations = [-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]

    original_comps = []
    # We calculate the source positions in pixels and then calculate the
    # world coordinates to put in the skycomponent description
    for iy in locations:
        for ix in locations:
            if ix >= iy:
                p = int(round(centre[0] + ix * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[0]))), \
                    int(round(centre[1] + iy * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[1])))
                sc = pixel_to_skycoord(p[0], p[1], model.wcs)
    ## frequency and channel_bandwidth are replicated and they have already
    ## been split

    log.info('%d: predict finished in %f seconds' % (rank, end - start))
    print('%d: predict finished in %f seconds' % (rank, end - start),
          flush=True)

log.info('%d: About create image from visibility' % (rank))
sub_vis_list = numpy.array_split(vis_list, size)
sub_vis_list = comm.scatter(sub_vis_list, root=0)

sub_model_list = [
    create_image_from_visibility(
        sub_vis_list[f],
        npixel=npixel,
        frequency=[sub_frequency[rank][f]],
        channel_bandwidth=[sub_channel_bandwidth[rank][f]],
        cellsize=cellsize,
        phasecentre=phasecentre,
        polarisation_frame=PolarisationFrame("stokesI"))
    for f, freq in enumerate(sub_frequency[rank])
]


def concat_tuples(list_of_tuples):
    if len(list_of_tuples) < 2:
        result_list = list_of_tuples
    else:
        result_list = list_of_tuples[0]
        for l in list_of_tuples[1:]:
            result_list += l
    return result_list
    for field, vt in enumerate(vis_list):
        uvdist = numpy.sqrt(vt.data['uvw'][:, 0]**2 + vt.data['uvw'][:, 1]**2)
        plt.clf()
        plt.plot(uvdist, numpy.abs(vt.data['weight']), '.')
        plt.xlabel('uvdist')
        plt.ylabel('Amp Visibility')
        plt.title('Field %d' % (field))
        plt.show()

    cellsize = 0.00001

    model = create_image_from_visibility(
        vis_list[0],
        cellsize=cellsize,
        npixel=512,
        nchan=1,
        frequency=[0.5 * (8435100000.0 + 8.4851e+09)],
        channel_bandwidth=[1e8],
        imagecentre=vis_list[0].phasecentre,
        polarisation_frame=PolarisationFrame('stokesIQUV'))
    mosaic = copy_image(model)
    mosaicsens = copy_image(model)
    work = copy_image(model)

    for vt in vis_list:
        channel_model = create_image_from_visibility(
            vt,
            cellsize=cellsize,
            npixel=512,
            nchan=1,
            imagecentre=vis_list[0].phasecentre,
    def ingest_visibility(self,
                          freq=None,
                          chan_width=None,
                          times=None,
                          add_errors=False,
                          block=True,
                          bandpass=False):
        if freq is None:
            freq = [1e8]
        if chan_width is None:
            chan_width = [1e6]
        if times is None:
            times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 5)

        lowcore = create_named_configuration('LOWBD2', rmax=750.0)
        frequency = numpy.array(freq)
        channel_bandwidth = numpy.array(chan_width)

        phasecentre = SkyCoord(ra=+180.0 * u.deg,
                               dec=-60.0 * u.deg,
                               frame='icrs',
                               equinox='J2000')
        if block:
            vt = create_blockvisibility(
                lowcore,
                times,
                frequency,
                channel_bandwidth=channel_bandwidth,
                weight=1.0,
                phasecentre=phasecentre,
                polarisation_frame=PolarisationFrame("stokesI"))
        else:
            vt = create_visibility(
                lowcore,
                times,
                frequency,
                channel_bandwidth=channel_bandwidth,
                weight=1.0,
                phasecentre=phasecentre,
                polarisation_frame=PolarisationFrame("stokesI"))
        cellsize = 0.001
        model = create_image_from_visibility(
            vt,
            npixel=self.npixel,
            cellsize=cellsize,
            npol=1,
            frequency=frequency,
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame("stokesI"))
        nchan = len(self.frequency)
        flux = numpy.array(nchan * [[100.0]])
        facets = 4

        rpix = model.wcs.wcs.crpix - 1.0
        spacing_pixels = self.npixel // facets
        centers = [-1.5, -0.5, 0.5, 1.5]
        comps = list()
        for iy in centers:
            for ix in centers:
                p = int(round(rpix[0] + ix * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[0]))), \
                    int(round(rpix[1] + iy * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[1])))
                sc = pixel_to_skycoord(p[0], p[1], model.wcs, origin=1)
                comp = create_skycomponent(
                    direction=sc,
                    flux=flux,
                    frequency=frequency,
                    polarisation_frame=PolarisationFrame("stokesI"))
                comps.append(comp)
        if block:
            predict_skycomponent_visibility(vt, comps)
        else:
            predict_skycomponent_visibility(vt, comps)
        insert_skycomponent(model, comps)
        self.comps = comps
        self.model = copy_image(model)
        self.empty_model = create_empty_image_like(model)
        export_image_to_fits(
            model, '%s/test_pipeline_functions_model.fits' % (self.dir))

        if add_errors:
            # These will be the same for all calls
            numpy.random.seed(180555)
            gt = create_gaintable_from_blockvisibility(vt)
            gt = simulate_gaintable(gt, phase_error=1.0, amplitude_error=0.0)
            vt = apply_gaintable(vt, gt)

            if bandpass:
                bgt = create_gaintable_from_blockvisibility(vt, timeslice=1e5)
                bgt = simulate_gaintable(bgt,
                                         phase_error=0.01,
                                         amplitude_error=0.01,
                                         smooth_channels=4)
                vt = apply_gaintable(vt, bgt)

        return vt
Esempio n. 7
0
        advice = advise_wide_field(vt, wprojection_planes=4)

        # plt.clf()
        # plt.plot(vt.data['uvw'][:, 0], vt.data['uvw'][:, 1], '.', color='b')
        # plt.plot(-vt.data['uvw'][:, 0], -vt.data['uvw'][:, 1], '.', color='r')
        # plt.xlabel('U (wavelengths)')
        # plt.ylabel('V (wavelengths)')
        # plt.title("UV coverage")
        # plt.savefig(storedir + '/UV_coverage.pdf', format='pdf')
        # plt.show()

        vt.data['vis'] *= 0.0

        # print("*****************", vt.data['vis'])

        model = create_image_from_visibility(vt, npixel=npixel, nchan=1, cellsize=cellsize,
                                             polarisation_frame=PolarisationFrame('stokesI'))
        centre = model.wcs.wcs.crpix - 1
        spacing_pixels = npixel // 8
        # For an observation for the Sun, the disk is about 34 arcmin, if total FOV is 60 arcmin, then
        #   the disk edge would be 34/60*8
        log.info('Spacing in pixels = %s' % spacing_pixels)
        spacing = model.wcs.wcs.cdelt * spacing_pixels
        # locations = [-3.5, -2.5, -2.27, -1.5, -1., -0.5, 0.5, 1.0, 1.5, 2.0, 2.5, 3.5]
        locations = [-3.5, -3.0, -2.3, -1.5, -0.5, 0.5, 1.5, 2.3, 3.0, 3.5]

        original_comps = []
        # We calculate the source positions in pixels and then calculate the
        # world coordinates to put in the skycomponent description
        for iy in locations:
            for ix in locations:
                if True:  # ix >= iy:
Esempio n. 8
0
        channel_bandwidth=channel_bandwidth,
        weight=1.0,
        phasecentre=phasecentre,
        polarisation_frame=PolarisationFrame("stokesI"),
        zerow=True)

    vis = convert_blockvisibility_to_visibility(block_vis)
    advice = advise_wide_field(vis, guard_band_image=2.0, delA=0.02)

    cellsize = advice['cellsize']
    vis_slices = advice['vis_slices']
    npixel = advice['npixels2']

    small_model = create_image_from_visibility(block_vis,
                                               npixel=512,
                                               frequency=frequency,
                                               nchan=nfreqwin,
                                               cellsize=cellsize,
                                               phasecentre=phasecentre)

    vis.data['imaging_weight'][...] = vis.data['weight'][...]
    vis = weight_list_serial_workflow([vis], [small_model])[0]
    vis = taper_list_serial_workflow([vis], 3 * cellsize)[0]

    block_vis = convert_visibility_to_blockvisibility(vis)

    #######################################################################################################
    ### Generate the component model from the GLEAM catalog, including application of the primary beam. Read the
    # phase screen and calculate the gaintable for each component.
    flux_limit = args.flux_limit
    beam = create_image_from_visibility(block_vis,
                                        npixel=npixel,
                            weight=1.0, phasecentre=phasecentre,
                            polarisation_frame=PolarisationFrame('stokesI'))

        advice = advise_wide_field(vt, wprojection_planes=1)

        # plt.clf()
        plt.plot(vt.data['uvw'][:, 0], vt.data['uvw'][:, 1], '.', color='b')
        plt.plot(-vt.data['uvw'][:, 0], -vt.data['uvw'][:, 1], '.', color='r')
        plt.xlabel('U (wavelengths)')
        plt.ylabel('V (wavelengths)')
        plt.title("UV coverage")
        plt.savefig(results_dir+'/UV_coverage.pdf',format='pdf')

        vt.data['vis'] *= 0.0

        model = create_image_from_visibility(vt, npixel=npixel, npol=1, cellsize=cellsize)
        spacing_pixels = npixel // facets
        log.info('Spacing in pixels = %s' % spacing_pixels)
        spacing = model.wcs.wcs.cdelt* spacing_pixels #180.0 * cellsize * spacing_pixels / numpy.pi
        centers = -3.0, -2.0, -1.0, -0.5, +0.5, +1.0, 2.0, 3.0
        comps = list()
        for iy in centers:
            for ix in centers:
                pra = int(round(npixel // 2 + ix * spacing_pixels - 1))
                pdec = int(round(npixel // 2 + iy * spacing_pixels - 1))
                sc = pixel_to_skycoord(pra, pdec, model.wcs)
                log.info("Component at (%f, %f) %s" % (pra, pdec, str(sc)))
                comp = create_skycomponent(flux=flux, frequency=frequency, direction=sc,
                                           polarisation_frame=PolarisationFrame("stokesI"))
                comps.append(comp)
        predict_skycomponent_visibility(vt, comps)