easysnowdata.stations.clients.NVEClient#
- class easysnowdata.stations.clients.NVEClient(base_url: str = 'https://hydapi.nve.no/api/v1', timeout: int = 60, max_retries: int = 3, backoff: int = 4, session: Session | None = None, api_key: str | None = None)[source]#
Bases:
objectClient for the NVE HydAPI (Norwegian hydrological data service).
An API key is required for all endpoints — set the
NVE_API_KEYenvironment variable or passapi_key. Register for a free key at https://hydapi.nve.no/.- Parameters:
base_url (
str) – Base URL of the NVE HydAPI. Override for staging environments.timeout (
int) – HTTP request timeout in seconds.max_retries (
int) – Number of retry attempts on transient server errors (5xx).backoff (
int) – Base backoff delay in seconds. Actual delay = backoff × attempt_number.session (
requests.SessionorNone) – Optional pre-configured session (useful for proxies, etc.).
- get_stations(parameter_ids: list[int] | int | None = None, active_only: bool = False, bbox: tuple[float, float, float, float] | None = None) list[dict][source]#
List NVE hydrological stations, optionally filtered by parameter.
- Parameters:
parameter_ids (
list[int]orint, optional) – NVE parameter ID(s) to filter stations by. - 2002 : Snow depth (Snødybde) - 2003 : Snow Water Equivalent (Snøens vannekvivalent) (2001 is soil water — NOT a snow parameter.) Defaults to no parameter filter (all stations).active_only (
bool) – If True, return only currently active stations.bbox (
tuple, optional) –(min_lon, min_lat, max_lon, max_lat)bounding box filter.
- Returns:
list[dict]– One dict per station with keys:station_id,name,latitude,longitude,elevation_m,drainage_basin_key,status,station_url,parameters.
Example
>>> client = NVEClient() >>> swe_stations = client.get_stations(parameter_ids=2002) >>> len(swe_stations) > 20 True
- get_all_stations(active_only: bool = False, bbox: tuple[float, float, float, float] | None = None) list[dict][source]#
Get all NVE stations with snow parameters (SWE and/or snow depth).
This is the recommended entry point for discovering snow monitoring stations. It fetches stations with parameter 2003 (SWE) and parameter 2002 (snow depth) and deduplicates.
- Parameters:
- Returns:
list[dict]– Same schema asget_stations().
- get_series(parameter: int | None = None, station_id: str | None = None) list[dict][source]#
List time series available in HydAPI (GET /Series).
A series is one (station, parameter, version) combination with a list of supported time resolutions and the data range covered at each resolution.
- Parameters:
- Returns:
list[dict]– One dict per series with keys:station_id,station_name,parameter,parameter_name,version_no,unit,serie_from,serie_to,resolutions.resolutionsis the rawresolutionList— each entry hasresTime(minutes: 0/60/1440),dataFromTimeanddataToTime.
- get_observations(station_id: str, parameter_id: int, begin_date: str | date | None = None, end_date: str | date | None = None, resolution: int = 1440) list[dict][source]#
Fetch raw observations for a single station and parameter.
- Parameters:
station_id (
str) – NVE station ID, e.g."2.11.0".parameter_id (
int) – NVE parameter ID (e.g. 2003 for SWE, 2002 for snow depth).begin_date (
strordate, optional) – Start of the observation window ("YYYY-MM-DD").end_date (
strordate, optional) – End of the observation window (inclusive).resolution (
int) – Temporal resolution in minutes. 1440 = daily; 60 = hourly.
- Returns:
list[dict]– Observation records (one per timestamp), each withtime,value,correctionandqualityfields.- Raises:
NVEError – On request failure.
- get_data(station_ids: list[str] | str | None = None, variables: list[str] | str | None = None, bbox: tuple[float, float, float, float] | None = None, begin_date: str | date | None = None, end_date: str | date | None = None, interval: str = 'daily', include_flags: bool = False) list[dict][source]#
Standardized data fetch — returns a flat list of observation records.
- Parameters:
station_ids (
list[str]orstrorNone) – NVE station ID(s), e.g."2.11.0". Required unlessbboxis provided.variables (
list[str]orstrorNone) – NVE variable key(s) (e.g."swe_m") or standardized types (e.g."swe").Nonereturns all snow variables (SWE and snow depth).bbox (
tuple, optional) –(min_lon, min_lat, max_lon, max_lat). Alternative tostation_ids; fetches data for all snow stations in the box.begin_date (
strordate, optional) – Start date ("YYYY-MM-DD").end_date (
strordate, optional) – End date (inclusive).interval (
str) – Temporal resolution:"daily"(default) or"hourly".include_flags (
bool) – If True, add a"flag"key with the NVE quality code to each record.
- Returns:
list[dict]–Flat list of observation records:
{ "station_id": "2.11.0", "date": "2024-01-15", "variable": "swe_m", "type": "swe", "value": 12.5, # cm (converted from m × 100) "units": "cm", "interval": "daily", # "flag": "0" (only present when include_flags=True) }
Notes
SWE values (parameter 2003) are stored by NVE in metres. This method converts them to cm (× 100) so that
"units"is always"cm"for type"swe".Snow depth values (parameter 2002) are stored by NVE in cm and are returned as-is.
- Raises:
ValueError – If neither
station_idsnorbboxis provided.NVEError – On network / API failure.
Example
>>> client = NVEClient() >>> records = client.get_data( ... station_ids="2.11.0", ... variables=["swe"], ... begin_date="2024-01-01", ... end_date="2024-01-15", ... ) >>> records[0].keys() dict_keys(['station_id', 'date', 'variable', 'type', 'value', 'units', 'interval'])