Exemplo n.º 1
0
    def get_catalog(self, name = 'ims1_short_parsed', agency_uri = None, author_uri = None):
        ''' Get a catalog instance of the parsed bulletin.
        '''
        catalog = ev_core.Catalog(name = name, agency_uri = agency_uri)

        for cur_event_dict in self.events:
            if len(cur_event_dict['origins']) == 0:
                self.logger.error("No origins found for event %s. Can't compute the start time.", cur_event_dict['event_id'])

            orig_start_time = min([x['starttime'] for x in cur_event_dict['origins']])
            start_time = min([utcdatetime.UTCDateTime(orig_start_time.year, orig_start_time.month, orig_start_time.day,
                            x['arrival_time']['hour'], x['arrival_time']['minute'],
                            int(x['arrival_time']['second']),
                            int(round((x['arrival_time']['second'] - int(x['arrival_time']['second'])) * 1000000))) for x in cur_event_dict['phases']])
            end_time = max([utcdatetime.UTCDateTime(orig_start_time.year, orig_start_time.month, orig_start_time.day,
                            x['arrival_time']['hour'], x['arrival_time']['minute'],
                            int(x['arrival_time']['second']),
                            int(round((x['arrival_time']['second'] - int(x['arrival_time']['second'])) * 1000000))) for x in cur_event_dict['phases']])

            if start_time == end_time:
                end_time = start_time + 1;

            # TODO: The event type should be an instance of an event_type class
            # which is related to the event_type database table.
            cur_event = ev_core.Event(start_time = start_time,
                                   end_time = end_time,
                                   public_id = cur_event_dict['event_id'],
                                   #event_type = cur_event_dict['origins'][0]['event_type'],
                                   description = cur_event_dict['location'],
                                   agency_uri = cur_event_dict['origins'][0]['author'])

            catalog.add_events([cur_event,])

        return catalog
Exemplo n.º 2
0
    def test_write_to_database_with_events(self):
        ''' Test the writing to the database of a catalog with events.
        '''
        creation_time = UTCDateTime()
        catalog = ev_core.Catalog(name='test',
                                  description='A test description.',
                                  agency_uri='uot',
                                  author_uri='tester',
                                  creation_time=creation_time)

        # Create an event.
        start_time = '2000-01-01T00:00:00'
        end_time = '2000-01-01T01:00:00'
        creation_time = UTCDateTime()
        event = ev_core.Event(start_time=start_time,
                              end_time=end_time,
                              creation_time=creation_time)

        catalog.add_events([
            event,
        ])

        catalog.write_to_database(self.project)

        db_catalog_orm = self.project.dbTables['event_catalog']
        db_session = self.project.getDbSession()
        result = db_session.query(db_catalog_orm).all()
        db_session.close()
        self.assertEqual(len(result), 1)
        tmp = result[0]
        self.assertEqual(len(tmp.events), 1)
        self.assertEqual(tmp.events[0].ev_catalog_id, catalog.db_id)

        # Add a second event.
        start_time = '2000-01-02T00:00:00'
        end_time = '2000-01-02T01:00:00'
        creation_time = UTCDateTime()
        event = ev_core.Event(start_time=start_time,
                              end_time=end_time,
                              creation_time=creation_time)

        catalog.add_events([
            event,
        ])
        catalog.write_to_database(self.project)

        db_session = self.project.getDbSession()
        result = db_session.query(db_catalog_orm).all()
        db_session.close()
        self.assertEqual(len(result), 1)
        tmp = result[0]
        self.assertEqual(len(tmp.events), 2)
        self.assertEqual(tmp.events[0].ev_catalog_id, catalog.db_id)
        self.assertEqual(tmp.events[1].ev_catalog_id, catalog.db_id)
Exemplo n.º 3
0
 def create_catalog(self, name, description):
     ''' Create a new catalog in the database.
     '''
     catalog = event_core.Catalog(name = name,
                                  description = description,
                                  agency_uri = self.parent.project.activeUser.agency_uri,
                                  author_uri = self.parent.project.activeUser.author_uri,
                                  creation_time = UTCDateTime().isoformat())
     catalog.write_to_database(self.parent.project)
     cur_limit = self.pref_manager.get_limit('event_catalog')
     cur_limit.append(catalog.name)
     self.pref_manager.set_limit('event_catalog', cur_limit)
Exemplo n.º 4
0
 def test_catalog_creation(self):
     ''' Test the pSysmon Event class.
     '''
     # Create an event with valid time limits.
     catalog = ev_core.Catalog(name='test_name')
     self.assertIsInstance(catalog, ev_core.Catalog)
     self.assertEqual(catalog.name, 'test_name')
     self.assertIsNone(catalog.db_id)
     self.assertIsNone(catalog.description)
     self.assertIsNone(catalog.agency_uri)
     self.assertIsNone(catalog.author_uri)
     self.assertIsNotNone(catalog.creation_time)
     self.assertListEqual(catalog.events, [])
Exemplo n.º 5
0
    def get_catalog(self, name = 'csv_parsed', author_uri = None, agency_uri = None):
        ''' Get a catalog instance of the parsed csv file.
        '''
        catalog = ev_core.Catalog(name = name, agency_uri = agency_uri,
                                  author_uri = author_uri)

        # Adjust the URIs of the events.
        if author_uri or agency_uri:
            for cur_event in self.events:
                if not cur_event.author_uri and not cur_event.agency_uri:
                    cur_event.author_uri = author_uri
                    cur_event.agency_uri = agency_uri
        catalog.add_events(self.events)

        return catalog
