fmidev / smartmet-plugin-timeseries

MIT License
2 stars 1 forks source link

Full Python / pandas example of the use of the FMI opendata timeseries endpoint #69

Closed mikaelhg closed 1 year ago

mikaelhg commented 2 years ago

Since the Wiki went away, I'm adding a full Python example of the use of the FMI opendata timeseries endpoint as an issue.

import requests
import datetime
import pandas as pd
import numpy as np

end_time = datetime.datetime.utcnow()
start_time = end_time - datetime.timedelta(days=1)

aq_fields = {
    'fmisid': np.int32,
    'time': np.datetime64,
    'AQINDEX_PT1H_avg': np.float64,
    'PM10_PT1H_avg': np.float64,
    'PM25_PT1H_avg': np.float64,
    'O3_PT1H_avg': np.float64,
    'CO_PT1H_avg': np.float64,
    'SO2_PT1H_avg': np.float64,
    'NO2_PT1H_avg': np.float64,
    'TRSC_PT1H_avg': np.float64,
}

url = 'https://opendata.fmi.fi/timeseries'

params = {
    'format': 'json',
    'precision': 'double',
    'groupareas': '0',
    'producer': 'airquality_urban',
    'area': 'Uusimaa',
    'param': ','.join(aq_fields.keys()),
    'starttime': start_time.isoformat(timespec="seconds"),
    'endtime': end_time.isoformat(timespec="seconds"),
    'tz': 'UTC',
}

data = requests.get(url, params=params).json()

df = pd.DataFrame(data).astype(aq_fields)
#df = df.set_index(['fmisid', 'time'])
mikaelhg commented 1 year ago

Merged elsewhere.