-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateDartStation.py
90 lines (83 loc) · 3.51 KB
/
UpdateDartStation.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
#! /usr/bin/python
# -*- coding: utf-8 -*-
'''Update Dart Station information'''
__author__ = 'Junwei Deng'
from urllib import request
import re
import os
import csv
import operator
def UpdateDartStation():
'''download a station list of Dart and some necessary stat'''
urlNOAADartList = 'http://www.ndbc.noaa.gov/dart.shtml'#main page to get a list
with request.urlopen(urlNOAADartList) as f:
data=f.read()
data=data.decode('utf-8')
Temp=data.split('dartstns = [')[1].split('];',1)[0].replace("'","")
stations = [x.strip() for x in Temp.split(',')]
#print(stations)
with open("./cache/DartStationRecord.csv","r",encoding="utf-8")as f:
station_csv=csv.reader(f)
stationNum=[row[0] for row in station_csv]
stationNum.remove('StationNumber')
#print (stationNum)
f.close()
if operator.eq(stationNum,stations)==True:
print('No need to refresh Dart station!')
return
urlNOAAStationhead='http://www.ndbc.noaa.gov/station_page.php?station='
longitude=[]
latitude=[]
waterdepth=[]
for station in stations:
print(station,"is updating")
urlNOAAStation=urlNOAAStationhead+station
#print(urlNOAAStation)
with request.urlopen(urlNOAAStation) as f:
data=f.read()
data=data.decode('utf-8')
Temp=data.split(station+' (',1)[1].split(')',1)[0]#Temp is row data of location(e.g.'134.342N 18.954W')
try:
Temp2=data.split('Water depth:</b> ',1)[1].split(' m')[0]
except IndexError as e:
Temp2='-1'
#print(Temp)
#print(Temp2)
if re.match(r'(\d{1,3}.\d{3})N\s(\d{1,3}.\d{3})E',Temp):#use regex to match the string we get
m=re.match(r'(\d{1,3}.\d{3})N\s(\d{1,3}.\d{3})E',Temp)
latitude.append(m.group(1))
longitude.append(m.group(2))
waterdepth.append(Temp2)
continue
if re.match(r'(\d{1,3}.\d{3})S\s(\d{1,3}.\d{3})E',Temp):
m=re.match(r'(\d{1,3}.\d{3})S\s(\d{1,3}.\d{3})E',Temp)
latitude.append('-'+m.group(1))
longitude.append(m.group(2))
waterdepth.append(Temp2)
continue
if re.match(r'(\d{1,3}.\d{3})N\s(\d{1,3}.\d{3})W',Temp):
m=re.match(r'(\d{1,3}.\d{3})N\s(\d{1,3}.\d{3})W',Temp)
latitude.append(m.group(1))
longitude.append('-'+m.group(2))
waterdepth.append(Temp2)
continue
if re.match(r'(\d{1,3}.\d{3})S\s(\d{1,3}.\d{3})W',Temp):
m=re.match(r'(\d{1,3}.\d{3})S\s(\d{1,3}.\d{3})W',Temp)
latitude.append('-'+m.group(1))
longitude.append('-'+m.group(2))
waterdepth.append(Temp2)
continue
if os.path.exists('./cache'):#new cache folder for now and future
pass
else:
os.mkdir('./cache')
c=open("./cache/DartStationRecord.csv","w",newline='')#newline='' is for no empty line
#print('Open correctly!')
writer=csv.writer(c)#open the earthquake data cache file
writer.writerow(['StationNumber','Longitude','Latitude','WaterDepth'])
for station,longitude,latitude,waterdepth in zip(stations,longitude,latitude,waterdepth):
Temp=[station,longitude,latitude,waterdepth]
writer.writerow(Temp)
c.close()
print('Dart Station update!')
#updateDartStation()