|
| 1 | +""" |
| 2 | +Pydantic models for weather.gov API response, including astronomical data utilities. |
| 3 | +""" |
| 4 | + |
| 5 | +from datetime import datetime |
| 6 | +from typing import Any |
| 7 | +from zoneinfo import ZoneInfo |
| 8 | + |
| 9 | +from pydantic import BaseModel, Field |
| 10 | +from typing_extensions import Annotated |
| 11 | + |
| 12 | + |
| 13 | +class Distance(BaseModel): |
| 14 | + """Represents a distance value with unit code.""" |
| 15 | + |
| 16 | + unit_code: Annotated[str, Field(..., alias="unitCode")] |
| 17 | + value: float |
| 18 | + |
| 19 | + |
| 20 | +class Bearing(BaseModel): |
| 21 | + """Represents a bearing value with unit code.""" |
| 22 | + |
| 23 | + unit_code: Annotated[str, Field(..., alias="unitCode")] |
| 24 | + value: float |
| 25 | + |
| 26 | + |
| 27 | +class RelativeLocationProperties(BaseModel): |
| 28 | + """Properties for a relative location including |
| 29 | + city, state, distance, and bearing.""" |
| 30 | + |
| 31 | + city: str |
| 32 | + state: str |
| 33 | + distance: Distance |
| 34 | + bearing: Bearing |
| 35 | + |
| 36 | + |
| 37 | +class RelativeLocationGeometry(BaseModel): |
| 38 | + """Geometry for a relative location (type and coordinates).""" |
| 39 | + |
| 40 | + type: str |
| 41 | + coordinates: list[float] |
| 42 | + |
| 43 | + |
| 44 | +class RelativeLocation(BaseModel): |
| 45 | + """Relative location feature with geometry and properties.""" |
| 46 | + |
| 47 | + type: str |
| 48 | + geometry: RelativeLocationGeometry |
| 49 | + properties: RelativeLocationProperties |
| 50 | + |
| 51 | + |
| 52 | +class AstronomicalData(BaseModel): |
| 53 | + """Astronomical event times for a given location, |
| 54 | + with timezone conversion and formatting methods.""" |
| 55 | + |
| 56 | + sunrise: datetime |
| 57 | + sunset: datetime |
| 58 | + transit: datetime |
| 59 | + civil_twilight_begin: Annotated[datetime, Field(..., alias="civilTwilightBegin")] |
| 60 | + civil_twilight_end: Annotated[datetime, Field(..., alias="civilTwilightEnd")] |
| 61 | + nautical_twilight_begin: Annotated[ |
| 62 | + datetime, Field(..., alias="nauticalTwilightBegin") |
| 63 | + ] |
| 64 | + nautical_twilight_end: Annotated[datetime, Field(..., alias="nauticalTwilightEnd")] |
| 65 | + astronomical_twilight_begin: Annotated[ |
| 66 | + datetime, Field(..., alias="astronomicalTwilightBegin") |
| 67 | + ] |
| 68 | + astronomical_twilight_end: Annotated[ |
| 69 | + datetime, Field(..., alias="astronomicalTwilightEnd") |
| 70 | + ] |
| 71 | + |
| 72 | + def as_local(self, tz: ZoneInfo) -> dict[str, datetime]: |
| 73 | + return {name: value.astimezone(tz) for name, value in self.__dict__.items()} |
| 74 | + |
| 75 | + def formatted(self, tz: ZoneInfo, fmt: str) -> dict[str, str]: |
| 76 | + return {name: dt.strftime(fmt) for name, dt in self.as_local(tz).items()} |
| 77 | + |
| 78 | + |
| 79 | +class NWR(BaseModel): |
| 80 | + """NOAA Weather Radio transmitter info.""" |
| 81 | + |
| 82 | + transmitter: str |
| 83 | + same_code: Annotated[str, Field(..., alias="sameCode")] |
| 84 | + area_broadcast: Annotated[str, Field(..., alias="areaBroadcast")] |
| 85 | + point_broadcast: Annotated[str, Field(..., alias="pointBroadcast")] |
| 86 | + |
| 87 | + |
| 88 | +class Properties(BaseModel): |
| 89 | + """Top-level properties for a weather.gov point feature.""" |
| 90 | + |
| 91 | + id: Annotated[str, Field(..., alias="@id")] |
| 92 | + type_: Annotated[str, Field(..., alias="@type")] |
| 93 | + cwa: str |
| 94 | + type: str |
| 95 | + forecast_office: Annotated[str, Field(..., alias="forecastOffice")] |
| 96 | + grid_id: Annotated[str, Field(..., alias="gridId")] |
| 97 | + grid_x: Annotated[int, Field(..., alias="gridX")] |
| 98 | + grid_y: Annotated[int, Field(..., alias="gridY")] |
| 99 | + forecast: Annotated[str, Field(..., alias="forecast")] |
| 100 | + forecast_hourly: Annotated[str, Field(..., alias="forecastHourly")] |
| 101 | + forecast_grid_data: Annotated[str, Field(..., alias="forecastGridData")] |
| 102 | + observation_stations: Annotated[str, Field(..., alias="observationStations")] |
| 103 | + relative_location: Annotated[RelativeLocation, Field(..., alias="relativeLocation")] |
| 104 | + forecast_zone: Annotated[str, Field(..., alias="forecastZone")] |
| 105 | + county: str |
| 106 | + fire_weather_zone: Annotated[str, Field(..., alias="fireWeatherZone")] |
| 107 | + time_zone: Annotated[str, Field(..., alias="timeZone")] |
| 108 | + radar_station: Annotated[str, Field(..., alias="radarStation")] |
| 109 | + astronomical_data: Annotated[AstronomicalData, Field(..., alias="astronomicalData")] |
| 110 | + nwr: NWR |
| 111 | + |
| 112 | + |
| 113 | +class Geometry(BaseModel): |
| 114 | + """Geometry for a weather.gov feature (type and coordinates).""" |
| 115 | + |
| 116 | + type: str |
| 117 | + coordinates: list[float] |
| 118 | + |
| 119 | + |
| 120 | +class WeatherGovFeature(BaseModel): |
| 121 | + """Root model for weather.gov point feature response.""" |
| 122 | + |
| 123 | + context: Annotated[list[Any], Field(..., alias="@context")] |
| 124 | + id: str |
| 125 | + type: str |
| 126 | + geometry: Geometry |
| 127 | + properties: Properties |
0 commit comments