Ejemplo n.º 1
0
 def core_solve(self,
                spf,
                dpf,
                phase_error=0.1,
                amplitude_error=0.0,
                leakage=0.0,
                phase_only=True,
                niter=200,
                crosspol=False,
                residual_tol=1e-6,
                f=[100.0, 50.0, -10.0, 40.0]):
     self.actualSetup(spf, dpf, f=f)
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt,
                             phase_error=phase_error,
                             amplitude_error=amplitude_error,
                             leakage=leakage)
     original = copy_visibility(self.vis)
     vis = apply_gaintable(self.vis, gt)
     gtsol = solve_gaintable(self.vis,
                             original,
                             phase_only=phase_only,
                             niter=niter,
                             crosspol=crosspol,
                             tol=1e-6)
     vis = apply_gaintable(vis, gtsol, inverse=True)
     residual = numpy.max(gtsol.residual)
     assert residual < residual_tol, "%s %s Max residual = %s" % (spf, dpf,
                                                                  residual)
     log.debug(qa_gaintable(gt))
     assert numpy.max(numpy.abs(gtsol.gain - 1.0)) > 0.1
Ejemplo n.º 2
0
 def test_apply_gaintable_and_inverse_both(self):
     for spf, dpf in [('stokesIQUV', 'linear'), ('stokesIQUV', 'circular')]:
         self.actualSetup(spf, dpf)
         gt = create_gaintable_from_blockvisibility(self.vis)
         log.info("Created gain table: %s" % (gaintable_summary(gt)))
         gt = simulate_gaintable(gt, phase_error=0.1, amplitude_error=0.1)
         original = copy_visibility(self.vis)
         vis = apply_gaintable(self.vis, gt)
         vis = apply_gaintable(self.vis, gt, inverse=True)
         error = numpy.max(numpy.abs(vis.vis - original.vis))
         assert error < 1e-12, "Error = %s" % (error)
