Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F86495131
polar2.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Oct 6, 20:24
Size
1 KB
Mime Type
text/x-python
Expires
Tue, Oct 8, 20:24 (2 d)
Engine
blob
Format
Raw Data
Handle
21430444
Attached To
R890 mux0183
polar2.py
View Options
#!/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
Log In to Comment