Skip to content

refactor(edr): refactor EDR config dicts and provider (BDMS-1602) - #1060

Draft
jacob-a-brown wants to merge 42 commits into
stagingfrom
BDMS-1602-pygeoapi-edr-configuration-provider-refactor
Draft

jacob-a-brown wants to merge 42 commits into
stagingfrom
BDMS-1602-pygeoapi-edr-configuration-provider-refactor

Conversation

@jacob-a-brown

Copy link
Copy Markdown
Contributor

Why

This PR addresses the following problem / context:

  • The quasi-bespoke EDR provider and the config dicts that it read were able to to work if each row had a single parameter column, value column, and unit column. Now, however, each row may have multiple sets of parameter/value/unit columns and each set of columns may contain multiple parameters (think water chemistry with a multitude of analytes).
  • While water chemistry and water levels are time series, we will eventually serve data across a different independent axis, like depth or height.

How

Implementation summary: the following was changed / added / removed:

core/pygeoapi.py

  • Added the following keys to the EDR_COLLECTIONS dictionaries:
    • parameter_columns: describes the sets of columns for parameter names, units, and values. This enables the provider to have multiple parameters per row
    • station_properties: the properties of a station (e.g. site) that go into the properties for each feature in the feature class (queried at /locations)
    • independent_axis: the name of the independent axis (e.g. t or z) and the column of the view where those values reside

core/edr_provider.py

  • Use the parameter dictionaries from EDR_COLLECTIONS to create both the FeatureCollection from /locations and the CoverageCollection from all other endpoints in the collection (e.g. /area, /cube, /locations/{locId})
  • Because parameters are discovered from the data we have to loop through each parameter set in parameter_columns to get relevant data.
  • Because each range in a coverage must be the same length if a parameter does not exist at time stamp i then its value is None at that index. IMPORTANT: for water level data there is only one observed parameter and a qualifier. Each exists in the same row. This means that there will always be the same number of observations and qualifiers, even if one of them is None in the database. When making the water chemistry EDR the provider will need to be updated to set the associated qualifier to an appropriate message (such as "Parameter not analyzed for at this datetime. Value is set to None." Message TBD. Doing this is out of scope for this work and will be implemented when working on water chemistry EDR.)
  • I changed the name to StationEDRProvider since this will eventually be used for vertical data. We can, if we want, have the following setup instead: BaseStationEDRProvider that defines common methods and then a PointSeriesEDRProvider for time series data and VerticalProfileEDRProvider for vertical data. Thoughts? See CoverageJSON docs for the different domain types.

Author Checklist

  • Changes are limited to the intended scope.
  • Tests were added or updated where appropriate.
  • Relevant tests and lint checks pass.
  • No secrets, credentials, or sensitive data were publicly exposed.
  • Documentation was updated where necessary.

Reviewer Checklist

  • Use checkboxes here

Notes

Any special considerations, known limitations, risks, workarounds, or follow-up work to note?

  • 🚨 This PR includes tests that depend on the ogc_manual_waterlevels view. The original intention was to have all of this work in one PR, but after yesterday's meeting the EDR refactor and the view construction were split up into separate tickets. There are, therefore, some tests that fail. I don't want to get rid of them, though, because I think that they are important and do a good job describing what needs to occur. My intention is to open a stacked PR. In the next PR these tests, and others, will all pass. That means that that PR will need to get merged here first for all tests to pass. Then all of the work can get merged into staging.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

✅ 82.25% total — gate is 75%.

Coverage for the Python files changed in this PR
Name Stmts Miss Cover Missing
api/gis_artifacts.py 63 2 97% 165-171
core/edr_provider.py 220 45 80% 102, 138, 150-152, 175-185, 198, 276-281, 311, 327, 355-361, 367-373, 379-385, 408-414, 432-440, 442-443, 445-446, 448-453, 529, 675-680, 683
core/pygeoapi.py 198 20 90% 367, 371, 380, 385, 404, 411, 436-439, 586, 591, 679, 816, 834, 841, 843, 863, 865, 877
TOTAL 481 67 86%

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical location-query regressions break manual-waterlevels and existing chemistry endpoints, while CoverageJSON types copied into JSON Schema make the published schema invalid.

8 open issues
What changed in this PR

Refactors OcotilloAPI’s environmental data retrieval (EDR) provider to support multiple parameters per row and configurable station metadata, while preparing for independent axes beyond time.

Changes:

  • Renames the provider to StationEDRProvider and adds configurable parameter groups, station properties, and axis settings.
  • Replaces the combined water-level collection configuration with manual_waterlevels, including measurement qualifiers.
  • Expands provider, metadata, view-parity, and integration tests; some depend on the planned stacked view migration.
