예제 #1
0
 def test_create_image_from_visibility(self):
     self.actualSetUp()
     im = create_image_from_visibility(self.vis, nchan=1, npixel=128)
     assert im.data.shape == (1, 1, 128, 128)
     im = create_image_from_visibility(self.vis, frequency=self.frequency, npixel=128)
     assert im.data.shape == (len(self.frequency), 1, 128, 128)
     im = create_image_from_visibility(self.vis, frequency=self.frequency, npixel=128,
                                       nchan=1)
     assert im.data.shape == (1, 1, 128, 128)
예제 #2
0
 def test_create_primary_beams_AZELGEO(self):
     self.createVis()
     for telescope in ['VLA', 'ASKAP', 'MID', 'MID_GAUSS', 'MID_GRASP', 'LOW']:
         model = create_image_from_visibility(self.vis, cellsize=0.001, override_cellsize=False)
         beam = create_pb(model, telescope=telescope, use_local=True)
         assert numpy.max(beam.data) > 0.0
         export_image_to_fits(beam, "%s/test_primary_beam_AZELGEO_%s.fits" % (self.dir, telescope))
예제 #3
0
    def setUp(self):
        from data_models.parameters import arl_path
        self.dir = arl_path('test_results')
        self.lowcore = create_named_configuration('LOWBD2-CORE')
        self.times = (numpy.pi / (12.0)) * numpy.linspace(-3.0, 3.0, 7)
        self.frequency = numpy.array([1e8])
        self.channel_bandwidth = numpy.array([1e6])
        self.phasecentre = SkyCoord(ra=+180.0 * u.deg,
                                    dec=-60.0 * u.deg,
                                    frame='icrs',
                                    equinox='J2000')
        self.vis = create_visibility(
            self.lowcore,
            self.times,
            self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre,
            weight=1.0,
            polarisation_frame=PolarisationFrame('stokesI'),
            zerow=True)
        self.vis.data['vis'] *= 0.0

        # Create model
        self.test_model = create_test_image(cellsize=0.001,
                                            phasecentre=self.vis.phasecentre,
                                            frequency=self.frequency)
        self.vis = predict_2d(self.vis, self.test_model)
        assert numpy.max(numpy.abs(self.vis.vis)) > 0.0
        self.model = create_image_from_visibility(
            self.vis,
            npixel=512,
            cellsize=0.001,
            polarisation_frame=PolarisationFrame('stokesI'))
        self.dirty, sumwt = invert_2d(self.vis, self.model)
        self.psf, sumwt = invert_2d(self.vis, self.model, dopsf=True)
예제 #4
0
 def test_create_voltage_patterns(self):
     self.createVis()
     for telescope in ['VLA', 'ASKAP', 'LOW']:
         model = create_image_from_visibility(self.vis, cellsize=0.001, override_cellsize=False)
         beam=create_vp(model, telescope=telescope)
         assert numpy.max(numpy.abs(beam.data.real)) > 0.0
         assert numpy.max(numpy.abs(beam.data.imag)) < 1e-15, numpy.max(numpy.abs(beam.data.imag))
예제 #5
0
    def test_invert(self):

        uvfitsfile = arl_path("data/vis/ASKAP_example.fits")

        nchan_ave = 32
        nchan = 192
        for schan in range(0, nchan, nchan_ave):
            max_chan = min(nchan, schan + nchan_ave)
            bv = create_blockvisibility_from_uvfits(uvfitsfile,
                                                    range(schan, max_chan))[0]
            vis = convert_blockvisibility_to_visibility(bv)
            from processing_components.visibility.operations import convert_visibility_to_stokesI
            vis = convert_visibility_to_stokesI(vis)
            model = create_image_from_visibility(
                vis,
                npixel=256,
                polarisation_frame=PolarisationFrame('stokesI'))
            dirty, sumwt = invert_2d(vis, model, context='2d')
            assert (numpy.max(numpy.abs(dirty.data))) > 0.0
            assert dirty.shape == (nchan_ave, 1, 256, 256)
            import matplotlib.pyplot as plt
            from processing_components.image.operations import show_image
            show_image(dirty)
            plt.show()
            if self.persist:
                export_image_to_fits(
                    dirty, '%s/test_visibility_uvfits_dirty.fits' % self.dir)
