Dbl-dump

From Arbeitsgruppe Kuiper
Revision as of 15:51, 20 July 2026 by Lothar.brendel (talk | contribs) (new)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The script can be useful e.g. when plotting data from the dbl-files on the fly. It reads 1D or 2D dbl-files from a data-directory and writes an ASCII table to stdout. More than one dbl-file (from the same grid) can be specified as arguments, producing a table with more than two columns.

The first argument is a "selector" specifying the location of the cut in the 2D case: ix1, or just ix1 yields a cut with fixed \(x_1\)-coordinate, while ,ix2 produces one with fixed \(x_2\)-coordinate. The integers ix1 and ix2, respectively, denote the corresponding cell-number. (In the 1D case, the selector can be any character.)

#!/usr/bin/env python3                                                                                                                                                                        

# L.B. 2026                                                                                                                                                                                   

import os
from sys import argv,stdout,stderr
import numpy as np

if len(argv) < 3:
    print(f"usage: {os.path.basename(argv[0])} <selector> <dbl-file1> ...",file=stderr)
    exit(1)

sel = argv[1].split(',')
dbl_fn = argv[2]
dbl_dir = os.path.dirname(dbl_fn)
if not dbl_dir:
    dbl_dir="."
grid_fn = dbl_dir+"/grid.out"
time_fn = dbl_dir+"/dbl.out"


# parse "dbl.out" for time                                                                                                                                                                    
t = {}
with open(time_fn) as time:
    for line in time:
        line = line.split()
        t[int(line[0])] = float(line[1])


# pre-parse "grid.out"                                                                                                                                                                        
with open(grid_fn) as grid:
    dims = 0
    shape = []
    skip = []
    for k,line in enumerate(grid):
        if line[0] == "#":
            if "DIMENSIONS" in line:
                dims = int(line.split()[-1])
        elif line[0] in "0123456789":
            line = line.split()
            if len(line) == 1 and line[0] != "1":
                skip.append(k+1)
                shape.append(int(line[0]))


# read grid from "grid.out"                                                                                                                                                                   
pos = []
for skip,n in zip(skip,shape):
    axis = np.loadtxt(grid_fn,skiprows=skip,max_rows=n)
    pos.append(axis[:,1:].mean(1))


# read data file(s)                                                                                                                                                                           
for dbl_fn in argv[2:]:

    data = np.fromfile(dbl_fn,dtype="<f8").reshape(list(reversed(shape))).transpose()

    it = int(dbl_fn.split('.')[-2])
    header = f"# {os.path.basename(dbl_fn)}\n# t = {t[it]}\n"

    if dims==1:
        data = np.vstack((pos[0],data)).transpose()
    elif dims==2:
        if len(sel)==1 or sel[1]=="":  # f(y), x fixed                                                                                                                                        
            ix1 = int(sel[0])
            header += f"# x1 = {pos[0][ix1]}"
            data = np.vstack((pos[1],data[ix1])).transpose()
        else:
            ix2 = int(sel[1])
            header += f"# x2 = {pos[1][ix2]}"
            data = np.vstack((pos[0],data[:,ix2])).transpose()
    else:
        print(f"{argv[0]}: {dbl_fn} is on a {dims}D grid, can only handle 1D and 2D.",file=stderr)
        exit(1)

    print(header)
    np.savetxt(stdout,data,delimiter='\t')
    print()