def testGetRemoteShakeMap(self): """Check that we can retrieve both input and output from ftp at once""" myShakeEvent = '20110413170148' myShakeData = ShakeData(myShakeEvent) myExpectedInpFile = os.path.join(shakemapZipDir(), myShakeEvent + '.inp.zip') myExpectedOutFile = os.path.join(shakemapZipDir(), myShakeEvent + '.out.zip') if os.path.exists(myExpectedInpFile): os.remove(myExpectedInpFile) if os.path.exists(myExpectedOutFile): os.remove(myExpectedOutFile) myInpFile, myOutFile = myShakeData.fetchEvent() myMessage = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myInpFile, myExpectedInpFile, myMessage) myMessage = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myOutFile, myExpectedOutFile, myMessage) assert os.path.exists(myExpectedInpFile) assert os.path.exists(myExpectedOutFile)
def test_get_remote_shake_map(self): """Check that we can retrieve both input and output from ftp at once. """ shake_event = '20110413170148' shake_data = ShakeData(shake_event) expected_input_file = os.path.join(shakemap_zip_dir(), shake_event + '.inp.zip') expected_output_file = os.path.join(shakemap_zip_dir(), shake_event + '.out.zip') if os.path.exists(expected_input_file): os.remove(expected_input_file) if os.path.exists(expected_output_file): os.remove(expected_output_file) input_file, output_file = shake_data.fetch_event() message = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(input_file, expected_input_file, message) message = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(output_file, expected_output_file, message) assert os.path.exists(expected_input_file) assert os.path.exists(expected_output_file)
def test_get_remote_shake_map(self): """Check that we can retrieve both input and output from ftp at once. """ shake_data = ShakeData(SHAKE_ID) expected_input_file = os.path.join( shakemap_zip_dir(), SHAKE_ID + '.inp.zip') expected_output_file = os.path.join( shakemap_zip_dir(), SHAKE_ID + '.out.zip') if os.path.exists(expected_input_file): os.remove(expected_input_file) if os.path.exists(expected_output_file): os.remove(expected_output_file) input_file, output_file = shake_data.fetch_event() message = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(input_file, expected_input_file, message) message = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(output_file, expected_output_file, message) self.assertTrue(os.path.exists(expected_input_file)) self.assertTrue(os.path.exists(expected_output_file))
def test_get_shake_map_output(self): """Check that we can retrieve a shakemap 'out' input file.""" event_id = '20110413170148' shake_data = ShakeData(event_id) shakemap_file = shake_data.fetch_output() expected_file = os.path.join(shakemap_zip_dir(), event_id + '.out.zip') message = 'Expected path for downloaded shakemap OUT not received' self.assertEqual(shakemap_file, expected_file, message)
def testGetShakeMapOutput(self): """Check that we can retrieve a shakemap 'out' input file""" myEventId = '20110413170148' myShakeData = ShakeData(myEventId) myShakemapFile = myShakeData.fetchOutput() myExpectedFile = os.path.join(shakemapZipDir(), myEventId + '.out.zip') myMessage = 'Expected path for downloaded shakemap OUT not received' self.assertEqual(myShakemapFile, myExpectedFile, myMessage)
def test_get_shake_map_input(self): """Check that we can retrieve a shakemap 'inp' input file.""" shake_data = ShakeData(SHAKE_ID) shakemap_file = shake_data.fetch_input() expected_file = os.path.join( shakemap_zip_dir(), SHAKE_ID + '.inp.zip') message = 'Expected path for downloaded shakemap INP not received' self.assertEqual(shakemap_file, expected_file, message)
def test_get_shake_map_output(self): """Check that we can retrieve a shakemap 'out' input file.""" shake_data = ShakeData(SHAKE_ID) shakemap_file = shake_data.fetch_output() expected_file = os.path.join( shakemap_zip_dir(), SHAKE_ID + '.out.zip') message = 'Expected path for downloaded shakemap OUT not received' self.assertEqual(shakemap_file, expected_file, message)
def test_get_shake_map_input(self): """Check that we can retrieve a shakemap 'inp' input file""" shake_event = '20110413170148' shake_data = ShakeData(shake_event) shakemap_file = shake_data.fetch_input() expected_file = os.path.join(shakemap_zip_dir(), shake_event + '.inp.zip') message = 'Expected path for downloaded shakemap INP not received' self.assertEqual(shakemap_file, expected_file, message)
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_get_shake_map_output(self): """Check that we can retrieve a shakemap 'out' input file""" event_id = '20110413170148' shake_data = ShakeData(event_id) shakemap_file = shake_data.fetch_output() expected_file = os.path.join(shakemap_zip_dir(), event_id + '.out.zip') message = 'Expected path for downloaded shakemap OUT not received' self.assertEqual(shakemap_file, expected_file, 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_cache_paths(self): """Check we compute local cache paths properly.""" shake_event = '20120726022003' shake_data = ShakeData(shake_event) expected_input_path = ('/tmp/inasafe/realtime/shakemaps-zipped/' '20120726022003.inp.zip') expected_output_path = ('/tmp/inasafe/realtime/shakemaps-zipped/' '20120726022003.out.zip') input_path, output_path = shake_data.cache_paths() message = 'Expected: %s\nGot: %s' % (expected_input_path, input_path) assert input_path == expected_input_path, message message = 'Expected: %s\nGot: %s' % (expected_output_path, output_path) assert output_path == expected_output_path, message
def test_file_names(self): """Check we compute file names properly.""" shake_event = '20120726022003' shake_data = ShakeData(shake_event) expected_input_file_name = '20120726022003.inp.zip' expected_output_file_name = '20120726022003.out.zip' input_file_name, output_file_name = shake_data.file_names() message = 'Expected: %s\nGot: %s' % (expected_input_file_name, input_file_name) assert input_file_name == expected_input_file_name, message message = 'Expected: %s\nGot: %s' % (expected_output_file_name, output_file_name) assert output_file_name == expected_output_file_name, message
def test_extract_shake_map(self): """Test that we can extract the shakemap inp and out files.""" shake_event = '20120726022003' shake_data = ShakeData(shake_event) grid_xml = shake_data.extract(force_flag=True) extract_dir = shakemap_extract_dir() expected_grid_xml = (os.path.join(extract_dir, '20120726022003/grid.xml')) message = 'Expected: %s\nGot: %s\n' % (expected_grid_xml, grid_xml) assert expected_grid_xml in expected_grid_xml, message assert os.path.exists(grid_xml)
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 testFileNames(self): """Check we compute file names properly.""" myShakeEvent = '20120726022003' myShakeData = ShakeData(myShakeEvent) myExpectedInpFileName = '20120726022003.inp.zip' myExpectedOutFileName = '20120726022003.out.zip' myInpFileName, myOutFileName = myShakeData.fileNames() myMessage = 'Expected: %s\nGot: %s' % (myExpectedInpFileName, myInpFileName) assert myInpFileName == myExpectedInpFileName, myMessage myMessage = 'Expected: %s\nGot: %s' % (myExpectedOutFileName, myOutFileName) assert myOutFileName == myExpectedOutFileName, myMessage
def testExtractShakeMap(self): """Test that we can extract the shakemap inp and out files""" myShakeEvent = '20120726022003' myShakeData = ShakeData(myShakeEvent) myGridXml = myShakeData.extract(theForceFlag=True) myExtractDir = shakemapExtractDir() myExpectedGridXml = (os.path.join(myExtractDir, '20120726022003/grid.xml')) myMessage = 'Expected: %s\nGot: %s\n' % (myExpectedGridXml, myGridXml) assert myExpectedGridXml in myExpectedGridXml, myMessage assert os.path.exists(myGridXml)
def test_file_names(self): """Check we compute file names properly.""" shake_event = '20120726022003' shake_data = ShakeData(shake_event) expected_input_file_name = '20120726022003.inp.zip' expected_output_file_name = '20120726022003.out.zip' input_file_name, output_file_name = shake_data.file_names() message = 'Expected: %s\nGot: %s' % ( expected_input_file_name, input_file_name) assert input_file_name == expected_input_file_name, message message = 'Expected: %s\nGot: %s' % ( expected_output_file_name, output_file_name) assert output_file_name == expected_output_file_name, message
def testFileNames(self): """Check we compute file names properly.""" myShakeEvent = '20120726022003' myShakeData = ShakeData(myShakeEvent) myExpectedInpFileName = '20120726022003.inp.zip' myExpectedOutFileName = '20120726022003.out.zip' myInpFileName, myOutFileName = myShakeData.fileNames() myMessage = 'Expected: %s\nGot: %s' % ( myExpectedInpFileName, myInpFileName) assert myInpFileName == myExpectedInpFileName, myMessage myMessage = 'Expected: %s\nGot: %s' % ( myExpectedOutFileName, myOutFileName) assert myOutFileName == myExpectedOutFileName, myMessage
def testCachePaths(self): """Check we compute local cache paths properly.""" myShakeEvent = '20120726022003' myShakeData = ShakeData(myShakeEvent) myExpectedInpPath = ('/tmp/inasafe/realtime/shakemaps-zipped/' '20120726022003.inp.zip') myExpectedOutPath = ('/tmp/inasafe/realtime/shakemaps-zipped/' '20120726022003.out.zip') myInpPath, myOutPath = myShakeData.cachePaths() myMessage = 'Expected: %s\nGot: %s' % (myExpectedInpPath, myInpPath) assert myInpPath == myExpectedInpPath, myMessage myMessage = 'Expected: %s\nGot: %s' % (myExpectedOutPath, myOutPath) assert myOutPath == myExpectedOutPath, myMessage
def test_extract_shake_map(self): """Test that we can extract the shakemap inp and out files""" shake_event = '20120726022003' shake_data = ShakeData(shake_event) grid_xml = shake_data.extract(force_flag=True) extract_dir = shakemap_extract_dir() expected_grid_xml = (os.path.join(extract_dir, '20120726022003/grid.xml')) message = 'Expected: %s\nGot: %s\n' % (expected_grid_xml, grid_xml) assert expected_grid_xml in expected_grid_xml, message assert os.path.exists(grid_xml)
def test_extract_shake_map(self): """Test that we can extract the shakemap inp and out files.""" shake_data = ShakeData(SHAKE_ID) grid_xml = shake_data.extract(force_flag=True) extract_dir = shakemap_extract_dir() expected_grid_xml = os.path.join( extract_dir, '20120726022003/grid.xml') message = 'Expected: %s\nGot: %s\n' % ( str(expected_grid_xml), str(grid_xml)) self.assertEqual(expected_grid_xml, grid_xml, message) self.assertTrue(os.path.exists(grid_xml))
def test_file_names(self): """Check we compute file names properly.""" shake_data = ShakeData(SHAKE_ID) expected_input_file_name = '20120726022003.inp.zip' expected_output_file_name = '20120726022003.out.zip' input_file_name, output_file_name = shake_data.file_names() message = 'Expected: %s\nGot: %s' % ( expected_input_file_name, input_file_name) self.assertEqual(input_file_name, expected_input_file_name, message) message = 'Expected: %s\nGot: %s' % ( expected_output_file_name, output_file_name) self.assertEqual(output_file_name, expected_output_file_name, message)
def test_cache_paths(self): """Check we compute local cache paths properly.""" shake_data = ShakeData(SHAKE_ID) expected_input_path = ('%s/shakemaps-zipped/20120726022003.inp.zip' % INASAFE_WORK_DIR) expected_output_path = ('%s/shakemaps-zipped/20120726022003.out.zip' % INASAFE_WORK_DIR) input_path, output_path = shake_data.cache_paths() message = 'Expected: %s\nGot: %s' % (expected_input_path, input_path) self.assertEqual(input_path, expected_input_path, message) message = 'Expected: %s\nGot: %s' % (expected_output_path, output_path) self.assertEqual(output_path, expected_output_path, message)
def test_get_latest_shake_map(self): """Check that we can retrieve the latest shake event.""" # Simply dont set the event id in the ctor to get the latest shake_data = ShakeData() input_file, output_file = shake_data.fetch_event() event_id = shake_data.event_id expected_input_file = os.path.join(shakemap_zip_dir(), event_id + '.inp.zip') expected_output_file = os.path.join(shakemap_zip_dir(), event_id + '.out.zip') message = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(input_file, expected_input_file, message) message = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(output_file, expected_output_file, message)
def test_get_cached_shake_map(self): """Check that we can retrieve both input and output from ftp at once""" shake_event = '20120726022003' expected_input_file = os.path.join(shakemap_zip_dir(), shake_event + '.inp.zip') expected_output_file = os.path.join(shakemap_zip_dir(), shake_event + '.out.zip') shake_data = ShakeData(shake_event) input_file, output_file = shake_data.fetch_event() message = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(input_file, expected_input_file, message) message = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(output_file, expected_output_file, message)
def testGetLatestShakeMap(self): """Check that we can retrieve the latest shake event""" # Simply dont set the event id in the ctor to get the latest myShakeData = ShakeData() myInpFile, myOutFile = myShakeData.fetchEvent() myEventId = myShakeData.eventId myExpectedInpFile = os.path.join(shakemapZipDir(), myEventId + '.inp.zip') myExpectedOutFile = os.path.join(shakemapZipDir(), myEventId + '.out.zip') myMessage = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myInpFile, myExpectedInpFile, myMessage) myMessage = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myOutFile, myExpectedOutFile, myMessage)
def test_get_cached_shake_map(self): """Check that we can retrieve both input and output from ftp at once. """ shake_event = '20120726022003' expected_input_file = os.path.join(shakemap_zip_dir(), shake_event + '.inp.zip') expected_output_file = os.path.join(shakemap_zip_dir(), shake_event + '.out.zip') shake_data = ShakeData(shake_event) input_file, output_file = shake_data.fetch_event() message = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(input_file, expected_input_file, message) message = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (expected_output_file, output_file)) self.assertEqual(output_file, expected_output_file, message)
def testCheckEventIsOnServer(self): """Test that we can check if an event is on the server.""" myShakeEvent = '20120726022003' myShakeData = ShakeData(myShakeEvent) self.assertTrue(myShakeData.isOnServer(), ('Data for %s is on server' % myShakeEvent))
def test_check_event_is_on_server(self): """Test that we can check if an event is on the server.""" shake_data = ShakeData(SHAKE_ID) self.assertTrue(shake_data.is_on_server(), ('Data for %s is on server' % SHAKE_ID))
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 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 test_check_event_is_on_server(self): """Test that we can check if an event is on the server.""" shake_event = '20120726022003' shake_data = ShakeData(shake_event) self.assertTrue(shake_data.is_on_server(), ('Data for %s is on server' % shake_event))