Example #1
0
    zgrid[goodind] = NP.sqrt(1.0 - (xgrid[goodind]**2 + ygrid[goodind]**2))

    xvect = xgrid.ravel()
    yvect = ygrid.ravel()
    zvect = zgrid.ravel()
    xyzvect = NP.hstack((xvect.reshape(-1,1), yvect.reshape(-1,1), zvect.reshape(-1,1)))

if use_DSM or use_GSM:
    backdrop = HP.cartview(fluxes_DSM.ravel(), coord=['G','E'], rot=[180,0,0], xsize=backdrop_xsize, return_projected_map=True)
elif use_GLEAM or use_SUMSS:
    if backdrop_coords == 'radec':
        backdrop = griddata(NP.hstack((ra_deg.reshape(-1,1), dec_deg.reshape(-1,1))), fpeak, NP.hstack((xvect.reshape(-1,1), yvect.reshape(-1,1))), method='cubic')
        backdrop = backdrop.reshape(backdrop_xsize/2, backdrop_xsize)
    elif backdrop_coords == 'dircos':
        if (telescope == 'mwa_dipole') or (obs_mode == 'drift'):
            backdrop = PB.primary_beam_generator(xyzvect, freq, telescope=telescope, freq_scale='Hz', skyunits='dircos', phase_center=[0.0,0.0,1.0])
            backdrop = backdrop.reshape(backdrop_xsize, backdrop_xsize)
else:
    if backdrop_coords == 'radec':
        backdrop = griddata(NP.hstack((ra_deg.reshape(-1,1), dec_deg.reshape(-1,1))), fpeak, NP.hstack((xvect.reshape(-1,1), yvect.reshape(-1,1))), method='nearest')
        backdrop = backdrop.reshape(backdrop_xsize/2, backdrop_xsize)
    elif backdrop_coords == 'dircos':
        if (telescope == 'mwa_dipole') or (obs_mode == 'drift'):
            backdrop = PB.primary_beam_generator(xyzvect, freq, telescope=telescope, freq_scale='Hz', skyunits='dircos', phase_center=[0.0,0.0,1.0])
            backdrop = backdrop.reshape(backdrop_xsize, backdrop_xsize)

## Create data for overlay 

overlays = []
roi_obj_inds = []
for i in xrange(n_snaps):
        catlabel = NP.concatenate((catlabel, NP.repeat('NVSS',count_valid)))
        ra_deg = NP.concatenate((ra_deg, ra_deg_NVSS[NP.logical_and(NP.logical_and(not_in_SUMSS_ind, bright_source_ind), PS_ind)]))
        dec_deg = NP.concatenate((dec_deg, dec_deg_NVSS[NP.logical_and(NP.logical_and(not_in_SUMSS_ind, bright_source_ind), PS_ind)]))
        spindex = NP.concatenate((spindex, spindex_NVSS[NP.logical_and(NP.logical_and(not_in_SUMSS_ind, bright_source_ind), PS_ind)]))
        majax = NP.concatenate((majax, nvss_majax[NP.logical_and(NP.logical_and(not_in_SUMSS_ind, bright_source_ind), PS_ind)]))
        minax = NP.concatenate((minax, nvss_minax[NP.logical_and(NP.logical_and(not_in_SUMSS_ind, bright_source_ind), PS_ind)]))
        fluxes = NP.concatenate((fluxes, nvss_fpeak))
    
        ctlgobj = CTLG.Catalog(catlabel, freq_catalog, NP.hstack((ra_deg.reshape(-1,1), dec_deg.reshape(-1,1))), fluxes, spectral_index=spindex, src_shape=NP.hstack((majax.reshape(-1,1),minax.reshape(-1,1),NP.zeros(fluxes.size).reshape(-1,1))), src_shape_units=['degree','degree','degree'])

    if backdrop_coords == 'radec':
        backdrop = griddata(NP.hstack((ra_deg.reshape(-1,1), dec_deg.reshape(-1,1))), fluxes, NP.hstack((xvect.reshape(-1,1), yvect.reshape(-1,1))), method='cubic')
        backdrop = backdrop.reshape(backdrop_xsize/2, backdrop_xsize)
    elif backdrop_coords == 'dircos':
        if (telescope == 'mwa_dipole') or (obs_mode == 'drift'):
            backdrop = PB.primary_beam_generator(xyzvect, freq, telescope=telescope, freq_scale='Hz', skyunits='dircos', pointing_center=[0.0,0.0,1.0])
            backdrop = backdrop.reshape(backdrop_xsize, backdrop_xsize)
