Esempio n. 1
0
def fill_process_table(version_id, rawfile_id, parfile_id, template_id,
                       manip, nchan, nsub, existdb=None):
    db = existdb or database.Database()
    db.connect()

    ins = db.process.insert()
    values = {'version_id': version_id,
              'rawfile_id': rawfile_id,
              'parfile_id': parfile_id,
              'template_id': template_id,
              'manipulator': manip.name,
              'manipulator_args': manip.argstr,
              'nchan': nchan,
              'nsub': nsub,
              'toa_fitting_method': config.cfg.toa_fitting_method,
              'user_id': cache.get_userid()}
    result = db.execute(ins, values)
    process_id = result.inserted_primary_key[0]
    result.close()
    notify.print_info("Added processing run to DB. Processing ID: %d" %
                      process_id, 1)
    # Close DB connection
    if not existdb:
        db.close()
    return process_id
Esempio n. 2
0
def main(args):
    # Check to make sure user provided a comment
    if not args.dry_run and args.comments is None:
        raise errors.BadInputError("A comment describing the timfile is "
                                   "required!")
    
    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 == '-':
            argfile = sys.stdin
        else:
            if not os.path.exists(args.from_file):
                raise errors.FileError("The list of cmd line args (%s) "
                                       "does not exist." % args.from_file)
            argfile = open(args.from_file, 'r')
        for line in argfile:
            # Strip comments
            line = line.partition('#')[0].strip()
            if not line:
                # Skip empty line
                continue
            arglist = shlex.split(line.strip())
            args = parser.parse_args(arglist, namespace=args)

    # Establish a database connection
    db = database.Database()
    db.connect()

    trans = db.begin()
    try:
        cmdline = " ".join(sys.argv)
        toas = get_toas(args, db)
        if debug.is_on('TIMFILE'):
            # Check for / handle conflicts
            conflict_handler = CONFLICT_HANDLERS[args.on_conflict]
            toas = conflict_handler(toas)
            wt.write_timfile(toas, {'comments': args.comments,
                                    'user_id': cache.get_userid(),
                                    'add_time': "Not in DB!",
                                    'timfile_id': -1})
        elif args.dry_run:
            print_summary(toas, args.comments)
        else:
            conflict_handler = CONFLICT_HANDLERS[args.on_conflict]
            timfile_id = add_timfile_entry(toas, cmdline, args.comments,
                                           conflict_handler)
            notify.print_info("Created new timfile entry - timfile_id=%d (%s)" %
                              (timfile_id, utils.give_utc_now()), 1)
    except:
        db.rollback()
        db.close()
        raise
    else:
        db.commit()
        db.close()
Esempio n. 3
0
def add_timfile_entry(toas, cmdline, comments, conflict_handler, existdb=None):
    """Insert a timfile entry in the DB, and associate
        TOAs with it.

        Inputs:
            toas: A list of row objects each representing a TOA.
            cmdline: the command line used when running the program.
            comments: User comments describing the timfile.
            conflict_handler: A handler function to use.
            existdb: A (optional) existing database connection object.
                (Default: Establish a db connection)

        Output:
            timfile_id: The resulting ID of the timfile entry.
    """
    # Check for / handle conflicts
    toas = conflict_handler(toas)
    if not toas:
        raise errors.ToasterError("No TOAs match criteria provided!") 
    
    # Connect to DB, if not using an already-established connection
    db = existdb or database.Database()
    db.connect()

    # Insert timfile entry
    ins = db.timfiles.insert()
    values = {'user_id': cache.get_userid(),
              'version_id': version.get_version_id(db),
              'comments': comments,
              'pulsar_id': toas[0]['pulsar_id'],
              'input_args': cmdline}
    result = db.execute(ins, values)
    timfile_id = result.inserted_primary_key[0]
    result.close()
    
    # Associate the TOAs
    ins = db.toa_tim.insert()
    values = []
    for toa in toas:
        values.append({'timfile_id': timfile_id,
                       'toa_id': toa['toa_id']})
    db.execute(ins, values)

    if not existdb:
        db.close()

    return timfile_id
Esempio n. 4
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