Datasets:
Download data/faith_hill_to_csv.py from anon-closure-challenge-v2/closure-challenge-v2-cfd-cases: direct link, hf CLI and curl.
- Browser
- Download file 5.53 kB
-
https://huggingface.co/datasets/anon-closure-challenge-v2/closure-challenge-v2-cfd-cases/resolve/main/data/faith_hill_to_csv.py
- Command line
-
hf download hf://datasets/anon-closure-challenge-v2/closure-challenge-v2-cfd-cases/data/faith_hill_to_csv.py
-
curl -L -o faith_hill_to_csv.py https://huggingface.co/datasets/anon-closure-challenge-v2/closure-challenge-v2-cfd-cases/resolve/main/data/faith_hill_to_csv.py
5.53 kB
| """Convert Faith Hill PIV / PSP / FISF data to flat CSV. | |
| Faith Hill source layout: | |
| - PIV: one folder per dataset (1kHz_3940samp / 2Hz_4000samps). Each folder has 14 .dat | |
| files in Tecplot POINT format, each file containing (x, y, single_scalar). Merge them | |
| on the (x, y) grid into one CSV per dataset. | |
| - PSP: single Tecplot-style file with "VARIABLES =" line and one zone -> 2-col CSV. | |
| - FISF: single Tecplot-style file with "Variables =" line, no zone -> N-col CSV. | |
| Coords in mm (PIV) or inches (PSP/FISF) per source. Hill height h = 152.4 mm = 6 in. | |
| Usage: | |
| python faith_hill_to_csv.py <piv-data-folder|psp.dat|fisf.dat> <output_csv> | |
| """ | |
| import os | |
| import re | |
| import sys | |
| import glob | |
| import numpy as np | |
| import pandas as pd | |
| _SCALAR_FROM_NAME = { | |
| 'U_mean': 'U_mean', | |
| 'V_mean': 'V_mean', | |
| 'W_mean': 'W_mean', | |
| 'U_rms': 'U_rms', | |
| 'V_rms': 'V_rms', | |
| 'W_rms': 'W_rms', | |
| 'Re_stress_UU': 'UU', | |
| 'Re_stress_UV': 'UV', | |
| 'Re_stress_UW': 'UW', | |
| 'Re_stress_VV': 'VV', | |
| 'Re_stress_VW': 'VW', | |
| 'Re_stress_WW': 'WW', | |
| 'Ek_Ave': 'Ek_Ave', | |
| 'Ek_turb': 'Ek_turb', | |
| } | |
| _NUMERIC_RE = re.compile(r'^\s*[-+]?\d') | |
| def _parse_tecplot_point(path): | |
| """Parse a Tecplot POINT-format .dat file -> (x, y, value) arrays. | |
| Robust to multi-line headers: skips every line until the first one whose | |
| first non-whitespace character is a digit or sign. | |
| """ | |
| with open(path) as f: | |
| lines = f.readlines() | |
| data_start = 0 | |
| for i, line in enumerate(lines): | |
| if _NUMERIC_RE.match(line): | |
| data_start = i | |
| break | |
| else: | |
| raise RuntimeError(f'No numeric data found in {path}') | |
| arr = np.genfromtxt(lines[data_start:], dtype=float) | |
| if arr.ndim == 1: | |
| arr = arr.reshape(1, -1) | |
| if arr.shape[1] < 3: | |
| raise RuntimeError(f'{path}: expected >=3 columns, got {arr.shape[1]}') | |
| return arr[:, 0], arr[:, 1], arr[:, 2] | |
| def merge_piv_dataset(folder, out_csv): | |
| """Merge 14 Tecplot-POINT files in `folder` into one CSV by (x, y) grid.""" | |
| dat_files = [p for p in sorted(glob.glob(os.path.join(folder, '*.dat'))) | |
| if not os.path.basename(p).startswith('._')] | |
| if not dat_files: | |
| raise RuntimeError(f'No .dat in {folder}') | |
| df = None | |
| for path in dat_files: | |
| base = os.path.basename(path).replace('.dat', '').replace('_axis00', '') | |
| col = _SCALAR_FROM_NAME.get(base, base) | |
| x, y, v = _parse_tecplot_point(path) | |
| cur = pd.DataFrame({'x_mm': x, 'y_mm': y, col: v}) | |
| if df is None: | |
| df = cur | |
| else: | |
| df = pd.merge(df, cur, on=['x_mm', 'y_mm'], how='outer') | |
| # add k = 0.5 * (UU + VV + WW) | |
| if all(c in df.columns for c in ['UU', 'VV', 'WW']): | |
| df['k'] = 0.5 * (df['UU'] + df['VV'] + df['WW']) | |
| df = df.sort_values(['y_mm', 'x_mm']).reset_index(drop=True) | |
| df.to_csv(out_csv, index=False) | |
| print(f' PIV: {os.path.basename(folder)} -> {os.path.basename(out_csv)} ' | |
| f'({len(df)} rows, {len(df.columns)} cols)') | |
| def convert_psp(path, out_csv): | |
| """Parse PSP centerline .dat (Tecplot single-zone, 2 columns).""" | |
| with open(path) as f: | |
| content = f.read() | |
| var_match = re.search(r'VARIABLES\s*=\s*([^\n]+)', content, re.IGNORECASE) | |
| cols = [] | |
| if var_match: | |
| cols = re.findall(r'"([^"]+)"', var_match.group(1)) | |
| arr = np.genfromtxt(path, comments=None, | |
| skip_header=sum(1 for _ in re.findall( | |
| r'^(?:VARIABLES|TITLE|ZONE|zone|#)[^\n]*\n', content, re.MULTILINE))) | |
| # safer: parse line-by-line | |
| lines = content.split('\n') | |
| data_start = 0 | |
| for i, line in enumerate(lines): | |
| s = line.strip().lower() | |
| if s.startswith('zone') or s.startswith('#'): | |
| data_start = i + 1 | |
| elif s.startswith('variables') or s.startswith('title'): | |
| data_start = i + 1 | |
| arr = np.genfromtxt(lines[data_start:], dtype=float) | |
| if arr.ndim == 1: | |
| arr = arr.reshape(1, -1) | |
| if not cols or len(cols) != arr.shape[1]: | |
| cols = [f'col_{i}' for i in range(arr.shape[1])] | |
| # rename common columns | |
| rename = {'X': 'x_in', 'F1V1': 'Cp'} | |
| cols = [rename.get(c, c) for c in cols] | |
| df = pd.DataFrame(arr, columns=cols) | |
| df.to_csv(out_csv, index=False) | |
| print(f' PSP: {os.path.basename(path)} -> {os.path.basename(out_csv)} ' | |
| f'({len(df)} rows, {len(df.columns)} cols)') | |
| def convert_fisf(path, out_csv): | |
| """Parse FISF surface data ("Variables =..." header, N-col data).""" | |
| with open(path) as f: | |
| first_line = f.readline() | |
| cols = re.findall(r'"([^"]+)"', first_line) | |
| arr = np.genfromtxt(path, skip_header=1, dtype=float) | |
| if arr.ndim == 1: | |
| arr = arr.reshape(1, -1) | |
| if not cols or len(cols) != arr.shape[1]: | |
| cols = [f'col_{i}' for i in range(arr.shape[1])] | |
| df = pd.DataFrame(arr, columns=cols) | |
| df.to_csv(out_csv, index=False) | |
| print(f' FISF: {os.path.basename(path)} -> {os.path.basename(out_csv)} ' | |
| f'({len(df)} rows, {len(df.columns)} cols)') | |
| if __name__ == '__main__': | |
| if len(sys.argv) != 3: | |
| print(__doc__) | |
| sys.exit(1) | |
| src, dst = sys.argv[1], sys.argv[2] | |
| if os.path.isdir(src): | |
| merge_piv_dataset(src, dst) | |
| elif 'centerline_p150' in src.lower() or 'psp' in src.lower(): | |
| convert_psp(src, dst) | |
| elif 'fisf' in src.lower(): | |
| convert_fisf(src, dst) | |
| else: | |
| print(f'Unknown source type: {src}') | |
| sys.exit(1) | |