# Observations and stations (https://nolantw.github.io/cwa/en/guides/observations/)

CWA stations report temperature, humidity, wind, rainfall and other readings every hour. `cwa.observations` finds the station nearest a location and returns its reading; `cwa.stations` looks up the stations themselves.

## Nearby readings

```python
import cwa

reading = cwa.observations.nearest("臺北市信義區")

if reading:
    print(reading.station.name, reading.distance_km, reading.observed_at)
    observation = reading.observation
    print(observation.temperature, observation.humidity, observation.wind.speed)
```

`nearest()` only uses stations that meet both conditions below, and returns `None` when none do:

| Condition | Default | Parameter |
| --- | --- | --- |
| Distance from the location | Within 10 km | `max_distance_km` |
| Observation time | Within two hours | `max_age` (a `timedelta`) |

Mountain and island areas have fewer stations, so you may need to relax these. `None` removes a limit:

```python
from datetime import timedelta

reading = cwa.observations.nearest(
    "花蓮縣秀林鄉", max_distance_km=30, max_age=timedelta(hours=3)
)
```

The nearest station may be in a neighboring township; `reading.station` is the station that actually reported.

## Observation fields

`reading.observation` has these fields. Anything the station did not report is `None`:

| Field | Meaning |
| --- | --- |
| `temperature` | Air temperature (°C) |
| `humidity` | Relative humidity (%) |
| `pressure` | Station pressure (hPa). This is the pressure at the station's altitude, not the sea-level pressure on weather maps, so it is much lower at mountain stations |
| `wind.speed` | Wind speed (m/s) |
| `wind.direction` | Wind direction; see [Wind direction](#wind-direction) below |
| `precipitation.millimetres` | Rainfall since midnight Taiwan time (mm) |
| `precipitation.trace` | `True` when rain fell but too little to measure; `millimetres` is then `None` |
| `condition` | The weather as text, such as `晴` |
| `observed_at` | Observation time |

### Wind direction

A wind direction is not always an angle. `wind.direction.kind` says which case applies:

| `kind` | Meaning | `degrees` |
| --- | --- | --- |
| `DirectionKind.DEGREES` | A normal direction | 0 to 360; 0 is a north wind, 90 an east wind |
| `DirectionKind.CALM` | Calm | `None` |
| `DirectionKind.VARIABLE` | Variable | `None` |
| `DirectionKind.UNKNOWN` | Not reported | `None` |

## A specific station

If you know a station ID, get its latest reading directly:

```python
latest = cwa.observations.latest("466920")
```

It returns `None` when the station has no current reading, and raises `NotFoundError` when the ID does not exist.

## Looking up stations

| Method | Use |
| --- | --- |
| `stations.nearest(location, limit=3)` | The stations closest to a location, with distances |
| `stations.get("466920")` | A station by ID |
| `stations.list(county=County.TAIPEI)` | The stations in a county; omit `county` for all of them |

```python
for item in cwa.stations.nearest("臺北市信義區", limit=3):
    print(item.station.id, item.station.name, item.distance_km)
```

Station fields:

| Field | Meaning |
| --- | --- |
| `id` | Station ID |
| `name` | Station name |
| `coordinates` | Latitude and longitude |
| `county`, `township` | County and township, or `None` if unknown |
| `elevation_m` | Elevation (m) |

The station list comes from the CWA's current observation dataset, `O-A0001-001`, so it contains only stations that are currently reporting.

## Rainfall, UV and staffed stations

| Function | Contents |
| --- | --- |
| `cwa.observations.rainfall()` | Latest rain gauge totals: today, 10 minutes, 1, 3, 6, 12 and 24 hours, 2 and 3 days (mm) |
| `cwa.observations.uv()` | Today's maximum UV index at each station |
| `cwa.observations.staffed()` | Latest readings from staffed stations, with the same fields as `Observation` |

```python
rain = cwa.observations.rainfall("C0A9C0")
print(rain.observed_at, rain.past_hour, rain.past_24_hours)
print(cwa.observations.uv("466920").uv_index)
```

Each takes a station ID or a sequence of them; without an argument, every station is returned.