-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetiocData.py
81 lines (76 loc) · 2.97 KB
/
GetiocData.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
#! /usr/bin/python
# -*- coding: utf-8 -*-
'''Get ioc station data and save it in specific file'''
__author__ = 'Junwei Deng'
from dateutil.parser import parse
from urllib import request
from bs4 import BeautifulSoup
import csv
import re
from earthquake import Earthquake
from DateShift import DateShift
from settings import Settings
def GetiocData(stationnum,earthquake,Settings):
#You can directly use this function as following:
# Setting=Settings()
# earthquake=Earthquake()
# earthquake.initfrom('./cache/earthquake.csv',19)
# GetiocData('orma',earthquake,Setting)
ioc='http://www.ioc-sealevelmonitoring.org/bgraph.php?code='+stationnum+'&period=3&endtime='+str(DateShift(earthquake.date,2)[0])+'-'+str(DateShift(earthquake.date,2)[1])+'-'+str(DateShift(earthquake.date,2)[2])
header={'User-Agent': 'Mozilla/5.0'}
page=request.urlopen(ioc)
soup=BeautifulSoup(page,"html.parser")
print(ioc)
filename="./cache/iocData_"+stationnum+earthquake.date[0]+earthquake.date[1]+earthquake.date[2]+earthquake.time_zero[0]+earthquake.time_zero[1]+earthquake.time_zero[2]+".csv"
f = open(filename, 'w',newline='')
csv_writer = csv.writer(f)
time=''
prs=''
index=0
timeindex=0
prsindex=0
radindex=0
tables = soup.findAll('table')
try:
tab = tables[0]
except:
return -1
for tr in tab.findAll('tr'):
for td in tr.findAll('td'):
if td.getText()=='Time (UTC)':
timeindex=index
if td.getText()=='prs(m)':
prsindex=index
if td.getText()=='rad(m)':
radindex=index
index=index+1
break
if (prsindex==0 or Settings.iocprs==False) and (radindex==0 or Settings.iocrad==False):
return -1
for tr in tab.findAll('tr'):
index=0
for td in tr.findAll('td'):
if (index==timeindex):
time=td.getText()
m=re.match(r'(\d{4}-\d{2}-\d{2})\s(\d{2}:\d{2}:\d{2})',time)#2017-09-08 00:00:00
if m:
a = parse(m[1]+r"/"+m[2])# 2017-10-01/12:12:12
b = parse(earthquake.date[0]+'-'+earthquake.date[1]+'-'+earthquake.date[2]+'/'+earthquake.time_zero[0]+':'+earthquake.time_zero[1]+':'+earthquake.time_zero[2])
relatime=(a-b).total_seconds()
else:
relatime='Time(UTC)'
if (index==prsindex and prsindex!=0 and Settings.iocprs==True):
prs=td.getText()
if (index==radindex and radindex!=0 and Settings.iocrad==True and (Settings.iocprs==False or prsindex==0)):
prs=td.getText()
index=index+1
#print(relatime,prs)
try:
csv_writer.writerow([ x for x in [relatime,prs]])
except:
continue
return 0
# Setting=Settings()
# earthquake=Earthquake()
# earthquake.initfrom('./cache/earthquake.csv',19)
# GetiocData('orma',earthquake,Setting)