# Warnings (https://nolantw.github.io/cwa/en/guides/alerts/)

`cwa.alerts.active()` returns the weather warnings in effect, such as heavy rain advisories, low temperature advisories and typhoon warnings. Get all of them, or only those affecting a place.

```python
import cwa

for alert in cwa.alerts.active("臺北市信義區"):
    print(alert.title, alert.effective_at, alert.expires_at)
```

## Filtering

| Write | To get |
| --- | --- |
| `active()` | Every active warning in Taiwan |
| `active("臺北市信義區")` | Warnings affecting this township |
| `active(County.TAIPEI)` | Warnings affecting this county |
| `active(Coordinates(...))` | Warnings covering this point; needs `cwa[geo]` |
| `active(..., alert_type=AlertType.RAIN)` | Only one kind of warning |

`AlertType` is one of `RAIN` (heavy rain), `FOG` (dense fog), `WIND` (strong wind on land), `COLD` (low temperature), `HEAT` (high temperature), `TYPHOON`, `THUNDERSTORM` or `OTHER`.

## Warning fields

| Field | Meaning |
| --- | --- |
| `type` | The kind of warning, an `AlertType` |
| `title` | The title, such as `豪雨特報` |
| `text` | The full text published by the CWA, or `None` |
| `effective_at`, `expires_at` | When it takes effect and expires |
| `areas` | The published affected areas, each with a `label`, `county` and `township` |
| `polygons` | The coverage polygons published with typhoon warnings, as coordinates; empty for other warnings |

## Does it affect a place?

CWA warnings describe their coverage in varying detail: some list townships, some only counties, and some say things like "mountain areas". That is why `alert.affects(place)` returns one of three results rather than `True` or `False`:

| Result | Meaning | Suggested handling |
| --- | --- | --- |
| `Match.YES` | Definitely affects the place | Show the warning |
| `Match.NO` | Definitely does not | Skip it |
| `Match.UNKNOWN` | Not enough detail to tell, for example only the county is listed, or the area is described as "mountain areas" | Treat it as possibly affecting the place, and show `alert.areas` so users can judge |

```python
from cwa import Match

for alert in cwa.alerts.active("臺北市信義區"):
    match alert.affects("臺北市信義區"):
        case Match.YES:
            print("Affects this township:", alert.title)
        case Match.UNKNOWN:
            areas = ", ".join(area.label for area in alert.areas)
            print("May affect this township:", alert.title, areas)
```

When you query by place, `active()` drops only `Match.NO` warnings and keeps `Match.UNKNOWN` ones, so an important warning is never missed.

To check a point, use `alert.contains(coordinates)`, which also returns a `Match`. It needs `cwa[geo]` because polygon matching uses Shapely; `alert.geometry` gives the Shapely polygons too. Without it, both raise `MissingDependencyError`.

## Completeness

Warnings come from several datasets. If one of them is out of date or incomplete, `complete` is `False`:

```python
alerts = cwa.alerts.active("臺北市信義區")
if not alerts.complete:
    print("Warning data may be incomplete; try again later")
```

In that case, finding no warnings does not mean there are none. Check that `complete` is `True` before showing "no active warnings"; if it is `False`, query again later or tell users the data may be incomplete.

## Township-level warnings

`cwa.alerts.hazards()` reads the township-level rain, cold, heat, wind and fog warnings, including issued warnings that take effect later. The location argument works as in `active()`, and `alert_type` keeps one kind:

```python
from cwa import AlertType

for alert in cwa.alerts.hazards("臺北市信義區", alert_type=AlertType.RAIN):
    print(alert.title, alert.effective_at, alert.expires_at)
```