Groundwater chemistry#
Water quality analyses from SGU’s Grundvattenkvalitet, analysresultat och provplatser
(version 2) API, available as client.chemistry. See Groundwater chemistry
for a walkthrough.
Methods#
Get groundwater chemistry sampling sites. |
|
Get a specific groundwater chemistry sampling site by ID. |
|
Convenience function to get a sampling site by any site identifier. |
|
Convenience function to get multiple sampling sites by any site identifier. |
|
Get groundwater chemistry analysis results. |
|
Get a specific groundwater chemistry analysis result by ID. |
|
Get analysis results for a specific site with optional time filtering. |
|
Get analysis results for multiple sites with optional time filtering. |
|
Get analysis results filtered by chemical parameter (e.g., pH, nitrate). |
Models#
Collection of groundwater chemistry sampling sites (GeoJSON FeatureCollection). |
|
A groundwater chemistry sampling site (GeoJSON Feature). |
|
Properties for a groundwater chemistry sampling site. |
|
Collection of groundwater chemistry analysis results (GeoJSON FeatureCollection). |
|
A groundwater chemistry analysis result (GeoJSON Feature). |
|
Properties for a groundwater chemistry analysis result. |
Client#
- class GroundwaterChemistryClient(base_client)[source]#
Client for groundwater chemistry-related SGU API endpoints.
- get_sampling_sites(bbox=None, datetime=None, limit=None, filter_expr=None, sortby=None, **kwargs)[source]#
Get groundwater chemistry sampling sites.
This method is used internally by convenience functions like get_sampling_site_by_name() by constructing filter expressions. A user may also do this, but readability is greater when using the built-in convenience functions.
- Parameters:
bbox (
list[float] |None) – Bounding box as [min_lon, min_lat, max_lon, max_lat]datetime (
str|None) – Date/time filter (RFC 3339 format or interval)limit (
int|None) – Maximum number of features to return (paginated automatically)sortby (
list[str] |None) – List of sort expressions (e.g., [‘+name’, ‘-date’]) Note: the SGU v2 chemistry API currently ignores sortby; features are returned in the server’s default order.**kwargs (
Any) – Additional query parameters
- Return type:
- Returns:
Typed collection of groundwater chemistry sampling sites
- get_sampling_site(site_id)[source]#
Get a specific groundwater chemistry sampling site by ID.
This endpoint is provided by the OGC API but likely not used by any user.
- Parameters:
site_id (
str) – Site identifier- Return type:
- Returns:
Typed groundwater chemistry sampling site
- Raises:
ValueError – If site not found or multiple sites returned
- get_sampling_site_by_name(site_id=None, site_name=None, national_site_id=None, **kwargs)[source]#
Convenience function to get a sampling site by any site identifier.
- Parameters:
site_id (
str|None) – Site identifier (maps to ‘platsbeteckning’ in API)site_name (
str|None) – Site name (maps to ‘provplatsnamn’ in API)national_site_id (
int|None) – National sampling site ID (maps to ‘nationellt_provplatsid’ in API)**kwargs (
Any) – Additional query parameters (e.g., limit)
- Return type:
- Returns:
Typed groundwater chemistry sampling site
- Raises:
SGUNotFoundError – If no site matches.
SGUAmbiguousError – If several sites match.
ValueError – If not exactly one identifier is provided.
Example
>>> from sgu_client import SGUClient >>> with SGUClient() as client: ... # find site by id ... site = client.chemistry.get_sampling_site_by_name(site_id="TTK-442-M") ... # find site by name ... site = client.chemistry.get_sampling_site_by_name(site_name="Lena") ... # find site by national site id ... site = client.chemistry.get_sampling_site_by_name( ... national_site_id=359366 ... ) ... print(site.properties.municipality)
- get_sampling_sites_by_names(site_id=None, site_name=None, national_site_id=None, **kwargs)[source]#
Convenience function to get multiple sampling sites by any site identifier.
- Parameters:
site_id (
list[str] |None) – List of site identifiers (maps to ‘platsbeteckning’ in API)site_name (
list[str] |None) – List of site names (maps to ‘provplatsnamn’ in API)national_site_id (
list[int] |None) – List of national sampling site IDs (maps to ‘nationellt_provplatsid’ in API)**kwargs (
Any) – Additional query parameters (e.g., limit)
- Return type:
- Returns:
Typed collection of groundwater chemistry sampling sites
- Raises:
SGUNotFoundError – If any of the requested sites is not found.
ValueError – If not exactly one identifier is provided.
Example
>>> from sgu_client import SGUClient >>> with SGUClient() as client: ... # get multiple sampling sites by their IDs ... sites = client.chemistry.get_sampling_sites_by_names( ... site_id=["TTK-442-M", "SYD-681-C"] ... ) ... for site in sites.features: ... props = site.properties ... print(f"{props.station_id}: {props.national_site_id}")
- get_analysis_results(bbox=None, datetime=None, limit=None, filter_expr=None, sortby=None, tmin=None, tmax=None, **kwargs)[source]#
Get groundwater chemistry analysis results.
This method is used internally by convenience functions like get_results_by_site() by constructing filter expressions. A user may also do this, but readability is greater when using the built-in convenience functions.
- Parameters:
bbox (
list[float] |None) – Bounding box as [min_lon, min_lat, max_lon, max_lat]datetime (
str|None) – Raw OGC date/time filter; prefertmin/tmaxlimit (
int|None) – Maximum number of features to return (paginated automatically)sortby (
list[str] |None) – List of sort expressions (e.g., [‘+date’, ‘-value’]) Note: the SGU v2 chemistry API currently ignores sortby; features are returned in the server’s default order.tmin (
str|datetime|None) – Earliest sampling date (ISO string or datetime object)tmax (
str|datetime|None) – Latest sampling date (ISO string or datetime object)**kwargs (
Any) – Additional query parameters
- Return type:
- Returns:
Typed collection of groundwater chemistry analysis results
- get_analysis_result(result_id)[source]#
Get a specific groundwater chemistry analysis result by ID.
This endpoint is provided by the OGC API but likely not used by any user.
- Parameters:
result_id (
str) – Result identifier- Return type:
- Returns:
Typed groundwater chemistry analysis result
- Raises:
ValueError – If result not found or multiple results returned
- get_results_by_site(national_site_id=None, site_id=None, site_name=None, tmin=None, tmax=None, limit=None, **kwargs)[source]#
Get analysis results for a specific site with optional time filtering.
Analysis results are linked to sampling sites by national_site_id, which is the recommended (and fastest) way to identify a site. Using site_id or site_name requires an additional API request to look up the site.
- Parameters:
national_site_id (
int|None) – National sampling site ID (maps to ‘nationellt_provplatsid’ in API)site_id (
str|None) – Site identifier (maps to ‘platsbeteckning’ in API)site_name (
str|None) – Site name (maps to ‘provplatsnamn’ in API)tmin (
str|datetime|None) – Start time (ISO string or datetime object)tmax (
str|datetime|None) – End time (ISO string or datetime object)**kwargs (
Any) – Additional query parameters
- Return type:
- Returns:
Typed collection of groundwater chemistry analysis results
- Raises:
SGUNotFoundError – If a site_id or site_name matches no site.
SGUAmbiguousError – If a site_id or site_name matches several sites.
ValueError – If not exactly one of national_site_id, site_id or site_name is provided, or if the looked-up site has no national_site_id.
Example
>>> from sgu_client import SGUClient >>> from datetime import datetime, timezone >>> with SGUClient() as client: ... # get all results for a site ... results = client.chemistry.get_results_by_site(359366, limit=100) ... # get results with time filtering ... results = client.chemistry.get_results_by_site( ... 359366, ... tmin=datetime(2020, 1, 1, tzinfo=timezone.utc), ... tmax=datetime(2021, 1, 1, tzinfo=timezone.utc) ... )
- get_results_by_sites(national_site_id=None, site_id=None, site_name=None, tmin=None, tmax=None, limit=None, **kwargs)[source]#
Get analysis results for multiple sites with optional time filtering.
Analysis results are linked to sampling sites by national_site_id, which is the recommended (and fastest) way to identify sites. Using site_id or site_name requires an additional API request to look up the sites.
- Parameters:
national_site_id (
list[int] |None) – List of national sampling site IDs (maps to ‘nationellt_provplatsid’ in API)site_id (
list[str] |None) – List of site identifiers (maps to ‘platsbeteckning’ in API)site_name (
list[str] |None) – List of site names (maps to ‘provplatsnamn’ in API)tmin (
str|datetime|None) – Start time (ISO string or datetime object)tmax (
str|datetime|None) – End time (ISO string or datetime object)**kwargs (
Any) – Additional query parameters
- Return type:
- Returns:
Typed collection of groundwater chemistry analysis results
- Raises:
SGUNotFoundError – If any site_id or site_name matches no site.
ValueError – If not exactly one of national_site_id, site_id or site_name is provided, or if a looked-up site has no national_site_id.
Example
>>> from sgu_client import SGUClient >>> with SGUClient() as client: ... # get results for multiple sites ... results = client.chemistry.get_results_by_sites( ... [359366, 378865], ... tmin="2020-01-01T00:00:00Z", ... tmax="2021-01-01T00:00:00Z", ... limit=1000 ... ) ... print(f"Found {len(results.features)} analysis results")
- get_results_by_parameter(parameter, national_site_id=None, site_id=None, site_name=None, tmin=None, tmax=None, limit=None, **kwargs)[source]#
Get analysis results filtered by chemical parameter (e.g., pH, nitrate).
This convenience method makes it easy to retrieve all measurements for a specific chemical parameter, optionally filtered by site(s) and time range. Sites are identified the same way as in get_results_by_site(): national_site_id is recommended, while site_id or site_name require an additional API request.
- Parameters:
parameter (
str) – Parameter name as used by SGU (maps to ‘parameternamn’ in API), e.g. ‘pH’, ‘Bly’, ‘Nitratkväve’, ‘Klorid’national_site_id (
int|list[int] |None) – Optional national sampling site ID(s) to filter bysite_id (
str|list[str] |None) – Optional site identifier(s) to filter bysite_name (
str|list[str] |None) – Optional site name(s) to filter bytmin (
str|datetime|None) – Start time (ISO string or datetime object)tmax (
str|datetime|None) – End time (ISO string or datetime object)**kwargs (
Any) – Additional query parameters
- Return type:
- Returns:
Typed collection of groundwater chemistry analysis results
- Raises:
SGUNotFoundError – If any site_id or site_name matches no site.
ValueError – If more than one of national_site_id, site_id or site_name is provided, or if a looked-up site has no national_site_id.
Example
>>> from sgu_client import SGUClient >>> with SGUClient() as client: ... # get all lead measurements since 2020 ... results = client.chemistry.get_results_by_parameter( ... parameter="Bly", ... tmin="2020-01-01", ... ) ... # get pH measurements for a specific site ... results = client.chemistry.get_results_by_parameter( ... parameter="pH", ... national_site_id=359366, ... tmin="2020-01-01", ... tmax="2021-01-01" ... ) ... # ... or multiple sites ... results = client.chemistry.get_results_by_parameter( ... parameter="pH", ... national_site_id=[359366, 378865] ... )
Model details#
Pydantic models for groundwater chemistry data from SGU API.
- pydantic model SamplingSiteProperties[source]#
Properties for a groundwater chemistry sampling site.
-
field national_site_id:
int|None= None (alias 'nationellt_provplatsid')# National sampling site ID
-
field site_type_description:
str|None= None (alias 'provplatstyp_tx')# Sampling site type description
-
field site_category_code:
str|None= None (alias 'provplatskat_bedgr')# Sampling site category code (assessment basis)
-
field site_category_description:
str|None= None (alias 'provplatskat_bedgr_tx')# Sampling site category description
Whether coordinates are hidden
-
field positioning_method_code:
str|None= None (alias 'positioneringsmetod')# Positioning method code
-
field positioning_method_description:
str|None= None (alias 'positioneringsmetod_tx')# Positioning method description
-
field position_quality_description:
str|None= None (alias 'positionskvalitet_tx')# Position quality description
-
field water_district_description:
str|None= None (alias 'vattendistrikt_tx')# Water district description
-
field water_body_catchment_id:
str|None= None (alias 'vattenforekomst_aro_id')# Water body catchment ID
-
field main_catchment_id:
int|None= None (alias 'huvudavrinningsomrade_id')# Main catchment area ID
-
field elevation_system_description:
str|None= None (alias 'hojdsystem_tx')# Elevation reference system description
-
field elevation_method_description:
str|None= None (alias 'hojdmetod_tx')# Elevation method description
-
field overlying_soil_description:
str|None= None (alias 'jord_ovan_jord_tx')# Overlying soil type description
-
field groundwater_environment_code:
str|None= None (alias 'gvmiljo_bedgr')# Groundwater environment code (assessment basis)
-
field groundwater_environment_description:
str|None= None (alias 'gvmiljo_bedgr_tx')# Groundwater environment description
-
field geohydrological_position_code:
str|None= None (alias 'geohylag')# Geohydrological position code
-
field geohydrological_position_description:
str|None= None (alias 'geohylag_tx')# Geohydrological position description
-
field established_date:
Annotated[datetime|None] = None (alias 'etabldatum')# Site establishment date (midnight UTC)
- Constraints:
func = <function _parse_api_datetime at 0x7fa47d9d60c0>
json_schema_input_type = PydanticUndefined
-
field first_sample_date:
Annotated[datetime|None] = None (alias 'forstaprov')# Date of first sample (midnight UTC)
- Constraints:
func = <function _parse_api_datetime at 0x7fa47d9d60c0>
json_schema_input_type = PydanticUndefined
-
field latest_sample_date:
Annotated[datetime|None] = None (alias 'senasteprov')# Date of latest sample (midnight UTC)
- Constraints:
func = <function _parse_api_datetime at 0x7fa47d9d60c0>
json_schema_input_type = PydanticUndefined
-
field sampling_occasion_count:
int|None= None (alias 'antal_provtillf')# Number of sampling occasions
-
field sampling_purpose_description:
str|None= None (alias 'provtagningssyften_tx')# Sampling purpose description
-
field national_monitoring:
str|None= None (alias 'nationell')# National monitoring program (ja/nej)
-
field regional_monitoring:
str|None= None (alias 'regional')# Regional monitoring program (ja/nej)
-
field national_site_id:
- pydantic model AnalysisResultProperties[source]#
Properties for a groundwater chemistry analysis result.
-
field commissioned_by_description:
str|None= None (alias 'bestallare_tx')# Commissioning organisation
-
field performing_organisation_type:
str|None= None (alias 'utfor_org_typ')# Performing organisation type
-
field national_site_id:
int|None= None (alias 'nationellt_provplatsid')# National sampling site ID
-
field actual_sampling_point_east:
float|None= None (alias 'fkt_provpunkt_e')# Actual sampling point east coordinate
-
field actual_sampling_point_north:
float|None= None (alias 'fkt_provpunkt_n')# Actual sampling point north coordinate
-
field sampling_organisation_code:
str|None= None (alias 'provtag_org')# Sampling organisation code
-
field sampling_organisation_description:
str|None= None (alias 'provtag_org_tx')# Sampling organisation
-
field sampling_date:
Annotated[datetime|None] = None (alias 'provtagningsdatum')# Sampling date (UTC)
- Constraints:
func = <function _parse_api_datetime at 0x7fa47d9d60c0>
json_schema_input_type = PydanticUndefined
-
field sampling_end_date:
Annotated[datetime|None] = None (alias 'provtagning_slutdatum')# Sampling end date (UTC)
- Constraints:
func = <function _parse_api_datetime at 0x7fa47d9d60c0>
json_schema_input_type = PydanticUndefined
-
field sampling_purpose_description:
str|None= None (alias 'provtagningssyfte_tx')# Sampling purpose description
-
field direct_treatment_url:
str|None= None (alias 'direktbehandling_url')# Direct sample treatment URL
-
field purge_to_sample_time:
str|None= None (alias 'omsatt_tid_till_prov')# Time from purge to sampling
-
field performing_lab_description:
str|None= None (alias 'utforarlabbid_tx')# Performing laboratory description
-
field registration_date:
Annotated[datetime|None] = None (alias 'registreringsdatum')# Registration date (UTC)
- Constraints:
func = <function _parse_api_datetime at 0x7fa47d9d60c0>
json_schema_input_type = PydanticUndefined
-
field analysis_date:
Annotated[datetime|None] = None (alias 'analysdatum')# Analysis date (UTC)
- Constraints:
func = <function _parse_api_datetime at 0x7fa47d9d60c0>
json_schema_input_type = PydanticUndefined
-
field sample_preparation_url:
str|None= None (alias 'provberedning_url')# Sample preparation method URL
-
field sample_container_description:
str|None= None (alias 'provkarl_tx')# Sample container description
-
field sample_storage_description:
str|None= None (alias 'provlagring_tx')# Sample storage description
-
field measurement_value_annotation:
str|None= None (alias 'matvardetal_anm')# Measurement value annotation/qualifier
-
field measurement_value_span:
str|None= None (alias 'matvardespar')# Measurement value span/range
-
field measurement_standard_description:
str|None= None (alias 'matvstd_tx')# Measurement standard description
-
field measurement_uncertainty_unit_code:
str|None= None (alias 'matosakerhetenhet')# Measurement uncertainty unit code
-
field measurement_uncertainty_unit_description:
str|None= None (alias 'matosakerhetenhet_tx')# Measurement uncertainty unit
-
field measurement_uncertainty_type_code:
str|None= None (alias 'matosakerhettyp')# Measurement uncertainty type code
-
field measurement_uncertainty_type_description:
str|None= None (alias 'matosakerhettyp_tx')# Measurement uncertainty type
-
field measurement_comment:
str|None= None (alias 'kommentar_matvarde')# Measurement value comment
-
field commissioned_by_description:
- pydantic model SamplingSiteCollection[source]#
Collection of groundwater chemistry sampling sites (GeoJSON FeatureCollection).
Its DataFrame has
longitudeandlatitudecolumns, null for sites without a geometry.Examples
>>> from sgu_client import SGUClient >>> with SGUClient() as client: ... sites = client.chemistry.get_sampling_sites(limit=10) ... df = sites.to_dataframe() ... print(df[['national_site_id', 'station_id', 'site_name']].head())
- pydantic model AnalysisResultCollection[source]#
Collection of groundwater chemistry analysis results (GeoJSON FeatureCollection).
Its DataFrame has no geometry columns. Sorted by
sampling_date;to_series()defaults tomeasurement_value.pivot_by_parameter()gives one column per chemical parameter.Examples
>>> from sgu_client import SGUClient >>> with SGUClient() as client: ... results = client.chemistry.get_results_by_site(359366, limit=100) ... df = results.to_dataframe() ... print(df[['sampling_date', 'parameter_name', 'measurement_value']])
- pivot_by_parameter(values='measurement_value', index='sampling_date', columns='parameter_name', aggfunc='mean')[source]#
Pivot analysis results by parameter for easier time series analysis.
This creates a wide-format DataFrame where each chemical parameter becomes a column, making it easy to analyze multiple parameters over time.
- Parameters:
values (
str) – Column to use for values (default: ‘measurement_value’)index (
str) – Column to use as index (default: ‘sampling_date’)columns (
str) – Column to pivot into columns (default: ‘parameter_name’)aggfunc (
str) – Aggregation function if there are duplicate index/column pairs (default: ‘mean’). Can be ‘mean’, ‘median’, ‘first’, ‘last’, etc.
- Return type:
- Returns:
Pivoted DataFrame with parameters as columns.
Example
>>> from sgu_client import SGUClient >>> with SGUClient() as client: ... results = client.chemistry.get_results_by_site(359366) ... df_pivot = results.pivot_by_parameter() ... # now df_pivot has columns like 'pH', 'Nitratkväve', 'Klorid', etc.