Skip to content

pycoastkit.hasaki

Hasaki coast, Japan: offshore waves (NOWPHAS Kashima) and beach profiles (HORS).

Waves The Japanese wave network NOWPHAS (MLIT / PARI) publishes 2-hourly wave statistics for the Kashima station (code 207), the wave record used for the Hasaki beach. :func:download_nowphas fetches yearly files, :func:read_nowphas parses them and :func:qc_nowphas cleans them.

Terms: free to reuse. Cite "Nationwide Ocean Wave information network for
Ports and HArbourS (NOWPHAS), MLIT Japan" with the page URL, state any
processing, and for papers write that the data were observed by the MLIT
Ports and Harbours Bureau and processed by PARI. NOWPHAS asks users not to
harvest data automatically on a schedule - download once and keep the files.

Beach profiles The Hasaki Oceanographical Research Station (HORS, PARI) has surveyed a cross-shore profile along its pier from -115 m to +385 m every 5 m, on weekdays from 1986 to March 2011 and weekly since (Banno et al. 2020, J. Mar. Sci. Eng. 8, 871). Request or download the data from PARI (:data:HORS_DATA_PAGE) and read it with :func:read_hors_profiles. Elevations refer to the Hasaki Port datum (MHWS 1.25 m, MSL 0.65 m).

download_nowphas

download_nowphas(years, folder, station=KASHIMA_STATION, interval='2h', pause_s=3.0, overwrite=False, skip_missing=True)

Download yearly NOWPHAS wave files into folder (one request per year).

Parameters:

Name Type Description Default
years int or iterable of int

Years to fetch (Kashima 2-hourly: 1972-1982, 1984-2008, 2010-2024).

required
folder path

Where the text files are saved. Years already present are skipped, so the call can be repeated safely.

required
station str

NOWPHAS station code ("207" = Kashima, offshore of Hasaki).

KASHIMA_STATION
interval ('2h', '20min')

2-hourly statistics (long record) or 20-minute statistics (2005+).

"2h"
pause_s float

Pause between requests, to be gentle with the server.

3.0
skip_missing bool

Skip years without data (with a message) instead of raising an error.

True

Returns:

Type Description
list of Path

The data files, ready for :func:read_nowphas.

Notes

This uses the same request as the download form on the NOWPHAS website. If the website changes, download the yearly zip files by hand from :data:NOWPHAS_PAGE and pass them to :func:read_nowphas directly.

Source code in src/pycoastkit/hasaki.py
def download_nowphas(years, folder, station: str = KASHIMA_STATION, interval: str = "2h",
                     pause_s: float = 3.0, overwrite: bool = False,
                     skip_missing: bool = True) -> list[Path]:
    """Download yearly NOWPHAS wave files into ``folder`` (one request per year).

    Parameters
    ----------
    years : int or iterable of int
        Years to fetch (Kashima 2-hourly: 1972-1982, 1984-2008, 2010-2024).
    folder : path
        Where the text files are saved. Years already present are skipped,
        so the call can be repeated safely.
    station : str
        NOWPHAS station code (``"207"`` = Kashima, offshore of Hasaki).
    interval : {"2h", "20min"}
        2-hourly statistics (long record) or 20-minute statistics (2005+).
    pause_s : float
        Pause between requests, to be gentle with the server.
    skip_missing : bool
        Skip years without data (with a message) instead of raising an error.

    Returns
    -------
    list of Path
        The data files, ready for :func:`read_nowphas`.

    Notes
    -----
    This uses the same request as the download form on the NOWPHAS website.
    If the website changes, download the yearly zip files by hand from
    :data:`NOWPHAS_PAGE` and pass them to :func:`read_nowphas` directly.
    """
    if interval not in ("2h", "20min"):
        raise ValueError("interval must be '2h' or '20min'")
    folder = Path(folder)
    folder.mkdir(parents=True, exist_ok=True)
    years = [int(years)] if np.isscalar(years) else [int(y) for y in years]
    kind = "2hr" if interval == "2h" else "20min"
    letter = "0" if interval == "2h" else "e"
    saved = []

    for number, year in enumerate(years):
        name = f"h{station}{letter}.{year % 100:03d}.txt"  # e.g. h2070.095.txt for 1995
        target = folder / name
        if target.exists() and not overwrite:
            saved.append(target)
            continue
        if number:
            time.sleep(pause_s)
        payload = urllib.parse.urlencode({
            "start_yy": year,
            "end_yy": year,
            "checkedvtxt": f"{station}_{kind}_txt,",
            "code_list": "undefined",
            "code_map": "undefined",
        }).encode()
        request = urllib.request.Request(
            _NOWPHAS_DOWNLOAD, data=payload,
            headers={"User-Agent": "pycoastkit (Python package)"},
        )
        with urllib.request.urlopen(request, timeout=120) as response:
            content = response.read()
        member = None
        if zipfile.is_zipfile(io.BytesIO(content)):
            archive = zipfile.ZipFile(io.BytesIO(content))
            member = next((m for m in archive.namelist()
                           if Path(m).name.lower() == name.lower()), None)
        if member is None:
            message = (f"no {interval} data for station {station} in {year} "
                       f"(see {NOWPHAS_PAGE} for available years)")
            if not skip_missing:
                raise RuntimeError(message)
            print(f"skipped {year}: {message}")
            continue
        target.write_bytes(archive.read(member))
        saved.append(target)
        print(f"downloaded {name}")
    return saved

