Repository: obspy/obspy

Issue:
Failure to read underline "_" character from dataless file
In the attached dataless file ([UP_BACU_HH.dataless.zip](https://github.com/obspy/obspy/files/1303020/UP_BACU_HH.dataless.zip)) the site_name field in blockette 50 holds the string "T3930_b A6689 3930" as can be verified by opening the file using [pdcc](http://ds.iris.edu/ds/nodes/dmc/software/downloads/pdcc/), however running 

    from obspy.io.xseed import Parser
    print Parser("UP_BACU_HH.dataless").blockettes[50][0].site_name

outputs "T3930 b A6689 3930". 

Note that also writing strings with the underline character to a dataless file also fails (sorry, no simple code-snippet available here). 

(running obspy 1.0.3 on open suse installed via pip a few months ago) 




Discussion:
Can confirm the bug also exists in `master`.

Resolving patch:
diff --git a/obspy/io/xseed/fields.py b/obspy/io/xseed/fields.py
--- a/obspy/io/xseed/fields.py
+++ b/obspy/io/xseed/fields.py
@@ -56,7 +56,7 @@ def __str__(self):
         if self.id:
             return "F%02d" % self.id
 
-    def _repr_pretty_(self, p, cycle):
+    def _repr_pretty_(self, p, cycle):  # @UnusedVariable
         p.text(str(self))
 
     def convert(self, value):
@@ -81,25 +81,28 @@ def _format_string(self, s):
             return sn
         rx_list = []
         if 'U' in self.flags:
-            rx_list.append("[A-Z]")
+            # upper case A—Z
+            rx_list.append(r"[A-Z]")
         if 'L' in self.flags:
-            rx_list.append("[a-z]")
+            # lower case a—z
+            rx_list.append(r"[a-z]")
         if 'N' in self.flags:
-            rx_list.append("[0-9]")
+            # digits 0—9
+            rx_list.append(r"[0-9]")
         if 'P' in self.flags:
-            rx_list.append("[^A-Za-z0-9 ]")
+            # any punctuation characters (including “_”)
+            rx_list.append(r"[^A-Za-z0-9 ]")
         if 'S' in self.flags:
-            rx_list.append(" ")
+            # spaces between words
+            rx_list.append(r" ")
         if '_' in self.flags:
-            rx_list.append("_")
+            # underline symbol
+            rx_list.append(r"_")
+        # auto-format
         if 'U' in self.flags and 'L' not in self.flags:
             sn = sn.upper()
         elif 'L' in self.flags and 'U' not in self.flags:
             sn = sn.lower()
-        if 'S' in self.flags and 'X' not in self.flags:
-            sn = sn.replace("_", " ")
-        elif 'X' in self.flags and 'S' not in self.flags:
-            sn = sn.replace(" ", "_")
         rx = "|".join(rx_list)
         sn = "".join(re.findall(rx, sn))
         if re.match("(" + rx + ")*$", sn) is None:


Test patch:
diff --git a/obspy/io/xseed/tests/data/UP_BACU_HH.dataless b/obspy/io/xseed/tests/data/UP_BACU_HH.dataless
new file mode 100644
--- /dev/null
+++ b/obspy/io/xseed/tests/data/UP_BACU_HH.dataless
@@ -0,0 +1 @@
+000001V 010008402.4121950,001,00:00:00.0000~2500,001,00:00:00.0000~2017,257,12:54:01.0000~~~0110021001BACU 000003                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               000002A 0300237Steim2 Integer Compression Format~000105014F1 P4 W4 D C2 R1 P8 W4 D C2~P0 W4 N15 S2,0,1~T0 X W4~T1 Y4 W1 D C2~T2 W4 I D2~K0 X D30~K1 N0 D30 C2~K2 Y2 D15 C2~K3 Y3 D10 C2~T3 W4 I D2~K0 Y5 D6 C2~K1 Y6 D5 C2~K2 X D2 Y7 D4 C2~K3 X D30~0330015001SNSN~0340044001M/S~VELOCITY in Meters Per Second~0340025002V~EMF in Volts~0340040003COUNTS~DIGITAL UNIT in Counts~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               000003S 0500102BACU +59.854000+017.107800+0010.00001000T3930_b A6689 3930~0013210102017,220,10:01:00.0000~~NUP0520128  HHE0000000X~001002+59.854000+017.107800+0010.0000.0090.0+00.00001121.0000E+020.0000E+000000CG~2017,220,10:01:00.0000~~N0530382A01001002+5.71573E+08+1.00000E+00002+0.00000E+00+0.00000E+00+0.00000E+00+0.00000E+00+0.00000E+00+0.00000E+00+0.00000E+00+0.00000E+00005-1.48597E-01-1.48597E-01+0.00000E+00+0.00000E+00-1.48597E-01+1.48597E-01+0.00000E+00+0.00000E+00-1.13097E+03+0.00000E+00+0.00000E+00+0.00000E+00-1.00531E+03+0.00000E+00+0.00000E+00+0.00000E+00-5.02655E+02+0.00000E+00+0.00000E+00+0.00000E+00058003501+2.00120E+04+1.00000E+00000540024D02002003000000000570051022.5600E+050000100000+0.0000E+00+0.0000E+00058003502+3.11429E+05+1.00000E+0000058003500+6.23232E+09+1.00000E+0000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                000004                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          000005                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          000006                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          000007                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          000008                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
\ No newline at end of file
diff --git a/obspy/io/xseed/tests/test_fields.py b/obspy/io/xseed/tests/test_fields.py
--- a/obspy/io/xseed/tests/test_fields.py
+++ b/obspy/io/xseed/tests/test_fields.py
@@ -4,10 +4,11 @@
 from future.builtins import *  # NOQA @UnusedWildImport
 
 import io
+import string
 import unittest
 
 from obspy import UTCDateTime
-from obspy.io.xseed.fields import Float, VariableString
+from obspy.io.xseed.fields import Float, VariableString, FixedString
 
 
 class FieldsTestCase(unittest.TestCase):
@@ -148,6 +149,39 @@ def test_read_compact_date_time(self):
         self.assertEqual(dt, UTCDateTime(2008, 12, 23, 1, 30, 22, 500000))
         self.assertEqual(field.write(dt), b'2008,358,01:30:22.5000~')
 
+    def test_wrong_spaces(self):
+        """
+        VariableString(..., 'S') should remove leading or trailing spaces.
+        """
+        field = VariableString(1, "test", 1, 60, 'UNLPS', strict=True)
+        orig = b' T3930_b A6689 3930 ~'
+        result = field.read(io.BytesIO(orig))
+        self.assertEqual(result, 'T3930_b A6689 3930')
+
+    def test_fixed_string_any_punctuation(self):
+        """
+        FixedString(..., 'P') should allow any punctuation character.
+        """
+        field = FixedString(1, "test", 9, 'UP')
+        for character in string.punctuation:
+            orig = b'ABC' + character.encode("utf-8") + b'DEF'
+            result = field.read(io.BytesIO(orig))
+            self.assertEqual(result, 'ABC' + character + 'DEF')
+
+    def test_variable_string_any_punctuation(self):
+        """
+        VariableString(..., 'P') should allow any punctuation character.
+        """
+        field = VariableString(1, "test", 1, 30, 'UP')
+        for character in string.punctuation:
+            # need to skip tilde as it is also the end marker of a variable
+            # field
+            if character == '~':
+                continue
+            orig = b'ABC' + character.encode("utf-8") + b'DEF~'
+            result = field.read(io.BytesIO(orig))
+            self.assertEqual(result, 'ABC' + character + 'DEF')
+
 
 def suite():
     return unittest.makeSuite(FieldsTestCase, 'test')
diff --git a/obspy/io/xseed/tests/test_parser.py b/obspy/io/xseed/tests/test_parser.py
--- a/obspy/io/xseed/tests/test_parser.py
+++ b/obspy/io/xseed/tests/test_parser.py
@@ -807,6 +807,17 @@ def test_rotation_to_zne(self):
         # result.
         self.assertTrue(np.allclose(tr_2, tr_r_e, atol=tr_r_e.max() / 4.0))
 
+    def test_underline_in_site_name(self):
+        """
+        Test case for issue #1893.
+        """
+        filename = os.path.join(self.path, 'UP_BACU_HH.dataless')
+        parser = Parser()
+        parser.read(filename)
+        # value given by pdcc
+        self.assertEqual(parser.blockettes[50][0].site_name,
+                         'T3930_b A6689 3930')
+
 
 def suite():
     return unittest.makeSuite(ParserTestCase, 'test')