# 預報 (https://nolantw.github.io/cwa/reference/forecast/)

## `forecast.county`

```python
def county(county: None = None) -> tuple[CountyForecast, ...]
def county(county: CountyInput) -> CountyForecast
def county(county: Sequence[CountyInput]) -> Mapping[County, CountyForecast]
```

回傳縣市 36 小時預報，分為三個 12 小時時段。

```python
client.forecast.county(county: None = None) -> tuple[CountyForecast, ...]
client.forecast.county(county: CountyInput) -> CountyForecast
client.forecast.county(county: Sequence[CountyInput]) -> Mapping[County, CountyForecast]
await async_client.forecast.county(county: None = None) -> tuple[CountyForecast, ...]
await async_client.forecast.county(county: CountyInput) -> CountyForecast
await async_client.forecast.county(county: Sequence[CountyInput]) -> Mapping[County, CountyForecast]
```

## `forecast.get`

```python
def get(location: TownshipInput, *, days: int = 7) -> Forecast
def get(location: BatchScope, *, days: int = 7, errors: Literal['raise'] = 'raise') -> ForecastCollection
def get(location: BatchScope, *, days: int = 7, errors: Literal['collect']) -> BatchResult[Township, Forecast]
```

查詢一個或多個鄉鎮的預報。傳入縣市時，查詢該縣市所有鄉鎮。

**參數**

- `location` 鄉鎮名稱、鄉鎮物件、縣市或地點序列。
- `days` 要查詢的臺灣日期天數，接受 1 到 7。
- `errors` 批次查詢時可指定 `collect`，保留成功及失敗項目。

**回傳值**

單筆鄉鎮預報或多筆結果。指定 `errors="collect"` 時回傳 `BatchResult`。

**例外**

- `ValueError` 天數超出範圍。
- `BatchError` 批次有項目失敗，且 `errors` 為 `raise`。

**範例**

```python
from cwa import forecast

result = forecast.get("臺北市信義區", days=3)
```

```python
client.forecast.get(location: TownshipInput, *, days: int = 7) -> Forecast
client.forecast.get(location: BatchScope, *, days: int = 7, errors: Literal['raise'] = 'raise') -> ForecastCollection
client.forecast.get(location: BatchScope, *, days: int = 7, errors: Literal['collect']) -> BatchResult[Township, Forecast]
await async_client.forecast.get(location: TownshipInput, *, days: int = 7) -> Forecast
await async_client.forecast.get(location: BatchScope, *, days: int = 7, errors: Literal['raise'] = 'raise') -> ForecastCollection
await async_client.forecast.get(location: BatchScope, *, days: int = 7, errors: Literal['collect']) -> BatchResult[Township, Forecast]
```

## `forecast.get_all`

```python
def get_all(*, days: int = 7, errors: Literal['raise'] = 'raise') -> ForecastCollection
def get_all(*, days: int = 7, errors: Literal['collect']) -> BatchResult[Township, Forecast]
```

查詢所有鄉鎮的預報。

```python
client.forecast.get_all(*, days: int = 7, errors: Literal['raise'] = 'raise') -> ForecastCollection
client.forecast.get_all(*, days: int = 7, errors: Literal['collect']) -> BatchResult[Township, Forecast]
await async_client.forecast.get_all(*, days: int = 7, errors: Literal['raise'] = 'raise') -> ForecastCollection
await async_client.forecast.get_all(*, days: int = 7, errors: Literal['collect']) -> BatchResult[Township, Forecast]
```

## `forecast.CountyForecast`

```python
county: CountyName
periods: Annotated[tuple[CountyPeriod, ...], ordered(start)]
```

單一縣市的 36 小時預報，含三個 12 小時時段。

## `forecast.CountyPeriod`

```python
start: Time
end: Time
condition: Text
condition_code: Text
precipitation_probability: Number
min_temperature: Number
max_temperature: Number
comfort: Text
```

單一 12 小時時段。溫度單位為 °C，降雨機率單位為 %。

## `DayForecast`

