Beispiel #1
0
    def test_write_tables_wgs84(self):
        """Test if the MeasurementSet writer writes all of the tables."""
        if run_ms_tests==False:
            return

        testTime = time.time()
        testFile = os.path.join(self.testPath, 'ms-test-WGS.ms')

        # Get some data
        data = self.__initData_WGS84()

        # Start the table
        tbl = msv2.Ms(testFile, ref_time=testTime, frame='wgs84')
        tbl.set_stokes(['xx'])
        tbl.set_frequency(data['freq'],data['channel_width'])
        tbl.set_geometry(data['site'], data['antennas'])
        tbl.add_data_set(testTime, 2.0, data['bl'], data['vis'])
        tbl.write()

        # Make sure everyone is there
        self.assertTrue(os.path.exists(testFile))
        for tbl in ('ANTENNA', 'DATA_DESCRIPTION', 'FEED', 'FIELD', 'FLAG_CMD', 'HISTORY',
                    'OBSERVATION', 'POINTING', 'POLARIZATION', 'PROCESSOR', 'SOURCE',
                    'SPECTRAL_WINDOW', 'STATE'):
            self.assertTrue(os.path.exists(os.path.join(testFile, tbl)))
Beispiel #2
0
def export_blockvisibility_to_ms(msname, vis_list, source_name=None, ack=False):
    """ Minimal BlockVisibility to MS 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.

    Write a list of BlockVisibility's to a MS file, split by field and spectral window

    :param msname: File name of MS
    :param vislist: BlockVisibility
    :return:
    """
    try:
        import casacore.tables.tableutil as pt
        from casacore.tables import (makescacoldesc, makearrcoldesc, table, maketabdesc, tableexists, tableiswritable,
                             tableinfo, tablefromascii, tabledelete, makecoldesc, msconcat, removeDerivedMSCal,
                             taql, tablerename, tablecopy, tablecolumn, addDerivedMSCal, removeImagingColumns,
                             addImagingColumns, required_ms_desc, tabledefinehypercolumn, default_ms, makedminfo,
                             default_ms_subtable)
        from rascil.processing_components.visibility.msv2fund import Antenna, Stand
    except ModuleNotFoundError:
        raise ModuleNotFoundError("casacore is not installed")

    try:
        from rascil.processing_components.visibility import msv2
    except ModuleNotFoundError:
        raise ModuleNotFoundError("cannot import msv2")

    # log.debug("create_blockvisibility_from_ms: %s" % str(tab.info()))
    # Start the table
    tbl = msv2.Ms(msname, ref_time=0, source_name= source_name, if_delete=True)
    if source_name is None:
        source_name = 'RASCIL'
    for vis in vis_list:
        # Check polarisition
        npol = vis.npol
        nchan = vis.nchan
        nants = vis.nants
        if vis.polarisation_frame.type == 'linear':
            polarization = ['XX','XY','YX','YY']
        elif vis.polarisation_frame.type == 'stokesI':
            polarization = ['XX']
        elif vis.polarisation_frame.type == 'circular':
            polarization = ['RR','RL','LR','LL']
        elif vis.polarisation_frame.type == 'stokesIQUV':
            polarization = ['I','Q','U','V']
        # Current RASCIL supports I
        tbl.set_stokes(polarization)
        tbl.set_frequency(vis.frequency,vis.channel_bandwidth)
        n_ant = len(vis.configuration.xyz)

        antennas = []
        names = vis.configuration.names
        mount = vis.configuration.mount
        diameter = vis.configuration.diameter
        xyz = vis.configuration.xyz
        for i in range(len(names)):
            antennas.append(Antenna(i, Stand(names[i], xyz[i, 0], xyz[i, 1], xyz[i, 2])))

        # Set baselines and data
        blList = []

        antennas2 = antennas

        for i in range(0, n_ant - 1):
            for j in range(i + 1, n_ant):
                blList.append((antennas[i], antennas2[j]))

        tbl.set_geometry(vis.configuration, antennas)
        nbaseline = len(blList)
        ntimes = len(vis.data['time'])

        ms_vis = numpy.zeros([ntimes, nbaseline, nchan, npol]).astype('complex')
        ms_uvw = numpy.zeros([ntimes, nbaseline, 3])
        # bv_vis = numpy.zeros([ntimes, nants, nants, nchan, npol]).astype('complex')
        # bv_weight = numpy.zeros([ntimes, nants, nants, nchan, npol])
        # bv_imaging_weight = numpy.zeros([ntimes, nants, nants, nchan, npol])
        # bv_uvw = numpy.zeros([ntimes, nants, nants, 3])
        time = vis.data['time']
        int_time = vis.data['integration_time']
        bv_vis = vis.data['vis']
        bv_uvw = vis.data['uvw']

        # bv_antenna1 = vis.data['antenna1']
        # bv_antenna2 = vis.data['antenna2']

        time_last = time[0]
        time_index = 0
        for row,_ in enumerate(time):
            # MS has shape [row, npol, nchan]
            # BV has shape [ntimes, nants, nants, nchan, npol]
            bl = 0
            for i in range(0, n_ant - 1):
                for j in range(i + 1, n_ant):
                    ms_vis[row, bl,...] = bv_vis[row, j, i, ...]
                    # bv_weight[time_index, antenna2[row], antenna1[row], :, ...] = ms_weight[row, numpy.newaxis, ...]
                    # bv_imaging_weight[time_index, antenna2[row], antenna1[row], :, ...] = ms_weight[row, numpy.newaxis, ...]
                    ms_uvw[row,bl,:] = bv_uvw[row, j, i, :]
                    bl += 1

        # ms_vis = numpy.zeros([ntimes*vis.nants*vis.nants, vis.nchan, vis.npol]).astype('complex')
        # ms_uvw = vis.uvw.reshape(ntimes,-1,3)
        for ntime, time in enumerate(vis.data['time']):
            for ipol, pol in enumerate(polarization):
                if int_time[ntime] is not None:
                    tbl.add_data_set(time, int_time[ntime], blList, ms_vis[ntime, ..., ipol], pol=pol, source=source_name,
                                     phasecentre=vis.phasecentre, uvw=ms_uvw[ntime, :, :])
                else:
                    tbl.add_data_set(time, 0, blList, ms_vis[ntime, ..., ipol], pol=pol,
                                     source=source_name, phasecentre = vis.phasecentre, uvw=ms_uvw[ntime, :, :])
    tbl.write()
