Skip to content

Commit 3acb2a0

Browse files
committed
Convert jupyter notebooks to .py scripts
also update step 3 to use SPUD
1 parent e61fccf commit 3acb2a0

4 files changed

+216
-437
lines changed

2-rotate_H1H2_NE_RT.ipynb

-228
This file was deleted.

2-rotate_H1H2_NE_RT.py

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# %% markdown
2+
# # Rotate To R-T
3+
# Reads in Z-H1-H2 and first rotates to Z-N-E, then to Z-R-T and write out sac file. A left hand convention is assumed where H2 is 90$^{\circ}$ clockwise from H1, and Z points up. Input instrument azimuths are H1 clockwise of north.
4+
#
5+
# ### Station orientation file is of the form:
6+
# STA1 H1-AZI1
7+
#
8+
# STA2 H1-AZI2
9+
#
10+
# STA3 H1-AZI3
11+
#
12+
# ...
13+
#
14+
# If station is missing from list of orientations, then it is simply skipped.
15+
#
16+
# ##### JBR - 2/3/18
17+
# %% codecell
18+
%load_ext autoreload
19+
%autoreload
20+
from setup_parameters import *
21+
import numpy as np
22+
from obspy import read
23+
from obspy.signal.rotate import rotate2zne, rotate_ne_rt
24+
import pandas as pd
25+
import matplotlib.pylab as plt
26+
from obspy.io.sac import SACTrace
27+
import os
28+
%matplotlib inline
29+
# %% codecell
30+
data_dir = search_dir
31+
32+
# Read events
33+
evs = pd.read_csv(data_dir+'/evlist.txt',' ',header=None)
34+
evs.columns = ["event"]
35+
36+
# Orientations
37+
ori = pd.read_csv(ori_path,' ',header=None)
38+
ori.columns = ["sta", "H1azi"]
39+
stas = ori['sta']
40+
ori = ori.set_index('sta')
41+
# %% codecell
42+
for iev, event in enumerate(evs['event']):
43+
event = str(event)
44+
evdir = data_dir + event + '/'
45+
print('Working on : ',event)
46+
if iscleandir:
47+
os.system('rm '+evdir + event+'.'+'*'+'.'+'*'+'.'+Tcomp+'.sac')
48+
os.system('rm '+evdir + event+'.'+'*'+'.'+'*'+'.'+Rcomp+'.sac')
49+
os.system('rm '+evdir + event+'.'+'*'+'.'+'*'+'.'+Ncomp+'.sac')
50+
os.system('rm '+evdir + event+'.'+'*'+'.'+'*'+'.'+Ecomp+'.sac')
51+
52+
for ista, sta in enumerate(stas):
53+
try:
54+
st = read(evdir + event+'.*.'+sta+'.*.sac', debug_headers=True)
55+
except Exception:
56+
print('Missing data for station: ',sta)
57+
continue
58+
H1azi = ori.loc[sta]['H1azi']
59+
for itr in range(0,len(st)):
60+
if st[itr].stats.channel == H1comp:
61+
h1 = st[itr].data
62+
elif st[itr].stats.channel == H2comp:
63+
h2 = st[itr].data
64+
elif st[itr].stats.channel == Zcomp:
65+
z = st[itr].data
66+
ba = st[0].stats.sac.baz
67+
68+
# Rotate Z-H1-H2 to Z-N-E
69+
traces_zne = rotate2zne(data_1=z , azimuth_1=0, dip_1=-90,
70+
data_2=h1, azimuth_2=H1azi, dip_2=0,
71+
data_3=h2, azimuth_3=H1azi+90, dip_3=0)
72+
z2 = traces_zne[0]
73+
n = traces_zne[1]
74+
e = traces_zne[2]
75+
76+
# Rotate N-E to R-T
77+
traces_rt = rotate_ne_rt(n=n, e=e, ba=ba)
78+
r = traces_rt[0]
79+
t = traces_rt[1]
80+
81+
# Define new data streams
82+
st_bhn = st[0].copy()
83+
st_bhn.stats.channel = Ncomp
84+
st_bhn.data = n
85+
st_bhe = st[0].copy()
86+
st_bhe.stats.channel = Ecomp
87+
st_bhe.data = e
88+
st_bht = st[0].copy()
89+
st_bht.stats.channel = Tcomp
90+
st_bht.data = t
91+
st_bhr = st[0].copy()
92+
st_bhr.stats.channel = Rcomp
93+
st_bhr.data = r
94+
95+
# Remove existing file
96+
if os.path.exists(evdir + event+'.'+network+'.'+sta+'.'+Ncomp+'.sac'):
97+
os.remove(evdir + event+'.'+network+'.'+sta+'.'+Ncomp+'.sac')
98+
if os.path.exists(evdir + event+'.'+network+'.'+sta+'.'+Ecomp+'.sac'):
99+
os.remove(evdir + event+'.'+network+'.'+sta+'.'+Ecomp+'.sac')
100+
if os.path.exists(evdir + event+'.'+network+'.'+sta+'.'+Tcomp+'.sac'):
101+
os.remove(evdir + event+'.'+network+'.'+sta+'.'+Tcomp+'.sac')
102+
if os.path.exists(evdir + event+'.'+network+'.'+sta+'.'+Rcomp+'.sac'):
103+
os.remove(evdir + event+'.'+network+'.'+sta+'.'+Rcomp+'.sac')
104+
105+
network = st_bhr.stats.network
106+
# Save BHN, BHE, BHR, and BHT
107+
sac_n = SACTrace.from_obspy_trace(st_bhn)
108+
sac_n.write(evdir + event+'.'+network+'.'+sta+'.'+Ncomp+'.sac')
109+
sac_e = SACTrace.from_obspy_trace(st_bhe)
110+
sac_e.write(evdir + event+'.'+network+'.'+sta+'.'+Ecomp+'.sac')
111+
sac_t = SACTrace.from_obspy_trace(st_bht)
112+
sac_t.write(evdir + event+'.'+network+'.'+sta+'.'+Tcomp+'.sac')
113+
sac_r = SACTrace.from_obspy_trace(st_bhr)
114+
sac_r.write(evdir + event+'.'+network+'.'+sta+'.'+Rcomp+'.sac')
115+
116+
# fmin = 1/100
117+
# fmax = 1/20
118+
# st_bht.filter("bandpass", freqmin=fmin, freqmax=fmax, corners=2, zerophase=True)
119+
# st_bhr.filter("bandpass", freqmin=fmin, freqmax=fmax, corners=2, zerophase=True)
120+
# plt.figure(figsize=(10,5))
121+
# plt.plot(np.arange(0,len(r)), st_bht.data, color="red")
122+
# plt.plot(np.arange(0,len(t)), st_bhr.data, color="black")
123+
# # plt.plot(np.arange(0,len(stLHZ[0].data)), stLHZ[0].data, color="blue")
124+
# plt.xlim(0, 5000)
125+
# # plt.pause(3)
126+
# plt.draw()
127+
# %% codecell
128+
129+
# %% codecell

0 commit comments

Comments
 (0)