Saturday, 2 January 2016

Tide Indicator Pi Project #7 - Finding the two tide data points nearest to the current time.

This project is taking ages! I've done a lot since the last post however, but documented very little, so I'll do my best to recall how I got from there to here. You can see all the posts so far here.

The problem in a nutshell: The program needs to get the two tide data points either side of the current time, to work out what the tide is doing now.

Since the last post, the code has been modified to create a list of tuples, with each tuple having two data points (tide time, tide height)

It then works out the gap between each data point and the current time, and tries to store the two nearest times as 'nearest1' and 'nearest2'. Sometime it works:

Time Now:
2015-01-02 16:33

(datetime.datetime(2016, 1, 2, 17, 52, 40, 854958), u'4.0'),
(datetime.datetime(2016, 1, 2, 11, 9, 40, 854071), u'8.4')

Sometimes it doesn't and misses a point.

import urllib2
from bs4 import BeautifulSoup
from time import sleep
import datetime as dt

#open site and grab html

rawhtml = urllib2.urlopen("").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]

for i in rawtidedata: 
 n += 1

#extract each class of data (day, time , height) to a separate list (there are 10 data items for each day):


for n in range(0,lastdayofmonth*10,10):


#get time now:

currentTime =

#create a list of all the tide times as datetime objects:


for j in range (0,lastdayofmonth*4):
 #print tidetimes[j][0:2], tidetimes[j][3:6]
 if tidetimes[j]=='**':
  dtTideTimes.append(, hour=int(tidetimes[j][0:2]), minute=int(tidetimes[j][3:5])))

#create a tuple of time and height, and add each tuple to a list

 tupleHolder =(dtTideTimes[j], tideheights[j])

#print what we've got so far

for j in range (0,lastdayofmonth*4):
 print tideDataList[j]

#find the two closest data points to now in the list:

gap1 = abs(tideDataList[0][0] - currentTime)
nearest1 = tideDataList[0]
print gap1 

for j in range (0,lastdayofmonth*4):

 if (tideDataList[j][0] !="**"):

  gap2 = abs(tideDataList[j][0] - currentTime)
  print tideDataList[j][0], gap2, nearest1

  if (gap2 < gap1):

   nearest2 = nearest1
   nearest1 = tideDataList[j]
   gap1 = gap2

print (nearest1, nearest2)
#this nearly works!!! Gave the two nearest high tides, not nearest high and low.

No comments:

Post a Comment