Exemplo n.º 6
0
    def test_add_events(self):
        ''' Test the add_events method.
        '''
        catalog = ev_core.Catalog(name='test')

        # Create an event.
        start_time = '2000-01-01T00:00:00'
        end_time = '2000-01-01T01:00:00'
        creation_time = UTCDateTime()
        event = ev_core.Event(start_time=start_time,
                              end_time=end_time,
                              creation_time=creation_time)

        catalog.add_events([
            event,
        ])

        self.assertEqual(len(catalog.events), 1)
        self.assertEqual(catalog.events[0], event)
        self.assertEqual(event.parent, catalog)
Exemplo n.º 7
0
    def test_write_to_database(self):
        ''' Test the write_to_database method.
        '''
        creation_time = UTCDateTime()
        catalog = ev_core.Catalog(name='test',
                                  description='A test description.',
                                  agency_uri='uot',
                                  author_uri='tester',
                                  creation_time=creation_time)
        catalog.write_to_database(self.project)

        db_catalog_orm = self.project.dbTables['event_catalog']
        db_session = self.project.getDbSession()
        result = db_session.query(db_catalog_orm).all()
        db_session.close()
        self.assertEqual(len(result), 1)
        tmp = result[0]
        self.assertEqual(tmp.name, 'test')
        self.assertEqual(tmp.description, 'A test description.')
        self.assertEqual(tmp.agency_uri, 'uot')
        self.assertEqual(tmp.author_uri, 'tester')
        self.assertEqual(tmp.creation_time, creation_time.isoformat())
Exemplo n.º 8
0
    def test_bind(self):
        ''' Test the binding of the detections.
        '''
        # Create the test detections.
        catalog = detect.Catalog(name='test',
                                 description='A test description.',
                                 agency_uri='uot',
                                 author_uri='tester',
                                 creation_time=utcdatetime.UTCDateTime())

        channels = [('GILA', 'HHZ', 'ALPAACT', '00'),
                    ('GUWA', 'HHZ', 'ALPAACT', '00'),
                    ('G_NAWA', 'HHZ', 'ALPAACT', '00'),
                    ('SITA', 'HHZ', 'ALPAACT', '00')]
        events = {}
        events['2016-01-01T00:00:00'] = [0, 1, 2, 3]
        events['2016-01-01T01:00:00'] = [1, 0, 1, 2]
        events['2016-01-01T03:00:00'] = [2, 1, 0, 1]
        events['2016-01-01T04:00:00'] = [3, 2, 1, 0]

        for cur_start, cur_delay_list in events.items():
            for k, cur_delay in enumerate(cur_delay_list):
                cur_scnl = channels[k]
                cur_channel = self.project.geometry_inventory.get_channel(
                    station=cur_scnl[0],
                    name=cur_scnl[1],
                    network=cur_scnl[2],
                    location=cur_scnl[3])
                cur_channel = cur_channel[0]

                # Create a detection and add it to the catalog.
                start_time = utcdatetime.UTCDateTime(cur_start) + cur_delay
                end_time = start_time + 3
                cur_rec_stream = cur_channel.get_stream(start_time=start_time,
                                                        end_time=end_time)
                cur_rec_stream = cur_rec_stream[0]
                det = detect.Detection(start_time=start_time,
                                       end_time=end_time,
                                       creation_time=utcdatetime.UTCDateTime(),
                                       rec_stream_id=cur_rec_stream.id)
                catalog.add_detections([
                    det,
                ])
        catalog.write_to_database(self.project)

        # Get the channels for the detections.
        catalog.assign_channel(self.project.geometry_inventory)

        # Create an event catalog where to store the events.
        event_catalog = ev_core.Catalog(
            name='event_bind_test',
            description='A test description.',
            agency_uri='uot',
            author_uri='tester',
            creation_time=utcdatetime.UTCDateTime())

        # Bind the detections to events.
        binder = binding.EventBinder(event_catalog=event_catalog,
                                     author_uri='tester',
                                     agency_uri='uot')
        binder.compute_search_windows(
            self.project.geometry_inventory.get_station())
        binder.bind(catalog, channels)

        # Save the event catalog to the database.
        event_catalog.write_to_database(self.project)

        # Load the events from the database and check for the correctly
        # associated detections.
        db_catalog_orm = self.project.dbTables['event_catalog']
        db_session = self.project.getDbSession()
        try:
            result = db_session.query(db_catalog_orm).filter(
                db_catalog_orm.name == 'event_bind_test').all()
            loaded_catalog = ev_core.Catalog.from_db_catalog(result[0],
                                                             load_events=True)
            for cur_event_start, cur_delay in events.items():
                cur_event_start = utcdatetime.UTCDateTime(cur_event_start)
                selected_event = loaded_catalog.get_events(
                    start_time=cur_event_start, end_time=cur_event_start + 10)
                self.assertEqual(len(selected_event), 1)
                selected_event = selected_event[0]
                self.assertEqual(len(selected_event.detections), 4)
                detections = sorted(selected_event.detections,
                                    key=op.attrgetter('start_time'))
                cur_det_starts = [
                    cur_event_start + x for x in sorted(cur_delay)
                ]
                self.assertEqual(detections[0].start_time, cur_det_starts[0])
                self.assertEqual(detections[1].start_time, cur_det_starts[1])
                self.assertEqual(detections[2].start_time, cur_det_starts[2])
                self.assertEqual(detections[3].start_time, cur_det_starts[3])

        finally:
            db_session.close()