Example #1
0
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
def main():
    usage = "Usage: %prog [options]\n"
    usage += "\tCreates an image of the 408 MHz sky (annoted with sources) that includes contours for the MWA primary beam\n"
    usage += "\tThe beam is monochromatic, and is the sum of the XX and YY beams\n"
    usage += "\tThe date/time (UT) and beamformer delays must be specified\n"
    usage += "\tBeamformer delays should be separated by commas\n"
    usage += "\tFrequency is in MHz, or a coarse channel number (can also be comma-separated list)\n"
    usage += "\tDefault is to plot centered on RA=0, but if -r/--racenter, will center on LST\n"
    usage += "\tContours will be plotted at %s of the peak\n" % contourlevels
    usage += "\tExample:\tpython primarybeammap.py -c 98 --beamformer=1,0,0,0,3,3,3,3,6,6,6,6,9,9,9,8 \n\n"

    parser = OptionParser(usage=usage)
    parser.add_option('-c',
                      '--channel',
                      dest='channel',
                      default=None,
                      help='Center channel(s) of observation')
    parser.add_option('-f',
                      '--frequency',
                      dest='frequency',
                      default=None,
                      help='Center frequency(s) of observation [MHz]')
    parser.add_option('-b',
                      '--beamformer',
                      dest='delays',
                      default="0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
                      help='16 beamformer delays separated by commas')
    parser.add_option('-D',
                      '--date',
                      dest='date',
                      default=None,
                      help='UT Date')
    parser.add_option('-t',
                      '--time',
                      dest='time',
                      default=None,
                      help='UT Time')
    parser.add_option('-g', '--gps', dest='gps', default=None, help='GPS time')
    parser.add_option(
        '-m',
        '--model',
        dest='model',
        default='analytic',
        help='beam model: analytic, advanced, full_EE, full_EE_AAVS05')
    parser.add_option(
        '-p',
        '--plottype',
        dest='plottype',
        default='beamsky',
        help='Type of plot: all, beam, sky, beamsky, beamsky_scaled')
    parser.add_option('--title', dest='title', default=None, help='Plot title')
    parser.add_option('-e',
                      '--ext',
                      dest='extension',
                      default='png',
                      help='Plot extension [default=%default]')
    parser.add_option('-r',
                      '--racenter',
                      action="store_true",
                      dest="center",
                      default=False,
                      help="Center on LST?")
    parser.add_option('-s',
                      '--sunline',
                      dest="sunline",
                      default="1",
                      choices=['0', '1'],
                      help="Plot sun [default=%default]")
    parser.add_option('--tle',
                      dest='tle',
                      default=None,
                      help='Satellite TLE file')
    parser.add_option('--duration',
                      dest='duration',
                      default=300,
                      type=int,
                      help='Duration for plotting satellite track')
    parser.add_option('--size',
                      dest='size',
                      default=1000,
                      type=int,
                      help='Resolution of created beam file')
    parser.add_option('--dir',
                      dest='dir',
                      default=None,
                      help='output directory')

    parser.add_option('-v',
                      '--verbose',
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Increase verbosity of output")

    (options, args) = parser.parse_args()

    if options.dir is not None:
        mkdir_p(options.dir)

    if options.frequency is not None:
        if (',' in options.frequency):
            try:
                frequency = list(map(float, options.frequency.split(',')))
            except ValueError:
                logger.error("Could not parse frequency %s\n" %
                             options.frequency)
                sys.exit(1)
        else:
            try:
                frequency = float(options.frequency)
            except ValueError:
                logger.error("Could not parse frequency %s\n" %
                             options.frequency)
                sys.exit(1)
    else:
        frequency = options.frequency
    if options.channel is not None:
        if (',' in options.channel):
            try:
                channel = list(map(float, options.channel.split(',')))
            except ValueError:
                logger.error("Could not parse channel %s\n" % options.channel)
                sys.exit(1)
        else:
            try:
                channel = float(options.channel)
            except ValueError:
                logger.error("Could not parse channel %s\n" % options.channel)
                sys.exit(1)
    else:
        channel = options.channel

    if options.delays is not None:
        try:
            if (',' in options.delays):
                delays = list(map(int, options.delays.split(',')))
            else:
                delays = 16 * [int(options.delays)]
        except ValueError:
            logger.error("Could not parse beamformer delays %s\n" %
                         options.delays)
            sys.exit(1)
    else:
        delays = options.delays

    extension = options.extension
    plottype = options.plottype
    model = options.model
    if model not in [
            'analytic', 'advanced', 'full_EE', 'full_EE_AAVS05', '2016',
            '2015', '2014'
    ]:
        logger.error("Model %s not found\n" % model)
        sys.exit(1)
    if plottype not in ['all', 'beam', 'sky', 'beamsky', 'beamsky_scaled']:
        logger.error("Plot type %s not found\n" % plottype)
        sys.exit(1)
    gpsstring = options.gps
    gps = int(gpsstring)

    if (len(delays) < 16):
        logger.error("Must supply 1 or 16 delays\n")
        sys.exit(1)
    if (frequency is None):
        if (channel is not None):
            if (isinstance(channel, list)):
                frequency = list(
                    1.28 * numpy.array(channel)
                )  # multiplication by 1e6 is done later at line Convert to Hz
            else:
                frequency = 1.28 * channel  # multiplication by 1e6 is done later at line Convert to Hz
    if frequency is None:
        logger.error("Must supply frequency or channel\n")
        sys.exit(1)
    if (isinstance(frequency, int) or isinstance(frequency, float)):
        frequency = [frequency]
    frequency = numpy.array(frequency) * 1e6  # Convert to Hz

    for freq in frequency:
        print('frequency', freq)
        result = make_primarybeammap(gps,
                                     delays,
                                     freq,
                                     model=model,
                                     plottype=plottype,
                                     extension=extension,
                                     resolution=options.size,
                                     directory=options.dir)
        if (result is not None):
            print("Wrote %s" % result)
Example #3
0
#!/usr/bin/env python
"""
Script for calculating A/T for MWA 
Script calling function primarybeammap_tant.py to calculate antenna temperature according to MWA beam model (analytic, AEE or FEE) and scaled Haslam map
Example usage:
python ./mwa_sensitivity.py -b 18,13,8,3,17,12,7,2,16,11,6,1,15,10,5,0 -c 169 -p all -g 0  -m full_EE
python ./mwa_sensitivity.py -c 169 -p all -g 0  -m full_EE

Starting version by Marcin Sokolowski

main task is:
make_primarybeammap()

This is the script interface to the functions and modules defined in MWA_Tools/src/primarybeamap.py

"""

import errno
import math
from optparse import OptionParser
import os
import sys

from astropy.io import fits as pyfits

import numpy as np

from mwa_pb.primarybeammap_tant import contourlevels, get_beam_power, logger, make_primarybeammap
from mwa_pb import mwa_sweet_spots
from mwa_pb import metadata
Example #4
0
def calculate_sensitivity(freq,
                          delays,
                          gps,
                          trcv_type,
                          T_rcv,
                          size,
                          dirname,
                          model,
                          plottype,
                          extension,
                          pointing_az_deg=0,
                          pointing_za_deg=0,
                          add_sources=False,
                          zenithnorm=True,
                          antnum=128,
                          inttime=120,
                          bandwidth=1280000):
    freq_mhz = freq / 1e6
    print 'frequency=%.2f -> delays=%s' % (freq, delays)

    # if trcv_type',default='trcv_from_skymodel_with_err
    if trcv_type != "value":
        if trcv_type == "trcv_from_skymodel_with_err":
            T_rcv = trcv_from_skymodel_with_err(freq_mhz)
            print "T_rcv calculated from trcv_from_skymodel_with_err = %.2f K" % (
                T_rcv)

    result = make_primarybeammap(gps,
                                 delays,
                                 freq,
                                 model=model,
                                 plottype=plottype,
                                 extension=extension,
                                 resolution=size,
                                 directory=dirname,
                                 zenithnorm=zenithnorm,
                                 b_add_sources=add_sources)
    (beamsky_sum_XX, beam_sum_XX, Tant_XX, beam_dOMEGA_sum_XX, beamsky_sum_YY,
     beam_sum_YY, Tant_YY, beam_dOMEGA_sum_YY) = result

    beams = get_beam_power(delays,
                           freq,
                           model=model,
                           pointing_az_deg=pointing_az_deg,
                           pointing_za_deg=pointing_za_deg,
                           zenithnorm=zenithnorm)

    gain_XX = beams['XX'] / (beam_dOMEGA_sum_XX / (4.00 * math.pi))
    gain_YY = beams['YY'] / (beam_dOMEGA_sum_YY / (4.00 * math.pi))

    ant_efficiency = 1.00
    aeff_XX = (7161.97 / (freq_mhz * freq_mhz)) * (gain_XX * ant_efficiency)
    aeff_YY = (7161.97 / (freq_mhz * freq_mhz)) * (gain_YY * ant_efficiency)

    T_sys_XX = (Tant_XX + T_rcv)
    T_sys_YY = (Tant_YY + T_rcv)

    sens_XX = aeff_XX / T_sys_XX
    sens_YY = aeff_YY / T_sys_YY

    sefd_XX = (2760.00 / sens_XX)  # 2k/(A/T)
    sefd_YY = (2760.00 / sens_YY)  # 2k/(A/T)

    noise_XX = sefd_XX / math.sqrt(bandwidth * inttime * antnum * (antnum - 1))
    noise_YY = sefd_YY / math.sqrt(bandwidth * inttime * antnum * (antnum - 1))

    print "%.2f Hz :" % (freq)

    lstring = "\t\tXX (%.2f MHz) : T_ant_XX = %.2f  = (%.8f / %.8f) , beam(%.4f,%.4f)=%.8f , gain=%.8f , aeff=%.8f, "
    lstring += "sensitivity (A/T) = %.20f -> SEFD_XX = %.2f Jy -> noise_XX = %.4f Jy"
    params = (freq_mhz, Tant_XX, beamsky_sum_XX, beam_sum_XX, pointing_az_deg,
              pointing_za_deg, beams['XX'], gain_XX, aeff_XX, sens_XX, sefd_XX,
              noise_XX)
    print lstring % params

    lstring = "\t\tYY (%.2f MHz) : T_ant_YY = %.2f  = (%.8f / %.8f) , beam(%.4f,%.4f)=%.8f , gain=%.8f , aeff=%.8f, "
    lstring += "sensitivity (A/T) = %.20f -> SEFD_YY = %.2f Jy -> noise_YY = %.4f Jy"
    params = (freq_mhz, Tant_YY, beamsky_sum_YY, beam_sum_YY, pointing_az_deg,
              pointing_za_deg, beams['YY'], gain_YY, aeff_YY, sens_YY, sefd_YY,
              noise_YY)
    print lstring % params

    print "Noise expected on XX images = %.4f Jy" % noise_XX
    print "Noise expected on YY images = %.4f Jy" % noise_YY

    return (aeff_XX, T_sys_XX, sens_XX, sefd_XX, noise_XX, aeff_YY, T_sys_YY,
            sens_YY, sefd_YY, noise_YY)
Example #5
0
def find_t_sys_gain(pulsar, obsid,
                    p_ra=None, p_dec=None,
                    dect_beg=None, dect_end=None,
                    obs_beg=None, obs_end=None,
                    common_metadata=None, full_metadata=None,
                    query=None, min_z_power=0.3, trcvr=data_load.TRCVR_FILE):
    """Finds the system temperature and gain for an observation.

    Parameters
    ----------
    pulsar : `str`
        The Jname of the pulsar.
    obsid : `int`
        The MWA Observation ID.
    p_ra, p_dec : `str`, optional
        The target's right ascension and declination in sexidecimals. If not supplied will use the values from the ANTF.
    dect_beg, dect_end : `int`, optional
        The beg and end GPS time of the detection to calculate over.
        If not supplied will estimate beam enter and exit.
    obs_beg, obs_end : `int`, optional
        Beginning and end GPS time of the observation.
        If not supplied will use :py:meth:`vcstools.metadb_utils.obs_max_min` to find it.
    common_metadata : `list`, optional
        The list of common metadata generated from :py:meth:`vcstools.metadb_utils.get_common_obs_metadata`.
    full_metadata : `dict`, optional
        The dictionary of metadata generated from :py:meth:`vcstools.metadb_utils.getmeta`.
    query : psrqpy object, optional
        A previous psrqpy.QueryATNF query. Can be supplied to prevent performing a new query.
    min_z_power : `float`, optional
        Zenith normalised power cut off. |br| Default: 0.3.
    trcvr : `str`
        The location of the MWA receiver temp csv file. |br| Default: <vcstools_data_dir>MWA_Trcvr_tile_56.csv

    Returns
    -------
    t_sys : `float`
        The system temperature in K.
    t_sys_err : `float`
        The system temperature's uncertainty.
    gain : `float`
        The system gain in K/Jy.
    gain_err : `float`
        The gain's uncertainty.
    """
    # get ra and dec if not supplied
    if query is None:
        logger.debug("Obtaining pulsar RA and Dec from ATNF")
        query = psrqpy.QueryATNF(psrs=[pulsar], loadfromdb=data_load.ATNF_LOC).pandas
    query_id = list(query['PSRJ']).index(pulsar)
    if not p_ra or not p_dec:
        p_ra = query["RAJ"][query_id]
        p_dec= query["DECJ"][query_id]

    # get metadata if not supplied
    if not common_metadata:
        logger.debug("Obtaining obs metadata")
        common_metadata = get_common_obs_metadata(obsid)

    obsid, obs_ra, obs_dec, _, delays, centrefreq, channels = common_metadata

    if not dect_beg or not dect_end:
        # Estimate integration time from when the source enters and exits the beam
        dect_beg, dect_end = source_beam_coverage_and_times(obsid, pulsar,
                                p_ra=p_ra, p_dec=p_dec,
                                obs_beg=obs_beg, obs_end=obs_end,
                                min_z_power=min_z_power,
                                common_metadata=common_metadata,
                                query=query)[:2]
    start_time = dect_end - int(obsid)
    t_int = dect_end - dect_beg + 1

    #Get important info
    ntiles = 128 #TODO actually we excluded some tiles during beamforming, so we'll need to account for that here

    beam_power = get_beam_power_over_time(np.array([[pulsar, p_ra, p_dec]]),
                                          common_metadata=[obsid, obs_ra, obs_dec, t_int, delays,
                                                           centrefreq, channels],
                                          dt=100, start_time=start_time)
    mean_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 + get_Trec(centrefreq, trcvr_file=trcvr)
    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_alt_az_za(obsid, ra=p_ra, dec=p_dec)
    theta = np.radians(zas)
    gain = from_power_to_gain(mean_beam_power, centrefreq*1e6, ntiles, coh=True)
    logger.debug("mean_beam_power: {} theta: {} pi: {}".format(mean_beam_power, theta, np.pi))
    gain_err = gain * ((1. - mean_beam_power)*0.12 + 2.*(theta/(0.5*np.pi))**2. + 0.1)

    return t_sys, t_sys_err, gain, gain_err