Commit 2ac51daa authored by Mijian Xu's avatar Mijian Xu 😷
Browse files

Initial commit

parents
*/__pycache__/
*.egg-info
build/
dist/
\ No newline at end of file
from bqmail import query
\ No newline at end of file
from bqmail.query import Query
from obspy import UTCDateTime
from obspy.taup import TauPyModel
from obspy.clients.iris import Client
import smtplib
import time
from email.mime.text import MIMEText
model = TauPyModel()
cld = Client()
def sendmail(sender, contents, server='localhost',
recipient='breq_fast@iris.washington.edu',
port=465, password=''):
msg = MIMEText(contents, 'text')
msg['Subject'] = 'BREQ_fast'
msg['From'] = 'bqmail<'+sender+'>'
msg['To'] = recipient
if server == 'localhost':
try:
smtpObj = smtplib.SMTP(server)
smtpObj.sendmail(sender, recipient, msg.as_string())
return True
except smtplib.SMTPException:
print("Error when send mail by localhost")
return False
else:
try:
smtpObj = smtplib.SMTP_SSL(server, port)
smtpObj.login(sender, password)
smtpObj.sendmail(sender, recipient, msg.as_string())
return True
except smtplib.SMTPException:
print("Error in linking {}".format(server))
return False
def generatemsg(username, inst, mailname, media):
msg = ''
msg += '.NAME '+username+'\n'
msg += '.INST '+inst+'\n'
msg += '.MAIL\n'
msg += '.EMAIL '+mailname+'\n'
msg += '.PHONE\n'
msg += '.FAX\n'
msg += '.MEDIA '+media+'\n'
msg += '.ALTERNATE MEDIA '+media+'\n'
msg += '.ALTERNATE MEDIA '+media+'\n'
return msg
class BQMail():
def __init__(self, mailname, username='bqmail',
inst='', media='Electronic (FTP)',
server='localhost', password=''):
self.query = Query()
self.mailname = mailname
self.server = server
self.password = password
self.username = username
self.inst = inst
self.media = media
self.msgs = []
self.labels = []
self.header = generatemsg(username, inst, mailname, media)
def query_stations(self, **kwargs):
self.query.get_stations(**kwargs)
def query_events(self, **kwargs):
self.query.get_events(**kwargs)
def write_mail(self, arrange='station', time_before=0, time_after=1000,
mark='o', channel='BH?', location='', time_sleep=2):
locs = locals()
locs.pop('arrange')
locs.pop('self')
locs.pop('time_sleep')
if arrange == 'station':
self.station_mail(**locs)
for label, msg in zip(self.labels, self.msgs):
succ = sendmail(self.mailname, msg, server=self.server,
password=self.password)
if succ:
print('successfully send {}'.format(label))
time.sleep(time_sleep)
def station_mail(self, time_before=0, time_after=1000,
mark='o', channel='BH?', location=''):
for net in self.query.stations:
for sta in net:
msg = self.header
label = '{}.{}'.format(net.code, sta.code)
self.labels.append(label)
msg += '.LABEL {}\n.END\n'.format(label)
if mark == 'o':
for i, evt in self.query.events.iterrows():
b_time = (evt['date'] + time_before).strftime('%Y %m %d %H %M %S')
e_time = (evt['date'] + time_after).strftime('%Y %m %d %H %M %S')
msg += '{} {} {} {} 1 {} {}\n'.format(
sta.code, net.code, b_time, e_time,
channel, location)
else:
for i, evt in self.query.events.iterrows():
ttime = self.get_ttime(evt, sta, phase=mark)
if ttime is None:
continue
else:
b_time = (evt['date'] + ttime + time_before).strftime('%Y %m %d %H %M %S')
e_time = (evt['date'] + ttime + time_after).strftime('%Y %m %d %H %M %S')
msg += '{} {} {} {} 1 {} {}\n'.format(
sta.code, net.code, b_time, e_time,
channel, location)
self.msgs.append(msg)
def get_ttime(self, evt, obspy_station, phase='P'):
da = cld.distaz(obspy_station.latitude, obspy_station.longitude,
evt.evla, evt.evlo)
arr = model.get_travel_times(evt.evdp, da['distance'], phase_list=[phase])
if len(arr) == 0:
return None
else:
return arr[0].time
from obspy.clients.fdsn import Client
from obspy import UTCDateTime
import pandas as pd
import argparse
import sys
def _cat2df(cat):
cols = ['date', 'evla', 'evlo', 'evdp', 'mag', 'magtype']
data = [[evt.origins[0].time, evt.origins[0].latitude,
evt.origins[0].longitude, evt.origins[0].depth*0.001,
evt.magnitudes[0].mag, evt.magnitudes[0].magnitude_type] for evt in cat if evt.origins[0].depth is not None]
return pd.DataFrame(data, columns=cols)
class Query():
def __init__(self):
self.client = Client("IRIS")
def get_events(self, starttime=UTCDateTime(2000, 1, 1),
endtime=UTCDateTime.now(), **kwargs):
events = self.client.get_events(starttime=starttime,
endtime=endtime, **kwargs)
self.events = _cat2df(events)
def get_stations(self, **kwargs):
self.stations = self.client.get_stations(**kwargs)
def get_events():
parser = argparse.ArgumentParser(description="Get seismic events from IRIS WS")
parser.add_argument('-b', help='Start time', type=str, default=None)
parser.add_argument('-e', help='End time', type=str, default=None)
parser.add_argument('-d', help='Radial geographic constraints with <lat>/<lon>/<minradius>/<maxradius>', type=str, default=None)
parser.add_argument('-r', help='Box range with <lon1>/<lon2>/<lat1>/<lat2>', type=str, default=None)
parser.add_argument('-m', help='Magnitude <minmagnitude>[/<maxmagnitude>]', type=str, default=None)
parser.add_argument('-p', help='Focal depth <mindepth>[/<maxdepth>]', type=str, default=None)
parser.add_argument('-c', help='Catalog', type=str, default=None)
arg = parser.parse_args()
args = {}
if arg.c is not None:
args['catalog'] = arg.c
if arg.p is not None:
try:
values = [float(value) for value in arg.p.split('/')]
except:
raise ValueError('Error format with focal depth')
if len(values) == 1:
args['mindepth'] = values[0]
elif len(values) == 2:
args['mindepth'] = values[0]
args['maxdepth'] = values[1]
else:
raise ValueError('Error format with focal depth')
if arg.m is not None:
try:
values = [float(value) for value in arg.m.split('/')]
except:
raise ValueError('Error format with magnitude')
if len(values) == 1:
args['minmagnitude'] = values[0]
elif len(values) == 2:
args['minmagnitude'] = values[0]
args['maxmagnitude'] = values[1]
else:
raise ValueError('Error format with focal depth')
if arg.r is not None:
try:
values = [float(value) for value in arg.r.split('/')]
except:
raise ValueError('Error format with box range')
if len(values) == 4:
args['minlongitude'] = values[0]
args['maxlongitude'] = values[1]
args['minlatitude'] = values[2]
args['maxlatitude'] = values[3]
else:
raise ValueError('Error format with box range')
elif arg.d is not None:
try:
values = [float(value) for value in arg.d.split('/')]
except:
raise ValueError('Error format with Radial geographic constraints')
if len(values) == 4:
args['latitude'] = values[0]
args['longitude'] = values[1]
args['minradius'] = values[2]
args['maxradius'] = values[3]
else:
raise ValueError('Error format with radial geographic constraints')
else:
pass
if arg.b is not None:
try:
args['starttime'] = UTCDateTime(arg.b)
except:
raise ValueError('-b: Error format with time string')
if arg.e is not None:
try:
args['endtime'] = UTCDateTime(arg.e)
except:
raise ValueError('-e: Error format with time string')
if args == {}:
parser.print_usage()
sys.exit(1)
query = Query()
query.get_events(**args)
for i, row in query.events.iterrows():
print('{} {:.2f} {:.2f} {:.2f} {:.1f} {}'.format(
row.date.isoformat(), row.evla, row.evlo, row.evdp, row.mag, row.magtype))
def get_stations():
parser = argparse.ArgumentParser(description="Get stations from IRIS WS")
parser.add_argument('-n', help='Network', type=str, default=None)
parser.add_argument('-s', help='Station', type=str, default=None)
parser.add_argument('-r', help='Box range with <lon1>/<lon2>/<lat1>/<lat2>', type=str, default=None)
parser.add_argument('-d', help='Radial geographic constraints with <lat>/<lon>/<minradius>/<maxradius>', type=str, default=None)
parser.add_argument('-b', help='Start time', type=str, default=None)
parser.add_argument('-e', help='End time', type=str, default=None)
parser.add_argument('-c', help='Channel', type=str, default=None)
arg = parser.parse_args()
args = {}
if arg.n is not None:
args['network'] = arg.n
if arg.s is not None:
args['station'] = arg.s
if arg.r is not None:
try:
values = [float(value) for value in arg.r.split('/')]
except:
raise ValueError('Error format with box range')
if len(values) == 4:
args['minlongitude'] = values[0]
args['maxlongitude'] = values[1]
args['minlatitude'] = values[2]
args['maxlatitude'] = values[3]
else:
raise ValueError('Error format with box range')
elif arg.d is not None:
try:
values = [float(value) for value in arg.d.split('/')]
except:
raise ValueError('Error format with Radial geographic constraints')
args['latitude'] = values[0]
args['longitude'] = values[1]
args['minradius'] = values[2]
args['maxradius'] = values[3]
else:
pass
if arg.b is not None:
try:
args['starttime'] = UTCDateTime(arg.b)
except:
raise ValueError('-b: Error format with time string')
if arg.e is not None:
try:
args['endtime'] = UTCDateTime(arg.e)
except:
raise ValueError('-e: Error format with time string')
if arg.c is not None:
args['channel'] = arg.c
if args == {}:
parser.print_usage()
sys.exit(1)
query = Query()
query.get_stations(**args)
for net in query.stations:
for sta in net:
print('{} {} {:.4f} {:.4f} {} {} {}'.format(net.code, sta.code,
sta.latitude, sta.longitude, sta.start_date, sta.end_date,
sta.restricted_status))
if __name__ == "__main__":
query = Query()
query.get_events(minmagnitude=7, catalog='GCMT')
query.get_stations(network='3J')
# query.get_stations(network='3J', minlatitude=20,
# minlongitude=97, maxlatitude=40,
# maxlongitude=110)
print(query.events)
print(query.stations)
#!/usr/bin/env python
#
# Download seed file from IRIS DMC. Duplicate files from server would be check.
#
# Author: Mijian Xu @ Nanjing University
#
# History: 2016-09-29, Init Code, Mijian Xu
#
import os
try:
import urllib.request as rq
except:
import urllib as rq
import re
from multiprocessing.dummy import Pool as ThreadPool
import subprocess
import sys
import getopt
def Usage():
print("Usage: download_seed.py -u<user-name> [-n<thread-num>] [-P<out-path>] [filename]")
print(" -P Specify out path of downloaded seed files.")
print(" Default: current dirctory")
print(" -n Specify thread number at parallel downloading")
print(" -u Specify username in user directory on the IRIS.")
def wget(url_path):
url = url_path[0]
path = url_path[1]
resp = subprocess.Popen("wget -c -nc -P "+path+" "+url,shell=True)
resp.wait()
thread = 1
argv = sys.argv[1:]
if argv == []:
Usage()
sys.exit(1)
path = "./"
filename = None
try:
opts, args = getopt.getopt(argv, "u:n:P:")
except:
print("Arguments are not found!")
Usage()
sys.exit(1)
for op, value in opts:
if op == "-n":
thread = int(value)
elif op == "-u":
username = value
elif op == "-P":
path = value
else:
Usage()
sys.exit(1)
for op in argv:
if op[0] != '-':
thread = 1
filename = op
url = "http://ds.iris.edu/pub/userdata/"+username
html = rq.urlopen(url)
content = html.read().decode()
lst = []
print(filename)
if filename == None:
find_re = re.compile(r'href=".+?">',re.DOTALL)
for line in find_re.findall(content):
if line.find("mseed") > 0:
lst.append(line[6:-2])
else:
lst.append(filename)
lstpath = os.path.join(os.path.expanduser("~"),".IRIS.lst")
if os.path.exists(lstpath):
with open(lstpath, "r+") as f:
oldlst = [line.strip() for line in f.readlines()]
else:
oldlst = []
with open(lstpath, "w+") as f:
for line in lst:
f.write(line+"\n")
same_item = list(set(oldlst) & set(lst))
for item in same_item:
lst.remove(item)
if lst == []:
print("The whole date were downloaded from IRIS DMC.")
sys.exit(1)
link_lst = [[url+"/"+line, path] for line in lst]
pool = ThreadPool(thread)
print("start downloading")
results = pool.map(wget, link_lst)
pool.close()
pool.join()
from bqmail.mail import BQMail
from obspy import UTCDateTime
bq = BQMail('gomijianxu@163.com', server='smtp.163.com', password='nanwai99', username='mijian')
bq.query_events(starttime=UTCDateTime(2010, 1, 1), endtime=UTCDateTime(2018, 1, 1),
minmagnitude=5.5, catalog='GCMT')
bq.query_stations(network='CB', station='LZH')
bq.write_mail(time_before=0, time_after=1000)
# bq.write_mail(mark="P", time_before=-100, time_after=300)
#!/usr/bin/env python
from setuptools import find_packages, setup
packages = find_packages()
VERSION = "2.0"
setup(name='bqmail',
version=VERSION,
author='Mijian Xu',
author_email='gomijianxu@gmail.com',
license='GPLv3',
packages=find_packages(),
package_dir={'bqmail': 'bqmail'},
package_data={'': ['data/*']},
install_requires=['obspy', 'pandas'],
entry_points={'console_scripts': ['get_stations=bqmail.query:get_stations',
'get_events=bqmail.query:get_events']},
include_package_data=True,
zip_safe=False
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment