Output:
('Next: ', (datetime.datetime(2016, 1, 3, 6, 18, 23, 116073), u'4.3'), ' is ', datetime.timedelta(0, 21180, 2472), ' away. /n Previous: ', (datetime.datetime(2016, 1, 2, 23, 49, 23, 115191), u'8.1'), ' was ', datetime.timedelta(0, 2159, 998410), ' ago.')
('Sum of both gaps is ', datetime.timedelta(0, 23340, 882))
('Tide is Currently: ', 'falling')
('tide difference = ', -3.8)
('lower tide value', 4.299999999999999)
('Normalised Time =', 2159, 23340, 0.29060405051843885)
0.958070971113
('Current tide : ', 7.940669690228617)
Code:
#version 1.0
#This program pulls tide data from the ports of Jersey Website
#Under a licence from the UKHO
#
#It then calculates the current tide using a simplified sinusoidal harmonic approximation
#By finding the two tide data points either side of now and working out the current tide height
import urllib2
from bs4 import BeautifulSoup
from time import sleep
import datetime as dt
import math
#open site and grab html
rawhtml = urllib2.urlopen("http://www.ports.je/Pages/tides.aspx").read(40000)
soup = BeautifulSoup(rawhtml, "html.parser")
#get the tide data (it's all in tags)
rawtidedata = soup.findAll('td')
#parse all data points (date, times, heights) to one big list
#format of the list is [day,tm,ht,tm,ht,tm,lt,tm,lt]
n=0
parsedtidedata=[]
for i in rawtidedata:
parsedtidedata.append(rawtidedata[n].get_text())
n += 1
#extract each class of data (day, time , height) to a separate list (there are 10 data items for each day)
tidetimes=[]
tideheights=[]
tideday=[]
lastdayofmonth=int(parsedtidedata[-10])
for n in range(0,lastdayofmonth*10,10):
tideday.append(parsedtidedata[n])
tidetimes.extend([parsedtidedata[n+1],parsedtidedata[n+3],parsedtidedata[n+5],parsedtidedata[n+7]])
tideheights.extend([parsedtidedata[n+2],parsedtidedata[n+4],parsedtidedata[n+6],parsedtidedata[n+8]])
#get time now:
currentTime = dt.datetime.now()
#create a list of all the tide times as datetime objects:
dtTideTimes=[]
tideDataList=[]
for j in range (0,lastdayofmonth*4):
#print tidetimes[j][0:2], tidetimes[j][3:6]
if tidetimes[j]=='**':
dtTideTimes.append('**')
else:
dtTideTimes.append(dt.datetime.now().replace(day=int(j/4+1), hour=int(tidetimes[j][0:2]), minute=int(tidetimes[j][3:5])))
#make a tuple for each data point and add it to a list
tupleHolder =(dtTideTimes[j], tideheights[j])
tideDataList.append(tupleHolder)
#print what we've got so far
# print tideDataList[j]
#find the two closest times in the list to now:
gap1 = abs(tideDataList[0][0] - currentTime)
gap2 = abs(tideDataList[0][0] - currentTime)
nearest1 = tideDataList[0]
#print gap1
for j in range (0,lastdayofmonth*4):
if (tideDataList[j][0] !="**"):
gapx = abs(tideDataList[j][0] - currentTime)
#check if the data point is the first or second nearest to now.
#Generates the datapoints either side of now
if (gapx <= gap1):
nearest1 = tideDataList[j]
gap1 = gapx
if (gap1 < gapx and gapx <= gap2):
nearest2 = tideDataList[j]
gap2 = gapx
#print (nearest1, gap1)
#print (nearest2, gap2)
#print (gap1+gap2)
#and now the maths begins
#print ('tide height 1 = ', nearest1[1])
#print ('tide height 2 = ', nearest2[1])
#need to get them in order of time: (this works)
if nearest1[0] > nearest2[0]:
nextDataPoint = nearest1
prevDataPoint = nearest2
gapToNext = gap1
gapToPrev = gap2
else:
nextDataPoint = nearest2
prevDataPoint = nearest1
gapToNext = gap2
gapToPrev = gap1
gapSum = gapToNext + gapToPrev
print('Next: ', nextDataPoint,' is ',gapToNext, ' away. /n Previous: ', prevDataPoint, ' was ', gapToPrev, ' ago.')
print('Sum of both gaps is ', gapSum) #this works
#is the tide rising or falling?
tideDifference = float(nextDataPoint[1])-float(prevDataPoint[1])
if (tideDifference<0 data-blogger-escaped-0="prev" data-blogger-escaped-:="" data-blogger-escaped-all="" data-blogger-escaped-code="" data-blogger-escaped-currently:="" data-blogger-escaped-currenttide="" data-blogger-escaped-data="" data-blogger-escaped-difference=", tideDifference) #this works
lowerTide = (float(nearest1[1]) + float(nearest2[1]) - abs(tideDifference))/2
print (" data-blogger-escaped-doesn="" data-blogger-escaped-else:="" data-blogger-escaped-falling="" data-blogger-escaped-for="" data-blogger-escaped-ide="" data-blogger-escaped-is="" data-blogger-escaped-lower="" data-blogger-escaped-lowertide="" data-blogger-escaped-math.cos="" data-blogger-escaped-math.pi="" data-blogger-escaped-normalisedtime="" data-blogger-escaped-ormalised="" data-blogger-escaped-pi="next" data-blogger-escaped-print="" data-blogger-escaped-scaled="" data-blogger-escaped-t="" data-blogger-escaped-this="" data-blogger-escaped-tide="" data-blogger-escaped-tidedifference="" data-blogger-escaped-tidestate="" data-blogger-escaped-time=", gapToPrev.seconds, gapSum.seconds, normalisedTime)
print (math.cos(normalisedTime))
if tideState == " data-blogger-escaped-to="" data-blogger-escaped-urrent="" data-blogger-escaped-value="" data-blogger-escaped-work="" data-blogger-escaped-works="">
No comments:
Post a Comment