def test_get_catalogs_in_db(self): ''' Test the query of catalog names from the database. ''' catalog = pick_core.Catalog(name='catalog_name_1', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) catalog = pick_core.Catalog(name='catalog_name_2', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) catalog = pick_core.Catalog(name='catalog_name_3', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) # Create the library. library = pick_core.Library(name='test_name') catalog_names = library.get_catalogs_in_db(project=self.project) self.assertIsInstance(catalog_names, list) self.assertEqual(len(catalog_names), 3) self.assertListEqual( catalog_names, ['catalog_name_1', 'catalog_name_2', 'catalog_name_3'])
def test_library_creation(self): ''' Test the pSysmon Event class. ''' # Create an event with valid time limits. library = pick_core.Library(name='test_name') self.assertIsInstance(library, pick_core.Library) self.assertEqual(library.name, 'test_name') self.assertIsInstance(library.catalogs, dict) self.assertEqual(library.catalogs, {})
def __init__(self, **args): ''' Initialize the instance. ''' package_nodes.CollectionNode.__init__(self, **args) # The event catalog library self.event_library = event_core.Library('picks_export') self.pick_library = pick_core.Library('picks_export') self.create_selector_preferences() self.create_pick_selector_preferences() self.create_filter_preferences() self.create_output_preferences()
def __init__(self): ''' The constructor. ''' InteractivePlugin.__init__(self, name = 'pick', category = 'edit', tags = None ) # Create the logging logger instance. logger_prefix = psysmon.logConfig['package_prefix'] loggerName = logger_prefix + "." + __name__ + "." + self.__class__.__name__ self.logger = logging.getLogger(loggerName) self.icons['active'] = icons.hand_2_icon_16 self.cursor = wx.CURSOR_CROSS # The pick catalog library used to manage the catalogs. self.library = pick_core.Library('pick library') # The name of the selected catalog. self.selected_catalog_name = None # A flag indicating if the pick catalog was loaded for a selected # event. self.catalog_loaded_for_selected_event = False # The lines of the picks. self.pick_lines = {} # Add the pages to the preferences manager. to_page = self.pref_manager.add_page('tool options') cat_group = to_page.add_group('catalog') po_group = to_page.add_group('pick options') # Add the plugin preferences. item = psy_pm.SingleChoicePrefItem(name = 'catalog_mode', label = 'mode', value = 'time', limit = ['time',], tool_tip = 'Select a pick catalog to work on.') cat_group.add_item(item) item = psy_pm.SingleChoicePrefItem(name = 'pick_catalog', label = 'pick catalog', value = '', limit = [], tool_tip = 'Select a pick catalog to work on.', hooks = {'on_value_change': self.on_select_catalog}) cat_group.add_item(item) item = psy_pm.ActionItem(name = 'create_new_catalog', label = 'create new catalog', mode = 'button', action = self.on_create_new_catalog) cat_group.add_item(item) item = psy_pm.TextEditPrefItem(name = 'label', label = 'label', value = 'P', tool_tip = 'The label of the pick.') po_group.add_item(item) item = psy_pm.FloatSpinPrefItem(name = 'delete_snap_length', label = 'delete snap [s]', value = 0.1, limit = (0, 1000), tool_tip = 'The snap length used when deleting picks.') po_group.add_item(item)
def test_load_catalog_from_db(self): ''' Test the loading of catalogs from the database. ''' catalog = pick_core.Catalog(name='catalog_name_1', description='A test description.', agency_uri='uot', author_uri='tester') # Create some picks. pick_time = UTCDateTime('2011-01-01T00:00:00') channel = self.project.geometry_inventory.get_channel(name='HHZ', station='SITA') channel = channel[0] pick1 = pick_core.Pick(label='P', time=pick_time, amp1=10, channel=channel) pick2 = pick_core.Pick(label='S', time=pick_time + 10, amp1=20, channel=channel) catalog.add_picks([pick1, pick2]) catalog.write_to_database(self.project) # Create a second catalog. catalog = pick_core.Catalog(name='catalog_name_2', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) # Create the library. library = pick_core.Library(name='test_name') # Test the loading of one catalog without picks. library.load_catalog_from_db(project=self.project, name='catalog_name_1') self.assertEqual(len(library.catalogs), 1) self.assertEqual(iter(library.catalogs.keys()), ['catalog_name_1']) self.assertIsInstance(library.catalogs['catalog_name_1'], pick_core.Catalog) cur_catalog = library.catalogs['catalog_name_1'] self.assertEqual(len(cur_catalog.picks), 0) # Test the loading of one catalog without picks. library.clear() library.load_catalog_from_db(project=self.project, name='catalog_name_1', load_picks=True) self.assertEqual(len(library.catalogs), 1) self.assertEqual(iter(library.catalogs.keys()), ['catalog_name_1']) self.assertIsInstance(library.catalogs['catalog_name_1'], pick_core.Catalog) cur_catalog = library.catalogs['catalog_name_1'] self.assertEqual(len(cur_catalog.picks), 2) # Test the loading of all catalogs. library.clear() catalog_names = library.get_catalogs_in_db(project=self.project) library.load_catalog_from_db(project=self.project, name=catalog_names) self.assertEqual(len(library.catalogs), 2) self.assertListEqual(sorted(library.catalogs.keys()), ['catalog_name_1', 'catalog_name_2'])
def export(self, start_time, end_time, output_interval, event_catalog, event_ids = None, event_types = None, event_tags = None, pick_catalog_name = None): ''' Export the picks of events. ''' self.logger.info("Exporting event picks for timespan timespan %s to %s.", start_time.isoformat(), end_time.isoformat()) if event_tags is None: event_tags = [] if event_types is None: event_types = [] interval_start = self.compute_intervals(start_time = start_time, end_time = end_time, interval = output_interval) event_lib = event_core.Library('events') event_lib.load_catalog_from_db(self.project, name = event_catalog) catalog = event_lib.catalogs[event_catalog] pick_lib = pick_core.Library('picks') pick_lib.load_catalog_from_db(self.project, name = pick_catalog_name) pick_catalog = pick_lib.catalogs[pick_catalog_name] for k, cur_start_time in enumerate(interval_start[:-1]): cur_end_time = interval_start[k + 1] self.logger.info("Processing interval timespan %s to %s.", cur_start_time.isoformat(), cur_end_time.isoformat()) catalog.clear_events() if event_ids is None: # Load the events for the given time span from the database. # TODO: Remove the hardcoded min_event_length value and create # user-selectable filter fields. catalog.load_events(project = self.project, start_time = cur_start_time, end_time = cur_end_time, min_event_length = 0.1, event_tags = event_tags) else: # Load the events with the given ids from the database. Ignore the # time-span. catalog.load_events(event_id = event_ids) # Abort the execution if no events are available for the time span. if not catalog.events: if event_ids is None: self.logger.info('No events found for the timespan %s to %s.', cur_start_time.isoformat(), cur_end_time.isoformat()) else: self.logger.info('No events found for the specified event IDs: %s.', event_ids) continue res_columns = ['event_start_time', 'event_end_time', 'network', 'station', 'location', 'channel', 'pick_label', 'time'] # Loop through the events. n_events = len(catalog.events) sorted_events = sorted(catalog.events, key = lambda x: x.start_time) for k, cur_event in enumerate(sorted_events): self.logger.info("Processing event %d (%d/%d).", cur_event.db_id, k + 1, n_events) pick_catalog.clear_picks() # Create the result. cur_res = result.TableResult(name = 'event_picks', key_name = 'id', event_id = cur_event.db_id, start_time = cur_event.start_time, end_time = cur_event.end_time, origin_name = self.parent_name, origin_resource = self.parent_rid, column_names = res_columns) # TODO: Maybe it is more efficient to load all picks of the # processing timespan and then request the picks using the # start- and endtimes of the events in get_pick(). pick_catalog.load_picks(project = self.project, start_time = cur_event.start_time, end_time = cur_event.end_time) event_picks = pick_catalog.get_pick(start_time = cur_event.start_time, end_time = cur_event.end_time) self.logger.debug("event_picks: %s", event_picks) for cur_pick in event_picks: cur_res.add_row(key = cur_event.db_id, event_start_time = cur_event.start_time.isoformat(), event_end_time = cur_event.end_time.isoformat(), network = cur_pick.channel.parent_station.network, station = cur_pick.channel.parent_station.name, location = cur_pick.channel.parent_station.location, channel = cur_pick.channel.name, pick_label = cur_pick.label, time = cur_pick.time) cur_res.base_output_dir = self.output_dir if len(event_picks) > 0: cur_res.save()