Skip to content

UK NRFA (add more data) #34

Description

@simonmoulds

This relates to #5 and #32

The current UKFetcher access data from the EA Hydrology API, but in fact this API only provides access to England data. Here is a MWE to download data from the National River Flow Archive (NRFA), which is the official UK record of river flow, providing access to daily, monthly and peak flow data for around 1600 catchments across the UK.

import requests
import json
import pandas as pd

# The base URL to access the nrfa API
BASE_URL = "https://nrfaapps.ceh.ac.uk/nrfa/ws"

VALID_DATA_TYPES = [
    'gdf', 'ndf', 'gmf', 'nmf', 'cdr', 'cdr-d', 'cmr',
    'pot-stage', 'pot-flow', 'gauging-stage', 'gauging-flow',
    'amax-stage', 'amax-flow'
]


def catalogue():
    """Get the NRFA data catalog and metadata."""
    query_params = {
        "station": "*",
        "format": "json-object",
        "fields": "all"
    }
    # Send request using requests
    response = requests.get(f"{BASE_URL}/station-info", params=query_params)
    response.raise_for_status()  # raises an error for non-200 responses
    # Decode JSON and load into DataFrame
    data = response.json()
    df = pd.DataFrame(data["data"])
    return df


def _build_ts(response):
    """This is used to parse timeseries data from NRFA."""
    variable = response['data-type']['id']
    dates = response['data-stream'][0::2]
    values = response['data-stream'][1::2]
    df = pd.DataFrame.from_dict({'time': dates, variable: values})
    # Format `time` column as datetime. The API docs specify that
    # times returned by the API have ISO8601 format.
    df['time'] = pd.to_datetime(df['time'], format='ISO8601')
    return df


def get_ts(id, data_type):
    """Get a timeseries from the UK National River Flow Archive."""
    query_params = {
        "station": str(id),
        "data-type": data_type,
        "format": "json-object"
    }
    response = requests.get(f"{BASE_URL}/time-series", params=query_params)
    response.raise_for_status()
    data = response.json()
    df = _build_ts(data)
    return df

metadata = catalogue()
ts = get_ts(1001, "gdf")```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions