"""Formation tops of the Volve wells: `Geophysical_Interpretations/Wells/Well_picks_Volve_v1.dat` as a table. The file is fixed-width text. Every well has a block: a `Well NO ` line, a header line, a line of dashes that gives the column positions, and one line per pick. `parse_picks` slices every pick line at the dash columns and checks it against the block header. The file states no units; depths agree with the DDR in metres. Published columns: `Well name`, `Surface name`, `Obs#` (a surface crossed more than once is numbered), `Qlf` (ER eroded, FP faulted pick, FO faulted out, NL not logged, NR not reached), `MD`, `TVD`, `TVDSS`, `TWT`, `Dip`, `Azi`, `Easting`, `Northing`, `Intrp`. Added columns: `wellbore_picks` (the published name without "NO "), `wellbore` (the DDR wellbore the pick belongs to, see `PICKS_TO_DDR`; None when the well has no DDR), `track`, `unit` and `boundary` (the surface name split at its trailing "Top"/"Base"). """ from __future__ import annotations import re from pathlib import Path import pandas as pd from .names import canonical COLUMNS = ["Well name", "Surface name", "Obs#", "Qlf", "MD", "TVD", "TVDSS", "TWT", "Dip", "Azi", "Easting", "Northing", "Intrp"] NUMERIC = ["MD", "TVD", "TVDSS", "TWT", "Dip", "Azi", "Easting", "Northing"] QUALIFIERS = {"ER": "eroded", "FP": "faulted pick", "FO": "faulted out", "NL": "not logged", "NR": "not reached"} #: Names of the picks file that are not DDR wellbore names -> (DDR wellbore, track). Evidence: #: #: * `15/9-19 SR` is the NPD name of the 1993 re-entry of 15/9-19 S (NPD FactPages, wellbore 2105: entered #: 17.02.1993, TD 4641 m MD; its lithostratigraphy lists Shetland Gp 3827, Draupne Fm 4304, Skagerrak Fm 4340 m, #: the MD of the picks). NPD and the DDR cut the same hole differently. NPD: `15/9-19 S` is the first hole plus #: its technical sidetrack from 1493 m down to 3580 m (FactPages 2043), `15/9-19 SR` deepened it to 4641 m. DDR: #: `15/9-19 S` is the first hole only (49 reports to 19.12.1992, deepest 3353 m, stuck and plugged back) and #: `15/9-19 ST2` is everything drilled after the sidetrack (127 reports, 19.12.1992-26.07.1997, to 4641 m). The #: picks below the sidetrack point (3614-4579 m MD) lie below the deepest point of DDR `15/9-19 S` and inside #: `15/9-19 ST2`. Both DDR wellbores carry NPD number 2043; the DDR never uses the name SR. #: * `15/9-F-12 pilot` is the 8 1/2" pilot hole of F-12 (to 1353 m, 23.06-05.07.2007, then plugged). The DDR #: reports it under `15/9-F-12` (reports with diaPilot = 8.5), so its picks map to that wellbore, track "pilot". PICKS_TO_DDR = { "15/9-19 SR": ("15/9-19 ST2", "main"), "15/9-F-12 pilot": ("15/9-F-12", "pilot"), } #: DDR wellbores without a block of their own: (wellbore, block they share the upper hole with, MD of the #: kick-off in metres). Picks of that block above the kick-off lie in the shared hole and apply to both. #: #: * `15/9-19 S`: `15/9-19 ST2` was sidetracked from it at 1493 m (DDR activity 15_9_19_ST2_1992_12_19:000, #: "SIDETRACKED ... FROM 1493M"; NPD FactPages 2043: "plugging back and sidetracking from 1493 m"). #: * `15/9-F-9 A` left F-9 through a window in the 13 3/8" casing; the DDR records parameters "above 13 3/8" #: casing window at 395 m" (activity 15_9_F_9_A_2009_07_05:011) and the WITSML trajectory of F-9 A starts at #: 420.4 m. The picks of F-9 are at 146, 146, 885 and 1071.2 m, so any kick-off between 146 and 885 m gives the #: same two shared picks; the DDR itself puts the Utsira Fm of F-9 A at 959 m against 885 m in F-9. SHARED_TRUNK = [ ("15/9-19 S", "15/9-19 SR", 1493.0), ("15/9-F-9 A", "15/9-F-9", 395.0), ] _WELL = re.compile(r"^Well (NO .+?)\s*$") _BOUNDARY = re.compile(r"^(.*?)\s+(Top|Base)$") def picks_wellbore(name: str) -> str: """'NO 15/9-F-12 pilot' -> '15/9-F-12 pilot'; names without a suffix word go through `canonical`.""" s = re.sub(r"\s+", " ", name.strip()) try: return canonical(s) except ValueError: return re.sub(r"^NO\s+", "", s) def parse_picks(path: Path, ddr_wellbores: set[str] | None = None) -> pd.DataFrame: """One row per pick, in file order. `ddr_wellbores` (canonical names) decides the `wellbore` column: a pick gets a DDR wellbore only if its mapped name is one; with None every mapped name is kept.""" rows = [] well = spans = None lines = path.read_text(encoding="ascii").splitlines() for no, line in enumerate(lines, 1): m = _WELL.match(line) if m: well, spans = m.group(1), None continue if not line.strip() or line.startswith("#"): continue if well is None: raise ValueError(f"{path.name}:{no}: text before the first well block") if line.strip().startswith("Well name"): header = line continue if set(line.strip()) <= {"-", " "}: starts = [m.start() for m in re.finditer(r"-+", line)] if len(starts) != len(COLUMNS): raise ValueError(f"{path.name}:{no}: {len(starts)} columns, expected {len(COLUMNS)}") spans = list(zip(starts, starts[1:] + [None])) got = [header[a:b].strip() for a, b in spans] if got != COLUMNS: raise ValueError(f"{path.name}:{no}: header {got}") continue if spans is None: raise ValueError(f"{path.name}:{no}: pick line before the column line") cells = [line[a:b].strip() for a, b in spans] row = dict(zip(COLUMNS, cells)) if row["Well name"] != well: raise ValueError(f"{path.name}:{no}: pick of {row['Well name']!r} in the block of {well!r}") row["line"] = no rows.append(row) df = pd.DataFrame(rows) for c in NUMERIC: df[c] = pd.to_numeric(df[c].replace("", None)) # raises on anything that is not a number df["Obs#"] = df["Obs#"].astype(int) df["Qlf"] = df["Qlf"].replace("", None) unknown = set(df["Qlf"].dropna()) - set(QUALIFIERS) if unknown: raise ValueError(f"unknown qualifiers: {unknown}") df["wellbore_picks"] = df["Well name"].map(picks_wellbore) mapped = df["wellbore_picks"].map(lambda w: PICKS_TO_DDR.get(w, (w, "main"))) df["wellbore"] = [w if ddr_wellbores is None or w in ddr_wellbores else None for w, _ in mapped] df["track"] = [t for _, t in mapped] split = df["Surface name"].str.extract(_BOUNDARY) df["unit"] = split[0].fillna(df["Surface name"]) df["boundary"] = split[1] return df[["wellbore", "wellbore_picks", "track", "Surface name", "unit", "boundary", "Obs#", "Qlf", "MD", "TVD", "TVDSS", "TWT", "Dip", "Azi", "Easting", "Northing", "Intrp", "line"]].rename( columns={"Surface name": "surface", "Obs#": "obs", "Qlf": "qlf", "MD": "md", "TVD": "tvd", "TVDSS": "tvdss", "TWT": "twt", "Dip": "dip", "Azi": "azi", "Easting": "easting", "Northing": "northing", "Intrp": "intrp"}) def shared_trunk_picks(picks: pd.DataFrame) -> pd.DataFrame: """Picks that a DDR wellbore without a block of its own shares with its mother bore (`SHARED_TRUNK`): the published rows above the kick-off, re-labelled with that wellbore and `inherited_from`.""" out = [] for wellbore, block, kickoff in SHARED_TRUNK: part = picks[(picks["wellbore_picks"] == block) & (picks["md"] < kickoff)].copy() part["wellbore"], part["inherited_from"], part["md_kickoff"] = wellbore, block, kickoff out.append(part) return pd.concat(out, ignore_index=True)