Skip to Content
cwa
Advanced

Raw datasets

Read CWA open data by dataset ID, including datasets the SDK does not wrap.

When you need fields or query parameters the SDK does not cover, read the dataset by its ID; the SDK still handles the API key, retries and caching.

Dataset IDs and their parameters are listed in the CWA API documentation.

Reading JSON

from cwa import datasets

rain = datasets.get("O-A0002-001")  # rain gauge stations
sunrise = datasets.get("A-B0062-001", params={"CountyName": "臺北市"})

get() returns parsed JSON: data made of dict, list, strings and numbers, structured as in the CWA documentation.

params holds the CWA API query parameters described for each dataset. The SDK adds the API key itself; putting it in params raises ValueError.

Reading raw content

For XML or another format, use raw() to get the unprocessed bytes:

xml = datasets.raw("W-C0034-001", params={"format": "XML"})

get() handles only JSON; use raw() for any other format.

Validated forecast datasets

The township forecast datasets also have descriptor objects. Passing a descriptor to get() validates the response against its structure and returns typed objects instead of JSON:

from cwa import County, datasets

three_day, one_week = datasets.COUNTY_FORECASTS[County.TAIPEI]
forecast = datasets.get(three_day)
print(forecast.records.locations[0].name)
DescriptorDataset
datasets.COUNTY_FORECASTS[county]That county’s (three-day, one-week) forecasts
datasets.NATIONWIDE_TOWNSHIP_3_DAYThree-day forecasts for every township
datasets.NATIONWIDE_TOWNSHIP_7_DAYOne-week forecasts for every township

Forecasts covers normal use; reach for these descriptors when you need forecast elements the SDK does not summarize.

Several at once

Pass a list to request them simultaneously and get results keyed by dataset:

results = datasets.get(["O-A0002-001", "O-A0003-001"])
rain = results["O-A0002-001"]