Source code for sardine.vessel.vessel_history

from collections import deque
from math import ceil

from scripts.utils.point import Point


[docs] class VesselHistory: def __init__(self): self._history = deque()
[docs] def add_event(self, event): ''' Apends the given event to _history ''' self._history.append(event)
[docs] def history(self): ''' Returns the _history as a list ''' return list(self._history)
[docs] def history_without_waits(self): ''' Returns the list of events (history) omiting the waits ''' history = [event for event in self._history if not event.is_wait()] return history
[docs] def position_at(self, time): ''' Returns the position of the vessel in the specified time: It loops through the events in the history and checks if the time falls inside the event time range, if it does, it returns the position of that event. If there are no events in the history, it returns (0,0) If the time is after the end of the history, it returns the position of the final event ''' if not self._history: return Point(0, 0) i = 0 while True: try: event = self._history[i] if event.start <= time < event.end: position = event.position_at(time) return position except IndexError: end_pos = self._history[-1].position_at(self._history[-1].duration) return end_pos i += 1
[docs] def split(self, intervals_count, final_time): ''' Defines a list of intervals with step = ceil(final_time / intervals_count) For each interval, it loops through the events and clips them in the interval, if the portion exists (event overlaps with the interval), it's added to a list 'history per intervals' Returns 'history per intervals' ''' step = ceil(final_time / intervals_count) intervals = [(i * step, (i+1)*step) for i in range(intervals_count)] history_per_intervals = [] for interval in intervals: history_for_interval = [] for event in self._history: clipped = event.clip(interval[0], interval[1]) if clipped: history_for_interval.append(clipped) history_per_intervals.append(history_for_interval) return history_per_intervals
[docs] def split_per_day(self, final_time): ''' Sets an interval of one day Loops through the events and clips them in the interval, if the portion exists (event overlaps with the interval), it's added to a list 'history per intervals' Returns 'history per intervals' ''' # final_time in seconds history_per_intervals = [] interval_start = 0 one_day = 24*60*60 while interval_start < final_time: history_for_interval = [] for event in self._history: clipped = event.clip(interval_start, interval_start + one_day) if clipped: history_for_interval.append(clipped) history_per_intervals.append(history_for_interval) interval_start += one_day return history_per_intervals