aboutsummaryrefslogtreecommitdiff
path: root/qolab/data/trace.py
diff options
context:
space:
mode:
Diffstat (limited to 'qolab/data/trace.py')
-rw-r--r--qolab/data/trace.py37
1 files changed, 16 insertions, 21 deletions
diff --git a/qolab/data/trace.py b/qolab/data/trace.py
index bda4da5..843d48e 100644
--- a/qolab/data/trace.py
+++ b/qolab/data/trace.py
@@ -1,4 +1,4 @@
-from qolab.file_utils import save_table_with_header
+from qolab.file_utils import save_table_with_header, infer_compression
import datetime
import numpy as np
import yaml
@@ -25,28 +25,23 @@ def loadTraceRawHeaderAndData(fname, tryCompressedIfMissing=True):
headerstr=[]
data = None
if (not os.path.exists(fname)) and tryCompressedIfMissing:
- if os.path.exists(fname+'.gz'):
- fname += '.gz'
- if os.path.exists(fname+'.bz'):
- fname += '.bz'
- if os.path.exists(fname+'.bz2'):
- fname += '.bz2'
+ # attempt to locate compressed file for the missing base file
+ for ext in ['gz', 'bz', 'bz2']:
+ if os.path.exists(fname+'.'+ext):
+ fname += '.'+ext
+ break
# we will try to guess if the file compressed
_open = open
- for ext in ['gz', 'bz', 'bz2']:
- b, fext = os.path.splitext(fname)
- if fext != '.'+ext:
- continue
- if fext == '.gz':
- # TODO improve detection: gzip files have first 2 bytes set to b'\x1f\x8b'
- import gzip
- _open = gzip.open
- break
- if ( fext == '.bz') or (fext == '.bz2'):
- # TODO improve detection: bzip files have first 2 bytes set to b'BZ'
- import bz2
- _open = bz2.open
- break
+ compression = infer_compression(fname)
+ print(compression)
+ if compression == 'gzip':
+ # TODO improve detection: gzip files have first 2 bytes set to b'\x1f\x8b'
+ import gzip
+ _open = gzip.open
+ elif compression == 'bzip':
+ # TODO improve detection: bzip files have first 2 bytes set to b'BZ'
+ import bz2
+ _open = bz2.open
with _open(fname, mode='rb') as tracefile:
# Reading yaml header prefixed by '% '
# It sits at the top and below is just data in TSV format