def test_write_empty(self): """ Function to check that writing a blank event works as it should. """ from obspy.core.event import Event, Origin from obspy import UTCDateTime import os test_event = Event() with self.assertRaises(IndexError): eventtosfile(test_event, 'TEST', 'L', '.', 'test') test_event.origins.append(Origin()) with self.assertRaises(ValueError): eventtosfile(test_event, 'TEST', 'L', '.', 'test') test_event.origins[0].time = UTCDateTime() test_sfile = eventtosfile(test_event, 'TEST', 'L', '.', 'test') self.assertTrue(os.path.isfile(test_sfile)) os.remove(test_sfile)
def test_download_various_methods(self): """Will download data from server and store in various databases, then create templates using the various methods.""" import obspy if int(obspy.__version__.split('.')[0]) >= 1: from obspy.clients.fdsn import Client from obspy import read_events else: from obspy.fdsn import Client from obspy import readEvents as read_events from obspy.core.event import Catalog from obspy import UTCDateTime from eqcorrscan.utils.sfile_util import eventtosfile import os import shutil client = Client('GEONET') # get the events catalog = Catalog() data_stream = client._download('http://quakeml.geonet.org.nz/' + 'quakeml/1.2/2016p008194') data_stream.seek(0, 0) catalog += read_events(data_stream, format="quakeml") data_stream.close() # Select 3 channels to use and download sta_chans = [(pick.waveform_id.station_code, pick.waveform_id.channel_code) for pick in catalog[0].picks[0:3]] t1 = UTCDateTime(catalog[0].origins[0].time.date) t2 = t1 + 86400 bulk = [('NZ', sta_chan[0], '*', sta_chan[1], t1, t2) for sta_chan in sta_chans] continuous_st = client.get_waveforms_bulk(bulk) continuous_st.merge(fill_value=0) # Test multi_template_gen templates = multi_template_gen(catalog, continuous_st, length=3) self.assertEqual(len(templates), 1) # Test without an event templates = multi_template_gen(Catalog(), continuous_st, length=3) self.assertEqual(len(templates), 0) # Test from contbase method sfile = eventtosfile(catalog[0], 'TEST', 'L', '.', 'None', overwrite=True) os.makedirs(catalog[0].origins[0].time.date.strftime('Y%Y')) os.makedirs(catalog[0].origins[0].time.date. strftime('Y%Y' + os.sep + 'R%j.01')) for tr in continuous_st: tr.write(catalog[0].origins[0].time.date. strftime('Y%Y' + os.sep + 'R%j.01') + os.sep + tr.stats.station + '.' + tr.stats.network + '.' + tr.stats.location + '.' + tr.stats.channel + tr.stats.starttime.strftime('%Y.%j'), format='MSEED') template = from_contbase(sfile, contbase_list=[('.', 'Yyyyy/Rjjj.01', 'NZ')], lowcut=1.0, highcut=5.0, samp_rate=20, filt_order=4, length=3, prepick=0.5, swin='all') shutil.rmtree(continuous_st[0].stats.starttime.strftime('Y%Y'))
def test_download_write(self): """ Function to download quakeML files from a range of datacenters and \ attempt to write miniseed files """ import os from eqcorrscan.utils import sfile_util import obspy if int(obspy.__version__.split('.')[0]) >= 1: from obspy.clients.fdsn import Client from obspy import read_events from obspy.clients.fdsn.header import FDSNException else: from obspy.fdsn import Client from obspy import readEvents as read_events from obspy.fdsn.header import FDSNException import warnings event_list = [('GEONET', '2016p008122'), ('NCEDC', '72572665'), ('USGS', 'nc72597260')] for event_info in event_list: client = Client(event_info[0]) if event_info[0] == 'GEONET': try: data_stream = client.\ _download('http://quakeml.geonet.org.nz/' + 'quakeml/1.2/' + event_info[1]) data_stream.seek(0, 0) event = read_events(data_stream, format="quakeml") data_stream.close() except FDSNException: warnings.warn('FDSNException') continue else: try: event = client.get_events(eventid=event_info[1], includearrivals=True) except FDSNException: warnings.warn('FDSNException') continue test_Sfile_name = sfile_util.eventtosfile(event, 'test', 'L', '.', 'null', overwrite=True) os.remove(test_Sfile_name) return True
def test_download_write(self): """ Function to download quakeML files from a range of datacenters and \ attempt to write miniseed files """ import os from eqcorrscan.utils import sfile_util import obspy if int(obspy.__version__.split('.')[0]) >= 1: from obspy.clients.fdsn import Client from obspy import read_events from obspy.clients.fdsn.header import FDSNException else: from obspy.fdsn import Client from obspy import readEvents as read_events from obspy.fdsn.header import FDSNException import warnings event_list = [('GEONET', '2016p008122'), ('NCEDC', '72572665'), ('USGS', 'nc72597260')] for event_info in event_list: try: client = Client(event_info[0]) if event_info[0] == 'GEONET': data_stream = client.\ _download('http://quakeml.geonet.org.nz/' + 'quakeml/1.2/' + event_info[1]) data_stream.seek(0, 0) event = read_events(data_stream, format="quakeml") data_stream.close() else: event = client.get_events(eventid=event_info[1], includearrivals=True) except FDSNException: warnings.warn('FDSNException') continue test_Sfile_name = sfile_util.eventtosfile(event, 'test', 'L', '.', 'null', overwrite=True) os.remove(test_Sfile_name) return True
def test_read_write(self): """ Function to test the read and write capabilities of sfile_util. """ import os from obspy.core.event import Catalog import obspy if int(obspy.__version__.split('.')[0]) >= 1: from obspy.core.event import read_events else: from obspy.core.event import readEvents as read_events # Set-up a test event test_event = basic_test_event() # Add the event to a catalogue which can be used for QuakeML testing test_cat = Catalog() test_cat += test_event # Write the catalog test_cat.write("Test_catalog.xml", format='QUAKEML') # Read and check read_cat = read_events("Test_catalog.xml") os.remove("Test_catalog.xml") self.assertEqual(read_cat[0].resource_id, test_cat[0].resource_id) self.assertEqual(read_cat[0].picks, test_cat[0].picks) self.assertEqual(read_cat[0].origins[0].resource_id, test_cat[0].origins[0].resource_id) self.assertEqual(read_cat[0].origins[0].time, test_cat[0].origins[0].time) # Note that time_residuel_RMS is not a quakeML format self.assertEqual(read_cat[0].origins[0].longitude, test_cat[0].origins[0].longitude) self.assertEqual(read_cat[0].origins[0].latitude, test_cat[0].origins[0].latitude) self.assertEqual(read_cat[0].origins[0].depth, test_cat[0].origins[0].depth) self.assertEqual(read_cat[0].magnitudes, test_cat[0].magnitudes) self.assertEqual(read_cat[0].event_descriptions, test_cat[0].event_descriptions) self.assertEqual(read_cat[0].amplitudes[0].resource_id, test_cat[0].amplitudes[0].resource_id) self.assertEqual(read_cat[0].amplitudes[0].period, test_cat[0].amplitudes[0].period) self.assertEqual(read_cat[0].amplitudes[0].unit, test_cat[0].amplitudes[0].unit) self.assertEqual(read_cat[0].amplitudes[0].generic_amplitude, test_cat[0].amplitudes[0].generic_amplitude) self.assertEqual(read_cat[0].amplitudes[0].pick_id, test_cat[0].amplitudes[0].pick_id) self.assertEqual(read_cat[0].amplitudes[0].waveform_id, test_cat[0].amplitudes[0].waveform_id) # Check the read-write s-file functionality sfile = eventtosfile(test_cat[0], userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) del read_cat self.assertEqual(readwavename(sfile), ['test']) read_cat = Catalog() read_cat += readpicks(sfile) os.remove(sfile) self.assertEqual(read_cat[0].picks[0].time, test_cat[0].picks[0].time) self.assertEqual(read_cat[0].picks[0].backazimuth, test_cat[0].picks[0].backazimuth) self.assertEqual(read_cat[0].picks[0].onset, test_cat[0].picks[0].onset) self.assertEqual(read_cat[0].picks[0].phase_hint, test_cat[0].picks[0].phase_hint) self.assertEqual(read_cat[0].picks[0].polarity, test_cat[0].picks[0].polarity) self.assertEqual(read_cat[0].picks[0].waveform_id.station_code, test_cat[0].picks[0].waveform_id.station_code) self.assertEqual(read_cat[0].picks[0].waveform_id.channel_code[-1], test_cat[0].picks[0].waveform_id.channel_code[-1]) # assert read_cat[0].origins[0].resource_id ==\ # test_cat[0].origins[0].resource_id self.assertEqual(read_cat[0].origins[0].time, test_cat[0].origins[0].time) # Note that time_residuel_RMS is not a quakeML format self.assertEqual(read_cat[0].origins[0].longitude, test_cat[0].origins[0].longitude) self.assertEqual(read_cat[0].origins[0].latitude, test_cat[0].origins[0].latitude) self.assertEqual(read_cat[0].origins[0].depth, test_cat[0].origins[0].depth) self.assertEqual(read_cat[0].magnitudes[0].mag, test_cat[0].magnitudes[0].mag) self.assertEqual(read_cat[0].magnitudes[1].mag, test_cat[0].magnitudes[1].mag) self.assertEqual(read_cat[0].magnitudes[2].mag, test_cat[0].magnitudes[2].mag) self.assertEqual(read_cat[0].magnitudes[0].creation_info, test_cat[0].magnitudes[0].creation_info) self.assertEqual(read_cat[0].magnitudes[1].creation_info, test_cat[0].magnitudes[1].creation_info) self.assertEqual(read_cat[0].magnitudes[2].creation_info, test_cat[0].magnitudes[2].creation_info) self.assertEqual(read_cat[0].magnitudes[0].magnitude_type, test_cat[0].magnitudes[0].magnitude_type) self.assertEqual(read_cat[0].magnitudes[1].magnitude_type, test_cat[0].magnitudes[1].magnitude_type) self.assertEqual(read_cat[0].magnitudes[2].magnitude_type, test_cat[0].magnitudes[2].magnitude_type) self.assertEqual(read_cat[0].event_descriptions, test_cat[0].event_descriptions) # assert read_cat[0].amplitudes[0].resource_id ==\ # test_cat[0].amplitudes[0].resource_id self.assertEqual(read_cat[0].amplitudes[0].period, test_cat[0].amplitudes[0].period) self.assertEqual(read_cat[0].amplitudes[0].snr, test_cat[0].amplitudes[0].snr) del read_cat # assert read_cat[0].amplitudes[0].pick_id ==\ # test_cat[0].amplitudes[0].pick_id # assert read_cat[0].amplitudes[0].waveform_id ==\ # test_cat[0].amplitudes[0].waveform_id # Test the wrappers for PICK and EVENTINFO classes picks, evinfo = eventtopick(test_cat) # Test the conversion back conv_cat = Catalog() conv_cat.append(picktoevent(evinfo, picks)) self.assertEqual(conv_cat[0].picks[0].time, test_cat[0].picks[0].time) self.assertEqual(conv_cat[0].picks[0].backazimuth, test_cat[0].picks[0].backazimuth) self.assertEqual(conv_cat[0].picks[0].onset, test_cat[0].picks[0].onset) self.assertEqual(conv_cat[0].picks[0].phase_hint, test_cat[0].picks[0].phase_hint) self.assertEqual(conv_cat[0].picks[0].polarity, test_cat[0].picks[0].polarity) self.assertEqual(conv_cat[0].picks[0].waveform_id.station_code, test_cat[0].picks[0].waveform_id.station_code) self.assertEqual(conv_cat[0].picks[0].waveform_id.channel_code[-1], test_cat[0].picks[0].waveform_id.channel_code[-1]) # self.assertEqual(read_cat[0].origins[0].resource_id, # test_cat[0].origins[0].resource_id) self.assertEqual(conv_cat[0].origins[0].time, test_cat[0].origins[0].time) # Note that time_residuel_RMS is not a quakeML format self.assertEqual(conv_cat[0].origins[0].longitude, test_cat[0].origins[0].longitude) self.assertEqual(conv_cat[0].origins[0].latitude, test_cat[0].origins[0].latitude) self.assertEqual(conv_cat[0].origins[0].depth, test_cat[0].origins[0].depth) self.assertEqual(conv_cat[0].magnitudes[0].mag, test_cat[0].magnitudes[0].mag) self.assertEqual(conv_cat[0].magnitudes[1].mag, test_cat[0].magnitudes[1].mag) self.assertEqual(conv_cat[0].magnitudes[2].mag, test_cat[0].magnitudes[2].mag) self.assertEqual(conv_cat[0].magnitudes[0].creation_info, test_cat[0].magnitudes[0].creation_info) self.assertEqual(conv_cat[0].magnitudes[1].creation_info, test_cat[0].magnitudes[1].creation_info) self.assertEqual(conv_cat[0].magnitudes[2].creation_info, test_cat[0].magnitudes[2].creation_info) self.assertEqual(conv_cat[0].magnitudes[0].magnitude_type, test_cat[0].magnitudes[0].magnitude_type) self.assertEqual(conv_cat[0].magnitudes[1].magnitude_type, test_cat[0].magnitudes[1].magnitude_type) self.assertEqual(conv_cat[0].magnitudes[2].magnitude_type, test_cat[0].magnitudes[2].magnitude_type) self.assertEqual(conv_cat[0].event_descriptions, test_cat[0].event_descriptions) # self.assertEqual(read_cat[0].amplitudes[0].resource_id, # test_cat[0].amplitudes[0].resource_id) self.assertEqual(conv_cat[0].amplitudes[0].period, test_cat[0].amplitudes[0].period) self.assertEqual(conv_cat[0].amplitudes[0].snr, test_cat[0].amplitudes[0].snr)
def test_read_write(self): """ Function to test the read and write capabilities of sfile_util. """ import os from obspy.core.event import Catalog import obspy if int(obspy.__version__.split('.')[0]) >= 1: from obspy.core.event import read_events else: from obspy.core.event import readEvents as read_events # Set-up a test event test_event = full_test_event() # Add the event to a catalogue which can be used for QuakeML testing test_cat = Catalog() test_cat += test_event # Write the catalog test_cat.write("Test_catalog.xml", format='QUAKEML') # Read and check read_cat = read_events("Test_catalog.xml") os.remove("Test_catalog.xml") self.assertEqual(read_cat[0].resource_id, test_cat[0].resource_id) for i in range(len(read_cat[0].picks)): for key in read_cat[0].picks[i].keys(): # Ignore backazimuth errors and horizontal_slowness_errors if key in ['backazimuth_errors', 'horizontal_slowness_errors']: continue self.assertEqual(read_cat[0].picks[i][key], test_cat[0].picks[i][key]) self.assertEqual(read_cat[0].origins[0].resource_id, test_cat[0].origins[0].resource_id) self.assertEqual(read_cat[0].origins[0].time, test_cat[0].origins[0].time) # Note that time_residual_RMS is not a quakeML format self.assertEqual(read_cat[0].origins[0].longitude, test_cat[0].origins[0].longitude) self.assertEqual(read_cat[0].origins[0].latitude, test_cat[0].origins[0].latitude) self.assertEqual(read_cat[0].origins[0].depth, test_cat[0].origins[0].depth) # Check magnitudes self.assertEqual(read_cat[0].magnitudes, test_cat[0].magnitudes) self.assertEqual(read_cat[0].event_descriptions, test_cat[0].event_descriptions) # Check local magnitude amplitude self.assertEqual(read_cat[0].amplitudes[0].resource_id, test_cat[0].amplitudes[0].resource_id) self.assertEqual(read_cat[0].amplitudes[0].period, test_cat[0].amplitudes[0].period) self.assertEqual(read_cat[0].amplitudes[0].unit, test_cat[0].amplitudes[0].unit) self.assertEqual(read_cat[0].amplitudes[0].generic_amplitude, test_cat[0].amplitudes[0].generic_amplitude) self.assertEqual(read_cat[0].amplitudes[0].pick_id, test_cat[0].amplitudes[0].pick_id) self.assertEqual(read_cat[0].amplitudes[0].waveform_id, test_cat[0].amplitudes[0].waveform_id) # Check coda magnitude pick self.assertEqual(read_cat[0].amplitudes[1].resource_id, test_cat[0].amplitudes[1].resource_id) self.assertEqual(read_cat[0].amplitudes[1].type, test_cat[0].amplitudes[1].type) self.assertEqual(read_cat[0].amplitudes[1].unit, test_cat[0].amplitudes[1].unit) self.assertEqual(read_cat[0].amplitudes[1].generic_amplitude, test_cat[0].amplitudes[1].generic_amplitude) self.assertEqual(read_cat[0].amplitudes[1].pick_id, test_cat[0].amplitudes[1].pick_id) self.assertEqual(read_cat[0].amplitudes[1].waveform_id, test_cat[0].amplitudes[1].waveform_id) self.assertEqual(read_cat[0].amplitudes[1].magnitude_hint, test_cat[0].amplitudes[1].magnitude_hint) self.assertEqual(read_cat[0].amplitudes[1].snr, test_cat[0].amplitudes[1].snr) self.assertEqual(read_cat[0].amplitudes[1].category, test_cat[0].amplitudes[1].category) # Check the read-write s-file functionality sfile = eventtosfile(test_cat[0], userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) del read_cat self.assertEqual(readwavename(sfile), ['test']) read_cat = Catalog() read_cat += readpicks(sfile) os.remove(sfile) for i in range(len(read_cat[0].picks)): self.assertEqual(read_cat[0].picks[i].time, test_cat[0].picks[i].time) self.assertEqual(read_cat[0].picks[i].backazimuth, test_cat[0].picks[i].backazimuth) self.assertEqual(read_cat[0].picks[i].onset, test_cat[0].picks[i].onset) self.assertEqual(read_cat[0].picks[i].phase_hint, test_cat[0].picks[i].phase_hint) self.assertEqual(read_cat[0].picks[i].polarity, test_cat[0].picks[i].polarity) self.assertEqual(read_cat[0].picks[i].waveform_id.station_code, test_cat[0].picks[i].waveform_id.station_code) self.assertEqual(read_cat[0].picks[i].waveform_id.channel_code[-1], test_cat[0].picks[i].waveform_id.channel_code[-1]) # assert read_cat[0].origins[0].resource_id ==\ # test_cat[0].origins[0].resource_id self.assertEqual(read_cat[0].origins[0].time, test_cat[0].origins[0].time) # Note that time_residual_RMS is not a quakeML format self.assertEqual(read_cat[0].origins[0].longitude, test_cat[0].origins[0].longitude) self.assertEqual(read_cat[0].origins[0].latitude, test_cat[0].origins[0].latitude) self.assertEqual(read_cat[0].origins[0].depth, test_cat[0].origins[0].depth) self.assertEqual(read_cat[0].magnitudes[0].mag, test_cat[0].magnitudes[0].mag) self.assertEqual(read_cat[0].magnitudes[1].mag, test_cat[0].magnitudes[1].mag) self.assertEqual(read_cat[0].magnitudes[2].mag, test_cat[0].magnitudes[2].mag) self.assertEqual(read_cat[0].magnitudes[0].creation_info, test_cat[0].magnitudes[0].creation_info) self.assertEqual(read_cat[0].magnitudes[1].creation_info, test_cat[0].magnitudes[1].creation_info) self.assertEqual(read_cat[0].magnitudes[2].creation_info, test_cat[0].magnitudes[2].creation_info) self.assertEqual(read_cat[0].magnitudes[0].magnitude_type, test_cat[0].magnitudes[0].magnitude_type) self.assertEqual(read_cat[0].magnitudes[1].magnitude_type, test_cat[0].magnitudes[1].magnitude_type) self.assertEqual(read_cat[0].magnitudes[2].magnitude_type, test_cat[0].magnitudes[2].magnitude_type) self.assertEqual(read_cat[0].event_descriptions, test_cat[0].event_descriptions) # assert read_cat[0].amplitudes[0].resource_id ==\ # test_cat[0].amplitudes[0].resource_id self.assertEqual(read_cat[0].amplitudes[0].period, test_cat[0].amplitudes[0].period) self.assertEqual(read_cat[0].amplitudes[0].snr, test_cat[0].amplitudes[0].snr) # Check coda magnitude pick # Resource ids get overwritten because you can't have two the same in # memory # self.assertEqual(read_cat[0].amplitudes[1].resource_id, # test_cat[0].amplitudes[1].resource_id) self.assertEqual(read_cat[0].amplitudes[1].type, test_cat[0].amplitudes[1].type) self.assertEqual(read_cat[0].amplitudes[1].unit, test_cat[0].amplitudes[1].unit) self.assertEqual(read_cat[0].amplitudes[1].generic_amplitude, test_cat[0].amplitudes[1].generic_amplitude) # Resource ids get overwritten because you can't have two the same in # memory # self.assertEqual(read_cat[0].amplitudes[1].pick_id, # test_cat[0].amplitudes[1].pick_id) self.assertEqual(read_cat[0].amplitudes[1].waveform_id.station_code, test_cat[0].amplitudes[1].waveform_id.station_code) self.assertEqual( read_cat[0].amplitudes[1].waveform_id.channel_code, test_cat[0].amplitudes[1].waveform_id.channel_code[0] + test_cat[0].amplitudes[1].waveform_id.channel_code[-1]) self.assertEqual(read_cat[0].amplitudes[1].magnitude_hint, test_cat[0].amplitudes[1].magnitude_hint) # snr is not supported in s-file # self.assertEqual(read_cat[0].amplitudes[1].snr, # test_cat[0].amplitudes[1].snr) self.assertEqual(read_cat[0].amplitudes[1].category, test_cat[0].amplitudes[1].category) del read_cat # Test a deliberate fail test_cat.append(full_test_event()) with self.assertRaises(IOError): # Raises error due to multiple events in catalog sfile = eventtosfile(test_cat, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Raises error due to too long userID sfile = eventtosfile(test_cat[0], userID='TESTICLE', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Raises error due to unrecognised event type sfile = eventtosfile(test_cat[0], userID='TEST', evtype='U', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Raises error due to no output directory sfile = eventtosfile(test_cat[0], userID='TEST', evtype='L', outdir='albatross', wavefiles='test', explosion=True, overwrite=True) # Raises error due to incorrect wavefil formatting sfile = eventtosfile(test_cat[0], userID='TEST', evtype='L', outdir='.', wavefiles=1234, explosion=True, overwrite=True) with self.assertRaises(IndexError): invalid_origin = test_cat[0].copy() invalid_origin.origins = [] sfile = eventtosfile(invalid_origin, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) with self.assertRaises(ValueError): invalid_origin = test_cat[0].copy() invalid_origin.origins[0].time = None sfile = eventtosfile(invalid_origin, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Write a near empty origin valid_origin = test_cat[0].copy() valid_origin.origins[0].latitude = None valid_origin.origins[0].longitude = None valid_origin.origins[0].depth = None sfile = eventtosfile(valid_origin, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) self.assertTrue(os.path.isfile(sfile)) os.remove(sfile)
def test_read_write(self): """ Function to test the read and write capabilities of sfile_util. """ import os from obspy.core.event import Catalog import obspy if int(obspy.__version__.split('.')[0]) >= 1: from obspy.core.event import read_events else: from obspy.core.event import readEvents as read_events # Set-up a test event test_event = full_test_event() # Add the event to a catalogue which can be used for QuakeML testing test_cat = Catalog() test_cat += test_event # Write the catalog test_cat.write("Test_catalog.xml", format='QUAKEML') # Read and check read_cat = read_events("Test_catalog.xml") os.remove("Test_catalog.xml") self.assertEqual(read_cat[0].resource_id, test_cat[0].resource_id) for i in range(len(read_cat[0].picks)): for key in read_cat[0].picks[i].keys(): # Ignore backazimuth errors and horizontal_slowness_errors if key in ['backazimuth_errors', 'horizontal_slowness_errors']: continue self.assertEqual(read_cat[0].picks[i][key], test_cat[0].picks[i][key]) self.assertEqual(read_cat[0].origins[0].resource_id, test_cat[0].origins[0].resource_id) self.assertEqual(read_cat[0].origins[0].time, test_cat[0].origins[0].time) # Note that time_residual_RMS is not a quakeML format self.assertEqual(read_cat[0].origins[0].longitude, test_cat[0].origins[0].longitude) self.assertEqual(read_cat[0].origins[0].latitude, test_cat[0].origins[0].latitude) self.assertEqual(read_cat[0].origins[0].depth, test_cat[0].origins[0].depth) # Check magnitudes self.assertEqual(read_cat[0].magnitudes, test_cat[0].magnitudes) self.assertEqual(read_cat[0].event_descriptions, test_cat[0].event_descriptions) # Check local magnitude amplitude self.assertEqual(read_cat[0].amplitudes[0].resource_id, test_cat[0].amplitudes[0].resource_id) self.assertEqual(read_cat[0].amplitudes[0].period, test_cat[0].amplitudes[0].period) self.assertEqual(read_cat[0].amplitudes[0].unit, test_cat[0].amplitudes[0].unit) self.assertEqual(read_cat[0].amplitudes[0].generic_amplitude, test_cat[0].amplitudes[0].generic_amplitude) self.assertEqual(read_cat[0].amplitudes[0].pick_id, test_cat[0].amplitudes[0].pick_id) self.assertEqual(read_cat[0].amplitudes[0].waveform_id, test_cat[0].amplitudes[0].waveform_id) # Check coda magnitude pick self.assertEqual(read_cat[0].amplitudes[1].resource_id, test_cat[0].amplitudes[1].resource_id) self.assertEqual(read_cat[0].amplitudes[1].type, test_cat[0].amplitudes[1].type) self.assertEqual(read_cat[0].amplitudes[1].unit, test_cat[0].amplitudes[1].unit) self.assertEqual(read_cat[0].amplitudes[1].generic_amplitude, test_cat[0].amplitudes[1].generic_amplitude) self.assertEqual(read_cat[0].amplitudes[1].pick_id, test_cat[0].amplitudes[1].pick_id) self.assertEqual(read_cat[0].amplitudes[1].waveform_id, test_cat[0].amplitudes[1].waveform_id) self.assertEqual(read_cat[0].amplitudes[1].magnitude_hint, test_cat[0].amplitudes[1].magnitude_hint) self.assertEqual(read_cat[0].amplitudes[1].snr, test_cat[0].amplitudes[1].snr) self.assertEqual(read_cat[0].amplitudes[1].category, test_cat[0].amplitudes[1].category) # Check the read-write s-file functionality sfile = eventtosfile(test_cat[0], userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) del read_cat self.assertEqual(readwavename(sfile), ['test']) read_cat = Catalog() read_cat += readpicks(sfile) os.remove(sfile) for i in range(len(read_cat[0].picks)): self.assertEqual(read_cat[0].picks[i].time, test_cat[0].picks[i].time) self.assertEqual(read_cat[0].picks[i].backazimuth, test_cat[0].picks[i].backazimuth) self.assertEqual(read_cat[0].picks[i].onset, test_cat[0].picks[i].onset) self.assertEqual(read_cat[0].picks[i].phase_hint, test_cat[0].picks[i].phase_hint) self.assertEqual(read_cat[0].picks[i].polarity, test_cat[0].picks[i].polarity) self.assertEqual(read_cat[0].picks[i].waveform_id.station_code, test_cat[0].picks[i].waveform_id.station_code) self.assertEqual(read_cat[0].picks[i].waveform_id.channel_code[-1], test_cat[0].picks[i].waveform_id.channel_code[-1]) # assert read_cat[0].origins[0].resource_id ==\ # test_cat[0].origins[0].resource_id self.assertEqual(read_cat[0].origins[0].time, test_cat[0].origins[0].time) # Note that time_residual_RMS is not a quakeML format self.assertEqual(read_cat[0].origins[0].longitude, test_cat[0].origins[0].longitude) self.assertEqual(read_cat[0].origins[0].latitude, test_cat[0].origins[0].latitude) self.assertEqual(read_cat[0].origins[0].depth, test_cat[0].origins[0].depth) self.assertEqual(read_cat[0].magnitudes[0].mag, test_cat[0].magnitudes[0].mag) self.assertEqual(read_cat[0].magnitudes[1].mag, test_cat[0].magnitudes[1].mag) self.assertEqual(read_cat[0].magnitudes[2].mag, test_cat[0].magnitudes[2].mag) self.assertEqual(read_cat[0].magnitudes[0].creation_info, test_cat[0].magnitudes[0].creation_info) self.assertEqual(read_cat[0].magnitudes[1].creation_info, test_cat[0].magnitudes[1].creation_info) self.assertEqual(read_cat[0].magnitudes[2].creation_info, test_cat[0].magnitudes[2].creation_info) self.assertEqual(read_cat[0].magnitudes[0].magnitude_type, test_cat[0].magnitudes[0].magnitude_type) self.assertEqual(read_cat[0].magnitudes[1].magnitude_type, test_cat[0].magnitudes[1].magnitude_type) self.assertEqual(read_cat[0].magnitudes[2].magnitude_type, test_cat[0].magnitudes[2].magnitude_type) self.assertEqual(read_cat[0].event_descriptions, test_cat[0].event_descriptions) # assert read_cat[0].amplitudes[0].resource_id ==\ # test_cat[0].amplitudes[0].resource_id self.assertEqual(read_cat[0].amplitudes[0].period, test_cat[0].amplitudes[0].period) self.assertEqual(read_cat[0].amplitudes[0].snr, test_cat[0].amplitudes[0].snr) # Check coda magnitude pick # Resource ids get overwritten because you can't have two the same in # memory # self.assertEqual(read_cat[0].amplitudes[1].resource_id, # test_cat[0].amplitudes[1].resource_id) self.assertEqual(read_cat[0].amplitudes[1].type, test_cat[0].amplitudes[1].type) self.assertEqual(read_cat[0].amplitudes[1].unit, test_cat[0].amplitudes[1].unit) self.assertEqual(read_cat[0].amplitudes[1].generic_amplitude, test_cat[0].amplitudes[1].generic_amplitude) # Resource ids get overwritten because you can't have two the same in # memory # self.assertEqual(read_cat[0].amplitudes[1].pick_id, # test_cat[0].amplitudes[1].pick_id) self.assertEqual(read_cat[0].amplitudes[1].waveform_id.station_code, test_cat[0].amplitudes[1].waveform_id.station_code) self.assertEqual(read_cat[0].amplitudes[1].waveform_id.channel_code, test_cat[0].amplitudes[1]. waveform_id.channel_code[0] + test_cat[0].amplitudes[1]. waveform_id.channel_code[-1]) self.assertEqual(read_cat[0].amplitudes[1].magnitude_hint, test_cat[0].amplitudes[1].magnitude_hint) # snr is not supported in s-file # self.assertEqual(read_cat[0].amplitudes[1].snr, # test_cat[0].amplitudes[1].snr) self.assertEqual(read_cat[0].amplitudes[1].category, test_cat[0].amplitudes[1].category) del read_cat # Test a deliberate fail test_cat.append(full_test_event()) with self.assertRaises(IOError): # Raises error due to multiple events in catalog sfile = eventtosfile(test_cat, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Raises error due to too long userID sfile = eventtosfile(test_cat[0], userID='TESTICLE', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Raises error due to unrecognised event type sfile = eventtosfile(test_cat[0], userID='TEST', evtype='U', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Raises error due to no output directory sfile = eventtosfile(test_cat[0], userID='TEST', evtype='L', outdir='albatross', wavefiles='test', explosion=True, overwrite=True) # Raises error due to incorrect wavefil formatting sfile = eventtosfile(test_cat[0], userID='TEST', evtype='L', outdir='.', wavefiles=1234, explosion=True, overwrite=True) with self.assertRaises(IndexError): invalid_origin = test_cat[0].copy() invalid_origin.origins = [] sfile = eventtosfile(invalid_origin, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) with self.assertRaises(ValueError): invalid_origin = test_cat[0].copy() invalid_origin.origins[0].time = None sfile = eventtosfile(invalid_origin, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) # Write a near empty origin valid_origin = test_cat[0].copy() valid_origin.origins[0].latitude = None valid_origin.origins[0].longitude = None valid_origin.origins[0].depth = None sfile = eventtosfile(valid_origin, userID='TEST', evtype='L', outdir='.', wavefiles='test', explosion=True, overwrite=True) self.assertTrue(os.path.isfile(sfile)) os.remove(sfile)
def amp_pick_sfile(sfile, datapath, respdir, chans=['Z'], var_wintype=True, winlen=0.9, pre_pick=0.2, pre_filt=True, lowcut=1.0, highcut=20.0, corners=4): """ Function to pick amplitudes for local magnitudes from NORDIC s-files. Reads information from a SEISAN s-file, load the data and the \ picks, cut the data for the channels given around the S-window, simulate \ a Wood Anderson seismometer, then pick the maximum peak-to-trough \ amplitude. Output will be put into a mag_calc.out file which will be in full S-file \ format and can be copied to a REA database. :type sfile: str :param sfile: Path to NORDIC format s-file :type datapath: str :param datapath: Path to the waveform files - usually the path to the WAV \ directory :type respdir: str :param respdir: Path to the response information directory :type chans: list :param chans: List of the channels to pick on, defaults to ['Z'] - should \ just be the orientations, e.g. Z,1,2,N,E :type var_wintype: bool :param var_wintype: If True, the winlen will be \ multiplied by the P-S time if both P and S picks are \ available, otherwise it will be multiplied by the \ hypocentral distance*0.34 - derived using a p-s ratio of \ 1.68 and S-velocity of 1.5km/s to give a large window, \ defaults to True :type winlen: float :param winlen: Length of window, see above parameter, if var_wintype is \ False then this will be in seconds, otherwise it is the \ multiplier to the p-s time, defaults to 0.5. :type pre_pick: float :param pre_pick: Time before the s-pick to start the cut window, defaults \ to 0.2 :type pre_filt: bool :param pre_filt: To apply a pre-filter or not, defaults to True :type lowcut: float :param lowcut: Lowcut in Hz for the pre-filter, defaults to 1.0 :type highcut: float :param highcut: Highcut in Hz for the pre-filter, defaults to 20.0 :type corners: int :param corners: Number of corners to use in the pre-filter :returns: obspy.core.event """ from eqcorrscan.utils import sfile_util from obspy import read import shutil # First we need to work out what stations have what picks event = sfile_util.readpicks(sfile) # Read in waveforms stream = read(datapath+'/'+sfile_util.readwavename(sfile)[0]) if len(sfile_util.readwavename(sfile)) > 1: for wavfile in sfile_util.readwavename(sfile): stream += read(datapath+'/'+wavfile) stream.merge() # merge the data, just in case! event_picked = amp_pick_event(event=event, st=stream, respdir=respdir, chans=chans, var_wintype=var_wintype, winlen=winlen, pre_pick=pre_pick, pre_filt=pre_filt, lowcut=lowcut, highcut=highcut, corners=corners) new_sfile = sfile_util.eventtosfile(event=event, userID=str('EQCO'), evtype=str('L'), outdir=str('.'), wavefiles=sfile_util. readwavename(sfile)) shutil.move(new_sfile, 'mag_calc.out') return event
def amp_pick_sfile(sfile, datapath, respdir, chans=['Z'], var_wintype=True, winlen=0.9, pre_pick=0.2, pre_filt=True, lowcut=1.0, highcut=20.0, corners=4): """ Function to pick amplitudes for local magnitudes from NORDIC s-files. Reads information from a SEISAN s-file, load the data and the \ picks, cut the data for the channels given around the S-window, simulate \ a Wood Anderson seismometer, then pick the maximum peak-to-trough \ amplitude. Output will be put into a mag_calc.out file which will be in full S-file \ format and can be copied to a REA database. :type sfile: str :param sfile: Path to NORDIC format s-file :type datapath: str :param datapath: Path to the waveform files - usually the path to the WAV \ directory :type respdir: str :param respdir: Path to the response information directory :type chans: list :param chans: List of the channels to pick on, defaults to ['Z'] - should \ just be the orientations, e.g. Z,1,2,N,E :type var_wintype: bool :param var_wintype: If True, the winlen will be \ multiplied by the P-S time if both P and S picks are \ available, otherwise it will be multiplied by the \ hypocentral distance*0.34 - derived using a p-s ratio of \ 1.68 and S-velocity of 1.5km/s to give a large window, \ defaults to True :type winlen: float :param winlen: Length of window, see above parameter, if var_wintype is \ False then this will be in seconds, otherwise it is the \ multiplier to the p-s time, defaults to 0.5. :type pre_pick: float :param pre_pick: Time before the s-pick to start the cut window, defaults \ to 0.2 :type pre_filt: bool :param pre_filt: To apply a pre-filter or not, defaults to True :type lowcut: float :param lowcut: Lowcut in Hz for the pre-filter, defaults to 1.0 :type highcut: float :param highcut: Highcut in Hz for the pre-filter, defaults to 20.0 :type corners: int :param corners: Number of corners to use in the pre-filter :returns: obspy.core.event """ from eqcorrscan.utils import sfile_util from obspy import read import shutil # First we need to work out what stations have what picks event = sfile_util.readpicks(sfile) # Read in waveforms stream = read(datapath + '/' + sfile_util.readwavename(sfile)[0]) if len(sfile_util.readwavename(sfile)) > 1: for wavfile in sfile_util.readwavename(sfile): stream += read(datapath + '/' + wavfile) stream.merge() # merge the data, just in case! event_picked = amp_pick_event(event=event, st=stream, respdir=respdir, chans=chans, var_wintype=var_wintype, winlen=winlen, pre_pick=pre_pick, pre_filt=pre_filt, lowcut=lowcut, highcut=highcut, corners=corners) new_sfile = sfile_util.eventtosfile( event=event, userID=str('EQCO'), evtype=str('L'), outdir=str('.'), wavefiles=sfile_util.readwavename(sfile)) shutil.move(new_sfile, 'mag_calc.out') return event