Welcome to Yr to Pandas’s documentation!
This module provides a low-level client for the YR weather API.
- yr_to_pandas.yr_client.cached_yr_request(cache_filename, url, headers, params=None, **kwargs)[source]
Lowest level function, calls the yr api and returns a dictionary with the results.
Ensures Expires and If-Modified-Since yr headers are handled correctly to reduce load on server https://developer.yr.no/doc/GettingStarted/
Note: it is up to the application to ensure cache_filename is unique per unique request (i.e. different depending on url and location). From https://api.met.no/doc/TermsOfService: ensure you don’t request a new location for every meter!
- Parameters:
cache_filename (str) – Uses the given filename to cache the data.
url (str) – Page url to visit
headers (dict) – header sent to requests.get, ensure this has a custom ‘user-agent’ and ‘Accept’: ‘application/json’
params (dict) – params sent to requests.get, will typically contain at least ‘lat’ and ‘lon’, depends on url.
**kwargs – are passed to requests.get
- Returns:
Dictionary with results, depends on url, but will always contain ‘Expires’, ‘Last-Modified’ and ‘Cached’. The result is also stored in cache_filename as json and will be returned directly if this function is called before the data has expired.
- Return type:
dict
Example
>>> lon = 10.83576 >>> lat = 59.71949 >>> cache_filename = 'tmp.json' >>> server = 'https://api.met.no/weatherapi/' >>> headers = {'user-agent': 'python-yr/ob@cakebox.net'} >>> url = server + 'locationforecast/2.0/compact' >>> payload = {'lat': lat, 'lon': lon} >>> df = cached_yr_request(cache_filename, url, headers, params=payload) >>> df.keys() dict_keys(['type', 'geometry', 'properties', 'Expires', 'Last-Modified']) >>> df['properties'].keys() dict_keys(['meta', 'timeseries']) >>> entry0 = df['properties']['timeseries'][0] # look at first entry >>> entry0.keys() dict_keys(['time', 'data']) >>> entry0['data'].keys() dict_keys(['instant', 'next_12_hours', 'next_1_hours', 'next_6_hours'])
Examples
Provides basic examples to use the yr_to_pandas library.
Intended use-case is to adapt yr_examples to your needs. Using as is will:
fetch data for the specified latitude and longitude.
save the data in a local .parquet cache.
- yr_to_pandas.yr_examples.get_airquality(lat, lon, areaclass='grunnkrets')[source]
Call airqualityforecast/0.1 to get hourly forecast for a given lat/lon.
More information https://api.met.no/weatherapi/airqualityforecast/0.1/documentation
Uses the scripts directory as the cache directory both to cache the previous request (in a .json file) and to keep history of previous results (in a .parquet file).
- Parameters:
lat (int) – latitude, will be rounded to 4 digits.
lon (int) – longitude, will be rounded to 4 digits.
arealclass= (str) – Size of the area, one of grunnkrets, fylke, kommune or delomrade
- Returns:
returns a dataframe
- Return type:
pandas.DataFrame
Examples
>>> df = get_airquality(lat = 59.71949, lon = 10.83576) >>> df.keys() Index(['time', 'AQI', 'no2_concentration [ug/m3]', 'AQI_no2', 'no2_nonlocal_fraction [%]', 'no2_nonlocal_fraction_seasalt [%]', 'no2_local_fraction_traffic_exhaust [%]', 'no2_local_fraction_traffic_nonexhaust [%]', 'no2_local_fraction_shipping [%]', 'no2_local_fraction_heating [%]', 'no2_local_fraction_industry [%]', 'pm10_concentration [ug/m3]', 'AQI_pm10', 'pm10_nonlocal_fraction [%]', 'pm10_nonlocal_fraction_seasalt [%]', 'pm10_local_fraction_traffic_exhaust [%]', 'pm10_local_fraction_traffic_nonexhaust [%]', 'pm10_local_fraction_shipping [%]', 'pm10_local_fraction_heating [%]', 'pm10_local_fraction_industry [%]', 'pm25_concentration [ug/m3]', 'AQI_pm25', 'pm25_nonlocal_fraction [%]', 'pm25_nonlocal_fraction_seasalt [%]', 'pm25_local_fraction_traffic_exhaust [%]', 'pm25_local_fraction_traffic_nonexhaust [%]', 'pm25_local_fraction_shipping [%]', 'pm25_local_fraction_heating [%]', 'pm25_local_fraction_industry [%]', 'o3_concentration [ug/m3]', 'AQI_o3', 'o3_nonlocal_fraction [%]', 'o3_nonlocal_fraction_seasalt [%]', 'o3_local_fraction_traffic_exhaust [%]', 'o3_local_fraction_traffic_nonexhaust [%]', 'o3_local_fraction_shipping [%]', 'o3_local_fraction_heating [%]', 'o3_local_fraction_industry [%]'], dtype='object')
- yr_to_pandas.yr_examples.get_hourly_forecast_compact(lat, lon)[source]
Call locationforecast/2.0/compact to get hourly forecast for a given lat/lon.
Uses the scripts directory as the cache directory both to cache the previous request (in a .json file) and to keep history of previous results (in a .parquet file).
- Parameters:
lat (int) – latitude, will be rounded to 4 digits.
lon (int) – longitude, will be rounded to 4 digits.
- Returns:
returns a dataframe
- Return type:
pandas.DataFrame
Examples
>>> df = get_hourly_forecast_compact(lat = 59.71949, lon = 10.83576) >>> df.keys() Index(['air_pressure_at_sea_level', 'air_temperature', 'cloud_area_fraction', 'relative_humidity', 'wind_from_direction', 'wind_speed', 'time', 'next_1_hours_precipitation_amount', 'next_1_hours_symbol_code', 'next_6_hours_precipitation_amount', 'next_6_hours_symbol_code', 'next_12_hours_symbol_code'], dtype='object')
- yr_to_pandas.yr_examples.get_nowcast(lat, lon)[source]
Call nowcast/2.0/compact to get hourly forecast for a given lat/lon.
More information https://api.met.no/weatherapi/nowcast/2.0/documentation
Uses the scripts directory as the cache directory both to cache the previous request (in a .json file) and to keep history of previous results (in a .parquet file).
- Parameters:
lat (int) – latitude, will be rounded to 4 digits.
lon (int) – longitude, will be rounded to 4 digits.
- Returns:
returns a dataframe
- Return type:
pandas.DataFrame
Examples
>>> df = get_nowcast(lat = 59.71949, lon = 10.83576) >>> sorted(df.keys()) ['air_temperature', 'precipitation_rate', 'relative_humidity', 'time', 'wind_from_direction', 'wind_speed', 'wind_speed_of_gust']
Parsers
This module provides functions to parse YR weather API responses into pandas DataFrames.
- yr_to_pandas.yr_parser.parse_airquality(res)[source]
Convert a dictionary from airqualityforecast/0.1 to a flat pandas DataFrame.
- yr_to_pandas.yr_parser.parse_hourly_forecast_compact(res)[source]
Convert a dictionary from locationforecast/2.0/compact to a flat pandas DataFrame.
See
get_hourly_forecast_compact(...)
for example usage.
- yr_to_pandas.yr_parser.parse_nowcast(res)[source]
Convert a dictionary from nowcast/2.0/compact to a flat pandas DataFrame.
See
get_nowcast(...)
for example usage.