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
Beispiel #2
0
def calc_jones(az, za, target_freq_Hz=205e6):
    za_rad = za * math.pi / 180.0
    az_rad = az * math.pi / 180.0
    za_rad = np.array([[za_rad]])
    az_rad = np.array([[az_rad]])

    gridpoint = mwa_sweet_spots.find_closest_gridpoint(az, za)
    print("Az = %.2f [deg], za = %.2f [deg], gridpoint = %d" % (az, za, gridpoint[0]))

    delays = gridpoint[4]
    delays = np.vstack((delays, delays))
    print(delays)
    print("-----------")

    Jones_FullEE = primary_beam.MWA_Tile_full_EE(za_rad,
                                                 az_rad,
                                                 target_freq_Hz,
                                                 delays=delays,
                                                 zenithnorm=True,
                                                 jones=True, interp=True)
    # swap axis to have Jones martix in the 1st
    Jones_FullEE_swap = np.swapaxes(np.swapaxes(Jones_FullEE, 0, 2), 1, 3)

    # TEST if equivalent to :
    Jones_FullEE_2D = Jones_FullEE_swap[:, :, 0, 0]
    # Jones_FullEE_2D = np.array([ [Jones_FullEE_swap[0, 0][0][0], Jones_FullEE_swap[0, 1][0][0]] , [Jones_FullEE_swap[1, 0][0][0], Jones_FullEE_swap[1, 1][0][0]] ])
    print("Jones FullEE:")
    print("----------------------")
    print(Jones_FullEE)
    print("----------------------")
    print(Jones_FullEE_2D)
    print("----------------------")

    beams = {}
    beams['XX'], beams['YY'] = primary_beam.MWA_Tile_full_EE(za_rad,
                                                             az_rad,
                                                             target_freq_Hz,
                                                             delays=delays,
                                                             zenithnorm=True,
                                                             power=True)
    print("Beams power = %.4f / %.4f" % (beams['XX'], beams['YY']))

    # Average Embeded Element model:
    print("size(delays) = %d" % np.size(delays))
    Jones_AEE = primary_beam.MWA_Tile_advanced(za_rad, az_rad, target_freq_Hz, delays=delays, jones=True)
    # Jones_AEE = primary_beam.MWA_Tile_advanced(np.array([[0]]), np.array([[0]]), target_freq_Hz,delays=delays, zenithnorm=True, jones=True)
    Jones_AEE_swap = np.swapaxes(np.swapaxes(Jones_AEE, 0, 2), 1, 3)
    Jones_AEE_2D = Jones_AEE_swap[:, :, 0, 0]
    # Jones_AEE_2D = np.array([ [Jones_AEE_swap[0, 0][0][0], Jones_AEE_swap[0, 1][0][0]], [Jones_AEE_swap[1, 0][0][0], Jones_AEE_swap[1, 1][0][0]] ])
    print("----------------------")
    print("Jones AEE:")
    print("----------------------")
    print(Jones_AEE)
    print("----------------------")
    print(Jones_AEE_2D)
    print("----------------------")

    Jones_Anal = primary_beam.MWA_Tile_analytic(za_rad,
                                                az_rad,
                                                target_freq_Hz,
                                                delays=delays,
                                                zenithnorm=True,
                                                jones=True)
    Jones_Anal_swap = np.swapaxes(np.swapaxes(Jones_Anal, 0, 2), 1, 3)
    Jones_Anal_2D = Jones_Anal_swap[:, :, 0, 0]

    print("----------------------  COMPARISON OF JONES MATRICES FEE vs. AEE ----------------------")
    print("Jones FullEE:")
    print("----------------------")
    print(Jones_FullEE_2D)
    print("----------------------")
    print()
    print("Jones AEE:")
    print("----------------------")
    print(Jones_AEE_2D)
    print("----------------------")
    print()
    print("----------------------")
    print("Jones Analytic:")
    print("----------------------")
    print(Jones_Anal_2D)
    print("----------------------")

    return (Jones_FullEE_2D, Jones_AEE_2D, Jones_Anal_2D)
Beispiel #3
0
def calc_ratio(dec, target_freq_Hz, gridpoint):
    za = dec + 26.7
    az = 0

    print("\n\n\n\n")
    print("################################# DEC = %.2f #################################" % dec)
    za_rad = za * math.pi / 180.0
    az_rad = az * math.pi / 180.0
    za_rad = np.array([[za_rad]])
    az_rad = np.array([[az_rad]])

    # non-realistic scenario - I am ~tracking the source with the closest sweetspot - whereas I should be staying at the same sweetspot
    #   gridpoint=find_closest_gridpoint(za)
    #
    print("dec=%.4f [deg] -> za=%.4f [deg] - %s" % (dec, za, gridpoint))
    delays = gridpoint[4]
    delays = np.vstack((delays, delays))
    print(delays)
    print("-----------")

    Jones_FullEE = primary_beam.MWA_Tile_full_EE(za_rad, az_rad, target_freq_Hz, delays=delays, zenithnorm=True,
                                                 jones=True, interp=True)
    # swap axis to have Jones martix in the 1st
    Jones_FullEE_swap = np.swapaxes(np.swapaxes(Jones_FullEE, 0, 2), 1, 3)

    # TEST if equivalent to :
    Jones_FullEE_2D = Jones_FullEE_swap[:, :, 0, 0]
    # Jones_FullEE_2D = np.array([ [Jones_FullEE_swap[0, 0][0][0], Jones_FullEE_swap[0, 1][0][0]], [Jones_FullEE_swap[1, 0][0][0], Jones_FullEE_swap[1, 1][0][0]] ])
    print("Jones FullEE:")
    print("----------------------")
    print(Jones_FullEE)
    print("----------------------")
    print(Jones_FullEE_2D)
    print("----------------------")

    #  Average Embeded Element model:
    print("size(delays) = %d" % np.size(delays))
    Jones_AEE = primary_beam.MWA_Tile_advanced(za_rad, az_rad, target_freq_Hz, delays=delays, jones=True)
    # Jones_AEE = primary_beam.MWA_Tile_advanced(np.array([[0]]), np.array([[0]]), target_freq_Hz, delays=delays, zenithnorm=True, jones=True)
    Jones_AEE_swap = np.swapaxes(np.swapaxes(Jones_AEE, 0, 2), 1, 3)
    Jones_AEE_2D = Jones_AEE_swap[:, :, 0, 0]
    # Jones_AEE_2D = np.array([ [Jones_AEE_swap[0, 0][0][0], Jones_AEE_swap[0, 1][0][0]], [Jones_AEE_swap[1, 0][0][0], Jones_AEE_swap[1, 1][0][0]] ])
    print("----------------------")
    print("Jones AEE:")
    print("----------------------")
    print(Jones_AEE)
    print("----------------------")
    print(Jones_AEE_2D)
    print("----------------------")

    # Analytical Model:
    # beams = {}
    # beams['XX'], beams['YY'] = primary_beam.MWA_Tile_analytic(za_rad, az_rad, target_freq_Hz, delays=delays, zenithnorm=True, jones=True)
    Jones_Anal = primary_beam.MWA_Tile_analytic(za_rad,
                                                az_rad,
                                                target_freq_Hz,
                                                delays=delays,
                                                zenithnorm=True,
                                                jones=True)
    Jones_Anal_swap = np.swapaxes(np.swapaxes(Jones_Anal, 0, 2), 1, 3)
    Jones_Anal_2D = Jones_Anal_swap[:, :, 0, 0]
    # Jones_Anal_2D = np.array([ [Jones_Anal_swap[0, 0][0][0], Jones_Anal_swap[0, 1][0][0]] , [Jones_Anal_swap[1, 0][0][0],Jones_Anal_swap[1, 1][0][0]] ])
    print("----------------------")
    print("Jones Analytic:")
    print("----------------------")
    print(Jones_Anal)
    print("----------------------")
    print(Jones_Anal_2D)
    print("----------------------")
    print("TEST:")
    print("----------------------")
    print("%.8f    %.8f" % (Jones_Anal_2D[0, 0], Jones_Anal_2D[0, 1]))
    print("%.8f    %.8f" % (Jones_Anal_2D[1, 0], Jones_Anal_2D[1, 1]))
    print("----------------------")

    # Use Jones_FullEE_2D as REAL sky and then ...
    B_sky = np.array([[1, 0], [0, 1]])
    Jones_FullEE_2D_H = np.transpose(Jones_FullEE_2D.conj())
    B_app = np.dot(Jones_FullEE_2D, np.dot(B_sky, Jones_FullEE_2D_H))  # E x B x E^H

    # test the procedure itself:
    Jones_FullEE_2D_H_Inv = inv2x2(Jones_FullEE_2D_H)
    Jones_FullEE_2D_Inv = inv2x2(Jones_FullEE_2D)
    B_sky_cal = np.dot(Jones_FullEE_2D_Inv, np.dot(B_app, Jones_FullEE_2D_H_Inv))
    print(B_sky)
    print("Recovered using FullEE model:")
    print(B_sky_cal)

    # calibrate back using AEE model :
    #   Jones_AEE_2D=Jones_Anal_2D # overwrite Jones_AEE_2D with Analytic to use it for calibration
    Jones_AEE_2D_H = np.transpose(Jones_AEE_2D.conj())
    Jones_AEE_2D_H_Inv = inv2x2(Jones_AEE_2D_H)
    Jones_AEE_2D_Inv = inv2x2(Jones_AEE_2D)
    B_sky_cal = np.dot(Jones_AEE_2D_Inv, np.dot(B_app, Jones_AEE_2D_H_Inv))
    print("Recovered using AEE model:")
    print(B_sky_cal)
    # I_cal = B_sky_cal[0, 0] + B_sky_cal[1, 1]
    #   print "FINAL : %.8f ratio = %.8f / 2 = %.8f" % (dec,abs(I_cal),(abs(I_cal)/2.00))
    # ratio = abs(B_sky_cal[0][0] / B_sky_cal[1][1])
    # FINAL VALUES for the paper to compare with Figure 4 in GLEAM paper :
    ratio_ms = B_sky_cal[0][0] / B_sky_cal[1][1]
    gleam_ratio = ratio_ms
    gleam_XX = B_sky_cal[0][0]
    gleam_YY = B_sky_cal[1][1]
    # gleam_q_leakage = (B_sky_cal[0][0] - B_sky_cal[1][1]) / (B_sky_cal[0][0] + B_sky_cal[1][1])
    print("DEBUG (DEC = %.2f deg) : GLEAM-ratio = %.4f = (%s / %s)" % (dec, gleam_ratio, gleam_XX, gleam_YY))

    return (gleam_ratio.real, gleam_XX, gleam_YY)
                                              az,
                                              target_freq_Hz,
                                              delays=delays,
                                              zenithnorm=options.zenithnorm,
                                              jones=True,
                                              interp=True)

        if options.wsclean_image:
            sign_uv = -1
    elif model == 'avg_EE' or model == 'advanced' or model == '2015' or model == 'AEE':
        logger.info("Correcting with AEE(%s) model at frequency %.2f Hz" %
                    (model, target_freq_Hz))
        # logging.getLogger("mwa_tile").setLevel(logging.DEBUG)
        Jones = primary_beam.MWA_Tile_advanced(za,
                                               az,
                                               target_freq_Hz,
                                               delays=delays,
                                               zenithnorm=options.zenithnorm,
                                               jones=True)
        if options.wsclean_image:
            sign_uv = -1
    elif model == 'analytic' or model == '2014':
        logger.info("Correcting with analytic model")
        Jones = primary_beam.MWA_Tile_analytic(za,
                                               az,
                                               target_freq_Hz,
                                               delays=delays,
                                               zenithnorm=options.zenithnorm,
                                               jones=True)
        if options.wsclean_image:
            sign_q = -1
    else:
