| import re |
|
|
| def normalize_well_name(raw_name: str) -> str: |
| """ |
| Normalizes well names from various sources (WITSML, DDR, EDM) into a canonical format. |
| E.g.: |
| "15/9-F-5 W-508420" -> "15/9-F-5" |
| "NO 15/9-F-1 C 1bf1cc58-83af-4e13-9696-4fae2f9294ae" -> "15/9-F-1 C" |
| "15-9-F-1" -> "15/9-F-1" |
| "15_9-F-1" -> "15/9-F-1" |
| "15_9_F_1_C" -> "15/9-F-1 C" |
| """ |
| if not isinstance(raw_name, str) or not raw_name.strip(): |
| return "UNKNOWN" |
| |
| s = raw_name.strip() |
| |
| |
| s = re.sub(r'^NO[\s\-]+', '', s, flags=re.IGNORECASE) |
| |
| |
| |
| if " " in s: |
| s = s.split(" ")[0] |
| |
| |
| s = re.sub(r'^(\d+)[_\-](\d+)', r'\1/\2', s) |
| |
| |
| if '_' in s and '/' in s: |
| |
| parts = re.split(r'[_\-]+', s) |
| if len(parts) >= 3: |
| |
| base = f"{parts[0]}-{parts[1]}-{parts[2]}" |
| if len(parts) > 3: |
| base += f" {' '.join(parts[3:])}" |
| s = base |
| |
| |
| s = re.sub(r'_([A-Z])$', r' \1', s) |
| |
| s = re.sub(r'_(ST\d+|T\d+)$', r' \1', s) |
| |
| |
| |
| s = s.replace('_', ' ') |
| |
| |
| s = re.sub(r'\s+', ' ', s) |
| |
| return s.strip() |
|
|
| def safe_filename(name: str) -> str: |
| """Converts a canonical name to a safe filename string.""" |
| return name.replace("/", "_").replace(" ", "_").replace("-", "_") |
|
|
|
|