def test_invert(self): uvfitsfile = rascil_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 rascil.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) if self.doplot: import matplotlib.pyplot as plt from rascil.processing_components.image.operations import show_image show_image(dirty) plt.show(block=False) if self.persist: export_image_to_fits( dirty, '%s/test_visibility_uvfits_dirty.fits' % self.dir)
def test_convert_decoalesce_zero(self): cvis = convert_blockvisibility_to_visibility(self.blockvis) assert numpy.min(cvis.frequency) == numpy.min(self.frequency) assert numpy.min(cvis.frequency) > 0.0 dvis = decoalesce_visibility(cvis) assert dvis.nvis == self.blockvis.nvis dvis = decoalesce_visibility(cvis) assert dvis.nvis == self.blockvis.nvis
def test_coalesce_decoalesce_with_iter(self): for rows in vis_timeslice_iter(self.blockvis): visslice = create_visibility_from_rows(self.blockvis, rows) cvisslice = convert_blockvisibility_to_visibility(visslice) assert numpy.min(cvisslice.frequency) == numpy.min(self.frequency) assert numpy.min(cvisslice.frequency) > 0.0 dvisslice = decoalesce_visibility(cvisslice) assert dvisslice.nvis == visslice.nvis
def test_convert_blockvisibility(self): self.vis = create_blockvisibility( self.lowcore, self.times, self.frequency, phasecentre=self.phasecentre, weight=1.0, channel_bandwidth=self.channel_bandwidth) vis = convert_blockvisibility_to_visibility(self.vis) assert vis.nvis == len(vis.time) assert numpy.unique(vis.time).size == self.vis.time.size # pylint: disable=no-member
def actualSetUp(self, freqwin=1, block=True, dopol=False): self.npixel = 512 self.low = create_named_configuration('LOWBD2', rmax=750.0) self.freqwin = freqwin self.vis = list() self.ntimes = 5 self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0 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 freqwin > 1: self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin) self.channelwidth = numpy.array( freqwin * [self.frequency[1] - self.frequency[0]]) flux = numpy.array( [f * numpy.power(freq / 1e8, -0.7) for freq in self.frequency]) else: self.frequency = numpy.array([1e8]) self.channelwidth = numpy.array([1e6]) flux = numpy.array([f]) self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000') self.bvis = ingest_unittest_visibility(self.low, self.frequency, self.channelwidth, self.times, self.vis_pol, self.phasecentre, block=block) self.vis = convert_blockvisibility_to_visibility(self.bvis) self.model = create_unittest_model(self.vis, self.image_pol, npixel=self.npixel, nchan=freqwin) self.components = create_unittest_components(self.model, flux) self.model = insert_skycomponent(self.model, self.components) self.bvis = predict_skycomponent_visibility(self.bvis, self.components)
def create_visibility_from_uvfits(fitsname, channum=None, ack=False, antnum=None): """ Minimal UVFITS to BlockVisibility converter Creates a list of BlockVisibility's, split by field and spectral window :param fitsname: File name of UVFITS file :param channum: range of channels e.g. range(17,32), default is None meaning all :param antnum: the number of antenna :return: """ from rascil.processing_components.visibility.coalesce import convert_blockvisibility_to_visibility return [convert_blockvisibility_to_visibility(v) for v in create_blockvisibility_from_uvfits(fitsname=fitsname, channum=channum, ack=ack, antnum=antnum)]
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
def create_visibility_from_ms(msname, channum=None, start_chan=None, end_chan=None, ack=False): """ Minimal MS to BlockVisibility converter The MS format is much more general than the RASCIL BlockVisibility so we cut many corners. This requires casacore to be installed. If not an exception ModuleNotFoundError is raised. Creates a list of BlockVisibility's, split by field and spectral window Reading of a subset of channels is possible using either start_chan and end_chan or channnum. Using start_chan and end_chan is preferred since it only reads the channels required. Channum is more flexible and can be used to read a random list of channels. :param msname: File name of MS :param channum: range of channels e.g. range(17,32), default is None meaning all :param start_chan: Starting channel to read :param end_chan: End channel to read :return: """ from rascil.processing_components.visibility.coalesce import convert_blockvisibility_to_visibility return [convert_blockvisibility_to_visibility(v) for v in create_blockvisibility_from_ms(msname=msname, channum=channum, start_chan=start_chan, end_chan=end_chan, ack=ack)]
def test_predict_sky_components_coalesce(self): sc = create_low_test_skycomponents_from_gleam( flux_limit=10.0, polarisation_frame=PolarisationFrame("stokesI"), frequency=self.frequency, kind='cubic', phasecentre=SkyCoord("17h20m31s", "-00d58m45s"), radius=0.1) self.config = create_named_configuration('LOWBD2-CORE') self.phasecentre = SkyCoord("17h20m31s", "-00d58m45s") sampling_time = 3.76 self.times = numpy.arange(0.0, +300 * sampling_time, sampling_time) self.vis = create_blockvisibility( self.config, self.times, self.frequency, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame('stokesI'), channel_bandwidth=self.channel_bandwidth) self.vis = dft_skycomponent_visibility(self.vis, sc) cvt = convert_blockvisibility_to_visibility(self.vis) assert cvt.cindex is not None
def actualSetUp(self, freqwin=1, block=True, dopol=False, zerow=False): self.npixel = 1024 self.low = create_named_configuration('LOWBD2', rmax=550.0) self.freqwin = freqwin self.blockvis_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]) self.phasecentre = SkyCoord(ra=+0.0 * u.deg, dec=-40.0 * u.deg, frame='icrs', equinox='J2000') self.blockvis_list = [ ingest_unittest_visibility(self.low, [self.frequency[freqwin]], [self.channelwidth[freqwin]], self.times, self.vis_pol, self.phasecentre, block=block, zerow=zerow) for freqwin, _ in enumerate(self.frequency) ] self.vis_list = [ convert_blockvisibility_to_visibility(bv) for bv in self.blockvis_list ] self.skymodel_list = [ create_low_test_skymodel_from_gleam( npixel=self.npixel, cellsize=self.cellsize, frequency=[self.frequency[f]], phasecentre=self.phasecentre, polarisation_frame=PolarisationFrame("stokesI"), flux_limit=0.6, flux_threshold=1.0, flux_max=5.0) for f, freq in enumerate(self.frequency) ] assert isinstance(self.skymodel_list[0].image, Image), self.skymodel_list[0].image assert isinstance(self.skymodel_list[0].components[0], Skycomponent), self.skymodel_list[0].components[0] assert len(self.skymodel_list[0].components) == 35, len( self.skymodel_list[0].components) self.skymodel_list = expand_skymodel_by_skycomponents( self.skymodel_list[0]) assert len(self.skymodel_list) == 36, len(self.skymodel_list) assert numpy.max(numpy.abs( self.skymodel_list[-1].image.data)) > 0.0, "Image is empty" self.vis_list = [ copy_visibility(self.vis_list[0], zero=True) for i, _ in enumerate(self.skymodel_list) ]
flux = numpy.array([f]) phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-45.0 * u.deg, frame='icrs', equinox='J2000') blockvis = ingest_unittest_visibility(low, frequency, channelwidth, times, blockvis_pol, phasecentre, block=block, zerow=zerow) vis = convert_blockvisibility_to_visibility(blockvis) model = create_unittest_model(vis, image_pol, npixel=npixel, nchan=freqwin) components = create_unittest_components(model, flux) model = insert_skycomponent(model, components) blockvis = predict_skycomponent_visibility(blockvis, components) #blockvis = dft_skycomponent_visibility(blockvis, components) blockvis1 = copy_visibility(blockvis) vis1 = convert_blockvisibility_to_visibility(blockvis1) # Calculate the model convolved with a Gaussian. cmodel = smooth_image(model)
def test_export_ms(self): if run_ms_tests == False: return msoutfile = rascil_path("test_results/test_export_ms_ASKAP_output.ms") from astropy.coordinates import SkyCoord from astropy import units as u from rascil.processing_components.image.operations import show_image, export_image_to_fits from rascil.processing_components.simulation import create_named_configuration from rascil.processing_components.simulation import create_test_image from rascil.processing_components.imaging.base import create_image_from_visibility from rascil.processing_components.imaging.base import advise_wide_field from rascil.workflows.serial.imaging.imaging_serial import invert_list_serial_workflow, predict_list_serial_workflow from rascil.data_models.polarisation import PolarisationFrame lowr3 = create_named_configuration('LOWBD2', rmax=750.0) times = numpy.zeros([1]) frequency = numpy.array([1e8]) channelbandwidth = numpy.array([1e6]) phasecentre = SkyCoord(ra=+15.0 * u.deg, dec=-45.0 * u.deg, frame='icrs', equinox='J2000') bvis = create_blockvisibility( lowr3, times, frequency, phasecentre=phasecentre, weight=1.0, polarisation_frame=PolarisationFrame('stokesI'), channel_bandwidth=channelbandwidth) vt = convert_blockvisibility_to_visibility(bvis) advice = advise_wide_field(vt, guard_band_image=3.0, delA=0.1, facets=1, wprojection_planes=1, oversampling_synthesised_beam=4.0) cellsize = advice['cellsize'] m31image = create_test_image(frequency=frequency, cellsize=cellsize) nchan, npol, ny, nx = m31image.data.shape m31image.wcs.wcs.crval[0] = vt.phasecentre.ra.deg m31image.wcs.wcs.crval[1] = vt.phasecentre.dec.deg m31image.wcs.wcs.crpix[0] = float(nx // 2) m31image.wcs.wcs.crpix[1] = float(ny // 2) vt = predict_list_serial_workflow([vt], [m31image], context='2d')[0] # uvdist = numpy.sqrt(vt.data['uvw'][:, 0] ** 2 + vt.data['uvw'][:, 1] ** 2) # # model = create_image_from_visibility(vt, cellsize=cellsize, npixel=512) # dirty, sumwt = invert_list_serial_workflow([vt], [model], context='2d')[0] # psf, sumwt = invert_list_serial_workflow([vt], [model], context='2d', dopsf=True)[0] # # 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)) # results_dir="/Users/f.wang" # export_image_to_fits(dirty, '%s/imaging_dirty.fits' % (results_dir)) # export_image_to_fits(psf, '%s/imaging_psf.fits' % (results_dir)) v = convert_visibility_to_blockvisibility(vt) vis_list = [] vis_list.append(v) export_blockvisibility_to_ms(msoutfile, vis_list, source_name='M31')
bvis = vis[0] from rascil.processing_components.flagging.operations import flagging_blockvisibility, flagging_blockvisibility_with_bl # bvis = flagging_blockvisibility(vis[0], antenna=[8,9,10,11,27]) # baseline = [ [4,0],[4,1],[5,4],[21,4],[24,4],[25,4],[26,4],[27,4],[28,4],[29,4],[30,4],[31,4],[32,4],[36,4],[38,4],[39,4]] # baseline.append([[17,4],[17,13],[19,17],[26,17],[27,17],[28,17],[29,17],[30,17],[31,17],[39,17]]) # bvis = flagging_blockvisibility_with_bl(vis[0],baseline) # # vis_list = [] # vis_list.append(bvis) # # Output results # export_file_name = muser_output_path('ms_flag_test') + '.ms' # # export_blockvisibility_to_ms(export_file_name, vis_list, source_name='SUN') # Flagging vt = convert_blockvisibility_to_visibility(bvis) advice = advise_wide_field(vt, guard_band_image=3.0, delA=0.1, oversampling_synthesised_beam=4.0) cellsize = advice['cellsize'] 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='b') # plt.xlim([-400.0, 400.0]) # plt.ylim([-400.0, 400.0]) plt.show() # To check that we got the prediction right, plot the amplitude of the visibility.
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_2d, model=None, components=None, phase_error=0.0, amplitude_error=0.0, sleep=0.0, **kwargs): """ 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 = convert_blockvisibility_to_visibility(bvis) vis = predict(vis, model, **kwargs) bvis = convert_visibility_to_blockvisibility(vis) if components is not None: vis = dft_skycomponent_visibility(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) bvis = apply_gaintable(bvis, gt) import time time.sleep(sleep) yield bvis
def actualSetUp(self, add_errors=False, nfreqwin=7, dospectral=True, dopol=False, zerow=True): self.npixel = 512 self.low = create_named_configuration('LOWBD2', rmax=750.0) self.freqwin = nfreqwin self.vis_list = list() self.ntimes = 5 self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0 self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin) if self.freqwin > 1: self.channelwidth = numpy.array(self.freqwin * [self.frequency[1] - self.frequency[0]]) else: self.channelwidth = numpy.array([1e6]) if dopol: self.vis_pol = PolarisationFrame('linear') self.image_pol = PolarisationFrame('stokesIQUV') f = numpy.array([100.0, 20.0, 0.0, 0.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.blockvis_list = [ingest_unittest_visibility(self.low, [self.frequency[i]], [self.channelwidth[i]], self.times, self.vis_pol, self.phasecentre, block=True, zerow=zerow) for i in range(nfreqwin)] self.vis_list = [convert_blockvisibility_to_visibility(bv) for bv in self.blockvis_list] self.model_imagelist = [ create_unittest_model(self.vis_list[i], self.image_pol, npixel=self.npixel, cellsize=0.0005) for i in range(nfreqwin)] self.components_list = [create_unittest_components(self.model_imagelist[freqwin], flux[freqwin, :][numpy.newaxis, :]) for freqwin, m in enumerate(self.model_imagelist)] self.blockvis_list = [ dft_skycomponent_visibility(self.blockvis_list[freqwin], self.components_list[freqwin]) for freqwin, _ in enumerate(self.blockvis_list)] self.model_imagelist = [insert_skycomponent(self.model_imagelist[freqwin], self.components_list[freqwin]) for freqwin in range(nfreqwin)] model = self.model_imagelist[0] self.cmodel = smooth_image(model) if self.persist: export_image_to_fits(model, '%s/test_imaging_serial_model.fits' % self.dir) export_image_to_fits(self.cmodel, '%s/test_imaging_serial_cmodel.fits' % self.dir) if add_errors: gt = create_gaintable_from_blockvisibility(self.blockvis_list[0]) gt = simulate_gaintable(gt, phase_error=0.1, amplitude_error=0.0, smooth_channels=1, leakage=0.0) self.blockvis_list = [apply_gaintable(self.blockvis_list[i], gt) for i in range(self.freqwin)] self.vis_list = [convert_blockvisibility_to_visibility(bv) for bv in self.blockvis_list] self.model_imagelist = [ create_unittest_model(self.vis_list[i], self.image_pol, npixel=self.npixel, cellsize=0.0005) for i in range(nfreqwin)]
def actualSetUp(self, freqwin=1, block=True, dospectral=True, dopol=False, zerow=False, do_shift=False): self.npixel = 512 self.low = create_named_configuration('LOWBD2', rmax=750.0) self.freqwin = freqwin self.blockvis = list() self.ntimes = 5 self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0 if freqwin > 1: self.frequency = numpy.linspace(0.99e8, 1.01e8, self.freqwin) self.channelwidth = numpy.array( freqwin * [self.frequency[1] - self.frequency[0]]) else: self.frequency = numpy.array([1e8]) self.channelwidth = numpy.array([1e6]) if dopol: self.blockvis_pol = PolarisationFrame('linear') self.image_pol = PolarisationFrame('stokesIQUV') f = numpy.array([100.0, 20.0, -10.0, 1.0]) else: self.blockvis_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=-45.0 * u.deg, frame='icrs', equinox='J2000') self.blockvis = ingest_unittest_visibility(self.low, self.frequency, self.channelwidth, self.times, self.blockvis_pol, self.phasecentre, block=block, zerow=zerow) self.vis = convert_blockvisibility_to_visibility(self.blockvis) self.model = create_unittest_model(self.vis, self.image_pol, npixel=self.npixel, nchan=freqwin) self.components = create_unittest_components(self.model, flux) self.model = insert_skycomponent(self.model, self.components) self.blockvis = predict_skycomponent_visibility( self.blockvis, self.components) # Calculate the model convolved with a Gaussian. self.cmodel = smooth_image(self.model) if self.persist: export_image_to_fits(self.model, '%s/test_imaging_ng_model.fits' % self.dir) if self.persist: export_image_to_fits(self.cmodel, '%s/test_imaging_ng_cmodel.fits' % self.dir)