示例#1
0
    def test_writer_errors(self):
        """Test that common measurement set error conditions are caught."""

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

        # Get some data
        data = self._init_data()

        for i in range(4):
            # Start the file
            ms = measurementset.Ms(testFile, ref_time=testTime, overwrite=True)
            if i != 0:
                ms.set_stokes(['xx'])
            if i != 1:
                ms.set_frequency(data['freq'])
            if i != 2:
                ms.set_geometry(data['site'], data['antennas'])
            if i != 3:
                ms.add_data_set(testTime, 6.0, data['bl'], data['vis'])
            self.assertRaises(RuntimeError, ms.write)
示例#2
0
    def test_write_tables(self):
        """Test if the MeasurementSet writer writes all of the tables."""

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

        # Get some data
        data = self.__initData()

        # Start the table
        tbl = measurementset.Ms(testFile, ref_time=testTime)
        tbl.set_stokes(['xx'])
        tbl.set_frequency(data['freq'])
        tbl.set_geometry(data['site'], data['antennas'])
        tbl.add_data_set(testTime, 6.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)))
示例#3
0
    def test_multi_if(self):
        """writing more than one spectral window to a MeasurementSet."""

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

        # Get some data
        data = self._init_data()

        # Start the file
        fits = measurementset.Ms(testFile, ref_time=testTime)
        fits.set_stokes(['xx'])
        fits.set_frequency(data['freq'])
        fits.set_frequency(data['freq'] + 10e6)
        fits.set_geometry(data['site'], data['antennas'])
        fits.add_data_set(
            unix_to_taimjd(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')
        mapper = [int(m[3:], 10) for m in mapper]

        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()
示例#4
0
    def test_main_table(self):
        """Test the primary data table."""

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

        # Get some data
        data = self._init_data()

        # Start the file
        fits = measurementset.Ms(testFile, ref_time=testTime)
        fits.set_stokes(['xx'])
        fits.set_frequency(data['freq'])
        fits.set_geometry(data['site'], data['antennas'])
        fits.add_data_set(unix_to_taimjd(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')
        mapper = [int(m[3:], 10) for m in mapper]

        # 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()
示例#5
0
def main(args):
    # Setup the site information
    station = stations.lwasv
    ants = station.antennas
    nAnt = len([a for a in ants if a.pol == 0])
    
    phase_freq_range = [0, 0]
    for filename in args.filename:
        # Open the file and get ready to go
        idf = CORFile(filename)
        nBL = idf.get_info('nbaseline')
        nchan = idf.get_info('nchan')
        tInt = idf.get_info('tint')
        nFpO = nBL * nchan / 72
        nInts = idf.get_info('nframe') / nFpO
        
        jd = astro.unix_to_utcjd(idf.get_info('start_time'))
        date = idf.get_info('start_time').datetime
        central_freq = idf.get_info('freq1')
        central_freq = central_freq[len(central_freq)/2]
        
        print("Data type:  %s" % type(idf))
        print("Samples per observations: %i" % (nFpO,))
        print("Integration Time: %.3f s" % tInt)
        print("Tuning frequency: %.3f Hz" % central_freq)
        print("Captures in file: %i (%.1f s)" % (nInts, nInts*tInt))
        print("==")
        print("Station: %s" % station.name)
        print("Date observed: %s" % date)
        print("Julian day: %.5f" % jd)
        print(" ")
        
        # Offset into the file
        offset = idf.offset(args.skip)
        if offset != 0.0:
            print("Skipped %.3f s into the file" % offset)
            
        # Open the file and go
        nFiles = int(args.duration /  tInt)
        if nFiles == 0:
            nFiles = numpy.inf
            
        fileCount = 0
        while fileCount < nFiles:
            try:
                tInt, tStart, data = idf.read(tInt)
            except Exception as e:
                print("ERROR: %s" % str(e))
                break
                
            freqs = idf.get_info('freq1')
            beginJD = astro.unix_to_utcjd( tStart )
            beginTime = datetime.utcfromtimestamp( tStart )
            
            if freqs[0] != phase_freq_range[0] or freqs[-1] != phase_freq_range[1]:
                print("Updating phasing for %.3f to %.3f MHz" % (freqs[0]/1e6, freqs[-1]/1e6))
                phase_freq_range[0] = freqs[ 0]
                phase_freq_range[1] = freqs[-1]
                
                k = 0
                phase = numpy.zeros((nBL, nchan, 2, 2), dtype=numpy.complex64)
                gaix = [a.cable.gain(freqs) for a in ants if a.pol == 0]
                gaiy = [a.cable.gain(freqs) for a in ants if a.pol == 1]
                dlyx = [a.cable.delay(freqs) - a.stand.z / speedOfLight for a in ants if a.pol == 0]
                dlyy = [a.cable.delay(freqs) - a.stand.z / speedOfLight for a in ants if a.pol == 1]
                for i in xrange(nAnt):
                    for j in xrange(i, nAnt):
                        phase[k,:,0,0] = numpy.exp(2j*numpy.pi*freqs*(dlyx[i] - dlyx[j])) \
                                            / numpy.sqrt(gaix[i]*gaix[j])
                        phase[k,:,0,1] = numpy.exp(2j*numpy.pi*freqs*(dlyx[i] - dlyy[j])) \
                                            / numpy.sqrt(gaix[i]*gaiy[j])
                        phase[k,:,1,0] = numpy.exp(2j*numpy.pi*freqs*(dlyy[i] - dlyx[j])) \
                                            / numpy.sqrt(gaiy[i]*gaix[j])
                        phase[k,:,1,1] = numpy.exp(2j*numpy.pi*freqs*(dlyy[i] - dlyy[j])) \
                                            / numpy.sqrt(gaiy[i]*gaiy[j])
                        
                        k += 1
                        
            for i in xrange(data.shape[-1]):
                data[...,i] *= phase
                
            # Convert to a dataDict
            try:
                blList  # pylint: disable=used-before-assignment
            except NameError:
                blList = uvutils.get_baselines(ants[0::2], include_auto=True)
                
            if args.output is None:
                outname = os.path.basename(filename)
                outname = os.path.splitext(outname)[0]
                outname = "%s_%i_%s.ms" % (outname, int(beginJD-astro.MJD_OFFSET), beginTime.strftime("%H_%M_%S"))
            else:
                base, ext = os.path.splitext(args.output)
                if ext == '':
                    ext = '.ms'
                outname = "%s_%i_%s%s" % (base, int(beginJD-astro.MJD_OFFSET), beginTime.strftime("%H_%M_%S"), ext)
                
            fits = measurementset.Ms(outname, ref_time=tStart)
            fits.set_stokes(['xx', 'xy', 'yx', 'yy'])
            fits.set_frequency(freqs)
            fits.set_geometry(station, ants[0::2])
            
            obsTime = astro.unix_to_taimjd(tStart)
            fits.add_data_set(obsTime, tInt, blList, data[:,:,0,0,0], pol='xx')
            fits.add_data_set(obsTime, tInt, blList, data[:,:,0,1,0], pol='xy')
            fits.add_data_set(obsTime, tInt, blList, data[:,:,1,0,0], pol='yx')
            fits.add_data_set(obsTime, tInt, blList, data[:,:,1,1,0], pol='yy')
            fits.write()
            fits.close()
            fileCount += 1
            
        idf.close()