else:
    if use_PS:
        catalog_file = '/data3/t_nithyanandan/project_MWA/foregrounds/PS_catalog.txt'
        catdata = ascii.read(catalog_file, comment='#', header_start=0, data_start=1)
        ra_deg = catdata['RA'].data
        dec_deg = catdata['DEC'].data
        fluxes = catdata['F_INT'].data
        
    if backdrop_coords == 'radec':
        ra_deg_wrapped = ra_deg.ravel() + 0.0
        ra_deg_wrapped[ra_deg > 180.0] -= 360.0
        
        dxvect = xgrid[0,1]-xgrid[0,0]
        dyvect = ygrid[1,0]-ygrid[0,0]
    def observe(
        self, timestamp, Tsys, bandpass, pointing_center, skymodel, tobs, pb_min=0.1, fov_radius=None, lst=None
    ):

        if bandpass.size != self.bp.shape[1]:
            raise ValueError("bandpass length does not match.")

        self.Tsys = self.Tsys + [Tsys]
        self.vis_rms_freq = self.vis_rms_freq + [
            2.0 * FCNST.k * Tsys / self.A_eff / self.eff_Q / NP.sqrt(2) / tobs / self.freq_resolution / CNST.Jy
        ]
        self.tobs = self.tobs + [tobs]
        self.lst = self.lst + [lst]

        if self.timestamp == []:
            self.bp = NP.asarray(bandpass).reshape(1, -1)
            self.pointing_center = NP.asarray(pointing_center).reshape(1, -1)
        else:
            self.bp = NP.vstack((self.bp, NP.asarray(bandpass).reshape(1, -1)))
            self.pointing_center = NP.vstack((self.pointing_center, NP.asarray(pointing_center).reshape(1, -1)))

        pointing_lon = self.pointing_center[-1, 0]
        pointing_lat = self.pointing_center[-1, 1]

        if self.skycoords == "radec":
            if self.pointing_coords == "hadec":
                if lst is not None:
                    pointing_lon = lst - self.pointing_center[-1, 0]
                    pointing_lat = self.pointing_center[-1, 1]
                else:
                    raise ValueError(
                        "LST must be provided. Sky coordinates are in RA-Dec format while pointing center is in HA-Dec format."
                    )
            elif self.pointing_coords == "altaz":
                pointing_lonlat = lst - GEOM.altaz2hadec(self.pointing_center[-1, :], self.latitude, units="degrees")
                pointing_lon = pointing_lonlat[0]
                pointing_lat = pointing_lonlat[1]
        elif self.skycoords == "hadec":
            if self.pointing_coords == "radec":
                if lst is not None:
                    pointing_lon = lst - self.pointing_center[-1, 0]
                    pointing_lat = self.pointing_center[-1, 1]
                else:
                    raise ValueError(
                        "LST must be provided. Sky coordinates are in RA-Dec format while pointing center is in HA-Dec format."
                    )
            elif self.pointing_coords == "altaz":
                pointing_lonlat = lst - GEOM.altaz2hadec(self.pointing_center[-1, :], self.latitude, units="degrees")
                pointing_lon = pointing_lonlat[0]
                pointing_lat = pointing_lonlat[1]
        else:
            if self.pointing_coords == "radec":
                if lst is not None:
                    pointing_lonlat = GEOM.hadec2altaz(
                        NP.asarray([lst - self.pointing_center[-1, 0], self.pointing_center[-1, 1]]),
                        self.latitude,
                        units="degrees",
                    )
                    pointing_lon = pointing_lonlat[0]
                    pointing_lat = pointing_lonlat[1]
                else:
                    raise ValueError(
                        "LST must be provided. Sky coordinates are in Alt-Az format while pointing center is in RA-Dec format."
                    )
            elif self.pointing_coords == "hadec":
                pointing_lonlat = GEOM.hadec2altaz(self.pointing_center, self.latitude, units="degrees")
                pointing_lon = pointing_lonlat[0]
                pointing_lat = pointing_lonlat[1]

        pointing_phase = 0.0

        baseline_in_local_frame = self.baseline
        if self.baseline_coords == "equatorial":
            baseline_in_local_frame = GEOM.xyz2enu(self.baseline, self.latitude, "degrees")

        ptmp = self.pointing_center[-1, :]  # Convert pointing center to Alt-Az coordinates
        if self.pointing_coords == "hadec":
            ptmp = GEOM.hadec2altaz(self.pointing_center[-1, :], self.latitude, units="degrees")
        elif self.pointing_coords == "radec":
            if lst is not None:
                ptmp = GEOM.hadec2altaz(
                    NP.asarray([lst - self.pointing_center[-1, 0], self.pointing_center[-1, 1]]),
                    self.latitude,
                    units="degrees",
                )
            else:
                raise ValueError(
                    "LST must be provided. Sky coordinates are in Alt-Az format while pointing center is in RA-Dec format."
                )

        ptmp = GEOM.altaz2dircos(ptmp, "degrees")  # Convert pointing center to direction cosine coordinates

        pointing_phase = (
            2.0
            * NP.pi
            * NP.dot(baseline_in_local_frame.reshape(1, -1), ptmp.reshape(-1, 1))
            * self.channels.reshape(1, -1)
            / FCNST.c
        )

        if fov_radius is None:
            fov_radius = 90.0

        # PDB.set_trace()
        m1, m2, d12 = GEOM.spherematch(
            pointing_lon,
            pointing_lat,
            skymodel.catalog.location[:, 0],
            skymodel.catalog.location[:, 1],
            fov_radius,
            maxmatches=0,
        )

        # if fov_radius is not None:
        #     m1, m2, d12 = GEOM.spherematch(pointing_lon, pointing_lat, skymodel.catalog.location[:,0], skymodel.catalog.location[:,1], fov_radius, maxmatches=0)
        # else:
        #     m1 = [0] * skymodel.catalog.location.shape[0]
        #     m2 = xrange(skymodel.catalog.location.shape[0])
        #     d12 = GEOM.sphdist(NP.empty(skymodel.catalog.shape[0]).fill(pointing_lon), NP.empty(skymodel.catalog.shape[0]).fill(pointing_lat), skymodel.catalog.location[:,0], skymodel.catalog.location[:,1])

        if len(d12) != 0:
            pb = NP.empty((len(d12), len(self.channels)))
            fluxes = NP.empty((len(d12), len(self.channels)))

            coords_str = self.skycoords
            if self.skycoords == "radec":
                coords_str = "altaz"
                source_positions = GEOM.hadec2altaz(
                    NP.hstack(
                        (
                            NP.asarray(lst - skymodel.catalog.location[m2, 0]).reshape(-1, 1),
                            skymodel.catalog.location[m2, 1].reshape(-1, 1),
                        )
                    ),
                    self.latitude,
                    "degrees",
                )

            for i in xrange(len(self.channels)):
                # pb[:,i] = PB.primary_beam_generator(d12, self.channels[i]/1.0e9, 'degrees', self.telescope)
                pb[:, i] = PB.primary_beam_generator(
                    source_positions, self.channels[i] / 1.0e9, "altaz", self.telescope
                )
                fluxes[:, i] = (
                    skymodel.catalog.flux_density[m2]
                    * (self.channels[i] / skymodel.catalog.frequency) ** skymodel.catalog.spectral_index[m2]
                )

            geometric_delays = DLY.geometric_delay(
                baseline_in_local_frame,
                source_positions,
                altaz=(coords_str == "altaz"),
                hadec=(coords_str == "hadec"),
                latitude=self.latitude,
            )
            self.geometric_delays = self.geometric_delays + [geometric_delays.reshape(len(source_positions))]

            phase_matrix = 2.0 * NP.pi * NP.repeat(
                geometric_delays.reshape(-1, 1), len(self.channels), axis=1
            ) * NP.repeat(self.channels.reshape(1, -1), len(d12), axis=0) - NP.repeat(pointing_phase, len(d12), axis=0)

            skyvis = NP.sum(
                pb
                * fluxes
                * NP.repeat(NP.asarray(bandpass).reshape(1, -1), len(d12), axis=0)
                * NP.exp(-1j * phase_matrix),
                axis=0,
            )
            if fov_radius is not None:
                self.obs_catalog_indices = self.obs_catalog_indices + [m2]
                # self.obs_catalog = self.obs_catalog + [skymodel.catalog.subset(m2)]
        else:
            print "No sources found in the catalog within matching radius. Simply populating the observed visibilities with noise."
            skyvis = NP.zeros((1, len(self.channels)))

        if self.timestamp == []:
            self.skyvis_freq = skyvis.reshape(1, -1)
            self.vis_noise_freq = self.vis_rms_freq[-1] * (
                NP.random.randn(len(self.channels)).reshape(1, -1)
                + 1j * NP.random.randn(len(self.channels)).reshape(1, -1)
            )
            self.vis_freq = self.skyvis_freq + self.vis_noise_freq
        else:
            self.skyvis_freq = NP.vstack((self.skyvis_freq, skyvis.reshape(1, -1)))
            self.vis_noise_freq = NP.vstack(
                (
                    self.vis_noise_freq,
                    self.vis_rms_freq[-1]
                    * (
                        NP.random.randn(len(self.channels)).reshape(1, -1)
                        + 1j * NP.random.randn(len(self.channels)).reshape(1, -1)
                    ),
                )
            )
            self.vis_freq = NP.vstack(
                (self.vis_freq, (self.skyvis_freq[-1, :] + self.vis_noise_freq[-1, :]).reshape(1, -1))
            )

        self.timestamp = self.timestamp + [timestamp]
