Esempio n. 1
0
    def test_gridding(self):
        """Test building a image from a visibility data set."""

        for filename, type in zip((idiFile, idiAltFile, uvFile),
                                  ('FITS-IDI', 'Alt. FITS-IDI', 'UVFITS')):
            with self.subTest(filetype=type):
                # Open the file
                idi = utils.CorrelatedData(filename)

                # Build the image
                ds = idi.get_data_set(1)
                junk = utils.build_gridded_image(ds, verbose=False)

                # Error checking
                self.assertRaises(RuntimeError,
                                  utils.build_gridded_image,
                                  ds,
                                  pol='XY')

                #
                # VisibilityData test
                #

                ds2 = VisibilityData()
                ds2.append(ds)
                junk = utils.build_gridded_image(ds, verbose=False)

                idi.close()
Esempio n. 2
0
    def test_image_coordinates(self):
        """Test getting per-pixel image coordinates."""

        for filename, type in zip((idiFile, idiAltFile, uvFile),
                                  ('FITS-IDI', 'Alt. FITS-IDI', 'UVFITS')):
            with self.subTest(filetype=type):
                uv = utils.CorrelatedData(filename)

                # Build the image
                ds = uv.get_data_set(1)
                junk = utils.build_gridded_image(ds, verbose=False)

                radec = utils.get_image_radec(junk, uv.get_antennaarray())
                azalt = utils.get_image_azalt(junk, uv.get_antennaarray())

                uv.close()
Esempio n. 3
0
    def test_plotting(self):
        """Test drawing an image."""

        # Setup
        antennas = lwa1.antennas[0:20]
        freqs = numpy.arange(30e6, 50e6, 1e6)
        aa = vis.build_sim_array(lwa1, antennas, freqs)

        # Build the data dictionary
        out = vis.build_sim_data(aa, vis.SOURCES, jd=2458962.16965)

        # Build an image
        img = utils.build_gridded_image(out)

        # Plot
        fig = plt.figure()
        ax = fig.gca()
        utils.plot_gridded_image(ax, img)
Esempio n. 4
0
    def test_clean_leastsq(self):
        """Test CLEANing using least squares in the image plane"""

        # Setup
        antennas = lwa1.antennas[0:20]
        freqs = numpy.arange(30e6, 50e6, 1e6)
        aa = vis.build_sim_array(lwa1, antennas, freqs)

        # Build the data dictionary
        out = vis.build_sim_data(aa, vis.SOURCES, jd=2458962.16965)

        with lsl.testing.SilentVerbose():
            # Build an image
            img = utils.build_gridded_image(out)

            # CLEAN
            deconv.lsq(aa,
                       out,
                       img,
                       max_iter=2,
                       verbose=False,
                       plot=run_plotting_tests)
Esempio n. 5
0
    def test_plotting_graticules(self):
        """Test adding a graticule to an image."""

        # Setup
        antennas = lwa1.antennas[0:20]
        freqs = numpy.arange(30e6, 50e6, 1e6)
        aa = vis.build_sim_array(lwa1, antennas, freqs)

        # Build the data dictionary
        out = vis.build_sim_data(aa, vis.SOURCES, jd=2458962.16965)

        # Build an image
        img = utils.build_gridded_image(out)

        # Plot
        fig = plt.figure()
        ax = fig.gca()
        utils.plot_gridded_image(ax, img)
        with self.subTest(type='RA/Dec.'):
            overlay.graticule_radec(ax, aa)
        with self.subTest(type='az/alt'):
            overlay.graticule_azalt(ax, aa)
        del fig
Esempio n. 6
0
def grid_visibilities(bl,
                      freqs,
                      vis,
                      tx_freq,
                      station,
                      valid_ants=None,
                      size=80,
                      res=0.5,
                      wres=0.10,
                      use_pol=0,
                      jd=None):
    '''
    Resamples the baseline-sampled visibilities on to a regular grid. 

    arguments:
    bl = pairs of antenna objects representing baselines (list)
    freqs = frequency channels for which we have correlations (list)
    vis = visibility samples corresponding to the baselines (numpy array)
    tx_freq = the frequency of the signal we want to locate
    valid_ants = which antennas we actually want to use (list)
    station = lsl station object - usually stations.lwasv
    according to LSL docstring:
        size = number of wavelengths which the UV matrix spans (this 
        determines the image resolution).
        res = resolution of the UV matrix (determines image field of view).
        wres: the gridding resolution of sqrt(w) when projecting to w=0.

    use_pol = which polarization to use (only 0 is supported right now)
    returns:
    gridded_image
    '''
    # In order to do the gridding, we need to build a VisibilityDataSet using
    # lsl.imaging.data.VisibilityDataSet. We have to build a bunch of stuff to
    # pass to its constructor.

    if valid_ants is None:
        valid_ants, n_baselines = select_antennas(station.antennas, use_pol)

    # we only want the bin nearest to our frequency
    target_bin = np.argmin([abs(tx_freq - f) for f in freqs])

    # Build antenna array
    freqs = np.array(freqs)
    antenna_array = simVis.build_sim_array(station,
                                           valid_ants,
                                           freqs / 1e9,
                                           jd=jd,
                                           force_flat=True)

    uvw = np.empty((len(bl), 3, len(freqs)))

    for i, f in enumerate(freqs):
        # wavelength = 3e8/f # TODO this should be fixed. What is currently happening is not true. Well it is, but only if you're looking for a specific transmitter frequency. Which I guess we are. I just mean it's not generalized.
        wavelength = 3e8 / tx_freq
        uvw[:, :, i] = uvw_from_antenna_pairs(bl, wavelength=wavelength)

    dataSet = VisibilityDataSet(jd=jd,
                                freq=freqs,
                                baselines=bl,
                                uvw=uvw,
                                antennarray=antenna_array)
    if use_pol == 0:
        pol_string = 'XX'
    else:
        raise RuntimeError("Only pol. XX supported right now.")
    polDataSet = PolarizationDataSet(pol_string, data=vis)
    dataSet.append(polDataSet)

    # Use lsl.imaging.utils.build_gridded_image (takes a VisibilityDataSet)
    gridded_image = build_gridded_image(dataSet,
                                        pol=pol_string,
                                        chan=target_bin,
                                        size=size,
                                        res=res,
                                        wres=wres)

    return gridded_image