```python
date: date
temperature: TemperatureRange
precipitation: PrecipitationForecast
wind: WindForecast
condition: WeatherCondition | None
periods: tuple[ForecastPeriod, ...]
sources: tuple[Source, ...]
```

涵蓋臺灣當地單一日期的預報。

## `Forecast`

```python
location: Township
as_of: datetime
sources: tuple[Source, ...]
periods: tuple[ForecastPeriod, ...]
days: tuple[DayForecast, ...]
today: DayForecast | None
tomorrow: DayForecast | None
temperature: NumericSeries
```

鄉鎮預報。可依臺灣日期查詢逐日預報，或依時間查詢指定時段。

### `Forecast.on`

```python
def on(day: date) -> DayForecast | None
```

查詢指定臺灣日期的預報。

### `Forecast.at`

```python
def at(instant: datetime) -> ForecastAtTime | None
```

查詢指定時間有效的預報。未帶時區的時間視為臺灣時間。

### `Forecast.between`

```python
def between(start: datetime, end: datetime) -> Forecast
```

擷取與指定時段重疊的資料。時段包含起點，不包含終點；未帶時區的時間視為臺灣時間。

### `Forecast.to_polars`

```python
def to_polars() -> pl.DataFrame
```

匯出逐日資料，每個有資料的日期為一列。

### `Forecast.to_pandas`

```python
def to_pandas() -> pd.DataFrame
```

匯出逐日資料。須安裝 `cwa[pandas]`。

### `Forecast.to_arrow`

```python
def to_arrow() -> pa.Table
```

匯出逐日資料。須安裝 `cwa[arrow]`。

## `ForecastAtTime`

```python
time: datetime
periods: tuple[ForecastPeriod, ...]
```

指定時間有效的已發布預報。

## `ForecastCollection`

```python
class ForecastCollection
```

以鄉鎮為索引的唯讀預報。

### `ForecastCollection.to_polars`

```python
def to_polars() -> pl.DataFrame
```

匯出逐日資料，每列對應一個鄉鎮及有資料的日期。

### `ForecastCollection.to_pandas`

```python
def to_pandas() -> pd.DataFrame
```

匯出逐日資料。須安裝 `cwa[pandas]`。

### `ForecastCollection.to_arrow`

```python
def to_arrow() -> pa.Table
```

匯出逐日資料。須安裝 `cwa[arrow]`。

## `ForecastPeriod`

```python
element: str
sample: ForecastSample[_Value]
```

單一預報要素及其數值與有效時段。

## `ForecastSample`

```python
value: T | None
start: datetime
end: datetime | None
source: Source
```

指定時刻或有效時段的預報值。

### `ForecastSample.overlaps`

```python
def overlaps(start: datetime, end: datetime) -> bool
```

判斷樣本是否與指定時段重疊。

### `ForecastSample.contains`

```python
def contains(instant: datetime) -> bool
```

判斷指定時間是否位於預報有效時段內。

## `LowerBound`

```python
value: float
```

僅知下限的發布值，例如 `>= 11`，表示實際值至少為 `value`。

## `NumericSeries`

```python
samples: tuple[ForecastSample[float], ...]
```

單一天氣要素的數值樣本。

### `NumericSeries.min`

```python
def min() -> float | None
```

回傳可用樣本的最小值。

### `NumericSeries.max`

```python
def max() -> float | None
```

回傳可用樣本的最大值。

### `NumericSeries.mean`

```python
def mean() -> float | None
```

回傳可用樣本的算術平均。

## `PrecipitationForecast`

```python
max_probability: int | None
```

已發布時段中最高的降雨機率，單位為百分比。

## `TemperatureRange`

```python
min: float | None
max: float | None
sampled_min: float | None
sampled_max: float | None
```

氣象署發布的最高及最低氣溫（°C），另列逐時預報的最高及最低值。

## `WeatherCondition`

```python
description: str
code: str | None
```

氣象署的天氣描述及選填代碼。

## `WindForecast`

```python
max_speed: float | None
max_speed_is_lower_bound: bool
```

已發布的最大風速，單位為公尺每秒。`max_speed_is_lower_bound` 為 `True` 時，至少一個時段僅發布下限，實際最大風速至少為 `max_speed`。