Skip to content

Commit

Permalink
Merge 5d5dc2e into 8f36463
Browse files Browse the repository at this point in the history
  • Loading branch information
dsentinel committed Sep 6, 2016
2 parents 8f36463 + 5d5dc2e commit 6785453
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Expand Up @@ -6,6 +6,9 @@
* `SACTrace.lpspol` and `lcalda` are `True` and `False` by default, when
created via `SACTrace.from_obspy_trace` with a `Trace` that has no SAC
inheritance. (see #1507)
- obspy.io.stationxml:
* Datetime fields are written with microseconds to StationXML if
microseconds are present. (see #1511)

1.0.2: (doi: 10.5281/zenodo.49636)
- obspy.core:
Expand Down
5 changes: 4 additions & 1 deletion obspy/io/stationxml/core.py
Expand Up @@ -1328,7 +1328,10 @@ def _obj2tag(parent, tag_name, tag_value):


def _format_time(value):
return value.strftime("%Y-%m-%dT%H:%M:%S+00:00")
if value.microsecond == 0:
return value.strftime("%Y-%m-%dT%H:%M:%S+00:00")
else:
return value.strftime("%Y-%m-%dT%H:%M:%S.%f+00:00")


# Remove once 0.11 has been released.
Expand Down
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<FDSNStationXML schemaVersion="1.0" xmlns="http://www.fdsn.org/xml/station/1">
<Source>OBS</Source>
<Module/>
<ModuleURI/>
<Created>2013-01-01T00:00:00.123456+00:00</Created>
<Network code="PY"/>
</FDSNStationXML>
36 changes: 26 additions & 10 deletions obspy/io/stationxml/tests/test_stationxml.py
Expand Up @@ -22,6 +22,7 @@

import obspy
from obspy.core.inventory import Inventory, Network
import obspy.io.stationxml.core


class StationXMLTestCase(unittest.TestCase):
Expand Down Expand Up @@ -114,6 +115,31 @@ def test_read_and_write_minimal_file(self):
self._assert_station_xml_equality(file_buffer,
expected_xml_file_buffer)

def test_subsecond_read_and_write_minimal_file(self):
"""
Test reading and writing of sub-second time in datetime field,
using creation time
"""
filename = os.path.join(self.data_dir,
"minimal_station_with_microseconds.xml")
inv = obspy.read_inventory(filename)

# Write it again. Also validate it to get more confidence. Suppress the
# writing of the ObsPy related tags to ease testing.
file_buffer = io.BytesIO()

inv.write(file_buffer, format="StationXML",
_suppress_module_tags=True)
file_buffer.seek(0, 0)

with open(filename, "rb") as open_file:
expected_xml_file_buffer = io.BytesIO(open_file.read())
expected_xml_file_buffer.seek(0, 0)

self._assert_station_xml_equality(file_buffer,
expected_xml_file_buffer)

def test_read_and_write_full_file(self):
"""
Test that reading and writing of a full StationXML document with all
Expand All @@ -126,20 +152,10 @@ def test_read_and_write_full_file(self):
# writing of the ObsPy related tags to ease testing.
file_buffer = io.BytesIO()

# XXX helper variable to debug writing the full random file, set True
# XXX for debug output
write_debug_output = False

inv.write(file_buffer, format="StationXML",
validate=(not write_debug_output),
_suppress_module_tags=True)
file_buffer.seek(0, 0)

if write_debug_output:
with open("/tmp/debugout.xml", "wb") as open_file:
open_file.write(file_buffer.read())
file_buffer.seek(0, 0)

with open(filename, "rb") as open_file:
expected_xml_file_buffer = io.BytesIO(open_file.read())
expected_xml_file_buffer.seek(0, 0)
Expand Down

0 comments on commit 6785453

Please sign in to comment.