read_nowphas

read_nowphas(sources)

Read NOWPHAS wave-statistics files (.txt, the downloaded .zip, or a folder).

Returns the raw values (including missing-value codes such as 99.99) with a time index and the columns flag, n_waves, h_mean, t_mean, hs, ts, h_1_10, t_1_10, hmax, tmax, direction:

  • h_mean/t_mean - mean wave height [m] / period [s]
  • hs/ts - significant wave (H1/3, T1/3)
  • h_1_10/t_1_10 - highest one-tenth waves
  • hmax/tmax - highest wave
  • direction - degrees clockwise from true north (waves coming from)

Pass the result to :func:qc_nowphas before analysis.

Source code in src/pycoastkit/hasaki.py
def read_nowphas(sources) -> pd.DataFrame:
    """Read NOWPHAS wave-statistics files (``.txt``, the downloaded ``.zip``, or a folder).

    Returns the raw values (including missing-value codes such as 99.99) with
    a ``time`` index and the columns ``flag, n_waves, h_mean, t_mean, hs, ts,
    h_1_10, t_1_10, hmax, tmax, direction``:

    * ``h_mean``/``t_mean`` - mean wave height [m] / period [s]
    * ``hs``/``ts`` - significant wave (H1/3, T1/3)
    * ``h_1_10``/``t_1_10`` - highest one-tenth waves
    * ``hmax``/``tmax`` - highest wave
    * ``direction`` - degrees clockwise from true north (waves coming from)

    Pass the result to :func:`qc_nowphas` before analysis.
    """
    if isinstance(sources, (str, Path)):
        sources = [sources]
    frames = []
    for source in sources:
        for name, text in _read_lines(source):
            lines = [line for line in text.splitlines()[1:] if line.strip()]
            if not lines:
                continue
            twenty_min = name.lower()[4] == "e"
            widths = _WIDTHS_20MIN if twenty_min else _WIDTHS_2H
            time_cols = ["year", "month", "day", "hour"] + (["minute"] if twenty_min else [])
            table = pd.read_fwf(io.StringIO("\n".join(lines)), widths=widths, header=None,
                                names=time_cols + _WAVE_COLUMNS)
            table.index = pd.to_datetime(table[time_cols])
            frames.append(table.drop(columns=time_cols))
    if not frames:
        raise FileNotFoundError("no NOWPHAS wave files found")
    data = pd.concat(frames).sort_index()
    data = data[~data.index.duplicated(keep="last")]
    data.index.name = "time"
    return data

qc_nowphas

qc_nowphas(raw, max_hs=25.0, calm_hs=0.25)

Clean NOWPHAS wave statistics.

Steps
  1. Missing codes (9999, 99.99, 999.9) and the special codes for calm (66.66, 666.6, flag 66) and "not a high wave" (77.77, 777.7) -> NaN.
  2. Records flagged missing (flag 9999) -> all values NaN.
  3. Unrealistic hs > max_hs -> NaN for that record's heights/periods.
  4. Direction code 6666 (calm, used until 1999) -> NaN; from 2000 on, NOWPHAS lists a direction even in calm seas, so direction is also set to NaN when hs <= calm_hs. A boolean calm column records both.

Returns:

Name Type Description
clean DataFrame

Same columns as the input plus calm; values are floats.

report DataFrame

Number of values changed per step and column.

