Created
May 29, 2015 14:49
-
-
Save jrleeman/3c3649fb1d671c6fdbbb to your computer and use it in GitHub Desktop.
Convert Lab Format Data to SAC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import os | |
import sys | |
from obspy.core import * | |
from datetime import datetime | |
class recordSet(object): | |
def __init__(self, path): | |
self.traces = [] | |
self.rec_files = [] | |
self.path = path | |
def readRecord(self, fname): | |
try: | |
f = open(fname,'rb') | |
amp = np.fromfile(f,dtype='<i4',count=-1,sep='') | |
amp = amp[2:] | |
amp.dtype = '<d' | |
except: | |
print "Binary read failed!" | |
try: | |
amp = np.loadtxt(fname) | |
except: | |
print "ASCII Failed!" | |
return amp | |
def readLogfile(self, fname): | |
logfile = open('%s/%s'%(path,fname),'r') | |
lines = [] | |
for line in logfile: | |
lines.append(line) | |
# Grab footer information | |
self.date,self.pulse_rate,self.rec_rate,self.n_pre_trigger,self.n_post_trigger,self.n_stack = lines[-1].split() | |
lines.pop(-1) | |
rec_files = [] | |
for line in lines: | |
fname, ftime, fampm = line.split() | |
fname = fname.split('\\')[-1] | |
rec_time = datetime.strptime('%s %s %s'%(self.date,ftime,fampm),'%m/%d/%Y %I:%M:%S %p') | |
self.rec_files.append([fname,rec_time]) | |
def readTraces(self): | |
for rec in self.rec_files: | |
fname, ftime = rec | |
data = self.readRecord(path + '/' + fname) | |
data = np.array(data,dtype=np.float32) | |
tr = Trace(data) | |
tr.stats.delta = 1./int(self.rec_rate) | |
tr.stats.starttime = ftime | |
self.traces.append(tr) | |
def writeSac(self): | |
for trace,fname in zip(self.traces, self.rec_files): | |
fname = fname[0].split('.')[0] + '.sac' | |
print fname | |
print trace | |
trace.write(self.path + '/' + fname,format='SAC') | |
path = sys.argv[1] | |
exp = recordSet(path) | |
# In directory, find the logfile | |
files = os.listdir(path) | |
for file in files: | |
if 'log' in file: | |
exp.readLogfile(file) | |
break | |
else: | |
pass | |
exp.readTraces() | |
exp.writeSac() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment