Beispiel #1
0
def parkes_reader(line, get_telescope_id=True):
    """Parse line, assuming it is a TOA in parkes format.
        Return a dictionary of information.

        Input:
            line: A single TOA line in parkes format.
            get_telescope_id: Query the database to get the telescope
                ID number. (Default: True)

        Output:
            toainfo: A dictionary of TOA information.
    """
    parkes_toa_re = re.compile(r'^ *?(?P<bad>(#|(C ))(?P<comment1>.*?))?'
                               r' (?P<info>.{24})(?P<freq>.{9})(?P<imjd>.{7})(?P<fmjd>\..{12}) '
                               r'(?P<phaseoffset>.{8}) (?P<err>.{7})(?P<info2>.{7}) '
                               r'(?P<site>.)(?P<dmcorr>[^#]*)')
    comment_re = re.compile(r'#(?P<comment>.*)$')
    
    match = parkes_toa_re.search(line.rstrip())
    if match is None:
        toainfo = None
        notify.print_debug("Line is not a Parkes-format TOA:\n    %s" % line, 'toaparse')
    else:
        grp = match.groupdict()
        toainfo = {}
        toainfo['is_bad'] = (grp['bad'] is not None)
        toainfo['freq'] = float(grp['freq'])
        toainfo['imjd'] = int(grp['imjd'])
        toainfo['fmjd'] = float(grp['fmjd'])
        toainfo['toa_unc_us'] = float(grp['err'])
        toainfo['telescope'] = grp['site']
        if get_telescope_id:
            toainfo['telescope_id'] = cache.get_telescope_info(grp['site'])['telescope_id']
        toainfo['extras'] = {'phaseoffset': float(grp['phaseoffset']),
                             'infostr': grp['info'].strip() + ' -- ' + grp['info2'].strip()}
        if grp['dmcorr']:
            toainfo['extras']['dmcorr'] = float(grp['dmcorr'])

        comments = []
        if grp['comment1']:
            comments.append(grp['comment1'].strip())
        match2 = comment_re.search(line[match.end():])
        if match2:
            grp2 = match2.groupdict()
            if grp2['comment']:
                comments.append(grp2['comment'].strip())
        toainfo['comment'] = " -- ".join(comments)
            
    return toainfo
Beispiel #2
0
def tempo2_reader(line, get_telescope_id=True):
    """Parse line, assuming it is a TOA in tempo2 format.
        Return a dictionary of information.

        Input:
            line: A single TOA line in Tempo2 format.
            get_telescope_id: Query the database to get the telescope
                ID number. (Default: True)

        Output:
            toainfo: A dictionary of TOA information.
    """
    tempo2_toa_re = re.compile(r'^ *(?P<bad>(#|(C )|(c ))(?P<comment1>.*?))? *'
                               r'(?P<file>[^ ]+) +'
                               r'(?P<freq>\d+(\.\d+)?) +(?P<imjd>\d+)(?P<fmjd>\.\d+) +'
                               r'(?P<err>\d+(\.\d+)?) +(?P<site>[^ ]+)')
    comment_re = re.compile(r'#(?P<comment>.*)$')
    tempo2_flag_re = re.compile(r'-(?P<flagkey>[^ ]+) +(?P<flagval>[^ ]+)')
    match = tempo2_toa_re.search(line)
    if match is None:
        toainfo = None
        notify.print_debug("Line is not a Tempo2 TOA:\n    %s" % line, 'toaparse')
    else:
        grp = match.groupdict()
     
        toainfo = {}
        toainfo['grp'] = grp
        toainfo['is_bad'] = (line.strip().startswith('#') or line.strip().lower().startswith('c ')) #(grp['bad'] is not None)
        toainfo['file'] = grp['file']
        toainfo['freq'] = float(grp['freq'])
        toainfo['imjd'] = int(grp['imjd'])
        toainfo['fmjd'] = float(grp['fmjd'])
        toainfo['toa_unc_us'] = float(grp['err'])
        toainfo['telescope'] = grp['site']
        toainfo['line'] = line
        if get_telescope_id:
            toainfo['telescope_id'] = cache.get_telescope_info(grp['site'])['telescope_id']
        comments = []
        if grp['comment1']:
            comments.append(grp['comment1'].strip())
        match2 = comment_re.search(line[match.end():])
        if match2:
            grp2 = match2.groupdict()
            if grp2['comment']:
                comments.append(grp2['comment'].strip())
        toainfo['comment'] = " -- ".join(comments)
            
        toainfo['extras'] = {}
        for key, val in tempo2_flag_re.findall(line[match.end():]):
            key = key.lower()
            key = KNOWN_FLAG_ALIASES.get(key, key)
            caster = KNOWN_FLAG_TYPES.get(key, str)
            try:
                toainfo['extras'][key] = caster(val.strip())
            except:
                notify.print_info("Couldn't cast %s:%s" % (key, val), 2)

    notify.print_debug("TOA line: %s\nParsed info: %s" % (line, toainfo),
                    'toaparse')

    return toainfo
Beispiel #3
0
def prep_file(fn):
    """Prepare file for archiving/loading.
        
        Also, perform some checks on the file to make sure we
        won't run into problems later. Checks peformed:
            - Existence of file.
            - Read/write access for file (so it can be moved).
            - Header contains all necessary values.
            - Site/observing system is recognized.

        Input:
            fn: The name of the file to check.

        Outputs:
            params: A dictionary of info to be uploaded.
    """
    # Check existence of file
    verify_file_path(fn)

    # Check file permissions allow for writing and reading
    if not os.access(fn, os.R_OK):
        raise errors.FileError("File (%s) is not readable!" % fn)

    # Grab header info
    hdritems = ["nbin", "nchan", "npol", "nsub", "type", "telescop",
                "name", "dec", "ra", "freq", "bw", "dm", "rm",
                # The names of these header params
                # vary with psrchive version
                # "dmc", "rm_c", "pol_c",
                "scale", "state", "length",
                "rcvr", "basis", "backend", "mjd"]

    params = get_header_vals(fn, hdritems)
    params['user_id'] = cache.get_userid()

    # Normalise telescope name
    tinfo = cache.get_telescope_info(params['telescop'])
    params['telescop'] = tinfo['telescope_name']
    params.update(tinfo)

    # Check if obssystem_id, pulsar_id, user_id can be found
    obssys_key = (params['telescop'].lower(), params['rcvr'].lower(),
                  params['backend'].lower())
    obssys_ids = cache.get_obssystemid_cache()
    if obssys_key not in obssys_ids:
        t, r, b = obssys_key
        raise errors.FileError("The observing system combination in the file "
                               "%s is not registered in the database. "
                               "(Telescope: %s, Receiver: %s; Backend: %s)." %
                               (fn, t, r, b))
    else:
        params['obssystem_id'] = obssys_ids[obssys_key]
        obssysinfo = cache.get_obssysinfo(params['obssystem_id'])
        params['band_descriptor'] = obssysinfo['band_descriptor']
        params['obssys_name'] = obssysinfo['name']

    # Check if pulsar_id is found
    try:
        psr_id = cache.get_pulsarid(params['name'])
    except errors.UnrecognizedValueError:
        if config.cfg.auto_add_pulsars:
            notify.print_info("Automatically inserting pulsar with "
                              "name '%s'" % params['name'], 1)
            # Add pulsar
            psr_id = add_pulsar.add_pulsar(params['name'])
            # Force an update of the pulsarid and pulsarname caches
            cache.get_pulsarid_cache(update=True)
            cache.get_pulsarname_cache(update=True)
        else:
            raise errors.FileError("The pulsar name %s (from file %s) is not "
                                   "recognized." % (params['name'], fn))
    # Normalise pulsar name
    params['name'] = cache.get_prefname(params['name'])
    params['pulsar_id'] = psr_id

    return params
Beispiel #4
0
def plot_toa_histogram(toas):
    """Given a list of TOAs (as returned by create_timfile.get_toas(...)
        make histogram plots.

        Input:
            toas: A list of TOAs.

        Output:
            fig: The newly created matplotlib Figure object.
    """
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(10, 6))

    # Summarize TOA info
    pulsars = {}
    telescopes = set()
    bands = set()
    psrnames = set()
    for toa in toas:
        psr = pulsars.setdefault(toa['pulsar_id'], {})
        obssysid = toa['obssystem_id']
        obssysinfo = cache.get_obssysinfo(obssysid)
        telid = obssysinfo['telescope_id']
        telname = cache.get_telescope_info(telid)['telescope_name']
        band = toa['band_descriptor']
        bands.add(band)
        telescopes.add(telname)
        psr[telname] = psr.get(telname, 0)+1
        psr[band] = psr.get(band, 0)+1

    band_colours = dict(zip(sorted(bands), ['r', 'b', 'g', 'c']))
    telescope_colours = {'Effelsberg': '#FFCE00',
                         'Jodrell': '#CE1124', 
                         'Nancay': '#0055A4', 
                         'WSRT': '#FF7F00', 
                         'Sardinia': '#007FFF',
                         'Parkes': '#EDC9AF',
                         'GBT': '#22BB22',
                         'Arecibo': '#00BFFF'}

    indices = []
    labels = []
    telleg = {}
    bandleg = {}
    telax = plt.axes((0.15, 0.1, 0.39, 0.8))
    bandax = plt.axes((0.56, 0.1, 0.39, 0.8), sharey=telax)
    for ii, (psrid, info) in enumerate(pulsars.iteritems()):
        indices.append(ii)
        labels.append(cache.get_pulsarname(psrid))
        total = 0
        for telname in sorted(telescopes):
            if telname in info:
                count = info[telname]
                bb = telax.barh(ii, count, height=1, left=total,
                                color=telescope_colours[telname])
                telleg[telname] = bb
                total += count
        total = 0
        for band in ['P-band', 'L-band', 'S-band']:
            if band in info:
                count = info[band]
                bb = bandax.barh(ii, count, height=1, left=total,
                                 color=band_colours[band])
                bandleg[band] = bb
                total += count
    telax.yaxis.set_ticks(np.array(indices)+0.5)
    telax.yaxis.set_ticklabels(labels)
    telax.set_ylim(-0.5, len(pulsars)+2.5)
    plt.setp((telax.get_xticklabels(),
              bandax.get_xticklabels()),
             rotation=30, ha='right')
    telax.set_xlabel("Number of TOAs")
    bandax.set_xlabel("Number of TOAs")
    telax.set_title("By telescope")
    bandax.set_title("By observing band")
    labels, handles = zip(*sorted(telleg.items()))
    telax.legend(handles, labels, prop=dict(size='small'))
    handles, labels = zip(*bandleg.items())
    bandax.legend(labels, handles, prop=dict(size='small'))
    plt.setp(bandax.yaxis.get_ticklabels(), visible=False)
    return fig
