Commit 21a0ee3e authored by Mijian Xu's avatar Mijian Xu 😷
Browse files

init util.py

parent a9866cd2
File mode changed from 100644 to 100755
#!/usr/bin/env python
#
#Author: Mijian Xu at NJU
# Author: Mijian Xu at NJU
#
#Revision History:
# Revision History:
# 2015/08/19
# 2015/09/23
#
def Usage():
print('Usage:')
print('python bqmail_conti.py -Istation.lst -Yyear1/month1/day1/year2/month2/day2 -Hhour [-Cchannel] [-Fformat] head.cfg')
print('-I -- Station list. format: Network station')
print('-Y -- Date range.')
print('-C -- Channel (e.g., ?H?, HHZ, BH?). Default: BH?')
print('-H -- Request continuous wave by hour.')
print('-F -- File format (SEED or miniseed). Default: SEED')
print('head.cfg -- Config file.')
print('Example: ./bqmail_conti.py -Iex_sta.lst -Y2003/12/3/2003/12/4 -H24 head.cfg')
# 2016/10/03
import datetime
import os, re
import sys, getopt
import time
import subprocess
from util import generatemsg, sendmail
try:
import configparser
config = configparser.ConfigParser()
......@@ -30,16 +21,28 @@ except:
import ConfigParser
config = ConfigParser.ConfigParser()
def Usage():
print('Usage:')
print('python bqmail_conti.py -Istation.lst -Yyear1/month1/day1/year2/month2/day2 -Hhour [-Cchannel] [-Fformat] head.cfg')
print('-I -- Station list. format: Network station')
print('-Y -- Date range.')
print('-C -- Channel (e.g., ?H?, HHZ, BH?). Default: BH?')
print('-H -- Request continuous wave by hour.')
print('-F -- File format (SEED or miniseed). Default: SEED')
print('head.cfg -- Config file.')
print('Example: ./bqmail_conti.py -Iex_sta.lst -Y2003/12/3/2003/12/4 -H24 head.cfg')
head = ''
argv = sys.argv[1:]
for o in argv:
if os.path.isfile(o):
print(o)
head = o
argv.remove(o)
break
try:
opts,args = getopt.getopt(argv, "hI:C:Y:H:F:")
opts,args = getopt.getopt(argv, "hI:C:b:e::H:F:c:")
except:
print('Arguments are not found!')
Usage()
......@@ -47,20 +50,30 @@ except:
if opts == []:
Usage()
sys.exit(1)
ops = [arg[0] for arg in opts]
chan = "BH?"
fformat = "seed"
isrange = True
for op, value in opts:
if op == "-I":
infile = value
elif op == "-H":
timeval = float(value)
elif op == "-Y":
yrange = value
isyrange = 1
elif op == "-b":
starttime = value
elif op == "-e":
endtime = value
elif op == "-C":
chan = value
elif op == "-F":
fformat = value
elif op == "-c":
datetimefile = value
if not os.path.exists(datetimefile):
print("No such file %s" % datetimefile)
sys.exit(1)
else:
isrange = False
elif op == "-h":
Usage()
sys.exit(1)
......@@ -68,23 +81,6 @@ for op, value in opts:
Usage()
sys.exit(1)
if head == '':
print("Head file are not exist!")
Usage()
sys.exit(1)
if isyrange:
y_split = yrange.split('/')
year1 = int(y_split[0])
mon1 = int(y_split[1])
day1 = int(y_split[2])
year2 = int(y_split[3])
mon2 = int(y_split[4])
day2 = int(y_split[5])
datemin=datetime.datetime(year1,mon1,day1)
datemax=datetime.datetime(year2,mon2,day2)
config.read(head)
eventlst = config.get("lst","eventlst")
NAME = config.get("info","NAME")
......@@ -93,7 +89,8 @@ EMAIL = config.get("info","EMAIL")
MEDIA = config.get("info","MEDIA")
ALTERNATEMEDIA = MEDIA
if fformat.lower() == 'seed':
recipient = 'breq_fast@iris.washington.edu'
# recipient = 'breq_fast@iris.washington.edu'
recipient = "gomijianxu@gmail.com"
elif fformat.lower() == 'miniseed':
recipient = 'miniseed@iris.washington.edu'
else:
......@@ -109,31 +106,57 @@ for stainfo in fid.readlines():
sta.append([stainfo_sp[0], stainfo_sp[1], stainfo_sp[2]])
else:
sta.append([stainfo_sp[0], stainfo_sp[1], ''])
fid.close()
if isrange:
if "T" in starttime:
datemin = datetime.datetime.strptime(starttime,"%Y-%m-%dT%H:%M:%S")
else:
try:
datemin = datetime.datetime.strptime(starttime,"%Y-%m-%d")
except:
print ("Wrong format in -b option")
sys.exit(1)
if "T" in endtime:
datemax = datetime.datetime.strptime(endtime,"%Y-%m-%dT%H:%M:%S")
else:
try:
datemax = datetime.datetime.strptime(endtime,"%Y-%m-%d")
except:
print ("Wrong format in -e option")
sys.exit(1)
nowtime = datemin
while 1:
if nowtime >= datemax:
break
endtime = nowtime + datetime.timedelta(hours=timeval)
LABEL = 'IRIS_'+nowtime.strftime('%Y.%m.%d.%H')
msg = generatemsg(NAME, INST, EMAIL, MEDIA, ALTERNATEMEDIA, LABEL)
for sta_row in sta:
msg += sta_row[1]+' '+sta_row[0]+' '+nowtime.strftime('%Y %m %d %H %M %S')+' '+endtime.strftime('%Y %m %d %H %M %S')+' 1 '+chan+' '+sta_row[2]+'\n'
check_send = sendmail(recipient, msg)
if check_send:
print("Successful sending the mail from "+nowtime.strftime('%Y.%m.%d.%H')+" to "+endtime.strftime('%Y.%m.%d.%H')+"!!!")
time.sleep(5)
else:
print("Some error occured")
else:
with open(datetimefile) as f:
datelst = [datetime.datetime.strptime(line.strip(), "%Y.%m.%d") for line in f.readlines()]
for nowtime in datelst:
endtime = nowtime + datetime.timedelta(hours=timeval)
LABEL = "IRIS_"+nowtime.strftime('%Y.%m.%d.%H')
msg = generatemsg(NAME, INST, EMAIL, MEDIA, ALTERNATEMEDIA, LABEL)
for sta_row in sta:
msg += sta_row[1]+' '+sta_row[0]+' '+nowtime.strftime('%Y %m %d %H %M %S')+' '+endtime.strftime('%Y %m %d %H %M %S')+' 1 '+chan+' '+sta_row[2]+'\n'
check_send = sendmail(recipient, msg)
if check_send:
print("Successful sending the mail from "+nowtime.strftime('%Y.%m.%d.%H')+" to "+endtime.strftime('%Y.%m.%d.%H')+"!!!")
time.sleep(5)
else:
print("Some error occured")
nowtime = datemin
while 1:
if nowtime >= datemax:
break
endtime = nowtime + datetime.timedelta(hours=timeval)
LABEL = 'IRIS_'+nowtime.strftime('%Y')+'.'+nowtime.strftime('%m')+'.'+nowtime.strftime('%d')+'.'+nowtime.strftime('%H')
msg = ''
msg += '.NAME '+NAME+'\n'
msg += '.INST '+INST+'\n'
msg += '.MAIL\n'
msg += '.EMAIL '+EMAIL+'\n'
msg += '.PHONE\n'
msg += '.FAX\n'
msg += '.MEDIA '+MEDIA+'\n'
msg += '.ALTERNATE MEDIA '+ALTERNATEMEDIA+'\n'
msg += '.ALTERNATE MEDIA '+ALTERNATEMEDIA+'\n'
msg += '.LABEL '+LABEL+'\n'
msg += '.END\n'
for sta_row in sta:
msg += sta_row[1]+' '+sta_row[0]+' '+nowtime.strftime('%Y %m %d %H %M %S')+' '+endtime.strftime('%Y %m %d %H %M %S')+' 1 '+chan+' '+sta_row[2]+'\n'
with open('tmp.bq','w') as fid_msg:
fid_msg.write(msg)
os.system('mail '+recipient+'<tmp.bq')
print("Successful sending the mail between "+nowtime.strftime('%Y')+'.'+nowtime.strftime('%m')+'.'+nowtime.strftime('%d')+'.'+nowtime.strftime('%H')+" and "+endtime.strftime('%Y')+'.'+endtime.strftime('%m')+'.'+endtime.strftime('%d')+'.'+endtime.strftime('%H')+"!!!")
nowtime = nowtime + datetime.timedelta(hours=timeval)
time.sleep(4.5)
os.system('rm tmp.bq')
#!/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
import urllib.request as rq
import re
from multiprocessing.dummy import Pool as ThreadPool
import subprocess
import sys
import getopt
def Usage():
print("Usage: download_seed.py -n<thread-num> -u<user-name> [-P<out-path>]")
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):
resp = subprocess.Popen("wget -c -nc -P "+path+" "+url,shell=True)
resp.wait()
argv = sys.argv[1:]
path = "./"
try:
opts, args = getopt.getopt(argv, "u:n:P:")
except:
print("Arguments are not found!")
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:
sys.exit(1)
url = "http://ds.iris.edu/pub/userdata/"+username
html = rq.urlopen(url)
content = html.read().decode()
lst = []
find_re = re.compile(r'href=.+?>',re.DOTALL)
for line in find_re.findall(content):
if line.find("seed") > 0:
lst.append(line[6:-2])
lstpath = os.path.expanduser("~"),".IRIS.lst")
if not os.path.exists(lstpath):
os.mknod(lstpath, mode=0o600)
with open(listpath, "r+") as f:
oldlst = [line.strip() for line in f.readlines()]
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 for line in lst]
pool = ThreadPool(thread)
print("start downloading")
path_lst = [path]*len(link_lst)
results = pool.map(wget, link_lst, path_lst)
pool.close()
pool.join()
#!/usr/bin/env python
import subprocess
def generatemsg(NAME, INST, EMAIL, MEDIA, ALTERNATEMEDIA, LABEL):
msg = ''
msg += '.NAME '+NAME+'\n'
msg += '.INST '+INST+'\n'
msg += '.MAIL\n'
msg += '.EMAIL '+EMAIL+'\n'
msg += '.PHONE\n'
msg += '.FAX\n'
msg += '.MEDIA '+MEDIA+'\n'
msg += '.ALTERNATE MEDIA '+ALTERNATEMEDIA+'\n'
msg += '.ALTERNATE MEDIA '+ALTERNATEMEDIA+'\n'
msg += '.LABEL '+LABEL+'\n'
msg += '.END\n'
return msg
def sendmail(recipient, msg):
p = subprocess.Popen(["mail",recipient], stdin=subprocess.PIPE)
try:
p.communicate(msg.encode())
p.wait()
return True
except:
return 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