Ejemplo n.º 3
0
 def ingest_visibility(self, freq=None, chan_width=None, times=None, reffrequency=None, add_errors=False,
                       block=True):
     if freq is None:
         freq = [1e8]
     if times is None:
         ntimes = 5
         times = numpy.linspace(-numpy.pi / 3.0, numpy.pi / 3.0, ntimes)
     if chan_width is None:
         chan_width = [1e6]
     
     if reffrequency is None:
         reffrequency = [1e8]
     lowcore = create_named_configuration('LOWBD2-CORE')
     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=reffrequency, phasecentre=phasecentre,
                                          polarisation_frame=PolarisationFrame("stokesI"))
     flux = numpy.array([[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(flux=flux, frequency=frequency, direction=sc,
                                        polarisation_frame=PolarisationFrame("stokesI"))
             comps.append(comp)
     if block:
         predict_skycomponent_blockvisibility(vt, comps)
     else:
         predict_skycomponent_visibility(vt, comps)
     insert_skycomponent(model, comps)
     self.model = copy_image(model)
     self.empty_model = create_empty_image_like(model)
     export_image_to_fits(model, '%s/test_pipeline_bags_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)
     return vt
Ejemplo n.º 4
0
def calibrate_blockvisibility(bvt: BlockVisibility,
                              model=None,
                              components=None,
                              predict=predict_2d,
                              **kwargs):
    """ calibrate BlockVisibility with respect to model and optionally components

    :param bvt: BlockVisibility
    :param model: Model image
    :param components: Sky components
    :returns: Calibrated BlockVisibility

    """
    assert model is not None or components is not None, "calibration requires a model or skycomponents"

    if model is not None:
        vtpred = convert_blockvisibility_to_visibility(bvt)
        vtpred = predict(vtpred, model, **kwargs)
        bvtpred = decoalesce_visibility(vtpred)
        if components is not None:
            bvtpred = predict_skycomponent_blockvisibility(bvtpred, components)
    else:
        bvtpred = copy_visibility(bvt, zero=True)
        bvtpred = predict_skycomponent_blockvisibility(bvtpred, components)

    gt = solve_gaintable(bvt, bvtpred, **kwargs)
    return apply_gaintable(bvt, gt, **kwargs)
Ejemplo n.º 5
0
def insert_unittest_errors(vt, seed=180555):
    """Simulate gain errors and apply
    
    :param vt:
    :param phase_error:
    :param amplitude_error:
    :return:
    """
    numpy.random.seed(seed)
    controls = create_calibration_controls()
    amp_errors = {'T': 0.0, 'G': 0.01, 'B': 0.01}
    phase_errors = {'T': 1.0, 'G': 0.1, 'B': 0.01}
    for c in "TGB":
        gaintable = \
            create_gaintable_from_blockvisibility(vt, timeslice=controls[c]['timeslice'])
        gaintable = simulate_gaintable(
            gaintable,
            timeslice=controls[c]['timeslice'],
            phase_only=controls[c]['phase_only'],
            crosspol=controls[c]['shape'] == 'matrix',
            phase_error=phase_errors[c],
            amplitude_error=amp_errors[c])
        vt = apply_gaintable(vt,
                             gaintable,
                             inverse=True,
                             timeslice=controls[c]['timeslice'])

    return vt
Ejemplo n.º 6
0
def calibrate_visibility(vt: Visibility,
                         model: Image = None,
                         components=None,
                         predict=predict_2d,
                         **kwargs) -> Visibility:
    """ calibrate Visibility with respect to model and optionally components

    :param vt: Visibility
    :param model: Model image
    :param components: Sky components
    :return: Calibrated visibility
    """
    assert model is not None or components is not None, "calibration requires a model or skycomponents"

    vtpred = copy_visibility(vt, zero=True)

    if model is not None:
        vtpred = predict(vtpred, model, **kwargs)
        if components is not None:
            vtpred = predict_skycomponent_visibility(vtpred, components)
    else:
        vtpred = predict_skycomponent_visibility(vtpred, components)

    bvt = decoalesce_visibility(vt)
    bvtpred = decoalesce_visibility(vtpred)
    gt = solve_gaintable(bvt, bvtpred, **kwargs)
    bvt = apply_gaintable(bvt,
                          gt,
                          inverse=get_parameter(kwargs, "inverse", False))
    return convert_blockvisibility_to_visibility(bvt)
Ejemplo n.º 7
0
def peel_skycomponent_blockvisibility(vis: BlockVisibility, sc: Union[Skycomponent, List[Skycomponent]], remove=True)\
        -> (BlockVisibility, List[GainTable]):
    """ Peel a collection of components.
    
    Sequentially solve the gain towards each Skycomponent and optionally remove the corrupted visibility from the
    observed visibility.

    :param params:
    :param vis: Visibility to be processed
    :param sc: Skycomponent or list of Skycomponents
    :return: subtracted visibility and list of GainTables
    """
    assert isinstance(
        vis, BlockVisibility), "vis is not a BlockVisibility: %r" % vis

    if not isinstance(sc, collections.Iterable):
        sc = [sc]

    gtlist = []
    for comp in sc:
        assert comp.shape == 'Point', "Cannot handle shape %s" % comp.shape

        modelvis = copy_visibility(vis, zero=True)
        modelvis = predict_skycomponent_blockvisibility(modelvis, comp)
        gt = solve_gaintable(vis, modelvis, phase_only=False)
        modelvis = apply_gaintable(modelvis, gt)
        if remove:
            vis.data['vis'] -= modelvis.data['vis']
        gtlist.append(gt)

    return vis, gtlist
 def test_apply_gaintable_null(self):
     for spf, dpf in[('stokesI', 'stokesI'), ('stokesIQUV', 'linear'), ('stokesIQUV', 'circular')]:
         self.actualSetup(spf, dpf)
         gt = create_gaintable_from_blockvisibility(self.vis, timeslice='auto')
         gt.data['gain']*=0.0
         original = copy_visibility(self.vis)
         vis = apply_gaintable(self.vis, gt, inverse=True, timeslice='auto')
         error = numpy.max(numpy.abs(vis.vis[:,0,1,...] - original.vis[:,0,1,...]))
         assert error < 1e-12, "Error = %s" % (error)
 def selfcal_single(vis, model, **kwargs):
     if vis is not None:
         predicted = copy_visibility(vis)
         predicted = predict(predicted, model, **kwargs)
         gtsol = solve_gaintable(vis, predicted, **kwargs)
         vis = apply_gaintable(vis, gtsol, inverse=True, **kwargs)
         return vis
     else:
         return None
Ejemplo n.º 10
0
 def test_create_gaintable_from_visibility(self):
     for spf, dpf in [('stokesIQUV', 'linear'), ('stokesIQUV', 'circular')]:
         self.actualSetup(spf, dpf)
         gt = create_gaintable_from_blockvisibility(self.vis)
         log.info("Created gain table: %s" % (gaintable_summary(gt)))
         gt = simulate_gaintable(gt, phase_error=0.1)
         original = copy_visibility(self.vis)
         vis = apply_gaintable(self.vis, gt)
         assert numpy.max(numpy.abs(vis.vis - original.vis)) > 0.0
Ejemplo n.º 11
0
def create_blockvisibility_iterator(config: Configuration, times: numpy.array, frequency: numpy.array,
                                    channel_bandwidth, phasecentre: SkyCoord, weight: float = 1,
                                    polarisation_frame=PolarisationFrame('stokesI'), integration_time=1.0,
                                    number_integrations=1, predict=predict_timeslice, model=None, components=None,
                                    phase_error=0.0, amplitude_error=0.0, sleep=0.0, **kwargs) -> BlockVisibility:
    """ Create a sequence of Visibilities and optionally predicting and coalescing

    This is useful mainly for performing large simulations. Do something like::
    
        vis_iter = create_blockvisibility_iterator(config, times, frequency, channel_bandwidth, phasecentre=phasecentre,
                                              weight=1.0, integration_time=30.0, number_integrations=3)

        for i, vis in enumerate(vis_iter):
        if i == 0:
            fullvis = vis
        else:
            fullvis = append_visibility(fullvis, vis)


    :param config: Configuration of antennas
    :param times: hour angles in radians
    :param frequency: frequencies (Hz] Shape [nchan]
    :param weight: weight of a single sample
    :param phasecentre: phasecentre of observation
    :param npol: Number of polarizations
    :param integration_time: Integration time ('auto' or value in s)
    :param number_integrations: Number of integrations to be created at each time.
    :param model: Model image to be inserted
    :param components: Components to be inserted
    :param sleep_time: Time to sleep between yields
    :return: Visibility

    """
    for time in times:
        actualtimes = time + numpy.arange(0, number_integrations) * integration_time * numpy.pi / 43200.0
        bvis = create_blockvisibility(config, actualtimes, frequency=frequency, phasecentre=phasecentre, weight=weight,
                                     polarisation_frame=polarisation_frame, integration_time=integration_time,
                                     channel_bandwidth=channel_bandwidth)
        
        if model is not None:
            vis = predict(bvis, model, **kwargs)
            bvis = convert_visibility_to_blockvisibility(vis)
        
        if components is not None:
            bvis = predict_skycomponent_blockvisibility(bvis, components)
        
        # Add phase errors
        if phase_error > 0.0 or amplitude_error > 0.0:
            gt = create_gaintable_from_blockvisibility(bvis)
            gt = simulate_gaintable(gt=gt, phase_error=phase_error, amplitude_error=amplitude_error)
            vis = apply_gaintable(bvis, gt)
        
        import time
        time.sleep(sleep)
        
        yield bvis
 def test_apply_gaintable_only(self):
     for spf, dpf in[('stokesI', 'stokesI'), ('stokesIQUV', 'linear'), ('stokesIQUV', 'circular')]:
         self.actualSetup(spf, dpf)
         gt = create_gaintable_from_blockvisibility(self.vis, timeslice='auto')
         log.info("Created gain table: %s" % (gaintable_summary(gt)))
         gt = simulate_gaintable(gt, phase_error=0.1, amplitude_error=0.01, timeslice='auto')
         original = copy_visibility(self.vis)
         vis = apply_gaintable(self.vis, gt, timeslice='auto')
         error = numpy.max(numpy.abs(vis.vis - original.vis))
         assert error > 10.0, "Error = %f" % (error)
Ejemplo n.º 13
0
 def test_solve_gaintable_scalar(self):
     self.actualSetup('stokesI', 'stokesI', f=[100.0])
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt, phase_error=10.0, amplitude_error=0.0)
     original = copy_visibility(self.vis)
     self.vis = apply_gaintable(self.vis, gt)
     gtsol = solve_gaintable(self.vis, original, phase_only=True, niter=200)
     residual = numpy.max(gtsol.residual)
     assert residual < 3e-8, "Max residual = %s" % (residual)
     assert numpy.max(numpy.abs(gtsol.gain - 1.0)) > 0.1
 def make_e(vis, calskymodel, evis_all):
     # Return the estep for a given skymodel
     evis = copy_visibility(vis)
     tvis = copy_visibility(vis, zero=True)
     tvis = predict_skycomponent_visibility(tvis, calskymodel[0].components)
     tvis = apply_gaintable(tvis, calskymodel[1])
     # E step is the data model for a window plus the difference between the observed data
     # and the summed data models or, put another way, its the observed data minus the
     # summed visibility for all other windows
     evis.data['vis'][...] = tvis.data['vis'][...] + vis.data['vis'][
         ...] - evis_all.data['vis'][...]
     return evis
 def test_create_gaintable_from_visibility_interval(self):
     for timeslice in [10.0, 'auto', 1e5]:
         for spf, dpf in[('stokesIQUV', 'linear')]:
             self.actualSetup(spf, dpf)
             gt = create_gaintable_from_blockvisibility(self.vis, timeslice=timeslice)
             log.info("Created gain table: %s" % (gaintable_summary(gt)))
             gt = simulate_gaintable(gt, phase_error=1.0, timeslice=timeslice)
             original = copy_visibility(self.vis)
             vis = apply_gaintable(self.vis, gt, timeslice=timeslice)
             assert numpy.max(numpy.abs(original.vis)) > 0.0
             assert numpy.max(numpy.abs(vis.vis)) > 0.0
             assert numpy.max(numpy.abs(vis.vis - original.vis)) > 0.0
Ejemplo n.º 16
0
def calibrate_function(vis, model_vis, context='T', controls=None, iteration=0, **kwargs):
    """ Calibrate using algorithm specified by context
    
    The context string can denote a sequence of calibrations e.g. TGB with different timescales.

    :param vis:
    :param model_vis:
    :param context: calibration contexts in order of correction e.g. 'TGB'
    :param control: controls dictionary, modified as necessary
    :param iteration: Iteration number to be compared to the 'first_selfcal' field.
    :param kwargs:
    :return: Calibrated data, dict(gaintables)
    """
    gaintables = {}
    
    if controls is None:
        controls = create_calibration_controls(**kwargs)
    
    isVis = isinstance(vis, Visibility)
    if isVis:
        avis = convert_visibility_to_blockvisibility(vis)
    else:
        avis = vis
    
    isMVis = isinstance(model_vis, Visibility)
    if isMVis:
        amvis = convert_visibility_to_blockvisibility(model_vis)
    else:
        amvis = model_vis
    
    for c in context:
        if iteration >= controls[c]['first_selfcal']:
            gaintables[c] = \
                create_gaintable_from_blockvisibility(avis,
                                                      timeslice=controls[c]['timeslice'])
            gaintables[c] = solve_gaintable(avis, amvis,
                                            timeslice=controls[c]['timeslice'],
                                            phase_only=controls[c]['phase_only'],
                                            crosspol=controls[c]['shape'] == 'matrix')
            log.debug('calibrate_function: Jones matrix %s, iteration %d' % (c, iteration))
            log.debug(qa_gaintable(gaintables[c], context='Jones matrix %s, iteration %d' % (c, iteration)))
            avis = apply_gaintable(avis, gaintables[c],
                                   inverse=True,
                                   timeslice=controls[c]['timeslice'])
        else:
            log.debug('calibrate_function: Jones matrix %s not solved, iteration %d' % (c, iteration))
    if isVis:
        return convert_blockvisibility_to_visibility(avis), gaintables
    else:
        return avis, gaintables
Ejemplo n.º 17
0
def skymodel_cal_e_step(vis: BlockVisibility, evis_all: BlockVisibility, calskymodel, **kwargs):
    """Calculates E step in equation A12

    This is the data model for this window plus the difference between observed data and summed data models

    :param evis_all: Sum data models
    :param csm: csm element being fit
    :param kwargs:
    :return: Data model (i.e. visibility) for this csm
    """
    evis = copy_visibility(evis_all)
    tvis = copy_visibility(vis, zero=True)
    tvis = predict_skycomponent_visibility(tvis, calskymodel[0].components)
    tvis = apply_gaintable(tvis, calskymodel[1])
    evis.data['vis'][...] = tvis.data['vis'][...] + vis.data['vis'][...] - evis_all.data['vis'][...]
    return evis
def sagecal_e_all(vis: BlockVisibility, thetas, **kwargs):
    """Calculates E step in equation A12
    
    This is the sum of the data models over all windows

    :param vis:
    :param thetas:
    :param kwargs:
    :return:
    """
    evis = copy_visibility(vis, zero=True)
    tvis = copy_visibility(vis, zero=True)
    for i, theta in enumerate(thetas):
        tvis.data['vis'][...] = 0.0
        tvis = predict_skycomponent_visibility(tvis, theta[0])
        tvis = apply_gaintable(tvis, theta[1])
        evis.data['vis'][...] += tvis.data['vis'][...]
    return evis
Ejemplo n.º 19
0
def skymodel_cal_e_all(vis: BlockVisibility, calskymodels):
    """Calculates E step in equation A12

    This is the sum of the data models over all skymodel

    :param vis: Visibility
    :param csm: List of (skymodel, gaintable) tuples
    :param kwargs:
    :return: Sum of data models (i.e. a visibility)
    """
    evis = copy_visibility(vis, zero=True)
    tvis = copy_visibility(vis, zero=True)
    for csm in calskymodels:
        tvis.data['vis'][...] = 0.0
        tvis = predict_skycomponent_visibility(tvis, csm[0].components)
        tvis = apply_gaintable(tvis, csm[1])
        evis.data['vis'][...] += tvis.data['vis'][...]
    return evis
Ejemplo n.º 20
0
 def ingest_visibility(self, freq=1e8, chan_width=1e6, times=None, reffrequency=None, add_errors=False):
     if times is None:
         times = [0.0]
     if reffrequency is None:
         reffrequency = [1e8]
     lowcore = create_named_configuration('LOWBD2-CORE')
     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')
     # Observe at zenith to ensure that timeslicing works well. We test that elsewhere.
     phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000')
     vt = create_blockvisibility(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=reffrequency,
                                          polarisation_frame=PolarisationFrame("stokesI"))
     flux = numpy.array([[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)
             comps.append(create_skycomponent(flux=flux, frequency=vt.frequency, direction=sc,
                                              polarisation_frame=PolarisationFrame("stokesI")))
     predict_skycomponent_blockvisibility(vt, comps)
     insert_skycomponent(model, comps)
     self.actualmodel = copy_image(model)
     export_image_to_fits(model, '%s/test_imaging_model.fits' % (self.results_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)
     return vt
def sagecal_e_step(vis: BlockVisibility,
                   evis_all: BlockVisibility,
                   theta,
                   beta=1.0,
                   **kwargs):
    """Calculates E step in equation A12
    
    This is the data model for this window plus the difference between observed data and summed data models

    :param vis:
    :param theta:
    :param kwargs:
    :return:
    """
    evis = copy_visibility(evis_all)
    tvis = copy_visibility(vis, zero=True)
    tvis = predict_skycomponent_visibility(tvis, theta[0])
    tvis = apply_gaintable(tvis, theta[1])
    evis.data['vis'][...] = tvis.data['vis'][...] + \
                            beta * (vis.data['vis'][...] - evis_all.data['vis'][...])
    return evis
 def actualSetup(self, sky_pol_frame='stokesI', data_pol_frame='stokesI', f=None, vnchan=1, doiso=True,
                 ntimes=1, flux_limit=18.0):
     
     nfreqwin = vnchan
     ntimes = ntimes
     rmax = 300.0
     npixel = 1024
     cellsize = 0.001
     frequency = numpy.linspace(0.8e8, 1.2e8, nfreqwin)
     if nfreqwin > 1:
         channel_bandwidth = numpy.array(nfreqwin * [frequency[1] - frequency[0]])
     else:
         channel_bandwidth = [0.4e8]
     times = numpy.linspace(-numpy.pi / 3.0, numpy.pi / 3.0, ntimes)
     
     phasecentre = SkyCoord(ra=+0.0 * u.deg, dec=-45.0 * u.deg, frame='icrs', equinox='J2000')
     
     lowcore = create_named_configuration('LOWBD2', rmax=rmax)
     
     block_vis = create_blockvisibility(lowcore, times, frequency=frequency, channel_bandwidth=channel_bandwidth,
                                        weight=1.0, phasecentre=phasecentre,
                                        polarisation_frame=PolarisationFrame("stokesI"))
     
     block_vis.data['uvw'][..., 2] = 0.0
     self.beam = create_image_from_visibility(block_vis, npixel=npixel, frequency=[numpy.average(frequency)],
                                              nchan=nfreqwin,
                                              channel_bandwidth=[numpy.sum(channel_bandwidth)], cellsize=cellsize,
                                              phasecentre=phasecentre)
     
     self.components = create_low_test_skycomponents_from_gleam(flux_limit=flux_limit, phasecentre=phasecentre,
                                                                frequency=frequency,
                                                                polarisation_frame=PolarisationFrame('stokesI'),
                                                                radius=npixel * cellsize)
     self.beam = create_low_test_beam(self.beam)
     self.components = apply_beam_to_skycomponent(self.components, self.beam, flux_limit=flux_limit / 100.0)
     print("Number of components %d" % len(self.components))
     
     self.vis = copy_visibility(block_vis, zero=True)
     gt = create_gaintable_from_blockvisibility(block_vis, timeslice='auto')
     for i, sc in enumerate(self.components):
         if sc.flux[0, 0] > 10:
             sc.flux[...] /= 10.0
         print('Component %d, flux = %s' % (i, str(sc.flux[0, 0])))
         component_vis = copy_visibility(block_vis, zero=True)
         gt = simulate_gaintable(gt, amplitude_error=0.0, phase_error=0.1, seed=None)
         component_vis = predict_skycomponent_visibility(component_vis, sc)
         component_vis = apply_gaintable(component_vis, gt)
         self.vis.data['vis'][...] += component_vis.data['vis'][...]
     
     # Do an isoplanatic selfcal
     self.model_vis = copy_visibility(self.vis, zero=True)
     self.model_vis = predict_skycomponent_visibility(self.model_vis, self.components)
     if doiso:
         gt = solve_gaintable(self.vis, self.model_vis, phase_only=True, timeslice='auto')
         self.vis = apply_gaintable(self.vis, gt, inverse=True)
     
     self.model_vis = convert_blockvisibility_to_visibility(self.model_vis)
     self.model_vis, _, _ = weight_visibility(self.model_vis, self.beam)
     self.dirty_model, sumwt = invert_function(self.model_vis, self.beam, context='2d')
     export_image_to_fits(self.dirty_model, "%s/test_sagecal-model_dirty.fits" % self.dir)
     
     lvis = convert_blockvisibility_to_visibility(self.vis)
     lvis, _, _ = weight_visibility(lvis, self.beam)
     dirty, sumwt = invert_function(lvis, self.beam, context='2d')
     print(qa_image(dirty))
     export_image_to_fits(dirty, "%s/test_sagecal-initial_dirty.fits" % self.dir)
block_vis=convert_visibility_to_blockvisibility(predicted_vis)
#print("np.sum(block_vis.data): ", numpy.sum(block_vis.data['vis']))

#print("nchan npol nants ", block_vis.nchan, block_vis.npol, block_vis.nants)
#print("uvw", block_vis.uvw, numpy.sum(block_vis.uvw))
#print("vis", block_vis.vis, numpy.sum(block_vis.vis))
#print("weight", block_vis.weight, numpy.sum(block_vis.weight))
#print("time", block_vis.time, numpy.sum(block_vis.time))
#print("integration_time", block_vis.integration_time, numpy.sum(block_vis.integration_time))
#print("nvis, size", block_vis.nvis, block_vis.size())

gt = create_gaintable_from_blockvisibility(block_vis)
#print("np.sum(gt.data): ", numpy.sum(gt.data['gain']))
gt = simulate_gaintable(gt, phase_error=1.0)
#print("np.sum(gt.data): ", numpy.sum(gt.data['gain']))
blockvis = apply_gaintable(block_vis, gt)
#print("np.sum(blockvis.data): ", numpy.sum(blockvis.data['vis']))


model = create_image_from_visibility(block_vis, npixel=npixel, frequency=[numpy.average(frequency)], nchan=1,
    channel_bandwidth=[numpy.sum(channel_bandwidth)], cellsize=cellsize, phasecentre=phasecentre)

#print("model sum, min, max, shape: ", numpy.sum(model.data), numpy.amin(model.data), numpy.amax(model.data), model.shape)

print(qa_image(model, context='Blockvis model image'))
export_image_to_fits(model, '%s/imaging-blockvis_model.fits'
                     % (results_dir))

dirty, sumwt = invert_function(predicted_vis, model, vis_slices=vis_slices, dopsf=False, context='wstack')

 def corrupt_vis(vis, gt, **kwargs):
     if gt is None:
         gt = create_gaintable_from_blockvisibility(vis, **kwargs)
         gt = simulate_gaintable(gt, **kwargs)
     return apply_gaintable(vis, gt)
 def predict_and_apply(ovis, calskymodel):
     tvis = copy_visibility(ovis, zero=True)
     tvis = predict_skycomponent_visibility(tvis, calskymodel[0].components)
     tvis = apply_gaintable(tvis, calskymodel[1])
     return tvis
 def solve_and_apply(vis, modelvis):
     gt = solve_gaintable(vis, modelvis, **kwargs)
     return apply_gaintable(vis, gt, **kwargs)
    def test_peel_skycomponent_blockvisibility(self):
        df = 1e6
        frequency = numpy.array([1e8 - df, 1e8, 1e8 + df])
        channel_bandwidth = numpy.array([df, df, df])

        # Define the component and give it some spectral behaviour
        f = numpy.array([100.0, 20.0, -10.0, 1.0])
        flux = numpy.array([f, 0.8 * f, 0.6 * f])
        phasecentre = SkyCoord(0 * u.deg, -60.0 * u.deg)

        config = create_named_configuration('LOWBD2-CORE')
        peeldirection = SkyCoord(+15 * u.deg, -60.0 * u.deg)
        times = numpy.linspace(-3.0, 3.0, 7) * numpy.pi / 12.0

        # Make the visibility
        vis = create_blockvisibility(
            config,
            times,
            frequency,
            phasecentre=phasecentre,
            weight=1.0,
            polarisation_frame=PolarisationFrame('linear'),
            channel_bandwidth=channel_bandwidth)
        vis.data['vis'][...] = 0.0

        # First add in the source to be peeled.
        peel = Skycomponent(direction=peeldirection,
                            frequency=frequency,
                            flux=flux,
                            polarisation_frame=PolarisationFrame("stokesIQUV"))
        vis = predict_skycomponent_visibility(vis, peel)

        # Make a gaintable and apply it to the visibility of the peeling source
        gt = create_gaintable_from_blockvisibility(vis, timeslice='auto')
        gt = simulate_gaintable(gt,
                                phase_error=0.01,
                                amplitude_error=0.01,
                                timeslice='auto')
        gt.data['gain'] *= 0.3
        vis = apply_gaintable(vis, gt, timeslice='auto')

        # Now create a plausible field using the GLEAM sources
        model = create_image_from_visibility(
            vis,
            cellsize=0.001,
            frequency=frequency,
            polarisation_frame=PolarisationFrame('stokesIQUV'))

        bm = create_low_test_beam(model=model)
        sc = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            polarisation_frame=PolarisationFrame("stokesIQUV"),
            frequency=frequency,
            kind='cubic',
            phasecentre=phasecentre,
            radius=0.1)
        sc = apply_beam_to_skycomponent(sc, bm)
        # Add in the visibility due to these sources
        vis = predict_skycomponent_visibility(vis, sc)
        assert numpy.max(numpy.abs(vis.vis)) > 0.0

        # Now we can peel
        vis, peel_gts = peel_skycomponent_blockvisibility(vis, peel)

        assert len(peel_gts) == 1
        residual = numpy.max(peel_gts[0].residual)
        assert residual < 0.7, "Peak residual %.6f too large" % (residual)

        im, sumwt = invert_timeslice(vis, model, timeslice='auto')
        qa = qa_image(im)

        assert numpy.abs(qa.data['max'] - 14.2) < 1.0, str(qa)