def test_writeAndReadDifferentEncodings(self): """ Writes and read a file with different encoding via the obspy.core methods. """ npts = 1000 np.random.seed(815) # make test reproducable data = np.random.randn(npts).astype('float64') * 1e3 + .5 st = Stream([Trace(data=data)]) # Loop over some record lengths. for encoding, value in ENCODINGS.iteritems(): seed_dtype = value[2] # Special handling for the ASCII dtype. NumPy 1.7 changes the # default dtype of numpy.string_ from "|S1" to "|S32". Enforce # "|S1|" here to be consistent across NumPy versions. if encoding == 0: seed_dtype = "|S1" with NamedTemporaryFile() as tf: tempfile = tf.name # Write it once with the encoding key and once with the value. st[0].data = data.astype(seed_dtype) st.verify() st.write(tempfile, format="MSEED", encoding=encoding) st2 = read(tempfile) del st2[0].stats.mseed np.testing.assert_array_equal(st[0].data, st2[0].data, "Arrays are not equal for encoding '%s'" % ENCODINGS[encoding][0]) del st2 ms = _MSStruct(tempfile) ms.read(-1, 1, 1, 0) self.assertEqual(ms.msr.contents.encoding, encoding) del ms # for valgrind
def test_writeAndReadDifferentEncodings(self): """ Writes and read a file with different encoding via the obspy.core methods. """ npts = 1000 np.random.seed(815) # make test reproducable data = np.random.randn(npts).astype('float64') * 1e3 + .5 st = Stream([Trace(data=data)]) # Loop over some record lengths. for encoding, value in ENCODINGS.iteritems(): seed_dtype = value[2] tempfile = NamedTemporaryFile().name # Write it once with the encoding key and once with the value. st[0].data = data.astype(seed_dtype) st.verify() st.write(tempfile, format="MSEED", encoding=encoding) st2 = read(tempfile) del st2[0].stats.mseed np.testing.assert_array_equal(st[0].data, st2[0].data) del st2 ms = _MSStruct(tempfile) ms.read(-1, 1, 1, 0) self.assertEqual(ms.msr.contents.encoding, encoding) del ms # for valgrind os.remove(tempfile)
def test_readSingleRecordToMSR(self): """ Tests readSingleRecordtoMSR against start and endtimes. Reference start and endtimes are obtained from the tracegroup. Both cases, with and without ms_p argument are tested. """ filename = os.path.join(self.path, 'data', 'BW.BGLD.__.EHE.D.2008.001.first_10_records') start, end = [1199145599915000L, 1199145620510000L] # start and endtime ms = _MSStruct(filename, init_msrmsf=False) ms.read(-1, 0, 1, 0) self.assertEqual(start, clibmseed.msr_starttime(ms.msr)) ms.offset = ms.filePosFromRecNum(-1) ms.read(-1, 0, 1, 0) self.assertEqual(end, clibmseed.msr_endtime(ms.msr)) del ms # for valgrind
def test_issue312(self): """ Tests issue #312 The blkt_link struct was defined wrong. """ filename = os.path.join(self.path, "data", "BW.BGLD.__.EHE.D.2008.001.first_10_records") # start and endtime ms = _MSStruct(filename) ms.read(-1, 0, 1, 0) blkt_link = ms.msr.contents.blkts.contents # The first blockette usually begins after 48 bytes. In the test file # it does. self.assertEqual(blkt_link.blktoffset, 48) # The first blockette is blockette 1000 in this file. self.assertEqual(blkt_link.blkt_type, 1000) # Only one blockette. self.assertEqual(blkt_link.next_blkt, 0) # Blockette data is 8 bytes - 4 bytes for the blockette header. self.assertEqual(blkt_link.blktdatalen, 4) del ms
def test_issue312(self): """ Tests issue #312 The blkt_link struct was defined wrong. """ filename = os.path.join(self.path, 'data', 'BW.BGLD.__.EHE.D.2008.001.first_10_records') # start and end time ms = _MSStruct(filename) ms.read(-1, 0, 1, 0) blkt_link = ms.msr.contents.blkts.contents # The first blockette usually begins after 48 bytes. In the test file # it does. self.assertEqual(blkt_link.blktoffset, 48) # The first blockette is blockette 1000 in this file. self.assertEqual(blkt_link.blkt_type, 1000) # Only one blockette. self.assertEqual(blkt_link.next_blkt, 0) # Blockette data is 8 bytes - 4 bytes for the blockette header. self.assertEqual(blkt_link.blktdatalen, 4) del ms
def test_filesFromLibmseed(self): """ Tests reading of files that are created by libmseed. This test also checks the files created by libmseed to some extend. """ path = os.path.join(self.path, "data", "encoding") # Dictionary. The key is the filename, the value a tuple: dtype, # sampletype, encoding, content def_content = np.arange(1, 51, dtype='int32') files = { os.path.join(path, "smallASCII.mseed"): ('|S1', 'a', 0, np.fromstring('ABCDEFGH', dtype='|S1')), # Tests all ASCII letters. os.path.join(path, "fullASCII.mseed"): ('|S1', 'a', 0, np.fromstring(""" !"#$%&'()*+,-./""" + """0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`""" + """abcdefghijklmnopqrstuvwxyz{|}~""", dtype='|S1')), # Note: int16 array will also be returned as int32. os.path.join(path, "int16_INT16.mseed"): ('int32', 'i', 1, def_content.astype('int16')), os.path.join(path, "int32_INT32.mseed"): ('int32', 'i', 3, def_content), os.path.join(path, "int32_Steim1.mseed"): ('int32', 'i', 10, def_content), os.path.join(path, "int32_Steim2.mseed"): ('int32', 'i', 11, def_content), os.path.join(path, "float32_Float32.mseed"): ('float32', 'f', 4, def_content.astype('float32')), os.path.join(path, "float64_Float64.mseed"): ('float64', 'd', 5, def_content.astype('float64')) } # Loop over all files and read them. for file in files.keys(): # Check little and big Endian for each file. for _i in ('littleEndian', 'bigEndian'): cur_file = file[:-6] + '_' + _i + '.mseed' st = read(os.path.join(cur_file)) # Check the array. np.testing.assert_array_equal(st[0].data, files[file][3]) # Check the dtype. self.assertEqual(st[0].data.dtype, files[file][0]) # Check byteorder. Should always be native byteorder. Byteorder # does not apply to ASCII arrays. if 'ASCII' in cur_file: self.assertEqual(st[0].data.dtype.byteorder, '|') else: self.assertEqual(st[0].data.dtype.byteorder, '=') del st # Read just the first record to check encoding. The sampletype # should follow from the encoding. But libmseed seems not to # read the sampletype when reading a file. ms = _MSStruct(cur_file, init_msrmsf=False) ms.read(-1, 0, 1, 0) # Check values. self.assertEqual(getattr(ms.msr.contents, 'encoding'), files[file][2]) if _i == 'littleEndian': self.assertEqual(getattr(ms.msr.contents, 'byteorder'), 0) else: self.assertEqual(getattr(ms.msr.contents, 'byteorder'), 1) # Deallocate for debugging with valrgind del ms