예제 #6
0
    def test_create_voltage_patterns_zernike(self):

        self.createVis(freq=1.4e9)
        cellsize = 8 * numpy.pi / 180.0 / 280
        model = create_image_from_visibility(self.vis,
                                             npixel=512,
                                             cellsize=cellsize,
                                             override_cellsize=False)
        plt.clf()
        fig, axs = plt.subplots(7, 7, gridspec_kw={'hspace': 0, 'wspace': 0})
        for noll in range(1, 50):
            zernikes = [{'coeff': 1.0, 'noll': noll}]
            vp = create_vp_generic_numeric(model,
                                           pointingcentre=None,
                                           diameter=15.0,
                                           blockage=0.0,
                                           taper='gaussian',
                                           edge=0.03162278,
                                           zernikes=zernikes,
                                           padding=2,
                                           use_local=True)
            vp_data = vp.data
            vp.data = numpy.real(vp_data)
            export_image_to_fits(
                vp, "%s/test_voltage_pattern_real_%s_NOLL%d.fits" %
                (self.dir, 'MID_ZERNIKES', noll))
            row = (noll - 1) // 7
            col = (noll - 1) - 7 * row
            ax = axs[row, col]
            ax.imshow(vp.data[0, 0], vmax=0.1, vmin=-0.01)
            #ax.set_title('Noll %d' % noll)
            ax.axis('off')

        plt.savefig("zernikes.png")
        plt.show()
    def test_create_voltage_patterns_illumination(self):

        self.createVis(freq=1.4e9)
        cellsize = 8 * numpy.pi / 180.0 / 280
        model = create_image_from_visibility(self.vis,
                                             npixel=512,
                                             cellsize=cellsize,
                                             override_cellsize=False)
        plt.clf()
        fig, axs = plt.subplots(5, 5, gridspec_kw={'hspace': 0, 'wspace': 0})
        # (r ** 2 + rho * (dx * dy) + diff * (dx ** 2 - dy ** 2))
        for irho, rho in enumerate([-0.1, -0.05, 0.0, 0.05, 0.1]):
            for idiff, diff in enumerate([-0.2, -0.15, -0.1, -0.05, 0.0]):
                vp = create_vp_generic_numeric(model,
                                               pointingcentre=None,
                                               diameter=15.0,
                                               blockage=0.0,
                                               taper='gaussian',
                                               edge=0.03162278,
                                               padding=2,
                                               use_local=True,
                                               rho=rho,
                                               diff=diff)
                vp_data = vp.data
                vp.data = numpy.real(vp_data)
                if self.persist:
                    export_image_to_fits(
                        vp,
                        "%s/test_voltage_pattern_real_%s_rho%.3f_diff%.3f.fits"
                        % (self.dir, "MID_TAPER", rho, diff))
                ax = axs[irho, idiff]
                ax.imshow(vp.data[0, 0])  #, vmax=0.1, vmin=-0.01)
                ax.axis('off')

        plt.show()
예제 #8
0
    def setUp(self):
        from data_models.parameters import arl_path
        self.dir = arl_path('test_results')

        self.vnchan = 7
        self.lowcore = create_named_configuration('LOWBD2', rmax=300.0)
        self.times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 7)
        self.frequency = numpy.linspace(8e7, 1.2e8, self.vnchan)
        self.startfrequency = numpy.array([8e7])
        self.channel_bandwidth = numpy.array(
            self.vnchan * [(1.0 - 1.0e-7) *
                           (self.frequency[1] - self.frequency[0])])
        self.phasecentre = SkyCoord(ra=+180.0 * u.deg,
                                    dec=-60.0 * u.deg,
                                    frame='icrs',
                                    equinox='J2000')
        self.vis = create_visibility(
            self.lowcore,
            times=self.times,
            frequency=self.frequency,
            phasecentre=self.phasecentre,
            weight=1.0,
            polarisation_frame=PolarisationFrame('stokesI'),
            channel_bandwidth=self.channel_bandwidth)
        self.model = create_image_from_visibility(
            self.vis,
            npixel=128,
            cellsize=0.001,
            nchan=self.vnchan,
            frequency=self.startfrequency)
