Page MenuHomec4science

polar2.py
No OneTemporary

File Metadata

Created
Sun, Oct 6, 20:24

polar2.py

#!/usr/bin/env python3
import math
from scipy.interpolate import interp1d
import numpy as np
class Polar :
def __init__(self, filename) :
f = open(filename, 'r')
self.dict = {}
self.angles = []
for row in f:
l = row.split(';')
key = l[0]
value = l[1:]
self.dict[key] = value
if key.isnumeric() :
self.angles.append(key)
f.close()
def _getI(self, tws) :
i = 0
prev_speed = 0
for speed in self.dict['TWA\\TWS'] :
if tws < float(speed) :
return i-1
prev_speed = speed
i += 1
return -1
def _getAngles(self, twa) :
twa = int(twa/5) * 5 # reduce twa to a 5-multiple
if twa > 180 :
twa = twa - 2*(twa - 180)
prev_angle = 0
for a in self.angles :
if twa <= float(a) :
a = str(a)
prev_angle = str(prev_angle)
return prev_angle,a,self.dict[prev_angle],self.dict[a]
prev_angle = a
print ('*** ERROR angle {0} not found'.format(twa))
exit(1)
def boatSpeed(self, twa, tws) :
i = self._getI(tws)
twa = math.fabs(twa)
twa = int(twa)
if twa > 180 :
twa = twa - 2*(twa - 180)
a1,a2,inf,sup = self._getAngles(twa)
x = np.array([float(a1),float(a2)])
y = np.array([float(inf[i]), float(sup[i])])
f = interp1d(x, y)
try :
speed = f(twa)
except :
speed = 0
speed = float(speed)
return speed
#--------------------
# Code for testing classes
#--------------------
if __name__ == '__main__':
#p = Polar('../polar/figaro2.pol')
#print ('Boat speed : {0}'.format(p.boatSpeed(45.2, 20.2)))
p = Polar('../polar/so36i.pol')
twa = 150
tws = 19
print ('boatSpeed({0},{1})'.format(twa,tws))
print ('Boat speed : {0:.2f}'.format(p.boatSpeed(twa, tws)))

Event Timeline