def main(args):

    h5fi = h5py.File(args.input_file, 'r')
    h5fo = h5py.File(args.output_file,'w')

    # Copy over important attributes
    for key in h5fi.attrs.keys():
        h5fo.attrs[key]=h5fi.attrs[key]
    
    # Copy over important datasets
    for key in h5fi.keys():
        temp_arr = h5fi[key]
        h5fo.create_dataset('vis_{}'.format(key),data=temp_arr)
    h5fo.attrs['grid_size']=args.size
    h5fo.attrs['grid_res']=args.res
    h5fo.attrs['grid_wres']=args.wres
    h5fo.create_dataset('l_est', (len(h5fo['vis_l_est']),))
    h5fo.create_dataset('m_est', (len(h5fo['vis_l_est']),))
    h5fo.create_dataset('extent', (len(h5fo['vis_l_est']),4))
    h5fo.create_dataset('elevation', (len(h5fo['vis_l_est']),))
    h5fo.create_dataset('azimuth', (len(h5fo['vis_l_est']),))
    h5fo.create_dataset('height', (len(h5fo['vis_l_est']),))

    
    h5fi.close() # done with input data now

    ## Begin doing stuff
    antennas = station.antennas
    valid_ants, n_baselines = select_antennas(antennas, h5fo.attrs['use_pol'], exclude=[256]) # to exclude outrigger

    tx_coords = h5fo.attrs['tx_coordinates']
    rx_coords = [station.lat * 180/np.pi, station.lon * 180/np.pi]

    ## Build freqs (same for every 'integration')
    freqs = np.empty((h5fo.attrs['fft_len'],),dtype=np.float64)
    #! Need to think of intelligent way of doing this.
    #! target_bin will probably not matter since all vis is the same
    freqs5 =   [5284999.9897182, 5291249.9897182, 5297499.9897182, 5303749.9897182,
                5309999.9897182, 5316249.9897182, 5322499.9897182, 5328749.9897182,
                5334999.9897182, 5341249.9897182, 5347499.9897182, 5353749.9897182,
                5359999.9897182, 5366249.9897182, 5372499.9897182, 5378749.9897182]
    for i in range(len(freqs)):
        freqs[i]=freqs5[i]

    ## Build bl (same for every 'integration')
    pol_string = 'xx' if h5fo.attrs['use_pol'] == 0 else 'yy'
    pol1, pol2 = pol_to_pols(pol_string)
    antennas1 = [a for a in valid_ants if a.pol == pol1]
    antennas2 = [a for a in valid_ants if a.pol == pol2]

    nStands = len(antennas1)
    baselines = uvutils.get_baselines(antennas1, antennas2=antennas2, include_auto=False, indicies=True)

    antennaBaselines = []
    for bl in range(len(baselines)):
            antennaBaselines.append( (antennas1[baselines[bl][0]], antennas2[baselines[bl][1]]) )
    bl = antennaBaselines

    uvw_m = np.array([np.array([b[0].stand.x - b[1].stand.x, b[0].stand.y - b[1].stand.y, b[0].stand.z - b[1].stand.z]) for b in bl])
    uvw = np.empty((len(bl), 3, len(freqs)))
    for i, f in enumerate(freqs):
        # wavelength = 3e8/f # TODO this should be fixed. What is currently happening is not true. Well it is, but only if you're looking for a specific transmitter frequency. Which I guess we are. I just mean it's not generalized.
        wavelength = 3e8/h5fo.attrs['tx_freq']
        uvw[:,:,i] = uvw_m/wavelength


    # Build antenna array (gets used in the VisibilityDataSet)
    # jd can't matter, right?
    jd = 2458847.2362531545
    antenna_array = simVis.build_sim_array(station, valid_ants, freqs/1e9, jd=jd, force_flat=True)
    # we only want the bin nearest to our frequency
    target_bin = np.argmin([abs(h5fo.attrs['tx_freq'] - f) for f in freqs])


    # Needed for PolarizationDataSet
    if h5fo.attrs['use_pol'] == 0:
        pol_string = 'XX'
        p=0 # this is related to the enumerate in lsl.imaging.utils.CorrelatedIDI().get_data_set() (for when there are multiple pols in a single dataset)
    else:
        raise RuntimeError("Only pol. XX supported right now.")


    if args.all_sky:
        fig, ax = plt.subplots()

    for k in np.arange(len(h5fo['vis_l_est'])):
        l_in = h5fo['vis_l_est'][k]
        m_in = h5fo['vis_m_est'][k]

        ## Build vis
        vismodel = point_source_visibility_model_uv(uvw[:,0,0],uvw[:,1,0],l_in,m_in)
        vis = np.empty((len(vismodel), len(freqs)), dtype=np.complex64)
        for i in np.arange(vis.shape[1]):
            vis[:,i] = vismodel

        if args.export_npy:
            print(args.export_npy)
            print("Exporting modelled u, v, w, and visibility")
            np.save('model-uvw{}.npy'.format(k), uvw)
            np.save('model-vis{}.npy'.format(k), vis)

        ## Start to build up the data structure for VisibilityDataSet

        dataSet = VisibilityDataSet(jd=jd, freq=freqs, baselines=bl, uvw=uvw, antennarray=antenna_array)
        polDataSet = PolarizationDataSet(pol_string, data=vis)
        dataSet.append(polDataSet)


        print('| Gridding and imaging with size={}, res={}, wres={}'.format(args.size, args.res, args.wres))

        gridded_image = build_gridded_image(dataSet, pol=pol_string,
            chan=target_bin, size=args.size,
            res=args.res, wres=args.wres)
        
        if args.export_npy:
            print("Exporting gridded u, v, and visibility")
            u,v = gridded_image.get_uv()
            np.save('gridded-u{}.npy'.format(k), u)
            np.save('gridded-v{}.npy'.format(k), v)
            np.save('gridded-vis{}.npy'.format(k), gridded_image.uv)


        l,m,img,extent=get_gimg_max(gridded_image, return_img=True)

        # Compute other values of interest
        elev, az = lm_to_ea(l, m)
        height = flatmirror_height(tx_coords, rx_coords, elev)

        h5fo['l_est'][k] = l
        h5fo['m_est'][k] = m

        h5fo['extent'][k] = extent

        h5fo['elevation'][k] = elev
        h5fo['azimuth'][k] = az
        h5fo['height'][k] = height

        if args.all_sky:
            ax.imshow(img, extent=extent, origin='lower', interpolation='nearest')
            ax.set_title('size={}, res={}, wres={}, iteration={}'.format(args.size,args.res,args.wres,k))
            ax.set_xlabel('l')
            ax.set_ylabel('m')
            ax.plot(l,m,marker='o', color='k', label='Image Max.')
            ax.plot(l_in,m_in,marker='x', color='r', label='Model (input)')
            plt.legend(loc='lower right')
            plt.savefig('allsky{}.png'.format(k))
            plt.cla()

        save_pkl_gridded = args.pkl_gridded and k in args.pkl_gridded
        if save_pkl_gridded:
            quickDict={'image':img, 'extent':extent}
            with open('gridded{}.pkl'.format(k), 'wb') as f:
                pickle.dump(quickDict, f, protocol=pickle.HIGHEST_PROTOCOL)

    h5fo.close()