File Description
tests/​test_ogc.py Moves the water-level publication regression test out of the general suite.
tests/​test_ogc_manual_waterlevels.py Adds manual-waterlevel response, publication, and view-contract tests.
tests/​test_ogc_field_metadata.py Changes numeric metadata test inputs to float.
tests/​test_ogc_field_descriptions.py Updates EDR metadata tests for multiple parameters and configurable axes.
tests/​test_migration_view_parity.py Checks deployed public/internal views and configured EDR relations.
tests/​test_edr_provider.py Adds parameter selection, null alignment, and configuration propagation tests.
tests/​features/​steps/​edr_water_data.py Updates behavior-test steps for manual water levels.
tests/​features/​edr-water-data.feature Revises scenarios for manual measurements and qualifiers.
tests/​conftest.py Adds formation metadata and a database-free provider fixture.
core/​pygeoapi.py Defines and forwards the expanded EDR collection configuration.
core/​ogc-field-descriptions.yml Removes the former water-level parameter documentation.
core/​gis-curated-layers.yml Updates a comment to use the new collection name.
core/​edr_provider.py Implements configurable parameter discovery, filtering, and coverage generation.

Review effort: Balanced

Comment thread core/edr_provider.py Resolved
Comment thread core/edr_provider.py Resolved
Comment thread core/pygeoapi.py Resolved
Comment thread core/edr_provider.py Resolved
Comment thread core/ogc-field-descriptions.yml Outdated
Comment on lines +2666 to +2667
# EDR collection parameters are discoverable from the data, so they are not hardcoded
# here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is yet another thing that was discovered and addressed while working on the backing view. The schema now builds the descriptions from the parameter itself. There are no verbose explanations, but because we want to "discover" parameters from the data (particularly for water chemistry) we can't have a fixed list. @jirhiker are you okay with this approach? If not we'll have to hardcode all parameters and their descriptions, which means that discovery can't occur from the data itself.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use this YAML file rather than the lexicon (I answered the same question on #1062). Discovering parameters from the data still works. _parameter_documentation already looks each discovered name up in ogc-field-descriptions.yml, and it only falls back to the raw name when there's no entry. So a fixed list isn't required: documented parameters get proper labels, and undocumented ones still show up under their names.

A lexicon lookup wouldn't help much either. The published names (e.g. groundwater level below ground surface) aren't lexicon terms.

For manual_waterlevels, please add entries for groundwater level below ground surface and groundwater level reason, using the wording from docs/ogc-waterlevels-data-contract.md. For chemistry, add analytes as they get documented, and the fallback covers the rest.


Generated by Claude Code

Comment thread tests/conftest.py Resolved
Comment thread tests/test_ogc_manual_waterlevels.py Resolved
Comment thread tests/test_ogc_manual_waterlevels.py Resolved

@jirhiker jirhiker left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stacking plan makes sense. The failing ogc_manual_waterlevels tests are expected until the view PR lands, and keeping them as the spec is reasonable. Since this PR targets staging, I'd mark it draft or add a do-not-merge label until the view PR has merged into this branch. If it merges early, /collections/waterlevels disappears and the new collection fails on every query.

Separate from the view, the inline comments cover issues that this PR has to fix itself. The main one is the /locations KeyError on core/edr_provider.py L300. The view PR won't fix it, and it'll probably surface as a failure in test_ogc_manual_waterlevel_feature_collection_shape once the view exists.

Generated by Claude Code

Comment thread core/edr_provider.py Resolved
Comment thread core/edr_provider.py Resolved
Comment thread core/edr_provider.py Resolved
Comment thread core/edr_provider.py Resolved
Comment thread core/edr_provider.py
"values": [None] * len(independent_axis_raw_values),
}

ranges[name]["values"][i] = r[parameter_column_set["value_column"]]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If two rows for a station share the same axis value (for example, two manual measurements recorded at the same datetime), the later row overwrites the earlier one here and the first reading disappears from the coverage without a warning. The old provider did this too, so it isn't a regression, but it now applies to every parameter set. Even a LOGGER.warning on collision would help.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a LOGGER.warning on collision and am keeping the existing value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that the second value should be kept? Or both of them? If both I'll figure out a way to do that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping both isn't an option. A CoverageJSON axis can't repeat a value, so two readings at the same datetime can't both go in one range. Keep one, but there's a bug in how the current check picks it.

The check runs separately for each parameter column and only fires when the existing value is non-null. Take two readings for the same well and datetime:

row observation_value qualifier_value
A NULL Site was unsafe to measure
B 50 Water level not affected

observation_value for A is None, so B's 50 replaces it with no warning. qualifier_value for A is non-null, so A's reason stays. The coverage then publishes 50 with "Site was unsafe to measure", a value and reason that came from different readings.

Which row arrives first is also random. _fetch_rows sorts by thing_id, <axis> only, so tied rows come back in whatever order Postgres picks.

Suggested fix:

  1. Add a tiebreaker to the ORDER BY: ORDER BY thing_id, {axis}, id.
  2. Decide keep-or-skip once per row, before the parameter_columns loop. Keep a seen set of axis indexes. If i has already been filled, log once and skip the whole row. A row's value and its qualifier then always stay together, and the lowest id wins every time.

Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - making this update now