예제 #9
0
    def setUp(self):
        from data_models.parameters import arl_path
        self.dir = arl_path('test_results')
        self.niter = 1000
        self.lowcore = create_named_configuration('LOWBD2-CORE')
        self.nchan = 5
        self.times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 7)
        self.frequency = numpy.linspace(0.9e8, 1.1e8, self.nchan)
        self.channel_bandwidth = numpy.array(
            self.nchan * [self.frequency[1] - self.frequency[0]])
        self.phasecentre = SkyCoord(ra=+0.0 * u.deg,
                                    dec=-45.0 * u.deg,
                                    frame='icrs',
                                    equinox='J2000')
        self.vis = create_visibility(
            self.lowcore,
            self.times,
            self.frequency,
            self.channel_bandwidth,
            phasecentre=self.phasecentre,
            weight=1.0,
            polarisation_frame=PolarisationFrame('stokesI'),
            zerow=True)
        self.vis.data['vis'] *= 0.0

        # Create model
        self.test_model = create_low_test_image_from_gleam(
            npixel=512,
            cellsize=0.001,
            phasecentre=self.vis.phasecentre,
            frequency=self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            flux_limit=1.0)
        beam = create_low_test_beam(self.test_model)
        export_image_to_fits(beam,
                             "%s/test_deconvolve_mmclean_beam.fits" % self.dir)
        self.test_model.data *= beam.data
        export_image_to_fits(
            self.test_model,
            "%s/test_deconvolve_mmclean_model.fits" % self.dir)
        self.vis = predict_2d(self.vis, self.test_model)
        assert numpy.max(numpy.abs(self.vis.vis)) > 0.0
        self.model = create_image_from_visibility(
            self.vis,
            npixel=512,
            cellsize=0.001,
            polarisation_frame=PolarisationFrame('stokesI'))
        self.dirty, sumwt = invert_2d(self.vis, self.model)
        self.psf, sumwt = invert_2d(self.vis, self.model, dopsf=True)
        export_image_to_fits(
            self.dirty, "%s/test_deconvolve_mmclean-dirty.fits" % self.dir)
        export_image_to_fits(self.psf,
                             "%s/test_deconvolve_mmclean-psf.fits" % self.dir)
        window = numpy.ones(shape=self.model.shape, dtype=numpy.bool)
        window[..., 129:384, 129:384] = True
        self.innerquarter = create_image_from_array(
            window,
            self.model.wcs,
            polarisation_frame=PolarisationFrame('stokesI'))
예제 #10
0
    def actualSetUp(self,
                    time=None,
                    frequency=None,
                    dospectral=False,
                    dopol=False):
        self.lowcore = create_named_configuration('LOWBD2', rmax=600)
        self.times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 5)

        if time is not None:
            self.times = time
        log.info("Times are %s" % (self.times))

        if dospectral:
            self.nchan = 3
            self.frequency = numpy.array([0.9e8, 1e8, 1.1e8])
            self.channel_bandwidth = numpy.array([1e7, 1e7, 1e7])
        else:
            self.frequency = numpy.array([1e8])
            self.channel_bandwidth = numpy.array([1e7])

        if dopol:
            self.vis_pol = PolarisationFrame('linear')
            self.image_pol = PolarisationFrame('stokesIQUV')
        else:
            self.vis_pol = PolarisationFrame('stokesI')
            self.image_pol = PolarisationFrame('stokesI')

        if dopol:
            f = numpy.array([100.0, 20.0, -10.0, 1.0])
        else:
            f = numpy.array([100.0])

        if dospectral:
            numpy.array([f, 0.8 * f, 0.6 * f])
        else:
            numpy.array([f])

        self.phasecentre = SkyCoord(ra=+180.0 * u.deg,
                                    dec=-60.0 * u.deg,
                                    frame='icrs',
                                    equinox='J2000')
        self.componentvis = create_visibility(
            self.lowcore,
            self.times,
            self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre,
            weight=1.0,
            polarisation_frame=self.vis_pol)
        self.uvw = self.componentvis.data['uvw']
        self.componentvis.data['vis'] *= 0.0

        # Create model
        self.model = create_image_from_visibility(
            self.componentvis,
            npixel=self.npixel,
            cellsize=0.001,
            nchan=len(self.frequency),
            polarisation_frame=self.image_pol)
예제 #11
0
 def test_get_frequency_map_mfs(self):
     self.model = create_image_from_visibility(
         self.vis,
         npixel=128,
         cellsize=0.001,
         nchan=1,
         frequency=self.startfrequency)
     spectral_mode, vfrequency_map = get_frequency_map(self.vis, self.model)
     assert numpy.max(vfrequency_map) == 0
     assert spectral_mode == 'mfs'
def create_unittest_model(vis, model_pol, npixel=None, cellsize=None, nchan=1):
    advice = advise_wide_field(vis, guard_band_image=2.0, delA=0.02, facets=1,
                               wprojection_planes=1, oversampling_synthesised_beam=4.0)
    if cellsize is None:
        cellsize = advice['cellsize']
    if npixel is None:
        npixel = advice['npixels2']
    model = create_image_from_visibility(vis, npixel=npixel, cellsize=cellsize, nchan=nchan,
                                         polarisation_frame=model_pol)
    return model
예제 #13
0
 def test_create_primary_beams(self):
     self.createVis(config='LOWBD2', rmax=1000.0)
     for telescope in ['VLA', 'ASKAP', 'MID', 'LOW']:
         model = create_image_from_visibility(self.vis,
                                              cellsize=0.001,
                                              override_cellsize=False)
         beam = create_pb(model, telescope=telescope)
         assert numpy.max(beam.data) > 0.0
         export_image_to_fits(
             beam, "%s/test_primary_beam_%s.fits" % (self.dir, telescope))
예제 #14
0
 def test_get_frequency_map_different_channel(self):
     self.model = create_image_from_visibility(
         self.vis,
         npixel=128,
         cellsize=0.001,
         frequency=self.startfrequency,
         nchan=3,
         channel_bandwidth=2e7)
     spectral_mode, vfrequency_map = get_frequency_map(self.vis, self.model)
     assert numpy.max(vfrequency_map) == self.model.nchan - 1
     assert spectral_mode == 'channel'
예제 #15
0
 def test_create_voltage_patterns_MID(self):
     self.createVis(freq=1.4e9)
     cellsize=8*numpy.pi/180.0/280
     model = create_image_from_visibility(self.vis, npixel=512, cellsize=cellsize, override_cellsize=False)
     for telescope in ['MID']:
         beam=create_vp(model, telescope=telescope, padding=4)
         beam_data = beam.data
         beam.data = numpy.real(beam_data)
         beam.wcs.wcs.crval[0]=0.0
         beam.wcs.wcs.crval[1]=90.0
         export_image_to_fits(beam, "%s/test_voltage_pattern_real_zenith_%s.fits" % (self.dir, telescope))
예제 #16
0
 def test_convert_weight(self):
     cvis = convert_blockvisibility_to_visibility(self.blockvis)
     model = create_image_from_visibility(vis=cvis, npixel=256, cellsize=0.001, phasecentre=self.phasecentre,
                                          polarisation_frame=PolarisationFrame('stokesI'))
     cvis = weight_visibility(cvis, model)
     assert numpy.mean(cvis.data['imaging_weight']) < 1.0
     assert numpy.std(cvis.data['imaging_weight']) > 0.0
     dvis = decoalesce_visibility(cvis)
     assert numpy.mean(dvis.data['imaging_weight']) < 1.0
     assert numpy.std(dvis.data['imaging_weight']) > 0.0
     assert dvis.nvis == self.blockvis.nvis
예제 #17
0
def main():
    dlg_string = os.environ['DLG_UID']
    dlg_string = dlg_string[(dlg_string.rindex('_') + 1):len(dlg_string)]
    dlg_uid = dlg_string.split('/')
    Freq_Iteration = int(dlg_uid[1])  # derived from ID
    Facet_Iteration = int(dlg_uid[2])  # derived from ID
    vt = load(1)
    phasecentre_array = [[+15, -45], [+15.2, -45], [+15, -44], [+14.8, -45],
                         [+15, -46]]

    phasecentre = SkyCoord(ra=phasecentre_array[Facet_Iteration][0] * u.deg,
                           dec=phasecentre_array[Facet_Iteration][1] * u.deg,
                           frame='icrs',
                           equinox='J2000')

    model = create_image_from_visibility(vt,
                                         phasecentre=phasecentre,
                                         cellsize=0.001,
                                         npixel=256)
    dirty, sumwt = invert_function(vt, model)
    psf, sumwt = invert_function(vt, model, dopsf=True)

    #show_image(dirty)
    print("Max, min in dirty image = %.6f, %.6f, sumwt = %f" %
          (dirty.data.max(), dirty.data.min(), sumwt))

    print("Max, min in PSF         = %.6f, %.6f, sumwt = %f" %
          (psf.data.max(), psf.data.min(), sumwt))

    export_image_to_fits(
        dirty, '%s/imaging_dirty_%02d_%02d.fits' %
        (results_dir, Freq_Iteration, Facet_Iteration))
    export_image_to_fits(
        psf, '%s/imaging_psf_%02d_%02d.fits' %
        (results_dir, Freq_Iteration, Facet_Iteration))

    # Deconvolve using clean

    comp, residual = deconvolve_cube(dirty,
                                     psf,
                                     niter=1000,
                                     threshold=0.001,
                                     fracthresh=0.01,
                                     window_shape='quarter',
                                     gain=0.7,
                                     scales=[0, 3, 10, 30])

    restored = restore_cube(comp, psf, residual)

    export_image_to_fits(
        restored, '%s/imaging_clean%02d_%02d.fits' %
        (results_dir, Freq_Iteration, Facet_Iteration))
    dump(2, restored)
예제 #18
0
 def test_create_primary_beams_RADEC(self):
     self.createVis()
     for telescope in ['VLA', 'ASKAP', 'MID', 'LOW']:
         model = create_image_from_visibility(self.vis,
                                              cellsize=self.cellsize,
                                              npixel=self.npixel,
                                              override_cellsize=False)
         beam = create_pb(model, telescope=telescope, use_local=False)
         assert numpy.max(beam.data) > 0.0
         export_image_to_fits(
             beam,
             "%s/test_primary_beam_RADEC_%s.fits" % (self.dir, telescope))
예제 #19
0
 def test_convert_weight(self):
     cvis = convert_blockvisibility_to_visibility(self.blockvis)
     cvis.data['imaging_weight'] = 10.0
     model = create_image_from_visibility(
         vis=cvis,
         npixel=256,
         cellsize=0.001,
         phasecentre=self.phasecentre,
         polarisation_frame=PolarisationFrame('stokesI'))
     cvis = weight_visibility(cvis, model)
     dvis = decoalesce_visibility(cvis, overwrite=True)
     assert numpy.max(dvis.data['imaging_weight'] - 10.0) < 1e-7
     assert dvis.nvis == self.blockvis.nvis
 def test_map_create_pb(self):
     self.createVis(config='LOWBD2', rmax=1000.0)
     model = create_image_from_visibility(self.vis,
                                          cellsize=0.001,
                                          override_cellsize=False)
     beam = image_arlexecute_map_workflow(model,
                                          create_pb,
                                          facets=4,
                                          pointingcentre=self.phasecentre,
                                          telescope='MID')
     beam = arlexecute.compute(beam, sync=True)
     assert numpy.max(beam.data) > 0.0
     export_image_to_fits(
         beam, "%s/test_image_arlexecute_scatter_gather.fits" % (self.dir))
예제 #21
0
    def test_create_voltage_patterns_MID_GAUSS(self):
        self.createVis(freq=1.4e9)
        cellsize=8*numpy.pi/180.0/280
        model = create_image_from_visibility(self.vis, npixel=512, cellsize=cellsize, override_cellsize=False)
        pointingcentre = SkyCoord(ra=+17.0 * u.deg, dec=-37.0 * u.deg, frame='icrs', equinox='J2000')
        for telescope in ['MID_GAUSS']:
            beam=create_vp(model, telescope=telescope, padding=4, pointingcentre=pointingcentre)
            beam_data = beam.data
            beam.data = numpy.real(beam_data)
            export_image_to_fits(beam, "%s/test_voltage_pattern_real_%s.fits" % (self.dir, telescope))
            beam.data = numpy.imag(beam_data)
            export_image_to_fits(beam, "%s/test_voltage_pattern_imag_%s.fits" % (self.dir, telescope))

            beam=create_vp(model, telescope=telescope, pointingcentre=pointingcentre)
            beam.data = numpy.real(beam.data)
            export_image_to_fits(beam, "%s/test_voltage_pattern_offset_%s.fits" % (self.dir, telescope))
예제 #22
0
 def test_create_voltage_patterns_MID(self):
     self.createVis(freq=1.4e9)
     model = create_image_from_visibility(self.vis,
                                          npixel=self.npixel,
                                          cellsize=self.cellsize,
                                          override_cellsize=False)
     for telescope in ['MID', 'MID_FEKO_B1', 'MID_FEKO_B2', 'MID_FEKO_Ku']:
         beam = create_vp(model, telescope=telescope, padding=4)
         beam_data = beam.data
         beam.data = numpy.real(beam_data)
         beam.wcs.wcs.crval[0] = 0.0
         beam.wcs.wcs.crval[1] = 90.0
         if self.persist:
             export_image_to_fits(
                 beam, "%s/test_voltage_pattern_real_zenith_%s.fits" %
                 (self.dir, telescope))