freq = NP.float(freq)
freq_resolution = NP.float(freq_resolution)
chans = (freq + (NP.arange(nchan) - 0.5 * nchan) * freq_resolution)/ 1e9 # in GHz
bandpass_str = '{0:0d}x{1:.1f}_kHz'.format(nchan, freq_resolution/1e3)

theta, phi = HP.pix2ang(nside, NP.arange(HP.nside2npix(nside)))
alt = 90.0 - NP.degrees(theta)
az = NP.degrees(phi)
altaz = NP.hstack((alt.reshape(-1,1), az.reshape(-1,1)))

pinfo = {}
pinfo['pointing_center'] = pointing_altaz
pinfo['pointing_coords'] = 'altaz'

pb = PB.primary_beam_generator(altaz, chans, telescope, freq_scale='GHz', skyunits='altaz', pointing_info=pinfo, short_dipole_approx=short_dipole_approx, half_wave_dipole_approx=half_wave_dipole_approx)

colnum = 0
npix = HP.nside2npix(nside)
frequencies = chans * 1e3

if outfile is not None:
    if not isinstance(outfile, str):
        raise TypeError('outfile parameter must be a string')
else:
    outfile = telescope_str+beam_id+'_'+ground_plane_str+'nside_{0:0d}_'.format(nside)+delaygain_err_str+'{0}_{1:.1f}_MHz'.format(bandpass_str, freq/1e6)+'.fits'

