def test_WaitingTime_get_weather_windows_end_dt(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    result = test.get_weather_windows(olc)

    assert result['end_dt'][0] == dt.datetime(2000, 1, 1, 18, 0)
def test_WaitingTime_get_weather_windows_no_olc(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {}

    result = test.get_weather_windows(olc)

    assert len(result['duration']) == 1
    assert np.isclose(result['duration'][0], 35064)
def test_WaitingTime_get_start_delay_negative(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date_met = dt.datetime(2000, 1, 3)

    with pytest.raises(RuntimeError):
        test._get_start_delay(windows, start_date_met, 0)
def test_WaitingTime_get_weather_windows_cum_gap(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    result = test.get_weather_windows(olc)
    cum_gap = result['cum_gap']

    assert cum_gap[0] == 0  # No gap to first window
    assert cum_gap[1] == 732  # 30 days and 12 hours till next
def test_WaitingTime_get_weather_windows_basic(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    result = test.get_weather_windows(olc)

    assert len(result['duration']) > 0
    assert (len(result['duration']) == len(result['start_dt']) == len(
        result['end_dt']))
def test_WaitingTime_get_whole_windows(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date_met = dt.datetime(2000, 1, 1)

    ind_ww_all = test._get_whole_windows(windows, start_date_met, 20)

    assert ind_ww_all[0] == 1
def test_WaitingTime_combined_window_strategy_short(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)

    _, waiting_time = test._combined_window_strategy(windows, start_date, 24)

    assert waiting_time == 0
def test_WaitingTime_get_start_delay(metocean_synth, test_input, expected):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date_met = dt.datetime(2000, 1, 1)

    start_delay = test._get_start_delay(windows, start_date_met, test_input)

    assert start_delay == expected
def test_WaitingTime_whole_window_strategy_long(metocean):

    test = WaitingTime(metocean)

    olc = {'maxHs': 4, 'maxTp': 15, 'maxWs': 15}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)

    start_delay, waiting_time = test._whole_window_strategy(
        windows, start_date, 5000)
    assert waiting_time is None
    assert start_delay is None
def test_WaitingTime_combined_window_strategy_max_start_delay(metocean_synth):

    test = WaitingTime(metocean_synth, max_start_delay=4)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)
    start_delay, waiting_time = test._combined_window_strategy(
        windows, start_date, 24)

    assert start_delay is None
    assert waiting_time is None
def test_WaitingTime_combined_window_strategy_single(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)
    start_delay, waiting_time = test._combined_window_strategy(
        windows, start_date, 24)

    assert np.isclose(start_delay, 750.)
    assert np.isclose(waiting_time, 0)
def test_WaitingTime_combined_window_strategy_multi(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)
    start_delay, waiting_time = test._combined_window_strategy(
        windows, start_date, 36)

    assert np.isclose(start_delay, 750.)  # Less waiting with 24 hour window
    assert np.isclose(waiting_time, 656.0)  # (27 + 27 + 28) * 24 / 3
def test_WaitingTime_whole_window_strategy(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)

    start_delay, waiting_time = test._whole_window_strategy(
        windows, start_date, 20)
    assert waiting_time is None
    assert np.isclose(start_delay, 750)  # 31 * 24 + 6
def test_WaitingTime_combined_window_strategy_leap(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 2, 29)

    start_delay, waiting_time = test._combined_window_strategy(
        windows, start_date, 12)

    assert np.isclose(start_delay, 14.0)  # (30 + 6 + 6) / 3
    assert waiting_time == 0
def test_WaitingTime_combined_window_strategy_max_start_none(
        metocean_synth_long):

    test = WaitingTime(metocean_synth_long, max_start_delay=None)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)

    start_delay, waiting_time = test._combined_window_strategy(
        windows, start_date, 12)

    assert np.isclose(start_delay, 17534)
    assert waiting_time == 0.
def test_WaitingTime_call_no_windows(mocker, metocean):

    test = WaitingTime(metocean)

    log_phase = mocker.Mock()
    log_phase.description = "Mocked phase"

    # Use very small OLC conditions for which no windows are in metocean
    journey = {
        'prep_dur': [48, 48],
        'prep_id': [u'Mobilisation', u'Vessel preparation & loading'],
        'sea_dur': [35.52257567817756, 6.0, 2, 4, 0.0, 35.52257567817756],
        'sea_id': [
            u'Transportation from port to site', u'Vessel Positioning',
            u'Access to the element', u'Inspection or Maintenance Operations',
            u'Transportation from site to site',
            u'Transportation from site to port'
        ],
        'sea_olc': [[0.01, 0.0, 0.0, 0.0], [2.5, 0, 0, 0], [4, 6, 15, 2],
                    [4, 6, 15, 2], [2.5, 0.0, 0.0, 0.0], [2.5, 0.0, 0.0, 0.0]],
        'wait_dur': []
    }

    sched_sol = {"journey": {0: journey}}
    start_date = dt.datetime(2000, 1, 1)

    journey, exit_flag = test(log_phase, sched_sol, start_date)

    assert exit_flag == 'NoWWindows'
    assert not journey
def test_WaitingTime_call_twice(mocker, metocean):

    test = WaitingTime(metocean)

    log_phase = mocker.Mock()
    log_phase.description = "Mocked phase"

    journey = {
        'prep_dur': [48, 48],
        'prep_id': [u'Mobilisation', u'Vessel preparation & loading'],
        'sea_dur': [35.52257567817756, 6.0, 2, 4, 0.0, 35.52257567817756],
        'sea_id': [
            u'Transportation from port to site', u'Vessel Positioning',
            u'Access to the element', u'Inspection or Maintenance Operations',
            u'Transportation from site to site',
            u'Transportation from site to port'
        ],
        'sea_olc': [[2.5, 0.0, 0.0, 0.0], [2.5, 0, 0, 0], [4, 6, 15, 2],
                    [4, 6, 15, 2], [2.5, 0.0, 0.0, 0.0], [2.5, 0.0, 0.0, 0.0]],
        'wait_dur': []
    }

    sched_sol = {"journey": {0: journey}}
    start_date = dt.datetime(2000, 1, 1)

    journey, exit_flag = test(log_phase, sched_sol, start_date)

    assert exit_flag == "WeatherWindowsFound"
    assert 'start_delay' in journey

    journey, exit_flag = test(log_phase, sched_sol, start_date)

    assert exit_flag == "WeatherWindowsFound"
    assert 'start_delay' in journey
def test_WaitingTime_init_years_fail_missing(metocean):

    metocean_copy = metocean.copy()
    metocean_copy = metocean_copy[metocean_copy["year [-]"] == 1992]

    with pytest.raises(ValueError) as excinfo:
        WaitingTime(metocean_copy)

    assert "No complete years" in str(excinfo.value)
def test_WaitingTime_init_years_fail_monotonic(metocean):

    metocean_copy = metocean.copy()
    metocean_copy = metocean_copy[metocean_copy["year [-]"].isin([1993, 1995])]

    with pytest.raises(ValueError) as excinfo:
        WaitingTime(metocean_copy)

    assert "not monotonic" in str(excinfo.value)
def test_WaitingTime_init_years_trim(metocean):

    init_years = len(metocean["year [-]"].unique())
    test = WaitingTime(metocean)

    years = iter(test.metocean["year [-]"].unique())
    first = next(years)

    assert all(a == b for a, b in enumerate(years, first + 1))
    assert len(test.metocean["year [-]"].unique()) == init_years - 1
def test_WaitingTime_init_years_extra(metocean):

    metocean_copy = metocean.copy()
    metocean_copy = metocean_copy[metocean_copy["year [-]"].isin([1995, 1996])]

    test = WaitingTime(metocean_copy)

    years = iter(test.metocean["year [-]"].unique())
    first = next(years)

    assert all(a == b for a, b in enumerate(years, first + 1))
    assert len(test.metocean["year [-]"].unique()) == 3
def test_WaitingTime_get_combined_windows(metocean_synth):

    test = WaitingTime(metocean_synth)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date_met = dt.datetime(2000, 1, 1)

    trimmed_windows = trim_weather_windows(windows, start_date_met)

    all_cum_durations = np.array(trimmed_windows['cum_duration'])
    all_cum_gaps = np.array(trimmed_windows['cum_gap'])

    delays, wait_times = test._get_combined_windows(all_cum_durations,
                                                    all_cum_gaps, 36)

    assert all(x < y for x, y in zip(delays, delays[1:]))
    assert all(x >= 0 for x in wait_times)

    assert delays[0] == 0
    assert delays[1] == 744
def test_WaitingTime_combined_window_strategy_optimise(metocean_synth):

    test = WaitingTime(metocean_synth)
    test.set_optimise_delay(True)

    olc = {'maxHs': 0.5, 'maxTp': 0.5, 'maxWs': 0.5, 'maxCs': 0.5}

    windows = test.get_weather_windows(olc)
    start_date = dt.datetime(2000, 1, 1)

    start_delay, waiting_time = test._combined_window_strategy(
        windows, start_date, 24)

    assert np.isclose(start_delay, 6.)
    assert np.isclose(waiting_time, 732.0)  # 30 days, 12 hours
def test_WaitingTime_init(metocean):
    result = WaitingTime(metocean)
    assert isinstance(result, WaitingTime)
def test_WaitingTime_init_years_fail_tstep(metocean_synth_non_matching_tstep):

    with pytest.raises(ValueError) as excinfo:
        WaitingTime(metocean_synth_non_matching_tstep)

    assert "not equal" in str(excinfo.value)
def test_WaitingTime_set_optimise_delay(metocean):

    test = WaitingTime(metocean)
    test.set_optimise_delay(True)

    assert test._optimise_delay
def test_WaitingTime_time_step_hours(metocean):
    result = WaitingTime(metocean)
    assert np.isclose(result._time_step_hours, 3)