Beispiel #5
0
def main(args):
    # Connect to the database
    db = database.Database()
    db.connect()

    try:
        if args.from_file is not None:
            # Re-create parser, so we can read arguments from file
            parser = utils.DefaultArguments()
            add_arguments(parser)
            if args.from_file == '-':
                obssyslist = sys.stdin
            else:
                if not os.path.exists(args.from_file):
                    raise errors.FileError("The obssystem list (%s) does " \
                                "not appear to exist." % args.from_file)
                obssyslist = open(args.from_file, 'r')
            numfails = 0
            numadded = 0
            for line in obssyslist:
                # Strip comments
                line = line.partition('#')[0].strip()
                if not line:
                    # Skip empty line
                    continue
                try:
                    customargs = copy.deepcopy(args)
                    arglist = shlex.split(line.strip())
                    parser.parse_args(arglist, namespace=customargs)
        
                    if customargs.telescope is None or customargs.backend is None or \
                            customargs.frontend is None or customargs.band is None or \
                            customargs.clock is None:
                        raise errors.BadInputError("Observing systems " \
                                "must have a telescope, backend, frontend, " \
                                "band descriptor, and clock file! At least " \
                                "one of these is missing.")
                    tinfo = cache.get_telescope_info(customargs.telescope)
                    telescope_id = tinfo['telescope_id']

                    if customargs.name is None:
                        customargs.name = "%s_%s_%s" % \
                                    (tinfo['telescope_abbrev'].upper(), \
                                     customargs.backend.upper(), \
                                     customargs.frontend.upper())
                    
                    obssystem_id = add_obssystem(db, customargs.name, telescope_id, \
                                    customargs.frontend, customargs.backend, \
                                    customargs.band, customargs.clock)
                    print "Successfully inserted new observing system. " \
                            "Returned obssystem_id: %d" % obssystem_id
                    numadded += 1
                except errors.ToasterError:
                    numfails += 1
                    traceback.print_exc()
            if args.from_file != '-':
                obssyslist.close()
            if numadded:
                notify.print_success("\n\n===================================\n" \
                                    "%d obssystems successfully added\n" \
                                    "===================================\n" % numadded)
            if numfails:
                raise errors.ToasterError(\
                    "\n\n===================================\n" \
                        "The adding of %d obssystems failed!\n" \
                        "Please review error output.\n" \
                        "===================================\n" % numfails)
        else:
            if args.telescope is None or args.backend is None or \
                    args.frontend is None or args.band is None or \
                    args.clock is None:
                raise errors.BadInputError("Observing systems " \
                        "must have a telescope, backend, frontend, " \
                        "band descriptor, and clock file! At least " \
                        "one of these is missing.")
            tinfo = cache.get_telescope_info(args.telescope)
            telescope_id = tinfo['telescope_id']

            if args.name is None:
                args.name = "%s_%s_%s" % \
                            (tinfo['telescope_abbrev'].upper(), \
                             args.backend.upper(), \
                             args.frontend.upper())
            obssystem_id = add_obssystem(db, args.name, telescope_id, \
                        args.frontend, args.backend, args.band, args.clock)
            print "Successfully inserted new observing system. " \
                        "Returned obssystem_id: %d" % obssystem_id
    finally:
        db.close()