# Forecasts (https://nolantw.github.io/cwa/en/guides/forecast/)

`cwa.forecast.get()` returns a township's forecast for the next one to seven days. Read it day by day, or ask for a specific time or time range.

```python
import cwa

forecast = cwa.forecast.get("臺北市信義區", days=7)

for day in forecast:
    condition = day.condition.description if day.condition else None
    print(day.date, day.temperature.min, day.temperature.max, condition)
```

`days` sets how many days of forecast to get, from 1 to 7, counting from today in Taiwan time. For example, `days=3` gets today, tomorrow and the day after, and `days=1` gets only today.

## One day

| Write | To get |
| --- | --- |
| `forecast.today` | Today |
| `forecast.tomorrow` | Tomorrow |
| `forecast.on(date(2026, 12, 1))` | A given date |
| `forecast[0]`, `forecast[-1]` | The first or last day, by position |

`today`, `tomorrow` and `on()` return `None` when the forecast does not cover that day, so check first:

```python
if forecast.tomorrow:
    print(forecast.tomorrow.precipitation.max_probability)
```

## Daily fields

| Field | Meaning | `None` when |
| --- | --- | --- |
| `date` | The Taiwan date | |
| `temperature.min`, `temperature.max` | The low and high published by the CWA (°C); see below | The published periods do not cover the whole day, usually today |
| `temperature.sampled_min`, `temperature.sampled_max` | The lowest and highest hourly temperature forecast during the day (°C) | The day has no hourly forecast, i.e. it is beyond the three-day forecast |
| `precipitation.max_probability` | The highest probability of precipitation among the day's periods (%) | The day has no precipitation forecast |
| `wind.max_speed` | The day's highest wind speed (m/s) | The day has no wind forecast |
| `condition` | The daytime (06:00–18:00) weather; `condition.description` is the text | The daytime period is over or unpublished, usually this evening |
| `periods` | Every raw forecast period of the day; see [Raw periods](#raw-periods) | |

### Low versus sampled low

The two pairs come from different sources and suit different uses:

| | `min`, `max` | `sampled_min`, `sampled_max` |
| --- | --- | --- |
| Source | The CWA's published 12-hour lows and highs | Hourly (or 3-hourly) temperature forecasts |
| Covers | 18:00 the evening before to 06:00 the next morning, slightly wider than the date | Only times within the date |
| Available | Every day from tomorrow; today's disappears once the daytime period ends | Days the three-day forecast covers |

The CWA publishes lows and highs for daytime (06:00–18:00) and night (18:00–06:00 the next day). The SDK takes every period covering the date, meaning the night before, the daytime and that night, to compute `min` and `max`. Because night periods cross midnight, `min` may come from late the evening before or early the next morning.

Use `min` and `max` for an everyday "today's high and low"; use `sampled_min` and `sampled_max` for the rest of today, or when you need values strictly within the date.

### Probability of precipitation

The CWA publishes the probability of precipitation for 3- or 12-hour periods, depending on the forecast. `max_probability` is the highest of the day's periods: 20% in the morning and 60% in the afternoon gives 60. It means "the wettest period of the day", not "the chance it rains at some point during the day".

### Wind lower bounds

Some wind speeds are published only as a lower bound, such as "11 m/s or more". `wind.max_speed_is_lower_bound` is then `True` and `max_speed` holds the bound; the actual wind may be stronger.

## At a given time

`at()` returns the forecast valid at one moment, and `between()` the forecast over a time range. A datetime without a timezone is taken as Taiwan time:

```python
from datetime import datetime, timedelta

now = datetime.now()

forecast_at_time = forecast.at(now + timedelta(hours=3))
if forecast_at_time:
    for period in forecast_at_time.periods:
        print(period.element, period.sample.value)

tonight = forecast.between(now, now + timedelta(hours=12))
print(tonight.temperature.min(), tonight.temperature.max())
```

| Method | Returns | Without data |
| --- | --- | --- |
| `at(time)` | Every forecast period valid at that time, in `at.periods` | `None` |
| `between(start, end)` | The forecast overlapping that range, of the same type as `forecast`, readable day by day | A forecast with no days |

Periods include their start and exclude their end: a 00:00–03:00 period applies at 00:00 but no longer at 03:00. `between()` keeps the original start and end of overlapping periods rather than trimming them.

`forecast.temperature` holds the point temperature forecasts and offers `min()`, `max()` and `mean()`. They skip missing values and return `None` when there are none.

## Raw periods

`periods` lists every forecast the CWA published, for when you need an element the SDK does not summarize:

| Field | Meaning |
| --- | --- |
| `element` | The CWA element name, such as `溫度` or `3小時降雨機率` |
| `sample.value` | The value; a value published only as a lower bound is a `LowerBound` |
| `sample.start`, `sample.end` | The validity period; `end` is `None` for a forecast at a single instant |
| `sample.source` | This value's [data source](/en/reading#data-sources) |

## Many townships

Pass a county or a list to get results keyed by township, which you can also export as a table. See [Batch queries and export](/en/advanced/batch).

## County 36-hour forecast

`cwa.forecast.county()` returns a county's forecast for the next 36 hours in three 12-hour periods:

```python
county = cwa.forecast.county("臺北市")
for period in county.periods:
    print(period.start, period.condition, period.precipitation_probability, period.min_temperature, period.max_temperature, period.comfort)
```

A sequence of counties returns a mapping keyed by `County`; without an argument, every county is returned.