Pipeline Cathodic Protection Voltage Diagnostic Script
Generate a Python diagnostic script to audit natural gas pipeline rectifier test points against NACE standards.
Use this prompt when deploying automated cathodic protection compliance monitoring for pipeline infrastructure. It polls rectifier telemetry, flags under-protection or hydrogen embrittlement risks, and logs audit-ready reports.
Role: Senior Pipeline Integrity Automation Specialist specializing in cathodic protection (CP) regulatory compliance.
Context
- Infrastructure Segment: {{pipeline_segment_id}}
- Telemetry Interface: {{rectifier_telemetry_endpoint}}
- Minimum Negative Potential Bound: {{min_protective_potential_mv}} mV CSE
- Overprotection Safety Threshold: {{max_overprotection_potential_mv}} mV CSE
- Polling Cycle Frequency: {{scan_interval_seconds}} seconds
- Audit Log Destination: {{incident_log_file}}
Task
Author a production-ready Python automation script that continuously audits cathodic protection voltage telemetry from field rectifiers along a pipeline asset, classifies corrosion risks against industry thresholds, and records tamper-resistant compliance logs.
Method
- Initialize polling loops hitting {{rectifier_telemetry_endpoint}} every {{scan_interval_seconds}} seconds with HTTP session pooling.
- Ingest instant-off and on-potential DC pipe-to-soil voltage metrics across active test stations on {{pipeline_segment_id}}.
- Validate voltage polarity conventions ensuring negative potentials are evaluated relative to copper-copper sulfate electrodes (CSE).
- Compare instant-off readings against {{min_protective_potential_mv}} mV to detect inadequate corrosion protection.
- Evaluate readings against {{max_overprotection_potential_mv}} mV to identify coating disbondment and hydrogen overvoltage hazards.
- Assign status categories per test point (Normal Protection, Under-Protected, Over-Protected, Sensor Open Circuit).
- Write structured JSON log lines for every detected violation directly to {{incident_log_file}} with atomic file locking.
Constraints
- MUST handle negative voltage integer comparisons correctly where -800 mV is less protective than -850 mV.
- MUST NOT terminate execution loop if a single rectifier test station returns a timeout or null payload.
- Output script must rely strictly on standard library modules or common industrial drivers (urllib3, json, logging).
- File writes to {{incident_log_file}} MUST use POSIX non-blocking file locks to avoid race conditions.
Output format
Format the response into three distinct parts:
- Script Setup: Brief summary of polling parameters and expected telemetry JSON schemas.
- Diagnostic Python Script: The complete, runnable Python audit script in a code block.
- Sample Log Output: An exact three-line mock of the JSON log entries generated inside {{incident_log_file}}.
Self-review
- Confirm that negative voltage thresholds ({{min_protective_potential_mv}} and {{max_overprotection_potential_mv}}) are compared with correct polarity math.
- Ensure {{pipeline_segment_id}} is included in all alert logging schemas.
- Verify that timing sleeps strictly match {{scan_interval_seconds}}.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.