|
| 1 | +"""Manual demo of the functionality shown in README.md. |
| 2 | +
|
| 3 | +Run this script to hit the live NOAA CO-OPS APIs and confirm that the |
| 4 | +package behaves as the README advertises. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + uv run python examples/readme_demo.py |
| 8 | +""" |
| 9 | + |
| 10 | +# Station populates metadata keys (name, lat_lon, metadata, ...) as |
| 11 | +# attributes at runtime, so static attribute checks don't apply here. |
| 12 | +# pyright: reportAttributeAccessIssue=false |
| 13 | + |
| 14 | +from pprint import pprint |
| 15 | + |
| 16 | +from noaa_coops import Station, get_stations_from_bbox |
| 17 | + |
| 18 | + |
| 19 | +def section(title: str) -> None: |
| 20 | + bar = "=" * 72 |
| 21 | + print(f"\n{bar}\n{title}\n{bar}") |
| 22 | + |
| 23 | + |
| 24 | +def demo_station_init() -> Station: |
| 25 | + section("1. Station init — Seattle (id=9447130)") |
| 26 | + seattle = Station(id="9447130") |
| 27 | + print(f"name: {seattle.name}") |
| 28 | + return seattle |
| 29 | + |
| 30 | + |
| 31 | +def demo_bbox_search() -> None: |
| 32 | + section("2. get_stations_from_bbox — NYC area") |
| 33 | + stations = get_stations_from_bbox( |
| 34 | + lat_coords=[40.389, 40.9397], |
| 35 | + lon_coords=[-74.4751, -73.7432], |
| 36 | + ) |
| 37 | + pprint(stations) |
| 38 | + assert stations, "Expected at least one station in the NYC bbox" |
| 39 | + |
| 40 | + station_one = Station(id=stations[0]) |
| 41 | + print(f"first station name: {station_one.name}") |
| 42 | + |
| 43 | + |
| 44 | +def demo_metadata(seattle: Station) -> None: |
| 45 | + section("3. Metadata attributes") |
| 46 | + pprint(list(seattle.metadata.items())[:5]) |
| 47 | + print(f"lat: {seattle.lat_lon['lat']}") |
| 48 | + print(f"lon: {seattle.lat_lon['lon']}") |
| 49 | + |
| 50 | + |
| 51 | +def demo_data_inventory(seattle: Station) -> None: |
| 52 | + section("4. Data inventory") |
| 53 | + pprint(seattle.data_inventory) |
| 54 | + |
| 55 | + |
| 56 | +def demo_get_data(seattle: Station) -> None: |
| 57 | + section("5. get_data — Seattle water level, Jan 2015") |
| 58 | + df = seattle.get_data( |
| 59 | + begin_date="20150101", |
| 60 | + end_date="20150131", |
| 61 | + product="water_level", |
| 62 | + datum="MLLW", |
| 63 | + units="metric", |
| 64 | + time_zone="gmt", |
| 65 | + ) |
| 66 | + print(f"shape: {df.shape}") |
| 67 | + print(f"columns: {list(df.columns)}") |
| 68 | + print(f"index name: {df.index.name}") |
| 69 | + print("head:") |
| 70 | + print(df.head()) |
| 71 | + assert not df.empty, "Expected non-empty water_level DataFrame" |
| 72 | + assert df.index.name == "t", "Expected timestamp index named 't'" |
| 73 | + |
| 74 | + |
| 75 | +def demo_currents() -> None: |
| 76 | + section("6. get_data — Oakland currents (examples/currents_example.py)") |
| 77 | + station = Station("s09010") |
| 78 | + df = station.get_data( |
| 79 | + begin_date="20210414", |
| 80 | + end_date="20210415", |
| 81 | + product="currents", |
| 82 | + bin_num=2, |
| 83 | + units="metric", |
| 84 | + time_zone="gmt", |
| 85 | + ) |
| 86 | + print(f"shape: {df.shape}") |
| 87 | + print("head:") |
| 88 | + print(df.head()) |
| 89 | + assert not df.empty, "Expected non-empty currents DataFrame" |
| 90 | + |
| 91 | + |
| 92 | +def main() -> None: |
| 93 | + seattle = demo_station_init() |
| 94 | + demo_bbox_search() |
| 95 | + demo_metadata(seattle) |
| 96 | + demo_data_inventory(seattle) |
| 97 | + demo_get_data(seattle) |
| 98 | + demo_currents() |
| 99 | + print("\nAll README demos completed successfully.") |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments