Skip to content

Instantly share code, notes, and snippets.

@sgrodnik
Created May 20, 2025 12:34
Show Gist options
  • Save sgrodnik/00647b2e024cad0e361a885ac1e434ed to your computer and use it in GitHub Desktop.
Save sgrodnik/00647b2e024cad0e361a885ac1e434ed to your computer and use it in GitHub Desktop.
r"""
extract_pressures_by_time.py
Extracts SECTION-PRESSURE pairs from a .PRN-like simulation file based on a specified TIME block.
The script:
- Searches for the block of data starting after the line:
TIME <value>.00 SECONDS
and the next:
SECTION PRESSURE CHANGES ( SECTION NUMBER AND TOTAL PRESSURE CHANGE - IN. WG )
- Stops at the line starting with:
SYSTEM SENSIBLE LATENT ...
- Filters lines with valid numeric data (ignoring empty lines and headers)
- Converts 8 SECTION-PRESSURE pairs per line into individual rows
- Sorts the result by SECTION number
- Saves the extracted data to a CSV file
Usage:
python extract_pressures_by_time.py --file <path_to_file> --time <TIME_value> --output <output_csv>
s
Example:
python extract_pressures_by_time.py --file "les_CFD 2.PRN" --time 300 --output pressures_300.csv
python "C:\Users\1M06174\Downloads\extract_pressures_by_time.py" --file "C:\Users\1M06174\Downloads\les_CFD 2.PRN" --time 300 --output "C:\Users\1M06174\Downloads\pressures_300.csv"
"""
import re
import csv
import argparse
from pathlib import Path
# Argument parsing
parser = argparse.ArgumentParser(description="Extract SECTION PRESSURE data by TIME from PRN-like file.")
parser.add_argument("--file", required=True, type=Path, help="Path to input .PRN file")
parser.add_argument("--time", type=float, required=True, help="Target TIME value (e.g., 300)")
parser.add_argument("--output", type=Path, default="output.csv", help="Output CSV file name")
args = parser.parse_args()
file_path = args.file
target_time = args.time
output_csv = args.output
# Read file
with file_path.open("r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
# Find line with TIME <value>.00 SECONDS
start_time_idx = next((i for i, line in enumerate(lines) if f"TIME {target_time:.2f} SECONDS" in line), None)
# Find SECTION PRESSURE CHANGES line after TIME
pressure_header_idx = None
for i in range(start_time_idx + 1, len(lines)):
if "SECTION PRESSURE CHANGES" in lines[i]:
pressure_header_idx = i
break
# Find SYSTEM ... LATENT line after pressure header
system_end_idx = None
for i in range(pressure_header_idx + 1, len(lines)):
if re.search(r"SYSTEM\s+SENSIBLE\s+LATENT", lines[i]):
system_end_idx = i
break
# Extract and parse valid SECTION PRESSURE pairs
valid_pairs = []
for line in lines[pressure_header_idx + 1:system_end_idx]:
if not line.strip():
continue
if re.match(r"^\s+[A-Za-z]", line):
continue
if re.match(r"^\s+\d", line):
tokens = line.strip().split()
if len(tokens) % 2 == 0:
for i in range(0, len(tokens), 2):
section = tokens[i]
pressure = tokens[i + 1]
valid_pairs.append((int(section), float(pressure)))
# Sort by SECTION number
valid_pairs.sort(key=lambda x: x[0])
# Save to CSV
with output_csv.open("w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["SECTION", "PRESSURE"])
for pair in valid_pairs:
writer.writerow(pair)
print(f"Time {target_time}: extracted {len(valid_pairs)} SECTION PRESSURE pairs ({valid_pairs[0][0]}...{valid_pairs[-1][0]}) to {output_csv}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment