Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

- name: Build site
run: |
uv run main.py
uv run -m wavey --resolution f
env:
TQDM_DISABLE: 1

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"matplotlib",
"mpld3",
"numpy",
"pillow>=9.1",
"pygrib",
"requests",
"tqdm",
Expand Down
2 changes: 2 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions main.py → wavey/__main__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import datetime
import io
import logging
from pathlib import Path

import matplotlib
import matplotlib.pyplot as plt
import mpld3
import numpy as np
import PIL.Image
import pygrib
from jinja2 import Environment, PackageLoader, select_autoescape
from tqdm import tqdm

from wavey.common import DATETIME_FORMAT, FEET_PER_METER, TZ_PACIFIC, TZ_UTC, setup_logging
from wavey.grib import NUM_DATA_POINTS, ForecastType, read_forecast_data
from wavey.map import DEFAULT_ARROW_LENGTH, Map
from wavey.map import DEFAULT_ARROW_LENGTH, RESOLUTION, Map
from wavey.nwfs import download_forecast, get_most_recent_forecast

# Force non-interactive backend to keep consistency between local and github actions
Expand Down Expand Up @@ -43,10 +45,30 @@ def utc_to_pt(dt: datetime.datetime) -> datetime.datetime:
return dt.astimezone(tz=TZ_PACIFIC)


def savefig(path: Path) -> None:
"""
Save matplotlib figure to PNG file.

We perform a bit of optimization to make the output filesize smaller
without sacrificing quality.

Args:
path: Path to output PNG file.
"""

bts = io.BytesIO()
plt.savefig(bts, format="png")

with PIL.Image.open(bts) as img:
img2 = img.convert("RGB").convert("P", palette=PIL.Image.Palette.ADAPTIVE)
img2.save(path, format="png")


def main(
grib_path: Path | None = None,
/,
out_dir: Path = Path("_site"),
resolution: RESOLUTION = "h",
) -> None:
"""
Create plots for significant wave height.
Expand All @@ -56,8 +78,13 @@ def main(
https://nomads.ncep.noaa.gov/pub/data/nccf/com/nwps/prod/. If none,
will download the most recent one to the current directory.
out_dir: Path to output directory.
resolution: Resolution of the coastline map. Options are crude, low,
intermediate, high, and full.
"""

if resolution != "f":
LOG.warning("Not drawing full resolution coastlines. Use the flag '--resolution f'")

out_dir.mkdir(parents=True, exist_ok=True)

# Download data, if needed
Expand Down Expand Up @@ -126,6 +153,7 @@ def main(
lat_max_idx=110,
lon_min_idx=20,
lon_max_idx=70,
resolution=resolution,
)

LOG.info("Drawing Breakwater map")
Expand All @@ -140,6 +168,7 @@ def main(
lat_max_idx=BREAKWATER_LAT_IDX + 3,
lon_min_idx=BREAKWATER_LON_IDX - 2,
lon_max_idx=BREAKWATER_LON_IDX + 3,
resolution=resolution,
draw_arrows_length=DEFAULT_ARROW_LENGTH / 3,
draw_arrows_stride=1,
)
Expand All @@ -157,6 +186,7 @@ def main(
lat_max_idx=MONASTERY_LAT_IDX + 4,
lon_min_idx=MONASTERY_LON_IDX - 3,
lon_max_idx=MONASTERY_LON_IDX + 2,
resolution=resolution,
draw_arrows_length=DEFAULT_ARROW_LENGTH / 3,
draw_arrows_stride=1,
)
Expand All @@ -176,7 +206,7 @@ def main(
map_mon.update(hour_i)

ax_main.set_title(f"Significant wave height (ft) and wave direction\nHour {hour_i:03} -- {pacific_time_str}")
plt.savefig(plot_dir / f"{hour_i}.png")
savefig(plot_dir / f"{hour_i}.png")

# Get current time

Expand Down
10 changes: 8 additions & 2 deletions wavey/map.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Iterable, NamedTuple
from typing import Iterable, Literal, NamedTuple

import matplotlib
import matplotlib.colors as mcolors
Expand Down Expand Up @@ -125,6 +125,9 @@ def _update_arrows(
arrow.arrow.set_data(x=arrow_start[0], y=arrow_start[1], dx=dir_vec[0], dy=dir_vec[1])


RESOLUTION = Literal["c", "l", "i", "h", "f"]


class Map:
def __init__(
self,
Expand All @@ -137,6 +140,7 @@ def __init__(
lat_max_idx: int,
lon_min_idx: int,
lon_max_idx: int,
resolution: RESOLUTION = "h",
draw_arrows_length: float = DEFAULT_ARROW_LENGTH,
draw_arrows_stride: int = 3,
) -> None:
Expand All @@ -156,6 +160,8 @@ def __init__(
lat_max_idx: For zooming-in to a smaller lat/lon bounding box.
lon_min_idx: For zooming-in to a smaller lat/lon bounding box.
lon_max_idx: For zooming-in to a smaller lat/lon bounding box.
resolution: Resolution of the coastline map. Options are crude,
low, intermediate, high, and full.
draw_arrows_length: Length of arrows.
draw_arrows_stride: Create an arrow for every multiple of `stride`
indicies.
Expand Down Expand Up @@ -187,7 +193,7 @@ def __init__(
llcrnrlon=lon_min,
urcrnrlat=lat_max,
urcrnrlon=lon_max,
resolution="f",
resolution=resolution,
ax=ax,
)
map.drawcoastlines()
Expand Down