Beispiel #3
0
    def test_multi_if(self):
        """writing more than one spectral window to a MeasurementSet."""
        if run_ms_tests==False:
            return
        testTime = time.time()
        testFile = os.path.join(self.testPath, 'ms-test-MultiIF.ms')

        # Get some data
        data = self.__initData()

        # Start the file
        fits = msv2.Ms(testFile, ref_time=testTime)
        fits.set_stokes(['xx'])
        fits.set_frequency(data['freq'],data['channel_width'])
        fits.set_frequency(data['freq']+10e6,data['channel_width'])

        fits.set_geometry(data['site'], data['antennas'])
        fits.add_data_set(testTime, 6.0, data['bl'],
                          numpy.concatenate([data['vis'], 10*data['vis']], axis=1))
        fits.write()

        # Open the table and examine
        ms = casacore.tables.table(testFile, ack=False)
        uvw  = ms.getcol('UVW')
        ant1 = ms.getcol('ANTENNA1')
        ant2 = ms.getcol('ANTENNA2')
        ddsc = ms.getcol('DATA_DESC_ID')
        vis  = ms.getcol('DATA')

        ms2 = casacore.tables.table(os.path.join(testFile, 'ANTENNA'), ack=False)
        mapper = ms2.getcol('NAME')

        ms3 = casacore.tables.table(os.path.join(testFile, 'DATA_DESCRIPTION'), ack=False)
        spw = [i for i in ms3.getcol('SPECTRAL_WINDOW_ID')]

        # Correct number of visibilities
        self.assertEqual(uvw.shape[0], 2*data['vis'].shape[0])
        self.assertEqual(vis.shape[0], 2*data['vis'].shape[0])

        # Correct number of uvw coordinates
        self.assertEqual(uvw.shape[1], 3)

        # Correct number of frequencies
        self.assertEqual(vis.shape[1], data['freq'].size)

        # Correct values
        for row in range(uvw.shape[0]):
            stand1 = ant1[row]
            stand2 = ant2[row]
            descid = ddsc[row]
            visData = vis[row,:,0]

            # Find out which visibility set in the random data corresponds to the
            # current visibility
            i = 0
            for a1,a2 in data['bl']:
                if a1.stand.id == mapper[stand1] and a2.stand.id == mapper[stand2]:
                    break
                else:
                    i = i + 1

            # Find out which spectral window this corresponds to
            if spw[descid] == 0:
                compData = data['vis']
            else:
                compData = 10*data['vis']

            # Run the comparison
            for vd, sd in zip(visData, compData[i,:]):
                self.assertAlmostEqual(vd, sd, 8)

        ms.close()
        ms2.close()
        ms3.close()
Beispiel #4
0
    def test_main_table(self):
        """Test the primary data table."""
        if run_ms_tests==False:
            return

        testTime = time.time()
        testFile = os.path.join(self.testPath, 'ms-test-UV.ms')

        # Get some data
        data = self.__initData()

        # Start the file
        fits = msv2.Ms(testFile, ref_time=testTime)
        fits.set_stokes(['xx'])
        fits.set_frequency(data['freq'],data['channel_width'])
        fits.set_geometry(data['site'], data['antennas'])
        fits.add_data_set(testTime, 6.0, data['bl'], data['vis'])
        fits.write()

        # Open the table and examine
        ms = casacore.tables.table(testFile, ack=False)
        uvw  = ms.getcol('UVW')
        ant1 = ms.getcol('ANTENNA1')
        ant2 = ms.getcol('ANTENNA2')
        vis  = ms.getcol('DATA')

        ms2 = casacore.tables.table(os.path.join(testFile, 'ANTENNA'), ack=False)
        mapper = ms2.getcol('NAME')

        # Correct number of visibilities
        self.assertEqual(uvw.shape[0], data['vis'].shape[0])
        self.assertEqual(vis.shape[0], data['vis'].shape[0])

        # Correct number of uvw coordinates
        self.assertEqual(uvw.shape[1], 3)

        # Correct number of frequencies
        self.assertEqual(vis.shape[1], data['freq'].size)

        # Correct values
        for row in range(uvw.shape[0]):
            stand1 = ant1[row]
            stand2 = ant2[row]
            visData = vis[row,:,0]

            # Find out which visibility set in the random data corresponds to the
            # current visibility
            i = 0
            for a1,a2 in data['bl']:
                if a1.stand.id == mapper[stand1] and a2.stand.id == mapper[stand2]:
                    break
                else:
                    i = i + 1

            # Run the comparison
            for vd, sd in zip(visData, data['vis'][i,:]):
                self.assertAlmostEqual(vd, sd, 8)
            i = i + 1

        ms.close()
        ms2.close()