Comment thread core/edr_provider.py Resolved
Comment thread core/pygeoapi.py Resolved
Comment thread tests/test_ogc_manual_waterlevels.py Resolved
@jacob-a-brown
jacob-a-brown marked this pull request as draft November 18, 2026 18:57

@jirhiker jirhiker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a second pass over the test files, which the first pass only skimmed. It leaves out anything already raised by Copilot or the first pass. The one real bug is the BDD Background assert that only runs when the catalog returns 200. The rest are test contradictions and nits.

One more item from the first pass is worth repeating: Copilot's station_name comment on core/pygeoapi.py L343 is as serious as the /locations KeyError. water_chemistry/locations breaks in this PR, and the view PR won't fix it.


Generated by Claude Code

Comment thread tests/features/steps/edr_water_data.py Resolved
Comment thread tests/test_ogc_field_descriptions.py Resolved
Comment thread tests/test_ogc_manual_waterlevels.py Resolved
Comment thread tests/test_ogc_manual_waterlevels.py Resolved
Comment thread tests/features/steps/edr_water_data.py Resolved

Copy link
Copy Markdown
Member

Two items from resolved threads are still open on both this branch and #1062:

  1. _has_column is dead code. It's at core/edr_provider.py:165 here (L193 on refactor(edr): create and update backing views (BDMS-1599) #1062), and nothing calls it now that _has_thing_type is gone. The docstring part of that thread was fixed, but the method is still there.
  2. ADR3.md:286-289 still documents the old collection. It shows GET /ogcapi/collections/waterlevels/locations/{thingId} and .../waterlevels/instances, and both will 404 after the rename. gis_artifacts.py and the data contract were updated, but the ADR wasn't. Update it to manual_waterlevels, and either drop the /instances example or mark it as pending transducer EDR.

Either can go in #1062 if that's easier, since that's merging into this branch.


Generated by Claude Code

@jacob-a-brown

Copy link
Copy Markdown
Contributor Author
  1. _has_thing_type was removed in refactor(edr): create and update backing views (BDMS-1599) #1062. This occurred as I worked on the backing views. To prevent merge conflicts I'd prefer to just keep this update in that branch and then it'll get fixed once that gets merged here.
  2. I'll make the update to ADR3.md and push that to this PR.

@jacob-a-brown

Copy link
Copy Markdown
Contributor Author

@jirhiker I just pushed an update for the ADR and it's ready for review/comments

@jacob-a-brown jacob-a-brown changed the title refactor(Edr): refacto EDR config dicts and provider (BDMS-1602) refactor(edr): refactor EDR config dicts and provider (BDMS-1602) Nov 23, 2026
@jacob-a-brown

Copy link
Copy Markdown
Contributor Author

@jirhiker and @ksmuczynski

Parameters are now hard-coded and described in core/ogc-field-descriptions.yml. If a parameter does not exist there the EDR provider defaults to the parameter name for the description and title. It then logs that this occurs at a warning level.

@jacob-a-brown

jacob-a-brown commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@jirhiker and @ksmuczynski

I ran into two issues during testing and validation that prevented this from being implemented correctly:

  1. A broken EDR provider prohibited all EDR providers from being published in the OpenAPI documentation; and
  2. A parameter with multiple units blindly defaulted to the first unit it came across, regardless of if it was or was not the right one.

The fixes, as seen in the last 2 commits (drop unloadable EDR collections & enforce only one unit per parameter), address these issues as follows:

  1. If there is a broken EDR collection, for whatever reason, it will be dropped from the OpenAPI documentation and the other collections will still be published.
  2. If a parameter has records with multiple units, such as mg/L and mg/L as NO3, that parameter is dropped from the collection. The collection itself is not removed. One of my big lessons from AMPAPI was to not accommodate bad data in the API. The API, I believe, should be strict in its enforcement and validation. If we accommodate bad data then in the end we are just masking an issue. Data ought to be corrected and normalized. If a parameter is dropped from a collection it will get logged with a warning level and we can make corrections as needed.

Neither of these problems are issues with water level data since they get normalized in the backing view. They are, however, problems with water chemistry data (the broken water chemistry collection was, for example, preventing the manual waterlevels collection from publishing in the OpenAPI documentation). My recommendation for when we create the water chemistry backing view is as follows: normalize it! Make sure that every parameter has one and only one unit. This means that we may need to get together with AMMP colleagues to determine which units we want reported (in some cases), and what to do about null units (assume that they're reported like every other one, such as mg/L?). While this isn't a fix of the data itself, this information will provide a blueprint for the migration from NMA to Ocotillo since we can use the same normalizations.

Thoughts/opinions?

(See screenshot for the dropped parameters and the logged warnings)

Screenshot 2026-11-24 at 2 06 03 PM

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants