def main(argv=None): """ Command-line arguments are created and parsed, fed to functions. """ parser = get_parser() options = parser.parse_args(argv) # options_dict = vars(options) session = get_session(options) files = get_files(options) tables = get_or_create_tables(options, session, create=True) lastids = ['arid', 'chanid', 'evid', 'orid', 'wfid'] last = get_lastids(session, tables['lastid'], lastids, create=True) # for item in iterable: for sacfile in files: print sacfile # row_dicts = get_row_dicts(item) tr = read(sacfile, format='SAC', debug_headers=True)[0] # rows needs to be a dict of lists, for make_atomic # row_dicts = get_row_dicts(tr.stats.sac) # put in the whole trace, to determine byte order? dicts = sac.sachdr2tables(tr.stats.sac, tables=tables.keys()) # row_instances = dicts_to_instances(row_dicts, tables) rows = dicts2rows(dicts, tables) # manage dir, dfile, datatype # XXX: hack. replace with an updated obspy.io.sac later. bo = tr.data.dtype.byteorder if bo == '<': datatype = 'f4' elif bo == '>': datatype = 't4' elif bo == '=': if sys.byteorder == 'little': datatype = 'f4' else: datatype = 't4' for wf in rows['wfdisc']: wf.datatype = datatype wf.dfile = os.path.basename(sacfile) if options.absolute_paths: idir = os.path.dirname(os.path.realpath(sacfile)) else: idir = os.path.dirname(sacfile) wf.dir = idir # manage the ids make_atomic(last, **rows) plugins = get_plugins(options) rows = apply_plugins(plugins, **rows) # add rows to the database # XXX: not done very elegantly. some problem rows are simply skipped. for table, instances in rows.items(): if instances: # could be empty [] try: session.add_all(instances) session.commit() except exc.IntegrityError as e: # duplicate or nonexistant primary keys session.rollback() print("rollback {}".format(table)) except exc.OperationalError as e: # no such table, or database is locked session.rollback() print("rollback {}".format(table))
def main(**kwargs): """ Command-line arguments are created and parsed, fed to functions. Parameters ---------- session : SQLAlchemy.orm.Session instance tables : list Canonical names for desired database tables. prefix : str Target core tables using 'account.prefix' naming. absolute_paths : bool bbfk : bool Pull in deast & dnorth info from user7 & user8 header fields file_list : str Name of a text file containing full SAC file names. files : list List of SAC file names. """ print("sac2db: {}".format(kwargs)) session = kwargs['session'] if kwargs.get('file_list'): files = get_files(kwargs['file_list'], file_check=_is_sac) else: files = kwargs['files'] tables = get_or_create_tables(session, create=True) lastids = ['arid', 'chanid', 'evid', 'orid', 'wfid'] last = get_lastids(session, tables['lastid'], lastids, create=True) # for item in iterable: for sacfile in files: print(sacfile) # row_dicts = get_row_dicts(item) tr = read(sacfile, format='SAC', debug_headers=True)[0] # sachdr2tables produces table dictionaries # rows needs to be a dict of lists, for make_atomic dicts = sac.sachdr2tables(tr.stats.sac, tables=tables.keys()) rows = dicts2rows(dicts, tables) # manage dir, dfile, datatype # XXX: hack. replace with an updated obspy.io.sac later. bo = tr.data.dtype.byteorder if bo == '<': datatype = 'f4' elif bo == '>': datatype = 't4' elif bo == '=': if sys.byteorder == 'little': datatype = 'f4' else: datatype = 't4' for wf in rows['wfdisc']: wf.datatype = datatype wf.dfile = os.path.basename(sacfile) if kwargs['absolute_paths']: idir = os.path.dirname(os.path.realpath(sacfile)) else: idir = os.path.dirname(sacfile) # make sure relative paths are non-empty if idir == '': idir = '.' wf.dir = idir # manage the ids make_atomic(last, **rows) # plugins = get_plugins(options) # rows = apply_plugins(plugins, **rows) # add rows to the database # XXX: not done very elegantly. some problem rows are simply skipped. for table, instances in rows.items(): if instances: # could be empty [] try: session.add_all(instances) session.commit() except exc.IntegrityError as e: # duplicate or nonexistant primary keys session.rollback() print("rollback {}".format(table)) except exc.OperationalError as e: # no such table, or database is locked session.rollback() print("rollback {}".format(table))
def main(**kwargs): """ Command-line arguments are created and parsed, fed to functions. Parameters ---------- session : SQLAlchemy.orm.Session instance tables : list Canonical names for desired database tables. prefix : str Target core tables using 'account.prefix' naming. absolute_paths : bool file_list : str Name of a text file containing full SAC file names. files : list List of SAC file names. """ print("sac2db: {}".format(kwargs)) session = kwargs['session'] if kwargs.get('file_list'): files = get_files(kwargs['file_list']) else: files = kwargs['files'] tables = get_or_create_tables(session, create=True) lastids = ['arid', 'chanid', 'evid', 'orid', 'wfid'] last = get_lastids(session, tables['lastid'], lastids, create=True) # for item in iterable: for sacfile in files: print(sacfile) # row_dicts = get_row_dicts(item) tr = read(sacfile, format='SAC', debug_headers=True)[0] # rows needs to be a dict of lists, for make_atomic # row_dicts = get_row_dicts(tr.stats.sac) # put in the whole trace, to determine byte order? dicts = sac.sachdr2tables(tr.stats.sac, tables=tables.keys()) # row_instances = dicts_to_instances(row_dicts, tables) rows = dicts2rows(dicts, tables) # manage dir, dfile, datatype # XXX: hack. replace with an updated obspy.io.sac later. bo = tr.data.dtype.byteorder if bo == '<': datatype = 'f4' elif bo == '>': datatype = 't4' elif bo == '=': if sys.byteorder == 'little': datatype = 'f4' else: datatype = 't4' for wf in rows['wfdisc']: wf.datatype = datatype wf.dfile = os.path.basename(sacfile) if kwargs['absolute_paths']: idir = os.path.dirname(os.path.realpath(sacfile)) else: idir = os.path.dirname(sacfile) wf.dir = idir # manage the ids make_atomic(last, **rows) # plugins = get_plugins(options) # rows = apply_plugins(plugins, **rows) # add rows to the database # XXX: not done very elegantly. some problem rows are simply skipped. for table, instances in rows.items(): if instances: # could be empty [] try: session.add_all(instances) session.commit() except exc.IntegrityError as e: # duplicate or nonexistant primary keys session.rollback() print("rollback {}".format(table)) except exc.OperationalError as e: # no such table, or database is locked session.rollback() print("rollback {}".format(table))