Skip to content

Instantly share code, notes, and snippets.

@megies
Created January 24, 2014 14:04
Show Gist options
  • Save megies/8597871 to your computer and use it in GitHub Desktop.
Save megies/8597871 to your computer and use it in GitHub Desktop.
datamark changes
diff --git a/obspy/datamark/__init__.py b/obspy/datamark/__init__.py
index c3a0e81..b53994d 100644
--- a/obspy/datamark/__init__.py
+++ b/obspy/datamark/__init__.py
@@ -12,6 +12,7 @@ This module provides read support for DataMark waveform data.
(http://www.gnu.org/copyleft/lesser.html)
"""
+from __future__ import unicode_literals
if __name__ == '__main__':
diff --git a/obspy/datamark/core.py b/obspy/datamark/core.py
index 7c26267..8cfd7fb 100644
--- a/obspy/datamark/core.py
+++ b/obspy/datamark/core.py
@@ -2,6 +2,11 @@
"""
DATAMARK bindings to ObsPy core module.
"""
+from __future__ import division
+from __future__ import unicode_literals
+from future.builtins import str
+from future.builtins import range
+from future.builtins import int
from obspy import Trace, UTCDateTime, Stream
import numpy as np
@@ -19,27 +24,28 @@ def isDATAMARK(filename): # @UnusedVariable
"""
# as long we don't have full format description we just try to read the
# file like readDATAMARK and check for errors
+ century = "20" # hardcoded ;(
try:
- fpin = open(filename, "rb")
- fpin.read(4)
- buff = fpin.read(6)
- yy = "%s%02x" % (20, np.fromstring(buff[0], dtype='b')[0])
- mm = "%x" % np.fromstring(buff[1], dtype='b')[0]
- dd = "%x" % np.fromstring(buff[2], dtype='b')[0]
- hh = "%x" % np.fromstring(buff[3], dtype='b')[0]
- mi = "%x" % np.fromstring(buff[4], dtype='b')[0]
- sec = "%x" % np.fromstring(buff[5], dtype='b')[0]
+ with open(filename, "rb") as fpin:
+ fpin.read(4)
+ buff = fpin.read(6)
+ yy = century + "%02x" % np.fromstring(buff[0:1], dtype='b')[0]
+ mm = "%x" % np.fromstring(buff[1:2], dtype='b')[0]
+ dd = "%x" % np.fromstring(buff[2:3], dtype='b')[0]
+ hh = "%x" % np.fromstring(buff[3:4], dtype='b')[0]
+ mi = "%x" % np.fromstring(buff[4:5], dtype='b')[0]
+ sec = "%x" % np.fromstring(buff[5:6], dtype='b')[0]
- # This will raise for invalid dates.
- UTCDateTime(int(yy), int(mm), int(dd), int(hh), int(mi),
- int(sec))
- buff = fpin.read(4)
- np.fromstring(buff[0], dtype='b')[0]
- np.fromstring(buff[1], dtype='b')[0]
- np.fromstring(buff[2], dtype='b')[0] >> 4
- np.fromstring(buff[3], dtype='b')[0]
- idata00 = fpin.read(4)
- np.fromstring(idata00, '>i')[0]
+ # This will raise for invalid dates.
+ UTCDateTime(int(yy), int(mm), int(dd), int(hh), int(mi),
+ int(sec))
+ buff = fpin.read(4)
+ np.fromstring(buff[0:1], dtype='b')[0]
+ np.fromstring(buff[1:2], dtype='b')[0]
+ np.fromstring(buff[2:3], dtype='b')[0] >> 4
+ np.fromstring(buff[3:4], dtype='b')[0]
+ idata00 = fpin.read(4)
+ np.fromstring(idata00, '>i')[0]
except:
return False
return True
@@ -79,12 +85,12 @@ def readDATAMARK(filename, century="20", **kwargs): # @UnusedVariable
buff = fpin.read(6)
leng += 6
- yy = "%s%02x" % (century, np.fromstring(buff[0], dtype='b')[0])
- mm = "%x" % np.fromstring(buff[1], dtype='b')[0]
- dd = "%x" % np.fromstring(buff[2], dtype='b')[0]
- hh = "%x" % np.fromstring(buff[3], dtype='b')[0]
- mi = "%x" % np.fromstring(buff[4], dtype='b')[0]
- sec = "%x" % np.fromstring(buff[5], dtype='b')[0]
+ yy = century + "%02x" % np.fromstring(buff[0:1], dtype='b')[0]
+ mm = "%x" % np.fromstring(buff[1:2], dtype='b')[0]
+ dd = "%x" % np.fromstring(buff[2:3], dtype='b')[0]
+ hh = "%x" % np.fromstring(buff[3:4], dtype='b')[0]
+ mi = "%x" % np.fromstring(buff[4:5], dtype='b')[0]
+ sec = "%x" % np.fromstring(buff[5:6], dtype='b')[0]
date = UTCDateTime(int(yy), int(mm), int(dd), int(hh), int(mi),
int(sec))
@@ -96,9 +102,9 @@ def readDATAMARK(filename, century="20", **kwargs): # @UnusedVariable
buff = fpin.read(4)
leng += 4
#_flag = np.fromstring(buff[0], dtype='b')[0]
- chanum = np.fromstring(buff[1], dtype='b')[0]
- datawide = np.fromstring(buff[2], dtype='b')[0] >> 4
- srate = np.fromstring(buff[3], dtype='b')[0]
+ chanum = np.fromstring(buff[1:2], dtype='b')[0]
+ datawide = np.fromstring(buff[2:3], dtype='b')[0] >> 4
+ srate = np.fromstring(buff[3:4], dtype='b')[0]
xlen = (srate - 1) * datawide
idata00 = fpin.read(4)
leng += 4
@@ -116,7 +122,7 @@ def readDATAMARK(filename, century="20", **kwargs): # @UnusedVariable
sdata += fpin.read(xlen - len(sdata))
msg = "This shouldn't happen, it's weird..."
warnings.warn(msg)
- for i in range((xlen / datawide)):
+ for i in range((xlen // datawide)):
idata2 = 0
if datawide == 1:
idata2 = np.fromstring(sdata[i:i + 1], 'b')[0]
@@ -137,7 +143,7 @@ def readDATAMARK(filename, century="20", **kwargs): # @UnusedVariable
output[chanum].append(idata22)
traces = []
- for i in output.keys():
+ for i in list(output.keys()):
t = Trace(data=np.array(output[i]))
t.stats.channel = str(i)
t.stats.sampling_rate = float(srate)
diff --git a/obspy/datamark/tests/__init__.py b/obspy/datamark/tests/__init__.py
index e22ffbd..b1ba82f 100644
--- a/obspy/datamark/tests/__init__.py
+++ b/obspy/datamark/tests/__init__.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
import unittest
from obspy.core.util import add_doctests, add_unittests
diff --git a/obspy/datamark/tests/test_core.py b/obspy/datamark/tests/test_core.py
index 80c2efc..597bb58 100644
--- a/obspy/datamark/tests/test_core.py
+++ b/obspy/datamark/tests/test_core.py
@@ -3,6 +3,7 @@
"""
The obspy.datamark.core test suite.
"""
+from __future__ import unicode_literals
from obspy import read
from obspy.core.utcdatetime import UTCDateTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment