-
Notifications
You must be signed in to change notification settings - Fork 15
/
down_stp_data.py
53 lines (50 loc) · 1.81 KB
/
down_stp_data.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
""" Download Example SCSN data by STP
STP can be downloaded from https://scedc.caltech.edu/data/stp/index.html
"""
import os, shutil, glob
from obspy import UTCDateTime
import subprocess
import multiprocessing as mp
# i/o files
num_workers = 5
fsta = 'input/example_pal.sta'
time_range = '20190704-20190707'
out_root = '/data/Example_data'
if not os.path.exists(out_root): os.makedirs(out_root)
start_time, end_time = [UTCDateTime(date) for date in time_range.split('-')]
num_days = (end_time.date - start_time.date).days
def down_stp_data(net, sta, date):
# make out_dir
year = str(date.year)
mon = str(date.month).zfill(2)
day = str(date.day).zfill(2)
date = year + mon + day
out_dir = os.path.join(out_root, date)
if not os.path.exists(out_dir): os.makedirs(out_dir)
# check download
out_paths = glob.glob(os.path.join(out_dir, '%s%s%s*.%s.%s.*'%(year, mon, day, net, sta)))
if len(out_paths)==3: return
# download & move
p = subprocess.Popen(['stp'], stdin=subprocess.PIPE)
s = "GAIN ON \n"
s+= "WIN {} {} HH% {}/{}/{},00:00:00 +1d \n".format(net, sta, year, mon, day)
s+= "quit \n"
p.communicate(s.encode())
# rename & move to out dir
fnames = glob.glob('%s%s%s*.%s.%s.*'%(year, mon, day, net, sta))
for fname in fnames:
chn = fname.split('.')[-2]
fname_new = '%s.%s.%s.%s.SAC'%(net,sta,date,chn)
os.rename(fname, fname_new)
shutil.move(fname_new, out_dir)
# download data
pool = mp.Pool(num_workers)
f=open(fsta); lines=f.readlines(); f.close()
for line in lines:
net, sta = line.split(',')[0].split('.')
for i in range(num_days):
date = start_time + i*86400
#down_stp_data(net, sta, date) # use single process for debugging
pool.apply_async(down_stp_data, args=(net, sta, date,))
pool.close()
pool.join()