예제 #23
0
 def test_create_voltage_patterns_MID_GAUSS(self):
     self.createVis()
     model = create_image_from_visibility(self.vis,
                                          npixel=self.npixel,
                                          cellsize=self.cellsize,
                                          override_cellsize=False)
     for telescope in ['MID_GAUSS']:
         beam = create_vp(model, telescope=telescope, padding=4)
         beam_data = beam.data
         beam.data = numpy.real(beam_data)
         export_image_to_fits(
             beam,
             "%s/test_voltage_pattern_real_%s.fits" % (self.dir, telescope))
         beam.data = numpy.imag(beam_data)
         export_image_to_fits(
             beam,
             "%s/test_voltage_pattern_imag_%s.fits" % (self.dir, telescope))
예제 #24
0
def image_2d(vis, npixel_advice, cell_advice, channel, results_dir):
    """Do 2D imaging of visibility data.
        
    Args:
    vis (obj): ARL visibility data.
    npixel_advice (float): number of pixels in output image.
    cell_advice (float): cellsize in output image.
    channel (int): channel number to be imaged (affects output filename).
    results_dir (str): directory to save results.
    
    Returns:
    dirty: dirty image.
    psf: image of psf.
    """
    try:
        vis_slices = len(np.unique(vis.time))
        print("There are %d timeslices" % vis_slices)
        # Obtain advice on w-proj parameters:
        advice = advise_wide_field(vis)
        # Create a model image:
        model = create_image_from_visibility(vis, cellsize=cell_advice, npixel=npixel_advice, phasecentre=vis.phasecentre, polarisation_frame=PolarisationFrame('stokesIQUV'))
        # Weight the visibilities:
        vis, _, _ = weight_visibility(vis, model)
        
        # Create a dirty image:
        dirty, sumwt = invert_serial(vis, model, context='2d', vis_slices=1, padding=2)
        # Create the psf:
        psf, sumwt = invert_serial(vis, model, dopsf=True, context='2d', vis_slices=1, padding=2)
        
        # Save to disk:
        export_image_to_fits(dirty, '%s/imaging_dirty_WStack-%s.fits'
                             % (results_dir, channel))
        export_image_to_fits(psf, '%s/imaging_psf_WStack-%s.fits'
                             % (results_dir, channel))
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise
    return dirty, psf
    def setUp(self):
        from data_models.parameters import arl_path
        self.dir = arl_path('test_results')
        self.lowcore = create_named_configuration('LOWBD2-CORE')
        self.times = (numpy.pi / (12.0)) * numpy.linspace(-3.0, 3.0, 7)
        self.frequency = numpy.array([1e8])
        self.channel_bandwidth = numpy.array([1e6])
        self.phasecentre = SkyCoord(ra=+180.0 * u.deg,
                                    dec=-60.0 * u.deg,
                                    frame='icrs',
                                    equinox='J2000')
        self.vis = create_visibility(
            self.lowcore,
            self.times,
            self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre,
            weight=1.0,
            polarisation_frame=PolarisationFrame('stokesI'))
        self.vis.data['vis'] *= 0.0
        self.vis.data['uvw'][:, 2] = 0.0

        # Create model
        self.model = create_test_image(cellsize=0.0015,
                                       phasecentre=self.vis.phasecentre,
                                       frequency=self.frequency)
        self.model.data[self.model.data > 1.0] = 1.0
        self.vis = predict_2d(self.vis, self.model)
        assert numpy.max(numpy.abs(self.vis.vis)) > 0.0
        export_image_to_fits(
            self.model, '%s/test_solve_skycomponent_model.fits' % (self.dir))
        self.bigmodel = create_image_from_visibility(self.vis,
                                                     cellsize=0.0015,
                                                     npixel=512)
        residual, sumwt = invert_2d(self.vis, self.bigmodel)
        export_image_to_fits(
            residual,
            '%s/test_solve_skycomponent_msclean_dirty.fits' % (self.dir))
    def actualSetup(self,
                    vnchan=1,
                    doiso=True,
                    ntimes=5,
                    flux_limit=2.0,
                    zerow=True,
                    fixed=False):

        nfreqwin = vnchan
        rmax = 300.0
        npixel = 512
        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=-60.0 * u.deg,
                               dec=-60.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"),
            zerow=zerow)

        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)

        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
            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_skymodel-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')
        if doiso:
            export_image_to_fits(
                dirty, "%s/test_skymodel-initial-iso-residual.fits" % self.dir)
        else:
            export_image_to_fits(
                dirty,
                "%s/test_skymodel-initial-noiso-residual.fits" % self.dir)

        self.skymodels = [
            SkyModel(components=[cm], fixed=fixed) for cm in self.components
        ]
    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
#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')