hdulist = []
hdulist += [fits.PrimaryHDU()]
hdulist[0].header['EXTNAME'] = 'PRIMARY'
hdulist[0].header['NPOL'] = (1, 'Number of polarizations')
else:
    pointings_radec = NP.hstack((NP.asarray(lst-pointing_init[0]).reshape(-1,1), pointing_init[1]+NP.zeros(n_snaps).reshape(-1,1)))

pointings_hadec = NP.hstack(((lst-pointings_radec[:,0]).reshape(-1,1), pointings_radec[:,1].reshape(-1,1)))
pointings_altaz = GEOM.hadec2altaz(pointings_hadec, MWA_latitude, units='degrees')
pointings_dircos = GEOM.altaz2dircos(pointings_altaz, units='degrees')
delay_matrix = DLY.delay_envelope(intrfrmtr.baseline, pointings_dircos, units='mks')

pbeams = []
m2s = []
for i in xrange(n_snaps):
    havect = lst[i] - ravect
    altaz = GEOM.hadec2altaz(NP.hstack((havect.reshape(-1,1),decvect.reshape(-1,1))), MWA_latitude, units='degrees')
    # m1, m2, d12 = GEOM.spherematch(pointings[i,0], pointings[i,1], ravect, decvect, 90.0, maxmatches=0)
    roi_altaz = NP.asarray(NP.where(altaz[:,0] >= 0.0)).ravel()
    pb = PB.primary_beam_generator(altaz[roi_altaz,:], freq, telescope='mwa', skyunits='altaz', freq_scale='Hz', phase_center=pointings_altaz[i,:])
    pbeams += [pb]
    m2s += [roi_altaz]

