Cape Cod - Simple workflow

Standardize, clean and wrangle Water Quality Portal data in Cape Cod into more analytic-ready formats using the harmonize_wq package

US EPA’s Water Quality Portal (WQP) aggregates water quality, biological, and physical data provided by many organizations and has become an essential resource with tools to query and retrieval data using python or R. Given the variety of data and variety of data originators, using the data in analysis often requires data cleaning to ensure it meets the required quality standards and data wrangling to get it in a more analytic-ready format. Recognizing the definition of analysis-ready varies depending on the analysis, the harmonixe_wq package is intended to be a flexible water quality specific framework to help:

  • Identify differences in data units (including speciation and basis)

  • Identify differences in sampling or analytic methods

  • Resolve data errors using transparent assumptions

  • Reduce data to the columns that are most commonly needed

  • Transform data from long to wide format

Domain experts must decide what data meets their quality standards for data comparability and any thresholds for acceptance or rejection.

Simple workflow

This example steps through a typical workflow in Cape Cod to demonstrate commonly used functionality

Install the required libraries

[1]:
import sys
#!python -m pip uninstall harmonize-wq --yes
# Use pip to install the package from pypi or the latest from github
#!{sys.executable} -m pip install harmonize-wq
# For latest dev version
#!{sys.executable} -m pip install git+https://github.com/USEPA/harmonize-wq.git@new_release_0-3-8

Create dataretrieval query using a polygon for Area Of Interest

[2]:
from harmonize_wq import wrangle
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/dataretrieval/nadp.py:44: UserWarning: GDAL not installed. Some functions will not work.
  warnings.warn('GDAL not installed. Some functions will not work.')
[3]:
# Read geometry for Area of Interest from geojson file url
# NOTE: alternatively you can direct it to a local shapefile
aoi_url = 'https://github.com/jbousquin/test_notebook/raw/main/temperature_data/NewEngland.geojson'
[4]:
# Map aoi (geojson is WGS1984 standard)
wrangle.as_gdf(aoi_url).plot()
[4]:
<Axes: >
../_images/notebooks_Harmonize_CapeCod_Simple_10_1.png

The Area Of Interest is made up of many small polygons. The query will be built from the total extent of these polygons, but alternatively could be restricted to the bounding box for just one such polygon.

[5]:
# Bounding box string for total extent
bBox = wrangle.get_bounding_box(aoi_url)
# Bounding box string for specific polygon by index
#bBox = wrangle.get_bounding_box(aoi_url, idx=0)
[6]:
# Build query
query = {'characteristicName': ['Phosphorus',
                                'Temperature, water',
                                'Depth, Secchi disk depth',
                                'Dissolved oxygen (DO)',
                                'Salinity',
                                'pH',
                                'Nitrogen',
                                'Conductivity',
                                'Organic carbon',
                                'Chlorophyll a',
                                'Turbidity',
                                'Sediment',
                                'Fecal Coliform',
                                'Escherichia coli']}
query['bBox'] = bBox
query['dataProfile'] = 'narrowResult'

Retrieve data

Stations will be retrieved using the query criteria seperately after results. md_narrow is metadata documenting the query not used here but meant for reproducibility

[7]:
import dataretrieval.wqp as wqp
[8]:
# Query for results
# Note: large quieries like this can take up a lot of RAM and may give a DtypeWarning,
# set low_memory=False or provide dataTypes for columns to use less memory.
res_narrow, md_narrow = wqp.get_results(**query)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/dataretrieval/wqp.py:83: DtypeWarning: Columns (8,10,13,15,17,19,20,21,22,23,28,31,32,33,34,36,38,60,63,64,65,66,67,68,69,70,71,72) have mixed types. Specify dtype option on import or set low_memory=False.
  df = pd.read_csv(StringIO(response.text), delimiter=',')