Esempio n. 8
0
def main(args):
    filename = args.filename

    idi = utils.CorrelatedData(filename)
    aa = idi.get_antennaarray()
    lo = idi.get_observer()
    lo.date = idi.date_obs.strftime("%Y/%m/%d %H:%M:%S")
    jd = lo.date + astro.DJD_OFFSET
    lst = str(lo.sidereal_time())

    nStand = len(idi.stands)
    nchan = len(idi.freq)
    freq = idi.freq

    print("Raw Stand Count: %i" % nStand)
    print("Final Baseline Count: %i" % (nStand * (nStand - 1) // 2, ))
    print(
        "Spectra Coverage: %.3f to %.3f MHz in %i channels (%.2f kHz/channel)"
        % (freq[0] / 1e6, freq[-1] / 1e6, nchan,
           (freq[-1] - freq[0]) / 1e3 / nchan))
    print("Polarization Products: %i starting with %i" %
          (len(idi.pols), idi.pols[0]))
    print("JD: %.3f" % jd)

    # Pull out something reasonable
    toWork = numpy.where((freq >= args.lower) & (freq <= args.upper))[0]

    print("Reading in FITS IDI data")
    nSets = idi.total_baseline_count // (nStand * (nStand + 1) // 2)
    for set in range(1, nSets + 1):
        print("Set #%i of %i" % (set, nSets))
        fullDict = idi.get_data_set(set)
        dataDict = fullDict.get_uv_range(min_uv=14.0)
        dataDict.sort()

        # Gather up the polarizations and baselines
        pols = dataDict['jd'].keys()
        bls = dataDict['bls'][pols[0]]
        print("The reduced list has %i baselines and %i channels" %
              (len(bls), len(toWork)))

        # Build a list of unique JDs for the data
        jdList = []
        for jd in dataDict['jd'][pols[0]]:
            if jd not in jdList:
                jdList.append(jd)

        # Build the simulated visibilities
        print("Building Model")
        simDict = simVis.build_sim_data(aa,
                                        simVis.SOURCES,
                                        jd=[
                                            jdList[0],
                                        ],
                                        pols=pols,
                                        baselines=bls)

        print("Running self cal.")
        simDict = simDict.sort()
        dataDict = dataDict.sort()
        fixedDataXX, delaysXX = selfcal.delay_only(aa,
                                                   dataDict,
                                                   simDict,
                                                   toWork,
                                                   'xx',
                                                   ref_ant=args.reference,
                                                   max_iter=60)
        fixedDataYY, delaysYY = selfcal.delay_only(aa,
                                                   dataDict,
                                                   simDict,
                                                   toWork,
                                                   'yy',
                                                   ref_ant=args.reference,
                                                   max_iter=60)
        fixedFullXX = simVis.scale_data(fullDict, delaysXX * 0 + 1, delaysXX)
        fixedFullYY = simVis.scale_data(fullDict, delaysYY * 0 + 1, delaysYY)

        print("    Saving results")
        outname = os.path.split(filename)[1]
        outname = os.path.splitext(outname)[0]
        outname = "%s.sc" % outname
        fh = open(outname, 'w')
        fh.write("################################\n")
        fh.write("#                              #\n")
        fh.write("# Columns:                     #\n")
        fh.write("# 1) Stand number              #\n")
        fh.write("# 2) X pol. amplitude          #\n")
        fh.write("# 3) X pol. delay (ns)         #\n")
        fh.write("# 4) Y pol. amplitude          #\n")
        fh.write("# 5) Y pol. delay (ns)         #\n")
        fh.write("#                              #\n")
        fh.write("################################\n")
        for i in xrange(delaysXX.size):
            fh.write("%3i  %.6g  %.6g  %.6g  %.6g\n" %
                     (idi.stands[i], 1.0, delaysXX[i], 1.0, delaysYY[i]))
        fh.close()

        # Build up the images for each polarization
        if args.plot:
            print("    Gridding")
            toWork = numpy.where((freq >= 80e6) & (freq <= 82e6))[0]
            try:
                imgXX = utils.build_gridded_image(fullDict,
                                                  size=80,
                                                  res=0.5,
                                                  pol='xx',
                                                  chan=toWork)
            except:
                imgXX = None

            try:
                imgYY = utils.build_gridded_image(fullDict,
                                                  size=80,
                                                  res=0.5,
                                                  pol='yy',
                                                  chan=toWork)
            except:
                imgYY = None

            try:
                simgXX = utils.build_gridded_image(simDict,
                                                   size=80,
                                                   res=0.5,
                                                   pol='xx',
                                                   chan=toWork)
            except:
                simgXX = None
            try:
                simgYY = utils.build_gridded_image(simDict,
                                                   size=80,
                                                   res=0.5,
                                                   pol='yy',
                                                   chan=toWork)
            except:
                simgYY = None

            try:
                fimgXX = utils.build_gridded_image(fixedFullXX,
                                                   size=80,
                                                   res=0.5,
                                                   pol='xx',
                                                   chan=toWork)
            except:
                fimgXX = None
            try:
                fimgYY = utils.build_gridded_image(fixedFullYY,
                                                   size=80,
                                                   res=0.5,
                                                   pol='yy',
                                                   chan=toWork)
            except:
                fimgYY = None

            # Plots
            print("    Plotting")
            fig = plt.figure()
            ax1 = fig.add_subplot(3, 2, 1)
            ax2 = fig.add_subplot(3, 2, 2)
            ax3 = fig.add_subplot(3, 2, 3)
            ax4 = fig.add_subplot(3, 2, 4)
            ax5 = fig.add_subplot(3, 2, 5)
            ax6 = fig.add_subplot(3, 2, 6)
            for ax, img, pol in zip(
                [ax1, ax2, ax3, ax4, ax5, ax6],
                [imgXX, imgYY, simgXX, simgYY, fimgXX, fimgYY],
                ['XX', 'YY', 'simXX', 'simYY', 'scalXX', 'scalYY']):
                # Skip missing images
                if img is None:
                    ax.text(0.5,
                            0.5,
                            'Not found in file',
                            color='black',
                            size=12,
                            horizontalalignment='center')

                    ax.xaxis.set_major_formatter(NullFormatter())
                    ax.yaxis.set_major_formatter(NullFormatter())

                    ax.set_title("%s @ %s LST" % (pol, lst))
                    continue

                # Display the image and label with the polarization/LST
                out = img.image(center=(80, 80))
                print(pol, out.min(), out.max())
                #if pol == 'scalXX':
                #out = numpy.rot90(out)
                #out = numpy.rot90(out)
                cb = ax.imshow(out,
                               extent=(1, -1, -1, 1),
                               origin='lower',
                               vmin=img.image().min(),
                               vmax=img.image().max())
                fig.colorbar(cb, ax=ax)
                ax.set_title("%s @ %s LST" % (pol, lst))

                # Turn off tick marks
                ax.xaxis.set_major_formatter(NullFormatter())
                ax.yaxis.set_major_formatter(NullFormatter())

                # Compute the positions of major sources and label the images
                compSrc = {}
                ax.plot(0, 0, marker='+', markersize=10, markeredgecolor='w')
                for name, src in simVis.SOURCES.items():
                    src.compute(aa)
                    top = src.get_crds(crdsys='top', ncrd=3)
                    az, alt = aipy.coord.top2azalt(top)
                    compSrc[name] = [az, alt]
                    if alt <= 0:
                        continue
                    ax.plot(top[0],
                            top[1],
                            marker='x',
                            markerfacecolor='None',
                            markeredgecolor='w',
                            linewidth=10.0,
                            markersize=10)
                    ax.text(top[0], top[1], name, color='white', size=12)

                # Add lines of constant RA and dec.
                graticle(ax, lo.sidereal_time(), lo.lat)

            plt.show()

    print("...Done")
Esempio n. 9
0
def lsq(aa,
        dataDict,
        aipyImg,
        input_image=None,
        size=80,
        res=0.50,
        wres=0.10,
        pol='XX',
        chan=None,
        gain=0.05,
        max_iter=150,
        rtol=1e-9,
        verbose=True,
        plot=False):
    """
    Given a AIPY antenna array instance, a data dictionary, and an AIPY ImgW 
    instance filled with data, return a deconvolved image.  This function 
    implements a least squares deconvolution.
    
    Least squares tuning parameters:
      * gain - least squares loop gain (default 0.05)
      * max_iter - Maximum number of iteration (default 150)
      * rtol - Minimum change in the residual RMS between iterations
               (default 1e-9)
    """

    # Sort out the channels to work on
    if chan is None:
        chan = range(dataDict.freq.size)

    # Get a grid of right ascensions and dec values for the image we are working with
    xyz = aipyImg.get_eq(aa.sidereal_time(), aa.lat, center=(size, size))
    ra, dec = eq2radec(xyz)

    # Get the list of baselines to generate visibilites for
    baselines = dataDict.baselines

    # Estimate the zenith beam response
    psfSrc = {
        'z':
        RadioFixedBody(aa.sidereal_time(),
                       aa.lat,
                       jys=1.0,
                       index=0,
                       epoch=aa.date)
    }
    psfDict = build_sim_data(aa,
                             psfSrc,
                             jd=aa.get_jultime(),
                             pols=[
                                 pol,
                             ],
                             chan=chan,
                             baselines=baselines,
                             flat_response=True)
    psf = utils.build_gridded_image(psfDict,
                                    size=size,
                                    res=res,
                                    wres=wres,
                                    chan=chan,
                                    pol=pol,
                                    verbose=verbose)
    psf = psf.image(center=(size, size))
    psf /= psf.max()

    # Fit a Guassian to the zenith beam response and use that for the restore beam
    beamCutout = psf[size // 2:3 * size // 2, size // 2:3 * size // 2]
    beamCutout = numpy.where(beamCutout > 0.0, beamCutout, 0.0)
    h, cx, cy, sx, sy = _fit_gaussian(beamCutout)
    gauGen = gaussian2d(1.0, size / 2 + cx, size / 2 + cy, sx, sy)
    FWHM = int(round((sx + sy) / 2.0 * 2.0 * numpy.sqrt(2.0 * numpy.log(2.0))))
    beamClean = psf * 0.0
    for i in range(beamClean.shape[0]):
        for j in range(beamClean.shape[1]):
            beamClean[i, j] = gauGen(i, j)
    beamClean /= beamClean.sum()
    convMask = xyz.mask[0, :, :]

    # Get the actual image out of the ImgW instance
    if input_image is None:
        img = aipyImg.image(center=(size, size))
    else:
        img = input_image * 1.0

    # Build the initial model
    mdl = img * 0 + img.max()
    mdl[numpy.where(mdl < 0)] = 0
    mdl[numpy.where(ra.mask == 1)] = 0

    # Determine the overall image->model scale factor
    bSrcs = {}
    rChan = [chan[0], chan[-1]]
    bSrcs['zenith'] = RadioFixedBody(aa.sidereal_time(),
                                     aa.lat,
                                     name='zenith',
                                     jys=1,
                                     index=0)
    simDict = build_sim_data(aa,
                             bSrcs,
                             jd=aa.get_jultime(),
                             pols=[
                                 pol,
                             ],
                             chan=rChan,
                             baselines=baselines,
                             flat_response=True)
    simImg = utils.build_gridded_image(simDict,
                                       size=size,
                                       res=res,
                                       wres=wres,
                                       chan=rChan,
                                       pol=pol,
                                       verbose=verbose)
    simImg = simImg.image(center=(size, size))

    simToModel = 1.0 / simImg.max()
    modelToSim = simImg.max() / 1.0

    # Go!
    if plot:
        import pylab
        from matplotlib import pyplot as plt

        pylab.ion()

    rChan = [chan[0], chan[-1]]
    mdl *= modelToSim
    diff = img - mdl
    diffScaled = 0.0 * diff / gain
    oldModel = mdl
    oldRMS = diff.std() * 1e6
    oldDiff = diff * 0.0
    rHist = []
    exitStatus = 'iteration'
    for k in range(max_iter):
        ## Update the model image but don't allow negative flux
        mdl += diffScaled * gain
        mdl[numpy.where(mdl <= 0)] = 0.0

        ## Convert the model image to an ensemble of point sources for forward
        ## modeling
        bSrcs = {}
        for i in range(mdl.shape[0]):
            for j in range(mdl.shape[1]):
                if dec.mask[i, j]:
                    continue
                if mdl[i, j] <= 0:
                    continue

                nm = '%i-%i' % (i, j)
                bSrcs[nm] = RadioFixedBody(ra[i, j],
                                           dec[i, j],
                                           name=nm,
                                           jys=mdl[i, j],
                                           index=0,
                                           epoch=aa.date)

        ## Model the visibilities
        simDict = build_sim_data(aa,
                                 bSrcs,
                                 jd=aa.get_jultime(),
                                 pols=[
                                     pol,
                                 ],
                                 chan=rChan,
                                 baselines=baselines,
                                 flat_response=True)

        ## Form the simulated image
        simImg = utils.build_gridded_image(simDict,
                                           size=size,
                                           res=res,
                                           wres=wres,
                                           chan=rChan,
                                           pol=pol,
                                           verbose=verbose)
        simImg = simImg.image(center=(size, size))

        ## Difference the image and the simulated image and scale it to the
        ## model's peak flux
        diff = img - simImg
        diff2 = _minor_cycle(diff, beamClean, gain=0.1, max_iter=2000)

        ## Compute the RMS and create an appropriately scaled version of the model
        RMS = diff.std()
        mdl2 = mdl * modelToSim

        ## Status report
        if verbose:
            print("Iteration %i:  %i sources used, RMS is %.4e" %
                  (k + 1, len(bSrcs.keys()), RMS))
            print("               -> maximum residual: %.4e (%.3f%% of peak)" %
                  (diff.max(), 100.0 * diff.max() / img.max()))
            print("               -> minimum residual: %.4e (%.3f%% of peak)" %
                  (diff.min(), 100.0 * diff.min() / img.max()))
            print("               -> delta RMS: %.4e (%.3f%%)" %
                  (RMS - oldRMS, 100.0 * (RMS - oldRMS) / RMS))

        ## Make the cleaned residuals map ready for updating the model
        diff = diff2
        diffScaled = diff * simToModel

        ## Has the RMS gone up?  If so, it is time to exit.  But first, restore
        ## the previous iteration
        if RMS - oldRMS > 0:
            mdl = oldModel
            diff = oldDiff

            exitStatus = 'residuals'

            break

        ## Is the RMS still changing in an acceptable manner?
        if abs(RMS - oldRMS) < rtol:
            # No need to go back a step
            #mdl = oldModel
            #diff = oldDiff

            exitStatus = 'tolerance'

            break

        ## Save the current iteration as the previous state
        rHist.append(RMS)
        oldRMS = RMS
        oldModel = mdl
        oldDiff = diff

        if plot:
            pylab.subplot(3, 2, 1)
            pylab.imshow(img,
                         origin='lower',
                         interpolation='nearest',
                         vmin=img.min(),
                         vmax=img.max())
            pylab.subplot(3, 2, 2)
            pylab.imshow(simImg,
                         origin='lower',
                         interpolation='nearest',
                         vmin=img.min(),
                         vmax=img.max())
            pylab.subplot(3, 2, 3)
            pylab.imshow(diff, origin='lower', interpolation='nearest')
            pylab.subplot(3, 2, 4)
            pylab.imshow(mdl, origin='lower', interpolation='nearest')
            pylab.subplot(3, 1, 3)
            pylab.cla()
            pylab.plot(rHist)
            pylab.draw()

    # Summary
    print("Exited after %i iterations with status '%s'" % (k + 1, exitStatus))

    # Restore
    conv = convolve(mdl2, beamClean, mode='same')
    conv = numpy.ma.array(conv, mask=convMask)

    if plot:
        # Make an image for comparison purposes if we are verbose
        fig = plt.figure()
        ax1 = fig.add_subplot(2, 2, 1)
        ax2 = fig.add_subplot(2, 2, 2)
        ax3 = fig.add_subplot(2, 2, 3)
        ax4 = fig.add_subplot(2, 2, 4)

        c = ax1.imshow(img,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(c, ax=ax1)
        ax1.set_title('Input')

        d = ax2.imshow(simImg,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(d, ax=ax2)
        ax2.set_title('Realized Model')

        e = ax3.imshow(diff,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(e, ax=ax3)
        ax3.set_title('Residuals')

        f = ax4.imshow(conv + diff,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(f, ax=ax4)
        ax4.set_title('Final')

        plt.show()

    if plot:
        pylab.ioff()

    return conv + diff
Esempio n. 10
0
def clean_sources(aa,
                  dataDict,
                  aipyImg,
                  srcs,
                  input_image=None,
                  size=80,
                  res=0.50,
                  wres=0.10,
                  pol='XX',
                  chan=None,
                  gain=0.1,
                  max_iter=150,
                  sigma=2.0,
                  verbose=True,
                  plot=False):
    """
    Given a AIPY antenna array instance, a data dictionary, an AIPY ImgW 
    instance filled with data, and a dictionary of sources, return the CLEAN
    components and the residuals map.  This function uses a CLEAN-like method
    that computes the array beam for each peak in the flux.  Thus the CLEAN 
    loop becomes: 
      1.  Find the peak flux in the residual image
      2.  Compute the systems response to a point source at that location
      3.  Remove the scaled porition of this beam from the residuals
      4.  Go to 1.
    
    This function differs from clean() in that it only cleans localized 
    regions around each source rather than the whole image.  This is
    intended to help the mem() function along.
    
    CLEAN tuning parameters:
      * gain - CLEAN loop gain (default 0.1)
      * max_iter - Maximum number of iterations (default 150)
      * sigma - Threshold in sigma to stop cleaning (default 2.0)
    """

    # Sort out the channels to work on
    if chan is None:
        chan = range(dataDict.freq.size)

    # Get a grid of right ascensions and dec values for the image we are working with
    xyz = aipyImg.get_eq(0.0, aa.lat, center=(size, size))
    RA, dec = eq2radec(xyz)
    RA += aa.sidereal_time()
    RA %= (2 * numpy.pi)
    top = aipyImg.get_top(center=(size, size))
    az, alt = top2azalt(top)

    # Get the list of baselines to generate visibilites for
    baselines = dataDict.baselines

    # Get the actual image out of the ImgW instance
    if input_image is None:
        img = aipyImg.image(center=(size, size))
    else:
        img = input_image * 1.0

    # Setup the arrays to hold the point sources and the residual.
    cleaned = numpy.zeros_like(img)
    working = numpy.zeros_like(img)
    working += img

    # Setup the dictionary that will hold the beams as they are computed
    prevBeam = {}

    # Estimate the zenith beam response
    psfSrc = {
        'z':
        RadioFixedBody(aa.sidereal_time(),
                       aa.lat,
                       jys=1.0,
                       index=0,
                       epoch=aa.date)
    }
    psfDict = build_sim_data(aa,
                             psfSrc,
                             jd=aa.get_jultime(),
                             pols=[
                                 pol,
                             ],
                             chan=chan,
                             baselines=baselines,
                             flat_response=True)
    psf = utils.build_gridded_image(psfDict,
                                    size=size,
                                    res=res,
                                    wres=wres,
                                    chan=chan,
                                    pol=pol,
                                    verbose=verbose)
    psf = psf.image(center=(size, size))
    psf /= psf.max()

    # Fit a Guassian to the zenith beam response and use that for the restore beam
    beamCutout = psf[size // 2:3 * size // 2, size // 2:3 * size // 2]
    beamCutout = numpy.where(beamCutout > 0.0, beamCutout, 0.0)
    h, cx, cy, sx, sy = _fit_gaussian(beamCutout)
    gauGen = gaussian2d(1.0, size / 2 + cx, size / 2 + cy, sx, sy)
    FWHM = int(round((sx + sy) / 2.0 * 2.0 * numpy.sqrt(2.0 * numpy.log(2.0))))
    beamClean = psf * 0.0
    for i in range(beamClean.shape[0]):
        for j in range(beamClean.shape[1]):
            beamClean[i, j] = gauGen(i, j)
    beamClean /= beamClean.sum()
    convMask = xyz.mask[0, :, :]

    # Go!
    if plot:
        import pylab
        from matplotlib import pyplot as plt

        pylab.ion()

    for name, src in srcs.items():
        # Make sure the source is up
        src.compute(aa)
        if verbose:
            print('Source: %s @ %s degrees elevation' % (name, src.alt))
        if src.alt <= 10 * numpy.pi / 180.0:
            continue

        # Locate the approximate position of the source
        srcDist = (src.ra - RA)**2 + (src.dec - dec)**2
        srcPeak = numpy.where(srcDist == srcDist.min())

        # Define the clean box - this is fixed at 2*FWHM in width on each side
        rx0 = max([0, srcPeak[0][0] - FWHM // 2])
        rx1 = min([rx0 + FWHM + 1, img.shape[0]])
        ry0 = max([0, srcPeak[1][0] - FWHM // 2])
        ry1 = min([ry0 + FWHM + 1, img.shape[1]])

        # Define the background box - this lies outside the clean box and serves
        # as a reference for the background
        X, Y = numpy.indices(working.shape)
        R = numpy.sqrt((X - srcPeak[0][0])**2 + (Y - srcPeak[1][0])**2)
        bpad = 3
        background = numpy.where((R <= FWHM + bpad) & (R > FWHM))
        while len(background[0]) == 0 and bpad < img.shape[0]:
            bpad += 1
            background = numpy.where((R <= FWHM + bpad) & (R > FWHM))

        px0 = min(background[0]) - 1
        px1 = max(background[0]) + 2
        py0 = min(background[1]) - 1
        py1 = max(background[1]) + 2

        exitStatus = 'iteration'
        for i in range(max_iter):
            # Find the location of the peak in the flux density
            peak = numpy.where(working[rx0:rx1,
                                       ry0:ry1] == working[rx0:rx1,
                                                           ry0:ry1].max())
            peak_x = peak[0][0] + rx0
            peak_y = peak[1][0] + ry0
            peakV = working[peak_x, peak_y]

            # Optimize the location
            try:
                peakParams = _fit_gaussian(
                    working[peak_x - FWHM // 2:peak_x + FWHM // 2 + 1,
                            peak_y - FWHM // 2:peak_y + FWHM // 2 + 1])
            except IndexError:
                peakParams = [peakV, FWHM // 2, FWHM // 2]
            peakVO = peakParams[0]
            peak_xO = peak_x - FWHM // 2 + peakParams[1]
            peak_yO = peak_y - FWHM // 2 + peakParams[2]

            # Quantize to try and keep the computation down without over-simplifiying things
            subpixelationLevel = 5
            peak_xO = round(
                peak_xO * subpixelationLevel) / float(subpixelationLevel)
            peak_yO = round(
                peak_yO * subpixelationLevel) / float(subpixelationLevel)

            # Pixel coordinates to right ascension, dec.
            try:
                peakRA = _interpolate(RA, peak_xO, peak_yO)
            except IndexError:
                peak_xO, peak_yO = peak_x, peak_y
                peakRA = RA[peak_x, peak_y]
            try:
                peakDec = _interpolate(dec, peak_xO, peak_yO)
            except IndexError:
                peakDec = dec[peak_x, peak_y]

            # Pixel coordinates to az, el
            try:
                peakAz = _interpolate(az, peak_xO, peak_yO)
            except IndexError:
                peak_xO, peak_yO = peak_x, peak_y
                peakAz = az[peak_x, peak_y]
            try:
                peakEl = _interpolate(alt, peak_x, peak_y)
            except IndexError:
                peakEl = alt[peak_x, peak_y]

            if verbose:
                currRA = deg_to_hms(peakRA * 180 / numpy.pi)
                currDec = deg_to_dms(peakDec * 180 / numpy.pi)
                currAz = deg_to_dms(peakAz * 180 / numpy.pi)
                currEl = deg_to_dms(peakEl * 180 / numpy.pi)

                print(
                    "%s - Iteration %i:  Log peak of %.3f at row: %i, column: %i"
                    % (name, i + 1, numpy.log10(peakV), peak_x, peak_y))
                print("               -> RA: %s, Dec: %s" % (currRA, currDec))
                print("               -> az: %s, el: %s" % (currAz, currEl))

            # Check for the exit criteria
            if peakV < 0:
                exitStatus = 'peak value is negative'

                break

            # Find the beam index and see if we need to compute the beam or not
            beamIndex = (int(peak_xO * subpixelationLevel),
                         int(peak_yO * subpixelationLevel))
            try:
                beam = prevBeam[beamIndex]

            except KeyError:
                if verbose:
                    print("               -> Computing beam(s)")

                beamSrc = {
                    'Beam':
                    RadioFixedBody(peakRA,
                                   peakDec,
                                   jys=1.0,
                                   index=0,
                                   epoch=aa.date)
                }
                beamDict = build_sim_data(aa,
                                          beamSrc,
                                          jd=aa.get_jultime(),
                                          pols=[
                                              pol,
                                          ],
                                          chan=chan,
                                          baselines=baselines,
                                          flat_response=True)
                beam = utils.build_gridded_image(beamDict,
                                                 size=size,
                                                 res=res,
                                                 wres=wres,
                                                 chan=chan,
                                                 pol=pol,
                                                 verbose=verbose)
                beam = beam.image(center=(size, size))
                beam /= beam.max()
                if verbose:
                    print("                  ", beam.mean(), beam.min(),
                          beam.max(), beam.sum())

                prevBeam[beamIndex] = beam
                if verbose:
                    print("               -> Beam cache contains %i entries" %
                          len(prevBeam.keys()))

            # Calculate how much signal needs to be removed...
            toRemove = gain * peakV * beam
            working -= toRemove
            asum = 0.0
            for l in range(int(peak_xO), int(peak_xO) + 2):
                if l > peak_xO:
                    side1 = (peak_xO + 0.5) - (l - 0.5)
                else:
                    side1 = (l + 0.5) - (peak_xO - 0.5)

                for m in range(int(peak_yO), int(peak_yO) + 2):
                    if m > peak_yO:
                        side2 = (peak_yO + 0.5) - (m - 0.5)
                    else:
                        side2 = (m + 0.5) - (peak_yO - 0.5)

                    area = side1 * side2
                    asum += area
                    #print('II', l, m, area, asum)
                    cleaned[l, m] += gain * area * peakV

            if plot:
                try:
                    pylab.subplot(2, 2, 1)
                    pylab.imshow((working + toRemove)[px0:px1, py0:py1],
                                 origin='lower',
                                 interpolation='nearest')
                    pylab.title('Before')

                    pylab.subplot(2, 2, 2)
                    pylab.imshow(working[px0:px1, py0:py1],
                                 origin='lower',
                                 interpolation='nearest')
                    pylab.title('After')

                    pylab.subplot(2, 2, 3)
                    pylab.imshow(toRemove[px0:px1, py0:py1],
                                 origin='lower',
                                 interpolation='nearest')
                    pylab.title('Removed')

                    pylab.subplot(2, 2, 4)
                    pylab.imshow(convolve(cleaned, beamClean,
                                          mode='same')[px0:px1, py0:py1],
                                 origin='lower',
                                 interpolation='nearest')
                    pylab.title('CLEAN Comps.')
                except:
                    pass

                try:
                    st.set_text('%s @ %i' % (name, i + 1))
                except NameError:
                    st = pylab.suptitle('%s @ %i' % (name, i + 1))
                pylab.draw()

            if numpy.abs(
                    numpy.max(working[rx0:rx1, ry0:ry1]) -
                    numpy.median(working[background])) / rStd(
                        working[background]) <= sigma:
                exitStatus = 'peak is less than %.3f-sigma' % sigma

                break

        # Summary
        print("Exited after %i iterations with status '%s'" %
              (i + 1, exitStatus))

    # Restore
    conv = convolve(cleaned, beamClean, mode='same')
    conv = numpy.ma.array(conv, mask=convMask)
    conv *= ((img - working).max() / conv.max())

    if plot:
        # Make an image for comparison purposes if we are verbose
        fig = plt.figure()
        ax1 = fig.add_subplot(2, 2, 1)
        ax2 = fig.add_subplot(2, 2, 2)
        ax3 = fig.add_subplot(2, 2, 3)
        ax4 = fig.add_subplot(2, 2, 4)

        c = ax1.imshow(img,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(c, ax=ax1)
        ax1.set_title('Input')

        d = ax2.imshow(conv,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(d, ax=ax2)
        ax2.set_title('CLEAN Comps.')

        e = ax3.imshow(working,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(e, ax=ax3)
        ax3.set_title('Residuals')

        f = ax4.imshow(conv + working,
                       extent=(1, -1, -1, 1),
                       origin='lower',
                       interpolation='nearest')
        fig.colorbar(f, ax=ax4)
        ax4.set_title('Final')

        plt.show()

    if plot:
        pylab.ioff()

    # Return
    return conv, working
Esempio n. 11
0
def main(args):

    ## Check we should bother doing anything

    if not args.export_npy and not args.export_h5 and not args.all_sky and not args.pkl_gridded:
        raise RuntimeError(
            "You have not selected a data output of any type. Read the docstring and pick something for me to do."
        )

    # Normalize all inputs to the same length
    sizes = [int(item) for item in args.size.split(',')]
    reses = [float(item) for item in args.res.split(',')]
    wreses = [float(item) for item in args.wres.split(',')]
    maxinputlen = max(len(sizes), len(reses), len(wreses))
    if len(sizes) not in [1, maxinputlen] or len(reses) not in [
            1, maxinputlen
    ] or len(wreses) not in [1, maxinputlen]:
        raise RuntimeError(" \
        For size, res and wres you must pass either the same number of values as the max or a single value.\n \
        For example:\n \
        ALLOWED     -> sizes=175,180,190, res=0.5, wres=0.5\n \
                    -> sizes=175,180,190, res=0.5, wres=0.5,0.6,0.7\n \
        NOT ALLOWED -> sizes=175,180,190, res=0.5, wres=0.5,0.6 \
        ")
    if len(
            sizes
    ) != maxinputlen:  # You'd think there must be a good way to do this with list comprehension.
        sizes = sizes * maxinputlen
    if len(reses) != maxinputlen:
        reses = reses * maxinputlen
    if len(wreses) != maxinputlen:
        wreses = wreses * maxinputlen
    all_grid_params = []
    while len(sizes) > 0:
        all_grid_params.append({
            'size': sizes.pop(),
            'res': reses.pop(),
            'wres': wreses.pop()
        })

    ## Begin doing stuff
    tx_coords = known_transmitters.parse_args(args)
    if not transmitter_coords:
        print("Please specify a transmitter location")
        return
    rx_coords = [station.lat * 180 / np.pi, station.lon * 180 / np.pi]

    antennas = station.antennas

    valid_ants, n_baselines = select_antennas(antennas, args.use_pol)

    if args.export_h5:
        h5fname = "simulation-results.h5"
        print("Output will be written to {}".format(h5fname))
        h5f = h5py.File(h5fname, 'w')

        ats = h5f.attrs
        ats['transmitter'] = args.transmitter
        ats['tx_freq'] = args.tx_freq
        ats['valid_ants'] = [a.id for a in valid_ants]
        ats['n_baselines'] = n_baselines
        ats['fft_len'] = args.fft_len
        ats['use_pol'] = args.use_pol
        ats['int_length'] = args.integration_length
        ats['l_model'] = args.l_model
        ats['m_model'] = args.m_model

        h5f.create_dataset('l_est', (len(all_grid_params), ))
        h5f.create_dataset('m_est', (len(all_grid_params), ))
        h5f.create_dataset('wres', (len(all_grid_params), ))
        h5f.create_dataset('res', (len(all_grid_params), ))
        h5f.create_dataset('size', (len(all_grid_params), ))
        h5f.create_dataset('extent', (len(all_grid_params), 4))
        h5f.create_dataset('elevation', (len(all_grid_params), ))
        h5f.create_dataset('azimuth', (len(all_grid_params), ))
        h5f.create_dataset('height', (len(all_grid_params), ))

    ## Build freqs
    freqs = np.empty((args.fft_len, ), dtype=np.float64)
    #! Need to think of intelligent way of doing this.
    #! target_bin will probably not matter since all vis is the same
    freqs5 = [
        5284999.9897182, 5291249.9897182, 5297499.9897182, 5303749.9897182,
        5309999.9897182, 5316249.9897182, 5322499.9897182, 5328749.9897182,
        5334999.9897182, 5341249.9897182, 5347499.9897182, 5353749.9897182,
        5359999.9897182, 5366249.9897182, 5372499.9897182, 5378749.9897182
    ]
    for i in range(len(freqs)):
        freqs[i] = freqs5[i]

    ## Build bl
    pol_string = 'xx' if args.use_pol == 0 else 'yy'
    pol1, pol2 = pol_to_pols(pol_string)
    antennas1 = [a for a in valid_ants if a.pol == pol1]
    antennas2 = [a for a in valid_ants if a.pol == pol2]

    nStands = len(antennas1)
    baselines = uvutils.get_baselines(antennas1,
                                      antennas2=antennas2,
                                      include_auto=False,
                                      indicies=True)

    antennaBaselines = []
    for bl in range(len(baselines)):
        antennaBaselines.append(
            (antennas1[baselines[bl][0]], antennas2[baselines[bl][1]]))
    bl = antennaBaselines

    uvw_m = np.array([
        np.array([
            b[0].stand.x - b[1].stand.x, b[0].stand.y - b[1].stand.y,
            b[0].stand.z - b[1].stand.z
        ]) for b in bl
    ])
    uvw = np.empty((len(bl), 3, len(freqs)))
    for i, f in enumerate(freqs):
        # wavelength = 3e8/f # TODO this should be fixed. What is currently happening is not true. Well it is, but only if you're looking for a specific transmitter frequency. Which I guess we are. I just mean it's not generalized.
        wavelength = 3e8 / args.tx_freq
        uvw[:, :, i] = uvw_m / wavelength

    ## Build vis
    vismodel = point_source_visibility_model_uv(uvw[:, 0, 0], uvw[:, 1, 0],
                                                args.l_model, args.m_model)
    vis = np.empty((len(vismodel), len(freqs)), dtype=np.complex64)
    for i in np.arange(vis.shape[1]):
        vis[:, i] = vismodel

    if args.export_npy:
        print(args.export_npy)
        print("Exporting modelled u, v, w, and visibility")
        np.save('model-uvw.npy', uvw)
        np.save('model-vis.npy', vis)

    ## Start to build up the data structure for VisibilityDataSet
    # we only want the bin nearest to our frequency
    target_bin = np.argmin([abs(args.tx_freq - f) for f in freqs])

    # This can't matter, right?
    # jd = tbnf.get_info('start_time').jd
    jd = 2458847.2362531545

    # Build antenna array
    antenna_array = simVis.build_sim_array(station,
                                           antennas,
                                           freqs / 1e9,
                                           jd=jd,
                                           force_flat=True)

    dataSet = VisibilityDataSet(jd=jd,
                                freq=freqs,
                                baselines=bl,
                                uvw=uvw,
                                antennarray=antenna_array)
    if args.use_pol == 0:
        pol_string = 'XX'
        p = 0  # this is related to the enumerate in lsl.imaging.utils.CorrelatedIDI().get_data_set() (for when there are multiple pols in a single dataset)
    else:
        raise RuntimeError("Only pol. XX supported right now.")
    polDataSet = PolarizationDataSet(pol_string, data=vis)
    dataSet.append(polDataSet)

    if args.all_sky:
        fig, ax = plt.subplots()

    # Iterate over size/res/wres and generate multiple grids/images
    k = 0
    for grid_params in all_grid_params:
        print('| Gridding and imaging with size={}, res={}, wres={}'.format(
            grid_params['size'], grid_params['res'], grid_params['wres']))

        gridded_image = build_gridded_image(dataSet,
                                            pol=pol_string,
                                            chan=target_bin,
                                            size=grid_params['size'],
                                            res=grid_params['res'],
                                            wres=grid_params['wres'])

        if args.export_npy:
            print("Exporting gridded u, v, and visibility")
            u, v = gridded_image.get_uv()
            np.save(
                'gridded-u-size-{}-res-{}-wres-{}.npy'.format(
                    grid_params['size'], grid_params['res'],
                    grid_params['wres']), u)
            np.save(
                'gridded-v-size-{}-res-{}-wres-{}.npy'.format(
                    grid_params['size'], grid_params['res'],
                    grid_params['wres']), v)
            np.save(
                'gridded-vis-size-{}-res-{}-wres-{}.npy'.format(
                    grid_params['size'], grid_params['res'],
                    grid_params['wres']), gridded_image.uv)

        l, m, img, extent = get_gimg_max(gridded_image, return_img=True)

        # Compute other values of interest
        elev, az = lm_to_ea(l, m)
        height = flatmirror_height(tx_coords, rx_coords, elev)

        if args.export_h5:
            h5f['l_est'][k] = l
            h5f['m_est'][k] = m
            h5f['wres'][k] = grid_params['wres']
            h5f['res'][k] = grid_params['res']
            h5f['size'][k] = grid_params['size']

            h5f['extent'][k] = extent

            h5f['elevation'][k] = elev
            h5f['azimuth'][k] = az
            h5f['height'][k] = height

        if args.all_sky:
            ax.imshow(img,
                      extent=extent,
                      origin='lower',
                      interpolation='nearest')
            ax.set_title('size={}, res={}, wres={}'.format(
                grid_params['size'], grid_params['res'], grid_params['wres']))
            ax.set_xlabel('l')
            ax.set_ylabel('m')
            ax.plot(l, m, marker='o', color='k', label='Image Max.')
            ax.plot(args.l_model,
                    args.m_model,
                    marker='x',
                    color='r',
                    label='Model (input)')
            plt.legend(loc='lower right')
            plt.savefig('allsky_size_{}_res_{}_wres_{}.png'.format(
                grid_params['size'], grid_params['res'], grid_params['wres']))
            plt.cla()

        save_pkl_gridded = args.pkl_gridded and k in args.pkl_gridded
        if save_pkl_gridded:
            quickDict = {'image': img, 'extent': extent}
            with open(
                    'gridded_size_{}_res_{}_wres_{}.pkl'.format(
                        grid_params['size'], grid_params['res'],
                        grid_params['wres']), 'wb') as f:
                pickle.dump(quickDict, f, protocol=pickle.HIGHEST_PROTOCOL)
        k += 1

    if args.export_h5:
        h5f.close()
Esempio n. 12
0
def main(args):
    filename = args.filename
    
    idi = utils.CorrelatedData(filename)
    aa = idi.get_antennaarray()
    lo = idi.get_observer()
    
    nStand = len(idi.stands)
    nchan = len(idi.freq)
    freq = idi.freq
    
    print("Raw Stand Count: %i" % nStand)
    print("Final Baseline Count: %i" % (nStand*(nStand-1)/2,))
    print("Spectra Coverage: %.3f to %.3f MHz in %i channels (%.2f kHz/channel)" % (freq[0]/1e6, freq[-1]/1e6, nchan, (freq[1] - freq[0])/1e3))
    try:
        print("Polarization Products: %s" % ' '.join([NUMERIC_STOKES[p] for p in idi.pols]))
    except KeyError:
        # Catch for CASA MS that use a different numbering scheme
        NUMERIC_STOKESMS = {1:'I', 2:'Q', 3:'U', 4:'V', 
                        9:'XX', 10:'XY', 11:'YX', 12:'YY'}
        print("Polarization Products: %s" % ' '.join([NUMERIC_STOKESMS[p] for p in idi.pols]))
        
    print("Reading in FITS IDI data")
    nSets = idi.integration_count
    for set in range(1, nSets+1):
        if args.dataset != -1 and args.dataset != set:
            continue
            
        print("Set #%i of %i" % (set, nSets))
        dataDict = idi.get_data_set(set, min_uv=args.uv_min)
        
        # Build a list of unique JDs for the data
        pols = dataDict.pols
        jdList = [dataDict.mjd + astro.MJD_OFFSET,]
        
        # Find the LST
        lo.date = jdList[0] - astro.DJD_OFFSET
        utc = str(lo.date)
        lst = str(lo.sidereal_time())   # pylint:disable=no-member
        
        # Pull out the right channels
        toWork = numpy.where( (freq >= args.freq_start) & (freq <= args.freq_stop) )[0]
        if len(toWork) == 0:
            raise RuntimeError("Cannot find data between %.2f and %.2f MHz" % (args.freq_start/1e6, args.freq_stop/1e6))
            
        # Integration report
        print("    Date Observed: %s" % utc)
        print("    LST: %s" % lst)
        print("    Selected Frequencies: %.3f to %.3f MHz" % (freq[toWork[0]]/1e6, freq[toWork[-1]]/1e6))
        
        # Prune out what needs to go
        if args.include != 'all' or args.exclude != 'none':
            print("    Processing include/exclude lists")
            dataDict = dataDict.get_antenna_subset(include=args.include, 
                                                   exclude=args.exclude, 
                                                   indicies=False)
            
            ## Report
            for pol in dataDict.pols:
                print("        %s now has %i baselines" % (pol, len(dataDict.baselines)))
                
        # Build up the images for each polarization
        print("    Gridding")
        img1 = None
        lbl1 = 'XX'
        for p in ('XX', 'RR', 'I'):
            try:
                img1 = utils.build_gridded_image(dataDict, size=NPIX_SIDE//2, res=0.5, pol=p, chan=toWork)
                lbl1 = p.upper()
            except:
                pass
                
        img2 = None
        lbl2 = 'YY'
        for p in ('YY', 'LL', 'Q'):
            try:
                img2 = utils.build_gridded_image(dataDict, size=NPIX_SIDE//2, res=0.5, pol=p, chan=toWork)
                lbl2 = p.upper()
            except:
                pass
                
        img3 = None
        lbl3 = 'XY'
        for p in ('XY', 'RL', 'U'):
            try:
                img3 = utils.build_gridded_image(dataDict, size=NPIX_SIDE//2, res=0.5, pol=p, chan=toWork)
                lbl3 = p.upper()
            except:
                pass
                
        img4 = None
        lbl4 = 'YX'
        for p in ('YX', 'LR', 'V'):
            try:
                img4 = utils.build_gridded_image(dataDict, size=NPIX_SIDE//2, res=0.5, pol=p, chan=toWork)
                lbl4 = p.upper()
            except:
                pass
                
        # Plots
        print("    Plotting")
        fig = plt.figure()
        ax1 = fig.add_subplot(2, 2, 1)
        ax2 = fig.add_subplot(2, 2, 2)
        ax3 = fig.add_subplot(2, 2, 3)
        ax4 = fig.add_subplot(2, 2, 4)
        for ax, img, pol in zip([ax1, ax2, ax3, ax4], [img1, img2, img3, img4], [lbl1, lbl2, lbl3, lbl4]):
            # Skip missing images
            if img is None:
                ax.text(0.5, 0.5, 'Not found in file', color='black', size=12, horizontalalignment='center')
                
                ax.xaxis.set_major_formatter( NullFormatter() )
                ax.yaxis.set_major_formatter( NullFormatter() )
                
                if not args.utc:
                    ax.set_title("%s @ %s LST" % (pol, lst))
                else:
                    ax.set_title("%s @ %s UTC" % (pol, utc))
                continue
                
            # Display the image and label with the polarization/LST
            cb = utils.plot_gridded_image(ax, img)
            fig.colorbar(cb, ax=ax)
            if not args.utc:
                ax.set_title("%s @ %s LST" % (pol, lst))
            else:
                ax.set_title("%s @ %s UTC" % (pol, utc))
                
            junk = img.image(center=(NPIX_SIDE//2,NPIX_SIDE//2))
            print("%s: image is %.4f to %.4f with mean %.4f" % (pol, junk.min(), junk.max(), junk.mean()))
            
            # Turn off tick marks
            ax.xaxis.set_major_formatter( NullFormatter() )
            ax.yaxis.set_major_formatter( NullFormatter() )
            
            # Compute the positions of major sources and label the images
            overlay.sources(ax, aa, simVis.SOURCES, label=not args.no_labels)
            
            # Add in the horizon
            overlay.horizon(ax, aa)
            
            # Add lines of constant RA and dec.
            if not args.no_grid:
                if not args.topo:
                    overlay.graticule_radec(ax, aa)
                else:
                    overlay.graticule_azalt(ax, aa)
                    
        plt.show()
        
        if args.fits is not None:
            ## Loop over the images to build up the FITS file
            hdulist = [astrofits.PrimaryHDU(),]
            for img,pol in zip((img1,img2,img3,img4), (lbl1,lbl2,lbl3,lbl4)):
                if img is None:
                    continue
                    
                ### Create the HDU
                try:
                    hdu = astrofits.ImageHDU(data=img.image(center=(NPIX_SIDE//2,NPIX_SIDE//2)), name=pol)
                except AttributeError:
                    hdu = astrofits.ImageHDU(data=img, name=pol)
                    
                ### Add in the coordinate information
                hdu.header['EPOCH'] = 2000.0 + (jdList[0] - 2451545.0) / 365.25
                hdu.header['CTYPE1'] = 'RA---SIN'
                hdu.header['CRPIX1'] = NPIX_SIDE//2+1
                hdu.header['CDELT1'] = -360.0/NPIX_SIDE/numpy.pi
                hdu.header['CRVAL1'] = lo.sidereal_time()*180/numpy.pi	# pylint:disable=no-member
                hdu.header['CTYPE2'] = 'DEC--SIN'
                hdu.header['CRPIX2'] = NPIX_SIDE//2+1
                hdu.header['CDELT2'] = 360.0/NPIX_SIDE/numpy.pi
                hdu.header['CRVAL2'] = lo.lat*180/numpy.pi
                hdu.header['LONPOLE'] = 180.0
                hdu.header['LATPOLE'] = 90.0
                
                ### Add the HDU to the list
                hdulist.append(hdu)
                
            ## Save the FITS file to disk
            hdulist = astrofits.HDUList(hdulist)
            overwrite = False
            if os.path.exists(args.fits):
                yn = input("WARNING: '%s' exists, overwrite? [Y/n]" % args.fits)
                if yn not in ('n', 'N'):
                    overwrite = True
            try:
                hdulist.writeto(args.fits, overwrite=overwrite)
            except IOError as e:
                print("WARNING: FITS image file not saved")
                
    print("...Done")