-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent_response_spider.py
97 lines (81 loc) · 2.89 KB
/
event_response_spider.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
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose: Find response files for stations in each event
# Status: Developing
# Dependence: Python 3.6
# Version: ALPHA
# Created Date: 15:58h, 31/01/2018
# Usage:
# python event_response_spider.py
#
#
# Author: Xiao Xiao, https://github.com/SeisPider
# Email: [email protected]
# Copyright (C) 2017-2017 Xiao Xiao
#-------------------------------------------------------------------------------
"""
Find and rewrite response files for each event
"""
from lib.respider import SourceResponse, logger
from obspy import UTCDateTime
from os.path import join, exists
import os, codecs
import multiprocessing as mp
from multiprocessing import Manager, Process, Pool
import itertools as it
def event_assign(time, database, export_dir="./event"):
"""Create response files for one particular event
Parameter
=========
time : `~ObsPy.UTCDateTime`
Origin Time
database : `~respider.SourceResponse`
database including response files
export_dir : str
directory of output
"""
# check directory existences
subdir = join(export_dir, time.strftime("%Y%m%d%H%M%S"))
if not exists(subdir):
os.makedirs(subdir, exist_ok=True)
def network_rewrite(network_resp, subdir):
"""Handle rewrite work of a network
"""
for key, value in network_resp.items():
net, sta, loc, cha = key.split(".")
outputfilename = "_".join(["PZs", net, sta, loc, cha])
outputfilename = join(subdir, outputfilename)
try:
rewrite_sacpz(value, outputfilename)
except:
logger.error("Can't rewrite")
responses = database.response_files_extractor(time)
for response in responses:
network_rewrite(response, subdir)
def rewrite_sacpz(inputfilename, outputfilename):
"""output sacpz file
Parameter
=========
inputfilename : str
file location and name of inputted sacpz file
outputfilename : str
file location and name of to be wrriten sacpz file
"""
with codecs.open(inputfilename, 'r', 'gbk') as inputf:
lines = inputf.readlines()
with codecs.open(outputfilename, 'w') as outputf:
for line in lines:
if line[0] == "*":
continue
else:
outputf.writelines(line)
if __name__ == '__main__':
# import dresponse file location
sourceresponse = SourceResponse(subdir="./info/Response")
# import event info
with open("./catalog_released.csv") as f:
lines = f.readlines()
for line in lines:
origin = UTCDateTime(line.split()[0])
event_assign(origin, sourceresponse)
logger.info("Fini. {}".format(origin.strftime("%Y%m%d%H%M%S")))