## Plotting animation

fig = PLT.figure(figsize=(14,14))
ax1 = fig.add_subplot(211)
# fig, (ax1, ax2) = PLT.subplots(2,1,figsize=(14,12))
ax1.set_xlabel(r'$\eta$ [$\mu$s]', fontsize=18)
ax1.set_ylabel('Amplitude [K Hz]', fontsize=18)
ax1.set_title('Delay Spectrum', fontsize=18, weight='semibold')
ax1.set_yscale('log')
ax1.set_xlim(1e6*NP.amin(lags)-1.0, 1e6*NP.amax(lags)+1.0)
ax1.set_ylim(0.5*NP.amin(NP.abs(vis_lag)),2.0*NP.amax(NP.abs(vis_lag)))
l1 = ax1.plot([], [], 'k+', [], [], 'k-', [], [], 'k-', [], [], 'k:', [], [], 'k:', markersize=10)
                               src_shape_units=['degree', 'degree', 'degree'])

    if backdrop_coords == 'radec':
        backdrop = griddata(NP.hstack(
            (ra_deg.reshape(-1, 1), dec_deg.reshape(-1, 1))),
                            fluxes,
                            NP.hstack(
                                (xvect.reshape(-1, 1), yvect.reshape(-1, 1))),
                            method='cubic')
        backdrop = backdrop.reshape(backdrop_xsize / 2, backdrop_xsize)
    elif backdrop_coords == 'dircos':
        if (telescope == 'mwa_dipole') or (obs_mode == 'drift'):
            backdrop = PB.primary_beam_generator(
                xyzvect,
                freq,
                telescope=telescope,
                freq_scale='Hz',
                skyunits='dircos',
                pointing_center=[0.0, 0.0, 1.0])
            backdrop = backdrop.reshape(backdrop_xsize, backdrop_xsize)
else:
    if use_PS:
        catalog_file = '/data3/t_nithyanandan/project_MWA/foregrounds/PS_catalog.txt'
        catdata = ascii.read(catalog_file,
                             comment='#',
                             header_start=0,
                             data_start=1)
        ra_deg = catdata['RA'].data
        dec_deg = catdata['DEC'].data
        fluxes = catdata['F_INT'].data