def test_get_list_event_ids(self): """Test get_list_event_ids.""" local_path = os.path.join(temp_dir('realtime-test'), 'shakemaps') shake_data = ShakeData(working_dir=local_path) list_id = shake_data.get_list_event_ids() expected_list_id = [SHAKE_ID] message = 'I got %s for the event ID in the server, Expectation %s' % ( list_id, expected_list_id) self.assertEqual(list_id, expected_list_id, message)
def test_extract(self): """Test extracting data to be used in earth quake realtime.""" local_path = os.path.join(temp_dir('realtime-test'), 'shakemaps') shake_data = ShakeData(working_dir=local_path) shake_data.extract() final_grid_xml_file = os.path.join( shake_data.extract_dir(), 'grid.xml') self.assertTrue( os.path.exists(final_grid_xml_file), 'grid.xml not found')
def test_get_latest_event_id(self): """Test get latest event id.""" local_path = os.path.join(temp_dir('realtime-test'), 'shakemaps') shake_data = ShakeData(working_dir=local_path) latest_id = shake_data.get_latest_event_id() # The latest event ID should be = SHAKE_ID since there's only one expected_event_id = SHAKE_ID message = 'I got %s for this latest event id, Expectation %s' % ( latest_id, expected_event_id) self.assertEqual(expected_event_id, latest_id, message)
def test_constructor(self): """Test create shake data.""" local_path = os.path.join(temp_dir('realtime-test'), 'shakemaps') try: event_one = ShakeData(working_dir=local_path) event_two = ShakeData(working_dir=temp_dir('realtime-test'), event=SHAKE_ID) self.assertEqual(event_one.event_id, SHAKE_ID) self.assertEqual(event_two.event_id, SHAKE_ID) except: raise
def test_validate_event(self): """Test validate_event works.""" local_path = os.path.join(temp_dir('realtime-test'), 'shakemaps') event = ShakeData(working_dir=local_path) self.assertTrue(event.validate_event())
def create_shake_events(event_id=None, population_path=None, working_dir=None, locale='en', force_flag=False): """ :param working_dir: The locale working dir where all the shakemaps are located. :type working_dir: str :param event_id: (Optional) Id of the event. Will be used to fetch the ShakeData for this event. The grid.xml file in the unpacked event will be used to initialise the state of the a ShakeGrid instance. If no event id is supplied, the most recent event recorded on working dir will be used. :type event_id: str :param locale:(Optional) string for iso locale to use for outputs. Defaults to en. Can also use 'id' or possibly more as translations are added. :type locale: str :param force_flag: Whether to force retrieval of the dataset. :type force_flag: bool :return: Shake Events to process :rtype: list[ShakeEvent] """ shake_events = [] if not os.path.exists(population_path): population_path = None # cron job executed this script minutely, so it is possible in one # minute that we have more than one shake_event. We can resolve this # by only retrieveng the shake id for that particular minute. # retrieve all the shake ids shake_ids = ShakeData.get_list_event_ids_from_folder(working_dir) shake_ids.sort() shake_ids.reverse() if not shake_ids: return [] if event_id: shake_events.append( ShakeEvent(working_dir=working_dir, event_id=event_id, locale=locale, force_flag=force_flag, population_raster_path=population_path)) else: last_int = int(shake_ids[0]) # sort descending for shake_id in shake_ids: if last_int - int(shake_id) < 100: shake_event = ShakeEvent( working_dir=working_dir, event_id=shake_id, locale=locale, force_flag=force_flag, population_raster_path=population_path) shake_events.append(shake_event) else: break return shake_events
def create_shake_events(event_id=None, population_path=None, working_dir=None, locale="en", force_flag=False): """ :param working_dir: The locale working dir where all the shakemaps are located. :type working_dir: str :param event_id: (Optional) Id of the event. Will be used to fetch the ShakeData for this event. The grid.xml file in the unpacked event will be used to initialise the state of the a ShakeGrid instance. If no event id is supplied, the most recent event recorded on working dir will be used. :type event_id: str :param locale:(Optional) string for iso locale to use for outputs. Defaults to en. Can also use 'id' or possibly more as translations are added. :type locale: str :param force_flag: Whether to force retrieval of the dataset. :type force_flag: bool :return: Shake Events to process :rtype: list[ShakeEvent] """ shake_events = [] if not os.path.exists(population_path): population_path = None # cron job executed this script minutely, so it is possible in one # minute that we have more than one shake_event. We can resolve this # by only retrieveng the shake id for that particular minute. # retrieve all the shake ids shake_ids = ShakeData.get_list_event_ids_from_folder(working_dir) shake_ids.sort() shake_ids.reverse() if not shake_ids: return [] if event_id: shake_events.append( ShakeEvent( working_dir=working_dir, event_id=event_id, locale=locale, force_flag=force_flag, population_raster_path=population_path, ) ) else: last_int = int(shake_ids[0]) # sort descending for shake_id in shake_ids: if last_int - int(shake_id) < 100: shake_event = ShakeEvent( working_dir=working_dir, event_id=shake_id, locale=locale, force_flag=force_flag, population_raster_path=population_path, ) shake_events.append(shake_event) else: break return shake_events