-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenspy_extdrums.py
227 lines (203 loc) · 8.25 KB
/
benspy_extdrums.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/home/seismo/venvs/py3seismo/bin/python
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 15:39:13 2020
Script for copying files from seiscomp archive to SDSarchive and create drums
@author: seismo
"""
import argparse
import json
import os
import subprocess
import sys
import tqdm
from obspy import UTCDateTime
#from obspy.clients.fdsn import Client
from obspy.clients.filesystem.sds import Client
def receive():
#######################
desc = "Create drum files for external stations from external server for visualization in Bensberg Drum Viewer"
parser = argparse.ArgumentParser(description=desc,
formatter_class=argparse.RawTextHelpFormatter)
def_day = 'yesterday'
def_comp = 'Z'
def_stas = 'AHRW,TNS,GSH,LAGB,MEM,BUG'
def_dict = '/mnt/Station/station/Inventory/json/drumplot_ext.json'
def_outd = '/mnt/Station/data'
parser.add_argument('-d', '--day', type=str,
default=def_day,
help=f"Day to create the drum files in format YYYY-MM-DD also possible 'today' or 'yesterday'\n\
[Default: {def_day}].")
parser.add_argument('-c', '--comp', type=str,
default=def_comp,
help=f"Channel to use to create drum files\n\
[Default: {def_comp}].")
parser.add_argument('-s', '--stations', type=str,
default=def_stas,
help=f"Create drum files for these stations only, comma-separated\n\
[Default: {def_stas}].")
parser.add_argument('-ed', '--extradict', type=str,
default=def_dict,
help=f"Use different dictionary for station specifications\n\
[Default: {def_dict}].")
parser.add_argument('-o', '--outputdir', type=str,
default=def_outd,
help=f"Output directory where store the drum files\n\
[Default: {def_outd}].")
arg = parser.parse_args()
return arg
def data_drum(stream, details, ddate):
'''
Compute and create drum list from data stream during one specific day.
Parameters
----------
stream : Obspy stream object.
Data stream including data for the desired day.
details : 3x1 list or numpy array.
List containing station name, minimum and maximum frequency to filter.
date : UTCDateTime object.
Desired day to compute the drum file.
Returns
-------
drum_list : Python list.
DESCRIPTION.
'''
DAY2SEC = 86400
TWOMINSEC = 120
LAPSE = 1-1e-5 # Infinitesimaly shorter than 1 s.
internal_name, fmin, fmax, _ = details
stream.detrend('linear')
stream.filter('bandpass', freqmin=fmin, freqmax=fmax, zerophase=True)
drum_list = [ [] for gr in range(DAY2SEC//TWOMINSEC) ]
startt = UTCDateTime(ddate.year, ddate.month, ddate.day)
sbar = tqdm.tqdm(range(DAY2SEC))
for second in sbar:
sbar.set_description('Seconds')
sliced_st = stream.slice(startt+second, startt+second+LAPSE).copy()
if sliced_st:
aux_max = -1e20
aux_min = 1e20
for tr in sliced_st:
aux_max = tr.data.max() if tr.data.max()>=aux_max else aux_max
aux_min = tr.data.min() if tr.data.min()<=aux_min else aux_min
else:
aux_max = aux_min = 0
sbar.set_description('No data')
index = second // TWOMINSEC
drum_list[index].append(int(aux_max))
drum_list[index].append(int(aux_min))
return drum_list
def data2file(drum_data, drum_path, startt, station):
'''
Save drum data to drum ASCII file.
Parameters
----------
drum_data : Nx1 List or Numpy Array.
List object containing min/max per second data per two minutes data.
drum_path : String.
Path to where the desired file will be saved.
startt : UTCDateTime object.
Starting time at midnight of the desired day.
Returns
-------
None.
'''
TWOMINSEC = 120
COLHEAD = 73 # Length of header
EVERY = 6 # 6 values per row
if os.path.exists(drum_path):
os.remove(drum_path)
os.makedirs(os.path.dirname(drum_path), exist_ok=True)
drum_file = open(drum_path, 'a')
for k, segment in enumerate(drum_data):
if not all(v==0 for v in segment): # Do not save if full of zeroes.
aux_start = startt + k*TWOMINSEC
day_header = f'{aux_start.year:04} {aux_start.month:02} {aux_start.day:02}'
hour_header = f'{aux_start.hour:02} {aux_start.minute:02}'
header = f' {station:<4}{day_header} {hour_header} {TWOMINSEC*2} 1'
full_head = f'{header:<{COLHEAD}}' + '\n'
drum_file.write(full_head)
subsegs = [ segment[i:i+EVERY] for i in range(0, len(segment), EVERY) ]
for subseg in subsegs:
data_line = ''.join([ f'{val:>12}' for val in subseg]) + '\n'
drum_file.write(data_line)
drum_file.close()
def crea_newdict(base_dict, _stal, _netl, _nostal):
'''
Create new dictionary based on (un)selected stations/groups
Parameters
----------
base_dict : Dictionary formatted as: { 'NET.STA.LOCID.CHAN' :
['Name', fmin, fmax, 'group']}
Base dictionary containing information
_stal : List
Selected stations to be included (default is ALL)
_netl : List
Selected group of stations to be included (default is ALL)
_nostal : List
Selected stations to not be included (default is NONE)
Returns
-------
new_dict : Dictionary
Dictionary with selected stations
'''
if _stal[0].upper()=='ALL' and _netl[0].upper()=='ALL':
new_dict = base_dict
elif _netl[0].upper()=='ALL' and _stal[0].upper()!='ALL':
new_dict = { key:value for key, value in base_dict.items()
if key.split('.')[1] in _stal }
elif _netl[0].upper()!='ALL' and _stal[0]=='ALL':
new_dict = { key:value for key, value in base_dict.items()
if value[3] in _netl and key.split('.')[1] not in _nostal }
else:
new_dict = { key:base_dict[key] for key, values in base_dict.items()
if ( values[0] in _stal or values[3] in _netl ) and values[0] not in _nostal }
return new_dict
def input2date(date):
## Setting the date for the drum plot creation
if date.upper() in ('TODAY', 'T', 'NOW'):
target = UTCDateTime.now()
elif date.upper() in ('YESTERDAY', 'YES', 'Y'):
target = UTCDateTime.now() - 3600*24
else:
try:
target = UTCDateTime(date)
except:
sys.exit('Time string is wrong. Format is YYYY-MM-DD (or YYYYMMDD)')
return target
def ext_drum(day, dictio, comp, output_dir):
sds_arc = '/mnt/SC-Share/seiscomp/var/lib/archive'
cl = Client(sds_arc)
for key, details in dictio.items():
print(key)
network, station, location, chan = key.split('.')
channel = f'{chan}{comp}'
start = UTCDateTime(day.year, day.month, day.day)
aux_start = start - 5*60
aux_endt = start + 24*3600 + 5*60
st = cl.get_waveforms(network, station, location,
channel, aux_start, aux_endt)
if not st:
print('No waveform data for this station')
continue
drum_list = data_drum(st, details, day)
file_name = f'DR_{start.year:04}{start.month:02}{start.day:02}.{station}'
path_drum_file = os.path.join(output_dir, details[3],
'drum', f'{day.month:02}', file_name)
data2file(drum_list, path_drum_file, start, station)
unix2dos = f'sudo unix2dos -n {path_drum_file} {path_drum_file}'
os.system(unix2dos)
if __name__ == "__main__":
args = receive()
date = args.day
stations = args.stations
component = args.comp
output_drum = args.outputdir
sta_dictfile = args.extradict
stationsl = [ s.upper() for s in (args.stations).split(',') ]
## Initialize parameters
sta_dict_drum = json.load(open(sta_dictfile))
new_dict = crea_newdict(sta_dict_drum, stationsl, 'ALL', '')
target_day = input2date(date)
## Create drum files if desired
ext_drum(target_day, new_dict, component, output_drum)