feat(geothermal): normalize OGC view temperatures to Celsius - #811
Merged
Conversation
The per-well geothermal views from d1e2f3a4b5c6 published legacy
temperatures unconverted and labelled them with max("TempUnit"), which
picks a unit lexically. A well with both C and F readings was labelled
'F' while values stayed mixed, and min/max aggregated across units.
Add public.nmw_temp_unit_code() and public.nmw_temp_to_c(), then rebuild
ogc_geothermal_wells_bht and ogc_geothermal_wells_temperature_profile
with min/max_bht_c, min/max_temp_c, a constant 'C' temp_unit, a
temp_unit_source listing the distinct source units, a temp_unit_mixed
flag, and an unconvertible_count. Raw columns and the series 'temp' key
are unchanged for compatibility; series objects gain temp_c and
temp_unit_source.
Function references are schema-qualified because materialized view
population runs with a restricted search_path, which breaks inlining of
an unqualified nested call.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Adds coverage for the f3a1c2b4d5e6 helpers and views: unit-code canonicalization across single-letter and spelled-out legacy forms, F/C/K conversion plus the NULL-on-unknown-unit contract, presence of the Celsius columns on both temperature views, and that temp_unit publishes the constant 'C'. Matview columns are read from pg_attribute because materialized views do not appear in information_schema.columns. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The per-well geothermal views created in
d1e2f3a4b5c6publish legacy temperatures unconverted and label them withmax("TempUnit"). Two consequences:max()picks a unit lexically ('F' > 'C'), so a well holding both C and F readings is labelled'F'while the values stay mixed.min("Temp")/max("Temp")aggregate across those mixed units — 100 F sorts above 40 C, so the published extremes are meaningless.Nothing downstream compensates: pygeoapi serves the view columns as-is.
Change
New migration
f3a1c2b4d5e6(revises2d3c3a268652, single head) adds two helpers and rebuilds the two temperature views.Helpers:
public.nmw_temp_unit_code(text) -> text— canonicalizes a legacy unit to'C'/'F'/'K', or NULL when unrecognized.NMW_GtTempDepths."TempUnit"isString(1)andNMW_GtBhtData."TempUnit"isString(5), so both single-letter and spelled-out forms are accepted.public.nmw_temp_to_c(double precision, text) -> double precision— converts using that code. Unrecognized unit returns NULL rather than assuming a default, so bad rows are visible instead of silently wrong.ogc_geothermal_wells_bhtandogc_geothermal_wells_temperature_profilegain:min_bht_c/max_bht_c,min_temp_c/max_temp_c— aggregated over normalized values. These are the columns to chart.temp_unit— now the constant'C', describing the*_ccolumns.temp_unit_source— the distinct source units actually present for the well (C,F,C,F,UNKNOWN, ...).temp_unit_mixed— flags wells mixing units.unconvertible_count— readings whose unit was not recognized.seriesobjects gaintemp_candtemp_unit_source.Raw columns (
min_bht/max_bht,min_temp/max_temp, theseriestempkey) are unchanged for compatibility. They remain mixed-unit.Verification
Ran upgrade + downgrade against a local dev database inside a rolled-back transaction — both clean, nothing persisted. Spot checks:
212 F -> 100,40 C -> 40,273.15 K -> 0, bogus unit -> NULL, NULL unit -> NULL. Sample wells (allFin dev data) convert correctly:max_bht 138.9 -> 59.39.One bug surfaced during that run and is fixed here: materialized view population runs with a restricted
search_path, so inliningnmw_temp_to_cfailed on its unqualified nested call (ERROR: function nmw_temp_unit_code(text) does not exist). All function references are schema-qualified. NoSET search_pathon the functions, which would disable inlining across the ~370k-row source.Reviewer notes
temp_unit— it now returns'C'instead of the raw legacy code. Raw codes moved totemp_unit_source.WITH DATA; expect the usual matview build cost againstNMW_GtTempDepths.HtFlowUnit,GradUnit,TCondUnit,Q_unit,Kpr_unit,Ka_unit) and depth units are not normalized — the summary and interval heat-flow views are untouched. Worth a follow-up.SPEC.mdlists these views but gains no invariant for unit normalization here.🤖 Generated with Claude Code