Esempio n. 1
0
def _prep_plot_data(grids_15m, tidal_predictions, weather_path):
    max_ssh, max_ssh_time, risk_levels = {}, {}, {}
    u_wind_4h_avg, v_wind_4h_avg, max_wind_avg = {}, {}, {}
    for name in places.TIDE_GAUGE_SITES:
        ssh_ts = nc_tools.ssh_timeseries_at_point(grids_15m[name], 0, 0, datetimes=True)
        ttide = shared.get_tides(name, tidal_predictions)
        max_ssh[name], max_ssh_time[name] = shared.find_ssh_max(name, ssh_ts, ttide)
        risk_levels[name] = stormtools.storm_surge_risk_level(
            name, max_ssh[name], ttide
        )
        wind_avg = wind_tools.calc_wind_avg_at_point(
            arrow.get(max_ssh_time[name]),
            weather_path,
            places.PLACES[name]["wind grid ji"],
            avg_hrs=-4,
        )
        u_wind_4h_avg[name], v_wind_4h_avg[name] = wind_avg
        max_wind_avg[name], _ = wind_tools.wind_speed_dir(
            u_wind_4h_avg[name], v_wind_4h_avg[name]
        )
    plot_data = namedtuple(
        "PlotData",
        "ssh_ts, max_ssh, max_ssh_time, risk_levels, "
        "u_wind_4h_avg, v_wind_4h_avg, max_wind_avg",
    )
    return plot_data(
        ssh_ts,
        max_ssh,
        max_ssh_time,
        risk_levels,
        u_wind_4h_avg,
        v_wind_4h_avg,
        max_wind_avg,
    )
def _prep_plot_data(
    place,
    grid_T_hr,
    grids_15m,
    bathy,
    timezone,
    weather_path,
    tidal_predictions,
):
    ssh_hr = grid_T_hr.variables['sossheig']
    time_ssh_hr = nc_tools.timestamp(
        grid_T_hr, range(grid_T_hr.variables['time_counter'].size))
    try:
        j, i = places.PLACES[place]['NEMO grid ji']
    except KeyError as e:
        raise KeyError(f'place name or info key not found in '
                       f'salishsea_tools.places.PLACES: {e}')
    itime_max_ssh = np.argmax(ssh_hr[:, j, i])
    time_max_ssh_hr = time_ssh_hr[itime_max_ssh]
    ssh_15m_ts = nc_tools.ssh_timeseries_at_point(grids_15m[place],
                                                  0,
                                                  0,
                                                  datetimes=True)
    ttide = shared.get_tides(place, tidal_predictions)
    ssh_corr = shared.correct_model_ssh(ssh_15m_ts.ssh, ssh_15m_ts.time, ttide)
    max_ssh_15m, time_max_ssh_15m = shared.find_ssh_max(
        place, ssh_15m_ts, ttide)
    tides_15m = shared.interp_to_model_time(ssh_15m_ts.time, ttide.pred_all,
                                            ttide.time)
    residual = ssh_corr - tides_15m
    max_ssh_residual = residual[ssh_15m_ts.time == time_max_ssh_15m][0]
    wind_4h_avg = wind_tools.calc_wind_avg_at_point(
        arrow.get(time_max_ssh_15m),
        weather_path,
        places.PLACES[place]['wind grid ji'],
        avg_hrs=-4)
    wind_4h_avg = wind_tools.wind_speed_dir(*wind_4h_avg)
    plot_data = namedtuple(
        'PlotData', 'ssh_max_field, time_max_ssh_hr, ssh_15m_ts, ssh_corr, '
        'max_ssh_15m, time_max_ssh_15m, residual, max_ssh_residual, '
        'wind_4h_avg, '
        'ttide, bathy')
    return plot_data(
        ssh_max_field=ssh_hr[itime_max_ssh],
        time_max_ssh_hr=time_max_ssh_hr.to(timezone),
        ssh_15m_ts=ssh_15m_ts,
        ssh_corr=ssh_corr,
        max_ssh_15m=max_ssh_15m - places.PLACES[place]['mean sea lvl'],
        time_max_ssh_15m=arrow.get(time_max_ssh_15m).to(timezone),
        residual=residual,
        max_ssh_residual=max_ssh_residual,
        wind_4h_avg=wind_4h_avg,
        ttide=ttide,
        bathy=bathy,
    )
Esempio n. 3
0
def _calc_max_ssh(feed, ttide, run_date, run_type, config):
    results_path = config['results archive'][run_type]
    tide_gauge_stn = (
        config['storm surge feeds']['feeds'][feed]['tide gauge stn'])
    grid_T_15m = nc.Dataset(
        os.path.join(results_path,
                     run_date.format('DDMMMYY').lower(),
                     f'{tide_gauge_stn.replace(" ", "")}.nc'))
    ssh_ts = nc_tools.ssh_timeseries_at_point(grid_T_15m, 0, 0, datetimes=True)
    ssh_corr = nowcast.figures.shared.correct_model_ssh(
        ssh_ts.ssh, ssh_ts.time, ttide)
    max_ssh = np.max(ssh_corr) + PLACES[tide_gauge_stn]['mean sea lvl']
    max_ssh_time = ssh_ts.time[np.argmax(ssh_corr)]
    return max_ssh, max_ssh_time
Esempio n. 4
0
def test_ssh_timeseries_at_point_time_counter_type(
    datetimes,
    expected,
    nc_dataset,
):
    """Sea surface height timeseries time counter values have expected type
    """
    nc_dataset.createDimension('time_counter')
    nc_dataset.createDimension('y', 1)
    nc_dataset.createDimension('x', 1)
    ssh = nc_dataset.createVariable('sossheig', float,
                                    ('time_counter', 'y', 'x'))
    ssh[:] = np.array([5.0, 5.3])
    time_counter = nc_dataset.createVariable('time_counter', float,
                                             ('time_counter', ))
    time_counter.time_origin = '2002-OCT-26 00:00:00'
    time_counter[:] = np.array([0.5, 1.5]) * 60 * 60
    ssh_ts = nc_tools.ssh_timeseries_at_point(nc_dataset, 0, 0, datetimes)
    np.testing.assert_array_equal(ssh_ts.ssh, np.array([5.0, 5.3]))
    assert isinstance(ssh_ts.time[0], expected)
Esempio n. 5
0
def _calc_max_ssh(feed, ttide, run_date, run_type, config):
    results_path = config["results archive"][run_type]
    tide_gauge_stn = config["storm surge feeds"]["feeds"][feed]["tide gauge stn"]
    grid_T_15m = nc.Dataset(
        os.path.join(
            results_path,
            run_date.format("DDMMMYY").lower(),
            f'{tide_gauge_stn.replace(" ", "")}.nc',
        )
    )
    ssh_ts = nc_tools.ssh_timeseries_at_point(grid_T_15m, 0, 0, datetimes=True)
    max_ssh, max_ssh_time = nowcast.figures.shared.find_ssh_max(
        tide_gauge_stn, ssh_ts, ttide
    )
    if np.isnan(max_ssh):
        logger.critical(
            f"no {tide_gauge_stn} feed generated: max sea surface height is "
            f"NaN at {max_ssh_time}"
        )
        raise WorkerError
    return max_ssh, max_ssh_time