[9]:
# Look at initial results (input)
df = res_narrow
df
[9]:
OrganizationIdentifier OrganizationFormalName ActivityIdentifier ActivityStartDate ActivityStartTime/Time ActivityStartTime/TimeZoneCode MonitoringLocationIdentifier ResultIdentifier DataLoggerLine ResultDetectionConditionText ... AnalysisEndTime/TimeZoneCode ResultLaboratoryCommentCode ResultLaboratoryCommentText ResultDetectionQuantitationLimitUrl LaboratoryAccreditationIndicator LaboratoryAccreditationAuthorityName TaxonomistAccreditationIndicator TaxonomistAccreditationAuthorityName LabSamplePreparationUrl ProviderName
0 BRC Blackstone River Coalition (Volunteer) BRC-C-02-02-020:20131012040800:FM:0.2083333333... 2013-10-12 04:08:00 EST BRC-C-02-02-020 STORET-1039097035 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN STORET
1 OARS OARS - For the Assabet Sudbury & Concord River... OARS-CND-161:20130818:0651:FM:1 2013-08-18 06:51:00 EDT OARS-CND-161 STORET-838568413 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN STORET
2 OARS OARS - For the Assabet Sudbury & Concord River... OARS-SUD-064:20130721:0610:FM:1 2013-07-21 06:10:00 EDT OARS-SUD-064 STORET-838568309 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN STORET
3 CRWA Charles River Watershed Association (Massachus... CRWA-CYN20130809ROBTemp01 2013-08-09 11:14:33 EST CRWA-ROB STORET-591631481 130809111433.0 NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN STORET
4 WTGHA Wompanoag Tribe of Gay Head Aquinnah (Tribal) WTGHA-m41:20131017:FM:B 2013-10-17 NaN NaN WTGHA-M41 STORET-1041401688 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN STORET
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
457247 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400495 2024-01-08 12:00:00 EST USGS-01098530 NWIS-126836828 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NWIS
457248 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989046 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NWIS
457249 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989056 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NWIS
457250 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989063 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NWIS
457251 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989064 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NWIS

457252 rows × 78 columns

Harmonize data using defaults

[10]:
from harmonize_wq import harmonize
from harmonize_wq import location
from harmonize_wq import visualize
[11]:
# Harmonize it and look at how it changed (output)
# Note: 'ignore' unit dimensionality errors will warn when they are encountered and replace with NaN
# Note: depending on the Pint version this may cause a UnitStrippedWarning
df_harmonized = harmonize.harmonize_all(df, errors='ignore')
df_harmonized
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/wq_data.py:395: UserWarning: WARNING: 'ug/m3' UNDEFINED UNIT for Chlorophyll
  warn("WARNING: " + problem)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/wq_data.py:395: UserWarning: WARNING: 'ug/cm2' UNDEFINED UNIT for Chlorophyll
  warn("WARNING: " + problem)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/wq_data.py:395: UserWarning: WARNING: 'ppb' UNDEFINED UNIT for Chlorophyll
  warn("WARNING: " + problem)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'count' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/wq_data.py:395: UserWarning: WARNING: 'None' UNDEFINED UNIT for Secchi
  warn("WARNING: " + problem)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/wq_data.py:395: UserWarning: WARNING: 'CFUcol/100mL' UNDEFINED UNIT for E_coli
  warn("WARNING: " + problem)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'MPN/100mL' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'CFU/100mL' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'cfu/100mL' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: '%' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'count' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'MPN/100mL' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'CFU/100mL' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'cfu/100mL' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/wq_data.py:395: UserWarning: WARNING: 'cm3/g' UNDEFINED UNIT for Nitrogen
  warn("WARNING: " + problem)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/pandas/core/construction.py:616: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
  data = np.array(data, copy=copy)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/pandas/core/construction.py:616: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
  data = np.array(data, copy=copy)
2 Phosphorus sample fractions not in frac_dict
2 Phosphorus sample fractions not in frac_dict found in expected domains, mapped to "Other_Phosphorus"
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/basis.py:154: UserWarning: Mismatched ResultTemperatureBasisText: updated from 25 deg C to @25C (units)
  warn(f"Mismatched {flag}", UserWarning)
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'deg C' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'g / H2O' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'count' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/harmonize.py:149: UserWarning: Bad Turbidity unit: count
  warn(f"Bad Turbidity unit: {unit}")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'count' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/harmonize_wq/convert.py:128: UserWarning: WARNING: 'mV' converted to NaN
  warn(f"WARNING: '{unit}' converted to NaN")
[11]:
OrganizationIdentifier OrganizationFormalName ActivityIdentifier ActivityStartDate ActivityStartTime/Time ActivityStartTime/TimeZoneCode MonitoringLocationIdentifier ResultIdentifier DataLoggerLine ResultDetectionConditionText ... Carbon Phosphorus TP_Phosphorus TDP_Phosphorus Other_Phosphorus Salinity Sediment Temperature Turbidity pH
0 BRC Blackstone River Coalition (Volunteer) BRC-C-02-02-020:20131012040800:FM:0.2083333333... 2013-10-12 04:08:00 EST BRC-C-02-02-020 STORET-1039097035 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN 7.6 Nephelometric_Turbidity_Units NaN
1 OARS OARS - For the Assabet Sudbury & Concord River... OARS-CND-161:20130818:0651:FM:1 2013-08-18 06:51:00 EDT OARS-CND-161 STORET-838568413 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN 22.18 degree_Celsius NaN NaN
2 OARS OARS - For the Assabet Sudbury & Concord River... OARS-SUD-064:20130721:0610:FM:1 2013-07-21 06:10:00 EDT OARS-SUD-064 STORET-838568309 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 CRWA Charles River Watershed Association (Massachus... CRWA-CYN20130809ROBTemp01 2013-08-09 11:14:33 EST CRWA-ROB STORET-591631481 130809111433.0 NaN ... NaN NaN NaN NaN NaN NaN NaN 23.01 degree_Celsius NaN NaN
4 WTGHA Wompanoag Tribe of Gay Head Aquinnah (Tribal) WTGHA-m41:20131017:FM:B 2013-10-17 NaN NaN WTGHA-M41 STORET-1041401688 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN 1.0 Nephelometric_Turbidity_Units NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
457247 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400495 2024-01-08 12:00:00 EST USGS-01098530 NWIS-126836828 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN 2.9 Nephelometric_Turbidity_Units NaN
457248 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989046 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN 3.9 degree_Celsius NaN NaN
457249 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989056 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 7.4 dimensionless
457250 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989063 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
457251 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 EST USGS-01097050 NWIS-126989064 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN 2.7 Nephelometric_Turbidity_Units NaN

457252 rows × 98 columns

[12]:
# Get harmonized stations clipped to the Area of Interest
stations_gdf, stations, site_md = location.get_harmonized_stations(query, aoi=aoi_url)
[13]:
# Map number of usable results at each station
gdf_count = visualize.map_counts(df_harmonized, stations_gdf)
legend_kwds={"fmt": "{:.0f}", 'bbox_to_anchor':(1, 0.75)}
gdf_count.plot(column='cnt', cmap='Blues', legend=True, scheme='quantiles', legend_kwds=legend_kwds)
[13]:
<Axes: >
../_images/notebooks_Harmonize_CapeCod_Simple_23_1.png
[14]:
# Map average temperature results at each station
gdf_temperature = visualize.map_measure(df_harmonized, stations_gdf, 'Temperature')
gdf_temperature.plot(column='mean', cmap='OrRd', legend=True)
[14]:
<Axes: >
../_images/notebooks_Harmonize_CapeCod_Simple_24_1.png

Clean additional columns of data

[15]:
from harmonize_wq import clean
[16]:
df_cleaned = clean.datetime(df_harmonized)  # datetime
df_cleaned = clean.harmonize_depth(df_cleaned)  # Sample depth
/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/dataretrieval/utils.py:87: UserWarning: Warning: 167291 incomplete dates found, consider setting datetime_index to False.
  warnings.warn(

Reduce data to the columns that are most commonly needed

There are many columns in the dataframe that are characteristic specific, that is they have different values for the same sample depending on the characteristic. To ensure one result for each sample after the transformation of the data these columns must either be split, generating a new column for each characteristic with values, or moved out from the table if not being used.

[17]:
# Split single QA column into multiple by characteristic (rename the result to preserve these QA_flags)
df_expanded = wrangle.split_col(df_cleaned)
df_expanded
[17]:
OrganizationIdentifier OrganizationFormalName ActivityIdentifier ActivityStartDate ActivityStartTime/Time ActivityStartTime/TimeZoneCode MonitoringLocationIdentifier ResultIdentifier DataLoggerLine ResultDetectionConditionText ... QA_Secchi QA_pH QA_E_coli QA_Chlorophyll QA_Carbon QA_Conductivity QA_Temperature QA_DO QA_Sediment QA_Turbidity
0 BRC Blackstone River Coalition (Volunteer) BRC-C-02-02-020:20131012040800:FM:0.2083333333... 2013-10-12 04:08:00 -0500 BRC-C-02-02-020 STORET-1039097035 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 OARS OARS - For the Assabet Sudbury & Concord River... OARS-CND-161:20130818:0651:FM:1 2013-08-18 06:51:00 -0400 OARS-CND-161 STORET-838568413 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 OARS OARS - For the Assabet Sudbury & Concord River... OARS-SUD-064:20130721:0610:FM:1 2013-07-21 06:10:00 -0400 OARS-SUD-064 STORET-838568309 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 CRWA Charles River Watershed Association (Massachus... CRWA-CYN20130809ROBTemp01 2013-08-09 11:14:33 -0500 CRWA-ROB STORET-591631481 130809111433.0 NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 WTGHA Wompanoag Tribe of Gay Head Aquinnah (Tribal) WTGHA-m41:20131017:FM:B 2013-10-17 NaN NaN WTGHA-M41 STORET-1041401688 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
457247 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400495 2024-01-08 12:00:00 -0500 USGS-01098530 NWIS-126836828 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
457248 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 -0500 USGS-01097050 NWIS-126989046 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
457249 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 -0500 USGS-01097050 NWIS-126989056 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
457250 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 -0500 USGS-01097050 NWIS-126989063 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
457251 USGS-MA USGS Massachusetts Water Science Center nwisma.01.02400639 2024-02-12 08:45:00 -0500 USGS-01097050 NWIS-126989064 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

419173 rows × 115 columns

[18]:
# Split table into main with columns of interest and characteristic specific columns/metadata
main_df, chars_df = wrangle.split_table(df_expanded)
[19]:
# Drop empty columns (QA columns without flags)
main_df_small = main_df.dropna(axis=1, how='all')

Transform data from long to wide format

[20]:
transformed_df = wrangle.collapse_results(main_df_small)

Results are collapsed by retaining the first result that isn’t NAN. There can be several reasons for multiple results for the same parameter/characteristic sampled at the same station, time and by the same organization. The collapse_results function assumes the user has already reviewed the quality of all results and narrowed down instances of multiple results to only the desired/best/highest quality result before running this function.