Source code in src/pycoastkit/hasaki.py
def qc_nowphas(raw: pd.DataFrame, max_hs: float = 25.0, calm_hs: float = 0.25):
    """Clean NOWPHAS wave statistics.

    Steps
    -----
    1. Missing codes (9999, 99.99, 999.9) and the special codes for calm
       (66.66, 666.6, flag 66) and "not a high wave" (77.77, 777.7) -> NaN.
    2. Records flagged missing (flag 9999) -> all values NaN.
    3. Unrealistic ``hs > max_hs`` -> NaN for that record's heights/periods.
    4. Direction code 6666 (calm, used until 1999) -> NaN; from 2000 on,
       NOWPHAS lists a direction even in calm seas, so direction is also set
       to NaN when ``hs <= calm_hs``. A boolean ``calm`` column records both.

    Returns
    -------
    clean : pandas.DataFrame
        Same columns as the input plus ``calm``; values are floats.
    report : pandas.DataFrame
        Number of values changed per step and column.
    """
    data = raw.copy()
    flag = data["flag"]
    values = data.drop(columns=["flag"]).astype(float)
    heights = ["h_mean", "hs", "h_1_10", "hmax"]
    periods = ["t_mean", "ts", "t_1_10", "tmax"]
    report = {}

    def count(name, mask):
        report[name] = int(np.asarray(mask).sum())

    codes = pd.DataFrame(False, index=values.index, columns=values.columns)
    codes[heights] = values[heights].isin([99.99, 66.66, 77.77])
    codes[periods] = values[periods].isin([999.9, 666.6, 777.7])
    codes[["n_waves", "direction"]] = values[["n_waves", "direction"]].eq(9999)
    count("missing or special code", codes)
    values = values.mask(codes)

    missing_flag = flag.eq(9999)
    count("flag 9999 (whole record)", values[missing_flag].notna())
    values.loc[missing_flag] = np.nan

    too_high = values["hs"] > max_hs
    count(f"hs > {max_hs} m", values.loc[too_high, heights + periods].notna())
    values.loc[too_high, heights + periods] = np.nan

    calm_code = values["direction"].eq(6666)
    calm = calm_code | flag.eq(66) | (values["hs"] <= calm_hs)
    count("direction set to NaN (calm)", calm & values["direction"].notna())
    values.loc[calm, "direction"] = np.nan

    clean = values
    clean.insert(0, "flag", flag.where(~missing_flag))
    clean["calm"] = calm
    report["records"] = len(clean)
    report["records with valid hs"] = int(clean["hs"].notna().sum())
    return clean, pd.Series(report, name="count").to_frame()

read_hors_profiles

read_hors_profiles(path, units='cm', date_column='date')

Read HORS beach profiles from a CSV table.

Expected layout: one row per survey, a date column, and one column per cross-shore position (-115m, -110m, ..., 385m or plain numbers). Elevations are converted to metres when units="cm".

Returns a profile table (DatetimeIndex, float columns = positions in m) for the :mod:pycoastkit.beach functions.

Source code in src/pycoastkit/hasaki.py
def read_hors_profiles(path, units: str = "cm", date_column: str = "date") -> pd.DataFrame:
    """Read HORS beach profiles from a CSV table.

    Expected layout: one row per survey, a ``date`` column, and one column per
    cross-shore position (``-115m, -110m, ..., 385m`` or plain numbers).
    Elevations are converted to metres when ``units="cm"``.

    Returns a profile table (DatetimeIndex, float columns = positions in m)
    for the :mod:`pycoastkit.beach` functions.
    """
    table = pd.read_csv(path)
    if date_column not in table.columns:
        raise KeyError(f"column {date_column!r} not found; columns start with "
                       f"{list(table.columns[:3])}")
    table.index = pd.to_datetime(table.pop(date_column))
    table = table.apply(pd.to_numeric, errors="coerce")
    if units == "cm":
        table = table / 100.0
    elif units != "m":
        raise ValueError("units must be 'cm' or 'm'")

    return _with_numeric_columns(table)

hasaki_beach_state

hasaki_beach_state(profiles, water_level=HASAKI_MHWS_M, sigma=2.0, volume_zone=(-40.0, 60.0))

Daily shoreline position and beach volume from HORS profiles.

Profiles are smoothed (Gaussian, sigma grid points) to suppress survey noise, the shoreline is the seaward crossing of water_level (default MHWS, 1.25 m) and the volume is integrated over volume_zone [m] above the lowest surveyed bed at each position.

Returns a DataFrame with shoreline [m] and volume [m³/m].

Source code in src/pycoastkit/hasaki.py
def hasaki_beach_state(profiles: pd.DataFrame, water_level: float = HASAKI_MHWS_M,
                       sigma: float = 2.0, volume_zone=(-40.0, 60.0)) -> pd.DataFrame:
    """Daily shoreline position and beach volume from HORS profiles.

    Profiles are smoothed (Gaussian, ``sigma`` grid points) to suppress survey
    noise, the shoreline is the seaward crossing of ``water_level``
    (default MHWS, 1.25 m) and the volume is integrated over ``volume_zone``
    [m] above the lowest surveyed bed at each position.

    Returns a DataFrame with ``shoreline`` [m] and ``volume`` [m³/m].
    """
    smooth = smooth_profiles(profiles, sigma=sigma)
    return pd.DataFrame({
        "shoreline": shoreline_positions(smooth, water_level),
        "volume": beach_volume(smooth, *volume_zone),
    })