Exemplo n.º 1
0
def main(args):
    # Build caches
    cache.get_pulsarname_cache()
    pulsar_ids = args.pulsar_ids + \
        [cache.get_pulsarid(psr) for psr in args.psrnames]
    if not pulsar_ids:
        pulsar_ids = None
    if args.output_style == 'text':
        # Get pulsar info
        psrinfo = get_pulsarinfo(pulsar_ids)
        show_pulsars(psrinfo)
    elif args.output_style == 'dump':
        dump_pulsars(pulsar_ids)
    else:
        raise errors.UnrecognizedValueError("The output-style '%s' is "
                                            "not recognized!" %
                                            args.output_style)
Exemplo n.º 2
0
def get_pulsarinfo(pulsar_ids=None, existdb=None):
    """Return a dictionary of info for all pulsars.

        Inputs:
            pulsar_ids: A list of pulsar IDs to get info for.
                (Default: Get info for all pulsars).
            existdb: A (optional) existing database connection object.
                (Default: Establish a db connection)
        
        Output:
            psrinfo: A dictionary of pulsar info dictionaries.
    """
    db = database.Database()
    db.connect()
    trans = db.begin()

    try:
        # Get number of observations
        select = db.select([db.rawfiles.c.pulsar_id,
                            db.rawfiles.c.telescop,
                            database.sa.func.count(db.rawfiles.c.rawfile_id).\
                                label('numobs')],
                    from_obj=[db.rawfiles.\
                        outerjoin(db.replacement_rawfiles,
                            onclause=db.replacement_rawfiles.c.obsolete_rawfile_id ==
                                    db.rawfiles.c.rawfile_id)]).\
                    where(db.replacement_rawfiles.c.replacement_rawfile_id == None).\
                    group_by(db.rawfiles.c.pulsar_id,
                             db.rawfiles.c.telescop)
        result = db.execute(select)
        rawfile_rows = result.fetchall()
        result.close()
 
        # Get number of TOAs
        select = db.select([db.toas.c.pulsar_id,
                            database.sa.func.count(db.toas.c.toa_id).\
                                label('numtoas')]).\
                    group_by(db.toas.c.pulsar_id)
        result = db.execute(select)
        numtoas_rows = result.fetchall()
        result.close()
 
        # Get parfile info
        select = db.select([db.parfiles.c.pulsar_id,
                            db.parfiles.c.parfile_id,
                            db.parfiles.c.dm,
                            (1.0/db.parfiles.c.f0).label('period'),
                            db.parfiles.c.raj,
                            db.parfiles.c.decj,
                            db.parfiles.c.binary_model],
                    from_obj=[db.master_parfiles.\
                        outerjoin(db.parfiles,
                            onclause=db.parfiles.c.parfile_id ==
                                    db.master_parfiles.c.parfile_id)])
        result = db.execute(select)
        parfile_rows = result.fetchall()
        result.close()
       
        # Get curators
        select = db.select([db.curators])
        result = db.execute(select)
        curator_rows = result.fetchall()
        result.close()

        # Get pulsar names
        pulsarname_cache = cache.get_pulsarname_cache(existdb=db)
        pulsaralias_cache = cache.get_pulsaralias_cache(existdb=db)
        userinfo_cache = cache.get_userinfo_cache(existdb=db)
        telescopeinfo_cache = cache.get_telescopeinfo_cache(existdb=db)
    except:
        trans.rollback()
        raise
    else:
        trans.commit()
    finally:
        if not existdb:
            db.close()

    psrinfo = {}
    for psrid, name in pulsarname_cache.iteritems():
        if pulsar_ids is not None and psrid not in pulsar_ids:
            continue
        psrinfo[psrid] = {'name': name,
                          'aliases': pulsaralias_cache[psrid],
                          'telescopes': [],
                          'curators': [],
                          'numobs': 0,
                          'numtoas': 0,
                          'parfile_id': None,
                          'period': None,
                          'dm': None,
                          'raj': 'Unknown',
                          'decj': 'Unknown',
                          'binary': None}
    for row in rawfile_rows:
        psrid = row['pulsar_id']
        if pulsar_ids is not None and psrid not in pulsar_ids:
            continue
        psr = psrinfo[psrid]
        telname = telescopeinfo_cache[row['telescop'].lower()]['telescope_name']
        psr['telescopes'].append(telname)
        psr['numobs'] += row['numobs']
    for row in numtoas_rows:
        psrid = row['pulsar_id']
        if pulsar_ids is not None and psrid not in pulsar_ids:
            continue
        psr = psrinfo[psrid]
        psr['numtoas'] += row['numtoas']
    for row in parfile_rows:
        psrid = row['pulsar_id']
        if pulsar_ids is not None and psrid not in pulsar_ids:
            continue
        psr = psrinfo[psrid]
        psr['parfile_id'] = row['parfile_id']
        psr['period'] = row['period']
        psr['dm'] = row['dm']
        psr['raj'] = row['raj']
        psr['decj'] = row['decj']
        psr['binary'] = row['binary']
    for row in curator_rows:
        psrid = row['pulsar_id']
        if pulsar_ids is not None and psrid not in pulsar_ids:
            continue
        psr = psrinfo[psrid]
        if row['user_id'] is None:
            psr['curators'] = 'Everyone'
            break
        real_name = userinfo_cache[row['user_id']]['real_name'] 
        psr['curators'].append(real_name)
    return psrinfo
Exemplo n.º 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