Datasets:
Download data/dat_to_csv.py from anon-closure-challenge-v2/closure-challenge-v2-cfd-cases: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/datasets/anon-closure-challenge-v2/closure-challenge-v2-cfd-cases/resolve/main/data/dat_to_csv.py
- Command line
-
hf download hf://datasets/anon-closure-challenge-v2/closure-challenge-v2-cfd-cases/data/dat_to_csv.py
-
curl -L -o dat_to_csv.py https://huggingface.co/datasets/anon-closure-challenge-v2/closure-challenge-v2-cfd-cases/resolve/main/data/dat_to_csv.py
3.12 kB
| """Convert NASA Tecplot-zone .dat files to flat CSV. | |
| Each .dat file in a given directory is read via the same parser used in | |
| the original NASA challenge submission (readNasaZoneFile), then flattened | |
| to a single CSV with one row per (zone, sample) and a `zone` column to | |
| preserve the multi-zone structure. | |
| Usage: | |
| python dat_to_csv.py <dir1> [<dir2> ...] | |
| """ | |
| import sys | |
| import os | |
| import glob | |
| import numpy as np | |
| import pandas as pd | |
| def _text_in_line(line): | |
| if line == '': | |
| return False | |
| return max( | |
| [(ord(c) > 96) & (ord(c) < 101) | (ord(c) > 101) & (ord(c) < 123) | |
| for c in line.lower()] | |
| ) | |
| def read_nasa_zone_file(path): | |
| """Parse a NASA Tecplot-zone .dat file into a dict {zone: {var: array}}.""" | |
| with open(path) as f: | |
| data = f.read() | |
| data_lower = data.lower() | |
| var_start = data_lower.find('variables') | |
| var_end = var_start + data_lower[var_start:].find('\n') | |
| var_line = data[var_start:var_end] | |
| variables = var_line.split('"')[1::2] | |
| zones = [] | |
| zone_inds = [] | |
| data_start_inds = [] | |
| find_data_start = False | |
| if 'zone' not in data_lower: | |
| zones.append('zone') | |
| zone_inds.append(0) | |
| find_data_start = True | |
| lines = data.split('\n') | |
| lower_lines = data_lower.split('\n') | |
| for i, (line, lower) in enumerate(zip(lines, lower_lines)): | |
| if 'zone t=' in lower or 'zone, t=' in lower: | |
| zones.append(line.split('"')[1]) | |
| zone_inds.append(i) | |
| find_data_start = True | |
| if find_data_start and not _text_in_line(line): | |
| data_start_inds.append(i) | |
| find_data_start = False | |
| zone_inds.append(i + 1) | |
| out = {} | |
| for i, zone in enumerate(zones): | |
| buff = np.genfromtxt( | |
| path, | |
| skip_header=data_start_inds[i], | |
| max_rows=zone_inds[i + 1] - data_start_inds[i], | |
| ) | |
| if buff.ndim == 1: | |
| buff = buff.reshape(1, -1) | |
| out[zone] = {variables[j]: buff[:, j] for j in range(len(variables))} | |
| return out | |
| def dat_to_dataframe(path): | |
| zones = read_nasa_zone_file(path) | |
| frames = [] | |
| for zone_name, var_dict in zones.items(): | |
| df = pd.DataFrame(var_dict) | |
| df.insert(0, 'zone', zone_name) | |
| frames.append(df) | |
| return pd.concat(frames, ignore_index=True) | |
| def convert_directory(directory): | |
| dat_files = sorted(glob.glob(os.path.join(directory, '*.dat'))) | |
| if not dat_files: | |
| print(f' (no .dat files in {directory})') | |
| return | |
| for dat_path in dat_files: | |
| try: | |
| df = dat_to_dataframe(dat_path) | |
| except Exception as exc: | |
| print(f' FAILED {dat_path}: {exc}') | |
| continue | |
| csv_path = dat_path[:-4] + '.csv' | |
| df.to_csv(csv_path, index=False) | |
| print(f' {os.path.basename(dat_path)} -> {os.path.basename(csv_path)}' | |
| f' ({len(df)} rows, {len(df.columns)} cols)') | |
| if __name__ == '__main__': | |
| if len(sys.argv) < 2: | |
| print(__doc__) | |
| sys.exit(1) | |
| for d in sys.argv[1:]: | |
| print(f'\n[{d}]') | |
| convert_directory(d) | |