Files
TJWaterServer/epanet/fix_valve_ids2.py
2025-12-31 16:11:28 +08:00

66 lines
1.8 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
import re
inp = Path(r"d:\TJWaterServer\epanet\szhskeleton-patternfixed-ascii.inp")
text = inp.read_text(encoding='utf-8')
lines = text.splitlines()
start = None
for i,l in enumerate(lines):
if l.strip().upper() == '[VALVES]':
start = i
break
if start is None:
print('No [VALVES] section found')
raise SystemExit(1)
# collect until next section header or EOF
end = len(lines)
for j in range(start+1, len(lines)):
if re.match(r"^\s*\[.+\]", lines[j]):
end = j
break
block_lines = lines[start+1:end]
ids = []
for idx,l in enumerate(block_lines, start=start+1):
if not l.strip() or l.strip().startswith(';'):
continue
# first token
tok = l.split()[0]
ids.append((idx, tok, l))
from collections import defaultdict
count = defaultdict(list)
for ln, tok, l in ids:
count[tok].append(ln)
dups = {k:v for k,v in count.items() if len(v)>1}
print('Total valve entries found:', len(ids))
print('Duplicate token count:', len(dups))
if dups:
print('\nSample duplicates:')
for k,v in list(dups.items())[:20]:
print(k, 'lines:', v)
# show whether tokens are purely digits
num_only = [tok for ln,tok,l in ids if re.fullmatch(r'\d+', tok)]
print('\nNumeric-only valve IDs count:', len(num_only))
# show examples of numeric-only
if num_only:
print('Examples:', num_only[:20])
# write a short report
rep = inp.with_name(inp.stem + '-valves-report.txt')
with rep.open('w', encoding='utf-8') as f:
f.write(f'Total valve entries: {len(ids)}\n')
f.write(f'Duplicate tokens: {len(dups)}\n')
for k,v in dups.items():
f.write(f'{k}: lines {v}\n')
f.write('\nNumeric-only tokens:\n')
for tok in sorted(set(num_only)):
f.write(tok + '\n')
print('Wrote report to', rep)