def test_fill_awterm_to_convolutionfunction(self):
        make_pb = functools.partial(create_pb_generic,
                                    diameter=35.0,
                                    blockage=0.0,
                                    use_local=False)
        pb = make_pb(self.image)
        if self.persist:
            export_image_to_fits(
                pb, "%s/test_convolutionfunction_awterm_pb.fits" % self.dir)
        gcf, cf = create_awterm_convolutionfunction(self.image,
                                                    make_pb=make_pb,
                                                    nw=201,
                                                    wstep=8,
                                                    oversampling=8,
                                                    support=60,
                                                    use_aaf=True)

        assert numpy.max(numpy.abs(cf.data)) > 0.0
        if self.persist:
            export_image_to_fits(
                gcf, "%s/test_convolutionfunction_awterm_gcf.fits" % self.dir)
        cf_image = convert_convolutionfunction_to_image(cf)
        cf_image.data = numpy.real(cf_image.data)
        if self.persist:
            export_image_to_fits(
                cf_image,
                "%s/test_convolutionfunction_awterm_cf.fits" % self.dir)

        peak_location = numpy.unravel_index(numpy.argmax(numpy.abs(cf.data)),
                                            cf.shape)
        assert peak_location == (0, 0, 100, 4, 4, 30, 30), peak_location
        assert numpy.abs(cf.data[peak_location] - (0.07761529943522588 - 0j)) < 1e-7, \
            cf.data[peak_location]
        u_peak, v_peak = cf.grid_wcs.sub([1, 2]).wcs_pix2world(
            peak_location[-2], peak_location[-1], 0)
        assert numpy.abs(u_peak) < 1e-7, u_peak
        assert numpy.abs(v_peak) < 1e-7, u_peak

        bboxes = calculate_bounding_box_convolutionfunction(cf)
        assert len(bboxes) == 201, len(bboxes)
        assert len(bboxes[0]) == 3, len(bboxes[0])
        assert bboxes[-1][0] == 200, bboxes[-1][0]

        peak_location = numpy.unravel_index(numpy.argmax(numpy.abs(cf.data)),
                                            cf.shape)
        assert peak_location == (0, 0, 100, 4, 4, 30, 30), peak_location

        cf_clipped = apply_bounding_box_convolutionfunction(
            cf, fractional_level=1e-3)
        peak_location = numpy.unravel_index(
            numpy.argmax(numpy.abs(cf_clipped.data)), cf_clipped.shape)
        assert peak_location == (0, 0, 100, 4, 4, 21, 21), peak_location
        cf_image = convert_convolutionfunction_to_image(cf_clipped)
        cf_image.data = numpy.real(cf_image.data)
        if self.persist:
            export_image_to_fits(
                cf_image,
                "%s/test_convolutionfunction_awterm_clipped_cf.fits" %
                self.dir)
    def test_fill_wterm_to_convolutionfunction(self):
        gcf, cf = create_awterm_convolutionfunction(self.image,
                                                    make_pb=None,
                                                    nw=201,
                                                    wstep=8.0,
                                                    oversampling=8,
                                                    support=60,
                                                    use_aaf=True)
        if self.persist:
            export_image_to_fits(
                gcf, "%s/test_convolutionfunction_wterm_gcf.fits" % self.dir)

        cf_image = convert_convolutionfunction_to_image(cf)
        cf_image.data = numpy.real(cf_image.data)
        if self.persist:
            export_image_to_fits(
                cf_image,
                "%s/test_convolutionfunction_wterm_cf.fits" % self.dir)

        peak_location = numpy.unravel_index(numpy.argmax(numpy.abs(cf.data)),
                                            cf.shape)
        assert peak_location == (0, 0, 100, 4, 4, 30, 30), peak_location
        assert numpy.abs(cf.data[peak_location] - (0.1870600903328245-0j)) < 1e-7, \
            cf.data[peak_location]
        u_peak, v_peak = cf.grid_wcs.sub([1, 2]).wcs_pix2world(
            peak_location[-2], peak_location[-1], 0)
        assert numpy.abs(u_peak) < 1e-7, u_peak
        assert numpy.abs(v_peak) < 1e-7, u_peak

        cf_clipped = apply_bounding_box_convolutionfunction(cf, 1e-3)
        cf_image = convert_convolutionfunction_to_image(cf_clipped)
        cf_image.data = numpy.real(cf_image.data)
        if self.persist:
            export_image_to_fits(
                cf_image,
                "%s/test_convolutionfunction_wterm_clipped_real_cf.fits" %
                self.dir)
        cf_image = convert_convolutionfunction_to_image(cf_clipped)
        cf_image.data = numpy.imag(cf_image.data)
        if self.persist:
            export_image_to_fits(
                cf_image,
                "%s/test_convolutionfunction_wterm_clipped_imag_cf.fits" %
                self.dir)

        peak_location = numpy.unravel_index(
            numpy.argmax(numpy.abs(cf_clipped.data)), cf_clipped.shape)
        assert peak_location == (0, 0, 100, 4, 4, 27, 27), peak_location

        assert numpy.abs(cf_clipped.data[peak_location] - (0.1870600903328245-0j)) < 1e-7, \
            cf_clipped.data[peak_location]
        u_peak, v_peak = cf_clipped.grid_wcs.sub([1, 2]).wcs_pix2world(
            peak_location[-2], peak_location[-1], 0)
        assert numpy.abs(u_peak) < 1e-7, u_peak
        assert numpy.abs(v_peak) < 1e-7, u_peak
    def test_fill_aterm_to_convolutionfunction_noover(self):
        make_pb = functools.partial(create_pb_generic,
                                    diameter=35.0,
                                    blockage=0.0,
                                    use_local=False)
        pb = make_pb(self.image)

        if self.persist:
            export_image_to_fits(
                pb, "%s/test_convolutionfunction_aterm_pb.fits" % self.dir)
        gcf, cf = create_awterm_convolutionfunction(self.image,
                                                    make_pb=make_pb,
                                                    nw=1,
                                                    wstep=1e-7,
                                                    oversampling=1,
                                                    support=32,
                                                    use_aaf=True)
        cf_image = convert_convolutionfunction_to_image(cf)
        cf_image.data = numpy.real(cf_image.data)
        if self.persist:
            export_image_to_fits(
                cf_image,
                "%s/test_convolutionfunction_aterm_noover_cf.fits" % self.dir)

        peak_location = numpy.unravel_index(numpy.argmax(numpy.abs(cf.data)),
                                            cf.shape)
        assert numpy.abs(cf.data[peak_location] - 0.0776153022780847 +
                         0j) < 1e-7, cf.data[peak_location]
        assert peak_location == (0, 0, 0, 0, 0, 16, 16), peak_location
        u_peak, v_peak = cf.grid_wcs.sub([1, 2]).wcs_pix2world(
            peak_location[-2], peak_location[-1], 0)
        assert numpy.abs(u_peak) < 1e-7, u_peak
        assert numpy.abs(v_peak) < 1e-7, u_peak

        if self.persist:
            export_image_to_fits(
                gcf,
                "%s/test_convolutionfunction_aterm_noover_gcf.fits" % self.dir)

        cf_clipped = apply_bounding_box_convolutionfunction(
            cf, fractional_level=0.001)
        cf_image = convert_convolutionfunction_to_image(cf_clipped)
        cf_image.data = numpy.real(cf_image.data)
        if self.persist:
            export_image_to_fits(
                cf_image,
                "%s/test_convolutionfunction_aterm_clipped_noover_cf.fits" %
                self.dir)
        peak_location = numpy.unravel_index(
            numpy.argmax(numpy.abs(cf_clipped.data)), cf_clipped.shape)
        assert peak_location == (0, 0, 0, 0, 0, 5, 5), peak_location
    def actualSetUp(self,
                    add_errors=False,
                    freqwin=3,
                    block=True,
                    dospectral=True,
                    dopol=False,
                    zerow=False,
                    makegcfcf=False):

        self.npixel = 256
        self.low = create_named_configuration('LOWBD2', rmax=750.0)
        self.freqwin = freqwin
        self.bvis_list = list()
        self.ntimes = 5
        self.cellsize = 0.0005
        # Choose the interval so that the maximum change in w is smallish
        integration_time = numpy.pi * (24 / (12 * 60))
        self.times = numpy.linspace(-integration_time * (self.ntimes // 2),
                                    integration_time * (self.ntimes // 2),
                                    self.ntimes)

        if freqwin > 1:
            self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin)
            self.channelwidth = numpy.array(
                freqwin * [self.frequency[1] - self.frequency[0]])
        else:
            self.frequency = numpy.array([1.0e8])
            self.channelwidth = numpy.array([4e7])

        if dopol:
            self.vis_pol = PolarisationFrame('linear')
            self.image_pol = PolarisationFrame('stokesIQUV')
            f = numpy.array([100.0, 20.0, -10.0, 1.0])
        else:
            self.vis_pol = PolarisationFrame('stokesI')
            self.image_pol = PolarisationFrame('stokesI')
            f = numpy.array([100.0])

        if dospectral:
            flux = numpy.array(
                [f * numpy.power(freq / 1e8, -0.7) for freq in self.frequency])
        else:
            flux = numpy.array([f])

        self.phasecentre = SkyCoord(ra=+180.0 * u.deg,
                                    dec=-60.0 * u.deg,
                                    frame='icrs',
                                    equinox='J2000')
        self.bvis_list = [
            ingest_unittest_visibility(
                self.low,
                numpy.array([self.frequency[freqwin]]),
                numpy.array([self.channelwidth[freqwin]]),
                self.times,
                self.vis_pol,
                self.phasecentre,
                block=block,
                zerow=zerow) for freqwin, _ in enumerate(self.frequency)
        ]

        self.model_list = [
            create_unittest_model(self.bvis_list[freqwin],
                                  self.image_pol,
                                  cellsize=self.cellsize,
                                  npixel=self.npixel)
            for freqwin, _ in enumerate(self.frequency)
        ]

        self.components_list = [
            create_unittest_components(self.model_list[freqwin],
                                       flux[freqwin, :][numpy.newaxis, :],
                                       single=False)
            for freqwin, _ in enumerate(self.frequency)
        ]

        self.model_list = [
            insert_skycomponent(self.model_list[freqwin],
                                self.components_list[freqwin])
            for freqwin, _ in enumerate(self.frequency)
        ]

        self.bvis_list = [
            dft_skycomponent_visibility(self.bvis_list[freqwin],
                                        self.components_list[freqwin])
            for freqwin, _ in enumerate(self.frequency)
        ]
        centre = self.freqwin // 2
        # Calculate the model convolved with a Gaussian.
        self.model = self.model_list[centre]

        self.cmodel = smooth_image(self.model)
        if self.persist:
            export_image_to_fits(self.model,
                                 '%s/test_imaging_model.fits' % self.dir)
        if self.persist:
            export_image_to_fits(self.cmodel,
                                 '%s/test_imaging_cmodel.fits' % self.dir)

        if add_errors and block:
            self.bvis_list = [
                insert_unittest_errors(self.bvis_list[i])
                for i, _ in enumerate(self.frequency)
            ]

        self.components = self.components_list[centre]

        if makegcfcf:
            self.gcfcf = [
                create_awterm_convolutionfunction(self.model,
                                                  nw=61,
                                                  wstep=16.0,
                                                  oversampling=8,
                                                  support=64,
                                                  use_aaf=True)
            ]
            self.gcfcf_clipped = [
                (self.gcfcf[0][0],
                 apply_bounding_box_convolutionfunction(self.gcfcf[0][1],
                                                        fractional_level=1e-3))
            ]

            self.gcfcf_joint = [
                create_awterm_convolutionfunction(self.model,
                                                  nw=11,
                                                  wstep=16.0,
                                                  oversampling=8,
                                                  support=64,
                                                  use_aaf=True)
            ]

        else:
            self.gcfcf = None
            self.gcfcf_clipped = None
            self.gcfcf_joint = None