def test_mwa_alt_az_za(): """Test the mwa_alt_az_za function""" # obsid, alt, az, za tests = [(1117101752, -71.10724927808731, 145.74310748819693, 161.1072492780873), (1252177744, -14.709910184536241, 264.22976419794514, 104.70991018453624), (1247832024, 68.90133642304133, 161.50105995238945, 21.09866357695867), (1188439520, 60.78396503767497, 161.03537536398974, 29.216034962325033)] for obsid, exp_alt, exp_az, exp_za in tests: alt, az, za = mwa_alt_az_za(obsid) assert_almost_equal(alt, exp_alt, decimal=5) assert_almost_equal(az, exp_az, decimal=5) assert_almost_equal(za, exp_za, decimal=5)
def get_obs_metadata(obs): beam_meta_data = getmeta(service='obs', params={'obs_id': obs}) channels = beam_meta_data[u'rfstreams'][u"0"][u'frequencies'] freqs = [float(c) * 1.28 for c in channels] xdelays = beam_meta_data[u'rfstreams'][u"0"][u'xdelays'] #pythodelays = beam_meta_data[u'rfstreams'][u"0"][u'xdelays'] ydelays = beam_meta_data[u'rfstreams'][u"0"][u'ydelays'] _, pointing_AZ, pointing_ZA = mwa_alt_az_za(obs) return { "channels": channels, "frequencies": freqs, "xdelays": xdelays, "ydelays": ydelays, "az": pointing_AZ, "za": pointing_ZA }
def plot_beam_pattern(obsid, obsfreq, obstime, ra, dec, cutoff=0.1): # extra imports from MWA_Tools to access database and beam models from mwa_pb import primary_beam as pb _az = np.linspace(0, 360, 3600) _za = np.linspace(0, 90, 900) az, za = np.meshgrid(_az, _za) ## TARGET TRACKING ## times = Time(obstime, format='gps') target = SkyCoord(ra, dec, unit=(u.hourangle, u.deg)) location = EarthLocation(lat=-26.7033 * u.deg, lon=116.671 * u.deg, height=377.827 * u.m) altaz = target.transform_to(AltAz(obstime=times, location=location)) targetAZ = altaz.az.deg targetZA = 90 - altaz.alt.deg colours = cm.viridis(np.linspace(1, 0, len(targetAZ))) ## MWA BEAM CALCULATIONS ## delays = get_common_obs_metadata(obsid)[ 4] # x-pol and y-pol delays for obsid _, ptAZ, ptZA = mwa_alt_az_za(obsid) #ptAZ, _, ptZA = dbq.get_beam_pointing(obsid) # Az and ZA in degrees for obsid logger.info("obs pointing: ({0}, {1})".format(ptAZ, ptZA)) xp, yp = pb.MWA_Tile_analytic(np.radians(za), np.radians(az), obsfreq * 1e6, delays=delays, zenithnorm=True) #interp=True) pattern = np.sqrt((xp + yp) / 2.0).real # sum to get "total intensity" logger.debug("Pattern: {0}".format(pattern)) pmax = pattern.max() hpp = 0.5 * pmax # half-power point logger.info("tile pattern maximum: {0:.3f}".format(pmax)) logger.info("tile pattern half-max: {0:.3f}".format(hpp)) pattern[np.where(pattern < cutoff)] = 0 # ignore everything below cutoff # figure out the fwhm fwhm_idx = np.where((pattern > 0.498 * pmax) & (pattern < 0.502 * pmax)) fwhm_az_idx = fwhm_idx[1] fwhm_za_idx = fwhm_idx[0] # collapse beam pattern along axes pattern_ZAcol = pattern.mean(axis=0) pattern_AZcol = pattern.mean(axis=1) # figure out beam pattern value at target tracking points track_lines = [] logger.info("beam power at target track points:") for ta, tz in zip(targetAZ, targetZA): xp, yp = pb.MWA_Tile_analytic(np.radians([[tz]]), np.radians([[ta]]), obsfreq * 1e6, delays=delays, zenithnorm=True) #interp=False bp = (xp + yp) / 2 track_lines.append(bp[0]) logger.info("({0:.2f},{1:.2f}) = {2:.3f}".format(ta, tz, bp[0][0])) ## PLOTTING ## fig = plt.figure(figsize=(10, 8)) gs = gridspec.GridSpec(4, 4) axP = plt.subplot(gs[1:, 0:3]) axAZ = plt.subplot(gs[0, :3]) axZA = plt.subplot(gs[1:, 3]) axtxt = plt.subplot(gs[0, 3]) # info text in right-hand corner axis axtxt.axis('off') infostr = """Obs ID: {0} Frequency: {1:.2f}MHz Beam Pmax: {2:.3f} Beam half-Pmax: {3:.3f} """.format(obsid, obsfreq, pmax, hpp) axtxt.text(0.01, 0.5, infostr, verticalalignment='center') logger.debug("az: {0}, za: {1}, pattern: {2}, pmax: {3}".format( az, za, pattern, pmax)) # plot the actual beam patter over sky p = axP.contourf(az, za, pattern, 100, cmap=plt.get_cmap('gist_yarg'), vmax=pmax) # plot beam contours axP.scatter(_az[fwhm_az_idx], _za[fwhm_za_idx], marker='.', s=1, color='r') # plot the fwhm border axP.plot(ptAZ, ptZA, marker="+", ms=8, ls="", color='C0') # plot the tile beam pointing for ta, tz, c in zip(targetAZ, targetZA, colours): axP.plot(ta, tz, marker="x", ms=8, ls="", color=c) # plot the target track through the beam axP.set_xlim(0, 360) axP.set_ylim(0, 90) axP.set_xticks(np.arange(0, 361, 60)) axP.set_xlabel("Azimuth (deg)") axP.set_ylabel("Zenith angle (deg)") # setup and configure colourbar cbar_ax = fig.add_axes([0.122, -0.01, 0.58, 0.03]) cbar = plt.colorbar(p, cax=cbar_ax, orientation='horizontal', label="Zenith normalised power") cbar.ax.plot(hpp / pmax, [0.5], 'r.') for l, c in zip(track_lines, colours): cbar.ax.plot([l / pmax, l / pmax], [0, 1], color=c, lw=1.5) # plot collapsed beam patterns: axAZ.plot(_az, pattern_ZAcol, color='k') # collapsed along ZA for ta, c in zip(targetAZ, colours): # draw tracking points axAZ.axvline(ta, color=c) axAZ.set_xlim(0, 360) axAZ.set_xticks(np.arange(0, 361, 60)) axAZ.set_xticklabels([]) axAZ.set_yscale('log') axZA.plot(pattern_AZcol, _za, color='k') # collapsed along AZ for tz, c in zip(targetZA, colours): # draw tracking points axZA.axhline(tz, color=c) axZA.set_ylim(0, 90) axZA.set_yticklabels([]) axZA.set_xscale('log') plt.savefig("{0}_{1:.2f}MHz_flattile.png".format(obsid, obsfreq), bbox_inches='tight')
ch_offset = channels[-1] - channels[0] if ch_offset != nchans - 1: logger.error( "Picket fence observation - cannot combine picket fence incoherent sum data (with this script)" ) sys.exit(1) bw = nchans * 1.28 cfreq = (1.28 * min(channels) - 0.64) + bw / 2.0 logger.info("Centre frequency: {0} MHz".format(cfreq)) logger.info("Bandwidth: {0} MHz".format(bw)) logger.info("Acquiring pointing position information") ra, dec = metadata[1:3] alt, az, za = mwa_alt_az_za(args.obsID, ra=ra, dec=dec) user = getpass.getuser() os.system("mkdir -p {ics_dir}".format(ics_dir=ics_dir)) # TODO: this is bloody awful, surely there's a better way to do this? make_command = 'cd {ics_dir} && echo -e "{ics_dir}/mk_psrfits_in\n' \ '\n' \ '{obsid}\n' \ '\n' \ '{nfiles}\n' \ '{user}\n' \ '\n' \ '{obsid}\n' \ '\n' \ '\n' \
def get_beam_power_over_time(beam_meta_data, names_ra_dec, dt=296, centeronly=True, verbose=False, option='analytic', degrees=False, start_time=0): """ Calulates the power (gain at coordinate/gain at zenith) for each source over time. get_beam_power_over_time(beam_meta_data, names_ra_dec, dt=296, centeronly=True, verbose=False, option = 'analytic') Args: beam_meta_data: [obsid,ra, dec, time, delays,centrefreq, channels] obsid metadata obtained from meta.get_common_obs_metadata names_ra_dec: and array in the format [[source_name, RAJ, DecJ]] dt: time step in seconds for power calculations (default 296) centeronly: only calculates for the centre frequency (default True) verbose: prints extra data to (default False) option: primary beam model [analytic, advanced, full_EE] start_time: the time in seconds from the begining of the observation to start calculating at """ obsid, _, _, time, delays, centrefreq, channels = beam_meta_data names_ra_dec = np.array(names_ra_dec) logger.info("Calculating beam power for OBS ID: {0}".format(obsid)) starttimes = np.arange(start_time, time + start_time, dt) stoptimes = starttimes + dt stoptimes[stoptimes > time] = time Ntimes = len(starttimes) midtimes = float(obsid) + 0.5 * (starttimes + stoptimes) if not centeronly: PowersX = np.zeros((len(names_ra_dec), Ntimes, len(channels))) PowersY = np.zeros((len(names_ra_dec), Ntimes, len(channels))) # in Hz frequencies = np.array(channels) * 1.28e6 else: PowersX = np.zeros((len(names_ra_dec), Ntimes, 1)) PowersY = np.zeros((len(names_ra_dec), Ntimes, 1)) if centrefreq > 1e6: logger.warning( "centrefreq is greater than 1e6, assuming input with units of Hz." ) frequencies = np.array([centrefreq]) else: frequencies = np.array([centrefreq]) * 1e6 if degrees: RAs = np.array(names_ra_dec[:, 1], dtype=float) Decs = np.array(names_ra_dec[:, 2], dtype=float) else: RAs, Decs = sex2deg(names_ra_dec[:, 1], names_ra_dec[:, 2]) if len(RAs) == 0: sys.stderr.write('Must supply >=1 source positions\n') return None if not len(RAs) == len(Decs): sys.stderr.write('Must supply equal numbers of RAs and Decs\n') return None if verbose is False: #Supress print statements of the primary beam model functions sys.stdout = open(os.devnull, 'w') for itime in range(Ntimes): # this differ's from the previous ephem_utils method by 0.1 degrees _, Azs, Zas = mwa_alt_az_za(midtimes[itime], ra=RAs, dec=Decs, degrees=True) # go from altitude to zenith angle theta = np.radians(Zas) phi = np.radians(Azs) for ifreq in range(len(frequencies)): #Decide on beam model if option == 'analytic': rX, rY = primary_beam.MWA_Tile_analytic( theta, phi, freq=frequencies[ifreq], delays=delays, zenithnorm=True, power=True) elif option == 'advanced': rX, rY = primary_beam.MWA_Tile_advanced( theta, phi, freq=frequencies[ifreq], delays=delays, zenithnorm=True, power=True) elif option == 'full_EE': rX, rY = primary_beam.MWA_Tile_full_EE(theta, phi, freq=frequencies[ifreq], delays=delays, zenithnorm=True, power=True) PowersX[:, itime, ifreq] = rX PowersY[:, itime, ifreq] = rY if verbose is False: sys.stdout = sys.__stdout__ Powers = 0.5 * (PowersX + PowersY) return Powers
def find_t_sys_gain(pulsar, obsid, beg=None, t_int=None, p_ra=None, p_dec=None,\ obs_metadata=None, query=None, trcvr="/group/mwaops/PULSAR/MWA_Trcvr_tile_56.csv"): """ Finds the system temperature and gain for an observation. A function snippet originally written by Nick Swainston - adapted for general VCS use. Parameters: ----------- pulsar: str the J name of the pulsar. e.g. J2241-5236 obsid: int The observation ID. e.g. 1226406800 beg: int The beginning of the observing time t_int: float The total time that the target is in the beam p_ra: str OPTIONAL - the target's right ascension p_dec: str OPTIONAL - the target's declination obs_metadata: list OPTIONAL - the array generated from mwa_metadb_utils.get_common_obs_metadata(obsid) query: object OPTIONAL - The return of the psrqpy function for this pulsar trcvr: str The location of the MWA receiver temp csv file. Default = '/group/mwaops/PULSAR/MWA_Trcvr_tile_56.csv' Returns: -------- t_sys: float The system temperature t_sys_err: float The system temperature's uncertainty gain: float The system gain gain_err: float The gain's uncertainty """ #get ra and dec if not supplied if p_ra is None or p_dec is None and query is None: logger.debug("Obtaining pulsar RA and Dec from ATNF") query = psrqpy.QueryATNF(psrs=[pulsar], loadfromdb=ATNF_LOC).pandas p_ra = query["RAJ"][0] p_dec = query["DECJ"][0] elif query is not None: query = psrqpy.QueryATNF(psrs=[pulsar], loadfromdb=ATNF_LOC).pandas p_ra = query["RAJ"][0] p_dec = query["DECJ"][0] #get metadata if not supplied if obs_metadata is None: logger.debug("Obtaining obs metadata") obs_metadata = mwa_metadb_utils.get_common_obs_metadata(obsid) obsid, obs_ra, obs_dec, _, delays, centrefreq, channels = obs_metadata #get beg if not supplied if beg is None or t_int is None: logger.debug("Calculating beginning time for pulsar coverage") beg, _, t_int = find_times(obsid, pulsar, beg=beg) #Find 'start_time' for fpio - it's usually about 7 seconds #obs_start, _ = mwa_metadb_utils.obs_max_min(obsid) start_time = beg - int(obsid) #Get important info trec_table = Table.read(trcvr, format="csv") ntiles = 128 #TODO actually we excluded some tiles during beamforming, so we'll need to account for that here beam_power = fpio.get_beam_power_over_time([obsid, obs_ra, obs_dec, t_int, delays,\ centrefreq, channels],\ np.array([[pulsar, p_ra, p_dec]]),\ dt=100, start_time=start_time) beam_power = np.mean(beam_power) # Usa a primary beam function to convolve the sky temperature with the primary beam # (prints suppressed) sys.stdout = open(os.devnull, 'w') _, _, Tsky_XX, _, _, _, Tsky_YY, _ = pbtant.make_primarybeammap( int(obsid), delays, centrefreq * 1e6, 'analytic', plottype='None') sys.stdout = sys.__stdout__ #TODO can be inaccurate for coherent but is too difficult to simulate t_sky = (Tsky_XX + Tsky_YY) / 2. # Get T_sys by adding Trec and Tsky (other temperatures are assumed to be negligible t_sys_table = t_sky + submit_to_database.get_Trec(trec_table, centrefreq) t_sys = np.mean(t_sys_table) t_sys_err = t_sys * 0.02 #TODO: figure out what t_sys error is logger.debug("pul_ra: {} pul_dec: {}".format(p_ra, p_dec)) _, _, zas = mwa_metadb_utils.mwa_alt_az_za(obsid, ra=p_ra, dec=p_dec) theta = np.radians(zas) gain = submit_to_database.from_power_to_gain(beam_power, centrefreq * 1e6, ntiles, coh=True) logger.debug("beam_power: {} theta: {} pi: {}".format( beam_power, theta, np.pi)) gain_err = gain * ((1. - beam_power) * 0.12 + 2. * (theta / (0.5 * np.pi))**2. + 0.1) # Removed the below error catch because couldn't find an obs that breaks it #sometimes gain_err is a numpy array and sometimes it isnt so i have to to this... #try: # gain_err.shape # gain_err = gain_err[0] #except: # pass return t_sys, t_sys_err, gain, gain_err