def test_merge(self): """ Ensure the merge option of read_rg16 merges all contiguous traces together. """ for fcnt_file in FCNT_FILES: st_merged = rc._read_rg16(fcnt_file, merge=True) st = rc._read_rg16(fcnt_file).merge() self.assertEqual(len(st), len(st_merged)) self.assertEqual(st, st_merged)
def test_merge(self): """ Ensure the merge option of read_rg16 merges all contiguous traces together. """ for fcnt_file in FCNT_FILES: st_merged = _read_rg16(fcnt_file, merge=True) st = _read_rg16(fcnt_file).merge() self.assertEqual(len(st), len(st_merged)) self.assertEqual(st, st_merged)
def test_can_read_from_buffer(self): """ Ensure each stream can be read from a buffer. """ for fcnt_file in FCNT_FILES: with open(fcnt_file, 'rb') as fi: buff = io.BytesIO(fi.read()) buff.seek(0) try: _read_rg16(buff, 'mseed') except Exception: self.fail('failed to read from bytesIO')
def test_headonly_option(self): """ Ensure no data is returned when the option headonly is used. """ st = rc._read_rg16(THREE_CHAN_FCNT, headonly=True) for tr in st: self.assertEqual(len(tr.data), 0) self.assertNotEqual(tr.stats.npts, 0)
def test_contacts_north_and_merge(self): """ Ensure the "contacts_north" and "merge" parameters can be used together. See #2198. """ for filename in FCNT_FILES: st = rc._read_rg16(filename, contacts_north=True, merge=True) assert isinstance(st, obspy.Stream)
def test_contacts_north_and_merge(self): """ Ensure the "contacts_north" and "merge" parameters can be used together. See #2198. """ for filename in FCNT_FILES: st = _read_rg16(filename, contacts_north=True, merge=True) assert isinstance(st, obspy.Stream)
def test_starttime_endtime_option(self): """ Test the options starttime and endtime """ t1 = UTCDateTime(2017, 8, 9, 16, 0, 15) t2 = UTCDateTime(2017, 8, 9, 16, 0, 45) # read streams for testing. The three channel rg16 file has 6 traces # but the streams may have less depending on the starttime/endtime st = rc._read_rg16(THREE_CHAN_FCNT) # no time filtering st1 = rc._read_rg16(THREE_CHAN_FCNT, starttime=t1, endtime=t2) # test using starttime and endtime self.assertEqual(len(st1), len(st)) for tr, tr1 in zip(st, st1): self.assertEqual(tr.stats.starttime, tr1.stats.starttime) self.assertEqual(tr.stats.endtime, tr1.stats.endtime)
def test_starttime_endtime_option(self): """ Test the the options starttime and endtime """ t1 = UTCDateTime(2017, 8, 9, 16, 0, 15) t2 = UTCDateTime(2017, 8, 9, 16, 0, 45) # read streams for testing. The three channel rg16 file has 6 traces # but the streams may have less depending on the starttime/endtime st = rc._read_rg16(THREE_CHAN_FCNT) # no time filtering st1 = rc._read_rg16(THREE_CHAN_FCNT, starttime=t1, endtime=t2) # test using starttime and endtime self.assertEqual(len(st1), len(st)) for tr, tr1 in zip(st, st1): self.assertEqual(tr.stats.starttime, tr1.stats.starttime) self.assertEqual(tr.stats.endtime, tr1.stats.endtime)
def test_headonly_option(self): """ Ensure no data is returned when the option headonly is used. """ st = rc._read_rg16(THREE_CHAN_FCNT, headonly=True) for tr in st: self.assertEqual(len(tr.data), 0) self.assertNotEqual(tr.stats.npts, 0)
def test_intrablock_starttime_endtime(self): """ Test starttime/endtime options when starttime and endtime are comprised in a data block. """ t1 = UTCDateTime(2017, 8, 9, 16, 0, 47) t2 = UTCDateTime(2017, 8, 9, 16, 0, 58) # read streams for testing st = rc._read_rg16(THREE_CHAN_FCNT) # no time filtering st1 = rc._read_rg16(THREE_CHAN_FCNT, starttime=t1, endtime=t2) # test when starttime and endtime are comprised in a data packet. self.assertEqual(len(st1), 3) for tr, tr1 in zip(st[1::2], st1): self.assertEqual(tr.stats.starttime, tr1.stats.starttime) self.assertEqual(tr.stats.endtime, tr1.stats.endtime)
def test_no_data(self): """ Ensure no data is returned when the option is used. """ for fcnt_file in FCNT_FILES: st = _read_rg16(fcnt_file, headonly=True) for tr in st: self.assertEqual(len(tr.data), 0) self.assertNotEqual(tr.stats.npts, 0)
def test_starttime_endtime(self): """ Ensure starttimes and endtimes filter traces returned. """ for fcnt_file in FCNT_FILES: # get good times to filter on st = _read_rg16(fcnt_file, headonly=True) stats = st[0].stats t1, t2 = stats.starttime.timestamp, stats.endtime.timestamp tpoint = obspy.UTCDateTime((t1 + t2) / 2.) # this should only return one trace for each channel st = _read_rg16(fcnt_file, starttime=tpoint, endtime=tpoint) ids = {tr.id for tr in st} self.assertEqual(len(st), len(ids)) # make sure tpoint is in the time range start = st[0].stats.starttime end = st[0].stats.endtime self.assertLess(start, tpoint) self.assertLess(tpoint, end)
def test_can_write(self): """ Ensure the result of _read_rg16 is a stream object and that it can be written as mseed. """ st = rc._read_rg16(THREE_CHAN_FCNT) self.assertIsInstance(st, obspy.core.stream.Stream) bytstr = io.BytesIO() # test passes if this doesn't raise try: st.write(bytstr, 'mseed') except Exception: self.fail('Failed to write to mseed!')
def test_can_write(self): """ Ensure the result of _read_rg16 is a stream object and that it can be written as mseed. """ st = rc._read_rg16(THREE_CHAN_FCNT) self.assertIsInstance(st, obspy.core.stream.Stream) bytstr = io.BytesIO() # test passes if this doesn't raise try: st.write(bytstr, 'mseed') except Exception: self.fail('Failed to write to mseed!')
def test_contacts_north(self): """ Ensure the contacts north option maps channels and flips Z trace data. """ components = {'Z', 'N', 'E'} for filename, st_default in zip(FCNT_FILES, FCNT_STREAMS): st_mapped = _read_rg16(filename, contacts_north=True) # make sure components have been mapped to principal directions for tr in st_mapped: self.assertIn(tr.stats.channel[-1], components) # make sure z component is reverse of 2 tr_2 = st_default.select(component='2') tr_z = st_mapped.select(component='Z') # apparently the one component test file only has channel 3 so # we need to make sure a z component is found in each if len(tr_2) and len(tr_z): self.assertTrue(np.all(tr_2[0].data == -tr_z[0].data))
def test_reading_rg16_files(self): """ Ensure that the rg16 files are read by the function :func:`~obspy.core.stream.read` with or without specifying the format. """ for fcnt_file in FCNT_FILES: st_1 = read(fcnt_file) st_2 = read(fcnt_file, format="RG16") st_3 = rc._read_rg16(fcnt_file) # when the function read is called a key "_format" is introduced # in the object stats. This key is not created when the function # _read_rg16 is called. In order to check the stream equality, the # key "_format" was removed. for tr_1, tr_2 in zip(st_1, st_2): del tr_1.stats._format del tr_2.stats._format self.assertTrue(st_1 == st_3) self.assertTrue(st_2 == st_3)
def test_reading_rg16_files(self): """ Ensure that the rg16 files are read by the function :func:`~obspy.core.stream.read` with or without specifying the format. """ for fcnt_file in FCNT_FILES: st_1 = read(fcnt_file) st_2 = read(fcnt_file, format="RG16") st_3 = rc._read_rg16(fcnt_file) # when the function read is called a key "_format" is introduced # in the object stats. This key is not created when the function # _read_rg16 is called. In order to check the stream equality, the # key "_format" was removed. for tr_1, tr_2 in zip(st_1, st_2): del tr_1.stats._format del tr_2.stats._format self.assertTrue(st_1 == st_3) self.assertTrue(st_2 == st_3)
from __future__ import (absolute_import, division, print_function, unicode_literals) from future.builtins import * # NOQA import glob import io import unittest from os.path import dirname, join, basename import numpy as np import obspy from obspy.io.rg16.core import _read_rg16, _is_rg16 TEST_FCNT_DIRECTORY = join(dirname(__file__), 'data') FCNT_FILES = glob.glob(join(TEST_FCNT_DIRECTORY, '*')) FCNT_DICT = {basename(x): _read_rg16(x) for x in FCNT_FILES} FCNT_STREAMS = list(FCNT_DICT.values()) assert len(FCNT_FILES), 'No test files found' class TestReadRG16(unittest.TestCase): supported_samps = {250, 500, 1000, 2000} supported_number_of_components = {1, 3} def test_rg16_files_identified(self): """ Ensure the rg16 files are correctly labeled as such. """ for fcnt_file in FCNT_FILES: