Exemplo n.º 1
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]
        roi_sector_altaz = NP.asarray(NP.where(NP.logical_or(NP.logical_and(az[roi_altaz] >= -0.5*180.0/n_sky_sectors + sky_sector*180.0/n_sky_sectors, az[roi_altaz] < -0.5*180.0/n_sky_sectors + (sky_sector+1)*180.0/n_sky_sectors), NP.logical_and(az[roi_altaz] >= 180.0 - 0.5*180.0/n_sky_sectors + sky_sector*180.0/n_sky_sectors, az[roi_altaz] < 180.0 - 0.5*180.0/n_sky_sectors + (sky_sector+1)*180.0/n_sky_sectors)))).ravel()
        pb = NP.empty(xvect.size)
        pb.fill(NP.nan)
        bd = NP.empty(xvect.size)
        bd.fill(NP.nan)
        pb[roi_altaz] = PB.primary_beam_generator(altaz[roi_altaz,:], freq, telescope=telescope, skyunits='altaz', freq_scale='Hz', pointing_center=fhd_obsid_pointing_altaz)
        # bd[roi_altaz] = backdrop.ravel()[roi_altaz]
        # pb[roi_altaz[roi_sector_altaz]] = PB.primary_beam_generator(altaz[roi_altaz[roi_sector_altaz],:], freq, telescope=telescope, skyunits='altaz', freq_scale='Hz', phase_center=fhd_obsid_pointing_altaz)
        bd[roi_altaz[roi_sector_altaz]] = backdrop.ravel()[roi_altaz[roi_sector_altaz]]
        overlay['pbeam'] = pb
        overlay['backdrop'] = bd
        overlay['roi_obj_inds'] = roi_altaz
        overlay['roi_sector_inds'] = roi_altaz[roi_sector_altaz]
        overlay['delay_map'] = NP.empty((n_bins_baseline_orientation, xvect.size))
        overlay['delay_map'].fill(NP.nan)
        overlay['delay_map'][:,roi_altaz] = (DLY.geometric_delay(cardinal_bl, altaz[roi_altaz,:], altaz=True, dircos=False, hadec=False, latitude=latitude)-DLY.geometric_delay(cardinal_bl, pc, altaz=False, dircos=True, hadec=False, latitude=latitude)).T
        if use_CSM or use_SUMSS or use_NVSS or use_PS:
            src_hadec = NP.hstack(((fhd_lst-ctlgobj.location[:,0]).reshape(-1,1), ctlgobj.location[:,1].reshape(-1,1)))
            src_altaz = GEOM.hadec2altaz(src_hadec, latitude, units='degrees')
            roi_src_altaz = NP.asarray(NP.where(src_altaz[:,0] >= 0.0)).ravel()
            roi_pbeam = PB.primary_beam_generator(src_altaz[roi_src_altaz,:], freq, telescope=telescope, skyunits='altaz', freq_scale='Hz', pointing_center=fhd_obsid_pointing_altaz)
            overlay['src_ind'] = roi_src_altaz
            overlay['pbeam_on_src'] = roi_pbeam.ravel()

        # delay_envelope = DLY.delay_envelope(cardinal_bl, dircos[roi_altaz,:])
        # overlay['delay_map'][:,roi_altaz] = (DLY.geometric_delay(cardinal_bl, altaz[roi_altaz,:], altaz=True, dircos=False, hadec=False, latitude=latitude)-DLY.geometric_delay(cardinal_bl, fhd_obsid_pointing_altaz, altaz=True, dircos=False, hadec=False, latitude=latitude)).T
        # roi_obj_inds += [roi_altaz]
    elif backdrop_coords == 'dircos':
        havect = fhd_lst - ra_deg
        fg_altaz = GEOM.hadec2altaz(NP.hstack((havect.reshape(-1,1),dec_deg.reshape(-1,1))), latitude, units='degrees')
        fg_dircos = GEOM.altaz2dircos(fg_altaz, units='degrees')
     pointing_center=fhd_obsid_pointing_altaz)
 # bd[roi_altaz] = backdrop.ravel()[roi_altaz]
 # pb[roi_altaz[roi_sector_altaz]] = PB.primary_beam_generator(altaz[roi_altaz[roi_sector_altaz],:], freq, telescope=telescope, skyunits='altaz', freq_scale='Hz', phase_center=fhd_obsid_pointing_altaz)
 bd[roi_altaz[roi_sector_altaz]] = backdrop.ravel()[
     roi_altaz[roi_sector_altaz]]
 overlay['pbeam'] = pb
 overlay['backdrop'] = bd
 overlay['roi_obj_inds'] = roi_altaz
 overlay['roi_sector_inds'] = roi_altaz[roi_sector_altaz]
 overlay['delay_map'] = NP.empty(
     (n_bins_baseline_orientation, xvect.size))
 overlay['delay_map'].fill(NP.nan)
 overlay['delay_map'][:, roi_altaz] = (
     DLY.geometric_delay(cardinal_bl,
                         altaz[roi_altaz, :],
                         altaz=True,
                         dircos=False,
                         hadec=False,
                         latitude=latitude) -
     DLY.geometric_delay(cardinal_bl,
                         pc,
                         altaz=False,
                         dircos=True,
                         hadec=False,
                         latitude=latitude)).T
 if use_CSM or use_SUMSS or use_NVSS or use_PS:
     src_hadec = NP.hstack(
         ((fhd_lst - ctlgobj.location[:, 0]).reshape(-1, 1),
          ctlgobj.location[:, 1].reshape(-1, 1)))
     src_altaz = GEOM.hadec2altaz(src_hadec, latitude, units='degrees')
     roi_src_altaz = NP.asarray(
         NP.where(src_altaz[:, 0] >= 0.0)).ravel()