Commit 29049d59 authored by Mijian Xu's avatar Mijian Xu 😷
Browse files

update to 2.1.0

parent 3b5fa1ea
Pipeline #1375 failed with stage
in 15 seconds
import math
import numpy as np
def sind(deg):
rad = math.radians(deg)
return math.sin(rad)
def cosd(deg):
rad = math.radians(deg)
return math.cos(rad)
def tand(deg):
rad = math.radians(deg)
return math.tan(rad)
def cotd(deg):
rad = math.radians(deg)
return math.cos(rad) / math.sin(rad)
def asind(x):
rad = math.asin(x)
return math.degrees(rad)
def acosd(x):
rad = math.acos(x)
return math.degrees(rad)
def atand(x):
rad = math.atan(x)
return math.degrees(rad)
def km2deg(kilometers):
return kilometers / 111.19
def deg2km(degree):
return degree * 111.19
class distaz:
"""
c Subroutine to calculate the Great Circle Arc distance
c between two sets of geographic coordinates
c
c Equations take from Bullen, pages 154, 155
c
c T. Owens, September 19, 1991
c Sept. 25 -- fixed az and baz calculations
c
P. Crotwell, Setember 27, 1995
Converted to c to fix annoying problem of fortran giving wrong
answers if the input doesn't contain a decimal point.
H. P. Crotwell, September 18, 1997
Java version for direct use in java programs.
*
* C. Groves, May 4, 2004
* Added enough convenience constructors to choke a horse and made public double
* values use accessors so we can use this class as an immutable
H.P. Crotwell, May 31, 2006
Port to python, thus adding to the great list of languages to which
distaz has been ported from the origin fortran: C, Tcl, Java and now python
and I vaguely remember a perl port. Long live distaz!
"""
def __init__(self, lat1, lon1, lat2, lon2):
self.stalat = lat1
self.stalon = lon1
self.evtlat = lat2
self.evtlon = lon2
'''
if (lat1 == lat2) and (lon1 == lon2):
self.delta = 0.0
self.az = 0.0
self.baz = 0.0
return
'''
rad = 2. * math.pi / 360.0
"""
c
c scolat and ecolat are the geocentric colatitudes
c as defined by Richter (pg. 318)
c
c Earth Flattening of 1/298.257 take from Bott (pg. 3)
c
"""
sph = 1.0 / 298.257
scolat = math.pi / 2.0 - np.arctan((1. - sph) * (1. - sph) * np.tan(lat1 * rad))
ecolat = math.pi / 2.0 - np.arctan((1. - sph) * (1. - sph) * np.tan(lat2 * rad))
slon = lon1 * rad
elon = lon2 * rad
"""
c
c a - e are as defined by Bullen (pg. 154, Sec 10.2)
c These are defined for the pt. 1
c
"""
a = np.sin(scolat) * np.cos(slon)
b = np.sin(scolat) * np.sin(slon)
c = np.cos(scolat)
d = np.sin(slon)
e = -np.cos(slon)
g = -c * e
h = c * d
k = -np.sin(scolat)
"""
c
c aa - ee are the same as a - e, except for pt. 2
c
"""
aa = np.sin(ecolat) * np.cos(elon)
bb = np.sin(ecolat) * np.sin(elon)
cc = np.cos(ecolat)
dd = np.sin(elon)
ee = -np.cos(elon)
gg = -cc * ee
hh = cc * dd
kk = -np.sin(ecolat)
"""
c
c Bullen, Sec 10.2, eqn. 4
c
"""
delrad = np.arccos(a * aa + b * bb + c * cc)
self.delta = delrad / rad
"""
c
c Bullen, Sec 10.2, eqn 7 / eqn 8
c
c pt. 1 is unprimed, so this is technically the baz
c
c Calculate baz this way to avoid quadrant problems
c
"""
rhs1 = (aa - d) * (aa - d) + (bb - e) * (bb - e) + cc * cc - 2.
rhs2 = (aa - g) * (aa - g) + (bb - h) * (bb - h) + (cc - k) * (cc - k) - 2.
dbaz = np.arctan2(rhs1, rhs2)
dbaz_idx = np.where(dbaz < 0.0)[0]
if len(dbaz_idx) != 0:
if isinstance(dbaz, (int, float)):
dbaz += 2 * math.pi
else:
dbaz[dbaz_idx] += 2 * math.pi
self.baz = dbaz / rad
"""
c
c Bullen, Sec 10.2, eqn 7 / eqn 8
c
c pt. 2 is unprimed, so this is technically the az
c
"""
rhs1 = (a - dd) * (a - dd) + (b - ee) * (b - ee) + c * c - 2.
rhs2 = (a - gg) * (a - gg) + (b - hh) * (b - hh) + (c - kk) * (c - kk) - 2.
daz = np.arctan2(rhs1, rhs2)
daz_idx = np.where(daz < 0.0)[0]
if len(daz_idx) != 0:
if isinstance(daz, (int, float)):
daz += 2 * math.pi
else:
daz[daz_idx] += 2 * math.pi
self.az = daz / rad
"""
c
c Make sure 0.0 is always 0.0, not 360.
c
"""
idx = np.where(np.abs(self.baz - 360.) < .00001)[0]
if len(idx) != 0:
if isinstance(self.baz, float):
self.baz = 0.0
else:
self.baz[idx] = 0.0
idx = np.where(np.abs(self.baz) < .00001)[0]
if len(idx) != 0:
if isinstance(self.baz, float):
self.baz = 0.0
else:
self.baz[idx] = 0.0
idx = np.where(np.abs(self.az - 360.) < .00001)[0]
if len(idx) != 0:
if isinstance(self.az, float):
self.az = 0.0
else:
self.az[idx] = 0.0
idx = np.where(np.abs(self.az) < .00001)[0]
if len(idx) != 0:
if isinstance(self.az, float):
self.az = 0.0
else:
self.az[idx] = 0.0
la_idx = np.where(lat1 == lat2)[0]
lo_idx = np.where(lon1 == lon2)[0]
idx = la_idx[np.where(la_idx == lo_idx)[0]]
if len(idx) != 0:
if isinstance(self.delta, float):
self.delta = 0.
else:
self.delta[idx] = 0.
if isinstance(self.az, float):
self.az = 0.
else:
self.az[idx] = 0.
if isinstance(self.baz, float):
self.baz = 0.
else:
self.baz[idx] = 0.
def getDelta(self):
return self.delta
def getAz(self):
return self.az
def getBaz(self):
return self.baz
def degreesToKilometers(self):
return self.delta * 111.19
# distaz = DistAz(0, 0, 1,1)
# print "%f %f %f" % (distaz.getDelta(), distaz.getAz(), distaz.getBaz())
if __name__ == '__main__':
ela = 1
elo = 1
sla = 2
slo = 1
da = distaz(ela, elo, sla, slo)
print(da.baz)
......@@ -3,36 +3,49 @@ 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
from email.header import Header
from .distaz import distaz
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':
def connectsmtp(server, port, sender, password):
smtpObj = smtplib.SMTP_SSL(server, port)
smtpObj.login(sender, password)
return smtpObj
def loginmail(sender, server='localhost', password='', port=465, test_num=5):
test_it = 1
while test_it <= test_num:
if test_it > 1:
print('Try to link to {} in {} times'.format(server, test_it))
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
smtpObj = connectsmtp(server, port, sender, password)
except:
test_it += 1
continue
else:
break
if test_it > test_num:
raise ConnectionError("Error in linking {}".format(server))
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
return smtpObj
def sendmail(smtpObj, sender, contents,
recipient='breq_fast@iris.washington.edu'):
msg = MIMEText(contents, 'text')
msg['Subject'] = Header('batch request seismic data', 'utf-8')
msg['From'] = Header(sender)
msg['To'] = Header(recipient)
try:
smtpObj.sendmail(sender, recipient, msg.as_string())
return True
except Exception as e:
print('Error in sending mail: {}'.format(e))
return False
def generatemsg(username, inst, mailname, media):
......@@ -60,8 +73,8 @@ class BQMail():
self.username = username
self.inst = inst
self.media = media
self.msgs = []
self.labels = []
self.msgs = ''
self.label = ''
self.header = generatemsg(username, inst, mailname, media)
def query_stations(self, **kwargs):
......@@ -70,53 +83,53 @@ class BQMail():
def query_events(self, **kwargs):
self.query.get_events(**kwargs)
def send_mail(self, arrange='station', time_sleep=2, **kwargs):
if arrange == 'station':
self.station_mail(**kwargs)
def send_mail(self, arrange='events', write_mail=False, **kwargs):
if arrange == 'events':
self.event_mail(**kwargs)
elif arrange == 'continue':
self.conti_mail(**kwargs)
else:
raise ValueError('variable arrange must be in \'station\' and \'continue\'')
for label, msg in zip(self.labels, self.msgs):
succ = sendmail(self.mailname, msg, server=self.server,
raise ValueError('variable arrange must be in \'events\' and \'continue\'')
smtpobj = loginmail(self.mailname, server=self.server,
password=self.password)
if succ:
print('successfully send {}'.format(label))
time.sleep(time_sleep)
# with open('msg.{}'.format(label), 'w') as f:
# f.write(msg)
succ = sendmail(smtpobj, self.mailname, self.msgs)
if succ:
print('successfully send {}'.format(self.label))
else:
print('Error in sending {}'.format(self.label))
if write_mail:
with open('msg.{}'.format(self.label), 'w') as f:
f.write(self.msgs)
def conti_mail(self, starttime=UTCDateTime(2000, 1, 1),
endtime=UTCDateTime.now(), time_val_in_hours=24,
channel='BH?', location=''):
self.query.get_conti(starttime, endtime, hours=time_val_in_hours)
self.msgs = self.header
self.label = '{}_{}'.format(starttime.strftime('%Y.%m.%d'),
endtime.strftime('%Y.%m.%d'))
self.msgs += '.LABEL {}\n.END\n'.format(self.label)
for sdt, edt in self.query.conti_time:
label = sdt.strftime('%Y.%m.%d')
self.labels.append(label)
msg = self.header
msg += '.LABEL {}\n.END\n'.format(label)
for net in self.query.stations:
for sta in net:
msg += '{} {} {} {} 1 {} {}\n'.format(
sta.code, net.code, sdt.strftime('%Y %m %d %H %M %S'),
edt.strftime('%Y %m %d %H %M %S'), channel, location)
self.msgs.append(msg)
self.msgs += '{} {} {} {} 1 {} {}\n'.format(
sta.code, net.code, sdt.strftime('%Y %m %d %H %M %S'),
edt.strftime('%Y %m %d %H %M %S'), channel, location)
def station_mail(self, time_before=0, time_after=1000,
mark='o', channel='BH?', location=''):
def event_mail(self, time_before=0, time_after=1000,
mark='o', channel='BH?', location=''):
self.msgs = self.header
self.label = 'Evts_{}'.format(UTCDateTime.now().strftime('%Y.%m.%dT%H%M%S'))
self.msgs += '.LABEL {}\n.END\n'.format(self.label)
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)
self.msgs += '{} {} {} {} 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)
......@@ -125,15 +138,14 @@ class BQMail():
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)
self.msgs += '{} {} {} {} 1 {} {}\n'.format(
sta.code, net.code, b_time, e_time,
channel, location)
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])
da = distaz(obspy_station.latitude, obspy_station.longitude,
evt.evla, evt.evlo)
arr = model.get_travel_times(evt.evdp, da.delta, phase_list=[phase])
if len(arr) == 0:
return None
else:
......
......@@ -20,12 +20,12 @@ class Query():
def get_events(self, starttime=UTCDateTime(2000, 1, 1),
endtime=UTCDateTime.now(), **kwargs):
events = self.client.get_events(starttime=starttime,
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_stations(self, includerestricted=False, **kwargs):
self.stations = self.client.get_stations(includerestricted=includerestricted, **kwargs)
def get_conti(self, starttime, endtime, hours=24):
self.conti_time = []
......
......@@ -28,7 +28,7 @@ def Usage():
def wget(url_path):
url = url_path[0]
path = url_path[1]
resp = subprocess.Popen("wget -c -nc -P "+path+" "+url,shell=True)
resp = subprocess.Popen("wget -c -nc -P "+path+" "+url, shell=True)
resp.wait()
thread = 1
......
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 = BQMail('xxx@xxx.com', server='smtp.xxx.com', password='xxxxx', username='bqmail')
bq.query_events(starttime=UTCDateTime(2010, 1, 1), endtime=UTCDateTime(2010, 3, 1),
minmagnitude=5.5, catalog='GCMT')
bq.query_stations(network='CB', station='*')
bq.send_mail(starttime=UTCDateTime(2015, 1, 1), endtime=UTCDateTime(2015, 2, 1), arrange='continue')
# bq.write_mail(time_before=0, time_after=1000)
# bq.write_mail(mark="P", time_before=-100, time_after=300)
bq.send_mail(mark="P", time_before=-100, time_after=300, arrange='events')
......@@ -5,7 +5,7 @@ packages = find_packages()
with open("README.md", "r") as fh:
long_description = fh.read()
VERSION = "2.0.7"
VERSION = "2.1.0"
setup(name='bqmail',
version=VERSION,
author='Mijian Xu',
......
Supports Markdown
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