Skip to content

Commit

Permalink
Merge pull request #1773 from lijunzh/sac_string_encoding_flag
Browse files Browse the repository at this point in the history
Variable string encoding for SAC files
  • Loading branch information
megies committed May 12, 2017
2 parents 85167d0 + 865fd6b commit ab82a58
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ master: (doi: 10.5281/zenodo.165135)
inheritance. (see #1507)
* Reference time not written to SAC file when made from scratch
(see #1575)
* Reinforce ASCII encoding in reading non-ASCII SAC files regardless of
default encoding setting. (see #1768)
- obspy.io.sh:
* Fix writing of long headers for Python 3 (see #1526)
* Whitespace in header fields is not ignored anymore (see #1552)
Expand Down
1 change: 1 addition & 0 deletions obspy/CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ Wassermann, Joachim
Williams, Mark C.
Winkelman, Andrew
Zad, Seyed Kasra Hosseini
Zhu, Lijun
2 changes: 1 addition & 1 deletion obspy/io/sac/sactrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ def read(cls, source, headonly=False, ascii=False, byteorder=None,
val = _ut._clean_str(val, strip_whitespace=False)
if val.startswith(native_str('-12345')):
val = HD.SNULL
hs[i] = val
hs[i] = val.encode('ASCII', 'replace')

sac = cls._from_arrays(hf, hi, hs, data)
if sac.dist is None:
Expand Down
Binary file added obspy/io/sac/tests/data/test_encode.sac
Binary file not shown.
8 changes: 8 additions & 0 deletions obspy/io/sac/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def setUp(self):
self.fileseis = os.path.join(self.path, "data", "seism.sac")
self.file_notascii = os.path.join(self.path, "data",
"non_ascii.sac")
self.file_encode = os.path.join(self.path, "data", "test_encode.sac")
self.testdata = np.array(
[-8.74227766e-08, -3.09016973e-01,
-5.87785363e-01, -8.09017122e-01, -9.51056600e-01,
Expand Down Expand Up @@ -915,6 +916,13 @@ def test_always_sac_reftime(self):
self.assertAlmostEqual(tr1.stats.sac.a, a, places=5)
self.assertEqual(tr1.stats.sac.b, b)

def test_wrong_encoding(self):
"""
Read SAC file with wrong encoding
"""
tr0 = read(self.file_encode)[0]
self.assertEqual(tr0.stats.get('channel'), '????????')


def suite():
return unittest.makeSuite(CoreTestCase, 'test')
Expand Down

0 comments on commit ab82a58

Please sign in to comment.