Beispiel #5
0
def createArrayFactor(za, az, pixel_area, data):
    """
    Primary function to calculate the array factor with the given information.

    Input:
      delays - the beamformer delays required for point tile beam (should be a set of 16 numbers)
      time -  the time at which to evaluate the target position (as we need Azimuth and Zenith angle, which are time dependent)
      obsfreq - the centre observing frequency for the observation
      eff - the array efficiency (frequency and pointing dependent, currently require engineers to calculate for us...)
      flagged_tiles - the flagged tiles from the calibration solution (in the RTS format)
      xpos - x position of the tiles
      ypos - y position of the tiles
      zpos - z position of the tiles
      theta_res - the zenith angle resolution in degrees
      phi_res - the azimuth resolution in degrees
      za_chunk - list of ZA to compute array factor for
      write -  whether to actually write a file to disk
      rank - mpi rank (thread index)
    Return:
      results -  a list of lists cotaining [ZA, Az, beam power], for all ZA and Az in the given band
    """
    obsid = data['obsid']
    ra = data['ra']
    dec = data['dec']
    times = data['time']
    delays = data['delays']
    xpos = data['x']
    ypos = data['y']
    zpos = data['z']
    cable_delays = data['cd']
    theta_res = data['tres']
    phi_res = data['pres']
    beam_model = data['beam']
    coplanar = data['coplanar']
    ord_version = data['ord_version']
    no_delays = data['no_delays']
    plot_jobid = data['plot_jobid']

    # work out core depent part of data to process
    nfreq = len(data['freqs'])
    ifreq = rank % nfreq
    ichunk = rank // nfreq
    obsfreq = data['freqs'][ifreq]

    logger.info( "rank {:3d} Calculating phase of each sky position for each tile".format(rank))
    # calculate the relevent wavenumber for (theta,phi)
    #logger.info( "rank {:3d} Calculating wavenumbers".format(rank))
    if ord_version:
        #gx, gy, gz = calc_geometric_delay_distance((np.pi / 2) - az, za)
        gx, gy, gz = calc_geometric_delay_distance(az, za)
        logger.debug("rank {:3d} Wavenumbers shapes: {} {} {}".format(rank, gx.shape, gy.shape, gz.shape))

        # phase of each sky position for each tile
        ph_tile = cal_phase_ord(xpos, ypos, zpos, cable_delays, gx, gy, gz, obsfreq,
                                coplanar=coplanar, no_delays=no_delays)
        gx = gy = gz = None # Dereference for garbage collection
    else:
        kx, ky, kz = calcWaveNumbers(obsfreq, (np.pi / 2) - az, za)
        #logger.debug("kx[0] {} ky[0] {} kz[0] {}".format(kx[0], ky[0], kz[0]))
        logger.debug("rank {:3d} Wavenumbers shapes: {} {} {}".format(rank, kx.shape, ky.shape, kz.shape))

        # phase of each sky position for each tile
        ph_tile = calcSkyPhase(xpos, ypos, zpos, kx, ky, kz, coplanar=coplanar)
        kx = ky = kz = None # Dereference for garbage collection


    requests.get('https://ws.mwatelescope.org/progress/update',
                 params={'jobid':plot_jobid,
                         'workid':rank,
                         'current':2,
                         'total':5,
                         'desc':'Tile beam'})

    # calculate the tile beam at the given Az,ZA pixel
    logger.info( "rank {:3d} Calculating tile beam".format(rank))
    if beam_model == 'hyperbeam':
        # This method is no longer needed as mwa_pb uses hyperbeam
        jones = beam.calc_jones_array(az, za, obsfreq, delays, [1.0] * 16, True)
        jones = jones.reshape(za.shape[0], 1, 2, 2)
        vis = pb.mwa_tile.makeUnpolInstrumentalResponse(jones, jones)
        jones = None # Dereference for garbage collection
        tile_xpol, tile_ypol = (vis[:, :, 0, 0].real, vis[:, :, 1, 1].real)
        vis = None # Dereference for garbage collection
    elif beam_model == 'analytic':
        tile_xpol, tile_ypol = pb.MWA_Tile_analytic(za, az,
                                                freq=obsfreq, delays=[delays, delays],
                                                zenithnorm=True,
                                                power=True)
    elif beam_model == 'advanced':
        tile_xpol, tile_ypol = pb.MWA_Tile_advanced(za, az,
                                                freq=obsfreq, delays=[delays, delays],
                                                zenithnorm=True,
                                                power=True)
    elif beam_model == 'full_EE':
        tile_xpol, tile_ypol = pb.MWA_Tile_full_EE(za, az,
                                                freq=obsfreq, delays=np.array([delays, delays]),
                                                zenithnorm=True,
                                                power=True,
                                                interp=False)
    #logger.info("rank {:3d} Combining tile pattern".format(rank))
    tile_pattern = np.divide(np.add(tile_xpol, tile_ypol), 2.0)
    tile_pattern = tile_pattern.flatten()
    logger.debug("max(tile_pattern) {}".format(max(tile_pattern)))


    omega_A_times = []
    sum_B_T_times = []
    sum_B_times   = []
    phased_array_pattern_times = []
    for ti, time in enumerate(times):
        requests.get('https://ws.mwatelescope.org/progress/update',
                    params={'jobid':plot_jobid,
                            'workid':rank,
                            'current':3+ti,
                            'total'  :3+len(times),
                            'desc':'{}/{} array factor'.format(1+ti, len(times))})
        # get the target azimuth and zenith angle in radians and degrees
        # these are defined in the normal sense: za = 90 - elevation, az = angle east of North (i.e. E=90)
        logger.info( "rank {:3d} Calculating phase of each sky position for the target at time {}".format(rank, time))
        srcAz, srcZA, _, _ = getTargetAZZA(ra, dec, time)# calculate the target (kx,ky,kz)

        if ord_version:
            #t_gx, t_gy, t_gz = calc_geometric_delay_distance((np.pi / 2) - srcAz, srcZA)
            t_gx, t_gy, t_gz = calc_geometric_delay_distance(srcAz, srcZA)

            # phase of each sky position for each tile
            ph_target = cal_phase_ord(xpos, ypos, zpos, cable_delays, t_gx, t_gy, t_gz, obsfreq,
                                      coplanar=coplanar, no_delays=no_delays)
        else:
            target_kx, target_ky, target_kz = calcWaveNumbers(obsfreq, (np.pi / 2) - srcAz, srcZA)

            # Get phase of target for each tile phase of each sky position for each tile
            ph_target = calcSkyPhase(xpos, ypos, zpos, target_kx, target_ky, target_kz)

        # determine the interference pattern seen for each tile
        logger.info( "rank {:3d} Calculating array_factor".format(rank))
        #logger.debug("rank {:3d} ti: {} ph_tile {}".format(rank, ti, ph_tile))
        #logger.debug("rank {:3d} ti: {} ph_target {}".format(rank, ti, ph_target))
        array_factor, array_factor_power = calcArrayFactor(ph_tile, ph_target)
        ph_target = None # Dereference for garbage collection

        #logger.debug("array_factor_power[0] {}".format(array_factor_power[0]))
        logger.debug("rank {:3d} array_factor[0] {}".format(rank, array_factor[0]))
        logger.debug("rank {:3d} array_factor shapes: {}".format(rank, array_factor.shape))
        logger.debug("rank {:3d} array factor maximum = {}".format(rank, np.amax(array_factor_power)))

        # calculate the phased array power pattern
        logger.info( "rank {:3d} Calculating phased array pattern".format(rank))
        logger.debug("rank {:3d} tile_pattern.shape {} array_factor_power.shape {}".format(rank, tile_pattern.shape, array_factor_power.shape))
        phased_array_pattern = np.multiply(tile_pattern, array_factor_power)
        #phased_array_pattern = tile_pattern[0][0] * np.abs(array_factor)**2 # indexing due to tile_pattern now being a 2-D array
        phased_array_pattern_times.append(phased_array_pattern)

        # add this contribution to the beam solid angle
        logger.info( "rank {:3d} Calculating omega_A".format(rank))
        #omega_A_array = np.sin(za) * array_factor_power * np.radians(theta_res) * np.radians(phi_res)
        #omega_A_array = np.multiply(np.multiply(np.multiply(np.sin(za), array_factor_power), np.radians(theta_res)), np.radians(phi_res))
        omega_A_array = np.multiply(array_factor_power, pixel_area)
        logger.debug("rank {:3d} omega_A_array shapes: {}".format(rank, omega_A_array.shape))
        omega_A = np.sum(omega_A_array)
        logger.debug("rank {:3d} freq {:.2f}MHz beam_area: {}".format(rank, obsfreq/1e6, omega_A))
        omega_A_times.append(omega_A)
        #logger.debug("omega_A[1] vals: za[1] {}, array_factor_power[1] {}, theta_res {}, phi_res {}".format(za[1], array_factor_power[1], theta_res, phi_res))
        #logger.debug("omega_A[1] {}".format(omega_A_array[1]))

        # Do a partial sum over the sky and frequency to be finished outside of the function
        sum_B_T, sum_B = partial_convolve_sky_map(az, za, pixel_area, phased_array_pattern, obsfreq, time)
        sum_B_T_times.append(sum_B_T)
        sum_B_times.append(sum_B)
    ph_tile = None # Dereference for garbage collection

    # Average over time
    #omega_A = np.average(omega_A_times)
    #sum_B_T = np.average(sum_B_T_times)
    #sum_B   = np.average(sum_B_times)
    #phased_array_pattern = np.average(phased_array_pattern_times, axis=0)
    logger.debug("rank {:3d} phased_array_pattern: {}".format(rank, phased_array_pattern))
    logger.debug("rank {:3d} omega_A_times: {}".format(rank, omega_A_times))
    logger.info("rank {:3d} done".format(rank))

    # return lists of results for each time step
    return np.array(omega_A_times), np.array(sum_B_T_times), np.array(sum_B_times), np.array(phased_array_pattern_times)
def get_beam_power_over_time(names_ra_dec,
                             common_metadata=None,
                             dt=296,
                             centeronly=True,
                             verbose=False,
                             option='analytic',
                             degrees=False,
                             start_time=0):
    """Calculates the zenith normalised power for each source over time.

    Parameters
    ----------
    names_ra_dec : `list`
        An array in the format [[source_name, RAJ, DecJ]]
    common_metadata : `list`, optional
        The list of common metadata generated from :py:meth:`vcstools.metadb_utils.get_common_obs_metadata`
    dt : `int`, optional
        The time interval of how often powers are calculated. |br| Default: 296.
    centeronly : `boolean`, optional
        Only calculates for the centre frequency. |br| Default: `True`.
    verbose : `boolean`, optional
        If `True` will not supress the output from mwa_pb. |br| Default: `False`.
    option : `str`, optional
        The primary beam model to use out of [analytic, advanced, full_EE, hyperbeam]. |br| Default: analytic.
    degrees : `boolean`, optional
        If true assumes RAJ and DecJ are in degrees. |br| Default: `False`.
    start_time : `int`, optional
        The time in seconds from the begining of the observation to start calculating at. |br| Default: 0.

    Returns
    -------
    Powers : `numpy.array`, (len(names_ra_dec), ntimes, nfreqs)
        The zenith normalised power for each source over time.
    """
    if common_metadata is None:
        common_metadata = get_common_obs_metadata(obsid)
    obsid, _, _, time, delays, centrefreq, channels = common_metadata
    names_ra_dec = np.array(names_ra_dec)
    amps = [1.0] * 16
    logger.debug("Calculating beam power for OBS ID: {0}".format(obsid))

    if option == 'hyperbeam':
        if "mwa_hyperbeam" not in sys.modules:
            logger.error(
                "mwa_hyperbeam not installed so can not use hyperbeam to create a beam model. Exiting"
            )
            sys.exit(1)
        beam = mwa_hyperbeam.FEEBeam(config.h5file)

    # Work out time steps to calculate over
    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)

    # Work out frequency steps
    if centeronly:
        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
        nfreqs = 1
    else:
        # in Hz
        frequencies = np.array(channels) * 1.28e6
        nfreqs = len(channels)

    # Set up np power array
    PowersX = np.zeros((len(names_ra_dec), ntimes, nfreqs))
    PowersY = np.zeros((len(names_ra_dec), ntimes, nfreqs))

    # Convert RA and Dec to desired units
    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])
    # Then check if they're valid
    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(nfreqs):
            #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)
            elif option == 'hyperbeam':
                jones = beam.calc_jones_array(phi, theta,
                                              int(frequencies[ifreq]),
                                              delays[0], amps, True)
                jones = jones.reshape(1, len(phi), 2, 2)
                vis = primary_beam.mwa_tile.makeUnpolInstrumentalResponse(
                    jones, jones)
                rX, rY = (vis[:, :, 0, 0].real, vis[:, :, 1, 1].real)
        PowersX[:, itime, ifreq] = rX
        PowersY[:, itime, ifreq] = rY
    if verbose is False:
        sys.stdout = sys.__stdout__
    Powers = 0.5 * (PowersX + PowersY)
    return Powers